blob: 45e8f5c3e87846edd166d8a346ad022fbb03b64a [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
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400645 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500646}
647
Alex Wu8811bf92012-02-28 18:07:54 +0800648WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500649weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200650 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500651{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100652 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500653}
654
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400655WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500656weston_view_to_global_float(struct weston_view *view,
657 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200658{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200660 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
661
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663
664 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200665 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700666 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200667 v.f[3]);
668 *x = 0;
669 *y = 0;
670 return;
671 }
672
673 *x = v.f[0] / v.f[3];
674 *y = v.f[1] / v.f[3];
675 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500676 *x = sx + view->geometry.x;
677 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200678 }
679}
680
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500681WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200682weston_transformed_coord(int width, int height,
683 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200684 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685 float sx, float sy, float *bx, float *by)
686{
687 switch (transform) {
688 case WL_OUTPUT_TRANSFORM_NORMAL:
689 default:
690 *bx = sx;
691 *by = sy;
692 break;
693 case WL_OUTPUT_TRANSFORM_FLIPPED:
694 *bx = width - sx;
695 *by = sy;
696 break;
697 case WL_OUTPUT_TRANSFORM_90:
698 *bx = height - sy;
699 *by = sx;
700 break;
701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
702 *bx = height - sy;
703 *by = width - sx;
704 break;
705 case WL_OUTPUT_TRANSFORM_180:
706 *bx = width - sx;
707 *by = height - sy;
708 break;
709 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
710 *bx = sx;
711 *by = height - sy;
712 break;
713 case WL_OUTPUT_TRANSFORM_270:
714 *bx = sy;
715 *by = width - sx;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
718 *bx = sy;
719 *by = sx;
720 break;
721 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200722
723 *bx *= scale;
724 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200725}
726
727WL_EXPORT pixman_box32_t
728weston_transformed_rect(int width, int height,
729 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200730 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200731 pixman_box32_t rect)
732{
733 float x1, x2, y1, y2;
734
735 pixman_box32_t ret;
736
Alexander Larsson4ea95522013-05-22 14:41:37 +0200737 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200738 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200739 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200740 rect.x2, rect.y2, &x2, &y2);
741
742 if (x1 <= x2) {
743 ret.x1 = x1;
744 ret.x2 = x2;
745 } else {
746 ret.x1 = x2;
747 ret.x2 = x1;
748 }
749
750 if (y1 <= y2) {
751 ret.y1 = y1;
752 ret.y2 = y2;
753 } else {
754 ret.y1 = y2;
755 ret.y2 = y1;
756 }
757
758 return ret;
759}
760
761WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500762weston_transformed_region(int width, int height,
763 enum wl_output_transform transform,
764 int32_t scale,
765 pixman_region32_t *src, pixman_region32_t *dest)
766{
767 pixman_box32_t *src_rects, *dest_rects;
768 int nrects, i;
769
770 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
771 if (src != dest)
772 pixman_region32_copy(dest, src);
773 return;
774 }
775
776 src_rects = pixman_region32_rectangles(src, &nrects);
777 dest_rects = malloc(nrects * sizeof(*dest_rects));
778 if (!dest_rects)
779 return;
780
781 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
782 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
783 } else {
784 for (i = 0; i < nrects; i++) {
785 switch (transform) {
786 default:
787 case WL_OUTPUT_TRANSFORM_NORMAL:
788 dest_rects[i].x1 = src_rects[i].x1;
789 dest_rects[i].y1 = src_rects[i].y1;
790 dest_rects[i].x2 = src_rects[i].x2;
791 dest_rects[i].y2 = src_rects[i].y2;
792 break;
793 case WL_OUTPUT_TRANSFORM_90:
794 dest_rects[i].x1 = height - src_rects[i].y2;
795 dest_rects[i].y1 = src_rects[i].x1;
796 dest_rects[i].x2 = height - src_rects[i].y1;
797 dest_rects[i].y2 = src_rects[i].x2;
798 break;
799 case WL_OUTPUT_TRANSFORM_180:
800 dest_rects[i].x1 = width - src_rects[i].x2;
801 dest_rects[i].y1 = height - src_rects[i].y2;
802 dest_rects[i].x2 = width - src_rects[i].x1;
803 dest_rects[i].y2 = height - src_rects[i].y1;
804 break;
805 case WL_OUTPUT_TRANSFORM_270:
806 dest_rects[i].x1 = src_rects[i].y1;
807 dest_rects[i].y1 = width - src_rects[i].x2;
808 dest_rects[i].x2 = src_rects[i].y2;
809 dest_rects[i].y2 = width - src_rects[i].x1;
810 break;
811 case WL_OUTPUT_TRANSFORM_FLIPPED:
812 dest_rects[i].x1 = width - src_rects[i].x2;
813 dest_rects[i].y1 = src_rects[i].y1;
814 dest_rects[i].x2 = width - src_rects[i].x1;
815 dest_rects[i].y2 = src_rects[i].y2;
816 break;
817 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
818 dest_rects[i].x1 = height - src_rects[i].y2;
819 dest_rects[i].y1 = width - src_rects[i].x2;
820 dest_rects[i].x2 = height - src_rects[i].y1;
821 dest_rects[i].y2 = width - src_rects[i].x1;
822 break;
823 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
824 dest_rects[i].x1 = src_rects[i].x1;
825 dest_rects[i].y1 = height - src_rects[i].y2;
826 dest_rects[i].x2 = src_rects[i].x2;
827 dest_rects[i].y2 = height - src_rects[i].y1;
828 break;
829 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
830 dest_rects[i].x1 = src_rects[i].y1;
831 dest_rects[i].y1 = src_rects[i].x1;
832 dest_rects[i].x2 = src_rects[i].y2;
833 dest_rects[i].y2 = src_rects[i].x2;
834 break;
835 }
836 }
837 }
838
839 if (scale != 1) {
840 for (i = 0; i < nrects; i++) {
841 dest_rects[i].x1 *= scale;
842 dest_rects[i].x2 *= scale;
843 dest_rects[i].y1 *= scale;
844 dest_rects[i].y2 *= scale;
845 }
846 }
847
848 pixman_region32_clear(dest);
849 pixman_region32_init_rects(dest, dest_rects, nrects);
850 free(dest_rects);
851}
852
Jonny Lamb74130762013-11-26 18:19:46 +0100853static void
854scaler_surface_to_buffer(struct weston_surface *surface,
855 float sx, float sy, float *bx, float *by)
856{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200857 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200858 double src_width, src_height;
859 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
862 if (vp->surface.width == -1) {
863 *bx = sx;
864 *by = sy;
865 return;
866 }
Jonny Lamb74130762013-11-26 18:19:46 +0100867
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200868 src_x = 0.0;
869 src_y = 0.0;
870 src_width = surface->width_from_buffer;
871 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100872 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200873 src_x = wl_fixed_to_double(vp->buffer.src_x);
874 src_y = wl_fixed_to_double(vp->buffer.src_y);
875 src_width = wl_fixed_to_double(vp->buffer.src_width);
876 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100877 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200878
879 *bx = sx * src_width / surface->width + src_x;
880 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100881}
882
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500883WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200884weston_surface_to_buffer_float(struct weston_surface *surface,
885 float sx, float sy, float *bx, float *by)
886{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200887 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
888
Jonny Lamb74130762013-11-26 18:19:46 +0100889 /* first transform coordinates if the scaler is set */
890 scaler_surface_to_buffer(surface, sx, sy, bx, by);
891
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500892 weston_transformed_coord(surface->width_from_buffer,
893 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200894 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100895 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200896}
897
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898WL_EXPORT void
899weston_surface_to_buffer(struct weston_surface *surface,
900 int sx, int sy, int *bx, int *by)
901{
902 float bxf, byf;
903
Jonny Lamb74130762013-11-26 18:19:46 +0100904 weston_surface_to_buffer_float(surface,
905 sx, sy, &bxf, &byf);
906
Alexander Larsson4ea95522013-05-22 14:41:37 +0200907 *bx = floorf(bxf);
908 *by = floorf(byf);
909}
910
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200911WL_EXPORT pixman_box32_t
912weston_surface_to_buffer_rect(struct weston_surface *surface,
913 pixman_box32_t rect)
914{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200915 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100916 float xf, yf;
917
918 /* first transform box coordinates if the scaler is set */
919 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
920 rect.x1 = floorf(xf);
921 rect.y1 = floorf(yf);
922
923 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
924 rect.x2 = floorf(xf);
925 rect.y2 = floorf(yf);
926
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500927 return weston_transformed_rect(surface->width_from_buffer,
928 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200929 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200931}
932
933WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400935 struct weston_plane *plane)
936{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938 return;
939
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 weston_view_damage_below(view);
941 view->plane = plane;
942 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400943}
944
Pekka Paalanen51723d52015-02-17 13:10:01 +0200945/** Inflict damage on the plane where the view is visible.
946 *
947 * \param view The view that causes the damage.
948 *
949 * If the view is currently on a plane (including the primary plane),
950 * take the view's boundingbox, subtract all the opaque views that cover it,
951 * and add the remaining region as damage to the plane. This corresponds
952 * to the damage inflicted to the plane if this view disappeared.
953 *
954 * A repaint is scheduled for this view.
955 *
956 * The region of all opaque views covering this view is stored in
957 * weston_view::clip and updated by view_accumulate_damage() during
958 * weston_output_repaint(). Specifically, that region matches the
959 * scenegraph as it was last painted.
960 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400961WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200963{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500964 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200965
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500966 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200967 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800969 if (view->plane)
970 pixman_region32_union(&view->plane->damage,
971 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500972 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800973 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200974}
975
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400976static void
977weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
978{
979 uint32_t different = es->output_mask ^ mask;
980 uint32_t entered = mask & different;
981 uint32_t left = es->output_mask & different;
982 struct weston_output *output;
983 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500984 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400985
986 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500987 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400988 return;
989 if (different == 0)
990 return;
991
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500992 client = wl_resource_get_client(es->resource);
993
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400994 wl_list_for_each(output, &es->compositor->output_list, link) {
995 if (1 << output->id & different)
996 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500997 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400998 client);
999 if (resource == NULL)
1000 continue;
1001 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001002 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001003 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001004 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001005 }
1006}
1007
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001008
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001009static void
1010weston_surface_assign_output(struct weston_surface *es)
1011{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 struct weston_output *new_output;
1013 struct weston_view *view;
1014 pixman_region32_t region;
1015 uint32_t max, area, mask;
1016 pixman_box32_t *e;
1017
1018 new_output = NULL;
1019 max = 0;
1020 mask = 0;
1021 pixman_region32_init(&region);
1022 wl_list_for_each(view, &es->views, surface_link) {
1023 if (!view->output)
1024 continue;
1025
1026 pixman_region32_intersect(&region, &view->transform.boundingbox,
1027 &view->output->region);
1028
1029 e = pixman_region32_extents(&region);
1030 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1031
1032 mask |= view->output_mask;
1033
1034 if (area >= max) {
1035 new_output = view->output;
1036 max = area;
1037 }
1038 }
1039 pixman_region32_fini(&region);
1040
1041 es->output = new_output;
1042 weston_surface_update_output_mask(es, mask);
1043}
1044
1045static void
1046weston_view_assign_output(struct weston_view *ev)
1047{
1048 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049 struct weston_output *output, *new_output;
1050 pixman_region32_t region;
1051 uint32_t max, area, mask;
1052 pixman_box32_t *e;
1053
1054 new_output = NULL;
1055 max = 0;
1056 mask = 0;
1057 pixman_region32_init(&region);
1058 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001060 &output->region);
1061
1062 e = pixman_region32_extents(&region);
1063 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1064
1065 if (area > 0)
1066 mask |= 1 << output->id;
1067
1068 if (area >= max) {
1069 new_output = output;
1070 max = area;
1071 }
1072 }
1073 pixman_region32_fini(&region);
1074
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 ev->output = new_output;
1076 ev->output_mask = mask;
1077
1078 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001079}
1080
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001081static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001082weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1083 int from_x, int from_y, int *to_x, int *to_y)
1084{
1085 float x, y;
1086
1087 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1088 weston_view_from_global_float(to, x, y, &x, &y);
1089
1090 *to_x = round(x);
1091 *to_y = round(y);
1092}
1093
1094static void
1095weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1096{
1097 pixman_box32_t *a;
1098 pixman_box32_t b;
1099
1100 a = pixman_region32_extents(&from->geometry.scissor);
1101
1102 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1103 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1104
1105 pixman_region32_fini(&to->geometry.scissor);
1106 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1107}
1108
1109static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001110view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001112{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001113 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1114 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001115 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001116 { inbox->x1, inbox->y1 },
1117 { inbox->x1, inbox->y2 },
1118 { inbox->x2, inbox->y1 },
1119 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001120 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001121 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001122 int i;
1123
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001124 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001125 /* avoid rounding empty bbox to 1x1 */
1126 pixman_region32_init(bbox);
1127 return;
1128 }
1129
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001130 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001131 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001133 if (x < min_x)
1134 min_x = x;
1135 if (x > max_x)
1136 max_x = x;
1137 if (y < min_y)
1138 min_y = y;
1139 if (y > max_y)
1140 max_y = y;
1141 }
1142
Pekka Paalanen219b9822012-02-08 15:38:37 +02001143 int_x = floorf(min_x);
1144 int_y = floorf(min_y);
1145 pixman_region32_init_rect(bbox, int_x, int_y,
1146 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001147}
1148
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001149static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001151{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001153
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001154 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 view->geometry.x = roundf(view->geometry.x);
1156 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001157
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001158 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1160 view->transform.position.matrix.d[12] = view->geometry.x;
1161 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001162
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 view->transform.inverse = view->transform.position.matrix;
1166 view->transform.inverse.d[12] = -view->geometry.x;
1167 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001168
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001170 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001171 view->surface->width,
1172 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001173 if (view->geometry.scissor_enabled)
1174 pixman_region32_intersect(&view->transform.boundingbox,
1175 &view->transform.boundingbox,
1176 &view->geometry.scissor);
1177
1178 pixman_region32_translate(&view->transform.boundingbox,
1179 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001180
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 if (view->alpha == 1.0) {
1182 pixman_region32_copy(&view->transform.opaque,
1183 &view->surface->opaque);
1184 pixman_region32_translate(&view->transform.opaque,
1185 view->geometry.x,
1186 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001187 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188}
1189
1190static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001192{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 struct weston_view *parent = view->geometry.parent;
1194 struct weston_matrix *matrix = &view->transform.matrix;
1195 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001196 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001197 pixman_region32_t surfregion;
1198 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001201
1202 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1204 view->transform.position.matrix.d[12] = view->geometry.x;
1205 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001206
1207 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001209 weston_matrix_multiply(matrix, &tform->matrix);
1210
Pekka Paalanen483243f2013-03-08 14:56:50 +02001211 if (parent)
1212 weston_matrix_multiply(matrix, &parent->transform.matrix);
1213
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214 if (weston_matrix_invert(inverse, matrix) < 0) {
1215 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 weston_log("error: weston_view %p"
1217 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001218 return -1;
1219 }
1220
Pekka Paalanen380adf52015-02-16 14:39:11 +02001221 pixman_region32_init_rect(&surfregion, 0, 0,
1222 view->surface->width, view->surface->height);
1223 if (view->geometry.scissor_enabled)
1224 pixman_region32_intersect(&surfregion, &surfregion,
1225 &view->geometry.scissor);
1226 surfbox = pixman_region32_extents(&surfregion);
1227
1228 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1229 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001230
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001231 return 0;
1232}
1233
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001234static struct weston_layer *
1235get_view_layer(struct weston_view *view)
1236{
1237 if (view->parent_view)
1238 return get_view_layer(view->parent_view);
1239 return view->layer_link.layer;
1240}
1241
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001242WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001244{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001246 struct weston_layer *layer;
1247 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001250 return;
1251
Pekka Paalanen483243f2013-03-08 14:56:50 +02001252 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001254
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001256
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 pixman_region32_fini(&view->transform.boundingbox);
1260 pixman_region32_fini(&view->transform.opaque);
1261 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001262
Pekka Paalanencd403622012-01-25 13:37:39 +02001263 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 if (view->geometry.transformation_list.next ==
1265 &view->transform.position.link &&
1266 view->geometry.transformation_list.prev ==
1267 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001268 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 if (weston_view_update_transform_enable(view) < 0)
1272 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001273 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001274
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001275 layer = get_view_layer(view);
1276 if (layer) {
1277 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001278 pixman_region32_intersect(&view->transform.boundingbox,
1279 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001280 pixman_region32_intersect(&view->transform.opaque,
1281 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001282 pixman_region32_fini(&mask);
1283 }
1284
Pekka Paalanen380adf52015-02-16 14:39:11 +02001285 if (parent) {
1286 if (parent->geometry.scissor_enabled) {
1287 view->geometry.scissor_enabled = true;
1288 weston_view_transfer_scissor(parent, view);
1289 } else {
1290 view->geometry.scissor_enabled = false;
1291 }
1292 }
1293
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 wl_signal_emit(&view->surface->compositor->transform_signal,
1299 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001300}
1301
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001302WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001304{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001306
1307 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 * The invariant: if view->geometry.dirty, then all views
1309 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001310 * Corollary: if not parent->geometry.dirty, then all ancestors
1311 * are not dirty.
1312 */
1313
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001315 return;
1316
Jason Ekstranda7af7042013-10-12 22:38:11 -05001317 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001320 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001322}
1323
1324WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325weston_view_to_global_fixed(struct weston_view *view,
1326 wl_fixed_t vx, wl_fixed_t vy,
1327 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001328{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001329 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001330
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 weston_view_to_global_float(view,
1332 wl_fixed_to_double(vx),
1333 wl_fixed_to_double(vy),
1334 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001335 *x = wl_fixed_from_double(xf);
1336 *y = wl_fixed_from_double(yf);
1337}
1338
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001339WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340weston_view_from_global_float(struct weston_view *view,
1341 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001342{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001344 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1345
Jason Ekstranda7af7042013-10-12 22:38:11 -05001346 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001347
1348 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001349 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001351 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 *vx = 0;
1353 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001354 return;
1355 }
1356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 *vx = v.f[0] / v.f[3];
1358 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001359 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 *vx = x - view->geometry.x;
1361 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001362 }
1363}
1364
1365WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366weston_view_from_global_fixed(struct weston_view *view,
1367 wl_fixed_t x, wl_fixed_t y,
1368 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001369{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_view_from_global_float(view,
1373 wl_fixed_to_double(x),
1374 wl_fixed_to_double(y),
1375 &vxf, &vyf);
1376 *vx = wl_fixed_from_double(vxf);
1377 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001378}
1379
1380WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381weston_view_from_global(struct weston_view *view,
1382 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001383{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1387 *vx = floorf(vxf);
1388 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001389}
1390
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001391WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001392weston_surface_schedule_repaint(struct weston_surface *surface)
1393{
1394 struct weston_output *output;
1395
1396 wl_list_for_each(output, &surface->compositor->output_list, link)
1397 if (surface->output_mask & (1 << output->id))
1398 weston_output_schedule_repaint(output);
1399}
1400
1401WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402weston_view_schedule_repaint(struct weston_view *view)
1403{
1404 struct weston_output *output;
1405
1406 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1407 if (view->output_mask & (1 << output->id))
1408 weston_output_schedule_repaint(output);
1409}
1410
Pekka Paalanene508ce62015-02-19 13:59:55 +02001411/**
1412 * XXX: This function does it the wrong way.
1413 * surface->damage is the damage from the client, and causes
1414 * surface_flush_damage() to copy pixels. No window management action can
1415 * cause damage to the client-provided content, warranting re-upload!
1416 *
1417 * Instead of surface->damage, this function should record the damage
1418 * with all the views for this surface to avoid extraneous texture
1419 * uploads.
1420 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001423{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001424 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 0, 0, surface->width,
1426 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001427
Kristian Høgsberg98238702012-08-03 16:29:12 -04001428 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001429}
1430
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001431WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001433{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001434 if (view->geometry.x == x && view->geometry.y == y)
1435 return;
1436
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 view->geometry.x = x;
1438 view->geometry.y = y;
1439 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001440}
1441
Pekka Paalanen483243f2013-03-08 14:56:50 +02001442static void
1443transform_parent_handle_parent_destroy(struct wl_listener *listener,
1444 void *data)
1445{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001446 struct weston_view *view =
1447 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001448 geometry.parent_destroy_listener);
1449
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001451}
1452
1453WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001455 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001456{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 if (view->geometry.parent) {
1458 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1459 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001460
1461 if (!parent)
1462 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001463 }
1464
Jason Ekstranda7af7042013-10-12 22:38:11 -05001465 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001466
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001468 transform_parent_handle_parent_destroy;
1469 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001470 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001472 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001474 }
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 weston_view_geometry_dirty(view);
1477}
1478
Pekka Paalanen380adf52015-02-16 14:39:11 +02001479/** Set a clip mask rectangle on a view
1480 *
1481 * \param view The view to set the clip mask on.
1482 * \param x Top-left corner X coordinate of the clip rectangle.
1483 * \param y Top-left corner Y coordinate of the clip rectangle.
1484 * \param width Width of the clip rectangle, non-negative.
1485 * \param height Height of the clip rectangle, non-negative.
1486 *
1487 * A shell may set a clip mask rectangle on a view. Everything outside
1488 * the rectangle is cut away for input and output purposes: it is
1489 * not drawn and cannot be hit by hit-test based input like pointer
1490 * motion or touch-downs. Everything inside the rectangle will behave
1491 * normally. Clients are unaware of clipping.
1492 *
1493 * The rectangle is set in the surface local coordinates. Setting a clip
1494 * mask rectangle does not affect the view position, the view is positioned
1495 * as it would be without a clip. The clip also does not change
1496 * weston_surface::width,height.
1497 *
1498 * The clip mask rectangle is part of transformation inheritance
1499 * (weston_view_set_transform_parent()). A clip set in the root of the
1500 * transformation inheritance tree will affect all views in the tree.
1501 * A clip can be set only on the root view. Attempting to set a clip
1502 * on view that has a transformation parent will fail. Assigning a parent
1503 * to a view that has a clip set will cause the clip to be forgotten.
1504 *
1505 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1506 * on the additional transformations in the child views. These transformations
1507 * may not rotate the coordinate axes, i.e., only translation and scaling
1508 * are allowed. Violating this restriction causes the clipping to malfunction.
1509 * Furthermore, using scaling may cause rounding errors in child clipping.
1510 *
1511 * The clip mask rectangle is not automatically adjusted based on
1512 * wl_surface.attach dx and dy arguments.
1513 *
1514 * A clip mask rectangle can be set only if the compositor capability
1515 * WESTON_CAP_VIEW_CLIP_MASK is present.
1516 *
1517 * This function sets the clip mask rectangle and schedules a repaint for
1518 * the view.
1519 */
1520WL_EXPORT void
1521weston_view_set_mask(struct weston_view *view,
1522 int x, int y, int width, int height)
1523{
1524 struct weston_compositor *compositor = view->surface->compositor;
1525
1526 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1527 weston_log("%s not allowed without capability!\n", __func__);
1528 return;
1529 }
1530
1531 if (view->geometry.parent) {
1532 weston_log("view %p has a parent, clip forbidden!\n", view);
1533 return;
1534 }
1535
1536 if (width < 0 || height < 0) {
1537 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1538 x, y, width, height);
1539 return;
1540 }
1541
1542 pixman_region32_fini(&view->geometry.scissor);
1543 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1544 view->geometry.scissor_enabled = true;
1545 weston_view_geometry_dirty(view);
1546 weston_view_schedule_repaint(view);
1547}
1548
1549/** Remove the clip mask from a view
1550 *
1551 * \param view The view to remove the clip mask from.
1552 *
1553 * Removed the clip mask rectangle and schedules a repaint.
1554 *
1555 * \sa weston_view_set_mask
1556 */
1557WL_EXPORT void
1558weston_view_set_mask_infinite(struct weston_view *view)
1559{
1560 view->geometry.scissor_enabled = false;
1561 weston_view_geometry_dirty(view);
1562 weston_view_schedule_repaint(view);
1563}
1564
Derek Foreman280e7dd2014-10-03 13:13:42 -05001565WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566weston_view_is_mapped(struct weston_view *view)
1567{
1568 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001569 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001571 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001572}
1573
Derek Foreman280e7dd2014-10-03 13:13:42 -05001574WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001575weston_surface_is_mapped(struct weston_surface *surface)
1576{
1577 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001578 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001579 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001580 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001581}
1582
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001583static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001584surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001585{
1586 struct weston_view *view;
1587
1588 if (surface->width == width && surface->height == height)
1589 return;
1590
1591 surface->width = width;
1592 surface->height = height;
1593
1594 wl_list_for_each(view, &surface->views, surface_link)
1595 weston_view_geometry_dirty(view);
1596}
1597
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001598WL_EXPORT void
1599weston_surface_set_size(struct weston_surface *surface,
1600 int32_t width, int32_t height)
1601{
1602 assert(!surface->resource);
1603 surface_set_size(surface, width, height);
1604}
1605
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001606static int
1607fixed_round_up_to_int(wl_fixed_t f)
1608{
1609 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1610}
1611
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001612static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001613weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001614{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001615 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001616 int32_t width, height;
1617
1618 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001619 surface->width_from_buffer = 0;
1620 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001621 return;
1622 }
1623
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001624 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001625 case WL_OUTPUT_TRANSFORM_90:
1626 case WL_OUTPUT_TRANSFORM_270:
1627 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1628 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001629 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1630 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001631 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001632 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001633 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1634 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001635 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001636 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001637
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001638 surface->width_from_buffer = width;
1639 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001640}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001641
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001642static void
1643weston_surface_update_size(struct weston_surface *surface)
1644{
1645 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1646 int32_t width, height;
1647
1648 width = surface->width_from_buffer;
1649 height = surface->height_from_buffer;
1650
1651 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001652 surface_set_size(surface,
1653 vp->surface.width, vp->surface.height);
1654 return;
1655 }
1656
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001657 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001658 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1659 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1660
1661 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001662 return;
1663 }
1664
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001665 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001666}
1667
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001668WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001669weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001670{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001671 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001672
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001673 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001674
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001675 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001676}
1677
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678WL_EXPORT struct weston_view *
1679weston_compositor_pick_view(struct weston_compositor *compositor,
1680 wl_fixed_t x, wl_fixed_t y,
1681 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001682{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001684 wl_fixed_t view_x, view_y;
1685 int view_ix, view_iy;
1686 int ix = wl_fixed_to_int(x);
1687 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001688
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001690 if (!pixman_region32_contains_point(
1691 &view->transform.boundingbox, ix, iy, NULL))
1692 continue;
1693
1694 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1695 view_ix = wl_fixed_to_int(view_x);
1696 view_iy = wl_fixed_to_int(view_y);
1697
1698 if (!pixman_region32_contains_point(&view->surface->input,
1699 view_ix, view_iy, NULL))
1700 continue;
1701
Pekka Paalanen380adf52015-02-16 14:39:11 +02001702 if (view->geometry.scissor_enabled &&
1703 !pixman_region32_contains_point(&view->geometry.scissor,
1704 view_ix, view_iy, NULL))
1705 continue;
1706
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001707 *vx = view_x;
1708 *vy = view_y;
1709 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001710 }
1711
1712 return NULL;
1713}
1714
1715static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001717{
Daniel Stone37816df2012-05-16 18:45:18 +01001718 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001719
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001720 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001721 return;
1722
Daniel Stone37816df2012-05-16 18:45:18 +01001723 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001724 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001725}
1726
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001727WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001729{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001730 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001731
Jason Ekstranda7af7042013-10-12 22:38:11 -05001732 if (!weston_view_is_mapped(view))
1733 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001734
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 weston_view_damage_below(view);
1736 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001737 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001738 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 wl_list_remove(&view->link);
1740 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view->output_mask = 0;
1742 weston_surface_assign_output(view->surface);
1743
1744 if (weston_surface_is_mapped(view->surface))
1745 return;
1746
1747 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1748 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001749 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001751 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001752 NULL,
1753 wl_fixed_from_int(0),
1754 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001756 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001757 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001759
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760WL_EXPORT void
1761weston_surface_unmap(struct weston_surface *surface)
1762{
1763 struct weston_view *view;
1764
1765 wl_list_for_each(view, &surface->views, surface_link)
1766 weston_view_unmap(view);
1767 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001768}
1769
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001770static void
1771weston_surface_reset_pending_buffer(struct weston_surface *surface)
1772{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001773 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001774 surface->pending.sx = 0;
1775 surface->pending.sy = 0;
1776 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001777 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001778}
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780WL_EXPORT void
1781weston_view_destroy(struct weston_view *view)
1782{
1783 wl_signal_emit(&view->destroy_signal, view);
1784
1785 assert(wl_list_empty(&view->geometry.child_list));
1786
1787 if (weston_view_is_mapped(view)) {
1788 weston_view_unmap(view);
1789 weston_compositor_build_view_list(view->surface->compositor);
1790 }
1791
1792 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001793 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794
1795 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001796 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001798 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799
1800 weston_view_set_transform_parent(view, NULL);
1801
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 wl_list_remove(&view->surface_link);
1803
1804 free(view);
1805}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001806
1807WL_EXPORT void
1808weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001809{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001810 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001812
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001813 if (--surface->ref_count > 0)
1814 return;
1815
1816 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1817
Pekka Paalanene67858b2013-04-25 13:57:42 +03001818 assert(wl_list_empty(&surface->subsurface_list_pending));
1819 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1822 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001823
Jason Ekstrand7b982072014-05-20 14:33:03 -05001824 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001825
Pekka Paalanende685b82012-12-04 15:58:12 +02001826 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001827
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001828 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001829 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001830 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001831
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001832 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001833 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001834
Pekka Paalanen133e4392014-09-23 22:08:46 -04001835 weston_presentation_feedback_discard_list(&surface->feedback_list);
1836
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001837 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001838}
1839
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001840static void
1841destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001842{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001843 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001844
Giulio Camuffo0d379742013-11-15 22:06:15 +01001845 /* Set the resource to NULL, since we don't want to leave a
1846 * dangling pointer if the surface was refcounted and survives
1847 * the weston_surface_destroy() call. */
1848 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001849 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001850}
1851
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001852static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001853weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1854{
1855 struct weston_buffer *buffer =
1856 container_of(listener, struct weston_buffer, destroy_listener);
1857
1858 wl_signal_emit(&buffer->destroy_signal, buffer);
1859 free(buffer);
1860}
1861
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001862WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001863weston_buffer_from_resource(struct wl_resource *resource)
1864{
1865 struct weston_buffer *buffer;
1866 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001867
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001868 listener = wl_resource_get_destroy_listener(resource,
1869 weston_buffer_destroy_handler);
1870
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001871 if (listener)
1872 return container_of(listener, struct weston_buffer,
1873 destroy_listener);
1874
1875 buffer = zalloc(sizeof *buffer);
1876 if (buffer == NULL)
1877 return NULL;
1878
1879 buffer->resource = resource;
1880 wl_signal_init(&buffer->destroy_signal);
1881 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001882 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001883 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001884
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001885 return buffer;
1886}
1887
1888static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001889weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1890 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001891{
Pekka Paalanende685b82012-12-04 15:58:12 +02001892 struct weston_buffer_reference *ref =
1893 container_of(listener, struct weston_buffer_reference,
1894 destroy_listener);
1895
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001896 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001897 ref->buffer = NULL;
1898}
1899
1900WL_EXPORT void
1901weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001903{
1904 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001905 ref->buffer->busy_count--;
1906 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001907 assert(wl_resource_get_client(ref->buffer->resource));
1908 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001909 WL_BUFFER_RELEASE);
1910 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001911 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001912 }
1913
Pekka Paalanende685b82012-12-04 15:58:12 +02001914 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001915 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001916 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001917 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001918 }
1919
Pekka Paalanende685b82012-12-04 15:58:12 +02001920 ref->buffer = buffer;
1921 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1922}
1923
1924static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001925weston_surface_attach(struct weston_surface *surface,
1926 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001927{
1928 weston_buffer_reference(&surface->buffer_ref, buffer);
1929
Pekka Paalanena6421c42012-12-04 15:58:10 +02001930 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001931 if (weston_surface_is_mapped(surface))
1932 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001933 }
1934
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001935 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001936
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001937 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001938 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001939}
1940
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001941WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001942weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001943{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001944 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001945
1946 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001947 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001948}
1949
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001950WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001951weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001952{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001953 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001954
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001955 pixman_region32_union(&compositor->primary_plane.damage,
1956 &compositor->primary_plane.damage,
1957 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001958 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001959}
1960
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001961static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001963{
Pekka Paalanende685b82012-12-04 15:58:12 +02001964 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001965 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001966 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001967
Pekka Paalanenb5026542014-11-12 15:09:24 +02001968 if (weston_timeline_enabled_ &&
1969 pixman_region32_not_empty(&surface->damage))
1970 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1971 TLP_OUTPUT(surface->output), TLP_END);
1972
Jason Ekstrandef540082014-06-26 10:37:36 -07001973 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974}
1975
1976static void
1977view_accumulate_damage(struct weston_view *view,
1978 pixman_region32_t *opaque)
1979{
1980 pixman_region32_t damage;
1981
1982 pixman_region32_init(&damage);
1983 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001984 pixman_box32_t *extents;
1985
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001987 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001988 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 pixman_region32_copy(&damage, &view->surface->damage);
1990 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02001991 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001992 }
1993
Pekka Paalanen380adf52015-02-16 14:39:11 +02001994 pixman_region32_intersect(&damage, &damage,
1995 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001996 pixman_region32_subtract(&damage, &damage, opaque);
1997 pixman_region32_union(&view->plane->damage,
1998 &view->plane->damage, &damage);
1999 pixman_region32_fini(&damage);
2000 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002001 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002002}
2003
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002004static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002005compositor_accumulate_damage(struct weston_compositor *ec)
2006{
2007 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002008 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002009 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002010
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002011 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002012
2013 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002014 pixman_region32_copy(&plane->clip, &clip);
2015
2016 pixman_region32_init(&opaque);
2017
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 wl_list_for_each(ev, &ec->view_list, link) {
2019 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002020 continue;
2021
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002023 }
2024
2025 pixman_region32_union(&clip, &clip, &opaque);
2026 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002027 }
2028
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002029 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002030
Jason Ekstranda7af7042013-10-12 22:38:11 -05002031 wl_list_for_each(ev, &ec->view_list, link)
2032 ev->surface->touched = 0;
2033
2034 wl_list_for_each(ev, &ec->view_list, link) {
2035 if (ev->surface->touched)
2036 continue;
2037 ev->surface->touched = 1;
2038
2039 surface_flush_damage(ev->surface);
2040
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002041 /* Both the renderer and the backend have seen the buffer
2042 * by now. If renderer needs the buffer, it has its own
2043 * reference set. If the backend wants to keep the buffer
2044 * around for migrating the surface into a non-primary plane
2045 * later, keep_buffer is true. Otherwise, drop the core
2046 * reference now, and allow early buffer release. This enables
2047 * clients to use single-buffering.
2048 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 if (!ev->surface->keep_buffer)
2050 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002052}
2053
2054static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002055surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002056{
2057 struct weston_subsurface *sub;
2058
Pekka Paalanene67858b2013-04-25 13:57:42 +03002059 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002061 continue;
2062
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2064 wl_list_init(&sub->surface->views);
2065
2066 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002067 }
2068}
2069
2070static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002071surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 struct weston_subsurface *sub;
2074 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002075
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2077 if (sub->surface == surface)
2078 continue;
2079
George Kiagiadakised04d382014-06-13 18:10:26 +02002080 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2081 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002082 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002083 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084
2085 surface_free_unused_subsurface_views(sub->surface);
2086 }
2087}
2088
2089static void
2090view_list_add_subsurface_view(struct weston_compositor *compositor,
2091 struct weston_subsurface *sub,
2092 struct weston_view *parent)
2093{
2094 struct weston_subsurface *child;
2095 struct weston_view *view = NULL, *iv;
2096
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002097 if (!weston_surface_is_mapped(sub->surface))
2098 return;
2099
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2101 if (iv->geometry.parent == parent) {
2102 view = iv;
2103 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002104 }
2105 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002106
2107 if (view) {
2108 /* Put it back in the surface's list of views */
2109 wl_list_remove(&view->surface_link);
2110 wl_list_insert(&sub->surface->views, &view->surface_link);
2111 } else {
2112 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002113 weston_view_set_position(view,
2114 sub->position.x,
2115 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116 weston_view_set_transform_parent(view, parent);
2117 }
2118
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002119 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002120 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121
Pekka Paalanenb188e912013-11-19 14:03:35 +02002122 if (wl_list_empty(&sub->surface->subsurface_list)) {
2123 wl_list_insert(compositor->view_list.prev, &view->link);
2124 return;
2125 }
2126
2127 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2128 if (child->surface == sub->surface)
2129 wl_list_insert(compositor->view_list.prev, &view->link);
2130 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002131 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002132 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002133}
2134
2135static void
2136view_list_add(struct weston_compositor *compositor,
2137 struct weston_view *view)
2138{
2139 struct weston_subsurface *sub;
2140
2141 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002142
Pekka Paalanenb188e912013-11-19 14:03:35 +02002143 if (wl_list_empty(&view->surface->subsurface_list)) {
2144 wl_list_insert(compositor->view_list.prev, &view->link);
2145 return;
2146 }
2147
2148 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2149 if (sub->surface == view->surface)
2150 wl_list_insert(compositor->view_list.prev, &view->link);
2151 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002152 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002153 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002154}
2155
2156static void
2157weston_compositor_build_view_list(struct weston_compositor *compositor)
2158{
2159 struct weston_view *view;
2160 struct weston_layer *layer;
2161
2162 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002163 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002164 surface_stash_subsurface_views(view->surface);
2165
2166 wl_list_init(&compositor->view_list);
2167 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002168 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002169 view_list_add(compositor, view);
2170 }
2171 }
2172
2173 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002174 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002175 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002176}
2177
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002178static void
2179weston_output_take_feedback_list(struct weston_output *output,
2180 struct weston_surface *surface)
2181{
2182 struct weston_view *view;
2183 struct weston_presentation_feedback *feedback;
2184 uint32_t flags = 0xffffffff;
2185
2186 if (wl_list_empty(&surface->feedback_list))
2187 return;
2188
2189 /* All views must have the flag for the flag to survive. */
2190 wl_list_for_each(view, &surface->views, surface_link) {
2191 /* ignore views that are not on this output at all */
2192 if (view->output_mask & (1u << output->id))
2193 flags &= view->psf_flags;
2194 }
2195
2196 wl_list_for_each(feedback, &surface->feedback_list, link)
2197 feedback->psf_flags = flags;
2198
2199 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2200 wl_list_init(&surface->feedback_list);
2201}
2202
David Herrmann1edf44c2013-10-22 17:11:26 +02002203static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002204weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002205{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002206 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002207 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002208 struct weston_animation *animation, *next;
2209 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002210 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002211 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002212 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002213
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002214 if (output->destroying)
2215 return 0;
2216
Pekka Paalanenb5026542014-11-12 15:09:24 +02002217 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2218
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002219 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002221
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002222 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002223 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002224 } else {
2225 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002227 ev->psf_flags = 0;
2228 }
2229 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002230
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002232 wl_list_for_each(ev, &ec->view_list, link) {
2233 /* Note: This operation is safe to do multiple times on the
2234 * same surface.
2235 */
2236 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002238 &ev->surface->frame_callback_list);
2239 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002240
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002241 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002242 }
2243 }
2244
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002245 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002246
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002247 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002248 pixman_region32_intersect(&output_damage,
2249 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002250 pixman_region32_subtract(&output_damage,
2251 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002252
Scott Moreauccbf29d2012-02-22 14:21:41 -07002253 if (output->dirty)
2254 weston_output_update_matrix(output);
2255
David Herrmann1edf44c2013-10-22 17:11:26 +02002256 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002257
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002258 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002259
Kristian Høgsbergef044142011-06-21 15:02:12 -04002260 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002261
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002262 weston_compositor_repick(ec);
2263 wl_event_loop_dispatch(ec->input_loop, 0);
2264
Jonas Ådahldb773762012-06-13 00:01:21 +02002265 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002266 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002267 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002268 }
2269
Scott Moreaud64cf212012-06-08 19:40:54 -06002270 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002271 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002272 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002273 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002274
Pekka Paalanenb5026542014-11-12 15:09:24 +02002275 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2276
David Herrmann1edf44c2013-10-22 17:11:26 +02002277 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002278}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002279
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002280static int
2281weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002282{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002283 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002284
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002285 wl_event_loop_dispatch(compositor->input_loop, 0);
2286
2287 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002288}
2289
Pekka Paalanen82919792014-05-21 13:51:49 +03002290static void
2291weston_output_schedule_repaint_reset(struct weston_output *output)
2292{
2293 struct weston_compositor *compositor = output->compositor;
2294 struct wl_event_loop *loop;
2295 int fd;
2296
2297 output->repaint_scheduled = 0;
2298 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2299
2300 if (compositor->input_loop_source)
2301 return;
2302
2303 loop = wl_display_get_event_loop(compositor->wl_display);
2304 fd = wl_event_loop_get_fd(compositor->input_loop);
2305 compositor->input_loop_source =
2306 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2307 weston_compositor_read_input, compositor);
2308}
2309
Kristian Høgsbergef044142011-06-21 15:02:12 -04002310WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002311weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002312 const struct timespec *stamp,
2313 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002314{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002315 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen82919792014-05-21 13:51:49 +03002316 int r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002317 uint32_t refresh_nsec;
2318
Pekka Paalanenb5026542014-11-12 15:09:24 +02002319 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2320 TLP_VBLANK(stamp), TLP_END);
2321
Pekka Paalanen133e4392014-09-23 22:08:46 -04002322 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2323 weston_presentation_feedback_present_list(&output->feedback_list,
2324 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002325 output->msc,
2326 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002327
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002328 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002329
2330 if (output->repaint_needed &&
2331 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2332 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002333 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002334 if (!r)
2335 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002336 }
2337
Pekka Paalanen82919792014-05-21 13:51:49 +03002338 weston_output_schedule_repaint_reset(output);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002339}
2340
2341static void
2342idle_repaint(void *data)
2343{
2344 struct weston_output *output = data;
2345
Jonas Ådahle5a12252013-04-05 23:07:11 +02002346 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002347}
2348
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002349WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002350weston_layer_entry_insert(struct weston_layer_entry *list,
2351 struct weston_layer_entry *entry)
2352{
2353 wl_list_insert(&list->link, &entry->link);
2354 entry->layer = list->layer;
2355}
2356
2357WL_EXPORT void
2358weston_layer_entry_remove(struct weston_layer_entry *entry)
2359{
2360 wl_list_remove(&entry->link);
2361 wl_list_init(&entry->link);
2362 entry->layer = NULL;
2363}
2364
2365WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002366weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2367{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002368 wl_list_init(&layer->view_list.link);
2369 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002370 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002371 if (below != NULL)
2372 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002373}
2374
2375WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002376weston_layer_set_mask(struct weston_layer *layer,
2377 int x, int y, int width, int height)
2378{
2379 struct weston_view *view;
2380
2381 layer->mask.x1 = x;
2382 layer->mask.x2 = x + width;
2383 layer->mask.y1 = y;
2384 layer->mask.y2 = y + height;
2385
2386 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2387 weston_view_geometry_dirty(view);
2388 }
2389}
2390
2391WL_EXPORT void
2392weston_layer_set_mask_infinite(struct weston_layer *layer)
2393{
2394 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2395 UINT32_MAX, UINT32_MAX);
2396}
2397
2398WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002399weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002400{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002401 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002402 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002403
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002404 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2405 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002406 return;
2407
Pekka Paalanenb5026542014-11-12 15:09:24 +02002408 if (!output->repaint_needed)
2409 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2410
Kristian Høgsbergef044142011-06-21 15:02:12 -04002411 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002412 output->repaint_needed = 1;
2413 if (output->repaint_scheduled)
2414 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002415
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002416 wl_event_loop_add_idle(loop, idle_repaint, output);
2417 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002418 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2419
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002420
2421 if (compositor->input_loop_source) {
2422 wl_event_source_remove(compositor->input_loop_source);
2423 compositor->input_loop_source = NULL;
2424 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002425}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002426
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002427WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002428weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2429{
2430 struct weston_output *output;
2431
2432 wl_list_for_each(output, &compositor->output_list, link)
2433 weston_output_schedule_repaint(output);
2434}
2435
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002436static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002437surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002438{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002439 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002440}
2441
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002442static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002443surface_attach(struct wl_client *client,
2444 struct wl_resource *resource,
2445 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2446{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002447 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002448 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002449
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002450 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002451 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002452 if (buffer == NULL) {
2453 wl_client_post_no_memory(client);
2454 return;
2455 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002456 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002457
Pekka Paalanende685b82012-12-04 15:58:12 +02002458 /* Attach, attach, without commit in between does not send
2459 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002460 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002461
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002462 surface->pending.sx = sx;
2463 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002464 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002465}
2466
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002467static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002468surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002469 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002470 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002471{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002472 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002473
Pekka Paalanen8e159182012-10-10 12:49:25 +03002474 pixman_region32_union_rect(&surface->pending.damage,
2475 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002476 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002477}
2478
Kristian Høgsberg33418202011-08-16 23:01:28 -04002479static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002480destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002481{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002482 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002483
2484 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002485 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002486}
2487
2488static void
2489surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002490 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002491{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002492 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002493 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002494
2495 cb = malloc(sizeof *cb);
2496 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002497 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002498 return;
2499 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002500
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002501 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2502 callback);
2503 if (cb->resource == NULL) {
2504 free(cb);
2505 wl_resource_post_no_memory(resource);
2506 return;
2507 }
2508
Jason Ekstranda85118c2013-06-27 20:17:02 -05002509 wl_resource_set_implementation(cb->resource, NULL, cb,
2510 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002511
Pekka Paalanenbc106382012-10-10 12:49:31 +03002512 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002513}
2514
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002515static void
2516surface_set_opaque_region(struct wl_client *client,
2517 struct wl_resource *resource,
2518 struct wl_resource *region_resource)
2519{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002520 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002521 struct weston_region *region;
2522
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002523 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002524 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002525 pixman_region32_copy(&surface->pending.opaque,
2526 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002527 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002528 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002529 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002530}
2531
2532static void
2533surface_set_input_region(struct wl_client *client,
2534 struct wl_resource *resource,
2535 struct wl_resource *region_resource)
2536{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002537 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002538 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002539
2540 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002541 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002542 pixman_region32_copy(&surface->pending.input,
2543 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002544 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002545 pixman_region32_fini(&surface->pending.input);
2546 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002547 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002548}
2549
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002550static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002552{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553 struct weston_subsurface *sub;
2554
2555 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2556 parent_link_pending) {
2557 wl_list_remove(&sub->parent_link);
2558 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2559 }
2560}
2561
2562static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002563weston_surface_commit_state(struct weston_surface *surface,
2564 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002565{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002566 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002567 pixman_region32_t opaque;
2568
Alexander Larsson4ea95522013-05-22 14:41:37 +02002569 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002570 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002571 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002572 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002573
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002574 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002575 if (state->newly_attached)
2576 weston_surface_attach(surface, state->buffer);
2577 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002578
Jason Ekstrand7b982072014-05-20 14:33:03 -05002579 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002580 weston_surface_update_size(surface);
2581 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002582 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002583 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002584
Jason Ekstrand7b982072014-05-20 14:33:03 -05002585 state->sx = 0;
2586 state->sy = 0;
2587 state->newly_attached = 0;
2588 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002589
2590 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002591 if (weston_timeline_enabled_ &&
2592 pixman_region32_not_empty(&state->damage))
2593 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002594 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002595 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002596 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002597 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002598 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002599
2600 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002601 pixman_region32_init(&opaque);
2602 pixman_region32_intersect_rect(&opaque, &state->opaque,
2603 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002604
2605 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2606 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002607 wl_list_for_each(view, &surface->views, surface_link)
2608 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002609 }
2610
2611 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002612
2613 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002614 pixman_region32_intersect_rect(&surface->input, &state->input,
2615 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002616
Pekka Paalanenbc106382012-10-10 12:49:31 +03002617 /* wl_surface.frame */
2618 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002619 &state->frame_callback_list);
2620 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002621
2622 /* XXX:
2623 * What should happen with a feedback request, if there
2624 * is no wl_buffer attached for this commit?
2625 */
2626
2627 /* presentation.feedback */
2628 wl_list_insert_list(&surface->feedback_list,
2629 &state->feedback_list);
2630 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002631}
2632
2633static void
2634weston_surface_commit(struct weston_surface *surface)
2635{
2636 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002637
Pekka Paalanene67858b2013-04-25 13:57:42 +03002638 weston_surface_commit_subsurface_order(surface);
2639
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002640 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002641}
2642
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002643static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002644weston_subsurface_commit(struct weston_subsurface *sub);
2645
2646static void
2647weston_subsurface_parent_commit(struct weston_subsurface *sub,
2648 int parent_is_synchronized);
2649
2650static void
2651surface_commit(struct wl_client *client, struct wl_resource *resource)
2652{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002653 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002654 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2655
2656 if (sub) {
2657 weston_subsurface_commit(sub);
2658 return;
2659 }
2660
2661 weston_surface_commit(surface);
2662
2663 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2664 if (sub->surface != surface)
2665 weston_subsurface_parent_commit(sub, 0);
2666 }
2667}
2668
2669static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002670surface_set_buffer_transform(struct wl_client *client,
2671 struct wl_resource *resource, int transform)
2672{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002673 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002674
Jonny Lamba55f1392014-05-30 12:07:15 +02002675 /* if wl_output.transform grows more members this will need to be updated. */
2676 if (transform < 0 ||
2677 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2678 wl_resource_post_error(resource,
2679 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2680 "buffer transform must be a valid transform "
2681 "('%d' specified)", transform);
2682 return;
2683 }
2684
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002685 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002686 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002687}
2688
Alexander Larsson4ea95522013-05-22 14:41:37 +02002689static void
2690surface_set_buffer_scale(struct wl_client *client,
2691 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002692 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002693{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002694 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002695
Jonny Lamba55f1392014-05-30 12:07:15 +02002696 if (scale < 1) {
2697 wl_resource_post_error(resource,
2698 WL_SURFACE_ERROR_INVALID_SCALE,
2699 "buffer scale must be at least one "
2700 "('%d' specified)", scale);
2701 return;
2702 }
2703
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002704 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002705 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002706}
2707
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002708static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002709 surface_destroy,
2710 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002711 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002712 surface_frame,
2713 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002714 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002715 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002716 surface_set_buffer_transform,
2717 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002718};
2719
2720static void
2721compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002722 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002723{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002724 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002725 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002726
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002727 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002728 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002729 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002730 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002731 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002732
Jason Ekstranda85118c2013-06-27 20:17:02 -05002733 surface->resource =
2734 wl_resource_create(client, &wl_surface_interface,
2735 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002736 if (surface->resource == NULL) {
2737 weston_surface_destroy(surface);
2738 wl_resource_post_no_memory(resource);
2739 return;
2740 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002741 wl_resource_set_implementation(surface->resource, &surface_interface,
2742 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002743
2744 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002745}
2746
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002747static void
2748destroy_region(struct wl_resource *resource)
2749{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002750 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002751
2752 pixman_region32_fini(&region->region);
2753 free(region);
2754}
2755
2756static void
2757region_destroy(struct wl_client *client, struct wl_resource *resource)
2758{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002759 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002760}
2761
2762static void
2763region_add(struct wl_client *client, struct wl_resource *resource,
2764 int32_t x, int32_t y, int32_t width, int32_t height)
2765{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002766 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002767
2768 pixman_region32_union_rect(&region->region, &region->region,
2769 x, y, width, height);
2770}
2771
2772static void
2773region_subtract(struct wl_client *client, struct wl_resource *resource,
2774 int32_t x, int32_t y, int32_t width, int32_t height)
2775{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002776 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002777 pixman_region32_t rect;
2778
2779 pixman_region32_init_rect(&rect, x, y, width, height);
2780 pixman_region32_subtract(&region->region, &region->region, &rect);
2781 pixman_region32_fini(&rect);
2782}
2783
2784static const struct wl_region_interface region_interface = {
2785 region_destroy,
2786 region_add,
2787 region_subtract
2788};
2789
2790static void
2791compositor_create_region(struct wl_client *client,
2792 struct wl_resource *resource, uint32_t id)
2793{
2794 struct weston_region *region;
2795
2796 region = malloc(sizeof *region);
2797 if (region == NULL) {
2798 wl_resource_post_no_memory(resource);
2799 return;
2800 }
2801
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002802 pixman_region32_init(&region->region);
2803
Jason Ekstranda85118c2013-06-27 20:17:02 -05002804 region->resource =
2805 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002806 if (region->resource == NULL) {
2807 free(region);
2808 wl_resource_post_no_memory(resource);
2809 return;
2810 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002811 wl_resource_set_implementation(region->resource, &region_interface,
2812 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002813}
2814
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002815static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002816 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002817 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002818};
2819
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002820static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002821weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2822{
2823 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824
Jason Ekstrand7b982072014-05-20 14:33:03 -05002825 weston_surface_commit_state(surface, &sub->cached);
2826 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002827
2828 weston_surface_commit_subsurface_order(surface);
2829
2830 weston_surface_schedule_repaint(surface);
2831
Jason Ekstrand7b982072014-05-20 14:33:03 -05002832 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833}
2834
2835static void
2836weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2837{
2838 struct weston_surface *surface = sub->surface;
2839
2840 /*
2841 * If this commit would cause the surface to move by the
2842 * attach(dx, dy) parameters, the old damage region must be
2843 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002844 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002845 */
2846 pixman_region32_translate(&sub->cached.damage,
2847 -surface->pending.sx, -surface->pending.sy);
2848 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2849 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002850 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002851
2852 if (surface->pending.newly_attached) {
2853 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002854 weston_surface_state_set_buffer(&sub->cached,
2855 surface->pending.buffer);
2856 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002857 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002858 weston_presentation_feedback_discard_list(
2859 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002860 }
2861 sub->cached.sx += surface->pending.sx;
2862 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002863
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002864 sub->cached.buffer_viewport.changed |=
2865 surface->pending.buffer_viewport.changed;
2866 sub->cached.buffer_viewport.buffer =
2867 surface->pending.buffer_viewport.buffer;
2868 sub->cached.buffer_viewport.surface =
2869 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002870
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002871 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002872
2873 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2874
2875 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2876
2877 wl_list_insert_list(&sub->cached.frame_callback_list,
2878 &surface->pending.frame_callback_list);
2879 wl_list_init(&surface->pending.frame_callback_list);
2880
Pekka Paalanen133e4392014-09-23 22:08:46 -04002881 wl_list_insert_list(&sub->cached.feedback_list,
2882 &surface->pending.feedback_list);
2883 wl_list_init(&surface->pending.feedback_list);
2884
Jason Ekstrand7b982072014-05-20 14:33:03 -05002885 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002886}
2887
Derek Foreman280e7dd2014-10-03 13:13:42 -05002888static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2890{
2891 while (sub) {
2892 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002893 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002894
2895 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002896 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002897
2898 sub = weston_surface_to_subsurface(sub->parent);
2899 }
2900
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002901 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002902}
2903
2904static void
2905weston_subsurface_commit(struct weston_subsurface *sub)
2906{
2907 struct weston_surface *surface = sub->surface;
2908 struct weston_subsurface *tmp;
2909
2910 /* Recursive check for effectively synchronized. */
2911 if (weston_subsurface_is_synchronized(sub)) {
2912 weston_subsurface_commit_to_cache(sub);
2913 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002914 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002915 /* flush accumulated state from cache */
2916 weston_subsurface_commit_to_cache(sub);
2917 weston_subsurface_commit_from_cache(sub);
2918 } else {
2919 weston_surface_commit(surface);
2920 }
2921
2922 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2923 if (tmp->surface != surface)
2924 weston_subsurface_parent_commit(tmp, 0);
2925 }
2926 }
2927}
2928
2929static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002930weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002931{
2932 struct weston_surface *surface = sub->surface;
2933 struct weston_subsurface *tmp;
2934
Pekka Paalanene67858b2013-04-25 13:57:42 +03002935 /* From now on, commit_from_cache the whole sub-tree, regardless of
2936 * the synchronized mode of each child. This sub-surface or some
2937 * of its ancestors were synchronized, so we are synchronized
2938 * all the way down.
2939 */
2940
Jason Ekstrand7b982072014-05-20 14:33:03 -05002941 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002942 weston_subsurface_commit_from_cache(sub);
2943
2944 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2945 if (tmp->surface != surface)
2946 weston_subsurface_parent_commit(tmp, 1);
2947 }
2948}
2949
2950static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002951weston_subsurface_parent_commit(struct weston_subsurface *sub,
2952 int parent_is_synchronized)
2953{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002954 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002955 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002956 wl_list_for_each(view, &sub->surface->views, surface_link)
2957 weston_view_set_position(view,
2958 sub->position.x,
2959 sub->position.y);
2960
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002961 sub->position.set = 0;
2962 }
2963
2964 if (parent_is_synchronized || sub->synchronized)
2965 weston_subsurface_synchronized_commit(sub);
2966}
2967
Pekka Paalanen8274d902014-08-06 19:36:51 +03002968static int
2969subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2970{
2971 return snprintf(buf, len, "sub-surface");
2972}
2973
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002974static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002975subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002976{
2977 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002978 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002979
Jason Ekstranda7af7042013-10-12 22:38:11 -05002980 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002981 weston_view_set_position(view,
2982 view->geometry.x + dx,
2983 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002984
2985 /* No need to check parent mappedness, because if parent is not
2986 * mapped, parent is not in a visible layer, so this sub-surface
2987 * will not be drawn either.
2988 */
2989 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002990 struct weston_output *output;
2991
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002992 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002993 * because that would call it also for the parent surface,
2994 * which might not be mapped yet. That would lead to
2995 * inconsistent state, where the window could never be
2996 * mapped.
2997 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002998 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002999 * weston_surface_is_mapped() return true, so that when the
3000 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003001 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003002 */
3003 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003004 output = container_of(compositor->output_list.next,
3005 struct weston_output, link);
3006
3007 surface->output = output;
3008 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003009 }
3010}
3011
3012static struct weston_subsurface *
3013weston_surface_to_subsurface(struct weston_surface *surface)
3014{
3015 if (surface->configure == subsurface_configure)
3016 return surface->configure_private;
3017
3018 return NULL;
3019}
3020
Pekka Paalanen01388e22013-04-25 13:57:44 +03003021WL_EXPORT struct weston_surface *
3022weston_surface_get_main_surface(struct weston_surface *surface)
3023{
3024 struct weston_subsurface *sub;
3025
3026 while (surface && (sub = weston_surface_to_subsurface(surface)))
3027 surface = sub->parent;
3028
3029 return surface;
3030}
3031
Pekka Paalanen50b67472014-10-01 15:02:41 +03003032WL_EXPORT int
3033weston_surface_set_role(struct weston_surface *surface,
3034 const char *role_name,
3035 struct wl_resource *error_resource,
3036 uint32_t error_code)
3037{
3038 assert(role_name);
3039
3040 if (surface->role_name == NULL ||
3041 surface->role_name == role_name ||
3042 strcmp(surface->role_name, role_name) == 0) {
3043 surface->role_name = role_name;
3044
3045 return 0;
3046 }
3047
3048 wl_resource_post_error(error_resource, error_code,
3049 "Cannot assign role %s to wl_surface@%d,"
3050 " already has role %s\n",
3051 role_name,
3052 wl_resource_get_id(surface->resource),
3053 surface->role_name);
3054 return -1;
3055}
3056
Pekka Paalanen8274d902014-08-06 19:36:51 +03003057WL_EXPORT void
3058weston_surface_set_label_func(struct weston_surface *surface,
3059 int (*desc)(struct weston_surface *,
3060 char *, size_t))
3061{
3062 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003063 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003064}
3065
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003066/** Get the size of surface contents
3067 *
3068 * \param surface The surface to query.
3069 * \param width Returns the width of raw contents.
3070 * \param height Returns the height of raw contents.
3071 *
3072 * Retrieves the raw surface content size in pixels for the given surface.
3073 * This is the whole content size in buffer pixels. If the surface
3074 * has no content or the renderer does not implement this feature,
3075 * zeroes are returned.
3076 *
3077 * This function is used to determine the buffer size needed for
3078 * a weston_surface_copy_content() call.
3079 */
3080WL_EXPORT void
3081weston_surface_get_content_size(struct weston_surface *surface,
3082 int *width, int *height)
3083{
3084 struct weston_renderer *rer = surface->compositor->renderer;
3085
3086 if (!rer->surface_get_content_size) {
3087 *width = 0;
3088 *height = 0;
3089 return;
3090 }
3091
3092 rer->surface_get_content_size(surface, width, height);
3093}
3094
3095/** Copy surface contents to system memory.
3096 *
3097 * \param surface The surface to copy from.
3098 * \param target Pointer to the target memory buffer.
3099 * \param size Size of the target buffer in bytes.
3100 * \param src_x X location on contents to copy from.
3101 * \param src_y Y location on contents to copy from.
3102 * \param width Width in pixels of the area to copy.
3103 * \param height Height in pixels of the area to copy.
3104 * \return 0 for success, -1 for failure.
3105 *
3106 * Surface contents are maintained by the renderer. They can be in a
3107 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3108 * else.
3109 *
3110 * Surface contents are copied into memory pointed to by target,
3111 * which has size bytes of space available. The target memory
3112 * may be larger than needed, but being smaller returns an error.
3113 * The extra bytes in target may or may not be written; their content is
3114 * unspecified. Size must be large enough to hold the image.
3115 *
3116 * The image in the target memory will be arranged in rows from
3117 * top to bottom, and pixels on a row from left to right. The pixel
3118 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3119 * width * 4.
3120 *
3121 * Parameters src_x and src_y define the upper-left corner in buffer
3122 * coordinates (pixels) to copy from. Parameters width and height
3123 * define the size of the area to copy in pixels.
3124 *
3125 * The rectangle defined by src_x, src_y, width, height must fit in
3126 * the surface contents. Otherwise an error is returned.
3127 *
3128 * Use surface_get_data_size to determine the content size; the
3129 * needed target buffer size and rectangle limits.
3130 *
3131 * CURRENT IMPLEMENTATION RESTRICTIONS:
3132 * - the machine must be little-endian due to Pixman formats.
3133 *
3134 * NOTE: Pixman formats are premultiplied.
3135 */
3136WL_EXPORT int
3137weston_surface_copy_content(struct weston_surface *surface,
3138 void *target, size_t size,
3139 int src_x, int src_y,
3140 int width, int height)
3141{
3142 struct weston_renderer *rer = surface->compositor->renderer;
3143 int cw, ch;
3144 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3145
3146 if (!rer->surface_copy_content)
3147 return -1;
3148
3149 weston_surface_get_content_size(surface, &cw, &ch);
3150
3151 if (src_x < 0 || src_y < 0)
3152 return -1;
3153
3154 if (width <= 0 || height <= 0)
3155 return -1;
3156
3157 if (src_x + width > cw || src_y + height > ch)
3158 return -1;
3159
3160 if (width * bytespp * height > size)
3161 return -1;
3162
3163 return rer->surface_copy_content(surface, target, size,
3164 src_x, src_y, width, height);
3165}
3166
Pekka Paalanene67858b2013-04-25 13:57:42 +03003167static void
3168subsurface_set_position(struct wl_client *client,
3169 struct wl_resource *resource, int32_t x, int32_t y)
3170{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003171 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003172
3173 if (!sub)
3174 return;
3175
3176 sub->position.x = x;
3177 sub->position.y = y;
3178 sub->position.set = 1;
3179}
3180
3181static struct weston_subsurface *
3182subsurface_from_surface(struct weston_surface *surface)
3183{
3184 struct weston_subsurface *sub;
3185
3186 sub = weston_surface_to_subsurface(surface);
3187 if (sub)
3188 return sub;
3189
3190 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3191 if (sub->surface == surface)
3192 return sub;
3193
3194 return NULL;
3195}
3196
3197static struct weston_subsurface *
3198subsurface_sibling_check(struct weston_subsurface *sub,
3199 struct weston_surface *surface,
3200 const char *request)
3201{
3202 struct weston_subsurface *sibling;
3203
3204 sibling = subsurface_from_surface(surface);
3205
3206 if (!sibling) {
3207 wl_resource_post_error(sub->resource,
3208 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3209 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003210 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003211 return NULL;
3212 }
3213
3214 if (sibling->parent != sub->parent) {
3215 wl_resource_post_error(sub->resource,
3216 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3217 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003218 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003219 return NULL;
3220 }
3221
3222 return sibling;
3223}
3224
3225static void
3226subsurface_place_above(struct wl_client *client,
3227 struct wl_resource *resource,
3228 struct wl_resource *sibling_resource)
3229{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003230 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003231 struct weston_surface *surface =
3232 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003233 struct weston_subsurface *sibling;
3234
3235 if (!sub)
3236 return;
3237
3238 sibling = subsurface_sibling_check(sub, surface, "place_above");
3239 if (!sibling)
3240 return;
3241
3242 wl_list_remove(&sub->parent_link_pending);
3243 wl_list_insert(sibling->parent_link_pending.prev,
3244 &sub->parent_link_pending);
3245}
3246
3247static void
3248subsurface_place_below(struct wl_client *client,
3249 struct wl_resource *resource,
3250 struct wl_resource *sibling_resource)
3251{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003252 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003253 struct weston_surface *surface =
3254 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003255 struct weston_subsurface *sibling;
3256
3257 if (!sub)
3258 return;
3259
3260 sibling = subsurface_sibling_check(sub, surface, "place_below");
3261 if (!sibling)
3262 return;
3263
3264 wl_list_remove(&sub->parent_link_pending);
3265 wl_list_insert(&sibling->parent_link_pending,
3266 &sub->parent_link_pending);
3267}
3268
3269static void
3270subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3271{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003272 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003273
3274 if (sub)
3275 sub->synchronized = 1;
3276}
3277
3278static void
3279subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3280{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003281 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003282
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003283 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003284 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003285
3286 /* If sub became effectively desynchronized, flush. */
3287 if (!weston_subsurface_is_synchronized(sub))
3288 weston_subsurface_synchronized_commit(sub);
3289 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003290}
3291
3292static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003293weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3294{
3295 wl_list_remove(&sub->parent_link);
3296 wl_list_remove(&sub->parent_link_pending);
3297 wl_list_remove(&sub->parent_destroy_listener.link);
3298 sub->parent = NULL;
3299}
3300
3301static void
3302weston_subsurface_destroy(struct weston_subsurface *sub);
3303
3304static void
3305subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3306{
3307 struct weston_subsurface *sub =
3308 container_of(listener, struct weston_subsurface,
3309 surface_destroy_listener);
3310 assert(data == &sub->surface->resource);
3311
3312 /* The protocol object (wl_resource) is left inert. */
3313 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003314 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003315
3316 weston_subsurface_destroy(sub);
3317}
3318
3319static void
3320subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3321{
3322 struct weston_subsurface *sub =
3323 container_of(listener, struct weston_subsurface,
3324 parent_destroy_listener);
3325 assert(data == &sub->parent->resource);
3326 assert(sub->surface != sub->parent);
3327
3328 if (weston_surface_is_mapped(sub->surface))
3329 weston_surface_unmap(sub->surface);
3330
3331 weston_subsurface_unlink_parent(sub);
3332}
3333
3334static void
3335subsurface_resource_destroy(struct wl_resource *resource)
3336{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003337 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003338
3339 if (sub)
3340 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003341}
3342
3343static void
3344subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3345{
3346 wl_resource_destroy(resource);
3347}
3348
3349static void
3350weston_subsurface_link_parent(struct weston_subsurface *sub,
3351 struct weston_surface *parent)
3352{
3353 sub->parent = parent;
3354 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003355 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003356 &sub->parent_destroy_listener);
3357
3358 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3359 wl_list_insert(&parent->subsurface_list_pending,
3360 &sub->parent_link_pending);
3361}
3362
3363static void
3364weston_subsurface_link_surface(struct weston_subsurface *sub,
3365 struct weston_surface *surface)
3366{
3367 sub->surface = surface;
3368 sub->surface_destroy_listener.notify =
3369 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003370 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003371 &sub->surface_destroy_listener);
3372}
3373
3374static void
3375weston_subsurface_destroy(struct weston_subsurface *sub)
3376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003377 struct weston_view *view, *next;
3378
Pekka Paalanene67858b2013-04-25 13:57:42 +03003379 assert(sub->surface);
3380
3381 if (sub->resource) {
3382 assert(weston_surface_to_subsurface(sub->surface) == sub);
3383 assert(sub->parent_destroy_listener.notify ==
3384 subsurface_handle_parent_destroy);
3385
George Kiagiadakised04d382014-06-13 18:10:26 +02003386 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3387 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003388 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003389 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003390
Pekka Paalanene67858b2013-04-25 13:57:42 +03003391 if (sub->parent)
3392 weston_subsurface_unlink_parent(sub);
3393
Jason Ekstrand7b982072014-05-20 14:33:03 -05003394 weston_surface_state_fini(&sub->cached);
3395 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003396
3397 sub->surface->configure = NULL;
3398 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003399 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003400 } else {
3401 /* the dummy weston_subsurface for the parent itself */
3402 assert(sub->parent_destroy_listener.notify == NULL);
3403 wl_list_remove(&sub->parent_link);
3404 wl_list_remove(&sub->parent_link_pending);
3405 }
3406
3407 wl_list_remove(&sub->surface_destroy_listener.link);
3408 free(sub);
3409}
3410
3411static const struct wl_subsurface_interface subsurface_implementation = {
3412 subsurface_destroy,
3413 subsurface_set_position,
3414 subsurface_place_above,
3415 subsurface_place_below,
3416 subsurface_set_sync,
3417 subsurface_set_desync
3418};
3419
3420static struct weston_subsurface *
3421weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3422 struct weston_surface *parent)
3423{
3424 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003425 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003426
Bryce Harringtonde16d892014-11-20 22:21:57 -08003427 sub = zalloc(sizeof *sub);
3428 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003429 return NULL;
3430
Jason Ekstranda7af7042013-10-12 22:38:11 -05003431 wl_list_init(&sub->unused_views);
3432
Jason Ekstranda85118c2013-06-27 20:17:02 -05003433 sub->resource =
3434 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003435 if (!sub->resource) {
3436 free(sub);
3437 return NULL;
3438 }
3439
Jason Ekstranda85118c2013-06-27 20:17:02 -05003440 wl_resource_set_implementation(sub->resource,
3441 &subsurface_implementation,
3442 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003443 weston_subsurface_link_surface(sub, surface);
3444 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003445 weston_surface_state_init(&sub->cached);
3446 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003447 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003448
3449 return sub;
3450}
3451
3452/* Create a dummy subsurface for having the parent itself in its
3453 * sub-surface lists. Makes stacking order manipulation easy.
3454 */
3455static struct weston_subsurface *
3456weston_subsurface_create_for_parent(struct weston_surface *parent)
3457{
3458 struct weston_subsurface *sub;
3459
Bryce Harringtonde16d892014-11-20 22:21:57 -08003460 sub = zalloc(sizeof *sub);
3461 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003462 return NULL;
3463
3464 weston_subsurface_link_surface(sub, parent);
3465 sub->parent = parent;
3466 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3467 wl_list_insert(&parent->subsurface_list_pending,
3468 &sub->parent_link_pending);
3469
3470 return sub;
3471}
3472
3473static void
3474subcompositor_get_subsurface(struct wl_client *client,
3475 struct wl_resource *resource,
3476 uint32_t id,
3477 struct wl_resource *surface_resource,
3478 struct wl_resource *parent_resource)
3479{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003480 struct weston_surface *surface =
3481 wl_resource_get_user_data(surface_resource);
3482 struct weston_surface *parent =
3483 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003484 struct weston_subsurface *sub;
3485 static const char where[] = "get_subsurface: wl_subsurface@";
3486
3487 if (surface == parent) {
3488 wl_resource_post_error(resource,
3489 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3490 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003491 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003492 return;
3493 }
3494
3495 if (weston_surface_to_subsurface(surface)) {
3496 wl_resource_post_error(resource,
3497 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3498 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003499 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500 return;
3501 }
3502
Pekka Paalanen50b67472014-10-01 15:02:41 +03003503 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3504 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003505 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003506
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003507 if (weston_surface_get_main_surface(parent) == surface) {
3508 wl_resource_post_error(resource,
3509 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3510 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003511 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003512 return;
3513 }
3514
Pekka Paalanene67858b2013-04-25 13:57:42 +03003515 /* make sure the parent is in its own list */
3516 if (wl_list_empty(&parent->subsurface_list)) {
3517 if (!weston_subsurface_create_for_parent(parent)) {
3518 wl_resource_post_no_memory(resource);
3519 return;
3520 }
3521 }
3522
3523 sub = weston_subsurface_create(id, surface, parent);
3524 if (!sub) {
3525 wl_resource_post_no_memory(resource);
3526 return;
3527 }
3528
3529 surface->configure = subsurface_configure;
3530 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003531 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003532}
3533
3534static void
3535subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3536{
3537 wl_resource_destroy(resource);
3538}
3539
3540static const struct wl_subcompositor_interface subcompositor_interface = {
3541 subcompositor_destroy,
3542 subcompositor_get_subsurface
3543};
3544
3545static void
3546bind_subcompositor(struct wl_client *client,
3547 void *data, uint32_t version, uint32_t id)
3548{
3549 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003550 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003551
Jason Ekstranda85118c2013-06-27 20:17:02 -05003552 resource =
3553 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003554 if (resource == NULL) {
3555 wl_client_post_no_memory(client);
3556 return;
3557 }
3558 wl_resource_set_implementation(resource, &subcompositor_interface,
3559 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003560}
3561
3562static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003563weston_compositor_dpms(struct weston_compositor *compositor,
3564 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003565{
3566 struct weston_output *output;
3567
3568 wl_list_for_each(output, &compositor->output_list, link)
3569 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003570 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003571}
3572
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003573WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003574weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003575{
Neil Roberts8b62e202013-09-30 13:14:47 +01003576 uint32_t old_state = compositor->state;
3577
3578 /* The state needs to be changed before emitting the wake
3579 * signal because that may try to schedule a repaint which
3580 * will not work if the compositor is still sleeping */
3581 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3582
3583 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003584 case WESTON_COMPOSITOR_SLEEPING:
3585 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3586 /* fall through */
3587 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003588 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003589 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003590 /* fall through */
3591 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003592 wl_event_source_timer_update(compositor->idle_source,
3593 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003594 }
3595}
3596
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003597WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003598weston_compositor_offscreen(struct weston_compositor *compositor)
3599{
3600 switch (compositor->state) {
3601 case WESTON_COMPOSITOR_OFFSCREEN:
3602 return;
3603 case WESTON_COMPOSITOR_SLEEPING:
3604 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3605 /* fall through */
3606 default:
3607 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3608 wl_event_source_timer_update(compositor->idle_source, 0);
3609 }
3610}
3611
3612WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003613weston_compositor_sleep(struct weston_compositor *compositor)
3614{
3615 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3616 return;
3617
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003618 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003619 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3620 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3621}
3622
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003623static int
3624idle_handler(void *data)
3625{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003626 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003627
3628 if (compositor->idle_inhibit)
3629 return 1;
3630
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003631 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003632 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003633
3634 return 1;
3635}
3636
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003637WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003638weston_plane_init(struct weston_plane *plane,
3639 struct weston_compositor *ec,
3640 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003641{
3642 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003643 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003644 plane->x = x;
3645 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003646 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003647
3648 /* Init the link so that the call to wl_list_remove() when releasing
3649 * the plane without ever stacking doesn't lead to a crash */
3650 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003651}
3652
3653WL_EXPORT void
3654weston_plane_release(struct weston_plane *plane)
3655{
Xiong Zhang97116532013-10-23 13:58:31 +08003656 struct weston_view *view;
3657
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003658 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003659 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003660
Xiong Zhang97116532013-10-23 13:58:31 +08003661 wl_list_for_each(view, &plane->compositor->view_list, link) {
3662 if (view->plane == plane)
3663 view->plane = NULL;
3664 }
3665
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003666 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003667}
3668
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003669WL_EXPORT void
3670weston_compositor_stack_plane(struct weston_compositor *ec,
3671 struct weston_plane *plane,
3672 struct weston_plane *above)
3673{
3674 if (above)
3675 wl_list_insert(above->link.prev, &plane->link);
3676 else
3677 wl_list_insert(&ec->plane_list, &plane->link);
3678}
3679
Casey Dahlin9074db52012-04-19 22:50:09 -04003680static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003681{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003682 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003683}
3684
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003685static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003686bind_output(struct wl_client *client,
3687 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003688{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003689 struct weston_output *output = data;
3690 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003691 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003692
Jason Ekstranda85118c2013-06-27 20:17:02 -05003693 resource = wl_resource_create(client, &wl_output_interface,
3694 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003695 if (resource == NULL) {
3696 wl_client_post_no_memory(client);
3697 return;
3698 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003699
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003700 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003701 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003702
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003703 wl_output_send_geometry(resource,
3704 output->x,
3705 output->y,
3706 output->mm_width,
3707 output->mm_height,
3708 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003709 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003710 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003711 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003712 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003713 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003714
3715 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003716 wl_output_send_mode(resource,
3717 mode->flags,
3718 mode->width,
3719 mode->height,
3720 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003721 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003722
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003723 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003724 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003725}
3726
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003727/* Move other outputs when one is removed so the space remains contiguos. */
3728static void
3729weston_compositor_remove_output(struct weston_compositor *compositor,
3730 struct weston_output *remove_output)
3731{
3732 struct weston_output *output;
3733 int offset = 0;
3734
3735 wl_list_for_each(output, &compositor->output_list, link) {
3736 if (output == remove_output) {
3737 offset = output->width;
3738 continue;
3739 }
3740
3741 if (offset > 0) {
3742 weston_output_move(output,
3743 output->x - offset, output->y);
3744 output->dirty = 1;
3745 }
3746 }
3747}
3748
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003749WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003750weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003751{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003752 struct wl_resource *resource;
3753
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003754 output->destroying = 1;
3755
Pekka Paalanen133e4392014-09-23 22:08:46 -04003756 weston_presentation_feedback_discard_list(&output->feedback_list);
3757
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003758 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003759 wl_list_remove(&output->link);
3760
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003761 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003762 wl_signal_emit(&output->destroy_signal, output);
3763
Richard Hughesafe690c2013-05-02 10:10:04 +01003764 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003765 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003766 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003767 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003768
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003769 wl_resource_for_each(resource, &output->resource_list) {
3770 wl_resource_set_destructor(resource, NULL);
3771 }
3772
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003773 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003774}
3775
Scott Moreau1bad5db2012-08-18 01:04:05 -06003776static void
3777weston_output_compute_transform(struct weston_output *output)
3778{
3779 struct weston_matrix transform;
3780 int flip;
3781
3782 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003783 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003784
3785 switch(output->transform) {
3786 case WL_OUTPUT_TRANSFORM_FLIPPED:
3787 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3788 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3789 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003790 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003791 flip = -1;
3792 break;
3793 default:
3794 flip = 1;
3795 break;
3796 }
3797
3798 switch(output->transform) {
3799 case WL_OUTPUT_TRANSFORM_NORMAL:
3800 case WL_OUTPUT_TRANSFORM_FLIPPED:
3801 transform.d[0] = flip;
3802 transform.d[1] = 0;
3803 transform.d[4] = 0;
3804 transform.d[5] = 1;
3805 break;
3806 case WL_OUTPUT_TRANSFORM_90:
3807 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3808 transform.d[0] = 0;
3809 transform.d[1] = -flip;
3810 transform.d[4] = 1;
3811 transform.d[5] = 0;
3812 break;
3813 case WL_OUTPUT_TRANSFORM_180:
3814 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3815 transform.d[0] = -flip;
3816 transform.d[1] = 0;
3817 transform.d[4] = 0;
3818 transform.d[5] = -1;
3819 break;
3820 case WL_OUTPUT_TRANSFORM_270:
3821 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3822 transform.d[0] = 0;
3823 transform.d[1] = flip;
3824 transform.d[4] = -1;
3825 transform.d[5] = 0;
3826 break;
3827 default:
3828 break;
3829 }
3830
3831 weston_matrix_multiply(&output->matrix, &transform);
3832}
3833
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003834WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003835weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003836{
Scott Moreau850ca422012-05-21 15:21:25 -06003837 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003838
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003839 weston_matrix_init(&output->matrix);
3840 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003841 -(output->x + output->width / 2.0),
3842 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003843
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003844 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003845 2.0 / output->width,
3846 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003847
Scott Moreauccbf29d2012-02-22 14:21:41 -07003848 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003849 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003850 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003851 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3852 output->zoom.trans_y, 0);
3853 weston_matrix_scale(&output->matrix, magnification,
3854 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003855 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003856
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003857 weston_output_compute_transform(output);
3858
Scott Moreauccbf29d2012-02-22 14:21:41 -07003859 output->dirty = 0;
3860}
3861
Scott Moreau1bad5db2012-08-18 01:04:05 -06003862static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003863weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003864{
3865 output->transform = transform;
3866
3867 switch (transform) {
3868 case WL_OUTPUT_TRANSFORM_90:
3869 case WL_OUTPUT_TRANSFORM_270:
3870 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3871 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3872 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003873 output->width = output->current_mode->height;
3874 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003875 break;
3876 case WL_OUTPUT_TRANSFORM_NORMAL:
3877 case WL_OUTPUT_TRANSFORM_180:
3878 case WL_OUTPUT_TRANSFORM_FLIPPED:
3879 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003880 output->width = output->current_mode->width;
3881 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003882 break;
3883 default:
3884 break;
3885 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003886
Hardening57388e42013-09-18 23:56:36 +02003887 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003888 output->width /= scale;
3889 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003890}
3891
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003892static void
3893weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003894{
3895 output->x = x;
3896 output->y = y;
3897
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003898 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003899 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003900 output->width,
3901 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003902}
3903
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003904WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003905weston_output_move(struct weston_output *output, int x, int y)
3906{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003907 struct wl_resource *resource;
3908
3909 output->move_x = x - output->x;
3910 output->move_y = y - output->y;
3911
3912 if (output->move_x == 0 && output->move_y == 0)
3913 return;
3914
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003915 weston_output_init_geometry(output, x, y);
3916
3917 output->dirty = 1;
3918
3919 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003920 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003921
3922 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003923 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003924 wl_output_send_geometry(resource,
3925 output->x,
3926 output->y,
3927 output->mm_width,
3928 output->mm_height,
3929 output->subpixel,
3930 output->make,
3931 output->model,
3932 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003933
3934 if (wl_resource_get_version(resource) >= 2)
3935 wl_output_send_done(resource);
3936 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003937}
3938
3939WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003940weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003941 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003942 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003943{
3944 output->compositor = c;
3945 output->x = x;
3946 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003947 output->mm_width = mm_width;
3948 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003949 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003950 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003951
Alexander Larsson0b135062013-05-28 16:23:36 +02003952 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003953 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003954
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003955 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003956 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003957
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003958 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003959 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003960 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003961 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003962 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003963
Casey Dahlin58ba1372012-04-19 22:50:08 -04003964 output->id = ffs(~output->compositor->output_id_pool) - 1;
3965 output->compositor->output_id_pool |= 1 << output->id;
3966
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003967 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003968 wl_global_create(c->wl_display, &wl_output_interface, 2,
3969 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003970 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003971}
3972
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003973WL_EXPORT void
3974weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003975 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003976 wl_fixed_t *x, wl_fixed_t *y)
3977{
3978 wl_fixed_t tx, ty;
3979 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003980 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003981
Hardeningff39efa2013-09-18 23:56:35 +02003982 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3983 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003984
3985 switch(output->transform) {
3986 case WL_OUTPUT_TRANSFORM_NORMAL:
3987 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003988 tx = device_x;
3989 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003990 break;
3991 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003992 tx = device_y;
3993 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003994 break;
3995 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003996 tx = width - device_x;
3997 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003998 break;
3999 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004000 tx = width - device_y;
4001 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004002 break;
4003 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004004 tx = width - device_x;
4005 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004006 break;
4007 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004008 tx = width - device_y;
4009 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004010 break;
4011 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004012 tx = device_x;
4013 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004014 break;
4015 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004016 tx = device_y;
4017 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004018 break;
4019 }
4020
Neil Robertseb5a2002014-05-01 16:13:55 +01004021 tx /= output->current_scale;
4022 ty /= output->current_scale;
4023
4024 if (output->zoom.active) {
4025 zoom_scale = output->zoom.spring_z.current;
4026 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
4027 output->width / 2.0f *
4028 (zoom_scale + output->zoom.trans_x));
4029 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
4030 output->height / 2.0f *
4031 (zoom_scale + output->zoom.trans_y));
4032 tx = wl_fixed_from_double(zx);
4033 ty = wl_fixed_from_double(zy);
4034 }
4035
4036 *x = tx + wl_fixed_from_int(output->x);
4037 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004038}
4039
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004040static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004041destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004042{
Jonny Lamb74130762013-11-26 18:19:46 +01004043 struct weston_surface *surface =
4044 wl_resource_get_user_data(resource);
4045
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004046 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004047 surface->pending.buffer_viewport.buffer.src_width =
4048 wl_fixed_from_int(-1);
4049 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004050 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004051}
4052
4053static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004054viewport_destroy(struct wl_client *client,
4055 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004056{
4057 wl_resource_destroy(resource);
4058}
4059
4060static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004061viewport_set(struct wl_client *client,
4062 struct wl_resource *resource,
4063 wl_fixed_t src_x,
4064 wl_fixed_t src_y,
4065 wl_fixed_t src_width,
4066 wl_fixed_t src_height,
4067 int32_t dst_width,
4068 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004069{
Jonny Lamb74130762013-11-26 18:19:46 +01004070 struct weston_surface *surface =
4071 wl_resource_get_user_data(resource);
4072
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004073 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004074
Jonny Lamb8ae35902013-11-26 18:19:45 +01004075 if (wl_fixed_to_double(src_width) < 0 ||
4076 wl_fixed_to_double(src_height) < 0) {
4077 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004078 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004079 "source dimensions must be non-negative (%fx%f)",
4080 wl_fixed_to_double(src_width),
4081 wl_fixed_to_double(src_height));
4082 return;
4083 }
4084
4085 if (dst_width <= 0 || dst_height <= 0) {
4086 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004087 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004088 "destination dimensions must be positive (%dx%d)",
4089 dst_width, dst_height);
4090 return;
4091 }
4092
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004093 surface->pending.buffer_viewport.buffer.src_x = src_x;
4094 surface->pending.buffer_viewport.buffer.src_y = src_y;
4095 surface->pending.buffer_viewport.buffer.src_width = src_width;
4096 surface->pending.buffer_viewport.buffer.src_height = src_height;
4097 surface->pending.buffer_viewport.surface.width = dst_width;
4098 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004099 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004100}
4101
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004102static void
4103viewport_set_source(struct wl_client *client,
4104 struct wl_resource *resource,
4105 wl_fixed_t src_x,
4106 wl_fixed_t src_y,
4107 wl_fixed_t src_width,
4108 wl_fixed_t src_height)
4109{
4110 struct weston_surface *surface =
4111 wl_resource_get_user_data(resource);
4112
4113 assert(surface->viewport_resource != NULL);
4114
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004115 if (src_width == wl_fixed_from_int(-1) &&
4116 src_height == wl_fixed_from_int(-1)) {
4117 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004118 surface->pending.buffer_viewport.buffer.src_width =
4119 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004120 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004121 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004122 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004123
4124 if (src_width <= 0 || src_height <= 0) {
4125 wl_resource_post_error(resource,
4126 WL_VIEWPORT_ERROR_BAD_VALUE,
4127 "source size must be positive (%fx%f)",
4128 wl_fixed_to_double(src_width),
4129 wl_fixed_to_double(src_height));
4130 return;
4131 }
4132
4133 surface->pending.buffer_viewport.buffer.src_x = src_x;
4134 surface->pending.buffer_viewport.buffer.src_y = src_y;
4135 surface->pending.buffer_viewport.buffer.src_width = src_width;
4136 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004137 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004138}
4139
4140static void
4141viewport_set_destination(struct wl_client *client,
4142 struct wl_resource *resource,
4143 int32_t dst_width,
4144 int32_t dst_height)
4145{
4146 struct weston_surface *surface =
4147 wl_resource_get_user_data(resource);
4148
4149 assert(surface->viewport_resource != NULL);
4150
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004151 if (dst_width == -1 && dst_height == -1) {
4152 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004153 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004154 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004155 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004156 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004157
4158 if (dst_width <= 0 || dst_height <= 0) {
4159 wl_resource_post_error(resource,
4160 WL_VIEWPORT_ERROR_BAD_VALUE,
4161 "destination size must be positive (%dx%d)",
4162 dst_width, dst_height);
4163 return;
4164 }
4165
4166 surface->pending.buffer_viewport.surface.width = dst_width;
4167 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004168 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004169}
4170
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004171static const struct wl_viewport_interface viewport_interface = {
4172 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004173 viewport_set,
4174 viewport_set_source,
4175 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004176};
4177
4178static void
4179scaler_destroy(struct wl_client *client,
4180 struct wl_resource *resource)
4181{
4182 wl_resource_destroy(resource);
4183}
4184
4185static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004186scaler_get_viewport(struct wl_client *client,
4187 struct wl_resource *scaler,
4188 uint32_t id,
4189 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004190{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004191 int version = wl_resource_get_version(scaler);
4192 struct weston_surface *surface =
4193 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004194 struct wl_resource *resource;
4195
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004196 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004197 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004198 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4199 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004200 return;
4201 }
4202
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004203 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004204 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004205 if (resource == NULL) {
4206 wl_client_post_no_memory(client);
4207 return;
4208 }
4209
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004210 wl_resource_set_implementation(resource, &viewport_interface,
4211 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004212
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004213 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004214}
4215
4216static const struct wl_scaler_interface scaler_interface = {
4217 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004218 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004219};
4220
4221static void
4222bind_scaler(struct wl_client *client,
4223 void *data, uint32_t version, uint32_t id)
4224{
4225 struct wl_resource *resource;
4226
4227 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004228 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004229 if (resource == NULL) {
4230 wl_client_post_no_memory(client);
4231 return;
4232 }
4233
4234 wl_resource_set_implementation(resource, &scaler_interface,
4235 NULL, NULL);
4236}
4237
4238static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004239destroy_presentation_feedback(struct wl_resource *feedback_resource)
4240{
4241 struct weston_presentation_feedback *feedback;
4242
4243 feedback = wl_resource_get_user_data(feedback_resource);
4244
4245 wl_list_remove(&feedback->link);
4246 free(feedback);
4247}
4248
4249static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004250presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4251{
4252 wl_resource_destroy(resource);
4253}
4254
4255static void
4256presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004257 struct wl_resource *presentation_resource,
4258 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004259 uint32_t callback)
4260{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004261 struct weston_surface *surface;
4262 struct weston_presentation_feedback *feedback;
4263
4264 surface = wl_resource_get_user_data(surface_resource);
4265
Bryce Harringtonde16d892014-11-20 22:21:57 -08004266 feedback = zalloc(sizeof *feedback);
4267 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004268 goto err_calloc;
4269
4270 feedback->resource = wl_resource_create(client,
4271 &presentation_feedback_interface,
4272 1, callback);
4273 if (!feedback->resource)
4274 goto err_create;
4275
4276 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4277 destroy_presentation_feedback);
4278
4279 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4280
4281 return;
4282
4283err_create:
4284 free(feedback);
4285
4286err_calloc:
4287 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004288}
4289
4290static const struct presentation_interface presentation_implementation = {
4291 presentation_destroy,
4292 presentation_feedback
4293};
4294
4295static void
4296bind_presentation(struct wl_client *client,
4297 void *data, uint32_t version, uint32_t id)
4298{
4299 struct weston_compositor *compositor = data;
4300 struct wl_resource *resource;
4301
4302 resource = wl_resource_create(client, &presentation_interface,
4303 MIN(version, 1), id);
4304 if (resource == NULL) {
4305 wl_client_post_no_memory(client);
4306 return;
4307 }
4308
4309 wl_resource_set_implementation(resource, &presentation_implementation,
4310 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004311 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004312}
4313
4314static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004315compositor_bind(struct wl_client *client,
4316 void *data, uint32_t version, uint32_t id)
4317{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004318 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004319 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004320
Jason Ekstranda85118c2013-06-27 20:17:02 -05004321 resource = wl_resource_create(client, &wl_compositor_interface,
4322 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004323 if (resource == NULL) {
4324 wl_client_post_no_memory(client);
4325 return;
4326 }
4327
4328 wl_resource_set_implementation(resource, &compositor_interface,
4329 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004330}
4331
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004332static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004333log_uname(void)
4334{
4335 struct utsname usys;
4336
4337 uname(&usys);
4338
4339 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4340 usys.version, usys.machine);
4341}
4342
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004343WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004344weston_environment_get_fd(const char *env)
4345{
4346 char *e, *end;
4347 int fd, flags;
4348
4349 e = getenv(env);
4350 if (!e)
4351 return -1;
4352 fd = strtol(e, &end, 0);
4353 if (*end != '\0')
4354 return -1;
4355
4356 flags = fcntl(fd, F_GETFD);
4357 if (flags == -1)
4358 return -1;
4359
4360 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4361 unsetenv(env);
4362
4363 return fd;
4364}
4365
Pekka Paalanenb5026542014-11-12 15:09:24 +02004366static void
4367timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4368 uint32_t key, void *data)
4369{
4370 struct weston_compositor *compositor = data;
4371
4372 if (weston_timeline_enabled_)
4373 weston_timeline_close();
4374 else
4375 weston_timeline_open(compositor);
4376}
4377
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004378WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004379weston_compositor_init(struct weston_compositor *ec,
4380 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004381 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004382 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004383{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004384 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004385 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004386 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004387
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004388 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004389 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004390 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004391 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004392 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004393 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004394 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004395 wl_signal_init(&ec->idle_signal);
4396 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004397 wl_signal_init(&ec->show_input_panel_signal);
4398 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004399 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004400 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004401 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004402 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004403 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004404 wl_signal_init(&ec->session_signal);
4405 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004406
Casey Dahlin58ba1372012-04-19 22:50:08 -04004407 ec->output_id_pool = 0;
4408
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004409 if (!wl_global_create(display, &wl_compositor_interface, 3,
4410 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004411 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004412
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004413 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4414 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004415 return -1;
4416
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004417 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004418 ec, bind_scaler))
4419 return -1;
4420
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004421 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4422 ec, bind_presentation))
4423 return -1;
4424
Jason Ekstranda7af7042013-10-12 22:38:11 -05004425 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004426 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004427 wl_list_init(&ec->layer_list);
4428 wl_list_init(&ec->seat_list);
4429 wl_list_init(&ec->output_list);
4430 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004431 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004432 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004433 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004434 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004435 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004436
Xiong Zhang97116532013-10-23 13:58:31 +08004437 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004438 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004439
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004440 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4441 weston_config_section_get_string(s, "keymap_rules",
4442 (char **) &xkb_names.rules, NULL);
4443 weston_config_section_get_string(s, "keymap_model",
4444 (char **) &xkb_names.model, NULL);
4445 weston_config_section_get_string(s, "keymap_layout",
4446 (char **) &xkb_names.layout, NULL);
4447 weston_config_section_get_string(s, "keymap_variant",
4448 (char **) &xkb_names.variant, NULL);
4449 weston_config_section_get_string(s, "keymap_options",
4450 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004451
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004452 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4453 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004454
Jonny Lamb66a41a02014-08-12 14:58:25 +02004455 weston_config_section_get_int(s, "repeat-rate",
4456 &ec->kb_repeat_rate, 40);
4457 weston_config_section_get_int(s, "repeat-delay",
4458 &ec->kb_repeat_delay, 400);
4459
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004460 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004461
4462 wl_data_device_manager_init(ec->wl_display);
4463
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004464 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004465
Daniel Stone725c2c32012-06-22 14:04:36 +01004466 loop = wl_display_get_event_loop(ec->wl_display);
4467 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4468 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4469
4470 ec->input_loop = wl_event_loop_create();
4471
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004472 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4473 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4474
Pekka Paalanenb5026542014-11-12 15:09:24 +02004475 weston_compositor_add_debug_binding(ec, KEY_T,
4476 timeline_key_binding_handler, ec);
4477
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004478 weston_compositor_schedule_repaint(ec);
4479
Daniel Stone725c2c32012-06-22 14:04:36 +01004480 return 0;
4481}
4482
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004483WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004484weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004485{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004486 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004487
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004488 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004489 if (ec->input_loop_source)
4490 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004491
Matt Roper361d2ad2011-08-29 13:52:23 -07004492 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004493 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004494 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004495
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004496 if (ec->renderer)
4497 ec->renderer->destroy(ec);
4498
Daniel Stone325fc2d2012-05-30 16:31:58 +01004499 weston_binding_list_destroy_all(&ec->key_binding_list);
4500 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004501 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004502 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004503 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004504
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004505 weston_plane_release(&ec->primary_plane);
4506
Jonas Ådahlc97af922012-03-28 22:36:09 +02004507 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004508
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004509 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004510}
4511
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004512WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004513weston_compositor_exit_with_code(struct weston_compositor *compositor,
4514 int exit_code)
4515{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004516 if (compositor->exit_code == EXIT_SUCCESS)
4517 compositor->exit_code = exit_code;
4518
Frederic Plourdec336f062014-10-29 14:44:33 -04004519 wl_display_terminate(compositor->wl_display);
4520}
4521
4522WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004523weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4524 const struct weston_pointer_grab_interface *interface)
4525{
4526 struct weston_seat *seat;
4527
4528 ec->default_pointer_grab = interface;
4529 wl_list_for_each(seat, &ec->seat_list, link) {
4530 if (seat->pointer) {
4531 weston_pointer_set_default_grab(seat->pointer,
4532 interface);
4533 }
4534 }
4535}
4536
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004537WL_EXPORT int
4538weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4539 clockid_t clk_id)
4540{
4541 struct timespec ts;
4542
4543 if (clock_gettime(clk_id, &ts) < 0)
4544 return -1;
4545
4546 compositor->presentation_clock = clk_id;
4547
4548 return 0;
4549}
4550
4551/*
4552 * For choosing the software clock, when the display hardware or API
4553 * does not expose a compatible presentation timestamp.
4554 */
4555WL_EXPORT int
4556weston_compositor_set_presentation_clock_software(
4557 struct weston_compositor *compositor)
4558{
4559 /* In order of preference */
4560 static const clockid_t clocks[] = {
4561 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4562 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4563 CLOCK_MONOTONIC, /* no jumps, may crawl */
4564 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4565 CLOCK_REALTIME /* may jump and crawl */
4566 };
4567 unsigned i;
4568
4569 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4570 if (weston_compositor_set_presentation_clock(compositor,
4571 clocks[i]) == 0)
4572 return 0;
4573
4574 weston_log("Error: no suitable presentation clock available.\n");
4575
4576 return -1;
4577}
4578
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004579WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004580weston_version(int *major, int *minor, int *micro)
4581{
4582 *major = WESTON_VERSION_MAJOR;
4583 *minor = WESTON_VERSION_MINOR;
4584 *micro = WESTON_VERSION_MICRO;
4585}
4586
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004587static const char *
4588clock_name(clockid_t clk_id)
4589{
4590 static const char *names[] = {
4591 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4592 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4593 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4594 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4595 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4596 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4597 };
4598
4599 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4600 return "unknown";
4601
4602 return names[clk_id];
4603}
4604
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004605static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004606 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004607 const char *desc;
4608} capability_strings[] = {
4609 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004610 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004611};
4612
4613static void
4614weston_compositor_log_capabilities(struct weston_compositor *compositor)
4615{
4616 unsigned i;
4617 int yes;
4618
4619 weston_log("Compositor capabilities:\n");
4620 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4621 yes = compositor->capabilities & capability_strings[i].bit;
4622 weston_log_continue(STAMP_SPACE "%s %s\n",
4623 capability_strings[i].desc,
4624 yes ? "yes" : "no");
4625 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004626
4627 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4628 clock_name(compositor->presentation_clock),
4629 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004630}
4631
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004632static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004633{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004634 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004635
Martin Minarik6d118362012-06-07 18:01:59 +02004636 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004637 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004638
4639 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004640}
4641
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004642#ifdef HAVE_LIBUNWIND
4643
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004644static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004645print_backtrace(void)
4646{
4647 unw_cursor_t cursor;
4648 unw_context_t context;
4649 unw_word_t off;
4650 unw_proc_info_t pip;
4651 int ret, i = 0;
4652 char procname[256];
4653 const char *filename;
4654 Dl_info dlinfo;
4655
4656 pip.unwind_info = NULL;
4657 ret = unw_getcontext(&context);
4658 if (ret) {
4659 weston_log("unw_getcontext: %d\n", ret);
4660 return;
4661 }
4662
4663 ret = unw_init_local(&cursor, &context);
4664 if (ret) {
4665 weston_log("unw_init_local: %d\n", ret);
4666 return;
4667 }
4668
4669 ret = unw_step(&cursor);
4670 while (ret > 0) {
4671 ret = unw_get_proc_info(&cursor, &pip);
4672 if (ret) {
4673 weston_log("unw_get_proc_info: %d\n", ret);
4674 break;
4675 }
4676
4677 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4678 if (ret && ret != -UNW_ENOMEM) {
4679 if (ret != -UNW_EUNSPEC)
4680 weston_log("unw_get_proc_name: %d\n", ret);
4681 procname[0] = '?';
4682 procname[1] = 0;
4683 }
4684
4685 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4686 *dlinfo.dli_fname)
4687 filename = dlinfo.dli_fname;
4688 else
4689 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004690
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004691 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4692 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4693
4694 ret = unw_step(&cursor);
4695 if (ret < 0)
4696 weston_log("unw_step: %d\n", ret);
4697 }
4698}
4699
4700#else
4701
4702static void
4703print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004704{
4705 void *buffer[32];
4706 int i, count;
4707 Dl_info info;
4708
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004709 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4710 for (i = 0; i < count; i++) {
4711 dladdr(buffer[i], &info);
4712 weston_log(" [%016lx] %s (%s)\n",
4713 (long) buffer[i],
4714 info.dli_sname ? info.dli_sname : "--",
4715 info.dli_fname);
4716 }
4717}
4718
4719#endif
4720
4721static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004722on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004723{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004724 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004725 * then call the backend restore function, which will switch
4726 * back to the vt we launched from or ungrab X etc and then
4727 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004728 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004729 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004730 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004731
Peter Maatmane5b42e42013-03-27 22:38:53 +01004732 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004733
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004734 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004735
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004736 segv_compositor->restore(segv_compositor);
4737
4738 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004739}
4740
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004741WL_EXPORT void *
4742weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004743{
4744 char path[PATH_MAX];
4745 void *module, *init;
4746
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004747 if (name == NULL)
4748 return NULL;
4749
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004750 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004751 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004752 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004753 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004754
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004755 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4756 if (module) {
4757 weston_log("Module '%s' already loaded\n", path);
4758 dlclose(module);
4759 return NULL;
4760 }
4761
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004762 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004763 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004764 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004765 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004766 return NULL;
4767 }
4768
4769 init = dlsym(module, entrypoint);
4770 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004771 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004772 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004773 return NULL;
4774 }
4775
4776 return init;
4777}
4778
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004779static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004780load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004781 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004782{
4783 const char *p, *end;
4784 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004785 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004786 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004787
4788 if (modules == NULL)
4789 return 0;
4790
4791 p = modules;
4792 while (*p) {
4793 end = strchrnul(p, ',');
4794 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004795 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004796 if (!module_init)
4797 return -1;
4798 if (module_init(ec, argc, argv) < 0)
4799 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004800 p = end;
4801 while (*p == ',')
4802 p++;
4803
4804 }
4805
4806 return 0;
4807}
4808
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004809static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004810 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4811
4812static const char xdg_wrong_message[] =
4813 "fatal: environment variable XDG_RUNTIME_DIR\n"
4814 "is set to \"%s\", which is not a directory.\n";
4815
4816static const char xdg_wrong_mode_message[] =
4817 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004818 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4819 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004820
4821static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004822 "Refer to your distribution on how to get it, or\n"
4823 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4824 "on how to implement it.\n";
4825
Martin Minarik37032f82012-06-18 20:15:18 +02004826static void
4827verify_xdg_runtime_dir(void)
4828{
4829 char *dir = getenv("XDG_RUNTIME_DIR");
4830 struct stat s;
4831
4832 if (!dir) {
4833 weston_log(xdg_error_message);
4834 weston_log_continue(xdg_detail_message);
4835 exit(EXIT_FAILURE);
4836 }
4837
4838 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4839 weston_log(xdg_wrong_message, dir);
4840 weston_log_continue(xdg_detail_message);
4841 exit(EXIT_FAILURE);
4842 }
4843
4844 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4845 weston_log(xdg_wrong_mode_message,
4846 dir, s.st_mode & 0777, s.st_uid);
4847 weston_log_continue(xdg_detail_message);
4848 }
4849}
4850
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004851static int
4852usage(int error_code)
4853{
4854 fprintf(stderr,
4855 "Usage: weston [OPTIONS]\n\n"
4856 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4857 "Weston supports multiple backends, and depending on which backend is in use\n"
4858 "different options will be accepted.\n\n"
4859
4860
4861 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004862 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004863 " -B, --backend=MODULE\tBackend module, one of\n"
4864#if defined(BUILD_DRM_COMPOSITOR)
4865 "\t\t\t\tdrm-backend.so\n"
4866#endif
4867#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004868 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004869#endif
4870#if defined(BUILD_X11_COMPOSITOR)
4871 "\t\t\t\tx11-backend.so\n"
4872#endif
4873#if defined(BUILD_WAYLAND_COMPOSITOR)
4874 "\t\t\t\twayland-backend.so\n"
4875#endif
4876#if defined(BUILD_RDP_COMPOSITOR)
4877 "\t\t\t\trdp-backend.so\n"
4878#endif
4879#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4880 "\t\t\t\trpi-backend.so\n"
4881#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004882 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004883 " -S, --socket=NAME\tName of socket to listen on\n"
4884 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004885 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004886 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004887 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004888 " -h, --help\t\tThis help message\n\n");
4889
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004890#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004891 fprintf(stderr,
4892 "Options for drm-backend.so:\n\n"
4893 " --connector=ID\tBring up only this connector\n"
4894 " --seat=SEAT\t\tThe seat that weston should run on\n"
4895 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004896 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004897 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004898#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004899
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004900#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004901 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004902 "Options for fbdev-backend.so:\n\n"
4903 " --tty=TTY\t\tThe tty to use\n"
4904 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004905#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004906
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004907#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004908 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004909 "Options for x11-backend.so:\n\n"
4910 " --width=WIDTH\t\tWidth of X window\n"
4911 " --height=HEIGHT\tHeight of X window\n"
4912 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004913 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004914 " --output-count=COUNT\tCreate multiple outputs\n"
4915 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004916#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004917
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004918#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004919 fprintf(stderr,
4920 "Options for wayland-backend.so:\n\n"
4921 " --width=WIDTH\t\tWidth of Wayland surface\n"
4922 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004923 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004924 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004925 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004926 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004927 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004928 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004929#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004930
Pekka Paalanene31e0532013-05-22 18:03:07 +03004931#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4932 fprintf(stderr,
4933 "Options for rpi-backend.so:\n\n"
4934 " --tty=TTY\t\tThe tty to use\n"
4935 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4936 " --transform=TR\tThe output transformation, TR is one of:\n"
4937 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004938 " --opaque-regions\tEnable support for opaque regions, can be "
4939 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004940 "\n");
4941#endif
4942
Hardeningc077a842013-07-08 00:51:35 +02004943#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004944 fprintf(stderr,
4945 "Options for rdp-backend.so:\n\n"
4946 " --width=WIDTH\t\tWidth of desktop\n"
4947 " --height=HEIGHT\tHeight of desktop\n"
4948 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4949 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004950 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004951 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4952 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4953 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4954 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4955 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004956#endif
4957
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004958#if defined(BUILD_HEADLESS_COMPOSITOR)
4959 fprintf(stderr,
4960 "Options for headless-backend.so:\n\n"
4961 " --width=WIDTH\t\tWidth of memory surface\n"
4962 " --height=HEIGHT\tHeight of memory surface\n"
4963 " --transform=TR\tThe output transformation, TR is one of:\n"
4964 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4965 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4966#endif
4967
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004968 exit(error_code);
4969}
4970
Peter Maatmane5b42e42013-03-27 22:38:53 +01004971static void
4972catch_signals(void)
4973{
4974 struct sigaction action;
4975
4976 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4977 action.sa_sigaction = on_caught_signal;
4978 sigemptyset(&action.sa_mask);
4979 sigaction(SIGSEGV, &action, NULL);
4980 sigaction(SIGABRT, &action, NULL);
4981}
4982
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004983static void
4984handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4985{
4986 struct wl_client *client = data;
4987
4988 weston_log("Primary client died. Closing...\n");
4989
4990 wl_display_terminate(wl_client_get_display(client));
4991}
4992
Ryo Munakatad8deff62014-09-06 07:32:05 +09004993static char *
4994weston_choose_default_backend(void)
4995{
4996 char *backend = NULL;
4997
4998 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4999 backend = strdup("wayland-backend.so");
5000 else if (getenv("DISPLAY"))
5001 backend = strdup("x11-backend.so");
5002 else
5003 backend = strdup(WESTON_NATIVE_BACKEND);
5004
5005 return backend;
5006}
5007
5008static int
5009weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5010{
5011 if (socket_name) {
5012 if (wl_display_add_socket(display, socket_name)) {
5013 weston_log("fatal: failed to add socket: %m\n");
5014 return -1;
5015 }
5016 } else {
5017 socket_name = wl_display_add_socket_auto(display);
5018 if (!socket_name) {
5019 weston_log("fatal: failed to add socket: %m\n");
5020 return -1;
5021 }
5022 }
5023
5024 setenv("WAYLAND_DISPLAY", socket_name, 1);
5025
5026 return 0;
5027}
5028
Derek Foreman64a3df02014-10-23 12:24:18 -05005029static const struct { const char *name; uint32_t token; } transforms[] = {
5030 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5031 { "90", WL_OUTPUT_TRANSFORM_90 },
5032 { "180", WL_OUTPUT_TRANSFORM_180 },
5033 { "270", WL_OUTPUT_TRANSFORM_270 },
5034 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5035 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5036 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5037 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5038};
5039
5040WL_EXPORT int
5041weston_parse_transform(const char *transform, uint32_t *out)
5042{
5043 unsigned int i;
5044
5045 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5046 if (strcmp(transforms[i].name, transform) == 0) {
5047 *out = transforms[i].token;
5048 return 0;
5049 }
5050
5051 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5052 return -1;
5053}
5054
5055WL_EXPORT const char *
5056weston_transform_to_string(uint32_t output_transform)
5057{
5058 unsigned int i;
5059
5060 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5061 if (transforms[i].token == output_transform)
5062 return transforms[i].name;
5063
5064 return "<illegal value>";
5065}
5066
5067
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005068int main(int argc, char *argv[])
5069{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005070 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005071 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005072 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005073 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005074 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005075 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005076 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005077 int *argc, char *argv[],
5078 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005079 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005080 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005081 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005082 char *modules = NULL;
5083 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005084 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005085 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005086 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005087 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005088 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005089 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005090 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005091 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005092 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005093 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005094 struct wl_client *primary_client;
5095 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005096 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005097
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005098 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005099 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5100 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005101 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5102 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005103 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005104 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005105 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005106 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005107 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005108 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005109
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005110 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005111
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005112 if (help)
5113 usage(EXIT_SUCCESS);
5114
Scott Moreau12245142012-08-29 15:15:58 -06005115 if (version) {
5116 printf(PACKAGE_STRING "\n");
5117 return EXIT_SUCCESS;
5118 }
5119
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005120 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005121
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005122 weston_log("%s\n"
5123 STAMP_SPACE "%s\n"
5124 STAMP_SPACE "Bug reports to: %s\n"
5125 STAMP_SPACE "Build: %s\n",
5126 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005127 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005128 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005129
Martin Minarik37032f82012-06-18 20:15:18 +02005130 verify_xdg_runtime_dir();
5131
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005132 display = wl_display_create();
5133
Tiago Vignatti2116b892011-08-08 05:52:59 -07005134 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005135 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5136 display);
5137 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5138 display);
5139 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5140 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005141
5142 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005143 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5144 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005145
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305146 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
5147 ret = EXIT_FAILURE;
5148 goto out_signals;
5149 }
5150
Pekka Paalanen588bee12014-05-07 16:26:25 +03005151 if (noconfig == 0)
5152 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005153 if (config != NULL) {
5154 weston_log("Using config file '%s'\n",
5155 weston_config_get_full_path(config));
5156 } else {
5157 weston_log("Starting with no config file.\n");
5158 }
5159 section = weston_config_get_section(config, "core", NULL, NULL);
5160
Ryo Munakata03faed22014-09-06 07:32:51 +09005161 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005162 weston_config_section_get_string(section, "backend", &backend,
5163 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005164 if (!backend)
5165 backend = weston_choose_default_backend();
5166 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005167
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005168 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305169 if (!backend_init) {
5170 ret = EXIT_FAILURE;
5171 goto out_signals;
5172 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05005173
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005174 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005175 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005176 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305177 ret = EXIT_FAILURE;
5178 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005179 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005180
Peter Maatmane5b42e42013-03-27 22:38:53 +01005181 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005182 segv_compositor = ec;
5183
Frederic Plourde4a84c832014-10-30 15:06:34 -04005184 if (idle_time < 0)
5185 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5186 if (idle_time < 0)
5187 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005188 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005189 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005190 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005191
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005192 for (i = 1; i < argc; i++)
5193 weston_log("fatal: unhandled option: %s\n", argv[i]);
5194 if (argc > 1) {
5195 ret = EXIT_FAILURE;
5196 goto out;
5197 }
5198
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005199 weston_compositor_log_capabilities(ec);
5200
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005201 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5202 if (server_socket) {
5203 weston_log("Running with single client\n");
5204 fd = strtol(server_socket, &end, 0);
5205 if (*end != '\0')
5206 fd = -1;
5207 } else {
5208 fd = -1;
5209 }
5210
5211 if (fd != -1) {
5212 primary_client = wl_client_create(display, fd);
5213 if (!primary_client) {
5214 weston_log("fatal: failed to add client: %m\n");
5215 ret = EXIT_FAILURE;
5216 goto out;
5217 }
5218 primary_client_destroyed.notify =
5219 handle_primary_client_destroyed;
5220 wl_client_add_destroy_listener(primary_client,
5221 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005222 } else if (weston_create_listening_socket(display, socket_name)) {
5223 ret = EXIT_FAILURE;
5224 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005225 }
5226
Ryo Munakata03faed22014-09-06 07:32:51 +09005227 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005228 weston_config_section_get_string(section, "shell", &shell,
5229 "desktop-shell.so");
5230
Ryo Munakata03faed22014-09-06 07:32:51 +09005231 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005232 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005233
5234 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005235 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005236 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005237
5238 if (load_modules(ec, option_modules, &argc, argv) < 0)
5239 goto out;
5240
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005241 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5242 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5243 if (numlock_on) {
5244 wl_list_for_each(seat, &ec->seat_list, link) {
5245 if (seat->keyboard)
5246 weston_keyboard_set_locks(seat->keyboard,
5247 WESTON_NUM_LOCK,
5248 WESTON_NUM_LOCK);
5249 }
5250 }
5251
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005252 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005253
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005254 wl_display_run(display);
5255
Frederic Plourdec336f062014-10-29 14:44:33 -04005256 /* Allow for setting return exit code after
5257 * wl_display_run returns normally. This is
5258 * useful for devs/testers and automated tests
5259 * that want to indicate failure status to
5260 * testing infrastructure above
5261 */
5262 ret = ec->exit_code;
5263
Ryo Munakata03faed22014-09-06 07:32:51 +09005264out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005265 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005266 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005267
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005268 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005269
Daniel Stone855028f2012-05-01 20:37:10 +01005270 weston_compositor_xkb_destroy(ec);
5271
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005272 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305273
5274out_signals:
5275 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5276 if (signals[i])
5277 wl_event_source_remove(signals[i]);
5278
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005279 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005280
Martin Minarik19e6f262012-06-07 13:08:46 +02005281 weston_log_file_close();
5282
Ryo Munakata03faed22014-09-06 07:32:51 +09005283 free(backend);
5284 free(shell);
5285 free(socket_name);
5286 free(option_modules);
5287 free(log);
5288 free(modules);
5289
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005290 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005291}