blob: 3f6aa7d627b4430debdd53e96c358ed73332b72a [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
Pekka Paalanen0513a952014-05-21 16:17:27 +030065#define DEFAULT_REPAINT_WINDOW 7 /* milliseconds */
66
67#define NSEC_PER_SEC 1000000000
68
69static void
70timespec_sub(struct timespec *r,
71 const struct timespec *a, const struct timespec *b)
72{
73 r->tv_sec = a->tv_sec - b->tv_sec;
74 r->tv_nsec = a->tv_nsec - b->tv_nsec;
75 if (r->tv_nsec < 0) {
76 r->tv_sec--;
77 r->tv_nsec += NSEC_PER_SEC;
78 }
79}
80
81static int64_t
82timespec_to_nsec(const struct timespec *a)
83{
84 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
85}
86
Kristian Høgsberg27da5382011-06-21 17:32:25 -040087static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040088static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040089
90static int
91sigchld_handler(int signal_number, void *data)
92{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050093 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040094 int status;
95 pid_t pid;
96
Pekka Paalanen23ade622014-08-27 13:31:26 +030097 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
98 wl_list_for_each(p, &child_process_list, link) {
99 if (p->pid == pid)
100 break;
101 }
Bill Spitzak027b9622012-03-17 15:22:03 -0700102
Pekka Paalanen23ade622014-08-27 13:31:26 +0300103 if (&p->link == &child_process_list) {
104 weston_log("unknown child process exited\n");
105 continue;
106 }
107
108 wl_list_remove(&p->link);
109 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400110 }
111
Pekka Paalanen23ade622014-08-27 13:31:26 +0300112 if (pid < 0 && errno != ECHILD)
113 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400114
115 return 1;
116}
117
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200118static void
Alexander Larsson0b135062013-05-28 16:23:36 +0200119weston_output_transform_scale_init(struct weston_output *output,
120 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200121
Rob Bradford27b17932013-06-26 18:08:46 +0100122static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500123weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100124
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600125static void weston_mode_switch_finish(struct weston_output *output,
126 int mode_changed,
127 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800128{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200129 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200130 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200131 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600132 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200133
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200134 pixman_region32_init(&old_output_region);
135 pixman_region32_copy(&old_output_region, &output->region);
136
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200137 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200138 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200139
140 pixman_region32_init(&output->previous_damage);
141 pixman_region32_init_rect(&output->region, output->x, output->y,
142 output->width, output->height);
143
144 weston_output_update_matrix(output);
145
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200146 /* If a pointer falls outside the outputs new geometry, move it to its
147 * lower-right corner */
148 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400149 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200150 int32_t x, y;
151
152 if (!pointer)
153 continue;
154
155 x = wl_fixed_to_int(pointer->x);
156 y = wl_fixed_to_int(pointer->y);
157
158 if (!pixman_region32_contains_point(&old_output_region,
159 x, y, NULL) ||
160 pixman_region32_contains_point(&output->region,
161 x, y, NULL))
162 continue;
163
164 if (x >= output->x + output->width)
165 x = output->x + output->width - 1;
166 if (y >= output->y + output->height)
167 y = output->y + output->height - 1;
168
169 pointer->x = wl_fixed_from_int(x);
170 pointer->y = wl_fixed_from_int(y);
171 }
172
173 pixman_region32_fini(&old_output_region);
174
Derek Foremandd4cd332014-11-10 10:29:59 -0600175 if (!mode_changed && !scale_changed)
176 return;
177
Hardening57388e42013-09-18 23:56:36 +0200178 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600179 wl_resource_for_each(resource, &output->resource_list) {
180 if (mode_changed) {
181 wl_output_send_mode(resource,
182 output->current_mode->flags,
183 output->current_mode->width,
184 output->current_mode->height,
185 output->current_mode->refresh);
186 }
Hardening57388e42013-09-18 23:56:36 +0200187
Derek Foreman41bdc272014-11-05 13:26:57 -0600188 version = wl_resource_get_version(resource);
189 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200191
Derek Foreman41bdc272014-11-05 13:26:57 -0600192 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
193 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600194 }
195}
196
197WL_EXPORT int
198weston_output_mode_set_native(struct weston_output *output,
199 struct weston_mode *mode,
200 int32_t scale)
201{
202 int ret;
203 int mode_changed = 0, scale_changed = 0;
204
205 if (!output->switch_mode)
206 return -1;
207
208 if (!output->original_mode) {
209 mode_changed = 1;
210 ret = output->switch_mode(output, mode);
211 if (ret < 0)
212 return ret;
213 if (output->current_scale != scale) {
214 scale_changed = 1;
215 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200216 }
217 }
218
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600219 output->native_mode = mode;
220 output->native_scale = scale;
221
222 weston_mode_switch_finish(output, mode_changed, scale_changed);
223
224 return 0;
225}
226
227WL_EXPORT int
228weston_output_mode_switch_to_native(struct weston_output *output)
229{
230 int ret;
231 int mode_changed = 0, scale_changed = 0;
232
233 if (!output->switch_mode)
234 return -1;
235
236 if (!output->original_mode) {
237 weston_log("already in the native mode\n");
238 return -1;
239 }
240 /* the non fullscreen clients haven't seen a mode set since we
241 * switched into a temporary, so we need to notify them if the
242 * mode at that time is different from the native mode now.
243 */
244 mode_changed = (output->original_mode != output->native_mode);
245 scale_changed = (output->original_scale != output->native_scale);
246
247 ret = output->switch_mode(output, output->native_mode);
248 if (ret < 0)
249 return ret;
250
251 output->current_scale = output->native_scale;
252
253 output->original_mode = NULL;
254 output->original_scale = 0;
255
256 weston_mode_switch_finish(output, mode_changed, scale_changed);
257
258 return 0;
259}
260
261WL_EXPORT int
262weston_output_mode_switch_to_temporary(struct weston_output *output,
263 struct weston_mode *mode,
264 int32_t scale)
265{
266 int ret;
267
268 if (!output->switch_mode)
269 return -1;
270
271 /* original_mode is the last mode non full screen clients have seen,
272 * so we shouldn't change it if we already have one set.
273 */
274 if (!output->original_mode) {
275 output->original_mode = output->native_mode;
276 output->original_scale = output->native_scale;
277 }
278 ret = output->switch_mode(output, mode);
279 if (ret < 0)
280 return ret;
281
282 output->current_scale = scale;
283
284 weston_mode_switch_finish(output, 0, 0);
285
286 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800287}
288
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400289WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500290weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400291{
292 wl_list_insert(&child_process_list, &process->link);
293}
294
Benjamin Franzke06286262011-05-06 19:12:33 +0200295static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296child_client_exec(int sockfd, const char *path)
297{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200300 sigset_t allsigs;
301
302 /* do not give our signal mask to the new process */
303 sigfillset(&allsigs);
304 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200305
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100306 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
307 if (seteuid(getuid()) == -1) {
308 weston_log("compositor: failed seteuid\n");
309 return;
310 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500311
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500312 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
313 * non-CLOEXEC fd to pass through exec. */
314 clientfd = dup(sockfd);
315 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200316 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500317 return;
318 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200319
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500320 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200321 setenv("WAYLAND_SOCKET", s, 1);
322
323 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200324 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200325 path);
326}
327
328WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500329weston_client_launch(struct weston_compositor *compositor,
330 struct weston_process *proc,
331 const char *path,
332 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200333{
334 int sv[2];
335 pid_t pid;
336 struct wl_client *client;
337
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300338 weston_log("launching '%s'\n", path);
339
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300340 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200341 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200342 "socketpair failed while launching '%s': %m\n",
343 path);
344 return NULL;
345 }
346
347 pid = fork();
348 if (pid == -1) {
349 close(sv[0]);
350 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200351 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200352 "fork failed while launching '%s': %m\n", path);
353 return NULL;
354 }
355
356 if (pid == 0) {
357 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700358 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200359 }
360
361 close(sv[1]);
362
363 client = wl_client_create(compositor->wl_display, sv[0]);
364 if (!client) {
365 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200366 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200367 "wl_client_create failed while launching '%s'.\n",
368 path);
369 return NULL;
370 }
371
372 proc->pid = pid;
373 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500374 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200375
376 return client;
377}
378
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300379struct process_info {
380 struct weston_process proc;
381 char *path;
382};
383
384static void
385process_handle_sigchld(struct weston_process *process, int status)
386{
387 struct process_info *pinfo =
388 container_of(process, struct process_info, proc);
389
390 /*
391 * There are no guarantees whether this runs before or after
392 * the wl_client destructor.
393 */
394
395 if (WIFEXITED(status)) {
396 weston_log("%s exited with status %d\n", pinfo->path,
397 WEXITSTATUS(status));
398 } else if (WIFSIGNALED(status)) {
399 weston_log("%s died on signal %d\n", pinfo->path,
400 WTERMSIG(status));
401 } else {
402 weston_log("%s disappeared\n", pinfo->path);
403 }
404
405 free(pinfo->path);
406 free(pinfo);
407}
408
409WL_EXPORT struct wl_client *
410weston_client_start(struct weston_compositor *compositor, const char *path)
411{
412 struct process_info *pinfo;
413 struct wl_client *client;
414
415 pinfo = zalloc(sizeof *pinfo);
416 if (!pinfo)
417 return NULL;
418
419 pinfo->path = strdup(path);
420 if (!pinfo->path)
421 goto out_free;
422
423 client = weston_client_launch(compositor, &pinfo->proc, path,
424 process_handle_sigchld);
425 if (!client)
426 goto out_str;
427
428 return client;
429
430out_str:
431 free(pinfo->path);
432
433out_free:
434 free(pinfo);
435
436 return NULL;
437}
438
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200439static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300440region_init_infinite(pixman_region32_t *region)
441{
442 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
443 UINT32_MAX, UINT32_MAX);
444}
445
Pekka Paalanene67858b2013-04-25 13:57:42 +0300446static struct weston_subsurface *
447weston_surface_to_subsurface(struct weston_surface *surface);
448
Jason Ekstranda7af7042013-10-12 22:38:11 -0500449WL_EXPORT struct weston_view *
450weston_view_create(struct weston_surface *surface)
451{
452 struct weston_view *view;
453
Bryce Harringtonde16d892014-11-20 22:21:57 -0800454 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500455 if (view == NULL)
456 return NULL;
457
458 view->surface = surface;
459
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 /* Assign to surface */
461 wl_list_insert(&surface->views, &view->surface_link);
462
463 wl_signal_init(&view->destroy_signal);
464 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300465 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500466
Jason Ekstranda7af7042013-10-12 22:38:11 -0500467 pixman_region32_init(&view->clip);
468
469 view->alpha = 1.0;
470 pixman_region32_init(&view->transform.opaque);
471
472 wl_list_init(&view->geometry.transformation_list);
473 wl_list_insert(&view->geometry.transformation_list,
474 &view->transform.position.link);
475 weston_matrix_init(&view->transform.position.matrix);
476 wl_list_init(&view->geometry.child_list);
Pekka Paalanen380adf52015-02-16 14:39:11 +0200477 pixman_region32_init(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500478 pixman_region32_init(&view->transform.boundingbox);
479 view->transform.dirty = 1;
480
Jason Ekstranda7af7042013-10-12 22:38:11 -0500481 return view;
482}
483
Jason Ekstrand108865d2014-06-26 10:04:49 -0700484struct weston_frame_callback {
485 struct wl_resource *resource;
486 struct wl_list link;
487};
488
Pekka Paalanen133e4392014-09-23 22:08:46 -0400489struct weston_presentation_feedback {
490 struct wl_resource *resource;
491
492 /* XXX: could use just wl_resource_get_link() instead */
493 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200494
495 /* The per-surface feedback flags */
496 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400497};
498
499static void
500weston_presentation_feedback_discard(
501 struct weston_presentation_feedback *feedback)
502{
503 presentation_feedback_send_discarded(feedback->resource);
504 wl_resource_destroy(feedback->resource);
505}
506
507static void
508weston_presentation_feedback_discard_list(struct wl_list *list)
509{
510 struct weston_presentation_feedback *feedback, *tmp;
511
512 wl_list_for_each_safe(feedback, tmp, list, link)
513 weston_presentation_feedback_discard(feedback);
514}
515
516static void
517weston_presentation_feedback_present(
518 struct weston_presentation_feedback *feedback,
519 struct weston_output *output,
520 uint32_t refresh_nsec,
521 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200522 uint64_t seq,
523 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400524{
525 struct wl_client *client = wl_resource_get_client(feedback->resource);
526 struct wl_resource *o;
527 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400528
529 wl_resource_for_each(o, &output->resource_list) {
530 if (wl_resource_get_client(o) != client)
531 continue;
532
533 presentation_feedback_send_sync_output(feedback->resource, o);
534 }
535
536 secs = ts->tv_sec;
537 presentation_feedback_send_presented(feedback->resource,
538 secs >> 32, secs & 0xffffffff,
539 ts->tv_nsec,
540 refresh_nsec,
541 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200542 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400543 wl_resource_destroy(feedback->resource);
544}
545
546static void
547weston_presentation_feedback_present_list(struct wl_list *list,
548 struct weston_output *output,
549 uint32_t refresh_nsec,
550 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200551 uint64_t seq,
552 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400553{
554 struct weston_presentation_feedback *feedback, *tmp;
555
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200556 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
557 wl_list_empty(list));
558
Pekka Paalanen133e4392014-09-23 22:08:46 -0400559 wl_list_for_each_safe(feedback, tmp, list, link)
560 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200561 refresh_nsec, ts, seq,
562 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400563}
564
Jason Ekstrand7b982072014-05-20 14:33:03 -0500565static void
566surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
567{
568 struct weston_surface_state *state =
569 container_of(listener, struct weston_surface_state,
570 buffer_destroy_listener);
571
572 state->buffer = NULL;
573}
574
575static void
576weston_surface_state_init(struct weston_surface_state *state)
577{
578 state->newly_attached = 0;
579 state->buffer = NULL;
580 state->buffer_destroy_listener.notify =
581 surface_state_handle_buffer_destroy;
582 state->sx = 0;
583 state->sy = 0;
584
585 pixman_region32_init(&state->damage);
586 pixman_region32_init(&state->opaque);
587 region_init_infinite(&state->input);
588
589 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400590 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500591
592 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
593 state->buffer_viewport.buffer.scale = 1;
594 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
595 state->buffer_viewport.surface.width = -1;
596 state->buffer_viewport.changed = 0;
597}
598
599static void
600weston_surface_state_fini(struct weston_surface_state *state)
601{
602 struct weston_frame_callback *cb, *next;
603
604 wl_list_for_each_safe(cb, next,
605 &state->frame_callback_list, link)
606 wl_resource_destroy(cb->resource);
607
Pekka Paalanen133e4392014-09-23 22:08:46 -0400608 weston_presentation_feedback_discard_list(&state->feedback_list);
609
Jason Ekstrand7b982072014-05-20 14:33:03 -0500610 pixman_region32_fini(&state->input);
611 pixman_region32_fini(&state->opaque);
612 pixman_region32_fini(&state->damage);
613
614 if (state->buffer)
615 wl_list_remove(&state->buffer_destroy_listener.link);
616 state->buffer = NULL;
617}
618
619static void
620weston_surface_state_set_buffer(struct weston_surface_state *state,
621 struct weston_buffer *buffer)
622{
623 if (state->buffer == buffer)
624 return;
625
626 if (state->buffer)
627 wl_list_remove(&state->buffer_destroy_listener.link);
628 state->buffer = buffer;
629 if (state->buffer)
630 wl_signal_add(&state->buffer->destroy_signal,
631 &state->buffer_destroy_listener);
632}
633
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500634WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500635weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400638
Bryce Harringtonde16d892014-11-20 22:21:57 -0800639 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400640 if (surface == NULL)
641 return NULL;
642
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500643 wl_signal_init(&surface->destroy_signal);
644
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500645 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200646 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400647
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200648 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
649 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200650 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
651 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500652
653 weston_surface_state_init(&surface->pending);
654
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400655 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500656 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300657 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400658
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659 wl_list_init(&surface->views);
660
661 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400662 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500663
Pekka Paalanene67858b2013-04-25 13:57:42 +0300664 wl_list_init(&surface->subsurface_list);
665 wl_list_init(&surface->subsurface_list_pending);
666
Jason Ekstrand1e059042014-10-16 10:55:19 -0500667 weston_matrix_init(&surface->buffer_to_surface_matrix);
668 weston_matrix_init(&surface->surface_to_buffer_matrix);
669
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400670 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500671}
672
Alex Wu8811bf92012-02-28 18:07:54 +0800673WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500674weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200675 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500676{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100677 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500678}
679
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400680WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500681weston_view_to_global_float(struct weston_view *view,
682 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200683{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500684 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200685 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
686
Jason Ekstranda7af7042013-10-12 22:38:11 -0500687 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200688
689 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200690 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700691 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200692 v.f[3]);
693 *x = 0;
694 *y = 0;
695 return;
696 }
697
698 *x = v.f[0] / v.f[3];
699 *y = v.f[1] / v.f[3];
700 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500701 *x = sx + view->geometry.x;
702 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200703 }
704}
705
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500706WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200707weston_transformed_coord(int width, int height,
708 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200709 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200710 float sx, float sy, float *bx, float *by)
711{
712 switch (transform) {
713 case WL_OUTPUT_TRANSFORM_NORMAL:
714 default:
715 *bx = sx;
716 *by = sy;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED:
719 *bx = width - sx;
720 *by = sy;
721 break;
722 case WL_OUTPUT_TRANSFORM_90:
723 *bx = height - sy;
724 *by = sx;
725 break;
726 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
727 *bx = height - sy;
728 *by = width - sx;
729 break;
730 case WL_OUTPUT_TRANSFORM_180:
731 *bx = width - sx;
732 *by = height - sy;
733 break;
734 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
735 *bx = sx;
736 *by = height - sy;
737 break;
738 case WL_OUTPUT_TRANSFORM_270:
739 *bx = sy;
740 *by = width - sx;
741 break;
742 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
743 *bx = sy;
744 *by = sx;
745 break;
746 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200747
748 *bx *= scale;
749 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200750}
751
752WL_EXPORT pixman_box32_t
753weston_transformed_rect(int width, int height,
754 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200755 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200756 pixman_box32_t rect)
757{
758 float x1, x2, y1, y2;
759
760 pixman_box32_t ret;
761
Alexander Larsson4ea95522013-05-22 14:41:37 +0200762 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200763 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200764 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200765 rect.x2, rect.y2, &x2, &y2);
766
767 if (x1 <= x2) {
768 ret.x1 = x1;
769 ret.x2 = x2;
770 } else {
771 ret.x1 = x2;
772 ret.x2 = x1;
773 }
774
775 if (y1 <= y2) {
776 ret.y1 = y1;
777 ret.y2 = y2;
778 } else {
779 ret.y1 = y2;
780 ret.y2 = y1;
781 }
782
783 return ret;
784}
785
786WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500787weston_transformed_region(int width, int height,
788 enum wl_output_transform transform,
789 int32_t scale,
790 pixman_region32_t *src, pixman_region32_t *dest)
791{
792 pixman_box32_t *src_rects, *dest_rects;
793 int nrects, i;
794
795 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
796 if (src != dest)
797 pixman_region32_copy(dest, src);
798 return;
799 }
800
801 src_rects = pixman_region32_rectangles(src, &nrects);
802 dest_rects = malloc(nrects * sizeof(*dest_rects));
803 if (!dest_rects)
804 return;
805
806 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
807 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
808 } else {
809 for (i = 0; i < nrects; i++) {
810 switch (transform) {
811 default:
812 case WL_OUTPUT_TRANSFORM_NORMAL:
813 dest_rects[i].x1 = src_rects[i].x1;
814 dest_rects[i].y1 = src_rects[i].y1;
815 dest_rects[i].x2 = src_rects[i].x2;
816 dest_rects[i].y2 = src_rects[i].y2;
817 break;
818 case WL_OUTPUT_TRANSFORM_90:
819 dest_rects[i].x1 = height - src_rects[i].y2;
820 dest_rects[i].y1 = src_rects[i].x1;
821 dest_rects[i].x2 = height - src_rects[i].y1;
822 dest_rects[i].y2 = src_rects[i].x2;
823 break;
824 case WL_OUTPUT_TRANSFORM_180:
825 dest_rects[i].x1 = width - src_rects[i].x2;
826 dest_rects[i].y1 = height - src_rects[i].y2;
827 dest_rects[i].x2 = width - src_rects[i].x1;
828 dest_rects[i].y2 = height - src_rects[i].y1;
829 break;
830 case WL_OUTPUT_TRANSFORM_270:
831 dest_rects[i].x1 = src_rects[i].y1;
832 dest_rects[i].y1 = width - src_rects[i].x2;
833 dest_rects[i].x2 = src_rects[i].y2;
834 dest_rects[i].y2 = width - src_rects[i].x1;
835 break;
836 case WL_OUTPUT_TRANSFORM_FLIPPED:
837 dest_rects[i].x1 = width - src_rects[i].x2;
838 dest_rects[i].y1 = src_rects[i].y1;
839 dest_rects[i].x2 = width - src_rects[i].x1;
840 dest_rects[i].y2 = src_rects[i].y2;
841 break;
842 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
843 dest_rects[i].x1 = height - src_rects[i].y2;
844 dest_rects[i].y1 = width - src_rects[i].x2;
845 dest_rects[i].x2 = height - src_rects[i].y1;
846 dest_rects[i].y2 = width - src_rects[i].x1;
847 break;
848 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
849 dest_rects[i].x1 = src_rects[i].x1;
850 dest_rects[i].y1 = height - src_rects[i].y2;
851 dest_rects[i].x2 = src_rects[i].x2;
852 dest_rects[i].y2 = height - src_rects[i].y1;
853 break;
854 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
855 dest_rects[i].x1 = src_rects[i].y1;
856 dest_rects[i].y1 = src_rects[i].x1;
857 dest_rects[i].x2 = src_rects[i].y2;
858 dest_rects[i].y2 = src_rects[i].x2;
859 break;
860 }
861 }
862 }
863
864 if (scale != 1) {
865 for (i = 0; i < nrects; i++) {
866 dest_rects[i].x1 *= scale;
867 dest_rects[i].x2 *= scale;
868 dest_rects[i].y1 *= scale;
869 dest_rects[i].y2 *= scale;
870 }
871 }
872
873 pixman_region32_clear(dest);
874 pixman_region32_init_rects(dest, dest_rects, nrects);
875 free(dest_rects);
876}
877
Jonny Lamb74130762013-11-26 18:19:46 +0100878static void
879scaler_surface_to_buffer(struct weston_surface *surface,
880 float sx, float sy, float *bx, float *by)
881{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200882 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200883 double src_width, src_height;
884 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200886 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
887 if (vp->surface.width == -1) {
888 *bx = sx;
889 *by = sy;
890 return;
891 }
Jonny Lamb74130762013-11-26 18:19:46 +0100892
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200893 src_x = 0.0;
894 src_y = 0.0;
895 src_width = surface->width_from_buffer;
896 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100897 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200898 src_x = wl_fixed_to_double(vp->buffer.src_x);
899 src_y = wl_fixed_to_double(vp->buffer.src_y);
900 src_width = wl_fixed_to_double(vp->buffer.src_width);
901 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100902 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200903
904 *bx = sx * src_width / surface->width + src_x;
905 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100906}
907
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500908WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200909weston_surface_to_buffer_float(struct weston_surface *surface,
910 float sx, float sy, float *bx, float *by)
911{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200912 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
913
Jonny Lamb74130762013-11-26 18:19:46 +0100914 /* first transform coordinates if the scaler is set */
915 scaler_surface_to_buffer(surface, sx, sy, bx, by);
916
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500917 weston_transformed_coord(surface->width_from_buffer,
918 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200919 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100920 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200921}
922
Alexander Larsson4ea95522013-05-22 14:41:37 +0200923WL_EXPORT void
924weston_surface_to_buffer(struct weston_surface *surface,
925 int sx, int sy, int *bx, int *by)
926{
927 float bxf, byf;
928
Jonny Lamb74130762013-11-26 18:19:46 +0100929 weston_surface_to_buffer_float(surface,
930 sx, sy, &bxf, &byf);
931
Alexander Larsson4ea95522013-05-22 14:41:37 +0200932 *bx = floorf(bxf);
933 *by = floorf(byf);
934}
935
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200936WL_EXPORT pixman_box32_t
937weston_surface_to_buffer_rect(struct weston_surface *surface,
938 pixman_box32_t rect)
939{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200940 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100941 float xf, yf;
942
943 /* first transform box coordinates if the scaler is set */
944 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
945 rect.x1 = floorf(xf);
946 rect.y1 = floorf(yf);
947
948 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
949 rect.x2 = floorf(xf);
950 rect.y2 = floorf(yf);
951
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500952 return weston_transformed_rect(surface->width_from_buffer,
953 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200954 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200955 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200956}
957
Pekka Paalanene54e31c2015-03-04 14:23:28 +0200958/** Transform a region from surface coordinates to buffer coordinates
959 *
960 * \param surface The surface to fetch wl_viewport and buffer transformation
961 * from.
962 * \param surface_region[in] The region in surface coordinates.
963 * \param buffer_region[out] The region converted to buffer coordinates.
964 *
965 * Buffer_region must be init'd, but will be completely overwritten.
966 *
967 * Viewport and buffer transformations can only do translation, scaling,
968 * and rotations in 90-degree steps. Therefore the only loss in the
969 * conversion is coordinate flooring (rounding).
970 */
971WL_EXPORT void
972weston_surface_to_buffer_region(struct weston_surface *surface,
973 pixman_region32_t *surface_region,
974 pixman_region32_t *buffer_region)
975{
976 pixman_box32_t *src_rects, *dest_rects;
977 int nrects, i;
978
979 src_rects = pixman_region32_rectangles(surface_region, &nrects);
980 dest_rects = malloc(nrects * sizeof(*dest_rects));
981 if (!dest_rects)
982 return;
983
984 for (i = 0; i < nrects; i++) {
985 dest_rects[i] = weston_surface_to_buffer_rect(surface,
986 src_rects[i]);
987 }
988
989 pixman_region32_fini(buffer_region);
990 pixman_region32_init_rects(buffer_region, dest_rects, nrects);
991 free(dest_rects);
992}
993
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200994WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400996 struct weston_plane *plane)
997{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400999 return;
1000
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 weston_view_damage_below(view);
1002 view->plane = plane;
1003 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001004}
1005
Pekka Paalanen51723d52015-02-17 13:10:01 +02001006/** Inflict damage on the plane where the view is visible.
1007 *
1008 * \param view The view that causes the damage.
1009 *
1010 * If the view is currently on a plane (including the primary plane),
1011 * take the view's boundingbox, subtract all the opaque views that cover it,
1012 * and add the remaining region as damage to the plane. This corresponds
1013 * to the damage inflicted to the plane if this view disappeared.
1014 *
1015 * A repaint is scheduled for this view.
1016 *
1017 * The region of all opaque views covering this view is stored in
1018 * weston_view::clip and updated by view_accumulate_damage() during
1019 * weston_output_repaint(). Specifically, that region matches the
1020 * scenegraph as it was last painted.
1021 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001022WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001024{
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001025 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001026
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001027 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001028 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +08001030 if (view->plane)
1031 pixman_region32_union(&view->plane->damage,
1032 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -05001033 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -08001034 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001035}
1036
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037static void
1038weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1039{
1040 uint32_t different = es->output_mask ^ mask;
1041 uint32_t entered = mask & different;
1042 uint32_t left = es->output_mask & different;
1043 struct weston_output *output;
1044 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001045 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001046
1047 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001048 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049 return;
1050 if (different == 0)
1051 return;
1052
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001053 client = wl_resource_get_client(es->resource);
1054
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001055 wl_list_for_each(output, &es->compositor->output_list, link) {
1056 if (1 << output->id & different)
1057 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05001058 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001059 client);
1060 if (resource == NULL)
1061 continue;
1062 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001063 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001064 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001065 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001066 }
1067}
1068
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001069
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001070static void
1071weston_surface_assign_output(struct weston_surface *es)
1072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 struct weston_output *new_output;
1074 struct weston_view *view;
1075 pixman_region32_t region;
1076 uint32_t max, area, mask;
1077 pixman_box32_t *e;
1078
1079 new_output = NULL;
1080 max = 0;
1081 mask = 0;
1082 pixman_region32_init(&region);
1083 wl_list_for_each(view, &es->views, surface_link) {
1084 if (!view->output)
1085 continue;
1086
1087 pixman_region32_intersect(&region, &view->transform.boundingbox,
1088 &view->output->region);
1089
1090 e = pixman_region32_extents(&region);
1091 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1092
1093 mask |= view->output_mask;
1094
1095 if (area >= max) {
1096 new_output = view->output;
1097 max = area;
1098 }
1099 }
1100 pixman_region32_fini(&region);
1101
1102 es->output = new_output;
1103 weston_surface_update_output_mask(es, mask);
1104}
1105
1106static void
1107weston_view_assign_output(struct weston_view *ev)
1108{
1109 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001110 struct weston_output *output, *new_output;
1111 pixman_region32_t region;
1112 uint32_t max, area, mask;
1113 pixman_box32_t *e;
1114
1115 new_output = NULL;
1116 max = 0;
1117 mask = 0;
1118 pixman_region32_init(&region);
1119 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001121 &output->region);
1122
1123 e = pixman_region32_extents(&region);
1124 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1125
1126 if (area > 0)
1127 mask |= 1 << output->id;
1128
1129 if (area >= max) {
1130 new_output = output;
1131 max = area;
1132 }
1133 }
1134 pixman_region32_fini(&region);
1135
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 ev->output = new_output;
1137 ev->output_mask = mask;
1138
1139 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001140}
1141
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001142static void
Pekka Paalanen380adf52015-02-16 14:39:11 +02001143weston_view_to_view_map(struct weston_view *from, struct weston_view *to,
1144 int from_x, int from_y, int *to_x, int *to_y)
1145{
1146 float x, y;
1147
1148 weston_view_to_global_float(from, from_x, from_y, &x, &y);
1149 weston_view_from_global_float(to, x, y, &x, &y);
1150
1151 *to_x = round(x);
1152 *to_y = round(y);
1153}
1154
1155static void
1156weston_view_transfer_scissor(struct weston_view *from, struct weston_view *to)
1157{
1158 pixman_box32_t *a;
1159 pixman_box32_t b;
1160
1161 a = pixman_region32_extents(&from->geometry.scissor);
1162
1163 weston_view_to_view_map(from, to, a->x1, a->y1, &b.x1, &b.y1);
1164 weston_view_to_view_map(from, to, a->x2, a->y2, &b.x2, &b.y2);
1165
1166 pixman_region32_fini(&to->geometry.scissor);
1167 pixman_region32_init_with_extents(&to->geometry.scissor, &b);
1168}
1169
1170static void
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001171view_compute_bbox(struct weston_view *view, const pixman_box32_t *inbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001173{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001174 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1175 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001176 int32_t s[4][2] = {
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001177 { inbox->x1, inbox->y1 },
1178 { inbox->x1, inbox->y2 },
1179 { inbox->x2, inbox->y1 },
1180 { inbox->x2, inbox->y2 },
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001181 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001182 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001183 int i;
1184
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02001185 if (inbox->x1 == inbox->x2 || inbox->y1 == inbox->y2) {
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001186 /* avoid rounding empty bbox to 1x1 */
1187 pixman_region32_init(bbox);
1188 return;
1189 }
1190
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001191 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001192 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001194 if (x < min_x)
1195 min_x = x;
1196 if (x > max_x)
1197 max_x = x;
1198 if (y < min_y)
1199 min_y = y;
1200 if (y > max_y)
1201 max_y = y;
1202 }
1203
Pekka Paalanen219b9822012-02-08 15:38:37 +02001204 int_x = floorf(min_x);
1205 int_y = floorf(min_y);
1206 pixman_region32_init_rect(bbox, int_x, int_y,
1207 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001208}
1209
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001210static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001212{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001215 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 view->geometry.x = roundf(view->geometry.x);
1217 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001218
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001219 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1221 view->transform.position.matrix.d[12] = view->geometry.x;
1222 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->transform.inverse = view->transform.position.matrix;
1227 view->transform.inverse.d[12] = -view->geometry.x;
1228 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 pixman_region32_init_rect(&view->transform.boundingbox,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001231 0, 0,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001232 view->surface->width,
1233 view->surface->height);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001234 if (view->geometry.scissor_enabled)
1235 pixman_region32_intersect(&view->transform.boundingbox,
1236 &view->transform.boundingbox,
1237 &view->geometry.scissor);
1238
1239 pixman_region32_translate(&view->transform.boundingbox,
1240 view->geometry.x, view->geometry.y);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 if (view->alpha == 1.0) {
1243 pixman_region32_copy(&view->transform.opaque,
1244 &view->surface->opaque);
1245 pixman_region32_translate(&view->transform.opaque,
1246 view->geometry.x,
1247 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001248 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001249}
1250
1251static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001253{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 struct weston_view *parent = view->geometry.parent;
1255 struct weston_matrix *matrix = &view->transform.matrix;
1256 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001257 struct weston_transform *tform;
Pekka Paalanen380adf52015-02-16 14:39:11 +02001258 pixman_region32_t surfregion;
1259 const pixman_box32_t *surfbox;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001260
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001262
1263 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1265 view->transform.position.matrix.d[12] = view->geometry.x;
1266 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001267
1268 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001270 weston_matrix_multiply(matrix, &tform->matrix);
1271
Pekka Paalanen483243f2013-03-08 14:56:50 +02001272 if (parent)
1273 weston_matrix_multiply(matrix, &parent->transform.matrix);
1274
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001275 if (weston_matrix_invert(inverse, matrix) < 0) {
1276 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 weston_log("error: weston_view %p"
1278 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001279 return -1;
1280 }
1281
Pekka Paalanen380adf52015-02-16 14:39:11 +02001282 pixman_region32_init_rect(&surfregion, 0, 0,
1283 view->surface->width, view->surface->height);
1284 if (view->geometry.scissor_enabled)
1285 pixman_region32_intersect(&surfregion, &surfregion,
1286 &view->geometry.scissor);
1287 surfbox = pixman_region32_extents(&surfregion);
1288
1289 view_compute_bbox(view, surfbox, &view->transform.boundingbox);
1290 pixman_region32_fini(&surfregion);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001291
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001292 return 0;
1293}
1294
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001295static struct weston_layer *
1296get_view_layer(struct weston_view *view)
1297{
1298 if (view->parent_view)
1299 return get_view_layer(view->parent_view);
1300 return view->layer_link.layer;
1301}
1302
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001303WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001305{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001307 struct weston_layer *layer;
1308 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001311 return;
1312
Pekka Paalanen483243f2013-03-08 14:56:50 +02001313 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001317
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 pixman_region32_fini(&view->transform.boundingbox);
1321 pixman_region32_fini(&view->transform.opaque);
1322 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001323
Pekka Paalanencd403622012-01-25 13:37:39 +02001324 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 if (view->geometry.transformation_list.next ==
1326 &view->transform.position.link &&
1327 view->geometry.transformation_list.prev ==
1328 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001329 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001331 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 if (weston_view_update_transform_enable(view) < 0)
1333 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001334 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001335
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001336 layer = get_view_layer(view);
1337 if (layer) {
1338 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001339 pixman_region32_intersect(&view->transform.boundingbox,
1340 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001341 pixman_region32_intersect(&view->transform.opaque,
1342 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001343 pixman_region32_fini(&mask);
1344 }
1345
Pekka Paalanen380adf52015-02-16 14:39:11 +02001346 if (parent) {
1347 if (parent->geometry.scissor_enabled) {
1348 view->geometry.scissor_enabled = true;
1349 weston_view_transfer_scissor(parent, view);
1350 } else {
1351 view->geometry.scissor_enabled = false;
1352 }
1353 }
1354
Jason Ekstranda7af7042013-10-12 22:38:11 -05001355 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 wl_signal_emit(&view->surface->compositor->transform_signal,
1360 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001361}
1362
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001363WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001365{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001367
1368 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 * The invariant: if view->geometry.dirty, then all views
1370 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371 * Corollary: if not parent->geometry.dirty, then all ancestors
1372 * are not dirty.
1373 */
1374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001376 return;
1377
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001381 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001383}
1384
1385WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386weston_view_to_global_fixed(struct weston_view *view,
1387 wl_fixed_t vx, wl_fixed_t vy,
1388 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001389{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001390 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 weston_view_to_global_float(view,
1393 wl_fixed_to_double(vx),
1394 wl_fixed_to_double(vy),
1395 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001396 *x = wl_fixed_from_double(xf);
1397 *y = wl_fixed_from_double(yf);
1398}
1399
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001400WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401weston_view_from_global_float(struct weston_view *view,
1402 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001403{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001405 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1406
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001408
1409 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001410 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001412 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 *vx = 0;
1414 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001415 return;
1416 }
1417
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 *vx = v.f[0] / v.f[3];
1419 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001420 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 *vx = x - view->geometry.x;
1422 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001423 }
1424}
1425
1426WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001427weston_view_from_global_fixed(struct weston_view *view,
1428 wl_fixed_t x, wl_fixed_t y,
1429 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001430{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001432
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433 weston_view_from_global_float(view,
1434 wl_fixed_to_double(x),
1435 wl_fixed_to_double(y),
1436 &vxf, &vyf);
1437 *vx = wl_fixed_from_double(vxf);
1438 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001439}
1440
1441WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001442weston_view_from_global(struct weston_view *view,
1443 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001444{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001446
Jason Ekstranda7af7042013-10-12 22:38:11 -05001447 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1448 *vx = floorf(vxf);
1449 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001450}
1451
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001452WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001453weston_surface_schedule_repaint(struct weston_surface *surface)
1454{
1455 struct weston_output *output;
1456
1457 wl_list_for_each(output, &surface->compositor->output_list, link)
1458 if (surface->output_mask & (1 << output->id))
1459 weston_output_schedule_repaint(output);
1460}
1461
1462WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463weston_view_schedule_repaint(struct weston_view *view)
1464{
1465 struct weston_output *output;
1466
1467 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1468 if (view->output_mask & (1 << output->id))
1469 weston_output_schedule_repaint(output);
1470}
1471
Pekka Paalanene508ce62015-02-19 13:59:55 +02001472/**
1473 * XXX: This function does it the wrong way.
1474 * surface->damage is the damage from the client, and causes
1475 * surface_flush_damage() to copy pixels. No window management action can
1476 * cause damage to the client-provided content, warranting re-upload!
1477 *
1478 * Instead of surface->damage, this function should record the damage
1479 * with all the views for this surface to avoid extraneous texture
1480 * uploads.
1481 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001483weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001484{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001485 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 0, 0, surface->width,
1487 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001488
Kristian Høgsberg98238702012-08-03 16:29:12 -04001489 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001490}
1491
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001492WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001493weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001494{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001495 if (view->geometry.x == x && view->geometry.y == y)
1496 return;
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 view->geometry.x = x;
1499 view->geometry.y = y;
1500 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001501}
1502
Pekka Paalanen483243f2013-03-08 14:56:50 +02001503static void
1504transform_parent_handle_parent_destroy(struct wl_listener *listener,
1505 void *data)
1506{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 struct weston_view *view =
1508 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001509 geometry.parent_destroy_listener);
1510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001512}
1513
1514WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515weston_view_set_transform_parent(struct weston_view *view,
Pekka Paalanen380adf52015-02-16 14:39:11 +02001516 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 if (view->geometry.parent) {
1519 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1520 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001521
1522 if (!parent)
1523 view->geometry.scissor_enabled = false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001524 }
1525
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001529 transform_parent_handle_parent_destroy;
1530 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001531 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001533 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001535 }
1536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 weston_view_geometry_dirty(view);
1538}
1539
Pekka Paalanen380adf52015-02-16 14:39:11 +02001540/** Set a clip mask rectangle on a view
1541 *
1542 * \param view The view to set the clip mask on.
1543 * \param x Top-left corner X coordinate of the clip rectangle.
1544 * \param y Top-left corner Y coordinate of the clip rectangle.
1545 * \param width Width of the clip rectangle, non-negative.
1546 * \param height Height of the clip rectangle, non-negative.
1547 *
1548 * A shell may set a clip mask rectangle on a view. Everything outside
1549 * the rectangle is cut away for input and output purposes: it is
1550 * not drawn and cannot be hit by hit-test based input like pointer
1551 * motion or touch-downs. Everything inside the rectangle will behave
1552 * normally. Clients are unaware of clipping.
1553 *
1554 * The rectangle is set in the surface local coordinates. Setting a clip
1555 * mask rectangle does not affect the view position, the view is positioned
1556 * as it would be without a clip. The clip also does not change
1557 * weston_surface::width,height.
1558 *
1559 * The clip mask rectangle is part of transformation inheritance
1560 * (weston_view_set_transform_parent()). A clip set in the root of the
1561 * transformation inheritance tree will affect all views in the tree.
1562 * A clip can be set only on the root view. Attempting to set a clip
1563 * on view that has a transformation parent will fail. Assigning a parent
1564 * to a view that has a clip set will cause the clip to be forgotten.
1565 *
1566 * Because the clip mask is an axis-aligned rectangle, it poses restrictions
1567 * on the additional transformations in the child views. These transformations
1568 * may not rotate the coordinate axes, i.e., only translation and scaling
1569 * are allowed. Violating this restriction causes the clipping to malfunction.
1570 * Furthermore, using scaling may cause rounding errors in child clipping.
1571 *
1572 * The clip mask rectangle is not automatically adjusted based on
1573 * wl_surface.attach dx and dy arguments.
1574 *
1575 * A clip mask rectangle can be set only if the compositor capability
1576 * WESTON_CAP_VIEW_CLIP_MASK is present.
1577 *
1578 * This function sets the clip mask rectangle and schedules a repaint for
1579 * the view.
1580 */
1581WL_EXPORT void
1582weston_view_set_mask(struct weston_view *view,
1583 int x, int y, int width, int height)
1584{
1585 struct weston_compositor *compositor = view->surface->compositor;
1586
1587 if (!(compositor->capabilities & WESTON_CAP_VIEW_CLIP_MASK)) {
1588 weston_log("%s not allowed without capability!\n", __func__);
1589 return;
1590 }
1591
1592 if (view->geometry.parent) {
1593 weston_log("view %p has a parent, clip forbidden!\n", view);
1594 return;
1595 }
1596
1597 if (width < 0 || height < 0) {
1598 weston_log("%s: illegal args %d, %d, %d, %d\n", __func__,
1599 x, y, width, height);
1600 return;
1601 }
1602
1603 pixman_region32_fini(&view->geometry.scissor);
1604 pixman_region32_init_rect(&view->geometry.scissor, x, y, width, height);
1605 view->geometry.scissor_enabled = true;
1606 weston_view_geometry_dirty(view);
1607 weston_view_schedule_repaint(view);
1608}
1609
1610/** Remove the clip mask from a view
1611 *
1612 * \param view The view to remove the clip mask from.
1613 *
1614 * Removed the clip mask rectangle and schedules a repaint.
1615 *
1616 * \sa weston_view_set_mask
1617 */
1618WL_EXPORT void
1619weston_view_set_mask_infinite(struct weston_view *view)
1620{
1621 view->geometry.scissor_enabled = false;
1622 weston_view_geometry_dirty(view);
1623 weston_view_schedule_repaint(view);
1624}
1625
Derek Foreman280e7dd2014-10-03 13:13:42 -05001626WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627weston_view_is_mapped(struct weston_view *view)
1628{
1629 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001630 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001632 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001633}
1634
Derek Foreman280e7dd2014-10-03 13:13:42 -05001635WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001636weston_surface_is_mapped(struct weston_surface *surface)
1637{
1638 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001639 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001640 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001641 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001642}
1643
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001644static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001645surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001646{
1647 struct weston_view *view;
1648
1649 if (surface->width == width && surface->height == height)
1650 return;
1651
1652 surface->width = width;
1653 surface->height = height;
1654
1655 wl_list_for_each(view, &surface->views, surface_link)
1656 weston_view_geometry_dirty(view);
1657}
1658
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001659WL_EXPORT void
1660weston_surface_set_size(struct weston_surface *surface,
1661 int32_t width, int32_t height)
1662{
1663 assert(!surface->resource);
1664 surface_set_size(surface, width, height);
1665}
1666
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001667static int
1668fixed_round_up_to_int(wl_fixed_t f)
1669{
1670 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1671}
1672
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001673static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001674weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001675{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001676 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001677 int32_t width, height;
1678
1679 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001680 surface->width_from_buffer = 0;
1681 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001682 return;
1683 }
1684
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001685 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001686 case WL_OUTPUT_TRANSFORM_90:
1687 case WL_OUTPUT_TRANSFORM_270:
1688 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1689 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001690 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1691 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001692 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001693 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001694 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1695 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001696 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001697 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001698
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001699 surface->width_from_buffer = width;
1700 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001701}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001702
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001703static void
1704weston_surface_update_size(struct weston_surface *surface)
1705{
1706 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1707 int32_t width, height;
1708
1709 width = surface->width_from_buffer;
1710 height = surface->height_from_buffer;
1711
1712 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001713 surface_set_size(surface,
1714 vp->surface.width, vp->surface.height);
1715 return;
1716 }
1717
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001718 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001719 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1720 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1721
1722 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001723 return;
1724 }
1725
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001726 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001727}
1728
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001729WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001730weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001731{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001732 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001733
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001734 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001735
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001736 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001737}
1738
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739WL_EXPORT struct weston_view *
1740weston_compositor_pick_view(struct weston_compositor *compositor,
1741 wl_fixed_t x, wl_fixed_t y,
1742 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001743{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 struct weston_view *view;
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001745 wl_fixed_t view_x, view_y;
1746 int view_ix, view_iy;
1747 int ix = wl_fixed_to_int(x);
1748 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 wl_list_for_each(view, &compositor->view_list, link) {
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001751 if (!pixman_region32_contains_point(
1752 &view->transform.boundingbox, ix, iy, NULL))
1753 continue;
1754
1755 weston_view_from_global_fixed(view, x, y, &view_x, &view_y);
1756 view_ix = wl_fixed_to_int(view_x);
1757 view_iy = wl_fixed_to_int(view_y);
1758
1759 if (!pixman_region32_contains_point(&view->surface->input,
1760 view_ix, view_iy, NULL))
1761 continue;
1762
Pekka Paalanen380adf52015-02-16 14:39:11 +02001763 if (view->geometry.scissor_enabled &&
1764 !pixman_region32_contains_point(&view->geometry.scissor,
1765 view_ix, view_iy, NULL))
1766 continue;
1767
Pekka Paalanenfc22a522015-02-18 15:08:29 +02001768 *vx = view_x;
1769 *vy = view_y;
1770 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001771 }
1772
1773 return NULL;
1774}
1775
1776static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001777weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001778{
Daniel Stone37816df2012-05-16 18:45:18 +01001779 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001780
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001781 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001782 return;
1783
Daniel Stone37816df2012-05-16 18:45:18 +01001784 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001785 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001786}
1787
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001788WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001790{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001791 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 if (!weston_view_is_mapped(view))
1794 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001795
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 weston_view_damage_below(view);
1797 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001798 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001799 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 wl_list_remove(&view->link);
1801 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 view->output_mask = 0;
1803 weston_surface_assign_output(view->surface);
1804
1805 if (weston_surface_is_mapped(view->surface))
1806 return;
1807
1808 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1809 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001810 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001811 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001812 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001813 NULL,
1814 wl_fixed_from_int(0),
1815 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001817 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001818 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821WL_EXPORT void
1822weston_surface_unmap(struct weston_surface *surface)
1823{
1824 struct weston_view *view;
1825
1826 wl_list_for_each(view, &surface->views, surface_link)
1827 weston_view_unmap(view);
1828 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001829}
1830
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001831static void
1832weston_surface_reset_pending_buffer(struct weston_surface *surface)
1833{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001834 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001835 surface->pending.sx = 0;
1836 surface->pending.sy = 0;
1837 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001838 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001839}
1840
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841WL_EXPORT void
1842weston_view_destroy(struct weston_view *view)
1843{
1844 wl_signal_emit(&view->destroy_signal, view);
1845
1846 assert(wl_list_empty(&view->geometry.child_list));
1847
1848 if (weston_view_is_mapped(view)) {
1849 weston_view_unmap(view);
1850 weston_compositor_build_view_list(view->surface->compositor);
1851 }
1852
1853 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001854 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855
1856 pixman_region32_fini(&view->clip);
Pekka Paalanen380adf52015-02-16 14:39:11 +02001857 pixman_region32_fini(&view->geometry.scissor);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001859 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001860
1861 weston_view_set_transform_parent(view, NULL);
1862
Jason Ekstranda7af7042013-10-12 22:38:11 -05001863 wl_list_remove(&view->surface_link);
1864
1865 free(view);
1866}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001867
1868WL_EXPORT void
1869weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001870{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001871 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001873
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001874 if (--surface->ref_count > 0)
1875 return;
1876
1877 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1878
Pekka Paalanene67858b2013-04-25 13:57:42 +03001879 assert(wl_list_empty(&surface->subsurface_list_pending));
1880 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001881
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1883 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001884
Jason Ekstrand7b982072014-05-20 14:33:03 -05001885 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001886
Pekka Paalanende685b82012-12-04 15:58:12 +02001887 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001888
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001889 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001890 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001891 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001892
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001893 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001894 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001895
Pekka Paalanen133e4392014-09-23 22:08:46 -04001896 weston_presentation_feedback_discard_list(&surface->feedback_list);
1897
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001898 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001899}
1900
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001901static void
1902destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001903{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001904 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001905
Giulio Camuffo0d379742013-11-15 22:06:15 +01001906 /* Set the resource to NULL, since we don't want to leave a
1907 * dangling pointer if the surface was refcounted and survives
1908 * the weston_surface_destroy() call. */
1909 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001910 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001911}
1912
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001913static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001914weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1915{
1916 struct weston_buffer *buffer =
1917 container_of(listener, struct weston_buffer, destroy_listener);
1918
1919 wl_signal_emit(&buffer->destroy_signal, buffer);
1920 free(buffer);
1921}
1922
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001923WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001924weston_buffer_from_resource(struct wl_resource *resource)
1925{
1926 struct weston_buffer *buffer;
1927 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001928
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 listener = wl_resource_get_destroy_listener(resource,
1930 weston_buffer_destroy_handler);
1931
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001932 if (listener)
1933 return container_of(listener, struct weston_buffer,
1934 destroy_listener);
1935
1936 buffer = zalloc(sizeof *buffer);
1937 if (buffer == NULL)
1938 return NULL;
1939
1940 buffer->resource = resource;
1941 wl_signal_init(&buffer->destroy_signal);
1942 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001943 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001944 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001945
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001946 return buffer;
1947}
1948
1949static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001950weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1951 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001952{
Pekka Paalanende685b82012-12-04 15:58:12 +02001953 struct weston_buffer_reference *ref =
1954 container_of(listener, struct weston_buffer_reference,
1955 destroy_listener);
1956
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001958 ref->buffer = NULL;
1959}
1960
1961WL_EXPORT void
1962weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001963 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001964{
1965 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001966 ref->buffer->busy_count--;
1967 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001968 assert(wl_resource_get_client(ref->buffer->resource));
1969 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001970 WL_BUFFER_RELEASE);
1971 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001972 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001973 }
1974
Pekka Paalanende685b82012-12-04 15:58:12 +02001975 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001976 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001977 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001978 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001979 }
1980
Pekka Paalanende685b82012-12-04 15:58:12 +02001981 ref->buffer = buffer;
1982 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1983}
1984
1985static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001986weston_surface_attach(struct weston_surface *surface,
1987 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001988{
1989 weston_buffer_reference(&surface->buffer_ref, buffer);
1990
Pekka Paalanena6421c42012-12-04 15:58:10 +02001991 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001992 if (weston_surface_is_mapped(surface))
1993 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001994 }
1995
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001996 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001997
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001998 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001999 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01002000}
2001
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002002WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002003weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002004{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002005 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002006
2007 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002008 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002009}
2010
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05002011WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002012weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002013{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002014 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002015
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002016 pixman_region32_union(&compositor->primary_plane.damage,
2017 &compositor->primary_plane.damage,
2018 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002019 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002020}
2021
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04002022static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002024{
Pekka Paalanende685b82012-12-04 15:58:12 +02002025 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002026 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04002027 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002028
Pekka Paalanenb5026542014-11-12 15:09:24 +02002029 if (weston_timeline_enabled_ &&
2030 pixman_region32_not_empty(&surface->damage))
2031 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
2032 TLP_OUTPUT(surface->output), TLP_END);
2033
Jason Ekstrandef540082014-06-26 10:37:36 -07002034 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035}
2036
2037static void
2038view_accumulate_damage(struct weston_view *view,
2039 pixman_region32_t *opaque)
2040{
2041 pixman_region32_t damage;
2042
2043 pixman_region32_init(&damage);
2044 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002045 pixman_box32_t *extents;
2046
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 extents = pixman_region32_extents(&view->surface->damage);
Pekka Paalanenc7d7fdf2015-02-23 12:27:00 +02002048 view_compute_bbox(view, extents, &damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002049 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 pixman_region32_copy(&damage, &view->surface->damage);
2051 pixman_region32_translate(&damage,
Pekka Paalanen502f5e02015-02-23 14:08:25 +02002052 view->geometry.x, view->geometry.y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002053 }
2054
Pekka Paalanen380adf52015-02-16 14:39:11 +02002055 pixman_region32_intersect(&damage, &damage,
2056 &view->transform.boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 pixman_region32_subtract(&damage, &damage, opaque);
2058 pixman_region32_union(&view->plane->damage,
2059 &view->plane->damage, &damage);
2060 pixman_region32_fini(&damage);
2061 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02002062 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002063}
2064
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002065static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002066compositor_accumulate_damage(struct weston_compositor *ec)
2067{
2068 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002070 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002071
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002072 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002073
2074 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002075 pixman_region32_copy(&plane->clip, &clip);
2076
2077 pixman_region32_init(&opaque);
2078
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 wl_list_for_each(ev, &ec->view_list, link) {
2080 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002081 continue;
2082
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002084 }
2085
2086 pixman_region32_union(&clip, &clip, &opaque);
2087 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002088 }
2089
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002090 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002091
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 wl_list_for_each(ev, &ec->view_list, link)
2093 ev->surface->touched = 0;
2094
2095 wl_list_for_each(ev, &ec->view_list, link) {
2096 if (ev->surface->touched)
2097 continue;
2098 ev->surface->touched = 1;
2099
2100 surface_flush_damage(ev->surface);
2101
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002102 /* Both the renderer and the backend have seen the buffer
2103 * by now. If renderer needs the buffer, it has its own
2104 * reference set. If the backend wants to keep the buffer
2105 * around for migrating the surface into a non-primary plane
2106 * later, keep_buffer is true. Otherwise, drop the core
2107 * reference now, and allow early buffer release. This enables
2108 * clients to use single-buffering.
2109 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002110 if (!ev->surface->keep_buffer)
2111 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002112 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002113}
2114
2115static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002116surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002117{
2118 struct weston_subsurface *sub;
2119
Pekka Paalanene67858b2013-04-25 13:57:42 +03002120 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002122 continue;
2123
Jason Ekstranda7af7042013-10-12 22:38:11 -05002124 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
2125 wl_list_init(&sub->surface->views);
2126
2127 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 }
2129}
2130
2131static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05002132surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002133{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 struct weston_subsurface *sub;
2135 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002136
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2138 if (sub->surface == surface)
2139 continue;
2140
George Kiagiadakised04d382014-06-13 18:10:26 +02002141 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
2142 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002144 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002145
2146 surface_free_unused_subsurface_views(sub->surface);
2147 }
2148}
2149
2150static void
2151view_list_add_subsurface_view(struct weston_compositor *compositor,
2152 struct weston_subsurface *sub,
2153 struct weston_view *parent)
2154{
2155 struct weston_subsurface *child;
2156 struct weston_view *view = NULL, *iv;
2157
Pekka Paalanen661de3a2014-07-28 12:49:24 +03002158 if (!weston_surface_is_mapped(sub->surface))
2159 return;
2160
Jason Ekstranda7af7042013-10-12 22:38:11 -05002161 wl_list_for_each(iv, &sub->unused_views, surface_link) {
2162 if (iv->geometry.parent == parent) {
2163 view = iv;
2164 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002165 }
2166 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002167
2168 if (view) {
2169 /* Put it back in the surface's list of views */
2170 wl_list_remove(&view->surface_link);
2171 wl_list_insert(&sub->surface->views, &view->surface_link);
2172 } else {
2173 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002174 weston_view_set_position(view,
2175 sub->position.x,
2176 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002177 weston_view_set_transform_parent(view, parent);
2178 }
2179
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002180 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002182
Pekka Paalanenb188e912013-11-19 14:03:35 +02002183 if (wl_list_empty(&sub->surface->subsurface_list)) {
2184 wl_list_insert(compositor->view_list.prev, &view->link);
2185 return;
2186 }
2187
2188 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
2189 if (child->surface == sub->surface)
2190 wl_list_insert(compositor->view_list.prev, &view->link);
2191 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002192 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002193 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194}
2195
2196static void
2197view_list_add(struct weston_compositor *compositor,
2198 struct weston_view *view)
2199{
2200 struct weston_subsurface *sub;
2201
2202 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203
Pekka Paalanenb188e912013-11-19 14:03:35 +02002204 if (wl_list_empty(&view->surface->subsurface_list)) {
2205 wl_list_insert(compositor->view_list.prev, &view->link);
2206 return;
2207 }
2208
2209 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
2210 if (sub->surface == view->surface)
2211 wl_list_insert(compositor->view_list.prev, &view->link);
2212 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02002214 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002215}
2216
2217static void
2218weston_compositor_build_view_list(struct weston_compositor *compositor)
2219{
2220 struct weston_view *view;
2221 struct weston_layer *layer;
2222
2223 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002224 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 surface_stash_subsurface_views(view->surface);
2226
2227 wl_list_init(&compositor->view_list);
2228 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002229 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 view_list_add(compositor, view);
2231 }
2232 }
2233
2234 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002235 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237}
2238
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002239static void
2240weston_output_take_feedback_list(struct weston_output *output,
2241 struct weston_surface *surface)
2242{
2243 struct weston_view *view;
2244 struct weston_presentation_feedback *feedback;
2245 uint32_t flags = 0xffffffff;
2246
2247 if (wl_list_empty(&surface->feedback_list))
2248 return;
2249
2250 /* All views must have the flag for the flag to survive. */
2251 wl_list_for_each(view, &surface->views, surface_link) {
2252 /* ignore views that are not on this output at all */
2253 if (view->output_mask & (1u << output->id))
2254 flags &= view->psf_flags;
2255 }
2256
2257 wl_list_for_each(feedback, &surface->feedback_list, link)
2258 feedback->psf_flags = flags;
2259
2260 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2261 wl_list_init(&surface->feedback_list);
2262}
2263
David Herrmann1edf44c2013-10-22 17:11:26 +02002264static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002265weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002267 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002269 struct weston_animation *animation, *next;
2270 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002271 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002272 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002273 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002274
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002275 if (output->destroying)
2276 return 0;
2277
Pekka Paalanenb5026542014-11-12 15:09:24 +02002278 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2279
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002280 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002281 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002282
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002283 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002284 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002285 } else {
2286 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002287 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002288 ev->psf_flags = 0;
2289 }
2290 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002291
Pekka Paalanene67858b2013-04-25 13:57:42 +03002292 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002293 wl_list_for_each(ev, &ec->view_list, link) {
2294 /* Note: This operation is safe to do multiple times on the
2295 * same surface.
2296 */
2297 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002299 &ev->surface->frame_callback_list);
2300 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002301
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002302 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 }
2304 }
2305
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002306 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002307
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002308 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002309 pixman_region32_intersect(&output_damage,
2310 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002311 pixman_region32_subtract(&output_damage,
2312 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002313
Scott Moreauccbf29d2012-02-22 14:21:41 -07002314 if (output->dirty)
2315 weston_output_update_matrix(output);
2316
David Herrmann1edf44c2013-10-22 17:11:26 +02002317 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002318
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002319 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002320
Kristian Høgsbergef044142011-06-21 15:02:12 -04002321 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002322
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002323 weston_compositor_repick(ec);
2324 wl_event_loop_dispatch(ec->input_loop, 0);
2325
Jonas Ådahldb773762012-06-13 00:01:21 +02002326 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002327 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002328 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002329 }
2330
Scott Moreaud64cf212012-06-08 19:40:54 -06002331 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002332 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002333 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002334 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002335
Pekka Paalanenb5026542014-11-12 15:09:24 +02002336 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2337
David Herrmann1edf44c2013-10-22 17:11:26 +02002338 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002339}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002340
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002341static int
2342weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002343{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002344 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002345
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002346 wl_event_loop_dispatch(compositor->input_loop, 0);
2347
2348 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002349}
2350
Pekka Paalanen82919792014-05-21 13:51:49 +03002351static void
2352weston_output_schedule_repaint_reset(struct weston_output *output)
2353{
2354 struct weston_compositor *compositor = output->compositor;
2355 struct wl_event_loop *loop;
2356 int fd;
2357
2358 output->repaint_scheduled = 0;
2359 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2360
2361 if (compositor->input_loop_source)
2362 return;
2363
2364 loop = wl_display_get_event_loop(compositor->wl_display);
2365 fd = wl_event_loop_get_fd(compositor->input_loop);
2366 compositor->input_loop_source =
2367 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2368 weston_compositor_read_input, compositor);
2369}
2370
Pekka Paalanen0513a952014-05-21 16:17:27 +03002371static int
2372output_repaint_timer_handler(void *data)
2373{
2374 struct weston_output *output = data;
2375 struct weston_compositor *compositor = output->compositor;
2376
2377 if (output->repaint_needed &&
2378 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2379 compositor->state != WESTON_COMPOSITOR_OFFSCREEN &&
2380 weston_output_repaint(output) == 0)
2381 return 0;
2382
2383 weston_output_schedule_repaint_reset(output);
2384
2385 return 0;
2386}
2387
Kristian Høgsbergef044142011-06-21 15:02:12 -04002388WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002389weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002390 const struct timespec *stamp,
2391 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002392{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002393 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen0513a952014-05-21 16:17:27 +03002394 int32_t refresh_nsec;
2395 struct timespec now;
2396 struct timespec gone;
2397 int msec;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002398
Pekka Paalanenb5026542014-11-12 15:09:24 +02002399 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2400 TLP_VBLANK(stamp), TLP_END);
2401
Pekka Paalanen0513a952014-05-21 16:17:27 +03002402 refresh_nsec = 1000000000000LL / output->current_mode->refresh;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002403 weston_presentation_feedback_present_list(&output->feedback_list,
2404 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002405 output->msc,
2406 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002407
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002408 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002409
Pekka Paalanen0513a952014-05-21 16:17:27 +03002410 weston_compositor_read_presentation_clock(compositor, &now);
2411 timespec_sub(&gone, &now, stamp);
2412 msec = (refresh_nsec - timespec_to_nsec(&gone)) / 1000000; /* floor */
2413 msec -= compositor->repaint_msec;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002414
Pekka Paalanen0513a952014-05-21 16:17:27 +03002415 /* Also sanity check. */
2416 if (msec < 1 || msec > 1000)
2417 output_repaint_timer_handler(output);
2418 else
2419 wl_event_source_timer_update(output->repaint_timer, msec);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002420}
2421
2422static void
2423idle_repaint(void *data)
2424{
2425 struct weston_output *output = data;
2426
Jonas Ådahle5a12252013-04-05 23:07:11 +02002427 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002428}
2429
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002430WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002431weston_layer_entry_insert(struct weston_layer_entry *list,
2432 struct weston_layer_entry *entry)
2433{
2434 wl_list_insert(&list->link, &entry->link);
2435 entry->layer = list->layer;
2436}
2437
2438WL_EXPORT void
2439weston_layer_entry_remove(struct weston_layer_entry *entry)
2440{
2441 wl_list_remove(&entry->link);
2442 wl_list_init(&entry->link);
2443 entry->layer = NULL;
2444}
2445
2446WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002447weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2448{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002449 wl_list_init(&layer->view_list.link);
2450 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002451 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002452 if (below != NULL)
2453 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002454}
2455
2456WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002457weston_layer_set_mask(struct weston_layer *layer,
2458 int x, int y, int width, int height)
2459{
2460 struct weston_view *view;
2461
2462 layer->mask.x1 = x;
2463 layer->mask.x2 = x + width;
2464 layer->mask.y1 = y;
2465 layer->mask.y2 = y + height;
2466
2467 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2468 weston_view_geometry_dirty(view);
2469 }
2470}
2471
2472WL_EXPORT void
2473weston_layer_set_mask_infinite(struct weston_layer *layer)
2474{
2475 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2476 UINT32_MAX, UINT32_MAX);
2477}
2478
2479WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002480weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002481{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002482 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002483 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002484
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002485 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2486 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002487 return;
2488
Pekka Paalanenb5026542014-11-12 15:09:24 +02002489 if (!output->repaint_needed)
2490 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2491
Kristian Høgsbergef044142011-06-21 15:02:12 -04002492 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002493 output->repaint_needed = 1;
2494 if (output->repaint_scheduled)
2495 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002496
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002497 wl_event_loop_add_idle(loop, idle_repaint, output);
2498 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002499 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2500
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002501
2502 if (compositor->input_loop_source) {
2503 wl_event_source_remove(compositor->input_loop_source);
2504 compositor->input_loop_source = NULL;
2505 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002506}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002507
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002508WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002509weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2510{
2511 struct weston_output *output;
2512
2513 wl_list_for_each(output, &compositor->output_list, link)
2514 weston_output_schedule_repaint(output);
2515}
2516
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002517static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002518surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002519{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002520 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002521}
2522
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002523static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002524surface_attach(struct wl_client *client,
2525 struct wl_resource *resource,
2526 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2527{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002529 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002530
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002531 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002532 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002533 if (buffer == NULL) {
2534 wl_client_post_no_memory(client);
2535 return;
2536 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002537 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002538
Pekka Paalanende685b82012-12-04 15:58:12 +02002539 /* Attach, attach, without commit in between does not send
2540 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002541 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002542
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002543 surface->pending.sx = sx;
2544 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002545 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002546}
2547
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002548static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002549surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002550 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002551 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002552{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002553 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002554
Pekka Paalanen8e159182012-10-10 12:49:25 +03002555 pixman_region32_union_rect(&surface->pending.damage,
2556 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002557 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002558}
2559
Kristian Høgsberg33418202011-08-16 23:01:28 -04002560static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002561destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002562{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002563 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002564
2565 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002566 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002567}
2568
2569static void
2570surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002571 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002572{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002573 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002574 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002575
2576 cb = malloc(sizeof *cb);
2577 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002578 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002579 return;
2580 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002581
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002582 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2583 callback);
2584 if (cb->resource == NULL) {
2585 free(cb);
2586 wl_resource_post_no_memory(resource);
2587 return;
2588 }
2589
Jason Ekstranda85118c2013-06-27 20:17:02 -05002590 wl_resource_set_implementation(cb->resource, NULL, cb,
2591 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002592
Pekka Paalanenbc106382012-10-10 12:49:31 +03002593 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002594}
2595
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596static void
2597surface_set_opaque_region(struct wl_client *client,
2598 struct wl_resource *resource,
2599 struct wl_resource *region_resource)
2600{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002601 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002602 struct weston_region *region;
2603
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002604 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002605 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002606 pixman_region32_copy(&surface->pending.opaque,
2607 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002608 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002609 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002610 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002611}
2612
2613static void
2614surface_set_input_region(struct wl_client *client,
2615 struct wl_resource *resource,
2616 struct wl_resource *region_resource)
2617{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002618 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002619 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002620
2621 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002622 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002623 pixman_region32_copy(&surface->pending.input,
2624 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002625 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002626 pixman_region32_fini(&surface->pending.input);
2627 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002628 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002629}
2630
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002631static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002633{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634 struct weston_subsurface *sub;
2635
2636 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2637 parent_link_pending) {
2638 wl_list_remove(&sub->parent_link);
2639 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2640 }
2641}
2642
2643static void
Jason Ekstrand1e059042014-10-16 10:55:19 -05002644weston_surface_build_buffer_matrix(struct weston_surface *surface,
2645 struct weston_matrix *matrix)
2646{
2647 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
2648 double src_width, src_height, dest_width, dest_height;
2649
2650 weston_matrix_init(matrix);
2651
2652 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
2653 src_width = surface->width_from_buffer;
2654 src_height = surface->height_from_buffer;
2655 } else {
2656 src_width = wl_fixed_to_double(vp->buffer.src_width);
2657 src_height = wl_fixed_to_double(vp->buffer.src_height);
2658 }
2659
2660 if (vp->surface.width == -1) {
2661 dest_width = src_width;
2662 dest_height = src_height;
2663 } else {
2664 dest_width = vp->surface.width;
2665 dest_height = vp->surface.height;
2666 }
2667
2668 if (src_width != dest_width || src_height != dest_height)
2669 weston_matrix_scale(matrix,
2670 src_width / dest_width,
2671 src_height / dest_height, 1);
2672
2673 if (vp->buffer.src_width != wl_fixed_from_int(-1))
2674 weston_matrix_translate(matrix,
2675 wl_fixed_to_double(vp->buffer.src_x),
2676 wl_fixed_to_double(vp->buffer.src_y),
2677 0);
2678
2679 switch (vp->buffer.transform) {
2680 case WL_OUTPUT_TRANSFORM_FLIPPED:
2681 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2682 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2683 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2684 weston_matrix_scale(matrix, -1, 1, 1);
2685 weston_matrix_translate(matrix,
2686 surface->width_from_buffer, 0, 0);
2687 break;
2688 }
2689
2690 switch (vp->buffer.transform) {
2691 default:
2692 case WL_OUTPUT_TRANSFORM_NORMAL:
2693 case WL_OUTPUT_TRANSFORM_FLIPPED:
2694 break;
2695 case WL_OUTPUT_TRANSFORM_90:
2696 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2697 weston_matrix_rotate_xy(matrix, 0, 1);
2698 weston_matrix_translate(matrix,
2699 surface->height_from_buffer, 0, 0);
2700 break;
2701 case WL_OUTPUT_TRANSFORM_180:
2702 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2703 weston_matrix_rotate_xy(matrix, -1, 0);
2704 weston_matrix_translate(matrix,
2705 surface->width_from_buffer,
2706 surface->height_from_buffer, 0);
2707 break;
2708 case WL_OUTPUT_TRANSFORM_270:
2709 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2710 weston_matrix_rotate_xy(matrix, 0, -1);
2711 weston_matrix_translate(matrix,
2712 0, surface->width_from_buffer, 0);
2713 break;
2714 }
2715
2716 weston_matrix_scale(matrix, vp->buffer.scale, vp->buffer.scale, 1);
2717}
2718
2719static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002720weston_surface_commit_state(struct weston_surface *surface,
2721 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002722{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002723 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002724 pixman_region32_t opaque;
2725
Alexander Larsson4ea95522013-05-22 14:41:37 +02002726 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002727 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002728 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002729 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002730
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002731 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002732 if (state->newly_attached)
2733 weston_surface_attach(surface, state->buffer);
2734 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002735
Jason Ekstrand1e059042014-10-16 10:55:19 -05002736 weston_surface_build_buffer_matrix(surface,
2737 &surface->surface_to_buffer_matrix);
2738 weston_matrix_invert(&surface->buffer_to_surface_matrix,
2739 &surface->surface_to_buffer_matrix);
2740
Jason Ekstrand7b982072014-05-20 14:33:03 -05002741 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002742 weston_surface_update_size(surface);
2743 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002744 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002745 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002746
Jason Ekstrand7b982072014-05-20 14:33:03 -05002747 state->sx = 0;
2748 state->sy = 0;
2749 state->newly_attached = 0;
2750 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002751
2752 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002753 if (weston_timeline_enabled_ &&
2754 pixman_region32_not_empty(&state->damage))
2755 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002756 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002757 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002758 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002759 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002760 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002761
2762 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002763 pixman_region32_init(&opaque);
2764 pixman_region32_intersect_rect(&opaque, &state->opaque,
2765 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002766
2767 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2768 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002769 wl_list_for_each(view, &surface->views, surface_link)
2770 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002771 }
2772
2773 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002774
2775 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002776 pixman_region32_intersect_rect(&surface->input, &state->input,
2777 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002778
Pekka Paalanenbc106382012-10-10 12:49:31 +03002779 /* wl_surface.frame */
2780 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002781 &state->frame_callback_list);
2782 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002783
2784 /* XXX:
2785 * What should happen with a feedback request, if there
2786 * is no wl_buffer attached for this commit?
2787 */
2788
2789 /* presentation.feedback */
2790 wl_list_insert_list(&surface->feedback_list,
2791 &state->feedback_list);
2792 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002793}
2794
2795static void
2796weston_surface_commit(struct weston_surface *surface)
2797{
2798 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002799
Pekka Paalanene67858b2013-04-25 13:57:42 +03002800 weston_surface_commit_subsurface_order(surface);
2801
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002802 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002803}
2804
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002805static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002806weston_subsurface_commit(struct weston_subsurface *sub);
2807
2808static void
2809weston_subsurface_parent_commit(struct weston_subsurface *sub,
2810 int parent_is_synchronized);
2811
2812static void
2813surface_commit(struct wl_client *client, struct wl_resource *resource)
2814{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002815 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002816 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2817
2818 if (sub) {
2819 weston_subsurface_commit(sub);
2820 return;
2821 }
2822
2823 weston_surface_commit(surface);
2824
2825 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2826 if (sub->surface != surface)
2827 weston_subsurface_parent_commit(sub, 0);
2828 }
2829}
2830
2831static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002832surface_set_buffer_transform(struct wl_client *client,
2833 struct wl_resource *resource, int transform)
2834{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002835 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002836
Jonny Lamba55f1392014-05-30 12:07:15 +02002837 /* if wl_output.transform grows more members this will need to be updated. */
2838 if (transform < 0 ||
2839 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2840 wl_resource_post_error(resource,
2841 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2842 "buffer transform must be a valid transform "
2843 "('%d' specified)", transform);
2844 return;
2845 }
2846
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002847 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002848 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002849}
2850
Alexander Larsson4ea95522013-05-22 14:41:37 +02002851static void
2852surface_set_buffer_scale(struct wl_client *client,
2853 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002854 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002855{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002856 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002857
Jonny Lamba55f1392014-05-30 12:07:15 +02002858 if (scale < 1) {
2859 wl_resource_post_error(resource,
2860 WL_SURFACE_ERROR_INVALID_SCALE,
2861 "buffer scale must be at least one "
2862 "('%d' specified)", scale);
2863 return;
2864 }
2865
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002866 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002867 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002868}
2869
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002870static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002871 surface_destroy,
2872 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002873 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002874 surface_frame,
2875 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002876 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002877 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002878 surface_set_buffer_transform,
2879 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002880};
2881
2882static void
2883compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002884 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002885{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002886 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002887 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002888
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002889 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002890 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002891 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002892 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002893 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002894
Jason Ekstranda85118c2013-06-27 20:17:02 -05002895 surface->resource =
2896 wl_resource_create(client, &wl_surface_interface,
2897 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002898 if (surface->resource == NULL) {
2899 weston_surface_destroy(surface);
2900 wl_resource_post_no_memory(resource);
2901 return;
2902 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002903 wl_resource_set_implementation(surface->resource, &surface_interface,
2904 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002905
2906 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002907}
2908
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002909static void
2910destroy_region(struct wl_resource *resource)
2911{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002912 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002913
2914 pixman_region32_fini(&region->region);
2915 free(region);
2916}
2917
2918static void
2919region_destroy(struct wl_client *client, struct wl_resource *resource)
2920{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002921 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002922}
2923
2924static void
2925region_add(struct wl_client *client, struct wl_resource *resource,
2926 int32_t x, int32_t y, int32_t width, int32_t height)
2927{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002928 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002929
2930 pixman_region32_union_rect(&region->region, &region->region,
2931 x, y, width, height);
2932}
2933
2934static void
2935region_subtract(struct wl_client *client, struct wl_resource *resource,
2936 int32_t x, int32_t y, int32_t width, int32_t height)
2937{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002938 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002939 pixman_region32_t rect;
2940
2941 pixman_region32_init_rect(&rect, x, y, width, height);
2942 pixman_region32_subtract(&region->region, &region->region, &rect);
2943 pixman_region32_fini(&rect);
2944}
2945
2946static const struct wl_region_interface region_interface = {
2947 region_destroy,
2948 region_add,
2949 region_subtract
2950};
2951
2952static void
2953compositor_create_region(struct wl_client *client,
2954 struct wl_resource *resource, uint32_t id)
2955{
2956 struct weston_region *region;
2957
2958 region = malloc(sizeof *region);
2959 if (region == NULL) {
2960 wl_resource_post_no_memory(resource);
2961 return;
2962 }
2963
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002964 pixman_region32_init(&region->region);
2965
Jason Ekstranda85118c2013-06-27 20:17:02 -05002966 region->resource =
2967 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002968 if (region->resource == NULL) {
2969 free(region);
2970 wl_resource_post_no_memory(resource);
2971 return;
2972 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002973 wl_resource_set_implementation(region->resource, &region_interface,
2974 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002975}
2976
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002977static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002978 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002979 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002980};
2981
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002982static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002983weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2984{
2985 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002986
Jason Ekstrand7b982072014-05-20 14:33:03 -05002987 weston_surface_commit_state(surface, &sub->cached);
2988 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002989
2990 weston_surface_commit_subsurface_order(surface);
2991
2992 weston_surface_schedule_repaint(surface);
2993
Jason Ekstrand7b982072014-05-20 14:33:03 -05002994 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002995}
2996
2997static void
2998weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2999{
3000 struct weston_surface *surface = sub->surface;
3001
3002 /*
3003 * If this commit would cause the surface to move by the
3004 * attach(dx, dy) parameters, the old damage region must be
3005 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02003006 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03003007 */
3008 pixman_region32_translate(&sub->cached.damage,
3009 -surface->pending.sx, -surface->pending.sy);
3010 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
3011 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07003012 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013
3014 if (surface->pending.newly_attached) {
3015 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05003016 weston_surface_state_set_buffer(&sub->cached,
3017 surface->pending.buffer);
3018 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003019 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003020 weston_presentation_feedback_discard_list(
3021 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003022 }
3023 sub->cached.sx += surface->pending.sx;
3024 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02003025
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003026 sub->cached.buffer_viewport.changed |=
3027 surface->pending.buffer_viewport.changed;
3028 sub->cached.buffer_viewport.buffer =
3029 surface->pending.buffer_viewport.buffer;
3030 sub->cached.buffer_viewport.surface =
3031 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003033 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003034
3035 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
3036
3037 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
3038
3039 wl_list_insert_list(&sub->cached.frame_callback_list,
3040 &surface->pending.frame_callback_list);
3041 wl_list_init(&surface->pending.frame_callback_list);
3042
Pekka Paalanen133e4392014-09-23 22:08:46 -04003043 wl_list_insert_list(&sub->cached.feedback_list,
3044 &surface->pending.feedback_list);
3045 wl_list_init(&surface->pending.feedback_list);
3046
Jason Ekstrand7b982072014-05-20 14:33:03 -05003047 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003048}
3049
Derek Foreman280e7dd2014-10-03 13:13:42 -05003050static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03003051weston_subsurface_is_synchronized(struct weston_subsurface *sub)
3052{
3053 while (sub) {
3054 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003055 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003056
3057 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05003058 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003059
3060 sub = weston_surface_to_subsurface(sub->parent);
3061 }
3062
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01003063 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003064}
3065
3066static void
3067weston_subsurface_commit(struct weston_subsurface *sub)
3068{
3069 struct weston_surface *surface = sub->surface;
3070 struct weston_subsurface *tmp;
3071
3072 /* Recursive check for effectively synchronized. */
3073 if (weston_subsurface_is_synchronized(sub)) {
3074 weston_subsurface_commit_to_cache(sub);
3075 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05003076 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077 /* flush accumulated state from cache */
3078 weston_subsurface_commit_to_cache(sub);
3079 weston_subsurface_commit_from_cache(sub);
3080 } else {
3081 weston_surface_commit(surface);
3082 }
3083
3084 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3085 if (tmp->surface != surface)
3086 weston_subsurface_parent_commit(tmp, 0);
3087 }
3088 }
3089}
3090
3091static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003092weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003093{
3094 struct weston_surface *surface = sub->surface;
3095 struct weston_subsurface *tmp;
3096
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097 /* From now on, commit_from_cache the whole sub-tree, regardless of
3098 * the synchronized mode of each child. This sub-surface or some
3099 * of its ancestors were synchronized, so we are synchronized
3100 * all the way down.
3101 */
3102
Jason Ekstrand7b982072014-05-20 14:33:03 -05003103 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003104 weston_subsurface_commit_from_cache(sub);
3105
3106 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
3107 if (tmp->surface != surface)
3108 weston_subsurface_parent_commit(tmp, 1);
3109 }
3110}
3111
3112static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003113weston_subsurface_parent_commit(struct weston_subsurface *sub,
3114 int parent_is_synchronized)
3115{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003116 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003117 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05003118 wl_list_for_each(view, &sub->surface->views, surface_link)
3119 weston_view_set_position(view,
3120 sub->position.x,
3121 sub->position.y);
3122
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003123 sub->position.set = 0;
3124 }
3125
3126 if (parent_is_synchronized || sub->synchronized)
3127 weston_subsurface_synchronized_commit(sub);
3128}
3129
Pekka Paalanen8274d902014-08-06 19:36:51 +03003130static int
3131subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
3132{
3133 return snprintf(buf, len, "sub-surface");
3134}
3135
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003136static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003137subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138{
3139 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05003140 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003141
Jason Ekstranda7af7042013-10-12 22:38:11 -05003142 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06003143 weston_view_set_position(view,
3144 view->geometry.x + dx,
3145 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003146
3147 /* No need to check parent mappedness, because if parent is not
3148 * mapped, parent is not in a visible layer, so this sub-surface
3149 * will not be drawn either.
3150 */
3151 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003152 struct weston_output *output;
3153
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003154 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155 * because that would call it also for the parent surface,
3156 * which might not be mapped yet. That would lead to
3157 * inconsistent state, where the window could never be
3158 * mapped.
3159 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05003160 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03003161 * weston_surface_is_mapped() return true, so that when the
3162 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003163 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164 */
3165 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03003166 output = container_of(compositor->output_list.next,
3167 struct weston_output, link);
3168
3169 surface->output = output;
3170 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003171 }
3172}
3173
3174static struct weston_subsurface *
3175weston_surface_to_subsurface(struct weston_surface *surface)
3176{
3177 if (surface->configure == subsurface_configure)
3178 return surface->configure_private;
3179
3180 return NULL;
3181}
3182
Pekka Paalanen01388e22013-04-25 13:57:44 +03003183WL_EXPORT struct weston_surface *
3184weston_surface_get_main_surface(struct weston_surface *surface)
3185{
3186 struct weston_subsurface *sub;
3187
3188 while (surface && (sub = weston_surface_to_subsurface(surface)))
3189 surface = sub->parent;
3190
3191 return surface;
3192}
3193
Pekka Paalanen50b67472014-10-01 15:02:41 +03003194WL_EXPORT int
3195weston_surface_set_role(struct weston_surface *surface,
3196 const char *role_name,
3197 struct wl_resource *error_resource,
3198 uint32_t error_code)
3199{
3200 assert(role_name);
3201
3202 if (surface->role_name == NULL ||
3203 surface->role_name == role_name ||
3204 strcmp(surface->role_name, role_name) == 0) {
3205 surface->role_name = role_name;
3206
3207 return 0;
3208 }
3209
3210 wl_resource_post_error(error_resource, error_code,
3211 "Cannot assign role %s to wl_surface@%d,"
3212 " already has role %s\n",
3213 role_name,
3214 wl_resource_get_id(surface->resource),
3215 surface->role_name);
3216 return -1;
3217}
3218
Pekka Paalanen8274d902014-08-06 19:36:51 +03003219WL_EXPORT void
3220weston_surface_set_label_func(struct weston_surface *surface,
3221 int (*desc)(struct weston_surface *,
3222 char *, size_t))
3223{
3224 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02003225 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003226}
3227
Pekka Paalanenc647ed72015-02-09 13:16:57 +02003228/** Get the size of surface contents
3229 *
3230 * \param surface The surface to query.
3231 * \param width Returns the width of raw contents.
3232 * \param height Returns the height of raw contents.
3233 *
3234 * Retrieves the raw surface content size in pixels for the given surface.
3235 * This is the whole content size in buffer pixels. If the surface
3236 * has no content or the renderer does not implement this feature,
3237 * zeroes are returned.
3238 *
3239 * This function is used to determine the buffer size needed for
3240 * a weston_surface_copy_content() call.
3241 */
3242WL_EXPORT void
3243weston_surface_get_content_size(struct weston_surface *surface,
3244 int *width, int *height)
3245{
3246 struct weston_renderer *rer = surface->compositor->renderer;
3247
3248 if (!rer->surface_get_content_size) {
3249 *width = 0;
3250 *height = 0;
3251 return;
3252 }
3253
3254 rer->surface_get_content_size(surface, width, height);
3255}
3256
3257/** Copy surface contents to system memory.
3258 *
3259 * \param surface The surface to copy from.
3260 * \param target Pointer to the target memory buffer.
3261 * \param size Size of the target buffer in bytes.
3262 * \param src_x X location on contents to copy from.
3263 * \param src_y Y location on contents to copy from.
3264 * \param width Width in pixels of the area to copy.
3265 * \param height Height in pixels of the area to copy.
3266 * \return 0 for success, -1 for failure.
3267 *
3268 * Surface contents are maintained by the renderer. They can be in a
3269 * reserved weston_buffer or as a copy, e.g. a GL texture, or something
3270 * else.
3271 *
3272 * Surface contents are copied into memory pointed to by target,
3273 * which has size bytes of space available. The target memory
3274 * may be larger than needed, but being smaller returns an error.
3275 * The extra bytes in target may or may not be written; their content is
3276 * unspecified. Size must be large enough to hold the image.
3277 *
3278 * The image in the target memory will be arranged in rows from
3279 * top to bottom, and pixels on a row from left to right. The pixel
3280 * format is PIXMAN_a8b8g8r8, 4 bytes per pixel, and stride is exactly
3281 * width * 4.
3282 *
3283 * Parameters src_x and src_y define the upper-left corner in buffer
3284 * coordinates (pixels) to copy from. Parameters width and height
3285 * define the size of the area to copy in pixels.
3286 *
3287 * The rectangle defined by src_x, src_y, width, height must fit in
3288 * the surface contents. Otherwise an error is returned.
3289 *
3290 * Use surface_get_data_size to determine the content size; the
3291 * needed target buffer size and rectangle limits.
3292 *
3293 * CURRENT IMPLEMENTATION RESTRICTIONS:
3294 * - the machine must be little-endian due to Pixman formats.
3295 *
3296 * NOTE: Pixman formats are premultiplied.
3297 */
3298WL_EXPORT int
3299weston_surface_copy_content(struct weston_surface *surface,
3300 void *target, size_t size,
3301 int src_x, int src_y,
3302 int width, int height)
3303{
3304 struct weston_renderer *rer = surface->compositor->renderer;
3305 int cw, ch;
3306 const size_t bytespp = 4; /* PIXMAN_a8b8g8r8 */
3307
3308 if (!rer->surface_copy_content)
3309 return -1;
3310
3311 weston_surface_get_content_size(surface, &cw, &ch);
3312
3313 if (src_x < 0 || src_y < 0)
3314 return -1;
3315
3316 if (width <= 0 || height <= 0)
3317 return -1;
3318
3319 if (src_x + width > cw || src_y + height > ch)
3320 return -1;
3321
3322 if (width * bytespp * height > size)
3323 return -1;
3324
3325 return rer->surface_copy_content(surface, target, size,
3326 src_x, src_y, width, height);
3327}
3328
Pekka Paalanene67858b2013-04-25 13:57:42 +03003329static void
3330subsurface_set_position(struct wl_client *client,
3331 struct wl_resource *resource, int32_t x, int32_t y)
3332{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003333 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003334
3335 if (!sub)
3336 return;
3337
3338 sub->position.x = x;
3339 sub->position.y = y;
3340 sub->position.set = 1;
3341}
3342
3343static struct weston_subsurface *
3344subsurface_from_surface(struct weston_surface *surface)
3345{
3346 struct weston_subsurface *sub;
3347
3348 sub = weston_surface_to_subsurface(surface);
3349 if (sub)
3350 return sub;
3351
3352 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
3353 if (sub->surface == surface)
3354 return sub;
3355
3356 return NULL;
3357}
3358
3359static struct weston_subsurface *
3360subsurface_sibling_check(struct weston_subsurface *sub,
3361 struct weston_surface *surface,
3362 const char *request)
3363{
3364 struct weston_subsurface *sibling;
3365
3366 sibling = subsurface_from_surface(surface);
3367
3368 if (!sibling) {
3369 wl_resource_post_error(sub->resource,
3370 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3371 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003372 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003373 return NULL;
3374 }
3375
3376 if (sibling->parent != sub->parent) {
3377 wl_resource_post_error(sub->resource,
3378 WL_SUBSURFACE_ERROR_BAD_SURFACE,
3379 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003380 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003381 return NULL;
3382 }
3383
3384 return sibling;
3385}
3386
3387static void
3388subsurface_place_above(struct wl_client *client,
3389 struct wl_resource *resource,
3390 struct wl_resource *sibling_resource)
3391{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003392 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003393 struct weston_surface *surface =
3394 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003395 struct weston_subsurface *sibling;
3396
3397 if (!sub)
3398 return;
3399
3400 sibling = subsurface_sibling_check(sub, surface, "place_above");
3401 if (!sibling)
3402 return;
3403
3404 wl_list_remove(&sub->parent_link_pending);
3405 wl_list_insert(sibling->parent_link_pending.prev,
3406 &sub->parent_link_pending);
3407}
3408
3409static void
3410subsurface_place_below(struct wl_client *client,
3411 struct wl_resource *resource,
3412 struct wl_resource *sibling_resource)
3413{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003414 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003415 struct weston_surface *surface =
3416 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003417 struct weston_subsurface *sibling;
3418
3419 if (!sub)
3420 return;
3421
3422 sibling = subsurface_sibling_check(sub, surface, "place_below");
3423 if (!sibling)
3424 return;
3425
3426 wl_list_remove(&sub->parent_link_pending);
3427 wl_list_insert(&sibling->parent_link_pending,
3428 &sub->parent_link_pending);
3429}
3430
3431static void
3432subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3433{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003434 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003435
3436 if (sub)
3437 sub->synchronized = 1;
3438}
3439
3440static void
3441subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3442{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003443 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003444
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003445 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003446 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003447
3448 /* If sub became effectively desynchronized, flush. */
3449 if (!weston_subsurface_is_synchronized(sub))
3450 weston_subsurface_synchronized_commit(sub);
3451 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003452}
3453
3454static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003455weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3456{
3457 wl_list_remove(&sub->parent_link);
3458 wl_list_remove(&sub->parent_link_pending);
3459 wl_list_remove(&sub->parent_destroy_listener.link);
3460 sub->parent = NULL;
3461}
3462
3463static void
3464weston_subsurface_destroy(struct weston_subsurface *sub);
3465
3466static void
3467subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3468{
3469 struct weston_subsurface *sub =
3470 container_of(listener, struct weston_subsurface,
3471 surface_destroy_listener);
3472 assert(data == &sub->surface->resource);
3473
3474 /* The protocol object (wl_resource) is left inert. */
3475 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003476 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003477
3478 weston_subsurface_destroy(sub);
3479}
3480
3481static void
3482subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3483{
3484 struct weston_subsurface *sub =
3485 container_of(listener, struct weston_subsurface,
3486 parent_destroy_listener);
3487 assert(data == &sub->parent->resource);
3488 assert(sub->surface != sub->parent);
3489
3490 if (weston_surface_is_mapped(sub->surface))
3491 weston_surface_unmap(sub->surface);
3492
3493 weston_subsurface_unlink_parent(sub);
3494}
3495
3496static void
3497subsurface_resource_destroy(struct wl_resource *resource)
3498{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003499 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003500
3501 if (sub)
3502 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003503}
3504
3505static void
3506subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3507{
3508 wl_resource_destroy(resource);
3509}
3510
3511static void
3512weston_subsurface_link_parent(struct weston_subsurface *sub,
3513 struct weston_surface *parent)
3514{
3515 sub->parent = parent;
3516 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003517 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003518 &sub->parent_destroy_listener);
3519
3520 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3521 wl_list_insert(&parent->subsurface_list_pending,
3522 &sub->parent_link_pending);
3523}
3524
3525static void
3526weston_subsurface_link_surface(struct weston_subsurface *sub,
3527 struct weston_surface *surface)
3528{
3529 sub->surface = surface;
3530 sub->surface_destroy_listener.notify =
3531 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003532 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003533 &sub->surface_destroy_listener);
3534}
3535
3536static void
3537weston_subsurface_destroy(struct weston_subsurface *sub)
3538{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003539 struct weston_view *view, *next;
3540
Pekka Paalanene67858b2013-04-25 13:57:42 +03003541 assert(sub->surface);
3542
3543 if (sub->resource) {
3544 assert(weston_surface_to_subsurface(sub->surface) == sub);
3545 assert(sub->parent_destroy_listener.notify ==
3546 subsurface_handle_parent_destroy);
3547
George Kiagiadakised04d382014-06-13 18:10:26 +02003548 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3549 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003550 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003551 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003552
Pekka Paalanene67858b2013-04-25 13:57:42 +03003553 if (sub->parent)
3554 weston_subsurface_unlink_parent(sub);
3555
Jason Ekstrand7b982072014-05-20 14:33:03 -05003556 weston_surface_state_fini(&sub->cached);
3557 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003558
3559 sub->surface->configure = NULL;
3560 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003561 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003562 } else {
3563 /* the dummy weston_subsurface for the parent itself */
3564 assert(sub->parent_destroy_listener.notify == NULL);
3565 wl_list_remove(&sub->parent_link);
3566 wl_list_remove(&sub->parent_link_pending);
3567 }
3568
3569 wl_list_remove(&sub->surface_destroy_listener.link);
3570 free(sub);
3571}
3572
3573static const struct wl_subsurface_interface subsurface_implementation = {
3574 subsurface_destroy,
3575 subsurface_set_position,
3576 subsurface_place_above,
3577 subsurface_place_below,
3578 subsurface_set_sync,
3579 subsurface_set_desync
3580};
3581
3582static struct weston_subsurface *
3583weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3584 struct weston_surface *parent)
3585{
3586 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003587 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003588
Bryce Harringtonde16d892014-11-20 22:21:57 -08003589 sub = zalloc(sizeof *sub);
3590 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003591 return NULL;
3592
Jason Ekstranda7af7042013-10-12 22:38:11 -05003593 wl_list_init(&sub->unused_views);
3594
Jason Ekstranda85118c2013-06-27 20:17:02 -05003595 sub->resource =
3596 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003597 if (!sub->resource) {
3598 free(sub);
3599 return NULL;
3600 }
3601
Jason Ekstranda85118c2013-06-27 20:17:02 -05003602 wl_resource_set_implementation(sub->resource,
3603 &subsurface_implementation,
3604 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003605 weston_subsurface_link_surface(sub, surface);
3606 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003607 weston_surface_state_init(&sub->cached);
3608 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003609 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003610
3611 return sub;
3612}
3613
3614/* Create a dummy subsurface for having the parent itself in its
3615 * sub-surface lists. Makes stacking order manipulation easy.
3616 */
3617static struct weston_subsurface *
3618weston_subsurface_create_for_parent(struct weston_surface *parent)
3619{
3620 struct weston_subsurface *sub;
3621
Bryce Harringtonde16d892014-11-20 22:21:57 -08003622 sub = zalloc(sizeof *sub);
3623 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003624 return NULL;
3625
3626 weston_subsurface_link_surface(sub, parent);
3627 sub->parent = parent;
3628 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3629 wl_list_insert(&parent->subsurface_list_pending,
3630 &sub->parent_link_pending);
3631
3632 return sub;
3633}
3634
3635static void
3636subcompositor_get_subsurface(struct wl_client *client,
3637 struct wl_resource *resource,
3638 uint32_t id,
3639 struct wl_resource *surface_resource,
3640 struct wl_resource *parent_resource)
3641{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003642 struct weston_surface *surface =
3643 wl_resource_get_user_data(surface_resource);
3644 struct weston_surface *parent =
3645 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003646 struct weston_subsurface *sub;
3647 static const char where[] = "get_subsurface: wl_subsurface@";
3648
3649 if (surface == parent) {
3650 wl_resource_post_error(resource,
3651 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3652 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003653 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003654 return;
3655 }
3656
3657 if (weston_surface_to_subsurface(surface)) {
3658 wl_resource_post_error(resource,
3659 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3660 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003661 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003662 return;
3663 }
3664
Pekka Paalanen50b67472014-10-01 15:02:41 +03003665 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3666 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003667 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003668
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003669 if (weston_surface_get_main_surface(parent) == surface) {
3670 wl_resource_post_error(resource,
3671 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3672 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003673 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003674 return;
3675 }
3676
Pekka Paalanene67858b2013-04-25 13:57:42 +03003677 /* make sure the parent is in its own list */
3678 if (wl_list_empty(&parent->subsurface_list)) {
3679 if (!weston_subsurface_create_for_parent(parent)) {
3680 wl_resource_post_no_memory(resource);
3681 return;
3682 }
3683 }
3684
3685 sub = weston_subsurface_create(id, surface, parent);
3686 if (!sub) {
3687 wl_resource_post_no_memory(resource);
3688 return;
3689 }
3690
3691 surface->configure = subsurface_configure;
3692 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003693 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003694}
3695
3696static void
3697subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3698{
3699 wl_resource_destroy(resource);
3700}
3701
3702static const struct wl_subcompositor_interface subcompositor_interface = {
3703 subcompositor_destroy,
3704 subcompositor_get_subsurface
3705};
3706
3707static void
3708bind_subcompositor(struct wl_client *client,
3709 void *data, uint32_t version, uint32_t id)
3710{
3711 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003712 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003713
Jason Ekstranda85118c2013-06-27 20:17:02 -05003714 resource =
3715 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003716 if (resource == NULL) {
3717 wl_client_post_no_memory(client);
3718 return;
3719 }
3720 wl_resource_set_implementation(resource, &subcompositor_interface,
3721 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003722}
3723
3724static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003725weston_compositor_dpms(struct weston_compositor *compositor,
3726 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003727{
3728 struct weston_output *output;
3729
3730 wl_list_for_each(output, &compositor->output_list, link)
3731 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003732 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003733}
3734
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003735WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003736weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003737{
Neil Roberts8b62e202013-09-30 13:14:47 +01003738 uint32_t old_state = compositor->state;
3739
3740 /* The state needs to be changed before emitting the wake
3741 * signal because that may try to schedule a repaint which
3742 * will not work if the compositor is still sleeping */
3743 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3744
3745 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003746 case WESTON_COMPOSITOR_SLEEPING:
3747 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3748 /* fall through */
3749 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003750 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003751 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003752 /* fall through */
3753 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003754 wl_event_source_timer_update(compositor->idle_source,
3755 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003756 }
3757}
3758
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003759WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003760weston_compositor_offscreen(struct weston_compositor *compositor)
3761{
3762 switch (compositor->state) {
3763 case WESTON_COMPOSITOR_OFFSCREEN:
3764 return;
3765 case WESTON_COMPOSITOR_SLEEPING:
3766 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3767 /* fall through */
3768 default:
3769 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3770 wl_event_source_timer_update(compositor->idle_source, 0);
3771 }
3772}
3773
3774WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003775weston_compositor_sleep(struct weston_compositor *compositor)
3776{
3777 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3778 return;
3779
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003780 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003781 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3782 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3783}
3784
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003785static int
3786idle_handler(void *data)
3787{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003788 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003789
3790 if (compositor->idle_inhibit)
3791 return 1;
3792
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003793 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003794 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003795
3796 return 1;
3797}
3798
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003799WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003800weston_plane_init(struct weston_plane *plane,
3801 struct weston_compositor *ec,
3802 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003803{
3804 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003805 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003806 plane->x = x;
3807 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003808 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003809
3810 /* Init the link so that the call to wl_list_remove() when releasing
3811 * the plane without ever stacking doesn't lead to a crash */
3812 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003813}
3814
3815WL_EXPORT void
3816weston_plane_release(struct weston_plane *plane)
3817{
Xiong Zhang97116532013-10-23 13:58:31 +08003818 struct weston_view *view;
3819
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003820 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003821 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003822
Xiong Zhang97116532013-10-23 13:58:31 +08003823 wl_list_for_each(view, &plane->compositor->view_list, link) {
3824 if (view->plane == plane)
3825 view->plane = NULL;
3826 }
3827
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003828 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003829}
3830
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003831WL_EXPORT void
3832weston_compositor_stack_plane(struct weston_compositor *ec,
3833 struct weston_plane *plane,
3834 struct weston_plane *above)
3835{
3836 if (above)
3837 wl_list_insert(above->link.prev, &plane->link);
3838 else
3839 wl_list_insert(&ec->plane_list, &plane->link);
3840}
3841
Casey Dahlin9074db52012-04-19 22:50:09 -04003842static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003843{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003844 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003845}
3846
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003847static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003848bind_output(struct wl_client *client,
3849 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003850{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003851 struct weston_output *output = data;
3852 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003853 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003854
Jason Ekstranda85118c2013-06-27 20:17:02 -05003855 resource = wl_resource_create(client, &wl_output_interface,
3856 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003857 if (resource == NULL) {
3858 wl_client_post_no_memory(client);
3859 return;
3860 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003861
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003862 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003863 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003864
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003865 wl_output_send_geometry(resource,
3866 output->x,
3867 output->y,
3868 output->mm_width,
3869 output->mm_height,
3870 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003871 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003872 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003873 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003874 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003875 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003876
3877 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003878 wl_output_send_mode(resource,
3879 mode->flags,
3880 mode->width,
3881 mode->height,
3882 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003883 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003884
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003885 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003886 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003887}
3888
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003889/* Move other outputs when one is removed so the space remains contiguos. */
3890static void
3891weston_compositor_remove_output(struct weston_compositor *compositor,
3892 struct weston_output *remove_output)
3893{
3894 struct weston_output *output;
3895 int offset = 0;
3896
3897 wl_list_for_each(output, &compositor->output_list, link) {
3898 if (output == remove_output) {
3899 offset = output->width;
3900 continue;
3901 }
3902
3903 if (offset > 0) {
3904 weston_output_move(output,
3905 output->x - offset, output->y);
3906 output->dirty = 1;
3907 }
3908 }
3909}
3910
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003911WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003912weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003913{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003914 struct wl_resource *resource;
3915
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003916 output->destroying = 1;
3917
Pekka Paalanen0513a952014-05-21 16:17:27 +03003918 wl_event_source_remove(output->repaint_timer);
3919
Pekka Paalanen133e4392014-09-23 22:08:46 -04003920 weston_presentation_feedback_discard_list(&output->feedback_list);
3921
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003922 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003923 wl_list_remove(&output->link);
3924
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003925 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003926 wl_signal_emit(&output->destroy_signal, output);
3927
Richard Hughesafe690c2013-05-02 10:10:04 +01003928 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003929 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003930 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003931 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003932
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003933 wl_resource_for_each(resource, &output->resource_list) {
3934 wl_resource_set_destructor(resource, NULL);
3935 }
3936
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003937 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003938}
3939
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003940WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003941weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003942{
Scott Moreau850ca422012-05-21 15:21:25 -06003943 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003944
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003945 weston_matrix_init(&output->matrix);
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003946 weston_matrix_translate(&output->matrix, -output->x, -output->y, 0);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003947
Scott Moreauccbf29d2012-02-22 14:21:41 -07003948 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003949 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003950 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003951 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003952 -output->zoom.trans_y, 0);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003953 weston_matrix_scale(&output->matrix, magnification,
3954 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003955 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003956
Jason Ekstrandfb23df72014-10-16 10:55:21 -05003957 switch (output->transform) {
3958 case WL_OUTPUT_TRANSFORM_FLIPPED:
3959 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3960 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3961 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3962 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3963 weston_matrix_scale(&output->matrix, -1, 1, 1);
3964 break;
3965 }
3966
3967 switch (output->transform) {
3968 default:
3969 case WL_OUTPUT_TRANSFORM_NORMAL:
3970 case WL_OUTPUT_TRANSFORM_FLIPPED:
3971 break;
3972 case WL_OUTPUT_TRANSFORM_90:
3973 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3974 weston_matrix_translate(&output->matrix, 0, -output->height, 0);
3975 weston_matrix_rotate_xy(&output->matrix, 0, 1);
3976 break;
3977 case WL_OUTPUT_TRANSFORM_180:
3978 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3979 weston_matrix_translate(&output->matrix,
3980 -output->width, -output->height, 0);
3981 weston_matrix_rotate_xy(&output->matrix, -1, 0);
3982 break;
3983 case WL_OUTPUT_TRANSFORM_270:
3984 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3985 weston_matrix_translate(&output->matrix, -output->width, 0, 0);
3986 weston_matrix_rotate_xy(&output->matrix, 0, -1);
3987 break;
3988 }
3989
3990 if (output->current_scale != 1)
3991 weston_matrix_scale(&output->matrix,
3992 output->current_scale,
3993 output->current_scale, 1);
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003994
Scott Moreauccbf29d2012-02-22 14:21:41 -07003995 output->dirty = 0;
3996}
3997
Scott Moreau1bad5db2012-08-18 01:04:05 -06003998static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003999weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06004000{
4001 output->transform = transform;
4002
4003 switch (transform) {
4004 case WL_OUTPUT_TRANSFORM_90:
4005 case WL_OUTPUT_TRANSFORM_270:
4006 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
4007 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
4008 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02004009 output->width = output->current_mode->height;
4010 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004011 break;
4012 case WL_OUTPUT_TRANSFORM_NORMAL:
4013 case WL_OUTPUT_TRANSFORM_180:
4014 case WL_OUTPUT_TRANSFORM_FLIPPED:
4015 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02004016 output->width = output->current_mode->width;
4017 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06004018 break;
4019 default:
4020 break;
4021 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06004022
Hardening57388e42013-09-18 23:56:36 +02004023 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02004024 output->width /= scale;
4025 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02004026}
4027
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004028static void
4029weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07004030{
4031 output->x = x;
4032 output->y = y;
4033
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02004034 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07004035 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06004036 output->width,
4037 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004038}
4039
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004040WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004041weston_output_move(struct weston_output *output, int x, int y)
4042{
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004043 struct wl_resource *resource;
4044
4045 output->move_x = x - output->x;
4046 output->move_y = y - output->y;
4047
4048 if (output->move_x == 0 && output->move_y == 0)
4049 return;
4050
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004051 weston_output_init_geometry(output, x, y);
4052
4053 output->dirty = 1;
4054
4055 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004056 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004057
4058 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08004059 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004060 wl_output_send_geometry(resource,
4061 output->x,
4062 output->y,
4063 output->mm_width,
4064 output->mm_height,
4065 output->subpixel,
4066 output->make,
4067 output->model,
4068 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08004069
4070 if (wl_resource_get_version(resource) >= 2)
4071 wl_output_send_done(resource);
4072 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004073}
4074
4075WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004076weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02004077 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02004078 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004079{
Pekka Paalanen0513a952014-05-21 16:17:27 +03004080 struct wl_event_loop *loop;
4081
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004082 output->compositor = c;
4083 output->x = x;
4084 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02004085 output->mm_width = mm_width;
4086 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004087 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02004088 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07004089
Alexander Larsson0b135062013-05-28 16:23:36 +02004090 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06004091 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004092
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02004093 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02004094 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01004095
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04004096 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01004097 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06004098 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04004099 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04004100 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02004101
Pekka Paalanen0513a952014-05-21 16:17:27 +03004102 loop = wl_display_get_event_loop(c->wl_display);
4103 output->repaint_timer = wl_event_loop_add_timer(loop,
4104 output_repaint_timer_handler, output);
4105
Casey Dahlin58ba1372012-04-19 22:50:08 -04004106 output->id = ffs(~output->compositor->output_id_pool) - 1;
4107 output->compositor->output_id_pool |= 1 << output->id;
4108
Benjamin Franzkeb6879402012-04-10 18:28:54 +02004109 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004110 wl_global_create(c->wl_display, &wl_output_interface, 2,
4111 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01004112 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004113}
4114
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004115WL_EXPORT void
4116weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05004117 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004118 wl_fixed_t *x, wl_fixed_t *y)
4119{
Derek Foreman0f679412014-10-02 13:41:17 -05004120 struct weston_vector p = { {
4121 wl_fixed_to_double(device_x),
4122 wl_fixed_to_double(device_y),
4123 0.0,
4124 1.0 } };
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004125
Derek Foreman0f679412014-10-02 13:41:17 -05004126 weston_matrix_transform(&output->matrix, &p);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004127
Derek Foreman0f679412014-10-02 13:41:17 -05004128 *x = wl_fixed_from_double(p.f[0] / p.f[3]);
4129 *y = wl_fixed_from_double(p.f[1] / p.f[3]);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07004130}
4131
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01004132static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004133destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004134{
Jonny Lamb74130762013-11-26 18:19:46 +01004135 struct weston_surface *surface =
4136 wl_resource_get_user_data(resource);
4137
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004138 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02004139 surface->pending.buffer_viewport.buffer.src_width =
4140 wl_fixed_from_int(-1);
4141 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004142 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004143}
4144
4145static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004146viewport_destroy(struct wl_client *client,
4147 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004148{
4149 wl_resource_destroy(resource);
4150}
4151
4152static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004153viewport_set(struct wl_client *client,
4154 struct wl_resource *resource,
4155 wl_fixed_t src_x,
4156 wl_fixed_t src_y,
4157 wl_fixed_t src_width,
4158 wl_fixed_t src_height,
4159 int32_t dst_width,
4160 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004161{
Jonny Lamb74130762013-11-26 18:19:46 +01004162 struct weston_surface *surface =
4163 wl_resource_get_user_data(resource);
4164
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004165 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01004166
Jonny Lamb8ae35902013-11-26 18:19:45 +01004167 if (wl_fixed_to_double(src_width) < 0 ||
4168 wl_fixed_to_double(src_height) < 0) {
4169 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004170 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004171 "source dimensions must be non-negative (%fx%f)",
4172 wl_fixed_to_double(src_width),
4173 wl_fixed_to_double(src_height));
4174 return;
4175 }
4176
4177 if (dst_width <= 0 || dst_height <= 0) {
4178 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004179 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004180 "destination dimensions must be positive (%dx%d)",
4181 dst_width, dst_height);
4182 return;
4183 }
4184
Pekka Paalanen952b6c82014-03-14 14:38:15 +02004185 surface->pending.buffer_viewport.buffer.src_x = src_x;
4186 surface->pending.buffer_viewport.buffer.src_y = src_y;
4187 surface->pending.buffer_viewport.buffer.src_width = src_width;
4188 surface->pending.buffer_viewport.buffer.src_height = src_height;
4189 surface->pending.buffer_viewport.surface.width = dst_width;
4190 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004191 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004192}
4193
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004194static void
4195viewport_set_source(struct wl_client *client,
4196 struct wl_resource *resource,
4197 wl_fixed_t src_x,
4198 wl_fixed_t src_y,
4199 wl_fixed_t src_width,
4200 wl_fixed_t src_height)
4201{
4202 struct weston_surface *surface =
4203 wl_resource_get_user_data(resource);
4204
4205 assert(surface->viewport_resource != NULL);
4206
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004207 if (src_width == wl_fixed_from_int(-1) &&
4208 src_height == wl_fixed_from_int(-1)) {
4209 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004210 surface->pending.buffer_viewport.buffer.src_width =
4211 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004212 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004213 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004214 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004215
4216 if (src_width <= 0 || src_height <= 0) {
4217 wl_resource_post_error(resource,
4218 WL_VIEWPORT_ERROR_BAD_VALUE,
4219 "source size must be positive (%fx%f)",
4220 wl_fixed_to_double(src_width),
4221 wl_fixed_to_double(src_height));
4222 return;
4223 }
4224
4225 surface->pending.buffer_viewport.buffer.src_x = src_x;
4226 surface->pending.buffer_viewport.buffer.src_y = src_y;
4227 surface->pending.buffer_viewport.buffer.src_width = src_width;
4228 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004229 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004230}
4231
4232static void
4233viewport_set_destination(struct wl_client *client,
4234 struct wl_resource *resource,
4235 int32_t dst_width,
4236 int32_t dst_height)
4237{
4238 struct weston_surface *surface =
4239 wl_resource_get_user_data(resource);
4240
4241 assert(surface->viewport_resource != NULL);
4242
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004243 if (dst_width == -1 && dst_height == -1) {
4244 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004245 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004246 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004247 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004248 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03004249
4250 if (dst_width <= 0 || dst_height <= 0) {
4251 wl_resource_post_error(resource,
4252 WL_VIEWPORT_ERROR_BAD_VALUE,
4253 "destination size must be positive (%dx%d)",
4254 dst_width, dst_height);
4255 return;
4256 }
4257
4258 surface->pending.buffer_viewport.surface.width = dst_width;
4259 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02004260 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004261}
4262
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004263static const struct wl_viewport_interface viewport_interface = {
4264 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004265 viewport_set,
4266 viewport_set_source,
4267 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01004268};
4269
4270static void
4271scaler_destroy(struct wl_client *client,
4272 struct wl_resource *resource)
4273{
4274 wl_resource_destroy(resource);
4275}
4276
4277static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004278scaler_get_viewport(struct wl_client *client,
4279 struct wl_resource *scaler,
4280 uint32_t id,
4281 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01004282{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004283 int version = wl_resource_get_version(scaler);
4284 struct weston_surface *surface =
4285 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004286 struct wl_resource *resource;
4287
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004288 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01004289 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004290 WL_SCALER_ERROR_VIEWPORT_EXISTS,
4291 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01004292 return;
4293 }
4294
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004295 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004296 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004297 if (resource == NULL) {
4298 wl_client_post_no_memory(client);
4299 return;
4300 }
4301
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004302 wl_resource_set_implementation(resource, &viewport_interface,
4303 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01004304
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004305 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01004306}
4307
4308static const struct wl_scaler_interface scaler_interface = {
4309 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02004310 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01004311};
4312
4313static void
4314bind_scaler(struct wl_client *client,
4315 void *data, uint32_t version, uint32_t id)
4316{
4317 struct wl_resource *resource;
4318
4319 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004320 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01004321 if (resource == NULL) {
4322 wl_client_post_no_memory(client);
4323 return;
4324 }
4325
4326 wl_resource_set_implementation(resource, &scaler_interface,
4327 NULL, NULL);
4328}
4329
4330static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04004331destroy_presentation_feedback(struct wl_resource *feedback_resource)
4332{
4333 struct weston_presentation_feedback *feedback;
4334
4335 feedback = wl_resource_get_user_data(feedback_resource);
4336
4337 wl_list_remove(&feedback->link);
4338 free(feedback);
4339}
4340
4341static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004342presentation_destroy(struct wl_client *client, struct wl_resource *resource)
4343{
4344 wl_resource_destroy(resource);
4345}
4346
4347static void
4348presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004349 struct wl_resource *presentation_resource,
4350 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004351 uint32_t callback)
4352{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004353 struct weston_surface *surface;
4354 struct weston_presentation_feedback *feedback;
4355
4356 surface = wl_resource_get_user_data(surface_resource);
4357
Bryce Harringtonde16d892014-11-20 22:21:57 -08004358 feedback = zalloc(sizeof *feedback);
4359 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004360 goto err_calloc;
4361
4362 feedback->resource = wl_resource_create(client,
4363 &presentation_feedback_interface,
4364 1, callback);
4365 if (!feedback->resource)
4366 goto err_create;
4367
4368 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4369 destroy_presentation_feedback);
4370
4371 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4372
4373 return;
4374
4375err_create:
4376 free(feedback);
4377
4378err_calloc:
4379 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004380}
4381
4382static const struct presentation_interface presentation_implementation = {
4383 presentation_destroy,
4384 presentation_feedback
4385};
4386
4387static void
4388bind_presentation(struct wl_client *client,
4389 void *data, uint32_t version, uint32_t id)
4390{
4391 struct weston_compositor *compositor = data;
4392 struct wl_resource *resource;
4393
4394 resource = wl_resource_create(client, &presentation_interface,
4395 MIN(version, 1), id);
4396 if (resource == NULL) {
4397 wl_client_post_no_memory(client);
4398 return;
4399 }
4400
4401 wl_resource_set_implementation(resource, &presentation_implementation,
4402 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004403 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004404}
4405
4406static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004407compositor_bind(struct wl_client *client,
4408 void *data, uint32_t version, uint32_t id)
4409{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004410 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004411 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004412
Jason Ekstranda85118c2013-06-27 20:17:02 -05004413 resource = wl_resource_create(client, &wl_compositor_interface,
4414 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004415 if (resource == NULL) {
4416 wl_client_post_no_memory(client);
4417 return;
4418 }
4419
4420 wl_resource_set_implementation(resource, &compositor_interface,
4421 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004422}
4423
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004424static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004425log_uname(void)
4426{
4427 struct utsname usys;
4428
4429 uname(&usys);
4430
4431 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4432 usys.version, usys.machine);
4433}
4434
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004435WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004436weston_environment_get_fd(const char *env)
4437{
4438 char *e, *end;
4439 int fd, flags;
4440
4441 e = getenv(env);
4442 if (!e)
4443 return -1;
4444 fd = strtol(e, &end, 0);
4445 if (*end != '\0')
4446 return -1;
4447
4448 flags = fcntl(fd, F_GETFD);
4449 if (flags == -1)
4450 return -1;
4451
4452 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4453 unsetenv(env);
4454
4455 return fd;
4456}
4457
Pekka Paalanenb5026542014-11-12 15:09:24 +02004458static void
4459timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4460 uint32_t key, void *data)
4461{
4462 struct weston_compositor *compositor = data;
4463
4464 if (weston_timeline_enabled_)
4465 weston_timeline_close();
4466 else
4467 weston_timeline_open(compositor);
4468}
4469
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004470WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004471weston_compositor_init(struct weston_compositor *ec,
4472 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004473 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004474 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004475{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004476 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004477 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004478 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004479
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004480 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004481 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004482 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004483 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004484 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004485 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004486 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004487 wl_signal_init(&ec->idle_signal);
4488 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004489 wl_signal_init(&ec->show_input_panel_signal);
4490 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004491 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004492 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004493 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004494 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004495 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004496 wl_signal_init(&ec->session_signal);
4497 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004498
Casey Dahlin58ba1372012-04-19 22:50:08 -04004499 ec->output_id_pool = 0;
4500
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004501 if (!wl_global_create(display, &wl_compositor_interface, 3,
4502 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004503 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004504
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004505 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4506 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004507 return -1;
4508
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004509 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004510 ec, bind_scaler))
4511 return -1;
4512
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004513 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4514 ec, bind_presentation))
4515 return -1;
4516
Jason Ekstranda7af7042013-10-12 22:38:11 -05004517 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004518 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004519 wl_list_init(&ec->layer_list);
4520 wl_list_init(&ec->seat_list);
4521 wl_list_init(&ec->output_list);
4522 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004523 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004524 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004525 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004526 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004527 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004528
Xiong Zhang97116532013-10-23 13:58:31 +08004529 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004530 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004531
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004532 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4533 weston_config_section_get_string(s, "keymap_rules",
4534 (char **) &xkb_names.rules, NULL);
4535 weston_config_section_get_string(s, "keymap_model",
4536 (char **) &xkb_names.model, NULL);
4537 weston_config_section_get_string(s, "keymap_layout",
4538 (char **) &xkb_names.layout, NULL);
4539 weston_config_section_get_string(s, "keymap_variant",
4540 (char **) &xkb_names.variant, NULL);
4541 weston_config_section_get_string(s, "keymap_options",
4542 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004543
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004544 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4545 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004546
Jonny Lamb66a41a02014-08-12 14:58:25 +02004547 weston_config_section_get_int(s, "repeat-rate",
4548 &ec->kb_repeat_rate, 40);
4549 weston_config_section_get_int(s, "repeat-delay",
4550 &ec->kb_repeat_delay, 400);
4551
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004552 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004553
4554 wl_data_device_manager_init(ec->wl_display);
4555
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004556 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004557
Daniel Stone725c2c32012-06-22 14:04:36 +01004558 loop = wl_display_get_event_loop(ec->wl_display);
4559 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4560 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4561
4562 ec->input_loop = wl_event_loop_create();
4563
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004564 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4565 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4566
Pekka Paalanenb5026542014-11-12 15:09:24 +02004567 weston_compositor_add_debug_binding(ec, KEY_T,
4568 timeline_key_binding_handler, ec);
4569
Pekka Paalanen0513a952014-05-21 16:17:27 +03004570 s = weston_config_get_section(ec->config, "core", NULL, NULL);
4571 weston_config_section_get_int(s, "repaint-window", &ec->repaint_msec,
4572 DEFAULT_REPAINT_WINDOW);
4573 if (ec->repaint_msec < -10 || ec->repaint_msec > 1000) {
4574 weston_log("Invalid repaint_window value in config: %d\n",
4575 ec->repaint_msec);
4576 ec->repaint_msec = DEFAULT_REPAINT_WINDOW;
4577 }
4578 weston_log("Output repaint window is %d ms maximum.\n",
4579 ec->repaint_msec);
4580
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004581 weston_compositor_schedule_repaint(ec);
4582
Daniel Stone725c2c32012-06-22 14:04:36 +01004583 return 0;
4584}
4585
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004586WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004587weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004588{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004589 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004590
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004591 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004592 if (ec->input_loop_source)
4593 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004594
Matt Roper361d2ad2011-08-29 13:52:23 -07004595 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004596 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004597 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004598
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004599 if (ec->renderer)
4600 ec->renderer->destroy(ec);
4601
Daniel Stone325fc2d2012-05-30 16:31:58 +01004602 weston_binding_list_destroy_all(&ec->key_binding_list);
4603 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004604 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004605 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004606 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004607
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004608 weston_plane_release(&ec->primary_plane);
4609
Jonas Ådahlc97af922012-03-28 22:36:09 +02004610 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004611
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004612 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004613}
4614
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004615WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004616weston_compositor_exit_with_code(struct weston_compositor *compositor,
4617 int exit_code)
4618{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004619 if (compositor->exit_code == EXIT_SUCCESS)
4620 compositor->exit_code = exit_code;
4621
Frederic Plourdec336f062014-10-29 14:44:33 -04004622 wl_display_terminate(compositor->wl_display);
4623}
4624
4625WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004626weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4627 const struct weston_pointer_grab_interface *interface)
4628{
4629 struct weston_seat *seat;
4630
4631 ec->default_pointer_grab = interface;
4632 wl_list_for_each(seat, &ec->seat_list, link) {
4633 if (seat->pointer) {
4634 weston_pointer_set_default_grab(seat->pointer,
4635 interface);
4636 }
4637 }
4638}
4639
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004640WL_EXPORT int
4641weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4642 clockid_t clk_id)
4643{
4644 struct timespec ts;
4645
4646 if (clock_gettime(clk_id, &ts) < 0)
4647 return -1;
4648
4649 compositor->presentation_clock = clk_id;
4650
4651 return 0;
4652}
4653
4654/*
4655 * For choosing the software clock, when the display hardware or API
4656 * does not expose a compatible presentation timestamp.
4657 */
4658WL_EXPORT int
4659weston_compositor_set_presentation_clock_software(
4660 struct weston_compositor *compositor)
4661{
4662 /* In order of preference */
4663 static const clockid_t clocks[] = {
4664 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4665 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4666 CLOCK_MONOTONIC, /* no jumps, may crawl */
4667 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4668 CLOCK_REALTIME /* may jump and crawl */
4669 };
4670 unsigned i;
4671
4672 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4673 if (weston_compositor_set_presentation_clock(compositor,
4674 clocks[i]) == 0)
4675 return 0;
4676
4677 weston_log("Error: no suitable presentation clock available.\n");
4678
4679 return -1;
4680}
4681
Pekka Paalanen662f3842015-03-18 12:17:26 +02004682/** Read the current time from the Presentation clock
4683 *
4684 * \param compositor
4685 * \param ts[out] The current time.
4686 *
4687 * \note Reading the current time in user space is always imprecise to some
4688 * degree.
4689 *
4690 * This function is never meant to fail. If reading the clock does fail,
4691 * an error message is logged and a zero time is returned. Callers are not
4692 * supposed to detect or react to failures.
4693 */
4694WL_EXPORT void
4695weston_compositor_read_presentation_clock(
4696 const struct weston_compositor *compositor,
4697 struct timespec *ts)
4698{
4699 static bool warned;
4700 int ret;
4701
4702 ret = clock_gettime(compositor->presentation_clock, ts);
4703 if (ret < 0) {
4704 ts->tv_sec = 0;
4705 ts->tv_nsec = 0;
4706
4707 if (!warned)
4708 weston_log("Error: failure to read "
4709 "the presentation clock %#x: '%m' (%d)\n",
4710 compositor->presentation_clock, errno);
4711 warned = true;
4712 }
4713}
4714
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004715WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004716weston_version(int *major, int *minor, int *micro)
4717{
4718 *major = WESTON_VERSION_MAJOR;
4719 *minor = WESTON_VERSION_MINOR;
4720 *micro = WESTON_VERSION_MICRO;
4721}
4722
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004723static const char *
4724clock_name(clockid_t clk_id)
4725{
4726 static const char *names[] = {
4727 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4728 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4729 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4730 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4731 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4732 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4733 };
4734
4735 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4736 return "unknown";
4737
4738 return names[clk_id];
4739}
4740
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004741static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004742 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004743 const char *desc;
4744} capability_strings[] = {
4745 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004746 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004747};
4748
4749static void
4750weston_compositor_log_capabilities(struct weston_compositor *compositor)
4751{
4752 unsigned i;
4753 int yes;
4754
4755 weston_log("Compositor capabilities:\n");
4756 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4757 yes = compositor->capabilities & capability_strings[i].bit;
4758 weston_log_continue(STAMP_SPACE "%s %s\n",
4759 capability_strings[i].desc,
4760 yes ? "yes" : "no");
4761 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004762
4763 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4764 clock_name(compositor->presentation_clock),
4765 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004766}
4767
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004768static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004769{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004770 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004771
Martin Minarik6d118362012-06-07 18:01:59 +02004772 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004773 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004774
4775 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004776}
4777
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004778#ifdef HAVE_LIBUNWIND
4779
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004780static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004781print_backtrace(void)
4782{
4783 unw_cursor_t cursor;
4784 unw_context_t context;
4785 unw_word_t off;
4786 unw_proc_info_t pip;
4787 int ret, i = 0;
4788 char procname[256];
4789 const char *filename;
4790 Dl_info dlinfo;
4791
4792 pip.unwind_info = NULL;
4793 ret = unw_getcontext(&context);
4794 if (ret) {
4795 weston_log("unw_getcontext: %d\n", ret);
4796 return;
4797 }
4798
4799 ret = unw_init_local(&cursor, &context);
4800 if (ret) {
4801 weston_log("unw_init_local: %d\n", ret);
4802 return;
4803 }
4804
4805 ret = unw_step(&cursor);
4806 while (ret > 0) {
4807 ret = unw_get_proc_info(&cursor, &pip);
4808 if (ret) {
4809 weston_log("unw_get_proc_info: %d\n", ret);
4810 break;
4811 }
4812
4813 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4814 if (ret && ret != -UNW_ENOMEM) {
4815 if (ret != -UNW_EUNSPEC)
4816 weston_log("unw_get_proc_name: %d\n", ret);
4817 procname[0] = '?';
4818 procname[1] = 0;
4819 }
4820
4821 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4822 *dlinfo.dli_fname)
4823 filename = dlinfo.dli_fname;
4824 else
4825 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004826
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004827 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4828 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4829
4830 ret = unw_step(&cursor);
4831 if (ret < 0)
4832 weston_log("unw_step: %d\n", ret);
4833 }
4834}
4835
4836#else
4837
4838static void
4839print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004840{
4841 void *buffer[32];
4842 int i, count;
4843 Dl_info info;
4844
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004845 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4846 for (i = 0; i < count; i++) {
4847 dladdr(buffer[i], &info);
4848 weston_log(" [%016lx] %s (%s)\n",
4849 (long) buffer[i],
4850 info.dli_sname ? info.dli_sname : "--",
4851 info.dli_fname);
4852 }
4853}
4854
4855#endif
4856
4857static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004858on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004859{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004860 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004861 * then call the backend restore function, which will switch
4862 * back to the vt we launched from or ungrab X etc and then
4863 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004864 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004865 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004866 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004867
Peter Maatmane5b42e42013-03-27 22:38:53 +01004868 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004869
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004870 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004871
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004872 segv_compositor->restore(segv_compositor);
4873
4874 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004875}
4876
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004877WL_EXPORT void *
4878weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004879{
4880 char path[PATH_MAX];
4881 void *module, *init;
4882
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004883 if (name == NULL)
4884 return NULL;
4885
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004886 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004887 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004888 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004889 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004890
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004891 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4892 if (module) {
4893 weston_log("Module '%s' already loaded\n", path);
4894 dlclose(module);
4895 return NULL;
4896 }
4897
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004898 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004899 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004900 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004901 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004902 return NULL;
4903 }
4904
4905 init = dlsym(module, entrypoint);
4906 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004907 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004908 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004909 return NULL;
4910 }
4911
4912 return init;
4913}
4914
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004915static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004916load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004917 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004918{
4919 const char *p, *end;
4920 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004921 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004922 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004923
4924 if (modules == NULL)
4925 return 0;
4926
4927 p = modules;
4928 while (*p) {
4929 end = strchrnul(p, ',');
4930 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004931 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004932 if (!module_init)
4933 return -1;
4934 if (module_init(ec, argc, argv) < 0)
4935 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004936 p = end;
4937 while (*p == ',')
4938 p++;
4939
4940 }
4941
4942 return 0;
4943}
4944
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004945static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004946 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4947
4948static const char xdg_wrong_message[] =
4949 "fatal: environment variable XDG_RUNTIME_DIR\n"
4950 "is set to \"%s\", which is not a directory.\n";
4951
4952static const char xdg_wrong_mode_message[] =
4953 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004954 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4955 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004956
4957static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004958 "Refer to your distribution on how to get it, or\n"
4959 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4960 "on how to implement it.\n";
4961
Martin Minarik37032f82012-06-18 20:15:18 +02004962static void
4963verify_xdg_runtime_dir(void)
4964{
4965 char *dir = getenv("XDG_RUNTIME_DIR");
4966 struct stat s;
4967
4968 if (!dir) {
4969 weston_log(xdg_error_message);
4970 weston_log_continue(xdg_detail_message);
4971 exit(EXIT_FAILURE);
4972 }
4973
4974 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4975 weston_log(xdg_wrong_message, dir);
4976 weston_log_continue(xdg_detail_message);
4977 exit(EXIT_FAILURE);
4978 }
4979
4980 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4981 weston_log(xdg_wrong_mode_message,
4982 dir, s.st_mode & 0777, s.st_uid);
4983 weston_log_continue(xdg_detail_message);
4984 }
4985}
4986
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004987static int
4988usage(int error_code)
4989{
4990 fprintf(stderr,
4991 "Usage: weston [OPTIONS]\n\n"
4992 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4993 "Weston supports multiple backends, and depending on which backend is in use\n"
4994 "different options will be accepted.\n\n"
4995
4996
4997 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004998 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004999 " -B, --backend=MODULE\tBackend module, one of\n"
5000#if defined(BUILD_DRM_COMPOSITOR)
5001 "\t\t\t\tdrm-backend.so\n"
5002#endif
5003#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02005004 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07005005#endif
5006#if defined(BUILD_X11_COMPOSITOR)
5007 "\t\t\t\tx11-backend.so\n"
5008#endif
5009#if defined(BUILD_WAYLAND_COMPOSITOR)
5010 "\t\t\t\twayland-backend.so\n"
5011#endif
5012#if defined(BUILD_RDP_COMPOSITOR)
5013 "\t\t\t\trdp-backend.so\n"
5014#endif
5015#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5016 "\t\t\t\trpi-backend.so\n"
5017#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05005018 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005019 " -S, --socket=NAME\tName of socket to listen on\n"
5020 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005021 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005022 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03005023 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005024 " -h, --help\t\tThis help message\n\n");
5025
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005026#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005027 fprintf(stderr,
5028 "Options for drm-backend.so:\n\n"
5029 " --connector=ID\tBring up only this connector\n"
5030 " --seat=SEAT\t\tThe seat that weston should run on\n"
5031 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02005032 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005033 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005034#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005035
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005036#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005037 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005038 "Options for fbdev-backend.so:\n\n"
5039 " --tty=TTY\t\tThe tty to use\n"
5040 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005041#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005042
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005043#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01005044 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005045 "Options for x11-backend.so:\n\n"
5046 " --width=WIDTH\t\tWidth of X window\n"
5047 " --height=HEIGHT\tHeight of X window\n"
5048 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05005049 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005050 " --output-count=COUNT\tCreate multiple outputs\n"
5051 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005052#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005053
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005054#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005055 fprintf(stderr,
5056 "Options for wayland-backend.so:\n\n"
5057 " --width=WIDTH\t\tWidth of Wayland surface\n"
5058 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07005059 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06005060 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05005061 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05005062 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05005063 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005064 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07005065#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005066
Pekka Paalanene31e0532013-05-22 18:03:07 +03005067#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
5068 fprintf(stderr,
5069 "Options for rpi-backend.so:\n\n"
5070 " --tty=TTY\t\tThe tty to use\n"
5071 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
5072 " --transform=TR\tThe output transformation, TR is one of:\n"
5073 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01005074 " --opaque-regions\tEnable support for opaque regions, can be "
5075 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03005076 "\n");
5077#endif
5078
Hardeningc077a842013-07-08 00:51:35 +02005079#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07005080 fprintf(stderr,
5081 "Options for rdp-backend.so:\n\n"
5082 " --width=WIDTH\t\tWidth of desktop\n"
5083 " --height=HEIGHT\tHeight of desktop\n"
5084 " --env-socket=SOCKET\tUse that socket as peer connection\n"
5085 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07005086 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07005087 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
5088 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
5089 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
5090 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
5091 "\n");
Hardeningc077a842013-07-08 00:51:35 +02005092#endif
5093
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08005094#if defined(BUILD_HEADLESS_COMPOSITOR)
5095 fprintf(stderr,
5096 "Options for headless-backend.so:\n\n"
5097 " --width=WIDTH\t\tWidth of memory surface\n"
5098 " --height=HEIGHT\tHeight of memory surface\n"
5099 " --transform=TR\tThe output transformation, TR is one of:\n"
5100 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
5101 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
5102#endif
5103
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005104 exit(error_code);
5105}
5106
Peter Maatmane5b42e42013-03-27 22:38:53 +01005107static void
5108catch_signals(void)
5109{
5110 struct sigaction action;
5111
5112 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
5113 action.sa_sigaction = on_caught_signal;
5114 sigemptyset(&action.sa_mask);
5115 sigaction(SIGSEGV, &action, NULL);
5116 sigaction(SIGABRT, &action, NULL);
5117}
5118
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005119static void
5120handle_primary_client_destroyed(struct wl_listener *listener, void *data)
5121{
5122 struct wl_client *client = data;
5123
5124 weston_log("Primary client died. Closing...\n");
5125
5126 wl_display_terminate(wl_client_get_display(client));
5127}
5128
Ryo Munakatad8deff62014-09-06 07:32:05 +09005129static char *
5130weston_choose_default_backend(void)
5131{
5132 char *backend = NULL;
5133
5134 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
5135 backend = strdup("wayland-backend.so");
5136 else if (getenv("DISPLAY"))
5137 backend = strdup("x11-backend.so");
5138 else
5139 backend = strdup(WESTON_NATIVE_BACKEND);
5140
5141 return backend;
5142}
5143
5144static int
5145weston_create_listening_socket(struct wl_display *display, const char *socket_name)
5146{
5147 if (socket_name) {
5148 if (wl_display_add_socket(display, socket_name)) {
5149 weston_log("fatal: failed to add socket: %m\n");
5150 return -1;
5151 }
5152 } else {
5153 socket_name = wl_display_add_socket_auto(display);
5154 if (!socket_name) {
5155 weston_log("fatal: failed to add socket: %m\n");
5156 return -1;
5157 }
5158 }
5159
5160 setenv("WAYLAND_DISPLAY", socket_name, 1);
5161
5162 return 0;
5163}
5164
Derek Foreman64a3df02014-10-23 12:24:18 -05005165static const struct { const char *name; uint32_t token; } transforms[] = {
5166 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
5167 { "90", WL_OUTPUT_TRANSFORM_90 },
5168 { "180", WL_OUTPUT_TRANSFORM_180 },
5169 { "270", WL_OUTPUT_TRANSFORM_270 },
5170 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
5171 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
5172 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
5173 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
5174};
5175
5176WL_EXPORT int
5177weston_parse_transform(const char *transform, uint32_t *out)
5178{
5179 unsigned int i;
5180
5181 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5182 if (strcmp(transforms[i].name, transform) == 0) {
5183 *out = transforms[i].token;
5184 return 0;
5185 }
5186
5187 *out = WL_OUTPUT_TRANSFORM_NORMAL;
5188 return -1;
5189}
5190
5191WL_EXPORT const char *
5192weston_transform_to_string(uint32_t output_transform)
5193{
5194 unsigned int i;
5195
5196 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
5197 if (transforms[i].token == output_transform)
5198 return transforms[i].name;
5199
5200 return "<illegal value>";
5201}
5202
5203
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005204int main(int argc, char *argv[])
5205{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005206 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005207 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005208 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005209 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05005210 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05005211 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005212 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005213 int *argc, char *argv[],
5214 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005215 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005216 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05005217 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02005218 char *modules = NULL;
5219 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02005220 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005221 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04005222 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005223 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09005224 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06005225 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005226 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005227 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03005228 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005229 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005230 struct wl_client *primary_client;
5231 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005232 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005233
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005234 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09005235 { WESTON_OPTION_STRING, "backend", 'B', &backend },
5236 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005237 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
5238 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04005239 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02005240 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005241 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06005242 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03005243 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04005244 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05005245
Kristian Høgsberg4172f662013-02-20 15:27:49 -05005246 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005247
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04005248 if (help)
5249 usage(EXIT_SUCCESS);
5250
Scott Moreau12245142012-08-29 15:15:58 -06005251 if (version) {
5252 printf(PACKAGE_STRING "\n");
5253 return EXIT_SUCCESS;
5254 }
5255
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02005256 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005257
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03005258 weston_log("%s\n"
5259 STAMP_SPACE "%s\n"
5260 STAMP_SPACE "Bug reports to: %s\n"
5261 STAMP_SPACE "Build: %s\n",
5262 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04005263 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03005264 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04005265
Martin Minarik37032f82012-06-18 20:15:18 +02005266 verify_xdg_runtime_dir();
5267
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005268 display = wl_display_create();
5269
Tiago Vignatti2116b892011-08-08 05:52:59 -07005270 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005271 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
5272 display);
5273 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
5274 display);
5275 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
5276 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07005277
5278 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02005279 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
5280 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05005281
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305282 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
5283 ret = EXIT_FAILURE;
5284 goto out_signals;
5285 }
5286
Pekka Paalanen588bee12014-05-07 16:26:25 +03005287 if (noconfig == 0)
5288 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01005289 if (config != NULL) {
5290 weston_log("Using config file '%s'\n",
5291 weston_config_get_full_path(config));
5292 } else {
5293 weston_log("Starting with no config file.\n");
5294 }
5295 section = weston_config_get_section(config, "core", NULL, NULL);
5296
Ryo Munakata03faed22014-09-06 07:32:51 +09005297 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08005298 weston_config_section_get_string(section, "backend", &backend,
5299 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09005300 if (!backend)
5301 backend = weston_choose_default_backend();
5302 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04005303
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03005304 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305305 if (!backend_init) {
5306 ret = EXIT_FAILURE;
5307 goto out_signals;
5308 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05005309
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04005310 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005311 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03005312 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305313 ret = EXIT_FAILURE;
5314 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05005315 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04005316
Peter Maatmane5b42e42013-03-27 22:38:53 +01005317 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005318 segv_compositor = ec;
5319
Frederic Plourde4a84c832014-10-30 15:06:34 -04005320 if (idle_time < 0)
5321 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
5322 if (idle_time < 0)
5323 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04005324 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01005325 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04005326 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02005327
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05005328 for (i = 1; i < argc; i++)
5329 weston_log("fatal: unhandled option: %s\n", argv[i]);
5330 if (argc > 1) {
5331 ret = EXIT_FAILURE;
5332 goto out;
5333 }
5334
Pekka Paalanen7bb65102013-05-22 18:03:04 +03005335 weston_compositor_log_capabilities(ec);
5336
Jason Ekstrand923bfe62014-04-02 19:53:58 -05005337 server_socket = getenv("WAYLAND_SERVER_SOCKET");
5338 if (server_socket) {
5339 weston_log("Running with single client\n");
5340 fd = strtol(server_socket, &end, 0);
5341 if (*end != '\0')
5342 fd = -1;
5343 } else {
5344 fd = -1;
5345 }
5346
5347 if (fd != -1) {
5348 primary_client = wl_client_create(display, fd);
5349 if (!primary_client) {
5350 weston_log("fatal: failed to add client: %m\n");
5351 ret = EXIT_FAILURE;
5352 goto out;
5353 }
5354 primary_client_destroyed.notify =
5355 handle_primary_client_destroyed;
5356 wl_client_add_destroy_listener(primary_client,
5357 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09005358 } else if (weston_create_listening_socket(display, socket_name)) {
5359 ret = EXIT_FAILURE;
5360 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005361 }
5362
Ryo Munakata03faed22014-09-06 07:32:51 +09005363 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005364 weston_config_section_get_string(section, "shell", &shell,
5365 "desktop-shell.so");
5366
Ryo Munakata03faed22014-09-06 07:32:51 +09005367 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005368 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005369
5370 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09005371 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005372 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04005373
5374 if (load_modules(ec, option_modules, &argc, argv) < 0)
5375 goto out;
5376
Giulio Camuffode7e2b32014-08-28 19:44:10 +03005377 section = weston_config_get_section(config, "keyboard", NULL, NULL);
5378 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
5379 if (numlock_on) {
5380 wl_list_for_each(seat, &ec->seat_list, link) {
5381 if (seat->keyboard)
5382 weston_keyboard_set_locks(seat->keyboard,
5383 WESTON_NUM_LOCK,
5384 WESTON_NUM_LOCK);
5385 }
5386 }
5387
Pekka Paalanenc0444e32012-01-05 16:28:21 +02005388 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05005389
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005390 wl_display_run(display);
5391
Frederic Plourdec336f062014-10-29 14:44:33 -04005392 /* Allow for setting return exit code after
5393 * wl_display_run returns normally. This is
5394 * useful for devs/testers and automated tests
5395 * that want to indicate failure status to
5396 * testing infrastructure above
5397 */
5398 ret = ec->exit_code;
5399
Ryo Munakata03faed22014-09-06 07:32:51 +09005400out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005401 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005402 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005403
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005404 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005405
Daniel Stone855028f2012-05-01 20:37:10 +01005406 weston_compositor_xkb_destroy(ec);
5407
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005408 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305409
5410out_signals:
5411 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5412 if (signals[i])
5413 wl_event_source_remove(signals[i]);
5414
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005415 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005416
Martin Minarik19e6f262012-06-07 13:08:46 +02005417 weston_log_file_close();
5418
Ryo Munakata03faed22014-09-06 07:32:51 +09005419 free(backend);
5420 free(shell);
5421 free(socket_name);
5422 free(option_modules);
5423 free(log);
5424 free(modules);
5425
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005426 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005427}