blob: e060be1f00750d7c624dfb4d653e553feacbe054 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanenc647ed72015-02-09 13:16:57 +02004 * Copyright © 2012-2015 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010059#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040060#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030061#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040066static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067
68static int
69sigchld_handler(int signal_number, void *data)
70{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 int status;
73 pid_t pid;
74
Pekka Paalanen23ade622014-08-27 13:31:26 +030075 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
76 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
Bill Spitzak027b9622012-03-17 15:22:03 -070080
Pekka Paalanen23ade622014-08-27 13:31:26 +030081 if (&p->link == &child_process_list) {
82 weston_log("unknown child process exited\n");
83 continue;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040088 }
89
Pekka Paalanen23ade622014-08-27 13:31:26 +030090 if (pid < 0 && errno != ECHILD)
91 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93 return 1;
94}
95
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096static void
Alexander Larsson0b135062013-05-28 16:23:36 +020097weston_output_transform_scale_init(struct weston_output *output,
98 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020099
Rob Bradford27b17932013-06-26 18:08:46 +0100100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500101weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100102
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600103static void weston_mode_switch_finish(struct weston_output *output,
104 int mode_changed,
105 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800106{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200108 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600110 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200116 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Derek Foremandd4cd332014-11-10 10:29:59 -0600153 if (!mode_changed && !scale_changed)
154 return;
155
Hardening57388e42013-09-18 23:56:36 +0200156 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600157 wl_resource_for_each(resource, &output->resource_list) {
158 if (mode_changed) {
159 wl_output_send_mode(resource,
160 output->current_mode->flags,
161 output->current_mode->width,
162 output->current_mode->height,
163 output->current_mode->refresh);
164 }
Hardening57388e42013-09-18 23:56:36 +0200165
Derek Foreman41bdc272014-11-05 13:26:57 -0600166 version = wl_resource_get_version(resource);
167 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200169
Derek Foreman41bdc272014-11-05 13:26:57 -0600170 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
171 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600172 }
173}
174
175WL_EXPORT int
176weston_output_mode_set_native(struct weston_output *output,
177 struct weston_mode *mode,
178 int32_t scale)
179{
180 int ret;
181 int mode_changed = 0, scale_changed = 0;
182
183 if (!output->switch_mode)
184 return -1;
185
186 if (!output->original_mode) {
187 mode_changed = 1;
188 ret = output->switch_mode(output, mode);
189 if (ret < 0)
190 return ret;
191 if (output->current_scale != scale) {
192 scale_changed = 1;
193 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200194 }
195 }
196
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
202 return 0;
203}
204
205WL_EXPORT int
206weston_output_mode_switch_to_native(struct weston_output *output)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
210
211 if (!output->switch_mode)
212 return -1;
213
214 if (!output->original_mode) {
215 weston_log("already in the native mode\n");
216 return -1;
217 }
218 /* the non fullscreen clients haven't seen a mode set since we
219 * switched into a temporary, so we need to notify them if the
220 * mode at that time is different from the native mode now.
221 */
222 mode_changed = (output->original_mode != output->native_mode);
223 scale_changed = (output->original_scale != output->native_scale);
224
225 ret = output->switch_mode(output, output->native_mode);
226 if (ret < 0)
227 return ret;
228
229 output->current_scale = output->native_scale;
230
231 output->original_mode = NULL;
232 output->original_scale = 0;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
236 return 0;
237}
238
239WL_EXPORT int
240weston_output_mode_switch_to_temporary(struct weston_output *output,
241 struct weston_mode *mode,
242 int32_t scale)
243{
244 int ret;
245
246 if (!output->switch_mode)
247 return -1;
248
249 /* original_mode is the last mode non full screen clients have seen,
250 * so we shouldn't change it if we already have one set.
251 */
252 if (!output->original_mode) {
253 output->original_mode = output->native_mode;
254 output->original_scale = output->native_scale;
255 }
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259
260 output->current_scale = scale;
261
262 weston_mode_switch_finish(output, 0, 0);
263
264 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800265}
266
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400269{
270 wl_list_insert(&child_process_list, &process->link);
271}
272
Benjamin Franzke06286262011-05-06 19:12:33 +0200273static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274child_client_exec(int sockfd, const char *path)
275{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500276 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200278 sigset_t allsigs;
279
280 /* do not give our signal mask to the new process */
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100284 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
285 if (seteuid(getuid()) == -1) {
286 weston_log("compositor: failed seteuid\n");
287 return;
288 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500289
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
291 * non-CLOEXEC fd to pass through exec. */
292 clientfd = dup(sockfd);
293 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500295 return;
296 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 setenv("WAYLAND_SOCKET", s, 1);
300
301 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 path);
304}
305
306WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500307weston_client_launch(struct weston_compositor *compositor,
308 struct weston_process *proc,
309 const char *path,
310 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311{
312 int sv[2];
313 pid_t pid;
314 struct wl_client *client;
315
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300316 weston_log("launching '%s'\n", path);
317
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300318 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320 "socketpair failed while launching '%s': %m\n",
321 path);
322 return NULL;
323 }
324
325 pid = fork();
326 if (pid == -1) {
327 close(sv[0]);
328 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 "fork failed while launching '%s': %m\n", path);
331 return NULL;
332 }
333
334 if (pid == 0) {
335 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700336 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337 }
338
339 close(sv[1]);
340
341 client = wl_client_create(compositor->wl_display, sv[0]);
342 if (!client) {
343 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "wl_client_create failed while launching '%s'.\n",
346 path);
347 return NULL;
348 }
349
350 proc->pid = pid;
351 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200353
354 return client;
355}
356
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300357struct process_info {
358 struct weston_process proc;
359 char *path;
360};
361
362static void
363process_handle_sigchld(struct weston_process *process, int status)
364{
365 struct process_info *pinfo =
366 container_of(process, struct process_info, proc);
367
368 /*
369 * There are no guarantees whether this runs before or after
370 * the wl_client destructor.
371 */
372
373 if (WIFEXITED(status)) {
374 weston_log("%s exited with status %d\n", pinfo->path,
375 WEXITSTATUS(status));
376 } else if (WIFSIGNALED(status)) {
377 weston_log("%s died on signal %d\n", pinfo->path,
378 WTERMSIG(status));
379 } else {
380 weston_log("%s disappeared\n", pinfo->path);
381 }
382
383 free(pinfo->path);
384 free(pinfo);
385}
386
387WL_EXPORT struct wl_client *
388weston_client_start(struct weston_compositor *compositor, const char *path)
389{
390 struct process_info *pinfo;
391 struct wl_client *client;
392
393 pinfo = zalloc(sizeof *pinfo);
394 if (!pinfo)
395 return NULL;
396
397 pinfo->path = strdup(path);
398 if (!pinfo->path)
399 goto out_free;
400
401 client = weston_client_launch(compositor, &pinfo->proc, path,
402 process_handle_sigchld);
403 if (!client)
404 goto out_str;
405
406 return client;
407
408out_str:
409 free(pinfo->path);
410
411out_free:
412 free(pinfo);
413
414 return NULL;
415}
416
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200417static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300418region_init_infinite(pixman_region32_t *region)
419{
420 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
421 UINT32_MAX, UINT32_MAX);
422}
423
Pekka Paalanene67858b2013-04-25 13:57:42 +0300424static struct weston_subsurface *
425weston_surface_to_subsurface(struct weston_surface *surface);
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427WL_EXPORT struct weston_view *
428weston_view_create(struct weston_surface *surface)
429{
430 struct weston_view *view;
431
Bryce Harringtonde16d892014-11-20 22:21:57 -0800432 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 if (view == NULL)
434 return NULL;
435
436 view->surface = surface;
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 /* Assign to surface */
439 wl_list_insert(&surface->views, &view->surface_link);
440
441 wl_signal_init(&view->destroy_signal);
442 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300443 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 pixman_region32_init(&view->clip);
446
447 view->alpha = 1.0;
448 pixman_region32_init(&view->transform.opaque);
449
450 wl_list_init(&view->geometry.transformation_list);
451 wl_list_insert(&view->geometry.transformation_list,
452 &view->transform.position.link);
453 weston_matrix_init(&view->transform.position.matrix);
454 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200455 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500456 pixman_region32_init(&view->transform.boundingbox);
457 view->transform.dirty = 1;
458
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 return view;
460}
461
Jason Ekstrand108865d2014-06-26 10:04:49 -0700462struct weston_frame_callback {
463 struct wl_resource *resource;
464 struct wl_list link;
465};
466
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467struct weston_presentation_feedback {
468 struct wl_resource *resource;
469
470 /* XXX: could use just wl_resource_get_link() instead */
471 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200472
473 /* The per-surface feedback flags */
474 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475};
476
477static void
478weston_presentation_feedback_discard(
479 struct weston_presentation_feedback *feedback)
480{
481 presentation_feedback_send_discarded(feedback->resource);
482 wl_resource_destroy(feedback->resource);
483}
484
485static void
486weston_presentation_feedback_discard_list(struct wl_list *list)
487{
488 struct weston_presentation_feedback *feedback, *tmp;
489
490 wl_list_for_each_safe(feedback, tmp, list, link)
491 weston_presentation_feedback_discard(feedback);
492}
493
494static void
495weston_presentation_feedback_present(
496 struct weston_presentation_feedback *feedback,
497 struct weston_output *output,
498 uint32_t refresh_nsec,
499 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200500 uint64_t seq,
501 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502{
503 struct wl_client *client = wl_resource_get_client(feedback->resource);
504 struct wl_resource *o;
505 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506
507 wl_resource_for_each(o, &output->resource_list) {
508 if (wl_resource_get_client(o) != client)
509 continue;
510
511 presentation_feedback_send_sync_output(feedback->resource, o);
512 }
513
514 secs = ts->tv_sec;
515 presentation_feedback_send_presented(feedback->resource,
516 secs >> 32, secs & 0xffffffff,
517 ts->tv_nsec,
518 refresh_nsec,
519 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200520 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400521 wl_resource_destroy(feedback->resource);
522}
523
524static void
525weston_presentation_feedback_present_list(struct wl_list *list,
526 struct weston_output *output,
527 uint32_t refresh_nsec,
528 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200529 uint64_t seq,
530 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400531{
532 struct weston_presentation_feedback *feedback, *tmp;
533
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200534 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
535 wl_list_empty(list));
536
Pekka Paalanen133e4392014-09-23 22:08:46 -0400537 wl_list_for_each_safe(feedback, tmp, list, link)
538 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200539 refresh_nsec, ts, seq,
540 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400541}
542
Jason Ekstrand7b982072014-05-20 14:33:03 -0500543static void
544surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
545{
546 struct weston_surface_state *state =
547 container_of(listener, struct weston_surface_state,
548 buffer_destroy_listener);
549
550 state->buffer = NULL;
551}
552
553static void
554weston_surface_state_init(struct weston_surface_state *state)
555{
556 state->newly_attached = 0;
557 state->buffer = NULL;
558 state->buffer_destroy_listener.notify =
559 surface_state_handle_buffer_destroy;
560 state->sx = 0;
561 state->sy = 0;
562
563 pixman_region32_init(&state->damage);
564 pixman_region32_init(&state->opaque);
565 region_init_infinite(&state->input);
566
567 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400568 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500569
570 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
571 state->buffer_viewport.buffer.scale = 1;
572 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
573 state->buffer_viewport.surface.width = -1;
574 state->buffer_viewport.changed = 0;
575}
576
577static void
578weston_surface_state_fini(struct weston_surface_state *state)
579{
580 struct weston_frame_callback *cb, *next;
581
582 wl_list_for_each_safe(cb, next,
583 &state->frame_callback_list, link)
584 wl_resource_destroy(cb->resource);
585
Pekka Paalanen133e4392014-09-23 22:08:46 -0400586 weston_presentation_feedback_discard_list(&state->feedback_list);
587
Jason Ekstrand7b982072014-05-20 14:33:03 -0500588 pixman_region32_fini(&state->input);
589 pixman_region32_fini(&state->opaque);
590 pixman_region32_fini(&state->damage);
591
592 if (state->buffer)
593 wl_list_remove(&state->buffer_destroy_listener.link);
594 state->buffer = NULL;
595}
596
597static void
598weston_surface_state_set_buffer(struct weston_surface_state *state,
599 struct weston_buffer *buffer)
600{
601 if (state->buffer == buffer)
602 return;
603
604 if (state->buffer)
605 wl_list_remove(&state->buffer_destroy_listener.link);
606 state->buffer = buffer;
607 if (state->buffer)
608 wl_signal_add(&state->buffer->destroy_signal,
609 &state->buffer_destroy_listener);
610}
611
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500613weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500614{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500615 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400616
Bryce Harringtonde16d892014-11-20 22:21:57 -0800617 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400618 if (surface == NULL)
619 return NULL;
620
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500621 wl_signal_init(&surface->destroy_signal);
622
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500623 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200624 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400625
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200626 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
627 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200628 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
629 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500630
631 weston_surface_state_init(&surface->pending);
632
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400633 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500634 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300635 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400636
Jason Ekstranda7af7042013-10-12 22:38:11 -0500637 wl_list_init(&surface->views);
638
639 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400640 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500641
Pekka Paalanene67858b2013-04-25 13:57:42 +0300642 wl_list_init(&surface->subsurface_list);
643 wl_list_init(&surface->subsurface_list_pending);
644
Jason Ekstrand1e059042014-10-16 10:55:19 -0500645 weston_matrix_init(&surface->buffer_to_surface_matrix);
646 weston_matrix_init(&surface->surface_to_buffer_matrix);
647
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400648 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500649}
650
Alex Wu8811bf92012-02-28 18:07:54 +0800651WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200653 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500654{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100655 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500656}
657
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659weston_view_to_global_float(struct weston_view *view,
660 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200661{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
664
Jason Ekstranda7af7042013-10-12 22:38:11 -0500665 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200666
667 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200668 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700669 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200670 v.f[3]);
671 *x = 0;
672 *y = 0;
673 return;
674 }
675
676 *x = v.f[0] / v.f[3];
677 *y = v.f[1] / v.f[3];
678 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500679 *x = sx + view->geometry.x;
680 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200681 }
682}
683
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500684WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685weston_transformed_coord(int width, int height,
686 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200687 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200688 float sx, float sy, float *bx, float *by)
689{
690 switch (transform) {
691 case WL_OUTPUT_TRANSFORM_NORMAL:
692 default:
693 *bx = sx;
694 *by = sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_FLIPPED:
697 *bx = width - sx;
698 *by = sy;
699 break;
700 case WL_OUTPUT_TRANSFORM_90:
701 *bx = height - sy;
702 *by = sx;
703 break;
704 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
705 *bx = height - sy;
706 *by = width - sx;
707 break;
708 case WL_OUTPUT_TRANSFORM_180:
709 *bx = width - sx;
710 *by = height - sy;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
713 *bx = sx;
714 *by = height - sy;
715 break;
716 case WL_OUTPUT_TRANSFORM_270:
717 *bx = sy;
718 *by = width - sx;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
721 *bx = sy;
722 *by = sx;
723 break;
724 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200725
726 *bx *= scale;
727 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200728}
729
730WL_EXPORT pixman_box32_t
731weston_transformed_rect(int width, int height,
732 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200733 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200734 pixman_box32_t rect)
735{
736 float x1, x2, y1, y2;
737
738 pixman_box32_t ret;
739
Alexander Larsson4ea95522013-05-22 14:41:37 +0200740 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200742 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200743 rect.x2, rect.y2, &x2, &y2);
744
745 if (x1 <= x2) {
746 ret.x1 = x1;
747 ret.x2 = x2;
748 } else {
749 ret.x1 = x2;
750 ret.x2 = x1;
751 }
752
753 if (y1 <= y2) {
754 ret.y1 = y1;
755 ret.y2 = y2;
756 } else {
757 ret.y1 = y2;
758 ret.y2 = y1;
759 }
760
761 return ret;
762}
763
764WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500765weston_transformed_region(int width, int height,
766 enum wl_output_transform transform,
767 int32_t scale,
768 pixman_region32_t *src, pixman_region32_t *dest)
769{
770 pixman_box32_t *src_rects, *dest_rects;
771 int nrects, i;
772
773 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
774 if (src != dest)
775 pixman_region32_copy(dest, src);
776 return;
777 }
778
779 src_rects = pixman_region32_rectangles(src, &nrects);
780 dest_rects = malloc(nrects * sizeof(*dest_rects));
781 if (!dest_rects)
782 return;
783
784 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
785 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
786 } else {
787 for (i = 0; i < nrects; i++) {
788 switch (transform) {
789 default:
790 case WL_OUTPUT_TRANSFORM_NORMAL:
791 dest_rects[i].x1 = src_rects[i].x1;
792 dest_rects[i].y1 = src_rects[i].y1;
793 dest_rects[i].x2 = src_rects[i].x2;
794 dest_rects[i].y2 = src_rects[i].y2;
795 break;
796 case WL_OUTPUT_TRANSFORM_90:
797 dest_rects[i].x1 = height - src_rects[i].y2;
798 dest_rects[i].y1 = src_rects[i].x1;
799 dest_rects[i].x2 = height - src_rects[i].y1;
800 dest_rects[i].y2 = src_rects[i].x2;
801 break;
802 case WL_OUTPUT_TRANSFORM_180:
803 dest_rects[i].x1 = width - src_rects[i].x2;
804 dest_rects[i].y1 = height - src_rects[i].y2;
805 dest_rects[i].x2 = width - src_rects[i].x1;
806 dest_rects[i].y2 = height - src_rects[i].y1;
807 break;
808 case WL_OUTPUT_TRANSFORM_270:
809 dest_rects[i].x1 = src_rects[i].y1;
810 dest_rects[i].y1 = width - src_rects[i].x2;
811 dest_rects[i].x2 = src_rects[i].y2;
812 dest_rects[i].y2 = width - src_rects[i].x1;
813 break;
814 case WL_OUTPUT_TRANSFORM_FLIPPED:
815 dest_rects[i].x1 = width - src_rects[i].x2;
816 dest_rects[i].y1 = src_rects[i].y1;
817 dest_rects[i].x2 = width - src_rects[i].x1;
818 dest_rects[i].y2 = src_rects[i].y2;
819 break;
820 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
821 dest_rects[i].x1 = height - src_rects[i].y2;
822 dest_rects[i].y1 = width - src_rects[i].x2;
823 dest_rects[i].x2 = height - src_rects[i].y1;
824 dest_rects[i].y2 = width - src_rects[i].x1;
825 break;
826 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = height - src_rects[i].y2;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = height - src_rects[i].y1;
831 break;
832 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = src_rects[i].x1;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = src_rects[i].x2;
837 break;
838 }
839 }
840 }
841
842 if (scale != 1) {
843 for (i = 0; i < nrects; i++) {
844 dest_rects[i].x1 *= scale;
845 dest_rects[i].x2 *= scale;
846 dest_rects[i].y1 *= scale;
847 dest_rects[i].y2 *= scale;
848 }
849 }
850
851 pixman_region32_clear(dest);
852 pixman_region32_init_rects(dest, dest_rects, nrects);
853 free(dest_rects);
854}
855
Jonny Lamb74130762013-11-26 18:19:46 +0100856static void
857scaler_surface_to_buffer(struct weston_surface *surface,
858 float sx, float sy, float *bx, float *by)
859{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 double src_width, src_height;
862 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200863
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200864 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
865 if (vp->surface.width == -1) {
866 *bx = sx;
867 *by = sy;
868 return;
869 }
Jonny Lamb74130762013-11-26 18:19:46 +0100870
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200871 src_x = 0.0;
872 src_y = 0.0;
873 src_width = surface->width_from_buffer;
874 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100875 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200876 src_x = wl_fixed_to_double(vp->buffer.src_x);
877 src_y = wl_fixed_to_double(vp->buffer.src_y);
878 src_width = wl_fixed_to_double(vp->buffer.src_width);
879 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100880 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200881
882 *bx = sx * src_width / surface->width + src_x;
883 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100884}
885
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500886WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200887weston_surface_to_buffer_float(struct weston_surface *surface,
888 float sx, float sy, float *bx, float *by)
889{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200890 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892 /* first transform coordinates if the scaler is set */
893 scaler_surface_to_buffer(surface, sx, sy, bx, by);
894
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500895 weston_transformed_coord(surface->width_from_buffer,
896 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200897 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100898 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899}
900
Alexander Larsson4ea95522013-05-22 14:41:37 +0200901WL_EXPORT void
902weston_surface_to_buffer(struct weston_surface *surface,
903 int sx, int sy, int *bx, int *by)
904{
905 float bxf, byf;
906
Jonny Lamb74130762013-11-26 18:19:46 +0100907 weston_surface_to_buffer_float(surface,
908 sx, sy, &bxf, &byf);
909
Alexander Larsson4ea95522013-05-22 14:41:37 +0200910 *bx = floorf(bxf);
911 *by = floorf(byf);
912}
913
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200914WL_EXPORT pixman_box32_t
915weston_surface_to_buffer_rect(struct weston_surface *surface,
916 pixman_box32_t rect)
917{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200918 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100919 float xf, yf;
920
921 /* first transform box coordinates if the scaler is set */
922 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
923 rect.x1 = floorf(xf);
924 rect.y1 = floorf(yf);
925
926 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
927 rect.x2 = floorf(xf);
928 rect.y2 = floorf(yf);
929
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500930 return weston_transformed_rect(surface->width_from_buffer,
931 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200932 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200933 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200934}
935
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200936/** Transform a region from surface coordinates to buffer coordinates
937 *
938 * \param surface The surface to fetch wl_viewport and buffer transformation
939 * from.
940 * \param surface_region[in] The region in surface coordinates.
941 * \param buffer_region[out] The region converted to buffer coordinates.
942 *
943 * Buffer_region must be init'd, but will be completely overwritten.
944 *
945 * Viewport and buffer transformations can only do translation, scaling,
946 * and rotations in 90-degree steps. Therefore the only loss in the
947 * conversion is coordinate flooring (rounding).
948 */
949WL_EXPORT void
950weston_surface_to_buffer_region(struct weston_surface *surface,
951 pixman_region32_t *surface_region,
952 pixman_region32_t *buffer_region)
953{
954 pixman_box32_t *src_rects, *dest_rects;
955 int nrects, i;
956
957 src_rects = pixman_region32_rectangles(surface_region, &nrects);
958 dest_rects = malloc(nrects * sizeof(*dest_rects));
959 if (!dest_rects)
960 return;
961
962 for (i = 0; i < nrects; i++) {
963 dest_rects[i] = weston_surface_to_buffer_rect(surface,
964 src_rects[i]);
965 }
966
967 pixman_region32_fini(buffer_region);
968 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
969 free(dest_rects);
970}
971
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200972WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400974 struct weston_plane *plane)
975{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400977 return;
978
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 weston_view_damage_below(view);
980 view->plane = plane;
981 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400982}
983
Pekka Paalanen51723d52015-02-17 13:10:01 +0200984/** Inflict damage on the plane where the view is visible.
985 *
986 * \param view The view that causes the damage.
987 *
988 * If the view is currently on a plane (including the primary plane),
989 * take the view's boundingbox, subtract all the opaque views that cover it,
990 * and add the remaining region as damage to the plane. This corresponds
991 * to the damage inflicted to the plane if this view disappeared.
992 *
993 * A repaint is scheduled for this view.
994 *
995 * The region of all opaque views covering this view is stored in
996 * weston_view::clip and updated by view_accumulate_damage() during
997 * weston_output_repaint(). Specifically, that region matches the
998 * scenegraph as it was last painted.
999 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001000WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001002{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001003 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001004
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001005 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001006 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001008 if (view->plane)
1009 pixman_region32_union(&view->plane->damage,
1010 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001011 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001012 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001013}
1014
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001015static void
1016weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1017{
1018 uint32_t different = es->output_mask ^ mask;
1019 uint32_t entered = mask & different;
1020 uint32_t left = es->output_mask & different;
1021 struct weston_output *output;
1022 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001023 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024
1025 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001026 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001027 return;
1028 if (different == 0)
1029 return;
1030
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001031 client = wl_resource_get_client(es->resource);
1032
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001033 wl_list_for_each(output, &es->compositor->output_list, link) {
1034 if (1 << output->id & different)
1035 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001036 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037 client);
1038 if (resource == NULL)
1039 continue;
1040 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001041 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001042 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001043 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001044 }
1045}
1046
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001047
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048static void
1049weston_surface_assign_output(struct weston_surface *es)
1050{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 struct weston_output *new_output;
1052 struct weston_view *view;
1053 pixman_region32_t region;
1054 uint32_t max, area, mask;
1055 pixman_box32_t *e;
1056
1057 new_output = NULL;
1058 max = 0;
1059 mask = 0;
1060 pixman_region32_init(&region);
1061 wl_list_for_each(view, &es->views, surface_link) {
1062 if (!view->output)
1063 continue;
1064
1065 pixman_region32_intersect(&region, &view->transform.boundingbox,
1066 &view->output->region);
1067
1068 e = pixman_region32_extents(&region);
1069 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1070
1071 mask |= view->output_mask;
1072
1073 if (area >= max) {
1074 new_output = view->output;
1075 max = area;
1076 }
1077 }
1078 pixman_region32_fini(&region);
1079
1080 es->output = new_output;
1081 weston_surface_update_output_mask(es, mask);
1082}
1083
1084static void
1085weston_view_assign_output(struct weston_view *ev)
1086{
1087 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001088 struct weston_output *output, *new_output;
1089 pixman_region32_t region;
1090 uint32_t max, area, mask;
1091 pixman_box32_t *e;
1092
1093 new_output = NULL;
1094 max = 0;
1095 mask = 0;
1096 pixman_region32_init(&region);
1097 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001099 &output->region);
1100
1101 e = pixman_region32_extents(&region);
1102 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1103
1104 if (area > 0)
1105 mask |= 1 << output->id;
1106
1107 if (area >= max) {
1108 new_output = output;
1109 max = area;
1110 }
1111 }
1112 pixman_region32_fini(&region);
1113
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 ev->output = new_output;
1115 ev->output_mask = mask;
1116
1117 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001118}
1119
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001120static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001121weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1122 int from_x, int from_y, int *to_x, int *to_y)
1123{
1124 float x, y;
1125
1126 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1127 weston_view_from_global_float(to, x, y, &x, &y);
1128
1129 *to_x = round(x);
1130 *to_y = round(y);
1131}
1132
1133static void
1134weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1135{
1136 pixman_box32_t *a;
1137 pixman_box32_t b;
1138
1139 a = pixman_region32_extents(&from->geometry.scissor);
1140
1141 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1142 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1143
1144 pixman_region32_fini(&to->geometry.scissor);
1145 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1146}
1147
1148static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001149view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001151{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001152 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1153 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001154 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001155 { inbox->x1, inbox->y1 },
1156 { inbox->x1, inbox->y2 },
1157 { inbox->x2, inbox->y1 },
1158 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001159 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001160 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001161 int i;
1162
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001163 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001164 /* avoid rounding empty bbox to 1x1 */
1165 pixman_region32_init(bbox);
1166 return;
1167 }
1168
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001169 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001170 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001172 if (x < min_x)
1173 min_x = x;
1174 if (x > max_x)
1175 max_x = x;
1176 if (y < min_y)
1177 min_y = y;
1178 if (y > max_y)
1179 max_y = y;
1180 }
1181
Pekka Paalanen219b9822012-02-08 15:38:37 +02001182 int_x = floorf(min_x);
1183 int_y = floorf(min_y);
1184 pixman_region32_init_rect(bbox, int_x, int_y,
1185 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001186}
1187
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001192
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001193 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 view->geometry.x = roundf(view->geometry.x);
1195 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001196
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001197 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1199 view->transform.position.matrix.d[12] = view->geometry.x;
1200 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 view->transform.inverse = view->transform.position.matrix;
1205 view->transform.inverse.d[12] = -view->geometry.x;
1206 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001207
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001209 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001210 view->surface->width,
1211 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001212 if (view->geometry.scissor_enabled)
1213 pixman_region32_intersect(&view->transform.boundingbox,
1214 &view->transform.boundingbox,
1215 &view->geometry.scissor);
1216
1217 pixman_region32_translate(&view->transform.boundingbox,
1218 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001219
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 if (view->alpha == 1.0) {
1221 pixman_region32_copy(&view->transform.opaque,
1222 &view->surface->opaque);
1223 pixman_region32_translate(&view->transform.opaque,
1224 view->geometry.x,
1225 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001226 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001227}
1228
1229static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001231{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 struct weston_view *parent = view->geometry.parent;
1233 struct weston_matrix *matrix = &view->transform.matrix;
1234 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001235 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001236 pixman_region32_t surfregion;
1237 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001240
1241 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1243 view->transform.position.matrix.d[12] = view->geometry.x;
1244 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001245
1246 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001248 weston_matrix_multiply(matrix, &tform->matrix);
1249
Pekka Paalanen483243f2013-03-08 14:56:50 +02001250 if (parent)
1251 weston_matrix_multiply(matrix, &parent->transform.matrix);
1252
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001253 if (weston_matrix_invert(inverse, matrix) < 0) {
1254 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 weston_log("error: weston_view %p"
1256 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001257 return -1;
1258 }
1259
Pekka Paalanen380adf52015-02-16 14:39:11 +02001260 pixman_region32_init_rect(&surfregion, 0, 0,
1261 view->surface->width, view->surface->height);
1262 if (view->geometry.scissor_enabled)
1263 pixman_region32_intersect(&surfregion, &surfregion,
1264 &view->geometry.scissor);
1265 surfbox = pixman_region32_extents(&surfregion);
1266
1267 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1268 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001269
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270 return 0;
1271}
1272
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001273static struct weston_layer *
1274get_view_layer(struct weston_view *view)
1275{
1276 if (view->parent_view)
1277 return get_view_layer(view->parent_view);
1278 return view->layer_link.layer;
1279}
1280
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001281WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001283{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001285 struct weston_layer *layer;
1286 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001289 return;
1290
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 pixman_region32_fini(&view->transform.boundingbox);
1299 pixman_region32_fini(&view->transform.opaque);
1300 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001301
Pekka Paalanencd403622012-01-25 13:37:39 +02001302 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303 if (view->geometry.transformation_list.next ==
1304 &view->transform.position.link &&
1305 view->geometry.transformation_list.prev ==
1306 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001307 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001309 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 if (weston_view_update_transform_enable(view) < 0)
1311 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001312 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001313
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001314 layer = get_view_layer(view);
1315 if (layer) {
1316 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001317 pixman_region32_intersect(&view->transform.boundingbox,
1318 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001319 pixman_region32_intersect(&view->transform.opaque,
1320 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001321 pixman_region32_fini(&mask);
1322 }
1323
Pekka Paalanen380adf52015-02-16 14:39:11 +02001324 if (parent) {
1325 if (parent->geometry.scissor_enabled) {
1326 view->geometry.scissor_enabled = true;
1327 weston_view_transfer_scissor(parent, view);
1328 } else {
1329 view->geometry.scissor_enabled = false;
1330 }
1331 }
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001334
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001336
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 wl_signal_emit(&view->surface->compositor->transform_signal,
1338 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001339}
1340
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001341WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001343{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001344 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001345
1346 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 * The invariant: if view->geometry.dirty, then all views
1348 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001349 * Corollary: if not parent->geometry.dirty, then all ancestors
1350 * are not dirty.
1351 */
1352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001354 return;
1355
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001361}
1362
1363WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364weston_view_to_global_fixed(struct weston_view *view,
1365 wl_fixed_t vx, wl_fixed_t vy,
1366 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001367{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001368 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001369
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 weston_view_to_global_float(view,
1371 wl_fixed_to_double(vx),
1372 wl_fixed_to_double(vy),
1373 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001374 *x = wl_fixed_from_double(xf);
1375 *y = wl_fixed_from_double(yf);
1376}
1377
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001378WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379weston_view_from_global_float(struct weston_view *view,
1380 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001381{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001383 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001386
1387 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001388 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001390 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 *vx = 0;
1392 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001393 return;
1394 }
1395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 *vx = v.f[0] / v.f[3];
1397 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001398 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 *vx = x - view->geometry.x;
1400 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001401 }
1402}
1403
1404WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405weston_view_from_global_fixed(struct weston_view *view,
1406 wl_fixed_t x, wl_fixed_t y,
1407 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001408{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 weston_view_from_global_float(view,
1412 wl_fixed_to_double(x),
1413 wl_fixed_to_double(y),
1414 &vxf, &vyf);
1415 *vx = wl_fixed_from_double(vxf);
1416 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001417}
1418
1419WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420weston_view_from_global(struct weston_view *view,
1421 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001422{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001424
Jason Ekstranda7af7042013-10-12 22:38:11 -05001425 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1426 *vx = floorf(vxf);
1427 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001428}
1429
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001430WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001431weston_surface_schedule_repaint(struct weston_surface *surface)
1432{
1433 struct weston_output *output;
1434
1435 wl_list_for_each(output, &surface->compositor->output_list, link)
1436 if (surface->output_mask & (1 << output->id))
1437 weston_output_schedule_repaint(output);
1438}
1439
1440WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441weston_view_schedule_repaint(struct weston_view *view)
1442{
1443 struct weston_output *output;
1444
1445 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1446 if (view->output_mask & (1 << output->id))
1447 weston_output_schedule_repaint(output);
1448}
1449
Pekka Paalanene508ce62015-02-19 13:59:55 +02001450/**
1451 * XXX: This function does it the wrong way.
1452 * surface->damage is the damage from the client, and causes
1453 * surface_flush_damage() to copy pixels. No window management action can
1454 * cause damage to the client-provided content, warranting re-upload!
1455 *
1456 * Instead of surface->damage, this function should record the damage
1457 * with all the views for this surface to avoid extraneous texture
1458 * uploads.
1459 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001460WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001461weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001462{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001463 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 0, 0, surface->width,
1465 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001466
Kristian Høgsberg98238702012-08-03 16:29:12 -04001467 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001468}
1469
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001470WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001472{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001473 if (view->geometry.x == x && view->geometry.y == y)
1474 return;
1475
Jason Ekstranda7af7042013-10-12 22:38:11 -05001476 view->geometry.x = x;
1477 view->geometry.y = y;
1478 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001479}
1480
Pekka Paalanen483243f2013-03-08 14:56:50 +02001481static void
1482transform_parent_handle_parent_destroy(struct wl_listener *listener,
1483 void *data)
1484{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001485 struct weston_view *view =
1486 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001487 geometry.parent_destroy_listener);
1488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001490}
1491
1492WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001494 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001495{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 if (view->geometry.parent) {
1497 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1498 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001499
1500 if (!parent)
1501 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001502 }
1503
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001507 transform_parent_handle_parent_destroy;
1508 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001509 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001511 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001513 }
1514
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 weston_view_geometry_dirty(view);
1516}
1517
Pekka Paalanen380adf52015-02-16 14:39:11 +02001518/** Set a clip mask rectangle on a view
1519 *
1520 * \param view The view to set the clip mask on.
1521 * \param x Top-left corner X coordinate of the clip rectangle.
1522 * \param y Top-left corner Y coordinate of the clip rectangle.
1523 * \param width Width of the clip rectangle, non-negative.
1524 * \param height Height of the clip rectangle, non-negative.
1525 *
1526 * A shell may set a clip mask rectangle on a view. Everything outside
1527 * the rectangle is cut away for input and output purposes: it is
1528 * not drawn and cannot be hit by hit-test based input like pointer
1529 * motion or touch-downs. Everything inside the rectangle will behave
1530 * normally. Clients are unaware of clipping.
1531 *
1532 * The rectangle is set in the surface local coordinates. Setting a clip
1533 * mask rectangle does not affect the view position, the view is positioned
1534 * as it would be without a clip. The clip also does not change
1535 * weston_surface::width,height.
1536 *
1537 * The clip mask rectangle is part of transformation inheritance
1538 * (weston_view_set_transform_parent()). A clip set in the root of the
1539 * transformation inheritance tree will affect all views in the tree.
1540 * A clip can be set only on the root view. Attempting to set a clip
1541 * on view that has a transformation parent will fail. Assigning a parent
1542 * to a view that has a clip set will cause the clip to be forgotten.
1543 *
1544 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1545 * on the additional transformations in the child views. These transformations
1546 * may not rotate the coordinate axes, i.e., only translation and scaling
1547 * are allowed. Violating this restriction causes the clipping to malfunction.
1548 * Furthermore, using scaling may cause rounding errors in child clipping.
1549 *
1550 * The clip mask rectangle is not automatically adjusted based on
1551 * wl_surface.attach dx and dy arguments.
1552 *
1553 * A clip mask rectangle can be set only if the compositor capability
1554 * WESTON_CAP_VIEW_CLIP_MASK is present.
1555 *
1556 * This function sets the clip mask rectangle and schedules a repaint for
1557 * the view.
1558 */
1559WL_EXPORT void
1560weston_view_set_mask(struct weston_view *view,
1561 int x, int y, int width, int height)
1562{
1563 struct weston_compositor *compositor = view->surface->compositor;
1564
1565 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1566 weston_log("%s not allowed without capability!\n", __func__);
1567 return;
1568 }
1569
1570 if (view->geometry.parent) {
1571 weston_log("view %p has a parent, clip forbidden!\n", view);
1572 return;
1573 }
1574
1575 if (width < 0 || height < 0) {
1576 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1577 x, y, width, height);
1578 return;
1579 }
1580
1581 pixman_region32_fini(&view->geometry.scissor);
1582 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1583 view->geometry.scissor_enabled = true;
1584 weston_view_geometry_dirty(view);
1585 weston_view_schedule_repaint(view);
1586}
1587
1588/** Remove the clip mask from a view
1589 *
1590 * \param view The view to remove the clip mask from.
1591 *
1592 * Removed the clip mask rectangle and schedules a repaint.
1593 *
1594 * \sa weston_view_set_mask
1595 */
1596WL_EXPORT void
1597weston_view_set_mask_infinite(struct weston_view *view)
1598{
1599 view->geometry.scissor_enabled = false;
1600 weston_view_geometry_dirty(view);
1601 weston_view_schedule_repaint(view);
1602}
1603
Derek Foreman280e7dd2014-10-03 13:13:42 -05001604WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605weston_view_is_mapped(struct weston_view *view)
1606{
1607 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001608 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001610 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001611}
1612
Derek Foreman280e7dd2014-10-03 13:13:42 -05001613WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001614weston_surface_is_mapped(struct weston_surface *surface)
1615{
1616 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001617 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001618 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001619 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001620}
1621
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001622static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001623surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001624{
1625 struct weston_view *view;
1626
1627 if (surface->width == width && surface->height == height)
1628 return;
1629
1630 surface->width = width;
1631 surface->height = height;
1632
1633 wl_list_for_each(view, &surface->views, surface_link)
1634 weston_view_geometry_dirty(view);
1635}
1636
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001637WL_EXPORT void
1638weston_surface_set_size(struct weston_surface *surface,
1639 int32_t width, int32_t height)
1640{
1641 assert(!surface->resource);
1642 surface_set_size(surface, width, height);
1643}
1644
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001645static int
1646fixed_round_up_to_int(wl_fixed_t f)
1647{
1648 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1649}
1650
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001651static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001652weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001653{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001654 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001655 int32_t width, height;
1656
1657 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001658 surface->width_from_buffer = 0;
1659 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001660 return;
1661 }
1662
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001663 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001664 case WL_OUTPUT_TRANSFORM_90:
1665 case WL_OUTPUT_TRANSFORM_270:
1666 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1667 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001668 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1669 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001670 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001671 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001672 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1673 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001674 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001675 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001676
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001677 surface->width_from_buffer = width;
1678 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001679}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001680
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001681static void
1682weston_surface_update_size(struct weston_surface *surface)
1683{
1684 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1685 int32_t width, height;
1686
1687 width = surface->width_from_buffer;
1688 height = surface->height_from_buffer;
1689
1690 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001691 surface_set_size(surface,
1692 vp->surface.width, vp->surface.height);
1693 return;
1694 }
1695
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001696 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001697 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1698 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1699
1700 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001701 return;
1702 }
1703
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001704 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001705}
1706
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001707WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001709{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001710 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001711
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001712 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001713
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001714 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001715}
1716
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717WL_EXPORT struct weston_view *
1718weston_compositor_pick_view(struct weston_compositor *compositor,
1719 wl_fixed_t x, wl_fixed_t y,
1720 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001721{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001722 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001723 wl_fixed_t view_x, view_y;
1724 int view_ix, view_iy;
1725 int ix = wl_fixed_to_int(x);
1726 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001727
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001729 if (!pixman_region32_contains_point(
1730 &view->transform.boundingbox, ix, iy, NULL))
1731 continue;
1732
1733 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1734 view_ix = wl_fixed_to_int(view_x);
1735 view_iy = wl_fixed_to_int(view_y);
1736
1737 if (!pixman_region32_contains_point(&view->surface->input,
1738 view_ix, view_iy, NULL))
1739 continue;
1740
Pekka Paalanen380adf52015-02-16 14:39:11 +02001741 if (view->geometry.scissor_enabled &&
1742 !pixman_region32_contains_point(&view->geometry.scissor,
1743 view_ix, view_iy, NULL))
1744 continue;
1745
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001746 *vx = view_x;
1747 *vy = view_y;
1748 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001749 }
1750
1751 return NULL;
1752}
1753
1754static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001755weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001756{
Daniel Stone37816df2012-05-16 18:45:18 +01001757 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001758
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001759 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001760 return;
1761
Daniel Stone37816df2012-05-16 18:45:18 +01001762 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001763 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001764}
1765
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001766WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001768{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001769 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001770
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 if (!weston_view_is_mapped(view))
1772 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001773
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 weston_view_damage_below(view);
1775 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001776 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001777 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 wl_list_remove(&view->link);
1779 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 view->output_mask = 0;
1781 weston_surface_assign_output(view->surface);
1782
1783 if (weston_surface_is_mapped(view->surface))
1784 return;
1785
1786 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1787 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001788 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001790 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001791 NULL,
1792 wl_fixed_from_int(0),
1793 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001795 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001796 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001798
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799WL_EXPORT void
1800weston_surface_unmap(struct weston_surface *surface)
1801{
1802 struct weston_view *view;
1803
1804 wl_list_for_each(view, &surface->views, surface_link)
1805 weston_view_unmap(view);
1806 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001807}
1808
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001809static void
1810weston_surface_reset_pending_buffer(struct weston_surface *surface)
1811{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001812 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001813 surface->pending.sx = 0;
1814 surface->pending.sy = 0;
1815 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001816 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001817}
1818
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819WL_EXPORT void
1820weston_view_destroy(struct weston_view *view)
1821{
1822 wl_signal_emit(&view->destroy_signal, view);
1823
1824 assert(wl_list_empty(&view->geometry.child_list));
1825
1826 if (weston_view_is_mapped(view)) {
1827 weston_view_unmap(view);
1828 weston_compositor_build_view_list(view->surface->compositor);
1829 }
1830
1831 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001832 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833
1834 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001835 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001837 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838
1839 weston_view_set_transform_parent(view, NULL);
1840
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841 wl_list_remove(&view->surface_link);
1842
1843 free(view);
1844}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001845
1846WL_EXPORT void
1847weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001848{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001849 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001851
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001852 if (--surface->ref_count > 0)
1853 return;
1854
1855 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1856
Pekka Paalanene67858b2013-04-25 13:57:42 +03001857 assert(wl_list_empty(&surface->subsurface_list_pending));
1858 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001859
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1861 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001862
Jason Ekstrand7b982072014-05-20 14:33:03 -05001863 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001864
Pekka Paalanende685b82012-12-04 15:58:12 +02001865 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001866
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001867 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001868 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001869 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001870
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001871 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001872 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001873
Pekka Paalanen133e4392014-09-23 22:08:46 -04001874 weston_presentation_feedback_discard_list(&surface->feedback_list);
1875
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001876 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001877}
1878
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001879static void
1880destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001881{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001882 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001883
Giulio Camuffo0d379742013-11-15 22:06:15 +01001884 /* Set the resource to NULL, since we don't want to leave a
1885 * dangling pointer if the surface was refcounted and survives
1886 * the weston_surface_destroy() call. */
1887 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001888 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001889}
1890
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001891static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001892weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1893{
1894 struct weston_buffer *buffer =
1895 container_of(listener, struct weston_buffer, destroy_listener);
1896
1897 wl_signal_emit(&buffer->destroy_signal, buffer);
1898 free(buffer);
1899}
1900
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001901WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902weston_buffer_from_resource(struct wl_resource *resource)
1903{
1904 struct weston_buffer *buffer;
1905 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001906
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001907 listener = wl_resource_get_destroy_listener(resource,
1908 weston_buffer_destroy_handler);
1909
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001910 if (listener)
1911 return container_of(listener, struct weston_buffer,
1912 destroy_listener);
1913
1914 buffer = zalloc(sizeof *buffer);
1915 if (buffer == NULL)
1916 return NULL;
1917
1918 buffer->resource = resource;
1919 wl_signal_init(&buffer->destroy_signal);
1920 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001921 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001922 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001923
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001924 return buffer;
1925}
1926
1927static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001928weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1929 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001930{
Pekka Paalanende685b82012-12-04 15:58:12 +02001931 struct weston_buffer_reference *ref =
1932 container_of(listener, struct weston_buffer_reference,
1933 destroy_listener);
1934
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001935 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001936 ref->buffer = NULL;
1937}
1938
1939WL_EXPORT void
1940weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001941 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001942{
1943 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001944 ref->buffer->busy_count--;
1945 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001946 assert(wl_resource_get_client(ref->buffer->resource));
1947 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001948 WL_BUFFER_RELEASE);
1949 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001950 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001951 }
1952
Pekka Paalanende685b82012-12-04 15:58:12 +02001953 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001954 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001955 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001956 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001957 }
1958
Pekka Paalanende685b82012-12-04 15:58:12 +02001959 ref->buffer = buffer;
1960 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1961}
1962
1963static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001964weston_surface_attach(struct weston_surface *surface,
1965 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001966{
1967 weston_buffer_reference(&surface->buffer_ref, buffer);
1968
Pekka Paalanena6421c42012-12-04 15:58:10 +02001969 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001970 if (weston_surface_is_mapped(surface))
1971 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001972 }
1973
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001974 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001975
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001976 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001977 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001978}
1979
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001980WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001981weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001982{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001983 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001984
1985 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001986 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001987}
1988
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001989WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001990weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001991{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001992 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001993
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001994 pixman_region32_union(&compositor->primary_plane.damage,
1995 &compositor->primary_plane.damage,
1996 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001997 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001998}
1999
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002000static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002002{
Pekka Paalanende685b82012-12-04 15:58:12 +02002003 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002004 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002005 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002006
Pekka Paalanenb5026542014-11-12 15:09:24 +02002007 if (weston_timeline_enabled_ &&
2008 pixman_region32_not_empty(&surface->damage))
2009 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2010 TLP_OUTPUT(surface->output), TLP_END);
2011
Jason Ekstrandef540082014-06-26 10:37:36 -07002012 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013}
2014
2015static void
2016view_accumulate_damage(struct weston_view *view,
2017 pixman_region32_t *opaque)
2018{
2019 pixman_region32_t damage;
2020
2021 pixman_region32_init(&damage);
2022 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002023 pixman_box32_t *extents;
2024
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002026 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002027 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002028 pixman_region32_copy(&damage, &view->surface->damage);
2029 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002030 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002031 }
2032
Pekka Paalanen380adf52015-02-16 14:39:11 +02002033 pixman_region32_intersect(&damage, &damage,
2034 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 pixman_region32_subtract(&damage, &damage, opaque);
2036 pixman_region32_union(&view->plane->damage,
2037 &view->plane->damage, &damage);
2038 pixman_region32_fini(&damage);
2039 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002040 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002041}
2042
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002043static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002044compositor_accumulate_damage(struct weston_compositor *ec)
2045{
2046 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002048 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002049
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002050 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051
2052 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002053 pixman_region32_copy(&plane->clip, &clip);
2054
2055 pixman_region32_init(&opaque);
2056
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 wl_list_for_each(ev, &ec->view_list, link) {
2058 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002059 continue;
2060
Jason Ekstranda7af7042013-10-12 22:38:11 -05002061 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002062 }
2063
2064 pixman_region32_union(&clip, &clip, &opaque);
2065 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002066 }
2067
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002068 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002069
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 wl_list_for_each(ev, &ec->view_list, link)
2071 ev->surface->touched = 0;
2072
2073 wl_list_for_each(ev, &ec->view_list, link) {
2074 if (ev->surface->touched)
2075 continue;
2076 ev->surface->touched = 1;
2077
2078 surface_flush_damage(ev->surface);
2079
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002080 /* Both the renderer and the backend have seen the buffer
2081 * by now. If renderer needs the buffer, it has its own
2082 * reference set. If the backend wants to keep the buffer
2083 * around for migrating the surface into a non-primary plane
2084 * later, keep_buffer is true. Otherwise, drop the core
2085 * reference now, and allow early buffer release. This enables
2086 * clients to use single-buffering.
2087 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002088 if (!ev->surface->keep_buffer)
2089 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002090 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002091}
2092
2093static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002094surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002095{
2096 struct weston_subsurface *sub;
2097
Pekka Paalanene67858b2013-04-25 13:57:42 +03002098 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002099 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002100 continue;
2101
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2103 wl_list_init(&sub->surface->views);
2104
2105 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002106 }
2107}
2108
2109static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002111{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002112 struct weston_subsurface *sub;
2113 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002114
Jason Ekstranda7af7042013-10-12 22:38:11 -05002115 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2116 if (sub->surface == surface)
2117 continue;
2118
George Kiagiadakised04d382014-06-13 18:10:26 +02002119 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2120 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002122 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002123
2124 surface_free_unused_subsurface_views(sub->surface);
2125 }
2126}
2127
2128static void
2129view_list_add_subsurface_view(struct weston_compositor *compositor,
2130 struct weston_subsurface *sub,
2131 struct weston_view *parent)
2132{
2133 struct weston_subsurface *child;
2134 struct weston_view *view = NULL, *iv;
2135
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002136 if (!weston_surface_is_mapped(sub->surface))
2137 return;
2138
Jason Ekstranda7af7042013-10-12 22:38:11 -05002139 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2140 if (iv->geometry.parent == parent) {
2141 view = iv;
2142 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002143 }
2144 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145
2146 if (view) {
2147 /* Put it back in the surface's list of views */
2148 wl_list_remove(&view->surface_link);
2149 wl_list_insert(&sub->surface->views, &view->surface_link);
2150 } else {
2151 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002152 weston_view_set_position(view,
2153 sub->position.x,
2154 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002155 weston_view_set_transform_parent(view, parent);
2156 }
2157
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002158 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002160
Pekka Paalanenb188e912013-11-19 14:03:35 +02002161 if (wl_list_empty(&sub->surface->subsurface_list)) {
2162 wl_list_insert(compositor->view_list.prev, &view->link);
2163 return;
2164 }
2165
2166 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2167 if (child->surface == sub->surface)
2168 wl_list_insert(compositor->view_list.prev, &view->link);
2169 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002170 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002171 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002172}
2173
2174static void
2175view_list_add(struct weston_compositor *compositor,
2176 struct weston_view *view)
2177{
2178 struct weston_subsurface *sub;
2179
2180 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181
Pekka Paalanenb188e912013-11-19 14:03:35 +02002182 if (wl_list_empty(&view->surface->subsurface_list)) {
2183 wl_list_insert(compositor->view_list.prev, &view->link);
2184 return;
2185 }
2186
2187 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2188 if (sub->surface == view->surface)
2189 wl_list_insert(compositor->view_list.prev, &view->link);
2190 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002191 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002192 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002193}
2194
2195static void
2196weston_compositor_build_view_list(struct weston_compositor *compositor)
2197{
2198 struct weston_view *view;
2199 struct weston_layer *layer;
2200
2201 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002202 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 surface_stash_subsurface_views(view->surface);
2204
2205 wl_list_init(&compositor->view_list);
2206 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002207 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208 view_list_add(compositor, view);
2209 }
2210 }
2211
2212 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002213 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002215}
2216
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002217static void
2218weston_output_take_feedback_list(struct weston_output *output,
2219 struct weston_surface *surface)
2220{
2221 struct weston_view *view;
2222 struct weston_presentation_feedback *feedback;
2223 uint32_t flags = 0xffffffff;
2224
2225 if (wl_list_empty(&surface->feedback_list))
2226 return;
2227
2228 /* All views must have the flag for the flag to survive. */
2229 wl_list_for_each(view, &surface->views, surface_link) {
2230 /* ignore views that are not on this output at all */
2231 if (view->output_mask & (1u << output->id))
2232 flags &= view->psf_flags;
2233 }
2234
2235 wl_list_for_each(feedback, &surface->feedback_list, link)
2236 feedback->psf_flags = flags;
2237
2238 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2239 wl_list_init(&surface->feedback_list);
2240}
2241
David Herrmann1edf44c2013-10-22 17:11:26 +02002242static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002243weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002244{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002245 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002247 struct weston_animation *animation, *next;
2248 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002249 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002250 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002251 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002252
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002253 if (output->destroying)
2254 return 0;
2255
Pekka Paalanenb5026542014-11-12 15:09:24 +02002256 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2257
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002258 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002260
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002261 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002262 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002263 } else {
2264 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002265 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002266 ev->psf_flags = 0;
2267 }
2268 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002269
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002271 wl_list_for_each(ev, &ec->view_list, link) {
2272 /* Note: This operation is safe to do multiple times on the
2273 * same surface.
2274 */
2275 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002276 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 &ev->surface->frame_callback_list);
2278 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002279
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002280 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002281 }
2282 }
2283
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002284 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002285
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002286 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002287 pixman_region32_intersect(&output_damage,
2288 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002289 pixman_region32_subtract(&output_damage,
2290 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002291
Scott Moreauccbf29d2012-02-22 14:21:41 -07002292 if (output->dirty)
2293 weston_output_update_matrix(output);
2294
David Herrmann1edf44c2013-10-22 17:11:26 +02002295 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002296
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002297 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002298
Kristian Høgsbergef044142011-06-21 15:02:12 -04002299 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002300
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002301 weston_compositor_repick(ec);
2302 wl_event_loop_dispatch(ec->input_loop, 0);
2303
Jonas Ådahldb773762012-06-13 00:01:21 +02002304 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002305 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002306 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002307 }
2308
Scott Moreaud64cf212012-06-08 19:40:54 -06002309 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002310 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002311 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002312 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002313
Pekka Paalanenb5026542014-11-12 15:09:24 +02002314 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2315
David Herrmann1edf44c2013-10-22 17:11:26 +02002316 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002317}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002318
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002319static int
2320weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002321{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002322 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002323
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002324 wl_event_loop_dispatch(compositor->input_loop, 0);
2325
2326 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002327}
2328
Pekka Paalanen82919792014-05-21 13:51:49 +03002329static void
2330weston_output_schedule_repaint_reset(struct weston_output *output)
2331{
2332 struct weston_compositor *compositor = output->compositor;
2333 struct wl_event_loop *loop;
2334 int fd;
2335
2336 output->repaint_scheduled = 0;
2337 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2338
2339 if (compositor->input_loop_source)
2340 return;
2341
2342 loop = wl_display_get_event_loop(compositor->wl_display);
2343 fd = wl_event_loop_get_fd(compositor->input_loop);
2344 compositor->input_loop_source =
2345 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2346 weston_compositor_read_input, compositor);
2347}
2348
Kristian Høgsbergef044142011-06-21 15:02:12 -04002349WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002350weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002351 const struct timespec *stamp,
2352 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002353{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002354 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen82919792014-05-21 13:51:49 +03002355 int r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002356 uint32_t refresh_nsec;
2357
Pekka Paalanenb5026542014-11-12 15:09:24 +02002358 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2359 TLP_VBLANK(stamp), TLP_END);
2360
Pekka Paalanen133e4392014-09-23 22:08:46 -04002361 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2362 weston_presentation_feedback_present_list(&output->feedback_list,
2363 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002364 output->msc,
2365 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002366
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002367 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002368
2369 if (output->repaint_needed &&
2370 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2371 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002372 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002373 if (!r)
2374 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002375 }
2376
Pekka Paalanen82919792014-05-21 13:51:49 +03002377 weston_output_schedule_repaint_reset(output);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002378}
2379
2380static void
2381idle_repaint(void *data)
2382{
2383 struct weston_output *output = data;
2384
Jonas Ådahle5a12252013-04-05 23:07:11 +02002385 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002386}
2387
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002388WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002389weston_layer_entry_insert(struct weston_layer_entry *list,
2390 struct weston_layer_entry *entry)
2391{
2392 wl_list_insert(&list->link, &entry->link);
2393 entry->layer = list->layer;
2394}
2395
2396WL_EXPORT void
2397weston_layer_entry_remove(struct weston_layer_entry *entry)
2398{
2399 wl_list_remove(&entry->link);
2400 wl_list_init(&entry->link);
2401 entry->layer = NULL;
2402}
2403
2404WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002405weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2406{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002407 wl_list_init(&layer->view_list.link);
2408 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002409 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002410 if (below != NULL)
2411 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002412}
2413
2414WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002415weston_layer_set_mask(struct weston_layer *layer,
2416 int x, int y, int width, int height)
2417{
2418 struct weston_view *view;
2419
2420 layer->mask.x1 = x;
2421 layer->mask.x2 = x + width;
2422 layer->mask.y1 = y;
2423 layer->mask.y2 = y + height;
2424
2425 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2426 weston_view_geometry_dirty(view);
2427 }
2428}
2429
2430WL_EXPORT void
2431weston_layer_set_mask_infinite(struct weston_layer *layer)
2432{
2433 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2434 UINT32_MAX, UINT32_MAX);
2435}
2436
2437WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002438weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002439{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002440 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002441 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002442
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002443 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2444 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002445 return;
2446
Pekka Paalanenb5026542014-11-12 15:09:24 +02002447 if (!output->repaint_needed)
2448 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2449
Kristian Høgsbergef044142011-06-21 15:02:12 -04002450 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002451 output->repaint_needed = 1;
2452 if (output->repaint_scheduled)
2453 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002454
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002455 wl_event_loop_add_idle(loop, idle_repaint, output);
2456 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002457 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2458
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002459
2460 if (compositor->input_loop_source) {
2461 wl_event_source_remove(compositor->input_loop_source);
2462 compositor->input_loop_source = NULL;
2463 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002464}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002465
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002466WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002467weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2468{
2469 struct weston_output *output;
2470
2471 wl_list_for_each(output, &compositor->output_list, link)
2472 weston_output_schedule_repaint(output);
2473}
2474
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002475static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002476surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002477{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002478 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002479}
2480
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002481static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002482surface_attach(struct wl_client *client,
2483 struct wl_resource *resource,
2484 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2485{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002486 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002487 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002488
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002489 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002490 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002491 if (buffer == NULL) {
2492 wl_client_post_no_memory(client);
2493 return;
2494 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002495 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002496
Pekka Paalanende685b82012-12-04 15:58:12 +02002497 /* Attach, attach, without commit in between does not send
2498 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002499 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002500
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002501 surface->pending.sx = sx;
2502 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002503 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002504}
2505
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002506static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002507surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002508 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002509 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002510{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002511 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002512
Pekka Paalanen8e159182012-10-10 12:49:25 +03002513 pixman_region32_union_rect(&surface->pending.damage,
2514 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002515 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002516}
2517
Kristian Høgsberg33418202011-08-16 23:01:28 -04002518static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002519destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002520{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002521 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002522
2523 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002524 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002525}
2526
2527static void
2528surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002529 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002530{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002531 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002532 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002533
2534 cb = malloc(sizeof *cb);
2535 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002536 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002537 return;
2538 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002539
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002540 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2541 callback);
2542 if (cb->resource == NULL) {
2543 free(cb);
2544 wl_resource_post_no_memory(resource);
2545 return;
2546 }
2547
Jason Ekstranda85118c2013-06-27 20:17:02 -05002548 wl_resource_set_implementation(cb->resource, NULL, cb,
2549 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002550
Pekka Paalanenbc106382012-10-10 12:49:31 +03002551 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002552}
2553
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002554static void
2555surface_set_opaque_region(struct wl_client *client,
2556 struct wl_resource *resource,
2557 struct wl_resource *region_resource)
2558{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002559 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002560 struct weston_region *region;
2561
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002562 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002563 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002564 pixman_region32_copy(&surface->pending.opaque,
2565 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002566 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002567 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002568 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002569}
2570
2571static void
2572surface_set_input_region(struct wl_client *client,
2573 struct wl_resource *resource,
2574 struct wl_resource *region_resource)
2575{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002576 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002577 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002578
2579 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002580 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002581 pixman_region32_copy(&surface->pending.input,
2582 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002584 pixman_region32_fini(&surface->pending.input);
2585 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002586 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002587}
2588
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002589static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002590weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002591{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592 struct weston_subsurface *sub;
2593
2594 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2595 parent_link_pending) {
2596 wl_list_remove(&sub->parent_link);
2597 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2598 }
2599}
2600
2601static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002602weston_surface_build_buffer_matrix(struct weston_surface *surface,
2603 struct weston_matrix *matrix)
2604{
2605 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2606 double src_width, src_height, dest_width, dest_height;
2607
2608 weston_matrix_init(matrix);
2609
2610 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2611 src_width = surface->width_from_buffer;
2612 src_height = surface->height_from_buffer;
2613 } else {
2614 src_width = wl_fixed_to_double(vp->buffer.src_width);
2615 src_height = wl_fixed_to_double(vp->buffer.src_height);
2616 }
2617
2618 if (vp->surface.width == -1) {
2619 dest_width = src_width;
2620 dest_height = src_height;
2621 } else {
2622 dest_width = vp->surface.width;
2623 dest_height = vp->surface.height;
2624 }
2625
2626 if (src_width != dest_width || src_height != dest_height)
2627 weston_matrix_scale(matrix,
2628 src_width / dest_width,
2629 src_height / dest_height, 1);
2630
2631 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2632 weston_matrix_translate(matrix,
2633 wl_fixed_to_double(vp->buffer.src_x),
2634 wl_fixed_to_double(vp->buffer.src_y),
2635 0);
2636
2637 switch (vp->buffer.transform) {
2638 case WL_OUTPUT_TRANSFORM_FLIPPED:
2639 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2640 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2641 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2642 weston_matrix_scale(matrix, -1, 1, 1);
2643 weston_matrix_translate(matrix,
2644 surface->width_from_buffer, 0, 0);
2645 break;
2646 }
2647
2648 switch (vp->buffer.transform) {
2649 default:
2650 case WL_OUTPUT_TRANSFORM_NORMAL:
2651 case WL_OUTPUT_TRANSFORM_FLIPPED:
2652 break;
2653 case WL_OUTPUT_TRANSFORM_90:
2654 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2655 weston_matrix_rotate_xy(matrix, 0, 1);
2656 weston_matrix_translate(matrix,
2657 surface->height_from_buffer, 0, 0);
2658 break;
2659 case WL_OUTPUT_TRANSFORM_180:
2660 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2661 weston_matrix_rotate_xy(matrix, -1, 0);
2662 weston_matrix_translate(matrix,
2663 surface->width_from_buffer,
2664 surface->height_from_buffer, 0);
2665 break;
2666 case WL_OUTPUT_TRANSFORM_270:
2667 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2668 weston_matrix_rotate_xy(matrix, 0, -1);
2669 weston_matrix_translate(matrix,
2670 0, surface->width_from_buffer, 0);
2671 break;
2672 }
2673
2674 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2675}
2676
2677static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002678weston_surface_commit_state(struct weston_surface *surface,
2679 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002680{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002681 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002682 pixman_region32_t opaque;
2683
Alexander Larsson4ea95522013-05-22 14:41:37 +02002684 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002685 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002686 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002687 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002688
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002689 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002690 if (state->newly_attached)
2691 weston_surface_attach(surface, state->buffer);
2692 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002693
Jason Ekstrand1e059042014-10-16 10:55:19 -05002694 weston_surface_build_buffer_matrix(surface,
2695 &surface->surface_to_buffer_matrix);
2696 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2697 &surface->surface_to_buffer_matrix);
2698
Jason Ekstrand7b982072014-05-20 14:33:03 -05002699 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002700 weston_surface_update_size(surface);
2701 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002702 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002703 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002704
Jason Ekstrand7b982072014-05-20 14:33:03 -05002705 state->sx = 0;
2706 state->sy = 0;
2707 state->newly_attached = 0;
2708 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002709
2710 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002711 if (weston_timeline_enabled_ &&
2712 pixman_region32_not_empty(&state->damage))
2713 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002714 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002715 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002716 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002717 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002718 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002719
2720 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002721 pixman_region32_init(&opaque);
2722 pixman_region32_intersect_rect(&opaque, &state->opaque,
2723 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002724
2725 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2726 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 wl_list_for_each(view, &surface->views, surface_link)
2728 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002729 }
2730
2731 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002732
2733 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002734 pixman_region32_intersect_rect(&surface->input, &state->input,
2735 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002736
Pekka Paalanenbc106382012-10-10 12:49:31 +03002737 /* wl_surface.frame */
2738 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002739 &state->frame_callback_list);
2740 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002741
2742 /* XXX:
2743 * What should happen with a feedback request, if there
2744 * is no wl_buffer attached for this commit?
2745 */
2746
2747 /* presentation.feedback */
2748 wl_list_insert_list(&surface->feedback_list,
2749 &state->feedback_list);
2750 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002751}
2752
2753static void
2754weston_surface_commit(struct weston_surface *surface)
2755{
2756 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002757
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758 weston_surface_commit_subsurface_order(surface);
2759
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002760 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002761}
2762
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002763static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764weston_subsurface_commit(struct weston_subsurface *sub);
2765
2766static void
2767weston_subsurface_parent_commit(struct weston_subsurface *sub,
2768 int parent_is_synchronized);
2769
2770static void
2771surface_commit(struct wl_client *client, struct wl_resource *resource)
2772{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002773 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2775
2776 if (sub) {
2777 weston_subsurface_commit(sub);
2778 return;
2779 }
2780
2781 weston_surface_commit(surface);
2782
2783 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2784 if (sub->surface != surface)
2785 weston_subsurface_parent_commit(sub, 0);
2786 }
2787}
2788
2789static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002790surface_set_buffer_transform(struct wl_client *client,
2791 struct wl_resource *resource, int transform)
2792{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002793 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002794
Jonny Lamba55f1392014-05-30 12:07:15 +02002795 /* if wl_output.transform grows more members this will need to be updated. */
2796 if (transform < 0 ||
2797 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2798 wl_resource_post_error(resource,
2799 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2800 "buffer transform must be a valid transform "
2801 "('%d' specified)", transform);
2802 return;
2803 }
2804
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002805 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002806 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002807}
2808
Alexander Larsson4ea95522013-05-22 14:41:37 +02002809static void
2810surface_set_buffer_scale(struct wl_client *client,
2811 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002812 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002813{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002814 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002815
Jonny Lamba55f1392014-05-30 12:07:15 +02002816 if (scale < 1) {
2817 wl_resource_post_error(resource,
2818 WL_SURFACE_ERROR_INVALID_SCALE,
2819 "buffer scale must be at least one "
2820 "('%d' specified)", scale);
2821 return;
2822 }
2823
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002824 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002825 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002826}
2827
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002828static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002829 surface_destroy,
2830 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002831 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002832 surface_frame,
2833 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002834 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002835 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002836 surface_set_buffer_transform,
2837 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002838};
2839
2840static void
2841compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002842 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002843{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002844 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002845 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002846
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002847 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002848 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002849 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002850 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002851 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002852
Jason Ekstranda85118c2013-06-27 20:17:02 -05002853 surface->resource =
2854 wl_resource_create(client, &wl_surface_interface,
2855 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002856 if (surface->resource == NULL) {
2857 weston_surface_destroy(surface);
2858 wl_resource_post_no_memory(resource);
2859 return;
2860 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002861 wl_resource_set_implementation(surface->resource, &surface_interface,
2862 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002863
2864 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002865}
2866
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002867static void
2868destroy_region(struct wl_resource *resource)
2869{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002870 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002871
2872 pixman_region32_fini(&region->region);
2873 free(region);
2874}
2875
2876static void
2877region_destroy(struct wl_client *client, struct wl_resource *resource)
2878{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002879 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002880}
2881
2882static void
2883region_add(struct wl_client *client, struct wl_resource *resource,
2884 int32_t x, int32_t y, int32_t width, int32_t height)
2885{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002886 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002887
2888 pixman_region32_union_rect(&region->region, &region->region,
2889 x, y, width, height);
2890}
2891
2892static void
2893region_subtract(struct wl_client *client, struct wl_resource *resource,
2894 int32_t x, int32_t y, int32_t width, int32_t height)
2895{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002896 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002897 pixman_region32_t rect;
2898
2899 pixman_region32_init_rect(&rect, x, y, width, height);
2900 pixman_region32_subtract(&region->region, &region->region, &rect);
2901 pixman_region32_fini(&rect);
2902}
2903
2904static const struct wl_region_interface region_interface = {
2905 region_destroy,
2906 region_add,
2907 region_subtract
2908};
2909
2910static void
2911compositor_create_region(struct wl_client *client,
2912 struct wl_resource *resource, uint32_t id)
2913{
2914 struct weston_region *region;
2915
2916 region = malloc(sizeof *region);
2917 if (region == NULL) {
2918 wl_resource_post_no_memory(resource);
2919 return;
2920 }
2921
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002922 pixman_region32_init(&region->region);
2923
Jason Ekstranda85118c2013-06-27 20:17:02 -05002924 region->resource =
2925 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002926 if (region->resource == NULL) {
2927 free(region);
2928 wl_resource_post_no_memory(resource);
2929 return;
2930 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002931 wl_resource_set_implementation(region->resource, &region_interface,
2932 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002933}
2934
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002935static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002936 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002937 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002938};
2939
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002940static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2942{
2943 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002944
Jason Ekstrand7b982072014-05-20 14:33:03 -05002945 weston_surface_commit_state(surface, &sub->cached);
2946 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002947
2948 weston_surface_commit_subsurface_order(surface);
2949
2950 weston_surface_schedule_repaint(surface);
2951
Jason Ekstrand7b982072014-05-20 14:33:03 -05002952 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002953}
2954
2955static void
2956weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2957{
2958 struct weston_surface *surface = sub->surface;
2959
2960 /*
2961 * If this commit would cause the surface to move by the
2962 * attach(dx, dy) parameters, the old damage region must be
2963 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002964 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002965 */
2966 pixman_region32_translate(&sub->cached.damage,
2967 -surface->pending.sx, -surface->pending.sy);
2968 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2969 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002970 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002971
2972 if (surface->pending.newly_attached) {
2973 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002974 weston_surface_state_set_buffer(&sub->cached,
2975 surface->pending.buffer);
2976 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002977 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002978 weston_presentation_feedback_discard_list(
2979 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002980 }
2981 sub->cached.sx += surface->pending.sx;
2982 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002983
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002984 sub->cached.buffer_viewport.changed |=
2985 surface->pending.buffer_viewport.changed;
2986 sub->cached.buffer_viewport.buffer =
2987 surface->pending.buffer_viewport.buffer;
2988 sub->cached.buffer_viewport.surface =
2989 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002990
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002991 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002992
2993 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2994
2995 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2996
2997 wl_list_insert_list(&sub->cached.frame_callback_list,
2998 &surface->pending.frame_callback_list);
2999 wl_list_init(&surface->pending.frame_callback_list);
3000
Pekka Paalanen133e4392014-09-23 22:08:46 -04003001 wl_list_insert_list(&sub->cached.feedback_list,
3002 &surface->pending.feedback_list);
3003 wl_list_init(&surface->pending.feedback_list);
3004
Jason Ekstrand7b982072014-05-20 14:33:03 -05003005 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003006}
3007
Derek Foreman280e7dd2014-10-03 13:13:42 -05003008static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003009weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3010{
3011 while (sub) {
3012 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003013 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003014
3015 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003016 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003017
3018 sub = weston_surface_to_subsurface(sub->parent);
3019 }
3020
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003021 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003022}
3023
3024static void
3025weston_subsurface_commit(struct weston_subsurface *sub)
3026{
3027 struct weston_surface *surface = sub->surface;
3028 struct weston_subsurface *tmp;
3029
3030 /* Recursive check for effectively synchronized. */
3031 if (weston_subsurface_is_synchronized(sub)) {
3032 weston_subsurface_commit_to_cache(sub);
3033 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003034 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003035 /* flush accumulated state from cache */
3036 weston_subsurface_commit_to_cache(sub);
3037 weston_subsurface_commit_from_cache(sub);
3038 } else {
3039 weston_surface_commit(surface);
3040 }
3041
3042 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3043 if (tmp->surface != surface)
3044 weston_subsurface_parent_commit(tmp, 0);
3045 }
3046 }
3047}
3048
3049static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003050weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003051{
3052 struct weston_surface *surface = sub->surface;
3053 struct weston_subsurface *tmp;
3054
Pekka Paalanene67858b2013-04-25 13:57:42 +03003055 /* From now on, commit_from_cache the whole sub-tree, regardless of
3056 * the synchronized mode of each child. This sub-surface or some
3057 * of its ancestors were synchronized, so we are synchronized
3058 * all the way down.
3059 */
3060
Jason Ekstrand7b982072014-05-20 14:33:03 -05003061 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003062 weston_subsurface_commit_from_cache(sub);
3063
3064 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3065 if (tmp->surface != surface)
3066 weston_subsurface_parent_commit(tmp, 1);
3067 }
3068}
3069
3070static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003071weston_subsurface_parent_commit(struct weston_subsurface *sub,
3072 int parent_is_synchronized)
3073{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003074 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003075 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003076 wl_list_for_each(view, &sub->surface->views, surface_link)
3077 weston_view_set_position(view,
3078 sub->position.x,
3079 sub->position.y);
3080
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003081 sub->position.set = 0;
3082 }
3083
3084 if (parent_is_synchronized || sub->synchronized)
3085 weston_subsurface_synchronized_commit(sub);
3086}
3087
Pekka Paalanen8274d902014-08-06 19:36:51 +03003088static int
3089subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3090{
3091 return snprintf(buf, len, "sub-surface");
3092}
3093
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003094static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003095subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003096{
3097 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003098 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003099
Jason Ekstranda7af7042013-10-12 22:38:11 -05003100 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003101 weston_view_set_position(view,
3102 view->geometry.x + dx,
3103 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003104
3105 /* No need to check parent mappedness, because if parent is not
3106 * mapped, parent is not in a visible layer, so this sub-surface
3107 * will not be drawn either.
3108 */
3109 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003110 struct weston_output *output;
3111
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003112 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113 * because that would call it also for the parent surface,
3114 * which might not be mapped yet. That would lead to
3115 * inconsistent state, where the window could never be
3116 * mapped.
3117 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003118 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003119 * weston_surface_is_mapped() return true, so that when the
3120 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003121 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003122 */
3123 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003124 output = container_of(compositor->output_list.next,
3125 struct weston_output, link);
3126
3127 surface->output = output;
3128 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003129 }
3130}
3131
3132static struct weston_subsurface *
3133weston_surface_to_subsurface(struct weston_surface *surface)
3134{
3135 if (surface->configure == subsurface_configure)
3136 return surface->configure_private;
3137
3138 return NULL;
3139}
3140
Pekka Paalanen01388e22013-04-25 13:57:44 +03003141WL_EXPORT struct weston_surface *
3142weston_surface_get_main_surface(struct weston_surface *surface)
3143{
3144 struct weston_subsurface *sub;
3145
3146 while (surface && (sub = weston_surface_to_subsurface(surface)))
3147 surface = sub->parent;
3148
3149 return surface;
3150}
3151
Pekka Paalanen50b67472014-10-01 15:02:41 +03003152WL_EXPORT int
3153weston_surface_set_role(struct weston_surface *surface,
3154 const char *role_name,
3155 struct wl_resource *error_resource,
3156 uint32_t error_code)
3157{
3158 assert(role_name);
3159
3160 if (surface->role_name == NULL ||
3161 surface->role_name == role_name ||
3162 strcmp(surface->role_name, role_name) == 0) {
3163 surface->role_name = role_name;
3164
3165 return 0;
3166 }
3167
3168 wl_resource_post_error(error_resource, error_code,
3169 "Cannot assign role %s to wl_surface@%d,"
3170 " already has role %s\n",
3171 role_name,
3172 wl_resource_get_id(surface->resource),
3173 surface->role_name);
3174 return -1;
3175}
3176
Pekka Paalanen8274d902014-08-06 19:36:51 +03003177WL_EXPORT void
3178weston_surface_set_label_func(struct weston_surface *surface,
3179 int (*desc)(struct weston_surface *,
3180 char *, size_t))
3181{
3182 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003183 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003184}
3185
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003186/** Get the size of surface contents
3187 *
3188 * \param surface The surface to query.
3189 * \param width Returns the width of raw contents.
3190 * \param height Returns the height of raw contents.
3191 *
3192 * Retrieves the raw surface content size in pixels for the given surface.
3193 * This is the whole content size in buffer pixels. If the surface
3194 * has no content or the renderer does not implement this feature,
3195 * zeroes are returned.
3196 *
3197 * This function is used to determine the buffer size needed for
3198 * a weston_surface_copy_content() call.
3199 */
3200WL_EXPORT void
3201weston_surface_get_content_size(struct weston_surface *surface,
3202 int *width, int *height)
3203{
3204 struct weston_renderer *rer = surface->compositor->renderer;
3205
3206 if (!rer->surface_get_content_size) {
3207 *width = 0;
3208 *height = 0;
3209 return;
3210 }
3211
3212 rer->surface_get_content_size(surface, width, height);
3213}
3214
3215/** Copy surface contents to system memory.
3216 *
3217 * \param surface The surface to copy from.
3218 * \param target Pointer to the target memory buffer.
3219 * \param size Size of the target buffer in bytes.
3220 * \param src_x X location on contents to copy from.
3221 * \param src_y Y location on contents to copy from.
3222 * \param width Width in pixels of the area to copy.
3223 * \param height Height in pixels of the area to copy.
3224 * \return 0 for success, -1 for failure.
3225 *
3226 * Surface contents are maintained by the renderer. They can be in a
3227 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3228 * else.
3229 *
3230 * Surface contents are copied into memory pointed to by target,
3231 * which has size bytes of space available. The target memory
3232 * may be larger than needed, but being smaller returns an error.
3233 * The extra bytes in target may or may not be written; their content is
3234 * unspecified. Size must be large enough to hold the image.
3235 *
3236 * The image in the target memory will be arranged in rows from
3237 * top to bottom, and pixels on a row from left to right. The pixel
3238 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3239 * width * 4.
3240 *
3241 * Parameters src_x and src_y define the upper-left corner in buffer
3242 * coordinates (pixels) to copy from. Parameters width and height
3243 * define the size of the area to copy in pixels.
3244 *
3245 * The rectangle defined by src_x, src_y, width, height must fit in
3246 * the surface contents. Otherwise an error is returned.
3247 *
3248 * Use surface_get_data_size to determine the content size; the
3249 * needed target buffer size and rectangle limits.
3250 *
3251 * CURRENT IMPLEMENTATION RESTRICTIONS:
3252 * - the machine must be little-endian due to Pixman formats.
3253 *
3254 * NOTE: Pixman formats are premultiplied.
3255 */
3256WL_EXPORT int
3257weston_surface_copy_content(struct weston_surface *surface,
3258 void *target, size_t size,
3259 int src_x, int src_y,
3260 int width, int height)
3261{
3262 struct weston_renderer *rer = surface->compositor->renderer;
3263 int cw, ch;
3264 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3265
3266 if (!rer->surface_copy_content)
3267 return -1;
3268
3269 weston_surface_get_content_size(surface, &cw, &ch);
3270
3271 if (src_x < 0 || src_y < 0)
3272 return -1;
3273
3274 if (width <= 0 || height <= 0)
3275 return -1;
3276
3277 if (src_x + width > cw || src_y + height > ch)
3278 return -1;
3279
3280 if (width * bytespp * height > size)
3281 return -1;
3282
3283 return rer->surface_copy_content(surface, target, size,
3284 src_x, src_y, width, height);
3285}
3286
Pekka Paalanene67858b2013-04-25 13:57:42 +03003287static void
3288subsurface_set_position(struct wl_client *client,
3289 struct wl_resource *resource, int32_t x, int32_t y)
3290{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003291 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003292
3293 if (!sub)
3294 return;
3295
3296 sub->position.x = x;
3297 sub->position.y = y;
3298 sub->position.set = 1;
3299}
3300
3301static struct weston_subsurface *
3302subsurface_from_surface(struct weston_surface *surface)
3303{
3304 struct weston_subsurface *sub;
3305
3306 sub = weston_surface_to_subsurface(surface);
3307 if (sub)
3308 return sub;
3309
3310 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3311 if (sub->surface == surface)
3312 return sub;
3313
3314 return NULL;
3315}
3316
3317static struct weston_subsurface *
3318subsurface_sibling_check(struct weston_subsurface *sub,
3319 struct weston_surface *surface,
3320 const char *request)
3321{
3322 struct weston_subsurface *sibling;
3323
3324 sibling = subsurface_from_surface(surface);
3325
3326 if (!sibling) {
3327 wl_resource_post_error(sub->resource,
3328 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3329 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003330 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003331 return NULL;
3332 }
3333
3334 if (sibling->parent != sub->parent) {
3335 wl_resource_post_error(sub->resource,
3336 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3337 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003338 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003339 return NULL;
3340 }
3341
3342 return sibling;
3343}
3344
3345static void
3346subsurface_place_above(struct wl_client *client,
3347 struct wl_resource *resource,
3348 struct wl_resource *sibling_resource)
3349{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003350 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003351 struct weston_surface *surface =
3352 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003353 struct weston_subsurface *sibling;
3354
3355 if (!sub)
3356 return;
3357
3358 sibling = subsurface_sibling_check(sub, surface, "place_above");
3359 if (!sibling)
3360 return;
3361
3362 wl_list_remove(&sub->parent_link_pending);
3363 wl_list_insert(sibling->parent_link_pending.prev,
3364 &sub->parent_link_pending);
3365}
3366
3367static void
3368subsurface_place_below(struct wl_client *client,
3369 struct wl_resource *resource,
3370 struct wl_resource *sibling_resource)
3371{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003372 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003373 struct weston_surface *surface =
3374 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003375 struct weston_subsurface *sibling;
3376
3377 if (!sub)
3378 return;
3379
3380 sibling = subsurface_sibling_check(sub, surface, "place_below");
3381 if (!sibling)
3382 return;
3383
3384 wl_list_remove(&sub->parent_link_pending);
3385 wl_list_insert(&sibling->parent_link_pending,
3386 &sub->parent_link_pending);
3387}
3388
3389static void
3390subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3391{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003392 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003393
3394 if (sub)
3395 sub->synchronized = 1;
3396}
3397
3398static void
3399subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3400{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003401 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003402
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003403 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003404 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003405
3406 /* If sub became effectively desynchronized, flush. */
3407 if (!weston_subsurface_is_synchronized(sub))
3408 weston_subsurface_synchronized_commit(sub);
3409 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003410}
3411
3412static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003413weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3414{
3415 wl_list_remove(&sub->parent_link);
3416 wl_list_remove(&sub->parent_link_pending);
3417 wl_list_remove(&sub->parent_destroy_listener.link);
3418 sub->parent = NULL;
3419}
3420
3421static void
3422weston_subsurface_destroy(struct weston_subsurface *sub);
3423
3424static void
3425subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3426{
3427 struct weston_subsurface *sub =
3428 container_of(listener, struct weston_subsurface,
3429 surface_destroy_listener);
3430 assert(data == &sub->surface->resource);
3431
3432 /* The protocol object (wl_resource) is left inert. */
3433 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003434 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003435
3436 weston_subsurface_destroy(sub);
3437}
3438
3439static void
3440subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3441{
3442 struct weston_subsurface *sub =
3443 container_of(listener, struct weston_subsurface,
3444 parent_destroy_listener);
3445 assert(data == &sub->parent->resource);
3446 assert(sub->surface != sub->parent);
3447
3448 if (weston_surface_is_mapped(sub->surface))
3449 weston_surface_unmap(sub->surface);
3450
3451 weston_subsurface_unlink_parent(sub);
3452}
3453
3454static void
3455subsurface_resource_destroy(struct wl_resource *resource)
3456{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003457 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003458
3459 if (sub)
3460 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003461}
3462
3463static void
3464subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3465{
3466 wl_resource_destroy(resource);
3467}
3468
3469static void
3470weston_subsurface_link_parent(struct weston_subsurface *sub,
3471 struct weston_surface *parent)
3472{
3473 sub->parent = parent;
3474 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003475 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003476 &sub->parent_destroy_listener);
3477
3478 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3479 wl_list_insert(&parent->subsurface_list_pending,
3480 &sub->parent_link_pending);
3481}
3482
3483static void
3484weston_subsurface_link_surface(struct weston_subsurface *sub,
3485 struct weston_surface *surface)
3486{
3487 sub->surface = surface;
3488 sub->surface_destroy_listener.notify =
3489 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003490 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003491 &sub->surface_destroy_listener);
3492}
3493
3494static void
3495weston_subsurface_destroy(struct weston_subsurface *sub)
3496{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003497 struct weston_view *view, *next;
3498
Pekka Paalanene67858b2013-04-25 13:57:42 +03003499 assert(sub->surface);
3500
3501 if (sub->resource) {
3502 assert(weston_surface_to_subsurface(sub->surface) == sub);
3503 assert(sub->parent_destroy_listener.notify ==
3504 subsurface_handle_parent_destroy);
3505
George Kiagiadakised04d382014-06-13 18:10:26 +02003506 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3507 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003508 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003509 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003510
Pekka Paalanene67858b2013-04-25 13:57:42 +03003511 if (sub->parent)
3512 weston_subsurface_unlink_parent(sub);
3513
Jason Ekstrand7b982072014-05-20 14:33:03 -05003514 weston_surface_state_fini(&sub->cached);
3515 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003516
3517 sub->surface->configure = NULL;
3518 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003519 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003520 } else {
3521 /* the dummy weston_subsurface for the parent itself */
3522 assert(sub->parent_destroy_listener.notify == NULL);
3523 wl_list_remove(&sub->parent_link);
3524 wl_list_remove(&sub->parent_link_pending);
3525 }
3526
3527 wl_list_remove(&sub->surface_destroy_listener.link);
3528 free(sub);
3529}
3530
3531static const struct wl_subsurface_interface subsurface_implementation = {
3532 subsurface_destroy,
3533 subsurface_set_position,
3534 subsurface_place_above,
3535 subsurface_place_below,
3536 subsurface_set_sync,
3537 subsurface_set_desync
3538};
3539
3540static struct weston_subsurface *
3541weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3542 struct weston_surface *parent)
3543{
3544 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003545 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003546
Bryce Harringtonde16d892014-11-20 22:21:57 -08003547 sub = zalloc(sizeof *sub);
3548 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003549 return NULL;
3550
Jason Ekstranda7af7042013-10-12 22:38:11 -05003551 wl_list_init(&sub->unused_views);
3552
Jason Ekstranda85118c2013-06-27 20:17:02 -05003553 sub->resource =
3554 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003555 if (!sub->resource) {
3556 free(sub);
3557 return NULL;
3558 }
3559
Jason Ekstranda85118c2013-06-27 20:17:02 -05003560 wl_resource_set_implementation(sub->resource,
3561 &subsurface_implementation,
3562 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003563 weston_subsurface_link_surface(sub, surface);
3564 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003565 weston_surface_state_init(&sub->cached);
3566 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003567 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003568
3569 return sub;
3570}
3571
3572/* Create a dummy subsurface for having the parent itself in its
3573 * sub-surface lists. Makes stacking order manipulation easy.
3574 */
3575static struct weston_subsurface *
3576weston_subsurface_create_for_parent(struct weston_surface *parent)
3577{
3578 struct weston_subsurface *sub;
3579
Bryce Harringtonde16d892014-11-20 22:21:57 -08003580 sub = zalloc(sizeof *sub);
3581 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003582 return NULL;
3583
3584 weston_subsurface_link_surface(sub, parent);
3585 sub->parent = parent;
3586 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3587 wl_list_insert(&parent->subsurface_list_pending,
3588 &sub->parent_link_pending);
3589
3590 return sub;
3591}
3592
3593static void
3594subcompositor_get_subsurface(struct wl_client *client,
3595 struct wl_resource *resource,
3596 uint32_t id,
3597 struct wl_resource *surface_resource,
3598 struct wl_resource *parent_resource)
3599{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003600 struct weston_surface *surface =
3601 wl_resource_get_user_data(surface_resource);
3602 struct weston_surface *parent =
3603 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003604 struct weston_subsurface *sub;
3605 static const char where[] = "get_subsurface: wl_subsurface@";
3606
3607 if (surface == parent) {
3608 wl_resource_post_error(resource,
3609 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3610 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003611 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003612 return;
3613 }
3614
3615 if (weston_surface_to_subsurface(surface)) {
3616 wl_resource_post_error(resource,
3617 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3618 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003619 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003620 return;
3621 }
3622
Pekka Paalanen50b67472014-10-01 15:02:41 +03003623 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3624 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003625 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003626
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003627 if (weston_surface_get_main_surface(parent) == surface) {
3628 wl_resource_post_error(resource,
3629 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3630 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003631 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003632 return;
3633 }
3634
Pekka Paalanene67858b2013-04-25 13:57:42 +03003635 /* make sure the parent is in its own list */
3636 if (wl_list_empty(&parent->subsurface_list)) {
3637 if (!weston_subsurface_create_for_parent(parent)) {
3638 wl_resource_post_no_memory(resource);
3639 return;
3640 }
3641 }
3642
3643 sub = weston_subsurface_create(id, surface, parent);
3644 if (!sub) {
3645 wl_resource_post_no_memory(resource);
3646 return;
3647 }
3648
3649 surface->configure = subsurface_configure;
3650 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003651 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003652}
3653
3654static void
3655subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3656{
3657 wl_resource_destroy(resource);
3658}
3659
3660static const struct wl_subcompositor_interface subcompositor_interface = {
3661 subcompositor_destroy,
3662 subcompositor_get_subsurface
3663};
3664
3665static void
3666bind_subcompositor(struct wl_client *client,
3667 void *data, uint32_t version, uint32_t id)
3668{
3669 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003670 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003671
Jason Ekstranda85118c2013-06-27 20:17:02 -05003672 resource =
3673 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003674 if (resource == NULL) {
3675 wl_client_post_no_memory(client);
3676 return;
3677 }
3678 wl_resource_set_implementation(resource, &subcompositor_interface,
3679 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003680}
3681
3682static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003683weston_compositor_dpms(struct weston_compositor *compositor,
3684 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003685{
3686 struct weston_output *output;
3687
3688 wl_list_for_each(output, &compositor->output_list, link)
3689 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003690 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003691}
3692
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003693WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003694weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003695{
Neil Roberts8b62e202013-09-30 13:14:47 +01003696 uint32_t old_state = compositor->state;
3697
3698 /* The state needs to be changed before emitting the wake
3699 * signal because that may try to schedule a repaint which
3700 * will not work if the compositor is still sleeping */
3701 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3702
3703 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003704 case WESTON_COMPOSITOR_SLEEPING:
3705 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3706 /* fall through */
3707 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003708 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003709 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003710 /* fall through */
3711 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003712 wl_event_source_timer_update(compositor->idle_source,
3713 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003714 }
3715}
3716
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003717WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003718weston_compositor_offscreen(struct weston_compositor *compositor)
3719{
3720 switch (compositor->state) {
3721 case WESTON_COMPOSITOR_OFFSCREEN:
3722 return;
3723 case WESTON_COMPOSITOR_SLEEPING:
3724 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3725 /* fall through */
3726 default:
3727 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3728 wl_event_source_timer_update(compositor->idle_source, 0);
3729 }
3730}
3731
3732WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003733weston_compositor_sleep(struct weston_compositor *compositor)
3734{
3735 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3736 return;
3737
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003738 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003739 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3740 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3741}
3742
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003743static int
3744idle_handler(void *data)
3745{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003746 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003747
3748 if (compositor->idle_inhibit)
3749 return 1;
3750
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003751 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003752 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003753
3754 return 1;
3755}
3756
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003757WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003758weston_plane_init(struct weston_plane *plane,
3759 struct weston_compositor *ec,
3760 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003761{
3762 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003763 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003764 plane->x = x;
3765 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003766 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003767
3768 /* Init the link so that the call to wl_list_remove() when releasing
3769 * the plane without ever stacking doesn't lead to a crash */
3770 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003771}
3772
3773WL_EXPORT void
3774weston_plane_release(struct weston_plane *plane)
3775{
Xiong Zhang97116532013-10-23 13:58:31 +08003776 struct weston_view *view;
3777
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003778 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003779 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003780
Xiong Zhang97116532013-10-23 13:58:31 +08003781 wl_list_for_each(view, &plane->compositor->view_list, link) {
3782 if (view->plane == plane)
3783 view->plane = NULL;
3784 }
3785
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003786 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003787}
3788
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003789WL_EXPORT void
3790weston_compositor_stack_plane(struct weston_compositor *ec,
3791 struct weston_plane *plane,
3792 struct weston_plane *above)
3793{
3794 if (above)
3795 wl_list_insert(above->link.prev, &plane->link);
3796 else
3797 wl_list_insert(&ec->plane_list, &plane->link);
3798}
3799
Casey Dahlin9074db52012-04-19 22:50:09 -04003800static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003801{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003802 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003803}
3804
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003805static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003806bind_output(struct wl_client *client,
3807 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003809 struct weston_output *output = data;
3810 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003811 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003812
Jason Ekstranda85118c2013-06-27 20:17:02 -05003813 resource = wl_resource_create(client, &wl_output_interface,
3814 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003815 if (resource == NULL) {
3816 wl_client_post_no_memory(client);
3817 return;
3818 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003819
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003820 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003821 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003822
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003823 wl_output_send_geometry(resource,
3824 output->x,
3825 output->y,
3826 output->mm_width,
3827 output->mm_height,
3828 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003829 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003830 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003831 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003832 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003833 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003834
3835 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003836 wl_output_send_mode(resource,
3837 mode->flags,
3838 mode->width,
3839 mode->height,
3840 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003841 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003842
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003843 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003844 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003845}
3846
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003847/* Move other outputs when one is removed so the space remains contiguos. */
3848static void
3849weston_compositor_remove_output(struct weston_compositor *compositor,
3850 struct weston_output *remove_output)
3851{
3852 struct weston_output *output;
3853 int offset = 0;
3854
3855 wl_list_for_each(output, &compositor->output_list, link) {
3856 if (output == remove_output) {
3857 offset = output->width;
3858 continue;
3859 }
3860
3861 if (offset > 0) {
3862 weston_output_move(output,
3863 output->x - offset, output->y);
3864 output->dirty = 1;
3865 }
3866 }
3867}
3868
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003869WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003870weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003871{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003872 struct wl_resource *resource;
3873
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003874 output->destroying = 1;
3875
Pekka Paalanen133e4392014-09-23 22:08:46 -04003876 weston_presentation_feedback_discard_list(&output->feedback_list);
3877
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003878 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003879 wl_list_remove(&output->link);
3880
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003881 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003882 wl_signal_emit(&output->destroy_signal, output);
3883
Richard Hughesafe690c2013-05-02 10:10:04 +01003884 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003885 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003886 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003887 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003888
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003889 wl_resource_for_each(resource, &output->resource_list) {
3890 wl_resource_set_destructor(resource, NULL);
3891 }
3892
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003893 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003894}
3895
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003896WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003897weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003898{
Scott Moreau850ca422012-05-21 15:21:25 -06003899 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003900
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003901 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003902 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003903
Scott Moreauccbf29d2012-02-22 14:21:41 -07003904 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003905 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003906 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003907 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003908 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003909 weston_matrix_scale(&output->matrix, magnification,
3910 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003911 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003912
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003913 switch (output->transform) {
3914 case WL_OUTPUT_TRANSFORM_FLIPPED:
3915 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3916 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3917 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3918 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3919 weston_matrix_scale(&output->matrix, -1, 1, 1);
3920 break;
3921 }
3922
3923 switch (output->transform) {
3924 default:
3925 case WL_OUTPUT_TRANSFORM_NORMAL:
3926 case WL_OUTPUT_TRANSFORM_FLIPPED:
3927 break;
3928 case WL_OUTPUT_TRANSFORM_90:
3929 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3930 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3931 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3932 break;
3933 case WL_OUTPUT_TRANSFORM_180:
3934 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3935 weston_matrix_translate(&output->matrix,
3936 -output->width, -output->height, 0);
3937 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3938 break;
3939 case WL_OUTPUT_TRANSFORM_270:
3940 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3941 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3942 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3943 break;
3944 }
3945
3946 if (output->current_scale != 1)
3947 weston_matrix_scale(&output->matrix,
3948 output->current_scale,
3949 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003950
Scott Moreauccbf29d2012-02-22 14:21:41 -07003951 output->dirty = 0;
3952}
3953
Scott Moreau1bad5db2012-08-18 01:04:05 -06003954static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003955weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003956{
3957 output->transform = transform;
3958
3959 switch (transform) {
3960 case WL_OUTPUT_TRANSFORM_90:
3961 case WL_OUTPUT_TRANSFORM_270:
3962 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3963 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3964 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003965 output->width = output->current_mode->height;
3966 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003967 break;
3968 case WL_OUTPUT_TRANSFORM_NORMAL:
3969 case WL_OUTPUT_TRANSFORM_180:
3970 case WL_OUTPUT_TRANSFORM_FLIPPED:
3971 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003972 output->width = output->current_mode->width;
3973 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003974 break;
3975 default:
3976 break;
3977 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003978
Hardening57388e42013-09-18 23:56:36 +02003979 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003980 output->width /= scale;
3981 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003982}
3983
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003984static void
3985weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003986{
3987 output->x = x;
3988 output->y = y;
3989
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003990 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003991 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003992 output->width,
3993 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003994}
3995
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003996WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003997weston_output_move(struct weston_output *output, int x, int y)
3998{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003999 struct wl_resource *resource;
4000
4001 output->move_x = x - output->x;
4002 output->move_y = y - output->y;
4003
4004 if (output->move_x == 0 && output->move_y == 0)
4005 return;
4006
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004007 weston_output_init_geometry(output, x, y);
4008
4009 output->dirty = 1;
4010
4011 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004012 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004013
4014 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004015 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004016 wl_output_send_geometry(resource,
4017 output->x,
4018 output->y,
4019 output->mm_width,
4020 output->mm_height,
4021 output->subpixel,
4022 output->make,
4023 output->model,
4024 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004025
4026 if (wl_resource_get_version(resource) >= 2)
4027 wl_output_send_done(resource);
4028 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004029}
4030
4031WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004032weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004033 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004034 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004035{
4036 output->compositor = c;
4037 output->x = x;
4038 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004039 output->mm_width = mm_width;
4040 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004041 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004042 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004043
Alexander Larsson0b135062013-05-28 16:23:36 +02004044 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004045 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004046
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004047 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004048 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004049
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004050 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004051 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004052 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004053 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004054 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004055
Casey Dahlin58ba1372012-04-19 22:50:08 -04004056 output->id = ffs(~output->compositor->output_id_pool) - 1;
4057 output->compositor->output_id_pool |= 1 << output->id;
4058
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004059 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004060 wl_global_create(c->wl_display, &wl_output_interface, 2,
4061 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01004062 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004063}
4064
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004065WL_EXPORT void
4066weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004067 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004068 wl_fixed_t *x, wl_fixed_t *y)
4069{
Derek Foreman0f679412014-10-02 13:41:17 -05004070 struct weston_vector p = { {
4071 wl_fixed_to_double(device_x),
4072 wl_fixed_to_double(device_y),
4073 0.0,
4074 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004075
Derek Foreman0f679412014-10-02 13:41:17 -05004076 weston_matrix_transform(&output->matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004077
Derek Foreman0f679412014-10-02 13:41:17 -05004078 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4079 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004080}
4081
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004082static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004083destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004084{
Jonny Lamb74130762013-11-26 18:19:46 +01004085 struct weston_surface *surface =
4086 wl_resource_get_user_data(resource);
4087
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004088 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004089 surface->pending.buffer_viewport.buffer.src_width =
4090 wl_fixed_from_int(-1);
4091 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004092 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004093}
4094
4095static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004096viewport_destroy(struct wl_client *client,
4097 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004098{
4099 wl_resource_destroy(resource);
4100}
4101
4102static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004103viewport_set(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 int32_t dst_width,
4110 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004111{
Jonny Lamb74130762013-11-26 18:19:46 +01004112 struct weston_surface *surface =
4113 wl_resource_get_user_data(resource);
4114
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004115 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004116
Jonny Lamb8ae35902013-11-26 18:19:45 +01004117 if (wl_fixed_to_double(src_width) < 0 ||
4118 wl_fixed_to_double(src_height) < 0) {
4119 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004120 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004121 "source dimensions must be non-negative (%fx%f)",
4122 wl_fixed_to_double(src_width),
4123 wl_fixed_to_double(src_height));
4124 return;
4125 }
4126
4127 if (dst_width <= 0 || dst_height <= 0) {
4128 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004129 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004130 "destination dimensions must be positive (%dx%d)",
4131 dst_width, dst_height);
4132 return;
4133 }
4134
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004135 surface->pending.buffer_viewport.buffer.src_x = src_x;
4136 surface->pending.buffer_viewport.buffer.src_y = src_y;
4137 surface->pending.buffer_viewport.buffer.src_width = src_width;
4138 surface->pending.buffer_viewport.buffer.src_height = src_height;
4139 surface->pending.buffer_viewport.surface.width = dst_width;
4140 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004141 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004142}
4143
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004144static void
4145viewport_set_source(struct wl_client *client,
4146 struct wl_resource *resource,
4147 wl_fixed_t src_x,
4148 wl_fixed_t src_y,
4149 wl_fixed_t src_width,
4150 wl_fixed_t src_height)
4151{
4152 struct weston_surface *surface =
4153 wl_resource_get_user_data(resource);
4154
4155 assert(surface->viewport_resource != NULL);
4156
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004157 if (src_width == wl_fixed_from_int(-1) &&
4158 src_height == wl_fixed_from_int(-1)) {
4159 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004160 surface->pending.buffer_viewport.buffer.src_width =
4161 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004162 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004163 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004164 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004165
4166 if (src_width <= 0 || src_height <= 0) {
4167 wl_resource_post_error(resource,
4168 WL_VIEWPORT_ERROR_BAD_VALUE,
4169 "source size must be positive (%fx%f)",
4170 wl_fixed_to_double(src_width),
4171 wl_fixed_to_double(src_height));
4172 return;
4173 }
4174
4175 surface->pending.buffer_viewport.buffer.src_x = src_x;
4176 surface->pending.buffer_viewport.buffer.src_y = src_y;
4177 surface->pending.buffer_viewport.buffer.src_width = src_width;
4178 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004179 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004180}
4181
4182static void
4183viewport_set_destination(struct wl_client *client,
4184 struct wl_resource *resource,
4185 int32_t dst_width,
4186 int32_t dst_height)
4187{
4188 struct weston_surface *surface =
4189 wl_resource_get_user_data(resource);
4190
4191 assert(surface->viewport_resource != NULL);
4192
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004193 if (dst_width == -1 && dst_height == -1) {
4194 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004195 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004196 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004197 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004198 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004199
4200 if (dst_width <= 0 || dst_height <= 0) {
4201 wl_resource_post_error(resource,
4202 WL_VIEWPORT_ERROR_BAD_VALUE,
4203 "destination size must be positive (%dx%d)",
4204 dst_width, dst_height);
4205 return;
4206 }
4207
4208 surface->pending.buffer_viewport.surface.width = dst_width;
4209 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004210 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004211}
4212
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004213static const struct wl_viewport_interface viewport_interface = {
4214 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004215 viewport_set,
4216 viewport_set_source,
4217 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004218};
4219
4220static void
4221scaler_destroy(struct wl_client *client,
4222 struct wl_resource *resource)
4223{
4224 wl_resource_destroy(resource);
4225}
4226
4227static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004228scaler_get_viewport(struct wl_client *client,
4229 struct wl_resource *scaler,
4230 uint32_t id,
4231 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004232{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004233 int version = wl_resource_get_version(scaler);
4234 struct weston_surface *surface =
4235 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004236 struct wl_resource *resource;
4237
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004238 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004239 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004240 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4241 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004242 return;
4243 }
4244
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004245 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004246 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004247 if (resource == NULL) {
4248 wl_client_post_no_memory(client);
4249 return;
4250 }
4251
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004252 wl_resource_set_implementation(resource, &viewport_interface,
4253 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004254
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004255 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004256}
4257
4258static const struct wl_scaler_interface scaler_interface = {
4259 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004260 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004261};
4262
4263static void
4264bind_scaler(struct wl_client *client,
4265 void *data, uint32_t version, uint32_t id)
4266{
4267 struct wl_resource *resource;
4268
4269 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004270 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004271 if (resource == NULL) {
4272 wl_client_post_no_memory(client);
4273 return;
4274 }
4275
4276 wl_resource_set_implementation(resource, &scaler_interface,
4277 NULL, NULL);
4278}
4279
4280static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004281destroy_presentation_feedback(struct wl_resource *feedback_resource)
4282{
4283 struct weston_presentation_feedback *feedback;
4284
4285 feedback = wl_resource_get_user_data(feedback_resource);
4286
4287 wl_list_remove(&feedback->link);
4288 free(feedback);
4289}
4290
4291static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004292presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4293{
4294 wl_resource_destroy(resource);
4295}
4296
4297static void
4298presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004299 struct wl_resource *presentation_resource,
4300 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004301 uint32_t callback)
4302{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004303 struct weston_surface *surface;
4304 struct weston_presentation_feedback *feedback;
4305
4306 surface = wl_resource_get_user_data(surface_resource);
4307
Bryce Harringtonde16d892014-11-20 22:21:57 -08004308 feedback = zalloc(sizeof *feedback);
4309 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004310 goto err_calloc;
4311
4312 feedback->resource = wl_resource_create(client,
4313 &presentation_feedback_interface,
4314 1, callback);
4315 if (!feedback->resource)
4316 goto err_create;
4317
4318 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4319 destroy_presentation_feedback);
4320
4321 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4322
4323 return;
4324
4325err_create:
4326 free(feedback);
4327
4328err_calloc:
4329 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004330}
4331
4332static const struct presentation_interface presentation_implementation = {
4333 presentation_destroy,
4334 presentation_feedback
4335};
4336
4337static void
4338bind_presentation(struct wl_client *client,
4339 void *data, uint32_t version, uint32_t id)
4340{
4341 struct weston_compositor *compositor = data;
4342 struct wl_resource *resource;
4343
4344 resource = wl_resource_create(client, &presentation_interface,
4345 MIN(version, 1), id);
4346 if (resource == NULL) {
4347 wl_client_post_no_memory(client);
4348 return;
4349 }
4350
4351 wl_resource_set_implementation(resource, &presentation_implementation,
4352 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004353 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004354}
4355
4356static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004357compositor_bind(struct wl_client *client,
4358 void *data, uint32_t version, uint32_t id)
4359{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004360 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004361 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004362
Jason Ekstranda85118c2013-06-27 20:17:02 -05004363 resource = wl_resource_create(client, &wl_compositor_interface,
4364 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004365 if (resource == NULL) {
4366 wl_client_post_no_memory(client);
4367 return;
4368 }
4369
4370 wl_resource_set_implementation(resource, &compositor_interface,
4371 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004372}
4373
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004374static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004375log_uname(void)
4376{
4377 struct utsname usys;
4378
4379 uname(&usys);
4380
4381 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4382 usys.version, usys.machine);
4383}
4384
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004385WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004386weston_environment_get_fd(const char *env)
4387{
4388 char *e, *end;
4389 int fd, flags;
4390
4391 e = getenv(env);
4392 if (!e)
4393 return -1;
4394 fd = strtol(e, &end, 0);
4395 if (*end != '\0')
4396 return -1;
4397
4398 flags = fcntl(fd, F_GETFD);
4399 if (flags == -1)
4400 return -1;
4401
4402 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4403 unsetenv(env);
4404
4405 return fd;
4406}
4407
Pekka Paalanenb5026542014-11-12 15:09:24 +02004408static void
4409timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4410 uint32_t key, void *data)
4411{
4412 struct weston_compositor *compositor = data;
4413
4414 if (weston_timeline_enabled_)
4415 weston_timeline_close();
4416 else
4417 weston_timeline_open(compositor);
4418}
4419
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004420WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004421weston_compositor_init(struct weston_compositor *ec,
4422 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004423 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004424 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004425{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004426 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004427 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004428 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004429
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004430 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004431 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004432 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004433 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004434 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004435 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004436 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004437 wl_signal_init(&ec->idle_signal);
4438 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004439 wl_signal_init(&ec->show_input_panel_signal);
4440 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004441 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004442 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004443 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004444 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004445 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004446 wl_signal_init(&ec->session_signal);
4447 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004448
Casey Dahlin58ba1372012-04-19 22:50:08 -04004449 ec->output_id_pool = 0;
4450
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004451 if (!wl_global_create(display, &wl_compositor_interface, 3,
4452 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004453 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004454
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004455 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4456 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004457 return -1;
4458
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004459 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004460 ec, bind_scaler))
4461 return -1;
4462
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004463 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4464 ec, bind_presentation))
4465 return -1;
4466
Jason Ekstranda7af7042013-10-12 22:38:11 -05004467 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004468 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004469 wl_list_init(&ec->layer_list);
4470 wl_list_init(&ec->seat_list);
4471 wl_list_init(&ec->output_list);
4472 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004473 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004474 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004475 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004476 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004477 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004478
Xiong Zhang97116532013-10-23 13:58:31 +08004479 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004480 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004481
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004482 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4483 weston_config_section_get_string(s, "keymap_rules",
4484 (char **) &xkb_names.rules, NULL);
4485 weston_config_section_get_string(s, "keymap_model",
4486 (char **) &xkb_names.model, NULL);
4487 weston_config_section_get_string(s, "keymap_layout",
4488 (char **) &xkb_names.layout, NULL);
4489 weston_config_section_get_string(s, "keymap_variant",
4490 (char **) &xkb_names.variant, NULL);
4491 weston_config_section_get_string(s, "keymap_options",
4492 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004493
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004494 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4495 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004496
Jonny Lamb66a41a02014-08-12 14:58:25 +02004497 weston_config_section_get_int(s, "repeat-rate",
4498 &ec->kb_repeat_rate, 40);
4499 weston_config_section_get_int(s, "repeat-delay",
4500 &ec->kb_repeat_delay, 400);
4501
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004502 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004503
4504 wl_data_device_manager_init(ec->wl_display);
4505
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004506 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004507
Daniel Stone725c2c32012-06-22 14:04:36 +01004508 loop = wl_display_get_event_loop(ec->wl_display);
4509 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4510 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4511
4512 ec->input_loop = wl_event_loop_create();
4513
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004514 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4515 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4516
Pekka Paalanenb5026542014-11-12 15:09:24 +02004517 weston_compositor_add_debug_binding(ec, KEY_T,
4518 timeline_key_binding_handler, ec);
4519
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004520 weston_compositor_schedule_repaint(ec);
4521
Daniel Stone725c2c32012-06-22 14:04:36 +01004522 return 0;
4523}
4524
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004525WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004526weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004528 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004529
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004530 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004531 if (ec->input_loop_source)
4532 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004533
Matt Roper361d2ad2011-08-29 13:52:23 -07004534 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004535 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004536 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004537
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004538 if (ec->renderer)
4539 ec->renderer->destroy(ec);
4540
Daniel Stone325fc2d2012-05-30 16:31:58 +01004541 weston_binding_list_destroy_all(&ec->key_binding_list);
4542 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004543 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004544 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004545 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004546
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004547 weston_plane_release(&ec->primary_plane);
4548
Jonas Ådahlc97af922012-03-28 22:36:09 +02004549 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004550
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004551 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004552}
4553
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004554WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004555weston_compositor_exit_with_code(struct weston_compositor *compositor,
4556 int exit_code)
4557{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004558 if (compositor->exit_code == EXIT_SUCCESS)
4559 compositor->exit_code = exit_code;
4560
Frederic Plourdec336f062014-10-29 14:44:33 -04004561 wl_display_terminate(compositor->wl_display);
4562}
4563
4564WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004565weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4566 const struct weston_pointer_grab_interface *interface)
4567{
4568 struct weston_seat *seat;
4569
4570 ec->default_pointer_grab = interface;
4571 wl_list_for_each(seat, &ec->seat_list, link) {
4572 if (seat->pointer) {
4573 weston_pointer_set_default_grab(seat->pointer,
4574 interface);
4575 }
4576 }
4577}
4578
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004579WL_EXPORT int
4580weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4581 clockid_t clk_id)
4582{
4583 struct timespec ts;
4584
4585 if (clock_gettime(clk_id, &ts) < 0)
4586 return -1;
4587
4588 compositor->presentation_clock = clk_id;
4589
4590 return 0;
4591}
4592
4593/*
4594 * For choosing the software clock, when the display hardware or API
4595 * does not expose a compatible presentation timestamp.
4596 */
4597WL_EXPORT int
4598weston_compositor_set_presentation_clock_software(
4599 struct weston_compositor *compositor)
4600{
4601 /* In order of preference */
4602 static const clockid_t clocks[] = {
4603 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4604 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4605 CLOCK_MONOTONIC, /* no jumps, may crawl */
4606 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4607 CLOCK_REALTIME /* may jump and crawl */
4608 };
4609 unsigned i;
4610
4611 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4612 if (weston_compositor_set_presentation_clock(compositor,
4613 clocks[i]) == 0)
4614 return 0;
4615
4616 weston_log("Error: no suitable presentation clock available.\n");
4617
4618 return -1;
4619}
4620
Pekka Paalanen662f3842015-03-18 12:17:26 +02004621/** Read the current time from the Presentation clock
4622 *
4623 * \param compositor
4624 * \param ts[out] The current time.
4625 *
4626 * \note Reading the current time in user space is always imprecise to some
4627 * degree.
4628 *
4629 * This function is never meant to fail. If reading the clock does fail,
4630 * an error message is logged and a zero time is returned. Callers are not
4631 * supposed to detect or react to failures.
4632 */
4633WL_EXPORT void
4634weston_compositor_read_presentation_clock(
4635 const struct weston_compositor *compositor,
4636 struct timespec *ts)
4637{
4638 static bool warned;
4639 int ret;
4640
4641 ret = clock_gettime(compositor->presentation_clock, ts);
4642 if (ret < 0) {
4643 ts->tv_sec = 0;
4644 ts->tv_nsec = 0;
4645
4646 if (!warned)
4647 weston_log("Error: failure to read "
4648 "the presentation clock %#x: '%m' (%d)\n",
4649 compositor->presentation_clock, errno);
4650 warned = true;
4651 }
4652}
4653
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004654WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004655weston_version(int *major, int *minor, int *micro)
4656{
4657 *major = WESTON_VERSION_MAJOR;
4658 *minor = WESTON_VERSION_MINOR;
4659 *micro = WESTON_VERSION_MICRO;
4660}
4661
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004662static const char *
4663clock_name(clockid_t clk_id)
4664{
4665 static const char *names[] = {
4666 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4667 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4668 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4669 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4670 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4671 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4672 };
4673
4674 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4675 return "unknown";
4676
4677 return names[clk_id];
4678}
4679
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004680static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004681 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004682 const char *desc;
4683} capability_strings[] = {
4684 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004685 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004686};
4687
4688static void
4689weston_compositor_log_capabilities(struct weston_compositor *compositor)
4690{
4691 unsigned i;
4692 int yes;
4693
4694 weston_log("Compositor capabilities:\n");
4695 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4696 yes = compositor->capabilities & capability_strings[i].bit;
4697 weston_log_continue(STAMP_SPACE "%s %s\n",
4698 capability_strings[i].desc,
4699 yes ? "yes" : "no");
4700 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004701
4702 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4703 clock_name(compositor->presentation_clock),
4704 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004705}
4706
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004707static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004708{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004709 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004710
Martin Minarik6d118362012-06-07 18:01:59 +02004711 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004712 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004713
4714 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004715}
4716
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004717#ifdef HAVE_LIBUNWIND
4718
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004719static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004720print_backtrace(void)
4721{
4722 unw_cursor_t cursor;
4723 unw_context_t context;
4724 unw_word_t off;
4725 unw_proc_info_t pip;
4726 int ret, i = 0;
4727 char procname[256];
4728 const char *filename;
4729 Dl_info dlinfo;
4730
4731 pip.unwind_info = NULL;
4732 ret = unw_getcontext(&context);
4733 if (ret) {
4734 weston_log("unw_getcontext: %d\n", ret);
4735 return;
4736 }
4737
4738 ret = unw_init_local(&cursor, &context);
4739 if (ret) {
4740 weston_log("unw_init_local: %d\n", ret);
4741 return;
4742 }
4743
4744 ret = unw_step(&cursor);
4745 while (ret > 0) {
4746 ret = unw_get_proc_info(&cursor, &pip);
4747 if (ret) {
4748 weston_log("unw_get_proc_info: %d\n", ret);
4749 break;
4750 }
4751
4752 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4753 if (ret && ret != -UNW_ENOMEM) {
4754 if (ret != -UNW_EUNSPEC)
4755 weston_log("unw_get_proc_name: %d\n", ret);
4756 procname[0] = '?';
4757 procname[1] = 0;
4758 }
4759
4760 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4761 *dlinfo.dli_fname)
4762 filename = dlinfo.dli_fname;
4763 else
4764 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004765
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004766 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4767 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4768
4769 ret = unw_step(&cursor);
4770 if (ret < 0)
4771 weston_log("unw_step: %d\n", ret);
4772 }
4773}
4774
4775#else
4776
4777static void
4778print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004779{
4780 void *buffer[32];
4781 int i, count;
4782 Dl_info info;
4783
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004784 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4785 for (i = 0; i < count; i++) {
4786 dladdr(buffer[i], &info);
4787 weston_log(" [%016lx] %s (%s)\n",
4788 (long) buffer[i],
4789 info.dli_sname ? info.dli_sname : "--",
4790 info.dli_fname);
4791 }
4792}
4793
4794#endif
4795
4796static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004797on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004798{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004799 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004800 * then call the backend restore function, which will switch
4801 * back to the vt we launched from or ungrab X etc and then
4802 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004803 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004804 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004805 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004806
Peter Maatmane5b42e42013-03-27 22:38:53 +01004807 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004808
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004809 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004810
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004811 segv_compositor->restore(segv_compositor);
4812
4813 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004814}
4815
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004816WL_EXPORT void *
4817weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004818{
4819 char path[PATH_MAX];
4820 void *module, *init;
4821
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004822 if (name == NULL)
4823 return NULL;
4824
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004825 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004826 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004827 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004828 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004829
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004830 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4831 if (module) {
4832 weston_log("Module '%s' already loaded\n", path);
4833 dlclose(module);
4834 return NULL;
4835 }
4836
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004837 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004838 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004839 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004840 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004841 return NULL;
4842 }
4843
4844 init = dlsym(module, entrypoint);
4845 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004846 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004847 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004848 return NULL;
4849 }
4850
4851 return init;
4852}
4853
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004854static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004855load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004856 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004857{
4858 const char *p, *end;
4859 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004860 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004861 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004862
4863 if (modules == NULL)
4864 return 0;
4865
4866 p = modules;
4867 while (*p) {
4868 end = strchrnul(p, ',');
4869 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004870 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004871 if (!module_init)
4872 return -1;
4873 if (module_init(ec, argc, argv) < 0)
4874 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004875 p = end;
4876 while (*p == ',')
4877 p++;
4878
4879 }
4880
4881 return 0;
4882}
4883
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004884static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004885 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4886
4887static const char xdg_wrong_message[] =
4888 "fatal: environment variable XDG_RUNTIME_DIR\n"
4889 "is set to \"%s\", which is not a directory.\n";
4890
4891static const char xdg_wrong_mode_message[] =
4892 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004893 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4894 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004895
4896static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004897 "Refer to your distribution on how to get it, or\n"
4898 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4899 "on how to implement it.\n";
4900
Martin Minarik37032f82012-06-18 20:15:18 +02004901static void
4902verify_xdg_runtime_dir(void)
4903{
4904 char *dir = getenv("XDG_RUNTIME_DIR");
4905 struct stat s;
4906
4907 if (!dir) {
4908 weston_log(xdg_error_message);
4909 weston_log_continue(xdg_detail_message);
4910 exit(EXIT_FAILURE);
4911 }
4912
4913 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4914 weston_log(xdg_wrong_message, dir);
4915 weston_log_continue(xdg_detail_message);
4916 exit(EXIT_FAILURE);
4917 }
4918
4919 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4920 weston_log(xdg_wrong_mode_message,
4921 dir, s.st_mode & 0777, s.st_uid);
4922 weston_log_continue(xdg_detail_message);
4923 }
4924}
4925
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004926static int
4927usage(int error_code)
4928{
4929 fprintf(stderr,
4930 "Usage: weston [OPTIONS]\n\n"
4931 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4932 "Weston supports multiple backends, and depending on which backend is in use\n"
4933 "different options will be accepted.\n\n"
4934
4935
4936 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004937 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004938 " -B, --backend=MODULE\tBackend module, one of\n"
4939#if defined(BUILD_DRM_COMPOSITOR)
4940 "\t\t\t\tdrm-backend.so\n"
4941#endif
4942#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004943 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004944#endif
4945#if defined(BUILD_X11_COMPOSITOR)
4946 "\t\t\t\tx11-backend.so\n"
4947#endif
4948#if defined(BUILD_WAYLAND_COMPOSITOR)
4949 "\t\t\t\twayland-backend.so\n"
4950#endif
4951#if defined(BUILD_RDP_COMPOSITOR)
4952 "\t\t\t\trdp-backend.so\n"
4953#endif
4954#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4955 "\t\t\t\trpi-backend.so\n"
4956#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004957 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004958 " -S, --socket=NAME\tName of socket to listen on\n"
4959 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004960 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004961 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004962 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004963 " -h, --help\t\tThis help message\n\n");
4964
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004965#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004966 fprintf(stderr,
4967 "Options for drm-backend.so:\n\n"
4968 " --connector=ID\tBring up only this connector\n"
4969 " --seat=SEAT\t\tThe seat that weston should run on\n"
4970 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004971 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004972 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004973#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004974
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004975#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004976 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004977 "Options for fbdev-backend.so:\n\n"
4978 " --tty=TTY\t\tThe tty to use\n"
4979 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004980#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004981
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004982#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004983 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004984 "Options for x11-backend.so:\n\n"
4985 " --width=WIDTH\t\tWidth of X window\n"
4986 " --height=HEIGHT\tHeight of X window\n"
4987 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004988 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004989 " --output-count=COUNT\tCreate multiple outputs\n"
4990 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004991#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004992
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004993#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004994 fprintf(stderr,
4995 "Options for wayland-backend.so:\n\n"
4996 " --width=WIDTH\t\tWidth of Wayland surface\n"
4997 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004998 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004999 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005000 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005001 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005002 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005003 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005004#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005005
Pekka Paalanene31e0532013-05-22 18:03:07 +03005006#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5007 fprintf(stderr,
5008 "Options for rpi-backend.so:\n\n"
5009 " --tty=TTY\t\tThe tty to use\n"
5010 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5011 " --transform=TR\tThe output transformation, TR is one of:\n"
5012 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005013 " --opaque-regions\tEnable support for opaque regions, can be "
5014 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005015 "\n");
5016#endif
5017
Hardeningc077a842013-07-08 00:51:35 +02005018#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005019 fprintf(stderr,
5020 "Options for rdp-backend.so:\n\n"
5021 " --width=WIDTH\t\tWidth of desktop\n"
5022 " --height=HEIGHT\tHeight of desktop\n"
5023 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5024 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005025 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005026 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5027 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5028 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5029 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5030 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005031#endif
5032
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005033#if defined(BUILD_HEADLESS_COMPOSITOR)
5034 fprintf(stderr,
5035 "Options for headless-backend.so:\n\n"
5036 " --width=WIDTH\t\tWidth of memory surface\n"
5037 " --height=HEIGHT\tHeight of memory surface\n"
5038 " --transform=TR\tThe output transformation, TR is one of:\n"
5039 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5040 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5041#endif
5042
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005043 exit(error_code);
5044}
5045
Peter Maatmane5b42e42013-03-27 22:38:53 +01005046static void
5047catch_signals(void)
5048{
5049 struct sigaction action;
5050
5051 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5052 action.sa_sigaction = on_caught_signal;
5053 sigemptyset(&action.sa_mask);
5054 sigaction(SIGSEGV, &action, NULL);
5055 sigaction(SIGABRT, &action, NULL);
5056}
5057
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005058static void
5059handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5060{
5061 struct wl_client *client = data;
5062
5063 weston_log("Primary client died. Closing...\n");
5064
5065 wl_display_terminate(wl_client_get_display(client));
5066}
5067
Ryo Munakatad8deff62014-09-06 07:32:05 +09005068static char *
5069weston_choose_default_backend(void)
5070{
5071 char *backend = NULL;
5072
5073 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5074 backend = strdup("wayland-backend.so");
5075 else if (getenv("DISPLAY"))
5076 backend = strdup("x11-backend.so");
5077 else
5078 backend = strdup(WESTON_NATIVE_BACKEND);
5079
5080 return backend;
5081}
5082
5083static int
5084weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5085{
5086 if (socket_name) {
5087 if (wl_display_add_socket(display, socket_name)) {
5088 weston_log("fatal: failed to add socket: %m\n");
5089 return -1;
5090 }
5091 } else {
5092 socket_name = wl_display_add_socket_auto(display);
5093 if (!socket_name) {
5094 weston_log("fatal: failed to add socket: %m\n");
5095 return -1;
5096 }
5097 }
5098
5099 setenv("WAYLAND_DISPLAY", socket_name, 1);
5100
5101 return 0;
5102}
5103
Derek Foreman64a3df02014-10-23 12:24:18 -05005104static const struct { const char *name; uint32_t token; } transforms[] = {
5105 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5106 { "90", WL_OUTPUT_TRANSFORM_90 },
5107 { "180", WL_OUTPUT_TRANSFORM_180 },
5108 { "270", WL_OUTPUT_TRANSFORM_270 },
5109 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5110 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5111 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5112 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5113};
5114
5115WL_EXPORT int
5116weston_parse_transform(const char *transform, uint32_t *out)
5117{
5118 unsigned int i;
5119
5120 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5121 if (strcmp(transforms[i].name, transform) == 0) {
5122 *out = transforms[i].token;
5123 return 0;
5124 }
5125
5126 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5127 return -1;
5128}
5129
5130WL_EXPORT const char *
5131weston_transform_to_string(uint32_t output_transform)
5132{
5133 unsigned int i;
5134
5135 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5136 if (transforms[i].token == output_transform)
5137 return transforms[i].name;
5138
5139 return "<illegal value>";
5140}
5141
5142
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005143int main(int argc, char *argv[])
5144{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005145 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005146 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005147 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005148 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005149 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005150 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005151 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005152 int *argc, char *argv[],
5153 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005154 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005155 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005156 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005157 char *modules = NULL;
5158 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005159 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005160 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005161 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005162 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005163 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005164 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005165 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005166 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005167 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005168 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005169 struct wl_client *primary_client;
5170 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005171 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005172
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005173 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005174 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5175 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005176 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5177 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005178 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005179 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005180 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005181 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005182 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005183 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005184
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005185 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005186
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005187 if (help)
5188 usage(EXIT_SUCCESS);
5189
Scott Moreau12245142012-08-29 15:15:58 -06005190 if (version) {
5191 printf(PACKAGE_STRING "\n");
5192 return EXIT_SUCCESS;
5193 }
5194
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005195 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005196
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005197 weston_log("%s\n"
5198 STAMP_SPACE "%s\n"
5199 STAMP_SPACE "Bug reports to: %s\n"
5200 STAMP_SPACE "Build: %s\n",
5201 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005202 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005203 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005204
Martin Minarik37032f82012-06-18 20:15:18 +02005205 verify_xdg_runtime_dir();
5206
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005207 display = wl_display_create();
5208
Tiago Vignatti2116b892011-08-08 05:52:59 -07005209 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005210 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5211 display);
5212 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5213 display);
5214 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5215 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005216
5217 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005218 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5219 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005220
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305221 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
5222 ret = EXIT_FAILURE;
5223 goto out_signals;
5224 }
5225
Pekka Paalanen588bee12014-05-07 16:26:25 +03005226 if (noconfig == 0)
5227 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005228 if (config != NULL) {
5229 weston_log("Using config file '%s'\n",
5230 weston_config_get_full_path(config));
5231 } else {
5232 weston_log("Starting with no config file.\n");
5233 }
5234 section = weston_config_get_section(config, "core", NULL, NULL);
5235
Ryo Munakata03faed22014-09-06 07:32:51 +09005236 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005237 weston_config_section_get_string(section, "backend", &backend,
5238 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005239 if (!backend)
5240 backend = weston_choose_default_backend();
5241 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005242
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005243 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305244 if (!backend_init) {
5245 ret = EXIT_FAILURE;
5246 goto out_signals;
5247 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05005248
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005249 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005250 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005251 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305252 ret = EXIT_FAILURE;
5253 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005254 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005255
Peter Maatmane5b42e42013-03-27 22:38:53 +01005256 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005257 segv_compositor = ec;
5258
Frederic Plourde4a84c832014-10-30 15:06:34 -04005259 if (idle_time < 0)
5260 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5261 if (idle_time < 0)
5262 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005263 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005264 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005265 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005266
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005267 for (i = 1; i < argc; i++)
5268 weston_log("fatal: unhandled option: %s\n", argv[i]);
5269 if (argc > 1) {
5270 ret = EXIT_FAILURE;
5271 goto out;
5272 }
5273
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005274 weston_compositor_log_capabilities(ec);
5275
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005276 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5277 if (server_socket) {
5278 weston_log("Running with single client\n");
5279 fd = strtol(server_socket, &end, 0);
5280 if (*end != '\0')
5281 fd = -1;
5282 } else {
5283 fd = -1;
5284 }
5285
5286 if (fd != -1) {
5287 primary_client = wl_client_create(display, fd);
5288 if (!primary_client) {
5289 weston_log("fatal: failed to add client: %m\n");
5290 ret = EXIT_FAILURE;
5291 goto out;
5292 }
5293 primary_client_destroyed.notify =
5294 handle_primary_client_destroyed;
5295 wl_client_add_destroy_listener(primary_client,
5296 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005297 } else if (weston_create_listening_socket(display, socket_name)) {
5298 ret = EXIT_FAILURE;
5299 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005300 }
5301
Ryo Munakata03faed22014-09-06 07:32:51 +09005302 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005303 weston_config_section_get_string(section, "shell", &shell,
5304 "desktop-shell.so");
5305
Ryo Munakata03faed22014-09-06 07:32:51 +09005306 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005307 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005308
5309 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005310 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005311 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005312
5313 if (load_modules(ec, option_modules, &argc, argv) < 0)
5314 goto out;
5315
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005316 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5317 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5318 if (numlock_on) {
5319 wl_list_for_each(seat, &ec->seat_list, link) {
5320 if (seat->keyboard)
5321 weston_keyboard_set_locks(seat->keyboard,
5322 WESTON_NUM_LOCK,
5323 WESTON_NUM_LOCK);
5324 }
5325 }
5326
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005327 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005328
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005329 wl_display_run(display);
5330
Frederic Plourdec336f062014-10-29 14:44:33 -04005331 /* Allow for setting return exit code after
5332 * wl_display_run returns normally. This is
5333 * useful for devs/testers and automated tests
5334 * that want to indicate failure status to
5335 * testing infrastructure above
5336 */
5337 ret = ec->exit_code;
5338
Ryo Munakata03faed22014-09-06 07:32:51 +09005339out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005340 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005341 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005342
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005343 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005344
Daniel Stone855028f2012-05-01 20:37:10 +01005345 weston_compositor_xkb_destroy(ec);
5346
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005347 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305348
5349out_signals:
5350 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5351 if (signals[i])
5352 wl_event_source_remove(signals[i]);
5353
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005354 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005355
Martin Minarik19e6f262012-06-07 13:08:46 +02005356 weston_log_file_close();
5357
Ryo Munakata03faed22014-09-06 07:32:51 +09005358 free(backend);
5359 free(shell);
5360 free(socket_name);
5361 free(option_modules);
5362 free(log);
5363 free(modules);
5364
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005365 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005366}