blob: 47552f40ebc0dfad3bebcdaae2e3550488113091 [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 Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 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>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010059#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040060#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030061#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040066static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067
68static int
69sigchld_handler(int signal_number, void *data)
70{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 int status;
73 pid_t pid;
74
Pekka Paalanen23ade622014-08-27 13:31:26 +030075 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
76 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
Bill Spitzak027b9622012-03-17 15:22:03 -070080
Pekka Paalanen23ade622014-08-27 13:31:26 +030081 if (&p->link == &child_process_list) {
82 weston_log("unknown child process exited\n");
83 continue;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040088 }
89
Pekka Paalanen23ade622014-08-27 13:31:26 +030090 if (pid < 0 && errno != ECHILD)
91 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93 return 1;
94}
95
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096static void
Alexander Larsson0b135062013-05-28 16:23:36 +020097weston_output_transform_scale_init(struct weston_output *output,
98 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020099
Rob Bradford27b17932013-06-26 18:08:46 +0100100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500101weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100102
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600103static void weston_mode_switch_finish(struct weston_output *output,
104 int mode_changed,
105 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800106{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200108 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600110 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200116 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Derek Foremandd4cd332014-11-10 10:29:59 -0600153 if (!mode_changed && !scale_changed)
154 return;
155
Hardening57388e42013-09-18 23:56:36 +0200156 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600157 wl_resource_for_each(resource, &output->resource_list) {
158 if (mode_changed) {
159 wl_output_send_mode(resource,
160 output->current_mode->flags,
161 output->current_mode->width,
162 output->current_mode->height,
163 output->current_mode->refresh);
164 }
Hardening57388e42013-09-18 23:56:36 +0200165
Derek Foreman41bdc272014-11-05 13:26:57 -0600166 version = wl_resource_get_version(resource);
167 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200169
Derek Foreman41bdc272014-11-05 13:26:57 -0600170 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
171 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600172 }
173}
174
175WL_EXPORT int
176weston_output_mode_set_native(struct weston_output *output,
177 struct weston_mode *mode,
178 int32_t scale)
179{
180 int ret;
181 int mode_changed = 0, scale_changed = 0;
182
183 if (!output->switch_mode)
184 return -1;
185
186 if (!output->original_mode) {
187 mode_changed = 1;
188 ret = output->switch_mode(output, mode);
189 if (ret < 0)
190 return ret;
191 if (output->current_scale != scale) {
192 scale_changed = 1;
193 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200194 }
195 }
196
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
202 return 0;
203}
204
205WL_EXPORT int
206weston_output_mode_switch_to_native(struct weston_output *output)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
210
211 if (!output->switch_mode)
212 return -1;
213
214 if (!output->original_mode) {
215 weston_log("already in the native mode\n");
216 return -1;
217 }
218 /* the non fullscreen clients haven't seen a mode set since we
219 * switched into a temporary, so we need to notify them if the
220 * mode at that time is different from the native mode now.
221 */
222 mode_changed = (output->original_mode != output->native_mode);
223 scale_changed = (output->original_scale != output->native_scale);
224
225 ret = output->switch_mode(output, output->native_mode);
226 if (ret < 0)
227 return ret;
228
229 output->current_scale = output->native_scale;
230
231 output->original_mode = NULL;
232 output->original_scale = 0;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
236 return 0;
237}
238
239WL_EXPORT int
240weston_output_mode_switch_to_temporary(struct weston_output *output,
241 struct weston_mode *mode,
242 int32_t scale)
243{
244 int ret;
245
246 if (!output->switch_mode)
247 return -1;
248
249 /* original_mode is the last mode non full screen clients have seen,
250 * so we shouldn't change it if we already have one set.
251 */
252 if (!output->original_mode) {
253 output->original_mode = output->native_mode;
254 output->original_scale = output->native_scale;
255 }
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259
260 output->current_scale = scale;
261
262 weston_mode_switch_finish(output, 0, 0);
263
264 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800265}
266
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400269{
270 wl_list_insert(&child_process_list, &process->link);
271}
272
Benjamin Franzke06286262011-05-06 19:12:33 +0200273static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274child_client_exec(int sockfd, const char *path)
275{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500276 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200278 sigset_t allsigs;
279
280 /* do not give our signal mask to the new process */
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100284 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
285 if (seteuid(getuid()) == -1) {
286 weston_log("compositor: failed seteuid\n");
287 return;
288 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500289
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
291 * non-CLOEXEC fd to pass through exec. */
292 clientfd = dup(sockfd);
293 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500295 return;
296 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 setenv("WAYLAND_SOCKET", s, 1);
300
301 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 path);
304}
305
306WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500307weston_client_launch(struct weston_compositor *compositor,
308 struct weston_process *proc,
309 const char *path,
310 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311{
312 int sv[2];
313 pid_t pid;
314 struct wl_client *client;
315
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300316 weston_log("launching '%s'\n", path);
317
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300318 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320 "socketpair failed while launching '%s': %m\n",
321 path);
322 return NULL;
323 }
324
325 pid = fork();
326 if (pid == -1) {
327 close(sv[0]);
328 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 "fork failed while launching '%s': %m\n", path);
331 return NULL;
332 }
333
334 if (pid == 0) {
335 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700336 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337 }
338
339 close(sv[1]);
340
341 client = wl_client_create(compositor->wl_display, sv[0]);
342 if (!client) {
343 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "wl_client_create failed while launching '%s'.\n",
346 path);
347 return NULL;
348 }
349
350 proc->pid = pid;
351 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200353
354 return client;
355}
356
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300357struct process_info {
358 struct weston_process proc;
359 char *path;
360};
361
362static void
363process_handle_sigchld(struct weston_process *process, int status)
364{
365 struct process_info *pinfo =
366 container_of(process, struct process_info, proc);
367
368 /*
369 * There are no guarantees whether this runs before or after
370 * the wl_client destructor.
371 */
372
373 if (WIFEXITED(status)) {
374 weston_log("%s exited with status %d\n", pinfo->path,
375 WEXITSTATUS(status));
376 } else if (WIFSIGNALED(status)) {
377 weston_log("%s died on signal %d\n", pinfo->path,
378 WTERMSIG(status));
379 } else {
380 weston_log("%s disappeared\n", pinfo->path);
381 }
382
383 free(pinfo->path);
384 free(pinfo);
385}
386
387WL_EXPORT struct wl_client *
388weston_client_start(struct weston_compositor *compositor, const char *path)
389{
390 struct process_info *pinfo;
391 struct wl_client *client;
392
393 pinfo = zalloc(sizeof *pinfo);
394 if (!pinfo)
395 return NULL;
396
397 pinfo->path = strdup(path);
398 if (!pinfo->path)
399 goto out_free;
400
401 client = weston_client_launch(compositor, &pinfo->proc, path,
402 process_handle_sigchld);
403 if (!client)
404 goto out_str;
405
406 return client;
407
408out_str:
409 free(pinfo->path);
410
411out_free:
412 free(pinfo);
413
414 return NULL;
415}
416
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200417static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300418region_init_infinite(pixman_region32_t *region)
419{
420 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
421 UINT32_MAX, UINT32_MAX);
422}
423
Pekka Paalanene67858b2013-04-25 13:57:42 +0300424static struct weston_subsurface *
425weston_surface_to_subsurface(struct weston_surface *surface);
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427WL_EXPORT struct weston_view *
428weston_view_create(struct weston_surface *surface)
429{
430 struct weston_view *view;
431
Bryce Harringtonde16d892014-11-20 22:21:57 -0800432 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 if (view == NULL)
434 return NULL;
435
436 view->surface = surface;
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 /* Assign to surface */
439 wl_list_insert(&surface->views, &view->surface_link);
440
441 wl_signal_init(&view->destroy_signal);
442 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300443 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 pixman_region32_init(&view->clip);
446
447 view->alpha = 1.0;
448 pixman_region32_init(&view->transform.opaque);
449
450 wl_list_init(&view->geometry.transformation_list);
451 wl_list_insert(&view->geometry.transformation_list,
452 &view->transform.position.link);
453 weston_matrix_init(&view->transform.position.matrix);
454 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200455 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500456 pixman_region32_init(&view->transform.boundingbox);
457 view->transform.dirty = 1;
458
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 return view;
460}
461
Jason Ekstrand108865d2014-06-26 10:04:49 -0700462struct weston_frame_callback {
463 struct wl_resource *resource;
464 struct wl_list link;
465};
466
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467struct weston_presentation_feedback {
468 struct wl_resource *resource;
469
470 /* XXX: could use just wl_resource_get_link() instead */
471 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200472
473 /* The per-surface feedback flags */
474 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475};
476
477static void
478weston_presentation_feedback_discard(
479 struct weston_presentation_feedback *feedback)
480{
481 presentation_feedback_send_discarded(feedback->resource);
482 wl_resource_destroy(feedback->resource);
483}
484
485static void
486weston_presentation_feedback_discard_list(struct wl_list *list)
487{
488 struct weston_presentation_feedback *feedback, *tmp;
489
490 wl_list_for_each_safe(feedback, tmp, list, link)
491 weston_presentation_feedback_discard(feedback);
492}
493
494static void
495weston_presentation_feedback_present(
496 struct weston_presentation_feedback *feedback,
497 struct weston_output *output,
498 uint32_t refresh_nsec,
499 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200500 uint64_t seq,
501 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502{
503 struct wl_client *client = wl_resource_get_client(feedback->resource);
504 struct wl_resource *o;
505 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506
507 wl_resource_for_each(o, &output->resource_list) {
508 if (wl_resource_get_client(o) != client)
509 continue;
510
511 presentation_feedback_send_sync_output(feedback->resource, o);
512 }
513
514 secs = ts->tv_sec;
515 presentation_feedback_send_presented(feedback->resource,
516 secs >> 32, secs & 0xffffffff,
517 ts->tv_nsec,
518 refresh_nsec,
519 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200520 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400521 wl_resource_destroy(feedback->resource);
522}
523
524static void
525weston_presentation_feedback_present_list(struct wl_list *list,
526 struct weston_output *output,
527 uint32_t refresh_nsec,
528 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200529 uint64_t seq,
530 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400531{
532 struct weston_presentation_feedback *feedback, *tmp;
533
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200534 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
535 wl_list_empty(list));
536
Pekka Paalanen133e4392014-09-23 22:08:46 -0400537 wl_list_for_each_safe(feedback, tmp, list, link)
538 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200539 refresh_nsec, ts, seq,
540 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400541}
542
Jason Ekstrand7b982072014-05-20 14:33:03 -0500543static void
544surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
545{
546 struct weston_surface_state *state =
547 container_of(listener, struct weston_surface_state,
548 buffer_destroy_listener);
549
550 state->buffer = NULL;
551}
552
553static void
554weston_surface_state_init(struct weston_surface_state *state)
555{
556 state->newly_attached = 0;
557 state->buffer = NULL;
558 state->buffer_destroy_listener.notify =
559 surface_state_handle_buffer_destroy;
560 state->sx = 0;
561 state->sy = 0;
562
563 pixman_region32_init(&state->damage);
564 pixman_region32_init(&state->opaque);
565 region_init_infinite(&state->input);
566
567 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400568 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500569
570 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
571 state->buffer_viewport.buffer.scale = 1;
572 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
573 state->buffer_viewport.surface.width = -1;
574 state->buffer_viewport.changed = 0;
575}
576
577static void
578weston_surface_state_fini(struct weston_surface_state *state)
579{
580 struct weston_frame_callback *cb, *next;
581
582 wl_list_for_each_safe(cb, next,
583 &state->frame_callback_list, link)
584 wl_resource_destroy(cb->resource);
585
Pekka Paalanen133e4392014-09-23 22:08:46 -0400586 weston_presentation_feedback_discard_list(&state->feedback_list);
587
Jason Ekstrand7b982072014-05-20 14:33:03 -0500588 pixman_region32_fini(&state->input);
589 pixman_region32_fini(&state->opaque);
590 pixman_region32_fini(&state->damage);
591
592 if (state->buffer)
593 wl_list_remove(&state->buffer_destroy_listener.link);
594 state->buffer = NULL;
595}
596
597static void
598weston_surface_state_set_buffer(struct weston_surface_state *state,
599 struct weston_buffer *buffer)
600{
601 if (state->buffer == buffer)
602 return;
603
604 if (state->buffer)
605 wl_list_remove(&state->buffer_destroy_listener.link);
606 state->buffer = buffer;
607 if (state->buffer)
608 wl_signal_add(&state->buffer->destroy_signal,
609 &state->buffer_destroy_listener);
610}
611
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500613weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500614{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500615 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400616
Bryce Harringtonde16d892014-11-20 22:21:57 -0800617 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400618 if (surface == NULL)
619 return NULL;
620
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500621 wl_signal_init(&surface->destroy_signal);
622
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500623 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200624 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400625
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200626 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
627 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200628 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
629 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500630
631 weston_surface_state_init(&surface->pending);
632
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400633 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500634 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300635 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400636
Jason Ekstranda7af7042013-10-12 22:38:11 -0500637 wl_list_init(&surface->views);
638
639 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400640 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500641
Pekka Paalanene67858b2013-04-25 13:57:42 +0300642 wl_list_init(&surface->subsurface_list);
643 wl_list_init(&surface->subsurface_list_pending);
644
Jason Ekstrand1e059042014-10-16 10:55:19 -0500645 weston_matrix_init(&surface->buffer_to_surface_matrix);
646 weston_matrix_init(&surface->surface_to_buffer_matrix);
647
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400648 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500649}
650
Alex Wu8811bf92012-02-28 18:07:54 +0800651WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200653 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500654{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100655 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500656}
657
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659weston_view_to_global_float(struct weston_view *view,
660 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200661{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
664
Jason Ekstranda7af7042013-10-12 22:38:11 -0500665 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200666
667 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200668 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700669 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200670 v.f[3]);
671 *x = 0;
672 *y = 0;
673 return;
674 }
675
676 *x = v.f[0] / v.f[3];
677 *y = v.f[1] / v.f[3];
678 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500679 *x = sx + view->geometry.x;
680 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200681 }
682}
683
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500684WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685weston_transformed_coord(int width, int height,
686 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200687 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200688 float sx, float sy, float *bx, float *by)
689{
690 switch (transform) {
691 case WL_OUTPUT_TRANSFORM_NORMAL:
692 default:
693 *bx = sx;
694 *by = sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_FLIPPED:
697 *bx = width - sx;
698 *by = sy;
699 break;
700 case WL_OUTPUT_TRANSFORM_90:
701 *bx = height - sy;
702 *by = sx;
703 break;
704 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
705 *bx = height - sy;
706 *by = width - sx;
707 break;
708 case WL_OUTPUT_TRANSFORM_180:
709 *bx = width - sx;
710 *by = height - sy;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
713 *bx = sx;
714 *by = height - sy;
715 break;
716 case WL_OUTPUT_TRANSFORM_270:
717 *bx = sy;
718 *by = width - sx;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
721 *bx = sy;
722 *by = sx;
723 break;
724 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200725
726 *bx *= scale;
727 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200728}
729
730WL_EXPORT pixman_box32_t
731weston_transformed_rect(int width, int height,
732 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200733 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200734 pixman_box32_t rect)
735{
736 float x1, x2, y1, y2;
737
738 pixman_box32_t ret;
739
Alexander Larsson4ea95522013-05-22 14:41:37 +0200740 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200742 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200743 rect.x2, rect.y2, &x2, &y2);
744
745 if (x1 <= x2) {
746 ret.x1 = x1;
747 ret.x2 = x2;
748 } else {
749 ret.x1 = x2;
750 ret.x2 = x1;
751 }
752
753 if (y1 <= y2) {
754 ret.y1 = y1;
755 ret.y2 = y2;
756 } else {
757 ret.y1 = y2;
758 ret.y2 = y1;
759 }
760
761 return ret;
762}
763
764WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500765weston_transformed_region(int width, int height,
766 enum wl_output_transform transform,
767 int32_t scale,
768 pixman_region32_t *src, pixman_region32_t *dest)
769{
770 pixman_box32_t *src_rects, *dest_rects;
771 int nrects, i;
772
773 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
774 if (src != dest)
775 pixman_region32_copy(dest, src);
776 return;
777 }
778
779 src_rects = pixman_region32_rectangles(src, &nrects);
780 dest_rects = malloc(nrects * sizeof(*dest_rects));
781 if (!dest_rects)
782 return;
783
784 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
785 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
786 } else {
787 for (i = 0; i < nrects; i++) {
788 switch (transform) {
789 default:
790 case WL_OUTPUT_TRANSFORM_NORMAL:
791 dest_rects[i].x1 = src_rects[i].x1;
792 dest_rects[i].y1 = src_rects[i].y1;
793 dest_rects[i].x2 = src_rects[i].x2;
794 dest_rects[i].y2 = src_rects[i].y2;
795 break;
796 case WL_OUTPUT_TRANSFORM_90:
797 dest_rects[i].x1 = height - src_rects[i].y2;
798 dest_rects[i].y1 = src_rects[i].x1;
799 dest_rects[i].x2 = height - src_rects[i].y1;
800 dest_rects[i].y2 = src_rects[i].x2;
801 break;
802 case WL_OUTPUT_TRANSFORM_180:
803 dest_rects[i].x1 = width - src_rects[i].x2;
804 dest_rects[i].y1 = height - src_rects[i].y2;
805 dest_rects[i].x2 = width - src_rects[i].x1;
806 dest_rects[i].y2 = height - src_rects[i].y1;
807 break;
808 case WL_OUTPUT_TRANSFORM_270:
809 dest_rects[i].x1 = src_rects[i].y1;
810 dest_rects[i].y1 = width - src_rects[i].x2;
811 dest_rects[i].x2 = src_rects[i].y2;
812 dest_rects[i].y2 = width - src_rects[i].x1;
813 break;
814 case WL_OUTPUT_TRANSFORM_FLIPPED:
815 dest_rects[i].x1 = width - src_rects[i].x2;
816 dest_rects[i].y1 = src_rects[i].y1;
817 dest_rects[i].x2 = width - src_rects[i].x1;
818 dest_rects[i].y2 = src_rects[i].y2;
819 break;
820 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
821 dest_rects[i].x1 = height - src_rects[i].y2;
822 dest_rects[i].y1 = width - src_rects[i].x2;
823 dest_rects[i].x2 = height - src_rects[i].y1;
824 dest_rects[i].y2 = width - src_rects[i].x1;
825 break;
826 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = height - src_rects[i].y2;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = height - src_rects[i].y1;
831 break;
832 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = src_rects[i].x1;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = src_rects[i].x2;
837 break;
838 }
839 }
840 }
841
842 if (scale != 1) {
843 for (i = 0; i < nrects; i++) {
844 dest_rects[i].x1 *= scale;
845 dest_rects[i].x2 *= scale;
846 dest_rects[i].y1 *= scale;
847 dest_rects[i].y2 *= scale;
848 }
849 }
850
851 pixman_region32_clear(dest);
852 pixman_region32_init_rects(dest, dest_rects, nrects);
853 free(dest_rects);
854}
855
Jonny Lamb74130762013-11-26 18:19:46 +0100856static void
857scaler_surface_to_buffer(struct weston_surface *surface,
858 float sx, float sy, float *bx, float *by)
859{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 double src_width, src_height;
862 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200863
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200864 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
865 if (vp->surface.width == -1) {
866 *bx = sx;
867 *by = sy;
868 return;
869 }
Jonny Lamb74130762013-11-26 18:19:46 +0100870
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200871 src_x = 0.0;
872 src_y = 0.0;
873 src_width = surface->width_from_buffer;
874 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100875 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200876 src_x = wl_fixed_to_double(vp->buffer.src_x);
877 src_y = wl_fixed_to_double(vp->buffer.src_y);
878 src_width = wl_fixed_to_double(vp->buffer.src_width);
879 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100880 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200881
882 *bx = sx * src_width / surface->width + src_x;
883 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100884}
885
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500886WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200887weston_surface_to_buffer_float(struct weston_surface *surface,
888 float sx, float sy, float *bx, float *by)
889{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200890 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892 /* first transform coordinates if the scaler is set */
893 scaler_surface_to_buffer(surface, sx, sy, bx, by);
894
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500895 weston_transformed_coord(surface->width_from_buffer,
896 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200897 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100898 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899}
900
Alexander Larsson4ea95522013-05-22 14:41:37 +0200901WL_EXPORT void
902weston_surface_to_buffer(struct weston_surface *surface,
903 int sx, int sy, int *bx, int *by)
904{
905 float bxf, byf;
906
Jonny Lamb74130762013-11-26 18:19:46 +0100907 weston_surface_to_buffer_float(surface,
908 sx, sy, &bxf, &byf);
909
Alexander Larsson4ea95522013-05-22 14:41:37 +0200910 *bx = floorf(bxf);
911 *by = floorf(byf);
912}
913
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200914WL_EXPORT pixman_box32_t
915weston_surface_to_buffer_rect(struct weston_surface *surface,
916 pixman_box32_t rect)
917{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200918 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100919 float xf, yf;
920
921 /* first transform box coordinates if the scaler is set */
922 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
923 rect.x1 = floorf(xf);
924 rect.y1 = floorf(yf);
925
926 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
927 rect.x2 = floorf(xf);
928 rect.y2 = floorf(yf);
929
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500930 return weston_transformed_rect(surface->width_from_buffer,
931 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200932 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200933 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200934}
935
936WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938 struct weston_plane *plane)
939{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400941 return;
942
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 weston_view_damage_below(view);
944 view->plane = plane;
945 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400946}
947
Pekka Paalanen51723d52015-02-17 13:10:01 +0200948/** Inflict damage on the plane where the view is visible.
949 *
950 * \param view The view that causes the damage.
951 *
952 * If the view is currently on a plane (including the primary plane),
953 * take the view's boundingbox, subtract all the opaque views that cover it,
954 * and add the remaining region as damage to the plane. This corresponds
955 * to the damage inflicted to the plane if this view disappeared.
956 *
957 * A repaint is scheduled for this view.
958 *
959 * The region of all opaque views covering this view is stored in
960 * weston_view::clip and updated by view_accumulate_damage() during
961 * weston_output_repaint(). Specifically, that region matches the
962 * scenegraph as it was last painted.
963 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400964WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200966{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500967 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200968
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500969 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200970 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800972 if (view->plane)
973 pixman_region32_union(&view->plane->damage,
974 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500975 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800976 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200977}
978
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400979static void
980weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
981{
982 uint32_t different = es->output_mask ^ mask;
983 uint32_t entered = mask & different;
984 uint32_t left = es->output_mask & different;
985 struct weston_output *output;
986 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500987 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400988
989 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500990 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400991 return;
992 if (different == 0)
993 return;
994
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500995 client = wl_resource_get_client(es->resource);
996
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400997 wl_list_for_each(output, &es->compositor->output_list, link) {
998 if (1 << output->id & different)
999 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001000 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001001 client);
1002 if (resource == NULL)
1003 continue;
1004 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001005 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001006 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001007 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001008 }
1009}
1010
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001011
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001012static void
1013weston_surface_assign_output(struct weston_surface *es)
1014{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 struct weston_output *new_output;
1016 struct weston_view *view;
1017 pixman_region32_t region;
1018 uint32_t max, area, mask;
1019 pixman_box32_t *e;
1020
1021 new_output = NULL;
1022 max = 0;
1023 mask = 0;
1024 pixman_region32_init(&region);
1025 wl_list_for_each(view, &es->views, surface_link) {
1026 if (!view->output)
1027 continue;
1028
1029 pixman_region32_intersect(&region, &view->transform.boundingbox,
1030 &view->output->region);
1031
1032 e = pixman_region32_extents(&region);
1033 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1034
1035 mask |= view->output_mask;
1036
1037 if (area >= max) {
1038 new_output = view->output;
1039 max = area;
1040 }
1041 }
1042 pixman_region32_fini(&region);
1043
1044 es->output = new_output;
1045 weston_surface_update_output_mask(es, mask);
1046}
1047
1048static void
1049weston_view_assign_output(struct weston_view *ev)
1050{
1051 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001052 struct weston_output *output, *new_output;
1053 pixman_region32_t region;
1054 uint32_t max, area, mask;
1055 pixman_box32_t *e;
1056
1057 new_output = NULL;
1058 max = 0;
1059 mask = 0;
1060 pixman_region32_init(&region);
1061 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001063 &output->region);
1064
1065 e = pixman_region32_extents(&region);
1066 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1067
1068 if (area > 0)
1069 mask |= 1 << output->id;
1070
1071 if (area >= max) {
1072 new_output = output;
1073 max = area;
1074 }
1075 }
1076 pixman_region32_fini(&region);
1077
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 ev->output = new_output;
1079 ev->output_mask = mask;
1080
1081 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001082}
1083
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001084static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001085weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1086 int from_x, int from_y, int *to_x, int *to_y)
1087{
1088 float x, y;
1089
1090 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1091 weston_view_from_global_float(to, x, y, &x, &y);
1092
1093 *to_x = round(x);
1094 *to_y = round(y);
1095}
1096
1097static void
1098weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1099{
1100 pixman_box32_t *a;
1101 pixman_box32_t b;
1102
1103 a = pixman_region32_extents(&from->geometry.scissor);
1104
1105 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1106 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1107
1108 pixman_region32_fini(&to->geometry.scissor);
1109 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1110}
1111
1112static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001113view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001115{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001116 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1117 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001118 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001119 { inbox->x1, inbox->y1 },
1120 { inbox->x1, inbox->y2 },
1121 { inbox->x2, inbox->y1 },
1122 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001123 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001124 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001125 int i;
1126
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001127 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001128 /* avoid rounding empty bbox to 1x1 */
1129 pixman_region32_init(bbox);
1130 return;
1131 }
1132
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001133 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001134 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001136 if (x < min_x)
1137 min_x = x;
1138 if (x > max_x)
1139 max_x = x;
1140 if (y < min_y)
1141 min_y = y;
1142 if (y > max_y)
1143 max_y = y;
1144 }
1145
Pekka Paalanen219b9822012-02-08 15:38:37 +02001146 int_x = floorf(min_x);
1147 int_y = floorf(min_y);
1148 pixman_region32_init_rect(bbox, int_x, int_y,
1149 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001150}
1151
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001152static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001154{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001157 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 view->geometry.x = roundf(view->geometry.x);
1159 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001160
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001161 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001162 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1163 view->transform.position.matrix.d[12] = view->geometry.x;
1164 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001167
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 view->transform.inverse = view->transform.position.matrix;
1169 view->transform.inverse.d[12] = -view->geometry.x;
1170 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001171
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001173 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001174 view->surface->width,
1175 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001176 if (view->geometry.scissor_enabled)
1177 pixman_region32_intersect(&view->transform.boundingbox,
1178 &view->transform.boundingbox,
1179 &view->geometry.scissor);
1180
1181 pixman_region32_translate(&view->transform.boundingbox,
1182 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001183
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 if (view->alpha == 1.0) {
1185 pixman_region32_copy(&view->transform.opaque,
1186 &view->surface->opaque);
1187 pixman_region32_translate(&view->transform.opaque,
1188 view->geometry.x,
1189 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001190 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001191}
1192
1193static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 struct weston_view *parent = view->geometry.parent;
1197 struct weston_matrix *matrix = &view->transform.matrix;
1198 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001199 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001200 pixman_region32_t surfregion;
1201 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001204
1205 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1207 view->transform.position.matrix.d[12] = view->geometry.x;
1208 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001209
1210 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212 weston_matrix_multiply(matrix, &tform->matrix);
1213
Pekka Paalanen483243f2013-03-08 14:56:50 +02001214 if (parent)
1215 weston_matrix_multiply(matrix, &parent->transform.matrix);
1216
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001217 if (weston_matrix_invert(inverse, matrix) < 0) {
1218 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 weston_log("error: weston_view %p"
1220 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001221 return -1;
1222 }
1223
Pekka Paalanen380adf52015-02-16 14:39:11 +02001224 pixman_region32_init_rect(&surfregion, 0, 0,
1225 view->surface->width, view->surface->height);
1226 if (view->geometry.scissor_enabled)
1227 pixman_region32_intersect(&surfregion, &surfregion,
1228 &view->geometry.scissor);
1229 surfbox = pixman_region32_extents(&surfregion);
1230
1231 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1232 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001233
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001234 return 0;
1235}
1236
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001237static struct weston_layer *
1238get_view_layer(struct weston_view *view)
1239{
1240 if (view->parent_view)
1241 return get_view_layer(view->parent_view);
1242 return view->layer_link.layer;
1243}
1244
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001245WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001247{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001249 struct weston_layer *layer;
1250 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001251
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001253 return;
1254
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001259
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 pixman_region32_fini(&view->transform.boundingbox);
1263 pixman_region32_fini(&view->transform.opaque);
1264 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001265
Pekka Paalanencd403622012-01-25 13:37:39 +02001266 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 if (view->geometry.transformation_list.next ==
1268 &view->transform.position.link &&
1269 view->geometry.transformation_list.prev ==
1270 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001271 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001273 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 if (weston_view_update_transform_enable(view) < 0)
1275 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001276 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001277
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001278 layer = get_view_layer(view);
1279 if (layer) {
1280 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001281 pixman_region32_intersect(&view->transform.boundingbox,
1282 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001283 pixman_region32_intersect(&view->transform.opaque,
1284 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001285 pixman_region32_fini(&mask);
1286 }
1287
Pekka Paalanen380adf52015-02-16 14:39:11 +02001288 if (parent) {
1289 if (parent->geometry.scissor_enabled) {
1290 view->geometry.scissor_enabled = true;
1291 weston_view_transfer_scissor(parent, view);
1292 } else {
1293 view->geometry.scissor_enabled = false;
1294 }
1295 }
1296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001298
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 wl_signal_emit(&view->surface->compositor->transform_signal,
1302 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001303}
1304
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001305WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001307{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001309
1310 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 * The invariant: if view->geometry.dirty, then all views
1312 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313 * Corollary: if not parent->geometry.dirty, then all ancestors
1314 * are not dirty.
1315 */
1316
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001318 return;
1319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001323 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001324 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001325}
1326
1327WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328weston_view_to_global_fixed(struct weston_view *view,
1329 wl_fixed_t vx, wl_fixed_t vy,
1330 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001331{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001332 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001333
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 weston_view_to_global_float(view,
1335 wl_fixed_to_double(vx),
1336 wl_fixed_to_double(vy),
1337 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001338 *x = wl_fixed_from_double(xf);
1339 *y = wl_fixed_from_double(yf);
1340}
1341
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001342WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343weston_view_from_global_float(struct weston_view *view,
1344 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001345{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001347 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1348
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001350
1351 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001352 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001354 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 *vx = 0;
1356 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001357 return;
1358 }
1359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 *vx = v.f[0] / v.f[3];
1361 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001362 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 *vx = x - view->geometry.x;
1364 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001365 }
1366}
1367
1368WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369weston_view_from_global_fixed(struct weston_view *view,
1370 wl_fixed_t x, wl_fixed_t y,
1371 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001372{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 weston_view_from_global_float(view,
1376 wl_fixed_to_double(x),
1377 wl_fixed_to_double(y),
1378 &vxf, &vyf);
1379 *vx = wl_fixed_from_double(vxf);
1380 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001381}
1382
1383WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384weston_view_from_global(struct weston_view *view,
1385 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001386{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1390 *vx = floorf(vxf);
1391 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001392}
1393
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001394WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001395weston_surface_schedule_repaint(struct weston_surface *surface)
1396{
1397 struct weston_output *output;
1398
1399 wl_list_for_each(output, &surface->compositor->output_list, link)
1400 if (surface->output_mask & (1 << output->id))
1401 weston_output_schedule_repaint(output);
1402}
1403
1404WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405weston_view_schedule_repaint(struct weston_view *view)
1406{
1407 struct weston_output *output;
1408
1409 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1410 if (view->output_mask & (1 << output->id))
1411 weston_output_schedule_repaint(output);
1412}
1413
Pekka Paalanene508ce62015-02-19 13:59:55 +02001414/**
1415 * XXX: This function does it the wrong way.
1416 * surface->damage is the damage from the client, and causes
1417 * surface_flush_damage() to copy pixels. No window management action can
1418 * cause damage to the client-provided content, warranting re-upload!
1419 *
1420 * Instead of surface->damage, this function should record the damage
1421 * with all the views for this surface to avoid extraneous texture
1422 * uploads.
1423 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001424WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001426{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001427 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 0, 0, surface->width,
1429 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001430
Kristian Høgsberg98238702012-08-03 16:29:12 -04001431 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001432}
1433
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001434WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001436{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001437 if (view->geometry.x == x && view->geometry.y == y)
1438 return;
1439
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 view->geometry.x = x;
1441 view->geometry.y = y;
1442 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001443}
1444
Pekka Paalanen483243f2013-03-08 14:56:50 +02001445static void
1446transform_parent_handle_parent_destroy(struct wl_listener *listener,
1447 void *data)
1448{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 struct weston_view *view =
1450 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001451 geometry.parent_destroy_listener);
1452
Jason Ekstranda7af7042013-10-12 22:38:11 -05001453 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001454}
1455
1456WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001458 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001459{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460 if (view->geometry.parent) {
1461 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1462 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001463
1464 if (!parent)
1465 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001466 }
1467
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001469
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001471 transform_parent_handle_parent_destroy;
1472 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001473 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001475 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001477 }
1478
Jason Ekstranda7af7042013-10-12 22:38:11 -05001479 weston_view_geometry_dirty(view);
1480}
1481
Pekka Paalanen380adf52015-02-16 14:39:11 +02001482/** Set a clip mask rectangle on a view
1483 *
1484 * \param view The view to set the clip mask on.
1485 * \param x Top-left corner X coordinate of the clip rectangle.
1486 * \param y Top-left corner Y coordinate of the clip rectangle.
1487 * \param width Width of the clip rectangle, non-negative.
1488 * \param height Height of the clip rectangle, non-negative.
1489 *
1490 * A shell may set a clip mask rectangle on a view. Everything outside
1491 * the rectangle is cut away for input and output purposes: it is
1492 * not drawn and cannot be hit by hit-test based input like pointer
1493 * motion or touch-downs. Everything inside the rectangle will behave
1494 * normally. Clients are unaware of clipping.
1495 *
1496 * The rectangle is set in the surface local coordinates. Setting a clip
1497 * mask rectangle does not affect the view position, the view is positioned
1498 * as it would be without a clip. The clip also does not change
1499 * weston_surface::width,height.
1500 *
1501 * The clip mask rectangle is part of transformation inheritance
1502 * (weston_view_set_transform_parent()). A clip set in the root of the
1503 * transformation inheritance tree will affect all views in the tree.
1504 * A clip can be set only on the root view. Attempting to set a clip
1505 * on view that has a transformation parent will fail. Assigning a parent
1506 * to a view that has a clip set will cause the clip to be forgotten.
1507 *
1508 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1509 * on the additional transformations in the child views. These transformations
1510 * may not rotate the coordinate axes, i.e., only translation and scaling
1511 * are allowed. Violating this restriction causes the clipping to malfunction.
1512 * Furthermore, using scaling may cause rounding errors in child clipping.
1513 *
1514 * The clip mask rectangle is not automatically adjusted based on
1515 * wl_surface.attach dx and dy arguments.
1516 *
1517 * A clip mask rectangle can be set only if the compositor capability
1518 * WESTON_CAP_VIEW_CLIP_MASK is present.
1519 *
1520 * This function sets the clip mask rectangle and schedules a repaint for
1521 * the view.
1522 */
1523WL_EXPORT void
1524weston_view_set_mask(struct weston_view *view,
1525 int x, int y, int width, int height)
1526{
1527 struct weston_compositor *compositor = view->surface->compositor;
1528
1529 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1530 weston_log("%s not allowed without capability!\n", __func__);
1531 return;
1532 }
1533
1534 if (view->geometry.parent) {
1535 weston_log("view %p has a parent, clip forbidden!\n", view);
1536 return;
1537 }
1538
1539 if (width < 0 || height < 0) {
1540 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1541 x, y, width, height);
1542 return;
1543 }
1544
1545 pixman_region32_fini(&view->geometry.scissor);
1546 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1547 view->geometry.scissor_enabled = true;
1548 weston_view_geometry_dirty(view);
1549 weston_view_schedule_repaint(view);
1550}
1551
1552/** Remove the clip mask from a view
1553 *
1554 * \param view The view to remove the clip mask from.
1555 *
1556 * Removed the clip mask rectangle and schedules a repaint.
1557 *
1558 * \sa weston_view_set_mask
1559 */
1560WL_EXPORT void
1561weston_view_set_mask_infinite(struct weston_view *view)
1562{
1563 view->geometry.scissor_enabled = false;
1564 weston_view_geometry_dirty(view);
1565 weston_view_schedule_repaint(view);
1566}
1567
Derek Foreman280e7dd2014-10-03 13:13:42 -05001568WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569weston_view_is_mapped(struct weston_view *view)
1570{
1571 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001572 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001574 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001575}
1576
Derek Foreman280e7dd2014-10-03 13:13:42 -05001577WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001578weston_surface_is_mapped(struct weston_surface *surface)
1579{
1580 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001581 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001582 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001583 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001584}
1585
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001586static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001587surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001588{
1589 struct weston_view *view;
1590
1591 if (surface->width == width && surface->height == height)
1592 return;
1593
1594 surface->width = width;
1595 surface->height = height;
1596
1597 wl_list_for_each(view, &surface->views, surface_link)
1598 weston_view_geometry_dirty(view);
1599}
1600
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001601WL_EXPORT void
1602weston_surface_set_size(struct weston_surface *surface,
1603 int32_t width, int32_t height)
1604{
1605 assert(!surface->resource);
1606 surface_set_size(surface, width, height);
1607}
1608
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001609static int
1610fixed_round_up_to_int(wl_fixed_t f)
1611{
1612 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1613}
1614
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001615static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001616weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001617{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001618 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001619 int32_t width, height;
1620
1621 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001622 surface->width_from_buffer = 0;
1623 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001624 return;
1625 }
1626
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001627 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001628 case WL_OUTPUT_TRANSFORM_90:
1629 case WL_OUTPUT_TRANSFORM_270:
1630 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1631 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001632 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1633 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001634 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001635 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001636 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1637 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001638 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001639 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001640
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001641 surface->width_from_buffer = width;
1642 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001643}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001644
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001645static void
1646weston_surface_update_size(struct weston_surface *surface)
1647{
1648 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1649 int32_t width, height;
1650
1651 width = surface->width_from_buffer;
1652 height = surface->height_from_buffer;
1653
1654 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001655 surface_set_size(surface,
1656 vp->surface.width, vp->surface.height);
1657 return;
1658 }
1659
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001660 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001661 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1662 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1663
1664 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001665 return;
1666 }
1667
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001668 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001669}
1670
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001671WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001672weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001673{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001674 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001675
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001676 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001677
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001678 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001679}
1680
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681WL_EXPORT struct weston_view *
1682weston_compositor_pick_view(struct weston_compositor *compositor,
1683 wl_fixed_t x, wl_fixed_t y,
1684 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001685{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001687 wl_fixed_t view_x, view_y;
1688 int view_ix, view_iy;
1689 int ix = wl_fixed_to_int(x);
1690 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001691
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001693 if (!pixman_region32_contains_point(
1694 &view->transform.boundingbox, ix, iy, NULL))
1695 continue;
1696
1697 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1698 view_ix = wl_fixed_to_int(view_x);
1699 view_iy = wl_fixed_to_int(view_y);
1700
1701 if (!pixman_region32_contains_point(&view->surface->input,
1702 view_ix, view_iy, NULL))
1703 continue;
1704
Pekka Paalanen380adf52015-02-16 14:39:11 +02001705 if (view->geometry.scissor_enabled &&
1706 !pixman_region32_contains_point(&view->geometry.scissor,
1707 view_ix, view_iy, NULL))
1708 continue;
1709
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001710 *vx = view_x;
1711 *vy = view_y;
1712 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001713 }
1714
1715 return NULL;
1716}
1717
1718static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001719weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001720{
Daniel Stone37816df2012-05-16 18:45:18 +01001721 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001722
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001723 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001724 return;
1725
Daniel Stone37816df2012-05-16 18:45:18 +01001726 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001727 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001728}
1729
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001730WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001732{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001733 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001734
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 if (!weston_view_is_mapped(view))
1736 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001737
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738 weston_view_damage_below(view);
1739 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001740 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001741 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 wl_list_remove(&view->link);
1743 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 view->output_mask = 0;
1745 weston_surface_assign_output(view->surface);
1746
1747 if (weston_surface_is_mapped(view->surface))
1748 return;
1749
1750 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1751 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001752 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001754 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001755 NULL,
1756 wl_fixed_from_int(0),
1757 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001759 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001760 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001761}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001762
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763WL_EXPORT void
1764weston_surface_unmap(struct weston_surface *surface)
1765{
1766 struct weston_view *view;
1767
1768 wl_list_for_each(view, &surface->views, surface_link)
1769 weston_view_unmap(view);
1770 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001771}
1772
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001773static void
1774weston_surface_reset_pending_buffer(struct weston_surface *surface)
1775{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001776 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001777 surface->pending.sx = 0;
1778 surface->pending.sy = 0;
1779 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001780 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001781}
1782
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783WL_EXPORT void
1784weston_view_destroy(struct weston_view *view)
1785{
1786 wl_signal_emit(&view->destroy_signal, view);
1787
1788 assert(wl_list_empty(&view->geometry.child_list));
1789
1790 if (weston_view_is_mapped(view)) {
1791 weston_view_unmap(view);
1792 weston_compositor_build_view_list(view->surface->compositor);
1793 }
1794
1795 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001796 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797
1798 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001799 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001801 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802
1803 weston_view_set_transform_parent(view, NULL);
1804
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 wl_list_remove(&view->surface_link);
1806
1807 free(view);
1808}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001809
1810WL_EXPORT void
1811weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001812{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001813 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001814 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001815
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001816 if (--surface->ref_count > 0)
1817 return;
1818
1819 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1820
Pekka Paalanene67858b2013-04-25 13:57:42 +03001821 assert(wl_list_empty(&surface->subsurface_list_pending));
1822 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1825 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001826
Jason Ekstrand7b982072014-05-20 14:33:03 -05001827 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001828
Pekka Paalanende685b82012-12-04 15:58:12 +02001829 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001830
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001831 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001832 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001833 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001834
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001835 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001836 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001837
Pekka Paalanen133e4392014-09-23 22:08:46 -04001838 weston_presentation_feedback_discard_list(&surface->feedback_list);
1839
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001840 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001841}
1842
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001843static void
1844destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001845{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001846 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001847
Giulio Camuffo0d379742013-11-15 22:06:15 +01001848 /* Set the resource to NULL, since we don't want to leave a
1849 * dangling pointer if the surface was refcounted and survives
1850 * the weston_surface_destroy() call. */
1851 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001852 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001853}
1854
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001855static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001856weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1857{
1858 struct weston_buffer *buffer =
1859 container_of(listener, struct weston_buffer, destroy_listener);
1860
1861 wl_signal_emit(&buffer->destroy_signal, buffer);
1862 free(buffer);
1863}
1864
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001865WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001866weston_buffer_from_resource(struct wl_resource *resource)
1867{
1868 struct weston_buffer *buffer;
1869 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001870
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001871 listener = wl_resource_get_destroy_listener(resource,
1872 weston_buffer_destroy_handler);
1873
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001874 if (listener)
1875 return container_of(listener, struct weston_buffer,
1876 destroy_listener);
1877
1878 buffer = zalloc(sizeof *buffer);
1879 if (buffer == NULL)
1880 return NULL;
1881
1882 buffer->resource = resource;
1883 wl_signal_init(&buffer->destroy_signal);
1884 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001885 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001886 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001887
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001888 return buffer;
1889}
1890
1891static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001892weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1893 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001894{
Pekka Paalanende685b82012-12-04 15:58:12 +02001895 struct weston_buffer_reference *ref =
1896 container_of(listener, struct weston_buffer_reference,
1897 destroy_listener);
1898
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001900 ref->buffer = NULL;
1901}
1902
1903WL_EXPORT void
1904weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001905 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001906{
1907 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001908 ref->buffer->busy_count--;
1909 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001910 assert(wl_resource_get_client(ref->buffer->resource));
1911 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001912 WL_BUFFER_RELEASE);
1913 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001914 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001915 }
1916
Pekka Paalanende685b82012-12-04 15:58:12 +02001917 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001918 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001919 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001920 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001921 }
1922
Pekka Paalanende685b82012-12-04 15:58:12 +02001923 ref->buffer = buffer;
1924 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1925}
1926
1927static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001928weston_surface_attach(struct weston_surface *surface,
1929 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001930{
1931 weston_buffer_reference(&surface->buffer_ref, buffer);
1932
Pekka Paalanena6421c42012-12-04 15:58:10 +02001933 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001934 if (weston_surface_is_mapped(surface))
1935 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001936 }
1937
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001938 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001939
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001940 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001941 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001942}
1943
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001944WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001945weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001947 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001948
1949 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001950 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001951}
1952
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001953WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001954weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001955{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001956 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001957
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001958 pixman_region32_union(&compositor->primary_plane.damage,
1959 &compositor->primary_plane.damage,
1960 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001961 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001962}
1963
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001964static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001966{
Pekka Paalanende685b82012-12-04 15:58:12 +02001967 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001968 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001969 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001970
Pekka Paalanenb5026542014-11-12 15:09:24 +02001971 if (weston_timeline_enabled_ &&
1972 pixman_region32_not_empty(&surface->damage))
1973 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1974 TLP_OUTPUT(surface->output), TLP_END);
1975
Jason Ekstrandef540082014-06-26 10:37:36 -07001976 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977}
1978
1979static void
1980view_accumulate_damage(struct weston_view *view,
1981 pixman_region32_t *opaque)
1982{
1983 pixman_region32_t damage;
1984
1985 pixman_region32_init(&damage);
1986 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001987 pixman_box32_t *extents;
1988
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001990 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001991 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001992 pixman_region32_copy(&damage, &view->surface->damage);
1993 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02001994 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001995 }
1996
Pekka Paalanen380adf52015-02-16 14:39:11 +02001997 pixman_region32_intersect(&damage, &damage,
1998 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001999 pixman_region32_subtract(&damage, &damage, opaque);
2000 pixman_region32_union(&view->plane->damage,
2001 &view->plane->damage, &damage);
2002 pixman_region32_fini(&damage);
2003 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002004 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002005}
2006
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002007static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002008compositor_accumulate_damage(struct weston_compositor *ec)
2009{
2010 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002012 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002013
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002014 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002015
2016 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002017 pixman_region32_copy(&plane->clip, &clip);
2018
2019 pixman_region32_init(&opaque);
2020
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 wl_list_for_each(ev, &ec->view_list, link) {
2022 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002023 continue;
2024
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002026 }
2027
2028 pixman_region32_union(&clip, &clip, &opaque);
2029 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002030 }
2031
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002032 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002033
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 wl_list_for_each(ev, &ec->view_list, link)
2035 ev->surface->touched = 0;
2036
2037 wl_list_for_each(ev, &ec->view_list, link) {
2038 if (ev->surface->touched)
2039 continue;
2040 ev->surface->touched = 1;
2041
2042 surface_flush_damage(ev->surface);
2043
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002044 /* Both the renderer and the backend have seen the buffer
2045 * by now. If renderer needs the buffer, it has its own
2046 * reference set. If the backend wants to keep the buffer
2047 * around for migrating the surface into a non-primary plane
2048 * later, keep_buffer is true. Otherwise, drop the core
2049 * reference now, and allow early buffer release. This enables
2050 * clients to use single-buffering.
2051 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002052 if (!ev->surface->keep_buffer)
2053 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002054 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002055}
2056
2057static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002058surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002059{
2060 struct weston_subsurface *sub;
2061
Pekka Paalanene67858b2013-04-25 13:57:42 +03002062 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002064 continue;
2065
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2067 wl_list_init(&sub->surface->views);
2068
2069 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002070 }
2071}
2072
2073static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002075{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 struct weston_subsurface *sub;
2077 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002078
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2080 if (sub->surface == surface)
2081 continue;
2082
George Kiagiadakised04d382014-06-13 18:10:26 +02002083 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2084 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002085 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002086 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087
2088 surface_free_unused_subsurface_views(sub->surface);
2089 }
2090}
2091
2092static void
2093view_list_add_subsurface_view(struct weston_compositor *compositor,
2094 struct weston_subsurface *sub,
2095 struct weston_view *parent)
2096{
2097 struct weston_subsurface *child;
2098 struct weston_view *view = NULL, *iv;
2099
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002100 if (!weston_surface_is_mapped(sub->surface))
2101 return;
2102
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2104 if (iv->geometry.parent == parent) {
2105 view = iv;
2106 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002107 }
2108 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109
2110 if (view) {
2111 /* Put it back in the surface's list of views */
2112 wl_list_remove(&view->surface_link);
2113 wl_list_insert(&sub->surface->views, &view->surface_link);
2114 } else {
2115 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002116 weston_view_set_position(view,
2117 sub->position.x,
2118 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002119 weston_view_set_transform_parent(view, parent);
2120 }
2121
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002122 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124
Pekka Paalanenb188e912013-11-19 14:03:35 +02002125 if (wl_list_empty(&sub->surface->subsurface_list)) {
2126 wl_list_insert(compositor->view_list.prev, &view->link);
2127 return;
2128 }
2129
2130 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2131 if (child->surface == sub->surface)
2132 wl_list_insert(compositor->view_list.prev, &view->link);
2133 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002135 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002136}
2137
2138static void
2139view_list_add(struct weston_compositor *compositor,
2140 struct weston_view *view)
2141{
2142 struct weston_subsurface *sub;
2143
2144 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145
Pekka Paalanenb188e912013-11-19 14:03:35 +02002146 if (wl_list_empty(&view->surface->subsurface_list)) {
2147 wl_list_insert(compositor->view_list.prev, &view->link);
2148 return;
2149 }
2150
2151 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2152 if (sub->surface == view->surface)
2153 wl_list_insert(compositor->view_list.prev, &view->link);
2154 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002156 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002157}
2158
2159static void
2160weston_compositor_build_view_list(struct weston_compositor *compositor)
2161{
2162 struct weston_view *view;
2163 struct weston_layer *layer;
2164
2165 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002166 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167 surface_stash_subsurface_views(view->surface);
2168
2169 wl_list_init(&compositor->view_list);
2170 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002171 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172 view_list_add(compositor, view);
2173 }
2174 }
2175
2176 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002177 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002178 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002179}
2180
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002181static void
2182weston_output_take_feedback_list(struct weston_output *output,
2183 struct weston_surface *surface)
2184{
2185 struct weston_view *view;
2186 struct weston_presentation_feedback *feedback;
2187 uint32_t flags = 0xffffffff;
2188
2189 if (wl_list_empty(&surface->feedback_list))
2190 return;
2191
2192 /* All views must have the flag for the flag to survive. */
2193 wl_list_for_each(view, &surface->views, surface_link) {
2194 /* ignore views that are not on this output at all */
2195 if (view->output_mask & (1u << output->id))
2196 flags &= view->psf_flags;
2197 }
2198
2199 wl_list_for_each(feedback, &surface->feedback_list, link)
2200 feedback->psf_flags = flags;
2201
2202 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2203 wl_list_init(&surface->feedback_list);
2204}
2205
David Herrmann1edf44c2013-10-22 17:11:26 +02002206static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002207weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002208{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002209 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002210 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002211 struct weston_animation *animation, *next;
2212 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002213 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002214 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002215 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002216
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002217 if (output->destroying)
2218 return 0;
2219
Pekka Paalanenb5026542014-11-12 15:09:24 +02002220 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2221
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002222 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002224
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002225 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002226 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002227 } else {
2228 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002229 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002230 ev->psf_flags = 0;
2231 }
2232 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002233
Pekka Paalanene67858b2013-04-25 13:57:42 +03002234 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 wl_list_for_each(ev, &ec->view_list, link) {
2236 /* Note: This operation is safe to do multiple times on the
2237 * same surface.
2238 */
2239 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002240 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002241 &ev->surface->frame_callback_list);
2242 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002243
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002244 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245 }
2246 }
2247
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002248 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002249
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002250 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002251 pixman_region32_intersect(&output_damage,
2252 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002253 pixman_region32_subtract(&output_damage,
2254 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002255
Scott Moreauccbf29d2012-02-22 14:21:41 -07002256 if (output->dirty)
2257 weston_output_update_matrix(output);
2258
David Herrmann1edf44c2013-10-22 17:11:26 +02002259 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002260
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002261 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002262
Kristian Høgsbergef044142011-06-21 15:02:12 -04002263 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002264
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002265 weston_compositor_repick(ec);
2266 wl_event_loop_dispatch(ec->input_loop, 0);
2267
Jonas Ådahldb773762012-06-13 00:01:21 +02002268 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002269 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002270 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002271 }
2272
Scott Moreaud64cf212012-06-08 19:40:54 -06002273 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002274 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002275 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002276 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002277
Pekka Paalanenb5026542014-11-12 15:09:24 +02002278 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2279
David Herrmann1edf44c2013-10-22 17:11:26 +02002280 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002281}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002282
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002283static int
2284weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002285{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002286 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002287
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002288 wl_event_loop_dispatch(compositor->input_loop, 0);
2289
2290 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002291}
2292
Pekka Paalanen82919792014-05-21 13:51:49 +03002293static void
2294weston_output_schedule_repaint_reset(struct weston_output *output)
2295{
2296 struct weston_compositor *compositor = output->compositor;
2297 struct wl_event_loop *loop;
2298 int fd;
2299
2300 output->repaint_scheduled = 0;
2301 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2302
2303 if (compositor->input_loop_source)
2304 return;
2305
2306 loop = wl_display_get_event_loop(compositor->wl_display);
2307 fd = wl_event_loop_get_fd(compositor->input_loop);
2308 compositor->input_loop_source =
2309 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2310 weston_compositor_read_input, compositor);
2311}
2312
Kristian Høgsbergef044142011-06-21 15:02:12 -04002313WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002314weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002315 const struct timespec *stamp,
2316 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002317{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002318 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen82919792014-05-21 13:51:49 +03002319 int r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002320 uint32_t refresh_nsec;
2321
Pekka Paalanenb5026542014-11-12 15:09:24 +02002322 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2323 TLP_VBLANK(stamp), TLP_END);
2324
Pekka Paalanen133e4392014-09-23 22:08:46 -04002325 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2326 weston_presentation_feedback_present_list(&output->feedback_list,
2327 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002328 output->msc,
2329 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002330
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002331 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002332
2333 if (output->repaint_needed &&
2334 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2335 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002336 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002337 if (!r)
2338 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002339 }
2340
Pekka Paalanen82919792014-05-21 13:51:49 +03002341 weston_output_schedule_repaint_reset(output);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002342}
2343
2344static void
2345idle_repaint(void *data)
2346{
2347 struct weston_output *output = data;
2348
Jonas Ådahle5a12252013-04-05 23:07:11 +02002349 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002350}
2351
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002352WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002353weston_layer_entry_insert(struct weston_layer_entry *list,
2354 struct weston_layer_entry *entry)
2355{
2356 wl_list_insert(&list->link, &entry->link);
2357 entry->layer = list->layer;
2358}
2359
2360WL_EXPORT void
2361weston_layer_entry_remove(struct weston_layer_entry *entry)
2362{
2363 wl_list_remove(&entry->link);
2364 wl_list_init(&entry->link);
2365 entry->layer = NULL;
2366}
2367
2368WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002369weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2370{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002371 wl_list_init(&layer->view_list.link);
2372 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002373 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002374 if (below != NULL)
2375 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002376}
2377
2378WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002379weston_layer_set_mask(struct weston_layer *layer,
2380 int x, int y, int width, int height)
2381{
2382 struct weston_view *view;
2383
2384 layer->mask.x1 = x;
2385 layer->mask.x2 = x + width;
2386 layer->mask.y1 = y;
2387 layer->mask.y2 = y + height;
2388
2389 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2390 weston_view_geometry_dirty(view);
2391 }
2392}
2393
2394WL_EXPORT void
2395weston_layer_set_mask_infinite(struct weston_layer *layer)
2396{
2397 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2398 UINT32_MAX, UINT32_MAX);
2399}
2400
2401WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002402weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002403{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002404 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002405 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002406
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002407 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2408 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002409 return;
2410
Pekka Paalanenb5026542014-11-12 15:09:24 +02002411 if (!output->repaint_needed)
2412 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2413
Kristian Høgsbergef044142011-06-21 15:02:12 -04002414 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002415 output->repaint_needed = 1;
2416 if (output->repaint_scheduled)
2417 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002418
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002419 wl_event_loop_add_idle(loop, idle_repaint, output);
2420 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002421 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2422
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002423
2424 if (compositor->input_loop_source) {
2425 wl_event_source_remove(compositor->input_loop_source);
2426 compositor->input_loop_source = NULL;
2427 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002428}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002429
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002430WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002431weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2432{
2433 struct weston_output *output;
2434
2435 wl_list_for_each(output, &compositor->output_list, link)
2436 weston_output_schedule_repaint(output);
2437}
2438
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002439static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002440surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002441{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002442 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002443}
2444
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002445static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002446surface_attach(struct wl_client *client,
2447 struct wl_resource *resource,
2448 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2449{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002450 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002451 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002452
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002453 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002454 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002455 if (buffer == NULL) {
2456 wl_client_post_no_memory(client);
2457 return;
2458 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002459 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002460
Pekka Paalanende685b82012-12-04 15:58:12 +02002461 /* Attach, attach, without commit in between does not send
2462 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002463 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002464
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002465 surface->pending.sx = sx;
2466 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002467 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002468}
2469
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002470static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002471surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002472 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002473 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002474{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002475 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002476
Pekka Paalanen8e159182012-10-10 12:49:25 +03002477 pixman_region32_union_rect(&surface->pending.damage,
2478 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002479 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002480}
2481
Kristian Høgsberg33418202011-08-16 23:01:28 -04002482static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002483destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002484{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002485 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002486
2487 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002488 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002489}
2490
2491static void
2492surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002493 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002494{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002495 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002496 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002497
2498 cb = malloc(sizeof *cb);
2499 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002500 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002501 return;
2502 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002503
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002504 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2505 callback);
2506 if (cb->resource == NULL) {
2507 free(cb);
2508 wl_resource_post_no_memory(resource);
2509 return;
2510 }
2511
Jason Ekstranda85118c2013-06-27 20:17:02 -05002512 wl_resource_set_implementation(cb->resource, NULL, cb,
2513 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002514
Pekka Paalanenbc106382012-10-10 12:49:31 +03002515 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002516}
2517
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002518static void
2519surface_set_opaque_region(struct wl_client *client,
2520 struct wl_resource *resource,
2521 struct wl_resource *region_resource)
2522{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002523 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002524 struct weston_region *region;
2525
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002526 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002527 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002528 pixman_region32_copy(&surface->pending.opaque,
2529 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002530 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002531 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002532 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002533}
2534
2535static void
2536surface_set_input_region(struct wl_client *client,
2537 struct wl_resource *resource,
2538 struct wl_resource *region_resource)
2539{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002540 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002541 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002542
2543 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002544 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002545 pixman_region32_copy(&surface->pending.input,
2546 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002547 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002548 pixman_region32_fini(&surface->pending.input);
2549 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002550 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002551}
2552
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002553static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002554weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002555{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002556 struct weston_subsurface *sub;
2557
2558 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2559 parent_link_pending) {
2560 wl_list_remove(&sub->parent_link);
2561 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2562 }
2563}
2564
2565static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002566weston_surface_build_buffer_matrix(struct weston_surface *surface,
2567 struct weston_matrix *matrix)
2568{
2569 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2570 double src_width, src_height, dest_width, dest_height;
2571
2572 weston_matrix_init(matrix);
2573
2574 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2575 src_width = surface->width_from_buffer;
2576 src_height = surface->height_from_buffer;
2577 } else {
2578 src_width = wl_fixed_to_double(vp->buffer.src_width);
2579 src_height = wl_fixed_to_double(vp->buffer.src_height);
2580 }
2581
2582 if (vp->surface.width == -1) {
2583 dest_width = src_width;
2584 dest_height = src_height;
2585 } else {
2586 dest_width = vp->surface.width;
2587 dest_height = vp->surface.height;
2588 }
2589
2590 if (src_width != dest_width || src_height != dest_height)
2591 weston_matrix_scale(matrix,
2592 src_width / dest_width,
2593 src_height / dest_height, 1);
2594
2595 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2596 weston_matrix_translate(matrix,
2597 wl_fixed_to_double(vp->buffer.src_x),
2598 wl_fixed_to_double(vp->buffer.src_y),
2599 0);
2600
2601 switch (vp->buffer.transform) {
2602 case WL_OUTPUT_TRANSFORM_FLIPPED:
2603 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2604 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2605 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2606 weston_matrix_scale(matrix, -1, 1, 1);
2607 weston_matrix_translate(matrix,
2608 surface->width_from_buffer, 0, 0);
2609 break;
2610 }
2611
2612 switch (vp->buffer.transform) {
2613 default:
2614 case WL_OUTPUT_TRANSFORM_NORMAL:
2615 case WL_OUTPUT_TRANSFORM_FLIPPED:
2616 break;
2617 case WL_OUTPUT_TRANSFORM_90:
2618 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2619 weston_matrix_rotate_xy(matrix, 0, 1);
2620 weston_matrix_translate(matrix,
2621 surface->height_from_buffer, 0, 0);
2622 break;
2623 case WL_OUTPUT_TRANSFORM_180:
2624 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2625 weston_matrix_rotate_xy(matrix, -1, 0);
2626 weston_matrix_translate(matrix,
2627 surface->width_from_buffer,
2628 surface->height_from_buffer, 0);
2629 break;
2630 case WL_OUTPUT_TRANSFORM_270:
2631 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2632 weston_matrix_rotate_xy(matrix, 0, -1);
2633 weston_matrix_translate(matrix,
2634 0, surface->width_from_buffer, 0);
2635 break;
2636 }
2637
2638 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2639}
2640
2641static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002642weston_surface_commit_state(struct weston_surface *surface,
2643 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002644{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002645 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002646 pixman_region32_t opaque;
2647
Alexander Larsson4ea95522013-05-22 14:41:37 +02002648 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002649 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002650 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002651 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002652
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002653 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002654 if (state->newly_attached)
2655 weston_surface_attach(surface, state->buffer);
2656 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002657
Jason Ekstrand1e059042014-10-16 10:55:19 -05002658 weston_surface_build_buffer_matrix(surface,
2659 &surface->surface_to_buffer_matrix);
2660 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2661 &surface->surface_to_buffer_matrix);
2662
Jason Ekstrand7b982072014-05-20 14:33:03 -05002663 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002664 weston_surface_update_size(surface);
2665 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002666 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002667 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002668
Jason Ekstrand7b982072014-05-20 14:33:03 -05002669 state->sx = 0;
2670 state->sy = 0;
2671 state->newly_attached = 0;
2672 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002673
2674 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002675 if (weston_timeline_enabled_ &&
2676 pixman_region32_not_empty(&state->damage))
2677 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002678 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002679 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002680 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002681 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002682 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002683
2684 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002685 pixman_region32_init(&opaque);
2686 pixman_region32_intersect_rect(&opaque, &state->opaque,
2687 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002688
2689 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2690 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002691 wl_list_for_each(view, &surface->views, surface_link)
2692 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002693 }
2694
2695 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002696
2697 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002698 pixman_region32_intersect_rect(&surface->input, &state->input,
2699 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002700
Pekka Paalanenbc106382012-10-10 12:49:31 +03002701 /* wl_surface.frame */
2702 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002703 &state->frame_callback_list);
2704 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002705
2706 /* XXX:
2707 * What should happen with a feedback request, if there
2708 * is no wl_buffer attached for this commit?
2709 */
2710
2711 /* presentation.feedback */
2712 wl_list_insert_list(&surface->feedback_list,
2713 &state->feedback_list);
2714 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002715}
2716
2717static void
2718weston_surface_commit(struct weston_surface *surface)
2719{
2720 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002721
Pekka Paalanene67858b2013-04-25 13:57:42 +03002722 weston_surface_commit_subsurface_order(surface);
2723
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002724 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002725}
2726
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002727static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002728weston_subsurface_commit(struct weston_subsurface *sub);
2729
2730static void
2731weston_subsurface_parent_commit(struct weston_subsurface *sub,
2732 int parent_is_synchronized);
2733
2734static void
2735surface_commit(struct wl_client *client, struct wl_resource *resource)
2736{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002737 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002738 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2739
2740 if (sub) {
2741 weston_subsurface_commit(sub);
2742 return;
2743 }
2744
2745 weston_surface_commit(surface);
2746
2747 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2748 if (sub->surface != surface)
2749 weston_subsurface_parent_commit(sub, 0);
2750 }
2751}
2752
2753static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002754surface_set_buffer_transform(struct wl_client *client,
2755 struct wl_resource *resource, int transform)
2756{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002757 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002758
Jonny Lamba55f1392014-05-30 12:07:15 +02002759 /* if wl_output.transform grows more members this will need to be updated. */
2760 if (transform < 0 ||
2761 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2762 wl_resource_post_error(resource,
2763 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2764 "buffer transform must be a valid transform "
2765 "('%d' specified)", transform);
2766 return;
2767 }
2768
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002769 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002770 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002771}
2772
Alexander Larsson4ea95522013-05-22 14:41:37 +02002773static void
2774surface_set_buffer_scale(struct wl_client *client,
2775 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002776 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002777{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002778 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002779
Jonny Lamba55f1392014-05-30 12:07:15 +02002780 if (scale < 1) {
2781 wl_resource_post_error(resource,
2782 WL_SURFACE_ERROR_INVALID_SCALE,
2783 "buffer scale must be at least one "
2784 "('%d' specified)", scale);
2785 return;
2786 }
2787
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002788 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002789 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002790}
2791
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002792static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002793 surface_destroy,
2794 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002795 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002796 surface_frame,
2797 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002798 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002799 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002800 surface_set_buffer_transform,
2801 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002802};
2803
2804static void
2805compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002806 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002807{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002808 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002809 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002810
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002811 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002812 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002813 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002814 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002815 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002816
Jason Ekstranda85118c2013-06-27 20:17:02 -05002817 surface->resource =
2818 wl_resource_create(client, &wl_surface_interface,
2819 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002820 if (surface->resource == NULL) {
2821 weston_surface_destroy(surface);
2822 wl_resource_post_no_memory(resource);
2823 return;
2824 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002825 wl_resource_set_implementation(surface->resource, &surface_interface,
2826 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002827
2828 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002829}
2830
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002831static void
2832destroy_region(struct wl_resource *resource)
2833{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002834 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002835
2836 pixman_region32_fini(&region->region);
2837 free(region);
2838}
2839
2840static void
2841region_destroy(struct wl_client *client, struct wl_resource *resource)
2842{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002843 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002844}
2845
2846static void
2847region_add(struct wl_client *client, struct wl_resource *resource,
2848 int32_t x, int32_t y, int32_t width, int32_t height)
2849{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002850 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002851
2852 pixman_region32_union_rect(&region->region, &region->region,
2853 x, y, width, height);
2854}
2855
2856static void
2857region_subtract(struct wl_client *client, struct wl_resource *resource,
2858 int32_t x, int32_t y, int32_t width, int32_t height)
2859{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002860 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002861 pixman_region32_t rect;
2862
2863 pixman_region32_init_rect(&rect, x, y, width, height);
2864 pixman_region32_subtract(&region->region, &region->region, &rect);
2865 pixman_region32_fini(&rect);
2866}
2867
2868static const struct wl_region_interface region_interface = {
2869 region_destroy,
2870 region_add,
2871 region_subtract
2872};
2873
2874static void
2875compositor_create_region(struct wl_client *client,
2876 struct wl_resource *resource, uint32_t id)
2877{
2878 struct weston_region *region;
2879
2880 region = malloc(sizeof *region);
2881 if (region == NULL) {
2882 wl_resource_post_no_memory(resource);
2883 return;
2884 }
2885
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002886 pixman_region32_init(&region->region);
2887
Jason Ekstranda85118c2013-06-27 20:17:02 -05002888 region->resource =
2889 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002890 if (region->resource == NULL) {
2891 free(region);
2892 wl_resource_post_no_memory(resource);
2893 return;
2894 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002895 wl_resource_set_implementation(region->resource, &region_interface,
2896 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002897}
2898
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002899static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002900 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002901 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002902};
2903
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002904static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002905weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2906{
2907 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002908
Jason Ekstrand7b982072014-05-20 14:33:03 -05002909 weston_surface_commit_state(surface, &sub->cached);
2910 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002911
2912 weston_surface_commit_subsurface_order(surface);
2913
2914 weston_surface_schedule_repaint(surface);
2915
Jason Ekstrand7b982072014-05-20 14:33:03 -05002916 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002917}
2918
2919static void
2920weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2921{
2922 struct weston_surface *surface = sub->surface;
2923
2924 /*
2925 * If this commit would cause the surface to move by the
2926 * attach(dx, dy) parameters, the old damage region must be
2927 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002928 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929 */
2930 pixman_region32_translate(&sub->cached.damage,
2931 -surface->pending.sx, -surface->pending.sy);
2932 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2933 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002934 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002935
2936 if (surface->pending.newly_attached) {
2937 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002938 weston_surface_state_set_buffer(&sub->cached,
2939 surface->pending.buffer);
2940 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002942 weston_presentation_feedback_discard_list(
2943 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002944 }
2945 sub->cached.sx += surface->pending.sx;
2946 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002947
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002948 sub->cached.buffer_viewport.changed |=
2949 surface->pending.buffer_viewport.changed;
2950 sub->cached.buffer_viewport.buffer =
2951 surface->pending.buffer_viewport.buffer;
2952 sub->cached.buffer_viewport.surface =
2953 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002954
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002955 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002956
2957 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2958
2959 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2960
2961 wl_list_insert_list(&sub->cached.frame_callback_list,
2962 &surface->pending.frame_callback_list);
2963 wl_list_init(&surface->pending.frame_callback_list);
2964
Pekka Paalanen133e4392014-09-23 22:08:46 -04002965 wl_list_insert_list(&sub->cached.feedback_list,
2966 &surface->pending.feedback_list);
2967 wl_list_init(&surface->pending.feedback_list);
2968
Jason Ekstrand7b982072014-05-20 14:33:03 -05002969 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002970}
2971
Derek Foreman280e7dd2014-10-03 13:13:42 -05002972static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002973weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2974{
2975 while (sub) {
2976 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002977 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002978
2979 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002980 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002981
2982 sub = weston_surface_to_subsurface(sub->parent);
2983 }
2984
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002985 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002986}
2987
2988static void
2989weston_subsurface_commit(struct weston_subsurface *sub)
2990{
2991 struct weston_surface *surface = sub->surface;
2992 struct weston_subsurface *tmp;
2993
2994 /* Recursive check for effectively synchronized. */
2995 if (weston_subsurface_is_synchronized(sub)) {
2996 weston_subsurface_commit_to_cache(sub);
2997 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002998 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002999 /* flush accumulated state from cache */
3000 weston_subsurface_commit_to_cache(sub);
3001 weston_subsurface_commit_from_cache(sub);
3002 } else {
3003 weston_surface_commit(surface);
3004 }
3005
3006 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3007 if (tmp->surface != surface)
3008 weston_subsurface_parent_commit(tmp, 0);
3009 }
3010 }
3011}
3012
3013static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003014weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003015{
3016 struct weston_surface *surface = sub->surface;
3017 struct weston_subsurface *tmp;
3018
Pekka Paalanene67858b2013-04-25 13:57:42 +03003019 /* From now on, commit_from_cache the whole sub-tree, regardless of
3020 * the synchronized mode of each child. This sub-surface or some
3021 * of its ancestors were synchronized, so we are synchronized
3022 * all the way down.
3023 */
3024
Jason Ekstrand7b982072014-05-20 14:33:03 -05003025 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003026 weston_subsurface_commit_from_cache(sub);
3027
3028 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3029 if (tmp->surface != surface)
3030 weston_subsurface_parent_commit(tmp, 1);
3031 }
3032}
3033
3034static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003035weston_subsurface_parent_commit(struct weston_subsurface *sub,
3036 int parent_is_synchronized)
3037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003039 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 wl_list_for_each(view, &sub->surface->views, surface_link)
3041 weston_view_set_position(view,
3042 sub->position.x,
3043 sub->position.y);
3044
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003045 sub->position.set = 0;
3046 }
3047
3048 if (parent_is_synchronized || sub->synchronized)
3049 weston_subsurface_synchronized_commit(sub);
3050}
3051
Pekka Paalanen8274d902014-08-06 19:36:51 +03003052static int
3053subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3054{
3055 return snprintf(buf, len, "sub-surface");
3056}
3057
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003058static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003059subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003060{
3061 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003062 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003063
Jason Ekstranda7af7042013-10-12 22:38:11 -05003064 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003065 weston_view_set_position(view,
3066 view->geometry.x + dx,
3067 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003068
3069 /* No need to check parent mappedness, because if parent is not
3070 * mapped, parent is not in a visible layer, so this sub-surface
3071 * will not be drawn either.
3072 */
3073 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003074 struct weston_output *output;
3075
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003076 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077 * because that would call it also for the parent surface,
3078 * which might not be mapped yet. That would lead to
3079 * inconsistent state, where the window could never be
3080 * mapped.
3081 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003082 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003083 * weston_surface_is_mapped() return true, so that when the
3084 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003085 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003086 */
3087 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003088 output = container_of(compositor->output_list.next,
3089 struct weston_output, link);
3090
3091 surface->output = output;
3092 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003093 }
3094}
3095
3096static struct weston_subsurface *
3097weston_surface_to_subsurface(struct weston_surface *surface)
3098{
3099 if (surface->configure == subsurface_configure)
3100 return surface->configure_private;
3101
3102 return NULL;
3103}
3104
Pekka Paalanen01388e22013-04-25 13:57:44 +03003105WL_EXPORT struct weston_surface *
3106weston_surface_get_main_surface(struct weston_surface *surface)
3107{
3108 struct weston_subsurface *sub;
3109
3110 while (surface && (sub = weston_surface_to_subsurface(surface)))
3111 surface = sub->parent;
3112
3113 return surface;
3114}
3115
Pekka Paalanen50b67472014-10-01 15:02:41 +03003116WL_EXPORT int
3117weston_surface_set_role(struct weston_surface *surface,
3118 const char *role_name,
3119 struct wl_resource *error_resource,
3120 uint32_t error_code)
3121{
3122 assert(role_name);
3123
3124 if (surface->role_name == NULL ||
3125 surface->role_name == role_name ||
3126 strcmp(surface->role_name, role_name) == 0) {
3127 surface->role_name = role_name;
3128
3129 return 0;
3130 }
3131
3132 wl_resource_post_error(error_resource, error_code,
3133 "Cannot assign role %s to wl_surface@%d,"
3134 " already has role %s\n",
3135 role_name,
3136 wl_resource_get_id(surface->resource),
3137 surface->role_name);
3138 return -1;
3139}
3140
Pekka Paalanen8274d902014-08-06 19:36:51 +03003141WL_EXPORT void
3142weston_surface_set_label_func(struct weston_surface *surface,
3143 int (*desc)(struct weston_surface *,
3144 char *, size_t))
3145{
3146 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003147 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003148}
3149
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003150/** Get the size of surface contents
3151 *
3152 * \param surface The surface to query.
3153 * \param width Returns the width of raw contents.
3154 * \param height Returns the height of raw contents.
3155 *
3156 * Retrieves the raw surface content size in pixels for the given surface.
3157 * This is the whole content size in buffer pixels. If the surface
3158 * has no content or the renderer does not implement this feature,
3159 * zeroes are returned.
3160 *
3161 * This function is used to determine the buffer size needed for
3162 * a weston_surface_copy_content() call.
3163 */
3164WL_EXPORT void
3165weston_surface_get_content_size(struct weston_surface *surface,
3166 int *width, int *height)
3167{
3168 struct weston_renderer *rer = surface->compositor->renderer;
3169
3170 if (!rer->surface_get_content_size) {
3171 *width = 0;
3172 *height = 0;
3173 return;
3174 }
3175
3176 rer->surface_get_content_size(surface, width, height);
3177}
3178
3179/** Copy surface contents to system memory.
3180 *
3181 * \param surface The surface to copy from.
3182 * \param target Pointer to the target memory buffer.
3183 * \param size Size of the target buffer in bytes.
3184 * \param src_x X location on contents to copy from.
3185 * \param src_y Y location on contents to copy from.
3186 * \param width Width in pixels of the area to copy.
3187 * \param height Height in pixels of the area to copy.
3188 * \return 0 for success, -1 for failure.
3189 *
3190 * Surface contents are maintained by the renderer. They can be in a
3191 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3192 * else.
3193 *
3194 * Surface contents are copied into memory pointed to by target,
3195 * which has size bytes of space available. The target memory
3196 * may be larger than needed, but being smaller returns an error.
3197 * The extra bytes in target may or may not be written; their content is
3198 * unspecified. Size must be large enough to hold the image.
3199 *
3200 * The image in the target memory will be arranged in rows from
3201 * top to bottom, and pixels on a row from left to right. The pixel
3202 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3203 * width * 4.
3204 *
3205 * Parameters src_x and src_y define the upper-left corner in buffer
3206 * coordinates (pixels) to copy from. Parameters width and height
3207 * define the size of the area to copy in pixels.
3208 *
3209 * The rectangle defined by src_x, src_y, width, height must fit in
3210 * the surface contents. Otherwise an error is returned.
3211 *
3212 * Use surface_get_data_size to determine the content size; the
3213 * needed target buffer size and rectangle limits.
3214 *
3215 * CURRENT IMPLEMENTATION RESTRICTIONS:
3216 * - the machine must be little-endian due to Pixman formats.
3217 *
3218 * NOTE: Pixman formats are premultiplied.
3219 */
3220WL_EXPORT int
3221weston_surface_copy_content(struct weston_surface *surface,
3222 void *target, size_t size,
3223 int src_x, int src_y,
3224 int width, int height)
3225{
3226 struct weston_renderer *rer = surface->compositor->renderer;
3227 int cw, ch;
3228 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3229
3230 if (!rer->surface_copy_content)
3231 return -1;
3232
3233 weston_surface_get_content_size(surface, &cw, &ch);
3234
3235 if (src_x < 0 || src_y < 0)
3236 return -1;
3237
3238 if (width <= 0 || height <= 0)
3239 return -1;
3240
3241 if (src_x + width > cw || src_y + height > ch)
3242 return -1;
3243
3244 if (width * bytespp * height > size)
3245 return -1;
3246
3247 return rer->surface_copy_content(surface, target, size,
3248 src_x, src_y, width, height);
3249}
3250
Pekka Paalanene67858b2013-04-25 13:57:42 +03003251static void
3252subsurface_set_position(struct wl_client *client,
3253 struct wl_resource *resource, int32_t x, int32_t y)
3254{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003255 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003256
3257 if (!sub)
3258 return;
3259
3260 sub->position.x = x;
3261 sub->position.y = y;
3262 sub->position.set = 1;
3263}
3264
3265static struct weston_subsurface *
3266subsurface_from_surface(struct weston_surface *surface)
3267{
3268 struct weston_subsurface *sub;
3269
3270 sub = weston_surface_to_subsurface(surface);
3271 if (sub)
3272 return sub;
3273
3274 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3275 if (sub->surface == surface)
3276 return sub;
3277
3278 return NULL;
3279}
3280
3281static struct weston_subsurface *
3282subsurface_sibling_check(struct weston_subsurface *sub,
3283 struct weston_surface *surface,
3284 const char *request)
3285{
3286 struct weston_subsurface *sibling;
3287
3288 sibling = subsurface_from_surface(surface);
3289
3290 if (!sibling) {
3291 wl_resource_post_error(sub->resource,
3292 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3293 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003294 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003295 return NULL;
3296 }
3297
3298 if (sibling->parent != sub->parent) {
3299 wl_resource_post_error(sub->resource,
3300 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3301 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003302 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003303 return NULL;
3304 }
3305
3306 return sibling;
3307}
3308
3309static void
3310subsurface_place_above(struct wl_client *client,
3311 struct wl_resource *resource,
3312 struct wl_resource *sibling_resource)
3313{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003314 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003315 struct weston_surface *surface =
3316 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003317 struct weston_subsurface *sibling;
3318
3319 if (!sub)
3320 return;
3321
3322 sibling = subsurface_sibling_check(sub, surface, "place_above");
3323 if (!sibling)
3324 return;
3325
3326 wl_list_remove(&sub->parent_link_pending);
3327 wl_list_insert(sibling->parent_link_pending.prev,
3328 &sub->parent_link_pending);
3329}
3330
3331static void
3332subsurface_place_below(struct wl_client *client,
3333 struct wl_resource *resource,
3334 struct wl_resource *sibling_resource)
3335{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003336 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003337 struct weston_surface *surface =
3338 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003339 struct weston_subsurface *sibling;
3340
3341 if (!sub)
3342 return;
3343
3344 sibling = subsurface_sibling_check(sub, surface, "place_below");
3345 if (!sibling)
3346 return;
3347
3348 wl_list_remove(&sub->parent_link_pending);
3349 wl_list_insert(&sibling->parent_link_pending,
3350 &sub->parent_link_pending);
3351}
3352
3353static void
3354subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3355{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003356 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003357
3358 if (sub)
3359 sub->synchronized = 1;
3360}
3361
3362static void
3363subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3364{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003365 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003366
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003367 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003368 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003369
3370 /* If sub became effectively desynchronized, flush. */
3371 if (!weston_subsurface_is_synchronized(sub))
3372 weston_subsurface_synchronized_commit(sub);
3373 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003374}
3375
3376static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003377weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3378{
3379 wl_list_remove(&sub->parent_link);
3380 wl_list_remove(&sub->parent_link_pending);
3381 wl_list_remove(&sub->parent_destroy_listener.link);
3382 sub->parent = NULL;
3383}
3384
3385static void
3386weston_subsurface_destroy(struct weston_subsurface *sub);
3387
3388static void
3389subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3390{
3391 struct weston_subsurface *sub =
3392 container_of(listener, struct weston_subsurface,
3393 surface_destroy_listener);
3394 assert(data == &sub->surface->resource);
3395
3396 /* The protocol object (wl_resource) is left inert. */
3397 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003398 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003399
3400 weston_subsurface_destroy(sub);
3401}
3402
3403static void
3404subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3405{
3406 struct weston_subsurface *sub =
3407 container_of(listener, struct weston_subsurface,
3408 parent_destroy_listener);
3409 assert(data == &sub->parent->resource);
3410 assert(sub->surface != sub->parent);
3411
3412 if (weston_surface_is_mapped(sub->surface))
3413 weston_surface_unmap(sub->surface);
3414
3415 weston_subsurface_unlink_parent(sub);
3416}
3417
3418static void
3419subsurface_resource_destroy(struct wl_resource *resource)
3420{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003421 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003422
3423 if (sub)
3424 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003425}
3426
3427static void
3428subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3429{
3430 wl_resource_destroy(resource);
3431}
3432
3433static void
3434weston_subsurface_link_parent(struct weston_subsurface *sub,
3435 struct weston_surface *parent)
3436{
3437 sub->parent = parent;
3438 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003439 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003440 &sub->parent_destroy_listener);
3441
3442 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3443 wl_list_insert(&parent->subsurface_list_pending,
3444 &sub->parent_link_pending);
3445}
3446
3447static void
3448weston_subsurface_link_surface(struct weston_subsurface *sub,
3449 struct weston_surface *surface)
3450{
3451 sub->surface = surface;
3452 sub->surface_destroy_listener.notify =
3453 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003454 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003455 &sub->surface_destroy_listener);
3456}
3457
3458static void
3459weston_subsurface_destroy(struct weston_subsurface *sub)
3460{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003461 struct weston_view *view, *next;
3462
Pekka Paalanene67858b2013-04-25 13:57:42 +03003463 assert(sub->surface);
3464
3465 if (sub->resource) {
3466 assert(weston_surface_to_subsurface(sub->surface) == sub);
3467 assert(sub->parent_destroy_listener.notify ==
3468 subsurface_handle_parent_destroy);
3469
George Kiagiadakised04d382014-06-13 18:10:26 +02003470 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3471 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003472 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003473 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003474
Pekka Paalanene67858b2013-04-25 13:57:42 +03003475 if (sub->parent)
3476 weston_subsurface_unlink_parent(sub);
3477
Jason Ekstrand7b982072014-05-20 14:33:03 -05003478 weston_surface_state_fini(&sub->cached);
3479 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480
3481 sub->surface->configure = NULL;
3482 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003483 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484 } else {
3485 /* the dummy weston_subsurface for the parent itself */
3486 assert(sub->parent_destroy_listener.notify == NULL);
3487 wl_list_remove(&sub->parent_link);
3488 wl_list_remove(&sub->parent_link_pending);
3489 }
3490
3491 wl_list_remove(&sub->surface_destroy_listener.link);
3492 free(sub);
3493}
3494
3495static const struct wl_subsurface_interface subsurface_implementation = {
3496 subsurface_destroy,
3497 subsurface_set_position,
3498 subsurface_place_above,
3499 subsurface_place_below,
3500 subsurface_set_sync,
3501 subsurface_set_desync
3502};
3503
3504static struct weston_subsurface *
3505weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3506 struct weston_surface *parent)
3507{
3508 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003509 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003510
Bryce Harringtonde16d892014-11-20 22:21:57 -08003511 sub = zalloc(sizeof *sub);
3512 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003513 return NULL;
3514
Jason Ekstranda7af7042013-10-12 22:38:11 -05003515 wl_list_init(&sub->unused_views);
3516
Jason Ekstranda85118c2013-06-27 20:17:02 -05003517 sub->resource =
3518 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003519 if (!sub->resource) {
3520 free(sub);
3521 return NULL;
3522 }
3523
Jason Ekstranda85118c2013-06-27 20:17:02 -05003524 wl_resource_set_implementation(sub->resource,
3525 &subsurface_implementation,
3526 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003527 weston_subsurface_link_surface(sub, surface);
3528 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003529 weston_surface_state_init(&sub->cached);
3530 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003531 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003532
3533 return sub;
3534}
3535
3536/* Create a dummy subsurface for having the parent itself in its
3537 * sub-surface lists. Makes stacking order manipulation easy.
3538 */
3539static struct weston_subsurface *
3540weston_subsurface_create_for_parent(struct weston_surface *parent)
3541{
3542 struct weston_subsurface *sub;
3543
Bryce Harringtonde16d892014-11-20 22:21:57 -08003544 sub = zalloc(sizeof *sub);
3545 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546 return NULL;
3547
3548 weston_subsurface_link_surface(sub, parent);
3549 sub->parent = parent;
3550 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3551 wl_list_insert(&parent->subsurface_list_pending,
3552 &sub->parent_link_pending);
3553
3554 return sub;
3555}
3556
3557static void
3558subcompositor_get_subsurface(struct wl_client *client,
3559 struct wl_resource *resource,
3560 uint32_t id,
3561 struct wl_resource *surface_resource,
3562 struct wl_resource *parent_resource)
3563{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003564 struct weston_surface *surface =
3565 wl_resource_get_user_data(surface_resource);
3566 struct weston_surface *parent =
3567 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568 struct weston_subsurface *sub;
3569 static const char where[] = "get_subsurface: wl_subsurface@";
3570
3571 if (surface == parent) {
3572 wl_resource_post_error(resource,
3573 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3574 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003575 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003576 return;
3577 }
3578
3579 if (weston_surface_to_subsurface(surface)) {
3580 wl_resource_post_error(resource,
3581 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3582 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003583 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003584 return;
3585 }
3586
Pekka Paalanen50b67472014-10-01 15:02:41 +03003587 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3588 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003589 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003590
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003591 if (weston_surface_get_main_surface(parent) == surface) {
3592 wl_resource_post_error(resource,
3593 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3594 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003595 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003596 return;
3597 }
3598
Pekka Paalanene67858b2013-04-25 13:57:42 +03003599 /* make sure the parent is in its own list */
3600 if (wl_list_empty(&parent->subsurface_list)) {
3601 if (!weston_subsurface_create_for_parent(parent)) {
3602 wl_resource_post_no_memory(resource);
3603 return;
3604 }
3605 }
3606
3607 sub = weston_subsurface_create(id, surface, parent);
3608 if (!sub) {
3609 wl_resource_post_no_memory(resource);
3610 return;
3611 }
3612
3613 surface->configure = subsurface_configure;
3614 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003615 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003616}
3617
3618static void
3619subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3620{
3621 wl_resource_destroy(resource);
3622}
3623
3624static const struct wl_subcompositor_interface subcompositor_interface = {
3625 subcompositor_destroy,
3626 subcompositor_get_subsurface
3627};
3628
3629static void
3630bind_subcompositor(struct wl_client *client,
3631 void *data, uint32_t version, uint32_t id)
3632{
3633 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003634 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635
Jason Ekstranda85118c2013-06-27 20:17:02 -05003636 resource =
3637 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003638 if (resource == NULL) {
3639 wl_client_post_no_memory(client);
3640 return;
3641 }
3642 wl_resource_set_implementation(resource, &subcompositor_interface,
3643 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003644}
3645
3646static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003647weston_compositor_dpms(struct weston_compositor *compositor,
3648 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003649{
3650 struct weston_output *output;
3651
3652 wl_list_for_each(output, &compositor->output_list, link)
3653 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003654 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003655}
3656
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003657WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003658weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003659{
Neil Roberts8b62e202013-09-30 13:14:47 +01003660 uint32_t old_state = compositor->state;
3661
3662 /* The state needs to be changed before emitting the wake
3663 * signal because that may try to schedule a repaint which
3664 * will not work if the compositor is still sleeping */
3665 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3666
3667 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003668 case WESTON_COMPOSITOR_SLEEPING:
3669 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3670 /* fall through */
3671 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003672 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003673 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003674 /* fall through */
3675 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003676 wl_event_source_timer_update(compositor->idle_source,
3677 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003678 }
3679}
3680
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003681WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003682weston_compositor_offscreen(struct weston_compositor *compositor)
3683{
3684 switch (compositor->state) {
3685 case WESTON_COMPOSITOR_OFFSCREEN:
3686 return;
3687 case WESTON_COMPOSITOR_SLEEPING:
3688 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3689 /* fall through */
3690 default:
3691 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3692 wl_event_source_timer_update(compositor->idle_source, 0);
3693 }
3694}
3695
3696WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003697weston_compositor_sleep(struct weston_compositor *compositor)
3698{
3699 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3700 return;
3701
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003702 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003703 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3704 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3705}
3706
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003707static int
3708idle_handler(void *data)
3709{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003710 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003711
3712 if (compositor->idle_inhibit)
3713 return 1;
3714
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003715 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003716 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003717
3718 return 1;
3719}
3720
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003721WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003722weston_plane_init(struct weston_plane *plane,
3723 struct weston_compositor *ec,
3724 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003725{
3726 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003727 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003728 plane->x = x;
3729 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003730 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003731
3732 /* Init the link so that the call to wl_list_remove() when releasing
3733 * the plane without ever stacking doesn't lead to a crash */
3734 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003735}
3736
3737WL_EXPORT void
3738weston_plane_release(struct weston_plane *plane)
3739{
Xiong Zhang97116532013-10-23 13:58:31 +08003740 struct weston_view *view;
3741
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003742 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003743 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003744
Xiong Zhang97116532013-10-23 13:58:31 +08003745 wl_list_for_each(view, &plane->compositor->view_list, link) {
3746 if (view->plane == plane)
3747 view->plane = NULL;
3748 }
3749
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003750 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003751}
3752
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003753WL_EXPORT void
3754weston_compositor_stack_plane(struct weston_compositor *ec,
3755 struct weston_plane *plane,
3756 struct weston_plane *above)
3757{
3758 if (above)
3759 wl_list_insert(above->link.prev, &plane->link);
3760 else
3761 wl_list_insert(&ec->plane_list, &plane->link);
3762}
3763
Casey Dahlin9074db52012-04-19 22:50:09 -04003764static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003765{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003766 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003767}
3768
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003769static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003770bind_output(struct wl_client *client,
3771 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003773 struct weston_output *output = data;
3774 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003775 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003776
Jason Ekstranda85118c2013-06-27 20:17:02 -05003777 resource = wl_resource_create(client, &wl_output_interface,
3778 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003779 if (resource == NULL) {
3780 wl_client_post_no_memory(client);
3781 return;
3782 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003783
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003784 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003785 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003786
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003787 wl_output_send_geometry(resource,
3788 output->x,
3789 output->y,
3790 output->mm_width,
3791 output->mm_height,
3792 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003793 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003794 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003795 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003796 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003797 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003798
3799 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003800 wl_output_send_mode(resource,
3801 mode->flags,
3802 mode->width,
3803 mode->height,
3804 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003805 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003806
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003807 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003808 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003809}
3810
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003811/* Move other outputs when one is removed so the space remains contiguos. */
3812static void
3813weston_compositor_remove_output(struct weston_compositor *compositor,
3814 struct weston_output *remove_output)
3815{
3816 struct weston_output *output;
3817 int offset = 0;
3818
3819 wl_list_for_each(output, &compositor->output_list, link) {
3820 if (output == remove_output) {
3821 offset = output->width;
3822 continue;
3823 }
3824
3825 if (offset > 0) {
3826 weston_output_move(output,
3827 output->x - offset, output->y);
3828 output->dirty = 1;
3829 }
3830 }
3831}
3832
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003833WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003834weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003835{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003836 struct wl_resource *resource;
3837
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003838 output->destroying = 1;
3839
Pekka Paalanen133e4392014-09-23 22:08:46 -04003840 weston_presentation_feedback_discard_list(&output->feedback_list);
3841
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003842 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003843 wl_list_remove(&output->link);
3844
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003845 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003846 wl_signal_emit(&output->destroy_signal, output);
3847
Richard Hughesafe690c2013-05-02 10:10:04 +01003848 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003849 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003850 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003851 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003852
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003853 wl_resource_for_each(resource, &output->resource_list) {
3854 wl_resource_set_destructor(resource, NULL);
3855 }
3856
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003857 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003858}
3859
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003860WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003861weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003862{
Scott Moreau850ca422012-05-21 15:21:25 -06003863 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003864
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003865 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003866 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003867
Scott Moreauccbf29d2012-02-22 14:21:41 -07003868 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003869 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003870 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003871 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003872 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003873 weston_matrix_scale(&output->matrix, magnification,
3874 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003875 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003876
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003877 switch (output->transform) {
3878 case WL_OUTPUT_TRANSFORM_FLIPPED:
3879 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3880 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3881 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3882 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3883 weston_matrix_scale(&output->matrix, -1, 1, 1);
3884 break;
3885 }
3886
3887 switch (output->transform) {
3888 default:
3889 case WL_OUTPUT_TRANSFORM_NORMAL:
3890 case WL_OUTPUT_TRANSFORM_FLIPPED:
3891 break;
3892 case WL_OUTPUT_TRANSFORM_90:
3893 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3894 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3895 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3896 break;
3897 case WL_OUTPUT_TRANSFORM_180:
3898 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3899 weston_matrix_translate(&output->matrix,
3900 -output->width, -output->height, 0);
3901 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3902 break;
3903 case WL_OUTPUT_TRANSFORM_270:
3904 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3905 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3906 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3907 break;
3908 }
3909
3910 if (output->current_scale != 1)
3911 weston_matrix_scale(&output->matrix,
3912 output->current_scale,
3913 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003914
Scott Moreauccbf29d2012-02-22 14:21:41 -07003915 output->dirty = 0;
3916}
3917
Scott Moreau1bad5db2012-08-18 01:04:05 -06003918static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003919weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003920{
3921 output->transform = transform;
3922
3923 switch (transform) {
3924 case WL_OUTPUT_TRANSFORM_90:
3925 case WL_OUTPUT_TRANSFORM_270:
3926 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3927 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3928 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003929 output->width = output->current_mode->height;
3930 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003931 break;
3932 case WL_OUTPUT_TRANSFORM_NORMAL:
3933 case WL_OUTPUT_TRANSFORM_180:
3934 case WL_OUTPUT_TRANSFORM_FLIPPED:
3935 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003936 output->width = output->current_mode->width;
3937 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003938 break;
3939 default:
3940 break;
3941 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003942
Hardening57388e42013-09-18 23:56:36 +02003943 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003944 output->width /= scale;
3945 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003946}
3947
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003948static void
3949weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003950{
3951 output->x = x;
3952 output->y = y;
3953
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003954 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003955 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003956 output->width,
3957 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003958}
3959
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003960WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003961weston_output_move(struct weston_output *output, int x, int y)
3962{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003963 struct wl_resource *resource;
3964
3965 output->move_x = x - output->x;
3966 output->move_y = y - output->y;
3967
3968 if (output->move_x == 0 && output->move_y == 0)
3969 return;
3970
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003971 weston_output_init_geometry(output, x, y);
3972
3973 output->dirty = 1;
3974
3975 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003976 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003977
3978 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003979 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003980 wl_output_send_geometry(resource,
3981 output->x,
3982 output->y,
3983 output->mm_width,
3984 output->mm_height,
3985 output->subpixel,
3986 output->make,
3987 output->model,
3988 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003989
3990 if (wl_resource_get_version(resource) >= 2)
3991 wl_output_send_done(resource);
3992 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003993}
3994
3995WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003996weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003997 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003998 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003999{
4000 output->compositor = c;
4001 output->x = x;
4002 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004003 output->mm_width = mm_width;
4004 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004005 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004006 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004007
Alexander Larsson0b135062013-05-28 16:23:36 +02004008 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004009 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004010
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004011 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004012 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004013
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004014 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004015 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004016 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004017 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004018 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004019
Casey Dahlin58ba1372012-04-19 22:50:08 -04004020 output->id = ffs(~output->compositor->output_id_pool) - 1;
4021 output->compositor->output_id_pool |= 1 << output->id;
4022
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004023 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004024 wl_global_create(c->wl_display, &wl_output_interface, 2,
4025 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01004026 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004027}
4028
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004029WL_EXPORT void
4030weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004031 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004032 wl_fixed_t *x, wl_fixed_t *y)
4033{
Derek Foreman0f679412014-10-02 13:41:17 -05004034 struct weston_vector p = { {
4035 wl_fixed_to_double(device_x),
4036 wl_fixed_to_double(device_y),
4037 0.0,
4038 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004039
Derek Foreman0f679412014-10-02 13:41:17 -05004040 weston_matrix_transform(&output->matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004041
Derek Foreman0f679412014-10-02 13:41:17 -05004042 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4043 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004044}
4045
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004046static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004047destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004048{
Jonny Lamb74130762013-11-26 18:19:46 +01004049 struct weston_surface *surface =
4050 wl_resource_get_user_data(resource);
4051
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004052 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004053 surface->pending.buffer_viewport.buffer.src_width =
4054 wl_fixed_from_int(-1);
4055 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004056 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004057}
4058
4059static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004060viewport_destroy(struct wl_client *client,
4061 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004062{
4063 wl_resource_destroy(resource);
4064}
4065
4066static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004067viewport_set(struct wl_client *client,
4068 struct wl_resource *resource,
4069 wl_fixed_t src_x,
4070 wl_fixed_t src_y,
4071 wl_fixed_t src_width,
4072 wl_fixed_t src_height,
4073 int32_t dst_width,
4074 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004075{
Jonny Lamb74130762013-11-26 18:19:46 +01004076 struct weston_surface *surface =
4077 wl_resource_get_user_data(resource);
4078
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004079 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004080
Jonny Lamb8ae35902013-11-26 18:19:45 +01004081 if (wl_fixed_to_double(src_width) < 0 ||
4082 wl_fixed_to_double(src_height) < 0) {
4083 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004084 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004085 "source dimensions must be non-negative (%fx%f)",
4086 wl_fixed_to_double(src_width),
4087 wl_fixed_to_double(src_height));
4088 return;
4089 }
4090
4091 if (dst_width <= 0 || dst_height <= 0) {
4092 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004093 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004094 "destination dimensions must be positive (%dx%d)",
4095 dst_width, dst_height);
4096 return;
4097 }
4098
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004099 surface->pending.buffer_viewport.buffer.src_x = src_x;
4100 surface->pending.buffer_viewport.buffer.src_y = src_y;
4101 surface->pending.buffer_viewport.buffer.src_width = src_width;
4102 surface->pending.buffer_viewport.buffer.src_height = src_height;
4103 surface->pending.buffer_viewport.surface.width = dst_width;
4104 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004105 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004106}
4107
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004108static void
4109viewport_set_source(struct wl_client *client,
4110 struct wl_resource *resource,
4111 wl_fixed_t src_x,
4112 wl_fixed_t src_y,
4113 wl_fixed_t src_width,
4114 wl_fixed_t src_height)
4115{
4116 struct weston_surface *surface =
4117 wl_resource_get_user_data(resource);
4118
4119 assert(surface->viewport_resource != NULL);
4120
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004121 if (src_width == wl_fixed_from_int(-1) &&
4122 src_height == wl_fixed_from_int(-1)) {
4123 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004124 surface->pending.buffer_viewport.buffer.src_width =
4125 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004126 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004127 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004128 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004129
4130 if (src_width <= 0 || src_height <= 0) {
4131 wl_resource_post_error(resource,
4132 WL_VIEWPORT_ERROR_BAD_VALUE,
4133 "source size must be positive (%fx%f)",
4134 wl_fixed_to_double(src_width),
4135 wl_fixed_to_double(src_height));
4136 return;
4137 }
4138
4139 surface->pending.buffer_viewport.buffer.src_x = src_x;
4140 surface->pending.buffer_viewport.buffer.src_y = src_y;
4141 surface->pending.buffer_viewport.buffer.src_width = src_width;
4142 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004143 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004144}
4145
4146static void
4147viewport_set_destination(struct wl_client *client,
4148 struct wl_resource *resource,
4149 int32_t dst_width,
4150 int32_t dst_height)
4151{
4152 struct weston_surface *surface =
4153 wl_resource_get_user_data(resource);
4154
4155 assert(surface->viewport_resource != NULL);
4156
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004157 if (dst_width == -1 && dst_height == -1) {
4158 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004159 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004160 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004161 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004162 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004163
4164 if (dst_width <= 0 || dst_height <= 0) {
4165 wl_resource_post_error(resource,
4166 WL_VIEWPORT_ERROR_BAD_VALUE,
4167 "destination size must be positive (%dx%d)",
4168 dst_width, dst_height);
4169 return;
4170 }
4171
4172 surface->pending.buffer_viewport.surface.width = dst_width;
4173 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004174 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004175}
4176
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004177static const struct wl_viewport_interface viewport_interface = {
4178 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004179 viewport_set,
4180 viewport_set_source,
4181 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004182};
4183
4184static void
4185scaler_destroy(struct wl_client *client,
4186 struct wl_resource *resource)
4187{
4188 wl_resource_destroy(resource);
4189}
4190
4191static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004192scaler_get_viewport(struct wl_client *client,
4193 struct wl_resource *scaler,
4194 uint32_t id,
4195 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004196{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004197 int version = wl_resource_get_version(scaler);
4198 struct weston_surface *surface =
4199 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004200 struct wl_resource *resource;
4201
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004202 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004203 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004204 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4205 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004206 return;
4207 }
4208
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004209 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004210 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004211 if (resource == NULL) {
4212 wl_client_post_no_memory(client);
4213 return;
4214 }
4215
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004216 wl_resource_set_implementation(resource, &viewport_interface,
4217 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004218
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004219 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004220}
4221
4222static const struct wl_scaler_interface scaler_interface = {
4223 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004224 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004225};
4226
4227static void
4228bind_scaler(struct wl_client *client,
4229 void *data, uint32_t version, uint32_t id)
4230{
4231 struct wl_resource *resource;
4232
4233 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004234 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004235 if (resource == NULL) {
4236 wl_client_post_no_memory(client);
4237 return;
4238 }
4239
4240 wl_resource_set_implementation(resource, &scaler_interface,
4241 NULL, NULL);
4242}
4243
4244static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004245destroy_presentation_feedback(struct wl_resource *feedback_resource)
4246{
4247 struct weston_presentation_feedback *feedback;
4248
4249 feedback = wl_resource_get_user_data(feedback_resource);
4250
4251 wl_list_remove(&feedback->link);
4252 free(feedback);
4253}
4254
4255static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004256presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4257{
4258 wl_resource_destroy(resource);
4259}
4260
4261static void
4262presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004263 struct wl_resource *presentation_resource,
4264 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004265 uint32_t callback)
4266{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004267 struct weston_surface *surface;
4268 struct weston_presentation_feedback *feedback;
4269
4270 surface = wl_resource_get_user_data(surface_resource);
4271
Bryce Harringtonde16d892014-11-20 22:21:57 -08004272 feedback = zalloc(sizeof *feedback);
4273 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004274 goto err_calloc;
4275
4276 feedback->resource = wl_resource_create(client,
4277 &presentation_feedback_interface,
4278 1, callback);
4279 if (!feedback->resource)
4280 goto err_create;
4281
4282 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4283 destroy_presentation_feedback);
4284
4285 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4286
4287 return;
4288
4289err_create:
4290 free(feedback);
4291
4292err_calloc:
4293 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004294}
4295
4296static const struct presentation_interface presentation_implementation = {
4297 presentation_destroy,
4298 presentation_feedback
4299};
4300
4301static void
4302bind_presentation(struct wl_client *client,
4303 void *data, uint32_t version, uint32_t id)
4304{
4305 struct weston_compositor *compositor = data;
4306 struct wl_resource *resource;
4307
4308 resource = wl_resource_create(client, &presentation_interface,
4309 MIN(version, 1), id);
4310 if (resource == NULL) {
4311 wl_client_post_no_memory(client);
4312 return;
4313 }
4314
4315 wl_resource_set_implementation(resource, &presentation_implementation,
4316 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004317 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004318}
4319
4320static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004321compositor_bind(struct wl_client *client,
4322 void *data, uint32_t version, uint32_t id)
4323{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004324 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004325 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004326
Jason Ekstranda85118c2013-06-27 20:17:02 -05004327 resource = wl_resource_create(client, &wl_compositor_interface,
4328 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004329 if (resource == NULL) {
4330 wl_client_post_no_memory(client);
4331 return;
4332 }
4333
4334 wl_resource_set_implementation(resource, &compositor_interface,
4335 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004336}
4337
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004338static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004339log_uname(void)
4340{
4341 struct utsname usys;
4342
4343 uname(&usys);
4344
4345 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4346 usys.version, usys.machine);
4347}
4348
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004349WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004350weston_environment_get_fd(const char *env)
4351{
4352 char *e, *end;
4353 int fd, flags;
4354
4355 e = getenv(env);
4356 if (!e)
4357 return -1;
4358 fd = strtol(e, &end, 0);
4359 if (*end != '\0')
4360 return -1;
4361
4362 flags = fcntl(fd, F_GETFD);
4363 if (flags == -1)
4364 return -1;
4365
4366 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4367 unsetenv(env);
4368
4369 return fd;
4370}
4371
Pekka Paalanenb5026542014-11-12 15:09:24 +02004372static void
4373timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4374 uint32_t key, void *data)
4375{
4376 struct weston_compositor *compositor = data;
4377
4378 if (weston_timeline_enabled_)
4379 weston_timeline_close();
4380 else
4381 weston_timeline_open(compositor);
4382}
4383
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004384WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004385weston_compositor_init(struct weston_compositor *ec,
4386 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004387 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004388 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004389{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004390 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004391 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004392 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004393
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004394 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004395 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004396 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004397 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004398 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004399 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004400 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004401 wl_signal_init(&ec->idle_signal);
4402 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004403 wl_signal_init(&ec->show_input_panel_signal);
4404 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004405 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004406 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004407 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004408 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004409 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004410 wl_signal_init(&ec->session_signal);
4411 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004412
Casey Dahlin58ba1372012-04-19 22:50:08 -04004413 ec->output_id_pool = 0;
4414
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004415 if (!wl_global_create(display, &wl_compositor_interface, 3,
4416 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004417 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004418
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004419 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4420 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004421 return -1;
4422
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004423 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004424 ec, bind_scaler))
4425 return -1;
4426
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004427 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4428 ec, bind_presentation))
4429 return -1;
4430
Jason Ekstranda7af7042013-10-12 22:38:11 -05004431 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004432 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004433 wl_list_init(&ec->layer_list);
4434 wl_list_init(&ec->seat_list);
4435 wl_list_init(&ec->output_list);
4436 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004437 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004438 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004439 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004440 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004441 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004442
Xiong Zhang97116532013-10-23 13:58:31 +08004443 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004444 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004445
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004446 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4447 weston_config_section_get_string(s, "keymap_rules",
4448 (char **) &xkb_names.rules, NULL);
4449 weston_config_section_get_string(s, "keymap_model",
4450 (char **) &xkb_names.model, NULL);
4451 weston_config_section_get_string(s, "keymap_layout",
4452 (char **) &xkb_names.layout, NULL);
4453 weston_config_section_get_string(s, "keymap_variant",
4454 (char **) &xkb_names.variant, NULL);
4455 weston_config_section_get_string(s, "keymap_options",
4456 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004457
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004458 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4459 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004460
Jonny Lamb66a41a02014-08-12 14:58:25 +02004461 weston_config_section_get_int(s, "repeat-rate",
4462 &ec->kb_repeat_rate, 40);
4463 weston_config_section_get_int(s, "repeat-delay",
4464 &ec->kb_repeat_delay, 400);
4465
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004466 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004467
4468 wl_data_device_manager_init(ec->wl_display);
4469
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004470 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004471
Daniel Stone725c2c32012-06-22 14:04:36 +01004472 loop = wl_display_get_event_loop(ec->wl_display);
4473 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4474 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4475
4476 ec->input_loop = wl_event_loop_create();
4477
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004478 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4479 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4480
Pekka Paalanenb5026542014-11-12 15:09:24 +02004481 weston_compositor_add_debug_binding(ec, KEY_T,
4482 timeline_key_binding_handler, ec);
4483
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004484 weston_compositor_schedule_repaint(ec);
4485
Daniel Stone725c2c32012-06-22 14:04:36 +01004486 return 0;
4487}
4488
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004489WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004490weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004491{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004492 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004493
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004494 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004495 if (ec->input_loop_source)
4496 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004497
Matt Roper361d2ad2011-08-29 13:52:23 -07004498 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004499 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004500 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004501
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004502 if (ec->renderer)
4503 ec->renderer->destroy(ec);
4504
Daniel Stone325fc2d2012-05-30 16:31:58 +01004505 weston_binding_list_destroy_all(&ec->key_binding_list);
4506 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004507 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004508 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004509 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004510
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004511 weston_plane_release(&ec->primary_plane);
4512
Jonas Ådahlc97af922012-03-28 22:36:09 +02004513 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004514
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004515 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004516}
4517
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004518WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004519weston_compositor_exit_with_code(struct weston_compositor *compositor,
4520 int exit_code)
4521{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004522 if (compositor->exit_code == EXIT_SUCCESS)
4523 compositor->exit_code = exit_code;
4524
Frederic Plourdec336f062014-10-29 14:44:33 -04004525 wl_display_terminate(compositor->wl_display);
4526}
4527
4528WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004529weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4530 const struct weston_pointer_grab_interface *interface)
4531{
4532 struct weston_seat *seat;
4533
4534 ec->default_pointer_grab = interface;
4535 wl_list_for_each(seat, &ec->seat_list, link) {
4536 if (seat->pointer) {
4537 weston_pointer_set_default_grab(seat->pointer,
4538 interface);
4539 }
4540 }
4541}
4542
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004543WL_EXPORT int
4544weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4545 clockid_t clk_id)
4546{
4547 struct timespec ts;
4548
4549 if (clock_gettime(clk_id, &ts) < 0)
4550 return -1;
4551
4552 compositor->presentation_clock = clk_id;
4553
4554 return 0;
4555}
4556
4557/*
4558 * For choosing the software clock, when the display hardware or API
4559 * does not expose a compatible presentation timestamp.
4560 */
4561WL_EXPORT int
4562weston_compositor_set_presentation_clock_software(
4563 struct weston_compositor *compositor)
4564{
4565 /* In order of preference */
4566 static const clockid_t clocks[] = {
4567 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4568 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4569 CLOCK_MONOTONIC, /* no jumps, may crawl */
4570 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4571 CLOCK_REALTIME /* may jump and crawl */
4572 };
4573 unsigned i;
4574
4575 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4576 if (weston_compositor_set_presentation_clock(compositor,
4577 clocks[i]) == 0)
4578 return 0;
4579
4580 weston_log("Error: no suitable presentation clock available.\n");
4581
4582 return -1;
4583}
4584
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004585WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004586weston_version(int *major, int *minor, int *micro)
4587{
4588 *major = WESTON_VERSION_MAJOR;
4589 *minor = WESTON_VERSION_MINOR;
4590 *micro = WESTON_VERSION_MICRO;
4591}
4592
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004593static const char *
4594clock_name(clockid_t clk_id)
4595{
4596 static const char *names[] = {
4597 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4598 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4599 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4600 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4601 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4602 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4603 };
4604
4605 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4606 return "unknown";
4607
4608 return names[clk_id];
4609}
4610
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004611static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004612 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004613 const char *desc;
4614} capability_strings[] = {
4615 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004616 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004617};
4618
4619static void
4620weston_compositor_log_capabilities(struct weston_compositor *compositor)
4621{
4622 unsigned i;
4623 int yes;
4624
4625 weston_log("Compositor capabilities:\n");
4626 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4627 yes = compositor->capabilities & capability_strings[i].bit;
4628 weston_log_continue(STAMP_SPACE "%s %s\n",
4629 capability_strings[i].desc,
4630 yes ? "yes" : "no");
4631 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004632
4633 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4634 clock_name(compositor->presentation_clock),
4635 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004636}
4637
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004638static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004639{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004640 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004641
Martin Minarik6d118362012-06-07 18:01:59 +02004642 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004643 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004644
4645 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004646}
4647
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004648#ifdef HAVE_LIBUNWIND
4649
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004650static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004651print_backtrace(void)
4652{
4653 unw_cursor_t cursor;
4654 unw_context_t context;
4655 unw_word_t off;
4656 unw_proc_info_t pip;
4657 int ret, i = 0;
4658 char procname[256];
4659 const char *filename;
4660 Dl_info dlinfo;
4661
4662 pip.unwind_info = NULL;
4663 ret = unw_getcontext(&context);
4664 if (ret) {
4665 weston_log("unw_getcontext: %d\n", ret);
4666 return;
4667 }
4668
4669 ret = unw_init_local(&cursor, &context);
4670 if (ret) {
4671 weston_log("unw_init_local: %d\n", ret);
4672 return;
4673 }
4674
4675 ret = unw_step(&cursor);
4676 while (ret > 0) {
4677 ret = unw_get_proc_info(&cursor, &pip);
4678 if (ret) {
4679 weston_log("unw_get_proc_info: %d\n", ret);
4680 break;
4681 }
4682
4683 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4684 if (ret && ret != -UNW_ENOMEM) {
4685 if (ret != -UNW_EUNSPEC)
4686 weston_log("unw_get_proc_name: %d\n", ret);
4687 procname[0] = '?';
4688 procname[1] = 0;
4689 }
4690
4691 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4692 *dlinfo.dli_fname)
4693 filename = dlinfo.dli_fname;
4694 else
4695 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004696
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004697 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4698 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4699
4700 ret = unw_step(&cursor);
4701 if (ret < 0)
4702 weston_log("unw_step: %d\n", ret);
4703 }
4704}
4705
4706#else
4707
4708static void
4709print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004710{
4711 void *buffer[32];
4712 int i, count;
4713 Dl_info info;
4714
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004715 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4716 for (i = 0; i < count; i++) {
4717 dladdr(buffer[i], &info);
4718 weston_log(" [%016lx] %s (%s)\n",
4719 (long) buffer[i],
4720 info.dli_sname ? info.dli_sname : "--",
4721 info.dli_fname);
4722 }
4723}
4724
4725#endif
4726
4727static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004728on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004729{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004730 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004731 * then call the backend restore function, which will switch
4732 * back to the vt we launched from or ungrab X etc and then
4733 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004734 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004735 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004736 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004737
Peter Maatmane5b42e42013-03-27 22:38:53 +01004738 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004739
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004740 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004741
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004742 segv_compositor->restore(segv_compositor);
4743
4744 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004745}
4746
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004747WL_EXPORT void *
4748weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004749{
4750 char path[PATH_MAX];
4751 void *module, *init;
4752
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004753 if (name == NULL)
4754 return NULL;
4755
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004756 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004757 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004758 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004759 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004760
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004761 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4762 if (module) {
4763 weston_log("Module '%s' already loaded\n", path);
4764 dlclose(module);
4765 return NULL;
4766 }
4767
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004768 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004769 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004770 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004771 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004772 return NULL;
4773 }
4774
4775 init = dlsym(module, entrypoint);
4776 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004777 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004778 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004779 return NULL;
4780 }
4781
4782 return init;
4783}
4784
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004785static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004786load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004787 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004788{
4789 const char *p, *end;
4790 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004791 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004792 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004793
4794 if (modules == NULL)
4795 return 0;
4796
4797 p = modules;
4798 while (*p) {
4799 end = strchrnul(p, ',');
4800 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004801 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004802 if (!module_init)
4803 return -1;
4804 if (module_init(ec, argc, argv) < 0)
4805 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004806 p = end;
4807 while (*p == ',')
4808 p++;
4809
4810 }
4811
4812 return 0;
4813}
4814
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004815static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004816 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4817
4818static const char xdg_wrong_message[] =
4819 "fatal: environment variable XDG_RUNTIME_DIR\n"
4820 "is set to \"%s\", which is not a directory.\n";
4821
4822static const char xdg_wrong_mode_message[] =
4823 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004824 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4825 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004826
4827static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004828 "Refer to your distribution on how to get it, or\n"
4829 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4830 "on how to implement it.\n";
4831
Martin Minarik37032f82012-06-18 20:15:18 +02004832static void
4833verify_xdg_runtime_dir(void)
4834{
4835 char *dir = getenv("XDG_RUNTIME_DIR");
4836 struct stat s;
4837
4838 if (!dir) {
4839 weston_log(xdg_error_message);
4840 weston_log_continue(xdg_detail_message);
4841 exit(EXIT_FAILURE);
4842 }
4843
4844 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4845 weston_log(xdg_wrong_message, dir);
4846 weston_log_continue(xdg_detail_message);
4847 exit(EXIT_FAILURE);
4848 }
4849
4850 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4851 weston_log(xdg_wrong_mode_message,
4852 dir, s.st_mode & 0777, s.st_uid);
4853 weston_log_continue(xdg_detail_message);
4854 }
4855}
4856
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004857static int
4858usage(int error_code)
4859{
4860 fprintf(stderr,
4861 "Usage: weston [OPTIONS]\n\n"
4862 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4863 "Weston supports multiple backends, and depending on which backend is in use\n"
4864 "different options will be accepted.\n\n"
4865
4866
4867 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004868 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004869 " -B, --backend=MODULE\tBackend module, one of\n"
4870#if defined(BUILD_DRM_COMPOSITOR)
4871 "\t\t\t\tdrm-backend.so\n"
4872#endif
4873#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004874 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004875#endif
4876#if defined(BUILD_X11_COMPOSITOR)
4877 "\t\t\t\tx11-backend.so\n"
4878#endif
4879#if defined(BUILD_WAYLAND_COMPOSITOR)
4880 "\t\t\t\twayland-backend.so\n"
4881#endif
4882#if defined(BUILD_RDP_COMPOSITOR)
4883 "\t\t\t\trdp-backend.so\n"
4884#endif
4885#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4886 "\t\t\t\trpi-backend.so\n"
4887#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004888 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004889 " -S, --socket=NAME\tName of socket to listen on\n"
4890 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004891 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004892 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004893 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004894 " -h, --help\t\tThis help message\n\n");
4895
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004896#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004897 fprintf(stderr,
4898 "Options for drm-backend.so:\n\n"
4899 " --connector=ID\tBring up only this connector\n"
4900 " --seat=SEAT\t\tThe seat that weston should run on\n"
4901 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004902 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004903 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004904#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004905
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004906#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004907 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004908 "Options for fbdev-backend.so:\n\n"
4909 " --tty=TTY\t\tThe tty to use\n"
4910 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004911#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004912
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004913#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004914 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004915 "Options for x11-backend.so:\n\n"
4916 " --width=WIDTH\t\tWidth of X window\n"
4917 " --height=HEIGHT\tHeight of X window\n"
4918 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004919 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004920 " --output-count=COUNT\tCreate multiple outputs\n"
4921 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004922#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004923
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004924#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004925 fprintf(stderr,
4926 "Options for wayland-backend.so:\n\n"
4927 " --width=WIDTH\t\tWidth of Wayland surface\n"
4928 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004929 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004930 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004931 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004932 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004933 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004934 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004935#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004936
Pekka Paalanene31e0532013-05-22 18:03:07 +03004937#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4938 fprintf(stderr,
4939 "Options for rpi-backend.so:\n\n"
4940 " --tty=TTY\t\tThe tty to use\n"
4941 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4942 " --transform=TR\tThe output transformation, TR is one of:\n"
4943 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004944 " --opaque-regions\tEnable support for opaque regions, can be "
4945 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004946 "\n");
4947#endif
4948
Hardeningc077a842013-07-08 00:51:35 +02004949#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004950 fprintf(stderr,
4951 "Options for rdp-backend.so:\n\n"
4952 " --width=WIDTH\t\tWidth of desktop\n"
4953 " --height=HEIGHT\tHeight of desktop\n"
4954 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4955 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004956 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004957 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4958 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4959 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4960 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4961 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004962#endif
4963
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004964#if defined(BUILD_HEADLESS_COMPOSITOR)
4965 fprintf(stderr,
4966 "Options for headless-backend.so:\n\n"
4967 " --width=WIDTH\t\tWidth of memory surface\n"
4968 " --height=HEIGHT\tHeight of memory surface\n"
4969 " --transform=TR\tThe output transformation, TR is one of:\n"
4970 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4971 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4972#endif
4973
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004974 exit(error_code);
4975}
4976
Peter Maatmane5b42e42013-03-27 22:38:53 +01004977static void
4978catch_signals(void)
4979{
4980 struct sigaction action;
4981
4982 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4983 action.sa_sigaction = on_caught_signal;
4984 sigemptyset(&action.sa_mask);
4985 sigaction(SIGSEGV, &action, NULL);
4986 sigaction(SIGABRT, &action, NULL);
4987}
4988
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004989static void
4990handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4991{
4992 struct wl_client *client = data;
4993
4994 weston_log("Primary client died. Closing...\n");
4995
4996 wl_display_terminate(wl_client_get_display(client));
4997}
4998
Ryo Munakatad8deff62014-09-06 07:32:05 +09004999static char *
5000weston_choose_default_backend(void)
5001{
5002 char *backend = NULL;
5003
5004 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5005 backend = strdup("wayland-backend.so");
5006 else if (getenv("DISPLAY"))
5007 backend = strdup("x11-backend.so");
5008 else
5009 backend = strdup(WESTON_NATIVE_BACKEND);
5010
5011 return backend;
5012}
5013
5014static int
5015weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5016{
5017 if (socket_name) {
5018 if (wl_display_add_socket(display, socket_name)) {
5019 weston_log("fatal: failed to add socket: %m\n");
5020 return -1;
5021 }
5022 } else {
5023 socket_name = wl_display_add_socket_auto(display);
5024 if (!socket_name) {
5025 weston_log("fatal: failed to add socket: %m\n");
5026 return -1;
5027 }
5028 }
5029
5030 setenv("WAYLAND_DISPLAY", socket_name, 1);
5031
5032 return 0;
5033}
5034
Derek Foreman64a3df02014-10-23 12:24:18 -05005035static const struct { const char *name; uint32_t token; } transforms[] = {
5036 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5037 { "90", WL_OUTPUT_TRANSFORM_90 },
5038 { "180", WL_OUTPUT_TRANSFORM_180 },
5039 { "270", WL_OUTPUT_TRANSFORM_270 },
5040 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5041 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5042 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5043 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5044};
5045
5046WL_EXPORT int
5047weston_parse_transform(const char *transform, uint32_t *out)
5048{
5049 unsigned int i;
5050
5051 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5052 if (strcmp(transforms[i].name, transform) == 0) {
5053 *out = transforms[i].token;
5054 return 0;
5055 }
5056
5057 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5058 return -1;
5059}
5060
5061WL_EXPORT const char *
5062weston_transform_to_string(uint32_t output_transform)
5063{
5064 unsigned int i;
5065
5066 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5067 if (transforms[i].token == output_transform)
5068 return transforms[i].name;
5069
5070 return "<illegal value>";
5071}
5072
5073
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005074int main(int argc, char *argv[])
5075{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005076 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005077 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005078 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005079 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005080 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005081 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005082 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005083 int *argc, char *argv[],
5084 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005085 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005086 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005087 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005088 char *modules = NULL;
5089 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005090 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005091 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005092 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005093 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005094 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005095 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005096 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005097 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005098 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005099 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005100 struct wl_client *primary_client;
5101 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005102 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005103
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005104 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005105 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5106 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005107 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5108 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005109 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005110 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005111 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005112 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005113 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005114 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005115
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005116 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005117
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005118 if (help)
5119 usage(EXIT_SUCCESS);
5120
Scott Moreau12245142012-08-29 15:15:58 -06005121 if (version) {
5122 printf(PACKAGE_STRING "\n");
5123 return EXIT_SUCCESS;
5124 }
5125
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005126 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005127
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005128 weston_log("%s\n"
5129 STAMP_SPACE "%s\n"
5130 STAMP_SPACE "Bug reports to: %s\n"
5131 STAMP_SPACE "Build: %s\n",
5132 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005133 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005134 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005135
Martin Minarik37032f82012-06-18 20:15:18 +02005136 verify_xdg_runtime_dir();
5137
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005138 display = wl_display_create();
5139
Tiago Vignatti2116b892011-08-08 05:52:59 -07005140 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005141 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5142 display);
5143 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5144 display);
5145 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5146 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005147
5148 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005149 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5150 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005151
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305152 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
5153 ret = EXIT_FAILURE;
5154 goto out_signals;
5155 }
5156
Pekka Paalanen588bee12014-05-07 16:26:25 +03005157 if (noconfig == 0)
5158 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005159 if (config != NULL) {
5160 weston_log("Using config file '%s'\n",
5161 weston_config_get_full_path(config));
5162 } else {
5163 weston_log("Starting with no config file.\n");
5164 }
5165 section = weston_config_get_section(config, "core", NULL, NULL);
5166
Ryo Munakata03faed22014-09-06 07:32:51 +09005167 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005168 weston_config_section_get_string(section, "backend", &backend,
5169 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005170 if (!backend)
5171 backend = weston_choose_default_backend();
5172 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005173
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005174 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305175 if (!backend_init) {
5176 ret = EXIT_FAILURE;
5177 goto out_signals;
5178 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05005179
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005180 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005181 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005182 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305183 ret = EXIT_FAILURE;
5184 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005185 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005186
Peter Maatmane5b42e42013-03-27 22:38:53 +01005187 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005188 segv_compositor = ec;
5189
Frederic Plourde4a84c832014-10-30 15:06:34 -04005190 if (idle_time < 0)
5191 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5192 if (idle_time < 0)
5193 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005194 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005195 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005196 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005197
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005198 for (i = 1; i < argc; i++)
5199 weston_log("fatal: unhandled option: %s\n", argv[i]);
5200 if (argc > 1) {
5201 ret = EXIT_FAILURE;
5202 goto out;
5203 }
5204
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005205 weston_compositor_log_capabilities(ec);
5206
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005207 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5208 if (server_socket) {
5209 weston_log("Running with single client\n");
5210 fd = strtol(server_socket, &end, 0);
5211 if (*end != '\0')
5212 fd = -1;
5213 } else {
5214 fd = -1;
5215 }
5216
5217 if (fd != -1) {
5218 primary_client = wl_client_create(display, fd);
5219 if (!primary_client) {
5220 weston_log("fatal: failed to add client: %m\n");
5221 ret = EXIT_FAILURE;
5222 goto out;
5223 }
5224 primary_client_destroyed.notify =
5225 handle_primary_client_destroyed;
5226 wl_client_add_destroy_listener(primary_client,
5227 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005228 } else if (weston_create_listening_socket(display, socket_name)) {
5229 ret = EXIT_FAILURE;
5230 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005231 }
5232
Ryo Munakata03faed22014-09-06 07:32:51 +09005233 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005234 weston_config_section_get_string(section, "shell", &shell,
5235 "desktop-shell.so");
5236
Ryo Munakata03faed22014-09-06 07:32:51 +09005237 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005238 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005239
5240 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005241 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005242 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005243
5244 if (load_modules(ec, option_modules, &argc, argv) < 0)
5245 goto out;
5246
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005247 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5248 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5249 if (numlock_on) {
5250 wl_list_for_each(seat, &ec->seat_list, link) {
5251 if (seat->keyboard)
5252 weston_keyboard_set_locks(seat->keyboard,
5253 WESTON_NUM_LOCK,
5254 WESTON_NUM_LOCK);
5255 }
5256 }
5257
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005258 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005259
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005260 wl_display_run(display);
5261
Frederic Plourdec336f062014-10-29 14:44:33 -04005262 /* Allow for setting return exit code after
5263 * wl_display_run returns normally. This is
5264 * useful for devs/testers and automated tests
5265 * that want to indicate failure status to
5266 * testing infrastructure above
5267 */
5268 ret = ec->exit_code;
5269
Ryo Munakata03faed22014-09-06 07:32:51 +09005270out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005271 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005272 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005273
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005274 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005275
Daniel Stone855028f2012-05-01 20:37:10 +01005276 weston_compositor_xkb_destroy(ec);
5277
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005278 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305279
5280out_signals:
5281 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5282 if (signals[i])
5283 wl_event_source_remove(signals[i]);
5284
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005285 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005286
Martin Minarik19e6f262012-06-07 13:08:46 +02005287 weston_log_file_close();
5288
Ryo Munakata03faed22014-09-06 07:32:51 +09005289 free(backend);
5290 free(shell);
5291 free(socket_name);
5292 free(option_modules);
5293 free(log);
5294 free(modules);
5295
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005296 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005297}