blob: f1933f0bfe88e12d2f10e487e8f5fb3e937bdc63 [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 Paalanen31f7d782014-09-23 22:08:43 -04004 * Copyright © 2012-2014 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010059#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040060#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030061#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040066static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067
68static int
69sigchld_handler(int signal_number, void *data)
70{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 int status;
73 pid_t pid;
74
Pekka Paalanen23ade622014-08-27 13:31:26 +030075 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
76 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
Bill Spitzak027b9622012-03-17 15:22:03 -070080
Pekka Paalanen23ade622014-08-27 13:31:26 +030081 if (&p->link == &child_process_list) {
82 weston_log("unknown child process exited\n");
83 continue;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040088 }
89
Pekka Paalanen23ade622014-08-27 13:31:26 +030090 if (pid < 0 && errno != ECHILD)
91 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93 return 1;
94}
95
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096static void
Alexander Larsson0b135062013-05-28 16:23:36 +020097weston_output_transform_scale_init(struct weston_output *output,
98 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020099
Rob Bradford27b17932013-06-26 18:08:46 +0100100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500101weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100102
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600103static void weston_mode_switch_finish(struct weston_output *output,
104 int mode_changed,
105 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800106{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200108 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600110 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200116 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Derek Foremandd4cd332014-11-10 10:29:59 -0600153 if (!mode_changed && !scale_changed)
154 return;
155
Hardening57388e42013-09-18 23:56:36 +0200156 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600157 wl_resource_for_each(resource, &output->resource_list) {
158 if (mode_changed) {
159 wl_output_send_mode(resource,
160 output->current_mode->flags,
161 output->current_mode->width,
162 output->current_mode->height,
163 output->current_mode->refresh);
164 }
Hardening57388e42013-09-18 23:56:36 +0200165
Derek Foreman41bdc272014-11-05 13:26:57 -0600166 version = wl_resource_get_version(resource);
167 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200169
Derek Foreman41bdc272014-11-05 13:26:57 -0600170 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
171 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600172 }
173}
174
175WL_EXPORT int
176weston_output_mode_set_native(struct weston_output *output,
177 struct weston_mode *mode,
178 int32_t scale)
179{
180 int ret;
181 int mode_changed = 0, scale_changed = 0;
182
183 if (!output->switch_mode)
184 return -1;
185
186 if (!output->original_mode) {
187 mode_changed = 1;
188 ret = output->switch_mode(output, mode);
189 if (ret < 0)
190 return ret;
191 if (output->current_scale != scale) {
192 scale_changed = 1;
193 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200194 }
195 }
196
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
202 return 0;
203}
204
205WL_EXPORT int
206weston_output_mode_switch_to_native(struct weston_output *output)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
210
211 if (!output->switch_mode)
212 return -1;
213
214 if (!output->original_mode) {
215 weston_log("already in the native mode\n");
216 return -1;
217 }
218 /* the non fullscreen clients haven't seen a mode set since we
219 * switched into a temporary, so we need to notify them if the
220 * mode at that time is different from the native mode now.
221 */
222 mode_changed = (output->original_mode != output->native_mode);
223 scale_changed = (output->original_scale != output->native_scale);
224
225 ret = output->switch_mode(output, output->native_mode);
226 if (ret < 0)
227 return ret;
228
229 output->current_scale = output->native_scale;
230
231 output->original_mode = NULL;
232 output->original_scale = 0;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
236 return 0;
237}
238
239WL_EXPORT int
240weston_output_mode_switch_to_temporary(struct weston_output *output,
241 struct weston_mode *mode,
242 int32_t scale)
243{
244 int ret;
245
246 if (!output->switch_mode)
247 return -1;
248
249 /* original_mode is the last mode non full screen clients have seen,
250 * so we shouldn't change it if we already have one set.
251 */
252 if (!output->original_mode) {
253 output->original_mode = output->native_mode;
254 output->original_scale = output->native_scale;
255 }
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259
260 output->current_scale = scale;
261
262 weston_mode_switch_finish(output, 0, 0);
263
264 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800265}
266
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400269{
270 wl_list_insert(&child_process_list, &process->link);
271}
272
Benjamin Franzke06286262011-05-06 19:12:33 +0200273static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274child_client_exec(int sockfd, const char *path)
275{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500276 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200278 sigset_t allsigs;
279
280 /* do not give our signal mask to the new process */
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100284 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
285 if (seteuid(getuid()) == -1) {
286 weston_log("compositor: failed seteuid\n");
287 return;
288 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500289
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
291 * non-CLOEXEC fd to pass through exec. */
292 clientfd = dup(sockfd);
293 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500295 return;
296 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 setenv("WAYLAND_SOCKET", s, 1);
300
301 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 path);
304}
305
306WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500307weston_client_launch(struct weston_compositor *compositor,
308 struct weston_process *proc,
309 const char *path,
310 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311{
312 int sv[2];
313 pid_t pid;
314 struct wl_client *client;
315
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300316 weston_log("launching '%s'\n", path);
317
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300318 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320 "socketpair failed while launching '%s': %m\n",
321 path);
322 return NULL;
323 }
324
325 pid = fork();
326 if (pid == -1) {
327 close(sv[0]);
328 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 "fork failed while launching '%s': %m\n", path);
331 return NULL;
332 }
333
334 if (pid == 0) {
335 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700336 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337 }
338
339 close(sv[1]);
340
341 client = wl_client_create(compositor->wl_display, sv[0]);
342 if (!client) {
343 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "wl_client_create failed while launching '%s'.\n",
346 path);
347 return NULL;
348 }
349
350 proc->pid = pid;
351 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200353
354 return client;
355}
356
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300357struct process_info {
358 struct weston_process proc;
359 char *path;
360};
361
362static void
363process_handle_sigchld(struct weston_process *process, int status)
364{
365 struct process_info *pinfo =
366 container_of(process, struct process_info, proc);
367
368 /*
369 * There are no guarantees whether this runs before or after
370 * the wl_client destructor.
371 */
372
373 if (WIFEXITED(status)) {
374 weston_log("%s exited with status %d\n", pinfo->path,
375 WEXITSTATUS(status));
376 } else if (WIFSIGNALED(status)) {
377 weston_log("%s died on signal %d\n", pinfo->path,
378 WTERMSIG(status));
379 } else {
380 weston_log("%s disappeared\n", pinfo->path);
381 }
382
383 free(pinfo->path);
384 free(pinfo);
385}
386
387WL_EXPORT struct wl_client *
388weston_client_start(struct weston_compositor *compositor, const char *path)
389{
390 struct process_info *pinfo;
391 struct wl_client *client;
392
393 pinfo = zalloc(sizeof *pinfo);
394 if (!pinfo)
395 return NULL;
396
397 pinfo->path = strdup(path);
398 if (!pinfo->path)
399 goto out_free;
400
401 client = weston_client_launch(compositor, &pinfo->proc, path,
402 process_handle_sigchld);
403 if (!client)
404 goto out_str;
405
406 return client;
407
408out_str:
409 free(pinfo->path);
410
411out_free:
412 free(pinfo);
413
414 return NULL;
415}
416
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200417static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300418region_init_infinite(pixman_region32_t *region)
419{
420 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
421 UINT32_MAX, UINT32_MAX);
422}
423
Pekka Paalanene67858b2013-04-25 13:57:42 +0300424static struct weston_subsurface *
425weston_surface_to_subsurface(struct weston_surface *surface);
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427WL_EXPORT struct weston_view *
428weston_view_create(struct weston_surface *surface)
429{
430 struct weston_view *view;
431
Bryce Harringtonde16d892014-11-20 22:21:57 -0800432 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 if (view == NULL)
434 return NULL;
435
436 view->surface = surface;
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 /* Assign to surface */
439 wl_list_insert(&surface->views, &view->surface_link);
440
441 wl_signal_init(&view->destroy_signal);
442 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300443 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300446 pixman_region32_init(&view->transform.masked_boundingbox);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500447
448 view->alpha = 1.0;
449 pixman_region32_init(&view->transform.opaque);
450
451 wl_list_init(&view->geometry.transformation_list);
452 wl_list_insert(&view->geometry.transformation_list,
453 &view->transform.position.link);
454 weston_matrix_init(&view->transform.position.matrix);
455 wl_list_init(&view->geometry.child_list);
456 pixman_region32_init(&view->transform.boundingbox);
457 view->transform.dirty = 1;
458
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 return view;
460}
461
Jason Ekstrand108865d2014-06-26 10:04:49 -0700462struct weston_frame_callback {
463 struct wl_resource *resource;
464 struct wl_list link;
465};
466
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467struct weston_presentation_feedback {
468 struct wl_resource *resource;
469
470 /* XXX: could use just wl_resource_get_link() instead */
471 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200472
473 /* The per-surface feedback flags */
474 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400475};
476
477static void
478weston_presentation_feedback_discard(
479 struct weston_presentation_feedback *feedback)
480{
481 presentation_feedback_send_discarded(feedback->resource);
482 wl_resource_destroy(feedback->resource);
483}
484
485static void
486weston_presentation_feedback_discard_list(struct wl_list *list)
487{
488 struct weston_presentation_feedback *feedback, *tmp;
489
490 wl_list_for_each_safe(feedback, tmp, list, link)
491 weston_presentation_feedback_discard(feedback);
492}
493
494static void
495weston_presentation_feedback_present(
496 struct weston_presentation_feedback *feedback,
497 struct weston_output *output,
498 uint32_t refresh_nsec,
499 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200500 uint64_t seq,
501 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400502{
503 struct wl_client *client = wl_resource_get_client(feedback->resource);
504 struct wl_resource *o;
505 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400506
507 wl_resource_for_each(o, &output->resource_list) {
508 if (wl_resource_get_client(o) != client)
509 continue;
510
511 presentation_feedback_send_sync_output(feedback->resource, o);
512 }
513
514 secs = ts->tv_sec;
515 presentation_feedback_send_presented(feedback->resource,
516 secs >> 32, secs & 0xffffffff,
517 ts->tv_nsec,
518 refresh_nsec,
519 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200520 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400521 wl_resource_destroy(feedback->resource);
522}
523
524static void
525weston_presentation_feedback_present_list(struct wl_list *list,
526 struct weston_output *output,
527 uint32_t refresh_nsec,
528 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200529 uint64_t seq,
530 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400531{
532 struct weston_presentation_feedback *feedback, *tmp;
533
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200534 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
535 wl_list_empty(list));
536
Pekka Paalanen133e4392014-09-23 22:08:46 -0400537 wl_list_for_each_safe(feedback, tmp, list, link)
538 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200539 refresh_nsec, ts, seq,
540 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400541}
542
Jason Ekstrand7b982072014-05-20 14:33:03 -0500543static void
544surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
545{
546 struct weston_surface_state *state =
547 container_of(listener, struct weston_surface_state,
548 buffer_destroy_listener);
549
550 state->buffer = NULL;
551}
552
553static void
554weston_surface_state_init(struct weston_surface_state *state)
555{
556 state->newly_attached = 0;
557 state->buffer = NULL;
558 state->buffer_destroy_listener.notify =
559 surface_state_handle_buffer_destroy;
560 state->sx = 0;
561 state->sy = 0;
562
563 pixman_region32_init(&state->damage);
564 pixman_region32_init(&state->opaque);
565 region_init_infinite(&state->input);
566
567 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400568 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500569
570 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
571 state->buffer_viewport.buffer.scale = 1;
572 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
573 state->buffer_viewport.surface.width = -1;
574 state->buffer_viewport.changed = 0;
575}
576
577static void
578weston_surface_state_fini(struct weston_surface_state *state)
579{
580 struct weston_frame_callback *cb, *next;
581
582 wl_list_for_each_safe(cb, next,
583 &state->frame_callback_list, link)
584 wl_resource_destroy(cb->resource);
585
Pekka Paalanen133e4392014-09-23 22:08:46 -0400586 weston_presentation_feedback_discard_list(&state->feedback_list);
587
Jason Ekstrand7b982072014-05-20 14:33:03 -0500588 pixman_region32_fini(&state->input);
589 pixman_region32_fini(&state->opaque);
590 pixman_region32_fini(&state->damage);
591
592 if (state->buffer)
593 wl_list_remove(&state->buffer_destroy_listener.link);
594 state->buffer = NULL;
595}
596
597static void
598weston_surface_state_set_buffer(struct weston_surface_state *state,
599 struct weston_buffer *buffer)
600{
601 if (state->buffer == buffer)
602 return;
603
604 if (state->buffer)
605 wl_list_remove(&state->buffer_destroy_listener.link);
606 state->buffer = buffer;
607 if (state->buffer)
608 wl_signal_add(&state->buffer->destroy_signal,
609 &state->buffer_destroy_listener);
610}
611
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500613weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500614{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500615 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400616
Bryce Harringtonde16d892014-11-20 22:21:57 -0800617 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400618 if (surface == NULL)
619 return NULL;
620
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500621 wl_signal_init(&surface->destroy_signal);
622
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500623 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200624 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400625
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200626 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
627 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200628 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
629 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500630
631 weston_surface_state_init(&surface->pending);
632
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400633 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500634 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300635 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400636
Jason Ekstranda7af7042013-10-12 22:38:11 -0500637 wl_list_init(&surface->views);
638
639 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400640 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500641
Pekka Paalanene67858b2013-04-25 13:57:42 +0300642 wl_list_init(&surface->subsurface_list);
643 wl_list_init(&surface->subsurface_list_pending);
644
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400645 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500646}
647
Alex Wu8811bf92012-02-28 18:07:54 +0800648WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500649weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200650 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500651{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100652 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500653}
654
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400655WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500656weston_view_to_global_float(struct weston_view *view,
657 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200658{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200660 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
661
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663
664 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200665 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700666 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200667 v.f[3]);
668 *x = 0;
669 *y = 0;
670 return;
671 }
672
673 *x = v.f[0] / v.f[3];
674 *y = v.f[1] / v.f[3];
675 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500676 *x = sx + view->geometry.x;
677 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200678 }
679}
680
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500681WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200682weston_transformed_coord(int width, int height,
683 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200684 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685 float sx, float sy, float *bx, float *by)
686{
687 switch (transform) {
688 case WL_OUTPUT_TRANSFORM_NORMAL:
689 default:
690 *bx = sx;
691 *by = sy;
692 break;
693 case WL_OUTPUT_TRANSFORM_FLIPPED:
694 *bx = width - sx;
695 *by = sy;
696 break;
697 case WL_OUTPUT_TRANSFORM_90:
698 *bx = height - sy;
699 *by = sx;
700 break;
701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
702 *bx = height - sy;
703 *by = width - sx;
704 break;
705 case WL_OUTPUT_TRANSFORM_180:
706 *bx = width - sx;
707 *by = height - sy;
708 break;
709 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
710 *bx = sx;
711 *by = height - sy;
712 break;
713 case WL_OUTPUT_TRANSFORM_270:
714 *bx = sy;
715 *by = width - sx;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
718 *bx = sy;
719 *by = sx;
720 break;
721 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200722
723 *bx *= scale;
724 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200725}
726
727WL_EXPORT pixman_box32_t
728weston_transformed_rect(int width, int height,
729 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200730 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200731 pixman_box32_t rect)
732{
733 float x1, x2, y1, y2;
734
735 pixman_box32_t ret;
736
Alexander Larsson4ea95522013-05-22 14:41:37 +0200737 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200738 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200739 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200740 rect.x2, rect.y2, &x2, &y2);
741
742 if (x1 <= x2) {
743 ret.x1 = x1;
744 ret.x2 = x2;
745 } else {
746 ret.x1 = x2;
747 ret.x2 = x1;
748 }
749
750 if (y1 <= y2) {
751 ret.y1 = y1;
752 ret.y2 = y2;
753 } else {
754 ret.y1 = y2;
755 ret.y2 = y1;
756 }
757
758 return ret;
759}
760
761WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500762weston_transformed_region(int width, int height,
763 enum wl_output_transform transform,
764 int32_t scale,
765 pixman_region32_t *src, pixman_region32_t *dest)
766{
767 pixman_box32_t *src_rects, *dest_rects;
768 int nrects, i;
769
770 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
771 if (src != dest)
772 pixman_region32_copy(dest, src);
773 return;
774 }
775
776 src_rects = pixman_region32_rectangles(src, &nrects);
777 dest_rects = malloc(nrects * sizeof(*dest_rects));
778 if (!dest_rects)
779 return;
780
781 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
782 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
783 } else {
784 for (i = 0; i < nrects; i++) {
785 switch (transform) {
786 default:
787 case WL_OUTPUT_TRANSFORM_NORMAL:
788 dest_rects[i].x1 = src_rects[i].x1;
789 dest_rects[i].y1 = src_rects[i].y1;
790 dest_rects[i].x2 = src_rects[i].x2;
791 dest_rects[i].y2 = src_rects[i].y2;
792 break;
793 case WL_OUTPUT_TRANSFORM_90:
794 dest_rects[i].x1 = height - src_rects[i].y2;
795 dest_rects[i].y1 = src_rects[i].x1;
796 dest_rects[i].x2 = height - src_rects[i].y1;
797 dest_rects[i].y2 = src_rects[i].x2;
798 break;
799 case WL_OUTPUT_TRANSFORM_180:
800 dest_rects[i].x1 = width - src_rects[i].x2;
801 dest_rects[i].y1 = height - src_rects[i].y2;
802 dest_rects[i].x2 = width - src_rects[i].x1;
803 dest_rects[i].y2 = height - src_rects[i].y1;
804 break;
805 case WL_OUTPUT_TRANSFORM_270:
806 dest_rects[i].x1 = src_rects[i].y1;
807 dest_rects[i].y1 = width - src_rects[i].x2;
808 dest_rects[i].x2 = src_rects[i].y2;
809 dest_rects[i].y2 = width - src_rects[i].x1;
810 break;
811 case WL_OUTPUT_TRANSFORM_FLIPPED:
812 dest_rects[i].x1 = width - src_rects[i].x2;
813 dest_rects[i].y1 = src_rects[i].y1;
814 dest_rects[i].x2 = width - src_rects[i].x1;
815 dest_rects[i].y2 = src_rects[i].y2;
816 break;
817 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
818 dest_rects[i].x1 = height - src_rects[i].y2;
819 dest_rects[i].y1 = width - src_rects[i].x2;
820 dest_rects[i].x2 = height - src_rects[i].y1;
821 dest_rects[i].y2 = width - src_rects[i].x1;
822 break;
823 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
824 dest_rects[i].x1 = src_rects[i].x1;
825 dest_rects[i].y1 = height - src_rects[i].y2;
826 dest_rects[i].x2 = src_rects[i].x2;
827 dest_rects[i].y2 = height - src_rects[i].y1;
828 break;
829 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
830 dest_rects[i].x1 = src_rects[i].y1;
831 dest_rects[i].y1 = src_rects[i].x1;
832 dest_rects[i].x2 = src_rects[i].y2;
833 dest_rects[i].y2 = src_rects[i].x2;
834 break;
835 }
836 }
837 }
838
839 if (scale != 1) {
840 for (i = 0; i < nrects; i++) {
841 dest_rects[i].x1 *= scale;
842 dest_rects[i].x2 *= scale;
843 dest_rects[i].y1 *= scale;
844 dest_rects[i].y2 *= scale;
845 }
846 }
847
848 pixman_region32_clear(dest);
849 pixman_region32_init_rects(dest, dest_rects, nrects);
850 free(dest_rects);
851}
852
Jonny Lamb74130762013-11-26 18:19:46 +0100853static void
854scaler_surface_to_buffer(struct weston_surface *surface,
855 float sx, float sy, float *bx, float *by)
856{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200857 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200858 double src_width, src_height;
859 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
862 if (vp->surface.width == -1) {
863 *bx = sx;
864 *by = sy;
865 return;
866 }
Jonny Lamb74130762013-11-26 18:19:46 +0100867
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200868 src_x = 0.0;
869 src_y = 0.0;
870 src_width = surface->width_from_buffer;
871 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100872 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200873 src_x = wl_fixed_to_double(vp->buffer.src_x);
874 src_y = wl_fixed_to_double(vp->buffer.src_y);
875 src_width = wl_fixed_to_double(vp->buffer.src_width);
876 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100877 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200878
879 *bx = sx * src_width / surface->width + src_x;
880 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100881}
882
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500883WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200884weston_surface_to_buffer_float(struct weston_surface *surface,
885 float sx, float sy, float *bx, float *by)
886{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200887 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
888
Jonny Lamb74130762013-11-26 18:19:46 +0100889 /* first transform coordinates if the scaler is set */
890 scaler_surface_to_buffer(surface, sx, sy, bx, by);
891
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500892 weston_transformed_coord(surface->width_from_buffer,
893 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200894 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100895 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200896}
897
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898WL_EXPORT void
899weston_surface_to_buffer(struct weston_surface *surface,
900 int sx, int sy, int *bx, int *by)
901{
902 float bxf, byf;
903
Jonny Lamb74130762013-11-26 18:19:46 +0100904 weston_surface_to_buffer_float(surface,
905 sx, sy, &bxf, &byf);
906
Alexander Larsson4ea95522013-05-22 14:41:37 +0200907 *bx = floorf(bxf);
908 *by = floorf(byf);
909}
910
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200911WL_EXPORT pixman_box32_t
912weston_surface_to_buffer_rect(struct weston_surface *surface,
913 pixman_box32_t rect)
914{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200915 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100916 float xf, yf;
917
918 /* first transform box coordinates if the scaler is set */
919 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
920 rect.x1 = floorf(xf);
921 rect.y1 = floorf(yf);
922
923 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
924 rect.x2 = floorf(xf);
925 rect.y2 = floorf(yf);
926
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500927 return weston_transformed_rect(surface->width_from_buffer,
928 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200929 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200931}
932
933WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400935 struct weston_plane *plane)
936{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938 return;
939
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 weston_view_damage_below(view);
941 view->plane = plane;
942 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400943}
944
Pekka Paalanen51723d52015-02-17 13:10:01 +0200945/** Inflict damage on the plane where the view is visible.
946 *
947 * \param view The view that causes the damage.
948 *
949 * If the view is currently on a plane (including the primary plane),
950 * take the view's boundingbox, subtract all the opaque views that cover it,
951 * and add the remaining region as damage to the plane. This corresponds
952 * to the damage inflicted to the plane if this view disappeared.
953 *
954 * A repaint is scheduled for this view.
955 *
956 * The region of all opaque views covering this view is stored in
957 * weston_view::clip and updated by view_accumulate_damage() during
958 * weston_output_repaint(). Specifically, that region matches the
959 * scenegraph as it was last painted.
960 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400961WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200963{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500964 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200965
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500966 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300967 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800969 if (view->plane)
970 pixman_region32_union(&view->plane->damage,
971 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500972 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800973 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200974}
975
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400976static void
977weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
978{
979 uint32_t different = es->output_mask ^ mask;
980 uint32_t entered = mask & different;
981 uint32_t left = es->output_mask & different;
982 struct weston_output *output;
983 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500984 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400985
986 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500987 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400988 return;
989 if (different == 0)
990 return;
991
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500992 client = wl_resource_get_client(es->resource);
993
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400994 wl_list_for_each(output, &es->compositor->output_list, link) {
995 if (1 << output->id & different)
996 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500997 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400998 client);
999 if (resource == NULL)
1000 continue;
1001 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001002 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001003 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001004 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001005 }
1006}
1007
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001008
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001009static void
1010weston_surface_assign_output(struct weston_surface *es)
1011{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 struct weston_output *new_output;
1013 struct weston_view *view;
1014 pixman_region32_t region;
1015 uint32_t max, area, mask;
1016 pixman_box32_t *e;
1017
1018 new_output = NULL;
1019 max = 0;
1020 mask = 0;
1021 pixman_region32_init(&region);
1022 wl_list_for_each(view, &es->views, surface_link) {
1023 if (!view->output)
1024 continue;
1025
1026 pixman_region32_intersect(&region, &view->transform.boundingbox,
1027 &view->output->region);
1028
1029 e = pixman_region32_extents(&region);
1030 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1031
1032 mask |= view->output_mask;
1033
1034 if (area >= max) {
1035 new_output = view->output;
1036 max = area;
1037 }
1038 }
1039 pixman_region32_fini(&region);
1040
1041 es->output = new_output;
1042 weston_surface_update_output_mask(es, mask);
1043}
1044
1045static void
1046weston_view_assign_output(struct weston_view *ev)
1047{
1048 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001049 struct weston_output *output, *new_output;
1050 pixman_region32_t region;
1051 uint32_t max, area, mask;
1052 pixman_box32_t *e;
1053
1054 new_output = NULL;
1055 max = 0;
1056 mask = 0;
1057 pixman_region32_init(&region);
1058 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001060 &output->region);
1061
1062 e = pixman_region32_extents(&region);
1063 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1064
1065 if (area > 0)
1066 mask |= 1 << output->id;
1067
1068 if (area >= max) {
1069 new_output = output;
1070 max = area;
1071 }
1072 }
1073 pixman_region32_fini(&region);
1074
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 ev->output = new_output;
1076 ev->output_mask = mask;
1077
1078 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001079}
1080
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001081static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1083 int32_t width, int32_t height,
1084 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001085{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001086 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1087 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001088 int32_t s[4][2] = {
1089 { sx, sy },
1090 { sx, sy + height },
1091 { sx + width, sy },
1092 { sx + width, sy + height }
1093 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001094 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001095 int i;
1096
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001097 if (width == 0 || height == 0) {
1098 /* avoid rounding empty bbox to 1x1 */
1099 pixman_region32_init(bbox);
1100 return;
1101 }
1102
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001103 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001104 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001106 if (x < min_x)
1107 min_x = x;
1108 if (x > max_x)
1109 max_x = x;
1110 if (y < min_y)
1111 min_y = y;
1112 if (y > max_y)
1113 max_y = y;
1114 }
1115
Pekka Paalanen219b9822012-02-08 15:38:37 +02001116 int_x = floorf(min_x);
1117 int_y = floorf(min_y);
1118 pixman_region32_init_rect(bbox, int_x, int_y,
1119 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001120}
1121
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001122static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001124{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001126
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001127 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 view->geometry.x = roundf(view->geometry.x);
1129 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001130
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001131 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1133 view->transform.position.matrix.d[12] = view->geometry.x;
1134 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001135
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 view->transform.inverse = view->transform.position.matrix;
1139 view->transform.inverse.d[12] = -view->geometry.x;
1140 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001141
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 pixman_region32_init_rect(&view->transform.boundingbox,
1143 view->geometry.x,
1144 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001145 view->surface->width,
1146 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001147
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 if (view->alpha == 1.0) {
1149 pixman_region32_copy(&view->transform.opaque,
1150 &view->surface->opaque);
1151 pixman_region32_translate(&view->transform.opaque,
1152 view->geometry.x,
1153 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001154 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001155}
1156
1157static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001159{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 struct weston_view *parent = view->geometry.parent;
1161 struct weston_matrix *matrix = &view->transform.matrix;
1162 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001163 struct weston_transform *tform;
1164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001166
1167 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1169 view->transform.position.matrix.d[12] = view->geometry.x;
1170 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001171
1172 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001174 weston_matrix_multiply(matrix, &tform->matrix);
1175
Pekka Paalanen483243f2013-03-08 14:56:50 +02001176 if (parent)
1177 weston_matrix_multiply(matrix, &parent->transform.matrix);
1178
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001179 if (weston_matrix_invert(inverse, matrix) < 0) {
1180 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 weston_log("error: weston_view %p"
1182 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001183 return -1;
1184 }
1185
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001186 view_compute_bbox(view, 0, 0,
1187 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001189
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190 return 0;
1191}
1192
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001193static struct weston_layer *
1194get_view_layer(struct weston_view *view)
1195{
1196 if (view->parent_view)
1197 return get_view_layer(view->parent_view);
1198 return view->layer_link.layer;
1199}
1200
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001201WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001203{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001205 struct weston_layer *layer;
1206 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001207
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001209 return;
1210
Pekka Paalanen483243f2013-03-08 14:56:50 +02001211 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001213
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001215
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 pixman_region32_fini(&view->transform.boundingbox);
1219 pixman_region32_fini(&view->transform.opaque);
1220 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001221
Pekka Paalanencd403622012-01-25 13:37:39 +02001222 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 if (view->geometry.transformation_list.next ==
1224 &view->transform.position.link &&
1225 view->geometry.transformation_list.prev ==
1226 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001229 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 if (weston_view_update_transform_enable(view) < 0)
1231 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001232 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001233
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001234 layer = get_view_layer(view);
1235 if (layer) {
1236 pixman_region32_init_with_extents(&mask, &layer->mask);
1237 pixman_region32_intersect(&view->transform.masked_boundingbox,
1238 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001239 pixman_region32_intersect(&view->transform.opaque,
1240 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001241 pixman_region32_fini(&mask);
1242 }
1243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001247
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 wl_signal_emit(&view->surface->compositor->transform_signal,
1249 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001250}
1251
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001252WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001254{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001256
1257 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 * The invariant: if view->geometry.dirty, then all views
1259 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001260 * Corollary: if not parent->geometry.dirty, then all ancestors
1261 * are not dirty.
1262 */
1263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001265 return;
1266
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001268
Jason Ekstranda7af7042013-10-12 22:38:11 -05001269 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001270 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001272}
1273
1274WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275weston_view_to_global_fixed(struct weston_view *view,
1276 wl_fixed_t vx, wl_fixed_t vy,
1277 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001278{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001279 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 weston_view_to_global_float(view,
1282 wl_fixed_to_double(vx),
1283 wl_fixed_to_double(vy),
1284 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001285 *x = wl_fixed_from_double(xf);
1286 *y = wl_fixed_from_double(yf);
1287}
1288
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001289WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290weston_view_from_global_float(struct weston_view *view,
1291 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001292{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001294 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1295
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001297
1298 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001299 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001301 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 *vx = 0;
1303 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001304 return;
1305 }
1306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 *vx = v.f[0] / v.f[3];
1308 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001309 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 *vx = x - view->geometry.x;
1311 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001312 }
1313}
1314
1315WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316weston_view_from_global_fixed(struct weston_view *view,
1317 wl_fixed_t x, wl_fixed_t y,
1318 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001319{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_from_global_float(view,
1323 wl_fixed_to_double(x),
1324 wl_fixed_to_double(y),
1325 &vxf, &vyf);
1326 *vx = wl_fixed_from_double(vxf);
1327 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001328}
1329
1330WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331weston_view_from_global(struct weston_view *view,
1332 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001333{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001335
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1337 *vx = floorf(vxf);
1338 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001339}
1340
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001341WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001342weston_surface_schedule_repaint(struct weston_surface *surface)
1343{
1344 struct weston_output *output;
1345
1346 wl_list_for_each(output, &surface->compositor->output_list, link)
1347 if (surface->output_mask & (1 << output->id))
1348 weston_output_schedule_repaint(output);
1349}
1350
1351WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352weston_view_schedule_repaint(struct weston_view *view)
1353{
1354 struct weston_output *output;
1355
1356 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1357 if (view->output_mask & (1 << output->id))
1358 weston_output_schedule_repaint(output);
1359}
1360
1361WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001362weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001363{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001364 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 0, 0, surface->width,
1366 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001367
Kristian Høgsberg98238702012-08-03 16:29:12 -04001368 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001369}
1370
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001371WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001373{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001374 if (view->geometry.x == x && view->geometry.y == y)
1375 return;
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 view->geometry.x = x;
1378 view->geometry.y = y;
1379 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001380}
1381
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382static void
1383transform_parent_handle_parent_destroy(struct wl_listener *listener,
1384 void *data)
1385{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 struct weston_view *view =
1387 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001388 geometry.parent_destroy_listener);
1389
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001391}
1392
1393WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394weston_view_set_transform_parent(struct weston_view *view,
1395 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001396{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 if (view->geometry.parent) {
1398 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1399 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001400 }
1401
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001403
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001405 transform_parent_handle_parent_destroy;
1406 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001407 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001409 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001411 }
1412
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 weston_view_geometry_dirty(view);
1414}
1415
Derek Foreman280e7dd2014-10-03 13:13:42 -05001416WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001417weston_view_is_mapped(struct weston_view *view)
1418{
1419 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001420 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001421 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001422 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001423}
1424
Derek Foreman280e7dd2014-10-03 13:13:42 -05001425WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001426weston_surface_is_mapped(struct weston_surface *surface)
1427{
1428 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001429 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001430 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001431 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001432}
1433
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001434static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001435surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001436{
1437 struct weston_view *view;
1438
1439 if (surface->width == width && surface->height == height)
1440 return;
1441
1442 surface->width = width;
1443 surface->height = height;
1444
1445 wl_list_for_each(view, &surface->views, surface_link)
1446 weston_view_geometry_dirty(view);
1447}
1448
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001449WL_EXPORT void
1450weston_surface_set_size(struct weston_surface *surface,
1451 int32_t width, int32_t height)
1452{
1453 assert(!surface->resource);
1454 surface_set_size(surface, width, height);
1455}
1456
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001457static int
1458fixed_round_up_to_int(wl_fixed_t f)
1459{
1460 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1461}
1462
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001463static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001464weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001465{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001466 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001467 int32_t width, height;
1468
1469 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001470 surface->width_from_buffer = 0;
1471 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001472 return;
1473 }
1474
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001475 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001476 case WL_OUTPUT_TRANSFORM_90:
1477 case WL_OUTPUT_TRANSFORM_270:
1478 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1479 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001480 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1481 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001482 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001483 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001484 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1485 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001486 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001487 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001488
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001489 surface->width_from_buffer = width;
1490 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001491}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001492
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001493static void
1494weston_surface_update_size(struct weston_surface *surface)
1495{
1496 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1497 int32_t width, height;
1498
1499 width = surface->width_from_buffer;
1500 height = surface->height_from_buffer;
1501
1502 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001503 surface_set_size(surface,
1504 vp->surface.width, vp->surface.height);
1505 return;
1506 }
1507
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001508 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001509 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1510 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1511
1512 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001513 return;
1514 }
1515
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001516 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001517}
1518
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001519WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001520weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001521{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001522 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001523
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001524 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001525
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001526 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001527}
1528
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529WL_EXPORT struct weston_view *
1530weston_compositor_pick_view(struct weston_compositor *compositor,
1531 wl_fixed_t x, wl_fixed_t y,
1532 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001533{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001535 int ix = wl_fixed_to_int(x);
1536 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 wl_list_for_each(view, &compositor->view_list, link) {
1539 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001540 if (pixman_region32_contains_point(
1541 &view->transform.masked_boundingbox,
1542 ix, iy, NULL) &&
1543 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 wl_fixed_to_int(*vx),
1545 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001546 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001548 }
1549
1550 return NULL;
1551}
1552
1553static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001554weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001555{
Daniel Stone37816df2012-05-16 18:45:18 +01001556 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001557
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001558 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001559 return;
1560
Daniel Stone37816df2012-05-16 18:45:18 +01001561 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001562 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001563}
1564
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001565WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001567{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001568 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001569
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 if (!weston_view_is_mapped(view))
1571 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001572
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 weston_view_damage_below(view);
1574 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001575 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001576 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 wl_list_remove(&view->link);
1578 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 view->output_mask = 0;
1580 weston_surface_assign_output(view->surface);
1581
1582 if (weston_surface_is_mapped(view->surface))
1583 return;
1584
1585 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1586 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001587 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001589 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001590 NULL,
1591 wl_fixed_from_int(0),
1592 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001594 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001595 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001597
Jason Ekstranda7af7042013-10-12 22:38:11 -05001598WL_EXPORT void
1599weston_surface_unmap(struct weston_surface *surface)
1600{
1601 struct weston_view *view;
1602
1603 wl_list_for_each(view, &surface->views, surface_link)
1604 weston_view_unmap(view);
1605 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001606}
1607
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001608static void
1609weston_surface_reset_pending_buffer(struct weston_surface *surface)
1610{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001611 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001612 surface->pending.sx = 0;
1613 surface->pending.sy = 0;
1614 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001615 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001616}
1617
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618WL_EXPORT void
1619weston_view_destroy(struct weston_view *view)
1620{
1621 wl_signal_emit(&view->destroy_signal, view);
1622
1623 assert(wl_list_empty(&view->geometry.child_list));
1624
1625 if (weston_view_is_mapped(view)) {
1626 weston_view_unmap(view);
1627 weston_compositor_build_view_list(view->surface->compositor);
1628 }
1629
1630 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001631 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632
1633 pixman_region32_fini(&view->clip);
1634 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001635 pixman_region32_fini(&view->transform.masked_boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001636 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637
1638 weston_view_set_transform_parent(view, NULL);
1639
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640 wl_list_remove(&view->surface_link);
1641
1642 free(view);
1643}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001644
1645WL_EXPORT void
1646weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001647{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001648 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001650
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001651 if (--surface->ref_count > 0)
1652 return;
1653
1654 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1655
Pekka Paalanene67858b2013-04-25 13:57:42 +03001656 assert(wl_list_empty(&surface->subsurface_list_pending));
1657 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001658
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1660 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001661
Jason Ekstrand7b982072014-05-20 14:33:03 -05001662 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001663
Pekka Paalanende685b82012-12-04 15:58:12 +02001664 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001665
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001666 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001667 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001668 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001669
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001670 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001671 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001672
Pekka Paalanen133e4392014-09-23 22:08:46 -04001673 weston_presentation_feedback_discard_list(&surface->feedback_list);
1674
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001675 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001676}
1677
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001678static void
1679destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001680{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001681 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001682
Giulio Camuffo0d379742013-11-15 22:06:15 +01001683 /* Set the resource to NULL, since we don't want to leave a
1684 * dangling pointer if the surface was refcounted and survives
1685 * the weston_surface_destroy() call. */
1686 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001687 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001688}
1689
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001690static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001691weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1692{
1693 struct weston_buffer *buffer =
1694 container_of(listener, struct weston_buffer, destroy_listener);
1695
1696 wl_signal_emit(&buffer->destroy_signal, buffer);
1697 free(buffer);
1698}
1699
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001700WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001701weston_buffer_from_resource(struct wl_resource *resource)
1702{
1703 struct weston_buffer *buffer;
1704 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001705
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001706 listener = wl_resource_get_destroy_listener(resource,
1707 weston_buffer_destroy_handler);
1708
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001709 if (listener)
1710 return container_of(listener, struct weston_buffer,
1711 destroy_listener);
1712
1713 buffer = zalloc(sizeof *buffer);
1714 if (buffer == NULL)
1715 return NULL;
1716
1717 buffer->resource = resource;
1718 wl_signal_init(&buffer->destroy_signal);
1719 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001720 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001721 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001722
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001723 return buffer;
1724}
1725
1726static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001727weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1728 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001729{
Pekka Paalanende685b82012-12-04 15:58:12 +02001730 struct weston_buffer_reference *ref =
1731 container_of(listener, struct weston_buffer_reference,
1732 destroy_listener);
1733
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001734 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001735 ref->buffer = NULL;
1736}
1737
1738WL_EXPORT void
1739weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001740 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001741{
1742 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001743 ref->buffer->busy_count--;
1744 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001745 assert(wl_resource_get_client(ref->buffer->resource));
1746 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001747 WL_BUFFER_RELEASE);
1748 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001749 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001750 }
1751
Pekka Paalanende685b82012-12-04 15:58:12 +02001752 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001753 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001754 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001755 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001756 }
1757
Pekka Paalanende685b82012-12-04 15:58:12 +02001758 ref->buffer = buffer;
1759 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1760}
1761
1762static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001763weston_surface_attach(struct weston_surface *surface,
1764 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001765{
1766 weston_buffer_reference(&surface->buffer_ref, buffer);
1767
Pekka Paalanena6421c42012-12-04 15:58:10 +02001768 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001769 if (weston_surface_is_mapped(surface))
1770 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001771 }
1772
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001773 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001774
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001775 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001776 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001777}
1778
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001779WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001780weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001781{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001783
1784 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001785 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001786}
1787
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001788WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001789weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001790{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001791 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001792
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001793 pixman_region32_union(&compositor->primary_plane.damage,
1794 &compositor->primary_plane.damage,
1795 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001796 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001797}
1798
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001799static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001801{
Pekka Paalanende685b82012-12-04 15:58:12 +02001802 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001803 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001804 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001805
Pekka Paalanenb5026542014-11-12 15:09:24 +02001806 if (weston_timeline_enabled_ &&
1807 pixman_region32_not_empty(&surface->damage))
1808 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1809 TLP_OUTPUT(surface->output), TLP_END);
1810
Jason Ekstrandef540082014-06-26 10:37:36 -07001811 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812}
1813
1814static void
1815view_accumulate_damage(struct weston_view *view,
1816 pixman_region32_t *opaque)
1817{
1818 pixman_region32_t damage;
1819
1820 pixman_region32_init(&damage);
1821 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001822 pixman_box32_t *extents;
1823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 extents = pixman_region32_extents(&view->surface->damage);
1825 view_compute_bbox(view, extents->x1, extents->y1,
1826 extents->x2 - extents->x1,
1827 extents->y2 - extents->y1,
1828 &damage);
1829 pixman_region32_translate(&damage,
1830 -view->plane->x,
1831 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001832 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 pixman_region32_copy(&damage, &view->surface->damage);
1834 pixman_region32_translate(&damage,
1835 view->geometry.x - view->plane->x,
1836 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001837 }
1838
Jason Ekstranda7af7042013-10-12 22:38:11 -05001839 pixman_region32_subtract(&damage, &damage, opaque);
1840 pixman_region32_union(&view->plane->damage,
1841 &view->plane->damage, &damage);
1842 pixman_region32_fini(&damage);
1843 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001844 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001845}
1846
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001847static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001848compositor_accumulate_damage(struct weston_compositor *ec)
1849{
1850 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001852 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001853
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001854 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001855
1856 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001857 pixman_region32_copy(&plane->clip, &clip);
1858
1859 pixman_region32_init(&opaque);
1860
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861 wl_list_for_each(ev, &ec->view_list, link) {
1862 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001863 continue;
1864
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001866 }
1867
1868 pixman_region32_union(&clip, &clip, &opaque);
1869 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001870 }
1871
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001872 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001873
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874 wl_list_for_each(ev, &ec->view_list, link)
1875 ev->surface->touched = 0;
1876
1877 wl_list_for_each(ev, &ec->view_list, link) {
1878 if (ev->surface->touched)
1879 continue;
1880 ev->surface->touched = 1;
1881
1882 surface_flush_damage(ev->surface);
1883
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001884 /* Both the renderer and the backend have seen the buffer
1885 * by now. If renderer needs the buffer, it has its own
1886 * reference set. If the backend wants to keep the buffer
1887 * around for migrating the surface into a non-primary plane
1888 * later, keep_buffer is true. Otherwise, drop the core
1889 * reference now, and allow early buffer release. This enables
1890 * clients to use single-buffering.
1891 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892 if (!ev->surface->keep_buffer)
1893 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001894 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001895}
1896
1897static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001899{
1900 struct weston_subsurface *sub;
1901
Pekka Paalanene67858b2013-04-25 13:57:42 +03001902 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001903 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001904 continue;
1905
Jason Ekstranda7af7042013-10-12 22:38:11 -05001906 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1907 wl_list_init(&sub->surface->views);
1908
1909 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001910 }
1911}
1912
1913static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001915{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 struct weston_subsurface *sub;
1917 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001918
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1920 if (sub->surface == surface)
1921 continue;
1922
George Kiagiadakised04d382014-06-13 18:10:26 +02001923 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1924 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001926 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001927
1928 surface_free_unused_subsurface_views(sub->surface);
1929 }
1930}
1931
1932static void
1933view_list_add_subsurface_view(struct weston_compositor *compositor,
1934 struct weston_subsurface *sub,
1935 struct weston_view *parent)
1936{
1937 struct weston_subsurface *child;
1938 struct weston_view *view = NULL, *iv;
1939
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001940 if (!weston_surface_is_mapped(sub->surface))
1941 return;
1942
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1944 if (iv->geometry.parent == parent) {
1945 view = iv;
1946 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001947 }
1948 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949
1950 if (view) {
1951 /* Put it back in the surface's list of views */
1952 wl_list_remove(&view->surface_link);
1953 wl_list_insert(&sub->surface->views, &view->surface_link);
1954 } else {
1955 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001956 weston_view_set_position(view,
1957 sub->position.x,
1958 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 weston_view_set_transform_parent(view, parent);
1960 }
1961
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001962 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001963 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964
Pekka Paalanenb188e912013-11-19 14:03:35 +02001965 if (wl_list_empty(&sub->surface->subsurface_list)) {
1966 wl_list_insert(compositor->view_list.prev, &view->link);
1967 return;
1968 }
1969
1970 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1971 if (child->surface == sub->surface)
1972 wl_list_insert(compositor->view_list.prev, &view->link);
1973 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001975 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976}
1977
1978static void
1979view_list_add(struct weston_compositor *compositor,
1980 struct weston_view *view)
1981{
1982 struct weston_subsurface *sub;
1983
1984 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985
Pekka Paalanenb188e912013-11-19 14:03:35 +02001986 if (wl_list_empty(&view->surface->subsurface_list)) {
1987 wl_list_insert(compositor->view_list.prev, &view->link);
1988 return;
1989 }
1990
1991 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1992 if (sub->surface == view->surface)
1993 wl_list_insert(compositor->view_list.prev, &view->link);
1994 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001996 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997}
1998
1999static void
2000weston_compositor_build_view_list(struct weston_compositor *compositor)
2001{
2002 struct weston_view *view;
2003 struct weston_layer *layer;
2004
2005 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002006 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 surface_stash_subsurface_views(view->surface);
2008
2009 wl_list_init(&compositor->view_list);
2010 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002011 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002012 view_list_add(compositor, view);
2013 }
2014 }
2015
2016 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002017 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002019}
2020
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002021static void
2022weston_output_take_feedback_list(struct weston_output *output,
2023 struct weston_surface *surface)
2024{
2025 struct weston_view *view;
2026 struct weston_presentation_feedback *feedback;
2027 uint32_t flags = 0xffffffff;
2028
2029 if (wl_list_empty(&surface->feedback_list))
2030 return;
2031
2032 /* All views must have the flag for the flag to survive. */
2033 wl_list_for_each(view, &surface->views, surface_link) {
2034 /* ignore views that are not on this output at all */
2035 if (view->output_mask & (1u << output->id))
2036 flags &= view->psf_flags;
2037 }
2038
2039 wl_list_for_each(feedback, &surface->feedback_list, link)
2040 feedback->psf_flags = flags;
2041
2042 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2043 wl_list_init(&surface->feedback_list);
2044}
2045
David Herrmann1edf44c2013-10-22 17:11:26 +02002046static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002047weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002048{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002049 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002051 struct weston_animation *animation, *next;
2052 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002053 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002054 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002055 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002056
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002057 if (output->destroying)
2058 return 0;
2059
Pekka Paalanenb5026542014-11-12 15:09:24 +02002060 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2061
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002062 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002063 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002064
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002065 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002066 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002067 } else {
2068 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002070 ev->psf_flags = 0;
2071 }
2072 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002073
Pekka Paalanene67858b2013-04-25 13:57:42 +03002074 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002075 wl_list_for_each(ev, &ec->view_list, link) {
2076 /* Note: This operation is safe to do multiple times on the
2077 * same surface.
2078 */
2079 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002080 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002081 &ev->surface->frame_callback_list);
2082 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002083
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002084 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085 }
2086 }
2087
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002088 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002089
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002090 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002091 pixman_region32_intersect(&output_damage,
2092 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002093 pixman_region32_subtract(&output_damage,
2094 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002095
Scott Moreauccbf29d2012-02-22 14:21:41 -07002096 if (output->dirty)
2097 weston_output_update_matrix(output);
2098
David Herrmann1edf44c2013-10-22 17:11:26 +02002099 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002100
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002101 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002102
Kristian Høgsbergef044142011-06-21 15:02:12 -04002103 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002104
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002105 weston_compositor_repick(ec);
2106 wl_event_loop_dispatch(ec->input_loop, 0);
2107
Jonas Ådahldb773762012-06-13 00:01:21 +02002108 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002109 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002110 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002111 }
2112
Scott Moreaud64cf212012-06-08 19:40:54 -06002113 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002114 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002115 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002116 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002117
Pekka Paalanenb5026542014-11-12 15:09:24 +02002118 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2119
David Herrmann1edf44c2013-10-22 17:11:26 +02002120 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002121}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002122
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002123static int
2124weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002125{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002126 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002127
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002128 wl_event_loop_dispatch(compositor->input_loop, 0);
2129
2130 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002131}
2132
Pekka Paalanen82919792014-05-21 13:51:49 +03002133static void
2134weston_output_schedule_repaint_reset(struct weston_output *output)
2135{
2136 struct weston_compositor *compositor = output->compositor;
2137 struct wl_event_loop *loop;
2138 int fd;
2139
2140 output->repaint_scheduled = 0;
2141 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2142
2143 if (compositor->input_loop_source)
2144 return;
2145
2146 loop = wl_display_get_event_loop(compositor->wl_display);
2147 fd = wl_event_loop_get_fd(compositor->input_loop);
2148 compositor->input_loop_source =
2149 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2150 weston_compositor_read_input, compositor);
2151}
2152
Kristian Høgsbergef044142011-06-21 15:02:12 -04002153WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002154weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002155 const struct timespec *stamp,
2156 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002157{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002158 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen82919792014-05-21 13:51:49 +03002159 int r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002160 uint32_t refresh_nsec;
2161
Pekka Paalanenb5026542014-11-12 15:09:24 +02002162 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2163 TLP_VBLANK(stamp), TLP_END);
2164
Pekka Paalanen133e4392014-09-23 22:08:46 -04002165 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2166 weston_presentation_feedback_present_list(&output->feedback_list,
2167 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002168 output->msc,
2169 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002170
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002171 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002172
2173 if (output->repaint_needed &&
2174 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2175 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002176 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002177 if (!r)
2178 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002179 }
2180
Pekka Paalanen82919792014-05-21 13:51:49 +03002181 weston_output_schedule_repaint_reset(output);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002182}
2183
2184static void
2185idle_repaint(void *data)
2186{
2187 struct weston_output *output = data;
2188
Jonas Ådahle5a12252013-04-05 23:07:11 +02002189 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002190}
2191
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002192WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002193weston_layer_entry_insert(struct weston_layer_entry *list,
2194 struct weston_layer_entry *entry)
2195{
2196 wl_list_insert(&list->link, &entry->link);
2197 entry->layer = list->layer;
2198}
2199
2200WL_EXPORT void
2201weston_layer_entry_remove(struct weston_layer_entry *entry)
2202{
2203 wl_list_remove(&entry->link);
2204 wl_list_init(&entry->link);
2205 entry->layer = NULL;
2206}
2207
2208WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002209weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2210{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002211 wl_list_init(&layer->view_list.link);
2212 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002213 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002214 if (below != NULL)
2215 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002216}
2217
2218WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002219weston_layer_set_mask(struct weston_layer *layer,
2220 int x, int y, int width, int height)
2221{
2222 struct weston_view *view;
2223
2224 layer->mask.x1 = x;
2225 layer->mask.x2 = x + width;
2226 layer->mask.y1 = y;
2227 layer->mask.y2 = y + height;
2228
2229 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2230 weston_view_geometry_dirty(view);
2231 }
2232}
2233
2234WL_EXPORT void
2235weston_layer_set_mask_infinite(struct weston_layer *layer)
2236{
2237 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2238 UINT32_MAX, UINT32_MAX);
2239}
2240
2241WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002242weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002243{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002244 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002245 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002246
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002247 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2248 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002249 return;
2250
Pekka Paalanenb5026542014-11-12 15:09:24 +02002251 if (!output->repaint_needed)
2252 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2253
Kristian Høgsbergef044142011-06-21 15:02:12 -04002254 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002255 output->repaint_needed = 1;
2256 if (output->repaint_scheduled)
2257 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002258
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002259 wl_event_loop_add_idle(loop, idle_repaint, output);
2260 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002261 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2262
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002263
2264 if (compositor->input_loop_source) {
2265 wl_event_source_remove(compositor->input_loop_source);
2266 compositor->input_loop_source = NULL;
2267 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002268}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002269
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002270WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002271weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2272{
2273 struct weston_output *output;
2274
2275 wl_list_for_each(output, &compositor->output_list, link)
2276 weston_output_schedule_repaint(output);
2277}
2278
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002279static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002280surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002281{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002282 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002283}
2284
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002285static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002286surface_attach(struct wl_client *client,
2287 struct wl_resource *resource,
2288 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2289{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002290 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002291 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002292
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002293 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002294 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002295 if (buffer == NULL) {
2296 wl_client_post_no_memory(client);
2297 return;
2298 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002299 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002300
Pekka Paalanende685b82012-12-04 15:58:12 +02002301 /* Attach, attach, without commit in between does not send
2302 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002303 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002304
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002305 surface->pending.sx = sx;
2306 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002307 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002308}
2309
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002310static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002311surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002312 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002313 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002314{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002315 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002316
Pekka Paalanen8e159182012-10-10 12:49:25 +03002317 pixman_region32_union_rect(&surface->pending.damage,
2318 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002319 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002320}
2321
Kristian Høgsberg33418202011-08-16 23:01:28 -04002322static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002323destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002324{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002325 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002326
2327 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002328 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002329}
2330
2331static void
2332surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002333 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002334{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002335 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002336 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002337
2338 cb = malloc(sizeof *cb);
2339 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002340 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002341 return;
2342 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002343
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002344 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2345 callback);
2346 if (cb->resource == NULL) {
2347 free(cb);
2348 wl_resource_post_no_memory(resource);
2349 return;
2350 }
2351
Jason Ekstranda85118c2013-06-27 20:17:02 -05002352 wl_resource_set_implementation(cb->resource, NULL, cb,
2353 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002354
Pekka Paalanenbc106382012-10-10 12:49:31 +03002355 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002356}
2357
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002358static void
2359surface_set_opaque_region(struct wl_client *client,
2360 struct wl_resource *resource,
2361 struct wl_resource *region_resource)
2362{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002363 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002364 struct weston_region *region;
2365
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002366 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002367 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002368 pixman_region32_copy(&surface->pending.opaque,
2369 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002370 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002371 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002372 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002373}
2374
2375static void
2376surface_set_input_region(struct wl_client *client,
2377 struct wl_resource *resource,
2378 struct wl_resource *region_resource)
2379{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002380 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002381 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002382
2383 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002384 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002385 pixman_region32_copy(&surface->pending.input,
2386 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002387 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002388 pixman_region32_fini(&surface->pending.input);
2389 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002390 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002391}
2392
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002393static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002394weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002395{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002396 struct weston_subsurface *sub;
2397
2398 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2399 parent_link_pending) {
2400 wl_list_remove(&sub->parent_link);
2401 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2402 }
2403}
2404
2405static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002406weston_surface_commit_state(struct weston_surface *surface,
2407 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002408{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002409 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002410 pixman_region32_t opaque;
2411
Alexander Larsson4ea95522013-05-22 14:41:37 +02002412 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002413 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002414 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002415 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002416
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002417 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002418 if (state->newly_attached)
2419 weston_surface_attach(surface, state->buffer);
2420 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002421
Jason Ekstrand7b982072014-05-20 14:33:03 -05002422 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002423 weston_surface_update_size(surface);
2424 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002425 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002426 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002427
Jason Ekstrand7b982072014-05-20 14:33:03 -05002428 state->sx = 0;
2429 state->sy = 0;
2430 state->newly_attached = 0;
2431 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002432
2433 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002434 if (weston_timeline_enabled_ &&
2435 pixman_region32_not_empty(&state->damage))
2436 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002437 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002438 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002439 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002440 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002441 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002442
2443 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002444 pixman_region32_init(&opaque);
2445 pixman_region32_intersect_rect(&opaque, &state->opaque,
2446 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002447
2448 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2449 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002450 wl_list_for_each(view, &surface->views, surface_link)
2451 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002452 }
2453
2454 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002455
2456 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002457 pixman_region32_intersect_rect(&surface->input, &state->input,
2458 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002459
Pekka Paalanenbc106382012-10-10 12:49:31 +03002460 /* wl_surface.frame */
2461 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002462 &state->frame_callback_list);
2463 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002464
2465 /* XXX:
2466 * What should happen with a feedback request, if there
2467 * is no wl_buffer attached for this commit?
2468 */
2469
2470 /* presentation.feedback */
2471 wl_list_insert_list(&surface->feedback_list,
2472 &state->feedback_list);
2473 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002474}
2475
2476static void
2477weston_surface_commit(struct weston_surface *surface)
2478{
2479 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002480
Pekka Paalanene67858b2013-04-25 13:57:42 +03002481 weston_surface_commit_subsurface_order(surface);
2482
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002483 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002484}
2485
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002486static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002487weston_subsurface_commit(struct weston_subsurface *sub);
2488
2489static void
2490weston_subsurface_parent_commit(struct weston_subsurface *sub,
2491 int parent_is_synchronized);
2492
2493static void
2494surface_commit(struct wl_client *client, struct wl_resource *resource)
2495{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002496 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002497 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2498
2499 if (sub) {
2500 weston_subsurface_commit(sub);
2501 return;
2502 }
2503
2504 weston_surface_commit(surface);
2505
2506 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2507 if (sub->surface != surface)
2508 weston_subsurface_parent_commit(sub, 0);
2509 }
2510}
2511
2512static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002513surface_set_buffer_transform(struct wl_client *client,
2514 struct wl_resource *resource, int transform)
2515{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002516 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002517
Jonny Lamba55f1392014-05-30 12:07:15 +02002518 /* if wl_output.transform grows more members this will need to be updated. */
2519 if (transform < 0 ||
2520 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2521 wl_resource_post_error(resource,
2522 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2523 "buffer transform must be a valid transform "
2524 "('%d' specified)", transform);
2525 return;
2526 }
2527
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002528 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002529 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002530}
2531
Alexander Larsson4ea95522013-05-22 14:41:37 +02002532static void
2533surface_set_buffer_scale(struct wl_client *client,
2534 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002535 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002536{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002537 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002538
Jonny Lamba55f1392014-05-30 12:07:15 +02002539 if (scale < 1) {
2540 wl_resource_post_error(resource,
2541 WL_SURFACE_ERROR_INVALID_SCALE,
2542 "buffer scale must be at least one "
2543 "('%d' specified)", scale);
2544 return;
2545 }
2546
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002547 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002548 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002549}
2550
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002551static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002552 surface_destroy,
2553 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002554 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002555 surface_frame,
2556 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002557 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002558 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002559 surface_set_buffer_transform,
2560 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002561};
2562
2563static void
2564compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002565 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002566{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002567 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002568 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002569
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002570 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002571 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002572 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002573 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002574 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002575
Jason Ekstranda85118c2013-06-27 20:17:02 -05002576 surface->resource =
2577 wl_resource_create(client, &wl_surface_interface,
2578 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002579 if (surface->resource == NULL) {
2580 weston_surface_destroy(surface);
2581 wl_resource_post_no_memory(resource);
2582 return;
2583 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002584 wl_resource_set_implementation(surface->resource, &surface_interface,
2585 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002586
2587 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002588}
2589
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002590static void
2591destroy_region(struct wl_resource *resource)
2592{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002593 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002594
2595 pixman_region32_fini(&region->region);
2596 free(region);
2597}
2598
2599static void
2600region_destroy(struct wl_client *client, struct wl_resource *resource)
2601{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002602 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002603}
2604
2605static void
2606region_add(struct wl_client *client, struct wl_resource *resource,
2607 int32_t x, int32_t y, int32_t width, int32_t height)
2608{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002609 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002610
2611 pixman_region32_union_rect(&region->region, &region->region,
2612 x, y, width, height);
2613}
2614
2615static void
2616region_subtract(struct wl_client *client, struct wl_resource *resource,
2617 int32_t x, int32_t y, int32_t width, int32_t height)
2618{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002619 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002620 pixman_region32_t rect;
2621
2622 pixman_region32_init_rect(&rect, x, y, width, height);
2623 pixman_region32_subtract(&region->region, &region->region, &rect);
2624 pixman_region32_fini(&rect);
2625}
2626
2627static const struct wl_region_interface region_interface = {
2628 region_destroy,
2629 region_add,
2630 region_subtract
2631};
2632
2633static void
2634compositor_create_region(struct wl_client *client,
2635 struct wl_resource *resource, uint32_t id)
2636{
2637 struct weston_region *region;
2638
2639 region = malloc(sizeof *region);
2640 if (region == NULL) {
2641 wl_resource_post_no_memory(resource);
2642 return;
2643 }
2644
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002645 pixman_region32_init(&region->region);
2646
Jason Ekstranda85118c2013-06-27 20:17:02 -05002647 region->resource =
2648 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002649 if (region->resource == NULL) {
2650 free(region);
2651 wl_resource_post_no_memory(resource);
2652 return;
2653 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002654 wl_resource_set_implementation(region->resource, &region_interface,
2655 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002656}
2657
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002658static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002659 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002660 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002661};
2662
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002663static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2665{
2666 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002667
Jason Ekstrand7b982072014-05-20 14:33:03 -05002668 weston_surface_commit_state(surface, &sub->cached);
2669 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002670
2671 weston_surface_commit_subsurface_order(surface);
2672
2673 weston_surface_schedule_repaint(surface);
2674
Jason Ekstrand7b982072014-05-20 14:33:03 -05002675 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676}
2677
2678static void
2679weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2680{
2681 struct weston_surface *surface = sub->surface;
2682
2683 /*
2684 * If this commit would cause the surface to move by the
2685 * attach(dx, dy) parameters, the old damage region must be
2686 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002687 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002688 */
2689 pixman_region32_translate(&sub->cached.damage,
2690 -surface->pending.sx, -surface->pending.sy);
2691 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2692 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002693 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694
2695 if (surface->pending.newly_attached) {
2696 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002697 weston_surface_state_set_buffer(&sub->cached,
2698 surface->pending.buffer);
2699 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002700 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002701 weston_presentation_feedback_discard_list(
2702 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002703 }
2704 sub->cached.sx += surface->pending.sx;
2705 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002706
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002707 sub->cached.buffer_viewport.changed |=
2708 surface->pending.buffer_viewport.changed;
2709 sub->cached.buffer_viewport.buffer =
2710 surface->pending.buffer_viewport.buffer;
2711 sub->cached.buffer_viewport.surface =
2712 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002713
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002714 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002715
2716 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2717
2718 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2719
2720 wl_list_insert_list(&sub->cached.frame_callback_list,
2721 &surface->pending.frame_callback_list);
2722 wl_list_init(&surface->pending.frame_callback_list);
2723
Pekka Paalanen133e4392014-09-23 22:08:46 -04002724 wl_list_insert_list(&sub->cached.feedback_list,
2725 &surface->pending.feedback_list);
2726 wl_list_init(&surface->pending.feedback_list);
2727
Jason Ekstrand7b982072014-05-20 14:33:03 -05002728 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729}
2730
Derek Foreman280e7dd2014-10-03 13:13:42 -05002731static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2733{
2734 while (sub) {
2735 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002736 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002737
2738 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002739 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002740
2741 sub = weston_surface_to_subsurface(sub->parent);
2742 }
2743
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002744 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002745}
2746
2747static void
2748weston_subsurface_commit(struct weston_subsurface *sub)
2749{
2750 struct weston_surface *surface = sub->surface;
2751 struct weston_subsurface *tmp;
2752
2753 /* Recursive check for effectively synchronized. */
2754 if (weston_subsurface_is_synchronized(sub)) {
2755 weston_subsurface_commit_to_cache(sub);
2756 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002757 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758 /* flush accumulated state from cache */
2759 weston_subsurface_commit_to_cache(sub);
2760 weston_subsurface_commit_from_cache(sub);
2761 } else {
2762 weston_surface_commit(surface);
2763 }
2764
2765 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2766 if (tmp->surface != surface)
2767 weston_subsurface_parent_commit(tmp, 0);
2768 }
2769 }
2770}
2771
2772static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002773weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774{
2775 struct weston_surface *surface = sub->surface;
2776 struct weston_subsurface *tmp;
2777
Pekka Paalanene67858b2013-04-25 13:57:42 +03002778 /* From now on, commit_from_cache the whole sub-tree, regardless of
2779 * the synchronized mode of each child. This sub-surface or some
2780 * of its ancestors were synchronized, so we are synchronized
2781 * all the way down.
2782 */
2783
Jason Ekstrand7b982072014-05-20 14:33:03 -05002784 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002785 weston_subsurface_commit_from_cache(sub);
2786
2787 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2788 if (tmp->surface != surface)
2789 weston_subsurface_parent_commit(tmp, 1);
2790 }
2791}
2792
2793static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002794weston_subsurface_parent_commit(struct weston_subsurface *sub,
2795 int parent_is_synchronized)
2796{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002798 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 wl_list_for_each(view, &sub->surface->views, surface_link)
2800 weston_view_set_position(view,
2801 sub->position.x,
2802 sub->position.y);
2803
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002804 sub->position.set = 0;
2805 }
2806
2807 if (parent_is_synchronized || sub->synchronized)
2808 weston_subsurface_synchronized_commit(sub);
2809}
2810
Pekka Paalanen8274d902014-08-06 19:36:51 +03002811static int
2812subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2813{
2814 return snprintf(buf, len, "sub-surface");
2815}
2816
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002817static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002818subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819{
2820 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002821 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002822
Jason Ekstranda7af7042013-10-12 22:38:11 -05002823 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002824 weston_view_set_position(view,
2825 view->geometry.x + dx,
2826 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002827
2828 /* No need to check parent mappedness, because if parent is not
2829 * mapped, parent is not in a visible layer, so this sub-surface
2830 * will not be drawn either.
2831 */
2832 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002833 struct weston_output *output;
2834
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002835 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836 * because that would call it also for the parent surface,
2837 * which might not be mapped yet. That would lead to
2838 * inconsistent state, where the window could never be
2839 * mapped.
2840 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002841 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002842 * weston_surface_is_mapped() return true, so that when the
2843 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002844 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002845 */
2846 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002847 output = container_of(compositor->output_list.next,
2848 struct weston_output, link);
2849
2850 surface->output = output;
2851 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002852 }
2853}
2854
2855static struct weston_subsurface *
2856weston_surface_to_subsurface(struct weston_surface *surface)
2857{
2858 if (surface->configure == subsurface_configure)
2859 return surface->configure_private;
2860
2861 return NULL;
2862}
2863
Pekka Paalanen01388e22013-04-25 13:57:44 +03002864WL_EXPORT struct weston_surface *
2865weston_surface_get_main_surface(struct weston_surface *surface)
2866{
2867 struct weston_subsurface *sub;
2868
2869 while (surface && (sub = weston_surface_to_subsurface(surface)))
2870 surface = sub->parent;
2871
2872 return surface;
2873}
2874
Pekka Paalanen50b67472014-10-01 15:02:41 +03002875WL_EXPORT int
2876weston_surface_set_role(struct weston_surface *surface,
2877 const char *role_name,
2878 struct wl_resource *error_resource,
2879 uint32_t error_code)
2880{
2881 assert(role_name);
2882
2883 if (surface->role_name == NULL ||
2884 surface->role_name == role_name ||
2885 strcmp(surface->role_name, role_name) == 0) {
2886 surface->role_name = role_name;
2887
2888 return 0;
2889 }
2890
2891 wl_resource_post_error(error_resource, error_code,
2892 "Cannot assign role %s to wl_surface@%d,"
2893 " already has role %s\n",
2894 role_name,
2895 wl_resource_get_id(surface->resource),
2896 surface->role_name);
2897 return -1;
2898}
2899
Pekka Paalanen8274d902014-08-06 19:36:51 +03002900WL_EXPORT void
2901weston_surface_set_label_func(struct weston_surface *surface,
2902 int (*desc)(struct weston_surface *,
2903 char *, size_t))
2904{
2905 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002906 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002907}
2908
Pekka Paalanene67858b2013-04-25 13:57:42 +03002909static void
2910subsurface_set_position(struct wl_client *client,
2911 struct wl_resource *resource, int32_t x, int32_t y)
2912{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002913 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002914
2915 if (!sub)
2916 return;
2917
2918 sub->position.x = x;
2919 sub->position.y = y;
2920 sub->position.set = 1;
2921}
2922
2923static struct weston_subsurface *
2924subsurface_from_surface(struct weston_surface *surface)
2925{
2926 struct weston_subsurface *sub;
2927
2928 sub = weston_surface_to_subsurface(surface);
2929 if (sub)
2930 return sub;
2931
2932 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2933 if (sub->surface == surface)
2934 return sub;
2935
2936 return NULL;
2937}
2938
2939static struct weston_subsurface *
2940subsurface_sibling_check(struct weston_subsurface *sub,
2941 struct weston_surface *surface,
2942 const char *request)
2943{
2944 struct weston_subsurface *sibling;
2945
2946 sibling = subsurface_from_surface(surface);
2947
2948 if (!sibling) {
2949 wl_resource_post_error(sub->resource,
2950 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2951 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002952 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002953 return NULL;
2954 }
2955
2956 if (sibling->parent != sub->parent) {
2957 wl_resource_post_error(sub->resource,
2958 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2959 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002960 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002961 return NULL;
2962 }
2963
2964 return sibling;
2965}
2966
2967static void
2968subsurface_place_above(struct wl_client *client,
2969 struct wl_resource *resource,
2970 struct wl_resource *sibling_resource)
2971{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002972 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002973 struct weston_surface *surface =
2974 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002975 struct weston_subsurface *sibling;
2976
2977 if (!sub)
2978 return;
2979
2980 sibling = subsurface_sibling_check(sub, surface, "place_above");
2981 if (!sibling)
2982 return;
2983
2984 wl_list_remove(&sub->parent_link_pending);
2985 wl_list_insert(sibling->parent_link_pending.prev,
2986 &sub->parent_link_pending);
2987}
2988
2989static void
2990subsurface_place_below(struct wl_client *client,
2991 struct wl_resource *resource,
2992 struct wl_resource *sibling_resource)
2993{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002994 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002995 struct weston_surface *surface =
2996 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002997 struct weston_subsurface *sibling;
2998
2999 if (!sub)
3000 return;
3001
3002 sibling = subsurface_sibling_check(sub, surface, "place_below");
3003 if (!sibling)
3004 return;
3005
3006 wl_list_remove(&sub->parent_link_pending);
3007 wl_list_insert(&sibling->parent_link_pending,
3008 &sub->parent_link_pending);
3009}
3010
3011static void
3012subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3013{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003014 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003015
3016 if (sub)
3017 sub->synchronized = 1;
3018}
3019
3020static void
3021subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3022{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003023 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003024
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003025 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003026 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003027
3028 /* If sub became effectively desynchronized, flush. */
3029 if (!weston_subsurface_is_synchronized(sub))
3030 weston_subsurface_synchronized_commit(sub);
3031 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032}
3033
3034static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003035weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3036{
3037 wl_list_remove(&sub->parent_link);
3038 wl_list_remove(&sub->parent_link_pending);
3039 wl_list_remove(&sub->parent_destroy_listener.link);
3040 sub->parent = NULL;
3041}
3042
3043static void
3044weston_subsurface_destroy(struct weston_subsurface *sub);
3045
3046static void
3047subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3048{
3049 struct weston_subsurface *sub =
3050 container_of(listener, struct weston_subsurface,
3051 surface_destroy_listener);
3052 assert(data == &sub->surface->resource);
3053
3054 /* The protocol object (wl_resource) is left inert. */
3055 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003056 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003057
3058 weston_subsurface_destroy(sub);
3059}
3060
3061static void
3062subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3063{
3064 struct weston_subsurface *sub =
3065 container_of(listener, struct weston_subsurface,
3066 parent_destroy_listener);
3067 assert(data == &sub->parent->resource);
3068 assert(sub->surface != sub->parent);
3069
3070 if (weston_surface_is_mapped(sub->surface))
3071 weston_surface_unmap(sub->surface);
3072
3073 weston_subsurface_unlink_parent(sub);
3074}
3075
3076static void
3077subsurface_resource_destroy(struct wl_resource *resource)
3078{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003079 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003080
3081 if (sub)
3082 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003083}
3084
3085static void
3086subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3087{
3088 wl_resource_destroy(resource);
3089}
3090
3091static void
3092weston_subsurface_link_parent(struct weston_subsurface *sub,
3093 struct weston_surface *parent)
3094{
3095 sub->parent = parent;
3096 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003097 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003098 &sub->parent_destroy_listener);
3099
3100 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3101 wl_list_insert(&parent->subsurface_list_pending,
3102 &sub->parent_link_pending);
3103}
3104
3105static void
3106weston_subsurface_link_surface(struct weston_subsurface *sub,
3107 struct weston_surface *surface)
3108{
3109 sub->surface = surface;
3110 sub->surface_destroy_listener.notify =
3111 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003112 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003113 &sub->surface_destroy_listener);
3114}
3115
3116static void
3117weston_subsurface_destroy(struct weston_subsurface *sub)
3118{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003119 struct weston_view *view, *next;
3120
Pekka Paalanene67858b2013-04-25 13:57:42 +03003121 assert(sub->surface);
3122
3123 if (sub->resource) {
3124 assert(weston_surface_to_subsurface(sub->surface) == sub);
3125 assert(sub->parent_destroy_listener.notify ==
3126 subsurface_handle_parent_destroy);
3127
George Kiagiadakised04d382014-06-13 18:10:26 +02003128 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3129 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003130 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003131 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003132
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133 if (sub->parent)
3134 weston_subsurface_unlink_parent(sub);
3135
Jason Ekstrand7b982072014-05-20 14:33:03 -05003136 weston_surface_state_fini(&sub->cached);
3137 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138
3139 sub->surface->configure = NULL;
3140 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003141 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142 } else {
3143 /* the dummy weston_subsurface for the parent itself */
3144 assert(sub->parent_destroy_listener.notify == NULL);
3145 wl_list_remove(&sub->parent_link);
3146 wl_list_remove(&sub->parent_link_pending);
3147 }
3148
3149 wl_list_remove(&sub->surface_destroy_listener.link);
3150 free(sub);
3151}
3152
3153static const struct wl_subsurface_interface subsurface_implementation = {
3154 subsurface_destroy,
3155 subsurface_set_position,
3156 subsurface_place_above,
3157 subsurface_place_below,
3158 subsurface_set_sync,
3159 subsurface_set_desync
3160};
3161
3162static struct weston_subsurface *
3163weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3164 struct weston_surface *parent)
3165{
3166 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003167 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003168
Bryce Harringtonde16d892014-11-20 22:21:57 -08003169 sub = zalloc(sizeof *sub);
3170 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003171 return NULL;
3172
Jason Ekstranda7af7042013-10-12 22:38:11 -05003173 wl_list_init(&sub->unused_views);
3174
Jason Ekstranda85118c2013-06-27 20:17:02 -05003175 sub->resource =
3176 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003177 if (!sub->resource) {
3178 free(sub);
3179 return NULL;
3180 }
3181
Jason Ekstranda85118c2013-06-27 20:17:02 -05003182 wl_resource_set_implementation(sub->resource,
3183 &subsurface_implementation,
3184 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003185 weston_subsurface_link_surface(sub, surface);
3186 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003187 weston_surface_state_init(&sub->cached);
3188 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003190
3191 return sub;
3192}
3193
3194/* Create a dummy subsurface for having the parent itself in its
3195 * sub-surface lists. Makes stacking order manipulation easy.
3196 */
3197static struct weston_subsurface *
3198weston_subsurface_create_for_parent(struct weston_surface *parent)
3199{
3200 struct weston_subsurface *sub;
3201
Bryce Harringtonde16d892014-11-20 22:21:57 -08003202 sub = zalloc(sizeof *sub);
3203 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204 return NULL;
3205
3206 weston_subsurface_link_surface(sub, parent);
3207 sub->parent = parent;
3208 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3209 wl_list_insert(&parent->subsurface_list_pending,
3210 &sub->parent_link_pending);
3211
3212 return sub;
3213}
3214
3215static void
3216subcompositor_get_subsurface(struct wl_client *client,
3217 struct wl_resource *resource,
3218 uint32_t id,
3219 struct wl_resource *surface_resource,
3220 struct wl_resource *parent_resource)
3221{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003222 struct weston_surface *surface =
3223 wl_resource_get_user_data(surface_resource);
3224 struct weston_surface *parent =
3225 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003226 struct weston_subsurface *sub;
3227 static const char where[] = "get_subsurface: wl_subsurface@";
3228
3229 if (surface == parent) {
3230 wl_resource_post_error(resource,
3231 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3232 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003233 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003234 return;
3235 }
3236
3237 if (weston_surface_to_subsurface(surface)) {
3238 wl_resource_post_error(resource,
3239 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3240 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003241 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003242 return;
3243 }
3244
Pekka Paalanen50b67472014-10-01 15:02:41 +03003245 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3246 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003247 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003248
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003249 if (weston_surface_get_main_surface(parent) == surface) {
3250 wl_resource_post_error(resource,
3251 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3252 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003253 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003254 return;
3255 }
3256
Pekka Paalanene67858b2013-04-25 13:57:42 +03003257 /* make sure the parent is in its own list */
3258 if (wl_list_empty(&parent->subsurface_list)) {
3259 if (!weston_subsurface_create_for_parent(parent)) {
3260 wl_resource_post_no_memory(resource);
3261 return;
3262 }
3263 }
3264
3265 sub = weston_subsurface_create(id, surface, parent);
3266 if (!sub) {
3267 wl_resource_post_no_memory(resource);
3268 return;
3269 }
3270
3271 surface->configure = subsurface_configure;
3272 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003273 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003274}
3275
3276static void
3277subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3278{
3279 wl_resource_destroy(resource);
3280}
3281
3282static const struct wl_subcompositor_interface subcompositor_interface = {
3283 subcompositor_destroy,
3284 subcompositor_get_subsurface
3285};
3286
3287static void
3288bind_subcompositor(struct wl_client *client,
3289 void *data, uint32_t version, uint32_t id)
3290{
3291 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003292 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003293
Jason Ekstranda85118c2013-06-27 20:17:02 -05003294 resource =
3295 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003296 if (resource == NULL) {
3297 wl_client_post_no_memory(client);
3298 return;
3299 }
3300 wl_resource_set_implementation(resource, &subcompositor_interface,
3301 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003302}
3303
3304static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003305weston_compositor_dpms(struct weston_compositor *compositor,
3306 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003307{
3308 struct weston_output *output;
3309
3310 wl_list_for_each(output, &compositor->output_list, link)
3311 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003312 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003313}
3314
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003315WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003316weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003317{
Neil Roberts8b62e202013-09-30 13:14:47 +01003318 uint32_t old_state = compositor->state;
3319
3320 /* The state needs to be changed before emitting the wake
3321 * signal because that may try to schedule a repaint which
3322 * will not work if the compositor is still sleeping */
3323 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3324
3325 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003326 case WESTON_COMPOSITOR_SLEEPING:
3327 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3328 /* fall through */
3329 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003330 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003331 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003332 /* fall through */
3333 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003334 wl_event_source_timer_update(compositor->idle_source,
3335 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003336 }
3337}
3338
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003339WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003340weston_compositor_offscreen(struct weston_compositor *compositor)
3341{
3342 switch (compositor->state) {
3343 case WESTON_COMPOSITOR_OFFSCREEN:
3344 return;
3345 case WESTON_COMPOSITOR_SLEEPING:
3346 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3347 /* fall through */
3348 default:
3349 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3350 wl_event_source_timer_update(compositor->idle_source, 0);
3351 }
3352}
3353
3354WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003355weston_compositor_sleep(struct weston_compositor *compositor)
3356{
3357 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3358 return;
3359
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003360 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003361 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3362 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3363}
3364
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003365static int
3366idle_handler(void *data)
3367{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003368 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003369
3370 if (compositor->idle_inhibit)
3371 return 1;
3372
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003373 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003374 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003375
3376 return 1;
3377}
3378
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003379WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003380weston_plane_init(struct weston_plane *plane,
3381 struct weston_compositor *ec,
3382 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003383{
3384 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003385 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003386 plane->x = x;
3387 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003388 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003389
3390 /* Init the link so that the call to wl_list_remove() when releasing
3391 * the plane without ever stacking doesn't lead to a crash */
3392 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003393}
3394
3395WL_EXPORT void
3396weston_plane_release(struct weston_plane *plane)
3397{
Xiong Zhang97116532013-10-23 13:58:31 +08003398 struct weston_view *view;
3399
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003400 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003401 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003402
Xiong Zhang97116532013-10-23 13:58:31 +08003403 wl_list_for_each(view, &plane->compositor->view_list, link) {
3404 if (view->plane == plane)
3405 view->plane = NULL;
3406 }
3407
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003408 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003409}
3410
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003411WL_EXPORT void
3412weston_compositor_stack_plane(struct weston_compositor *ec,
3413 struct weston_plane *plane,
3414 struct weston_plane *above)
3415{
3416 if (above)
3417 wl_list_insert(above->link.prev, &plane->link);
3418 else
3419 wl_list_insert(&ec->plane_list, &plane->link);
3420}
3421
Casey Dahlin9074db52012-04-19 22:50:09 -04003422static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003423{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003424 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003425}
3426
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003427static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003428bind_output(struct wl_client *client,
3429 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003430{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003431 struct weston_output *output = data;
3432 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003433 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003434
Jason Ekstranda85118c2013-06-27 20:17:02 -05003435 resource = wl_resource_create(client, &wl_output_interface,
3436 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003437 if (resource == NULL) {
3438 wl_client_post_no_memory(client);
3439 return;
3440 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003441
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003442 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003443 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003444
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003445 wl_output_send_geometry(resource,
3446 output->x,
3447 output->y,
3448 output->mm_width,
3449 output->mm_height,
3450 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003451 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003452 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003453 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003454 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003455 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003456
3457 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003458 wl_output_send_mode(resource,
3459 mode->flags,
3460 mode->width,
3461 mode->height,
3462 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003463 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003464
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003465 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003466 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003467}
3468
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003469/* Move other outputs when one is removed so the space remains contiguos. */
3470static void
3471weston_compositor_remove_output(struct weston_compositor *compositor,
3472 struct weston_output *remove_output)
3473{
3474 struct weston_output *output;
3475 int offset = 0;
3476
3477 wl_list_for_each(output, &compositor->output_list, link) {
3478 if (output == remove_output) {
3479 offset = output->width;
3480 continue;
3481 }
3482
3483 if (offset > 0) {
3484 weston_output_move(output,
3485 output->x - offset, output->y);
3486 output->dirty = 1;
3487 }
3488 }
3489}
3490
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003491WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003492weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003493{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003494 struct wl_resource *resource;
3495
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003496 output->destroying = 1;
3497
Pekka Paalanen133e4392014-09-23 22:08:46 -04003498 weston_presentation_feedback_discard_list(&output->feedback_list);
3499
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003500 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003501 wl_list_remove(&output->link);
3502
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003503 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003504 wl_signal_emit(&output->destroy_signal, output);
3505
Richard Hughesafe690c2013-05-02 10:10:04 +01003506 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003507 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003508 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003509 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003510
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003511 wl_resource_for_each(resource, &output->resource_list) {
3512 wl_resource_set_destructor(resource, NULL);
3513 }
3514
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003515 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003516}
3517
Scott Moreau1bad5db2012-08-18 01:04:05 -06003518static void
3519weston_output_compute_transform(struct weston_output *output)
3520{
3521 struct weston_matrix transform;
3522 int flip;
3523
3524 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003525 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003526
3527 switch(output->transform) {
3528 case WL_OUTPUT_TRANSFORM_FLIPPED:
3529 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3530 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3531 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003532 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003533 flip = -1;
3534 break;
3535 default:
3536 flip = 1;
3537 break;
3538 }
3539
3540 switch(output->transform) {
3541 case WL_OUTPUT_TRANSFORM_NORMAL:
3542 case WL_OUTPUT_TRANSFORM_FLIPPED:
3543 transform.d[0] = flip;
3544 transform.d[1] = 0;
3545 transform.d[4] = 0;
3546 transform.d[5] = 1;
3547 break;
3548 case WL_OUTPUT_TRANSFORM_90:
3549 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3550 transform.d[0] = 0;
3551 transform.d[1] = -flip;
3552 transform.d[4] = 1;
3553 transform.d[5] = 0;
3554 break;
3555 case WL_OUTPUT_TRANSFORM_180:
3556 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3557 transform.d[0] = -flip;
3558 transform.d[1] = 0;
3559 transform.d[4] = 0;
3560 transform.d[5] = -1;
3561 break;
3562 case WL_OUTPUT_TRANSFORM_270:
3563 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3564 transform.d[0] = 0;
3565 transform.d[1] = flip;
3566 transform.d[4] = -1;
3567 transform.d[5] = 0;
3568 break;
3569 default:
3570 break;
3571 }
3572
3573 weston_matrix_multiply(&output->matrix, &transform);
3574}
3575
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003576WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003577weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003578{
Scott Moreau850ca422012-05-21 15:21:25 -06003579 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003580
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003581 weston_matrix_init(&output->matrix);
3582 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003583 -(output->x + output->width / 2.0),
3584 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003585
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003586 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003587 2.0 / output->width,
3588 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003589
Scott Moreauccbf29d2012-02-22 14:21:41 -07003590 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003591 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003592 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003593 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3594 output->zoom.trans_y, 0);
3595 weston_matrix_scale(&output->matrix, magnification,
3596 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003597 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003598
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003599 weston_output_compute_transform(output);
3600
Scott Moreauccbf29d2012-02-22 14:21:41 -07003601 output->dirty = 0;
3602}
3603
Scott Moreau1bad5db2012-08-18 01:04:05 -06003604static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003605weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003606{
3607 output->transform = transform;
3608
3609 switch (transform) {
3610 case WL_OUTPUT_TRANSFORM_90:
3611 case WL_OUTPUT_TRANSFORM_270:
3612 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3613 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3614 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003615 output->width = output->current_mode->height;
3616 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003617 break;
3618 case WL_OUTPUT_TRANSFORM_NORMAL:
3619 case WL_OUTPUT_TRANSFORM_180:
3620 case WL_OUTPUT_TRANSFORM_FLIPPED:
3621 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003622 output->width = output->current_mode->width;
3623 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003624 break;
3625 default:
3626 break;
3627 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003628
Hardening57388e42013-09-18 23:56:36 +02003629 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003630 output->width /= scale;
3631 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003632}
3633
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003634static void
3635weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003636{
3637 output->x = x;
3638 output->y = y;
3639
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003640 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003641 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003642 output->width,
3643 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003644}
3645
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003646WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003647weston_output_move(struct weston_output *output, int x, int y)
3648{
3649 pixman_region32_t old_region;
3650 struct wl_resource *resource;
3651
3652 output->move_x = x - output->x;
3653 output->move_y = y - output->y;
3654
3655 if (output->move_x == 0 && output->move_y == 0)
3656 return;
3657
3658 pixman_region32_init(&old_region);
3659 pixman_region32_copy(&old_region, &output->region);
3660
3661 weston_output_init_geometry(output, x, y);
3662
3663 output->dirty = 1;
3664
3665 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003666 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003667
3668 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003669 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003670 wl_output_send_geometry(resource,
3671 output->x,
3672 output->y,
3673 output->mm_width,
3674 output->mm_height,
3675 output->subpixel,
3676 output->make,
3677 output->model,
3678 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003679
3680 if (wl_resource_get_version(resource) >= 2)
3681 wl_output_send_done(resource);
3682 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003683}
3684
3685WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003686weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003687 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003688 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003689{
3690 output->compositor = c;
3691 output->x = x;
3692 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003693 output->mm_width = mm_width;
3694 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003695 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003696 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003697
Alexander Larsson0b135062013-05-28 16:23:36 +02003698 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003699 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003700
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003701 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003702 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003703
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003704 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003705 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003706 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003707 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003708 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003709
Casey Dahlin58ba1372012-04-19 22:50:08 -04003710 output->id = ffs(~output->compositor->output_id_pool) - 1;
3711 output->compositor->output_id_pool |= 1 << output->id;
3712
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003713 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003714 wl_global_create(c->wl_display, &wl_output_interface, 2,
3715 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003716 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003717}
3718
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003719WL_EXPORT void
3720weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003721 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003722 wl_fixed_t *x, wl_fixed_t *y)
3723{
3724 wl_fixed_t tx, ty;
3725 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003726 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003727
Hardeningff39efa2013-09-18 23:56:35 +02003728 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3729 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003730
3731 switch(output->transform) {
3732 case WL_OUTPUT_TRANSFORM_NORMAL:
3733 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003734 tx = device_x;
3735 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003736 break;
3737 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003738 tx = device_y;
3739 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003740 break;
3741 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003742 tx = width - device_x;
3743 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003744 break;
3745 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003746 tx = width - device_y;
3747 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003748 break;
3749 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003750 tx = width - device_x;
3751 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003752 break;
3753 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003754 tx = width - device_y;
3755 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003756 break;
3757 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003758 tx = device_x;
3759 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003760 break;
3761 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003762 tx = device_y;
3763 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003764 break;
3765 }
3766
Neil Robertseb5a2002014-05-01 16:13:55 +01003767 tx /= output->current_scale;
3768 ty /= output->current_scale;
3769
3770 if (output->zoom.active) {
3771 zoom_scale = output->zoom.spring_z.current;
3772 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3773 output->width / 2.0f *
3774 (zoom_scale + output->zoom.trans_x));
3775 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3776 output->height / 2.0f *
3777 (zoom_scale + output->zoom.trans_y));
3778 tx = wl_fixed_from_double(zx);
3779 ty = wl_fixed_from_double(zy);
3780 }
3781
3782 *x = tx + wl_fixed_from_int(output->x);
3783 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003784}
3785
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003786static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003787destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003788{
Jonny Lamb74130762013-11-26 18:19:46 +01003789 struct weston_surface *surface =
3790 wl_resource_get_user_data(resource);
3791
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003792 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003793 surface->pending.buffer_viewport.buffer.src_width =
3794 wl_fixed_from_int(-1);
3795 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003796 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003797}
3798
3799static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003800viewport_destroy(struct wl_client *client,
3801 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003802{
3803 wl_resource_destroy(resource);
3804}
3805
3806static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003807viewport_set(struct wl_client *client,
3808 struct wl_resource *resource,
3809 wl_fixed_t src_x,
3810 wl_fixed_t src_y,
3811 wl_fixed_t src_width,
3812 wl_fixed_t src_height,
3813 int32_t dst_width,
3814 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003815{
Jonny Lamb74130762013-11-26 18:19:46 +01003816 struct weston_surface *surface =
3817 wl_resource_get_user_data(resource);
3818
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003819 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003820
Jonny Lamb8ae35902013-11-26 18:19:45 +01003821 if (wl_fixed_to_double(src_width) < 0 ||
3822 wl_fixed_to_double(src_height) < 0) {
3823 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003824 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003825 "source dimensions must be non-negative (%fx%f)",
3826 wl_fixed_to_double(src_width),
3827 wl_fixed_to_double(src_height));
3828 return;
3829 }
3830
3831 if (dst_width <= 0 || dst_height <= 0) {
3832 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003833 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003834 "destination dimensions must be positive (%dx%d)",
3835 dst_width, dst_height);
3836 return;
3837 }
3838
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003839 surface->pending.buffer_viewport.buffer.src_x = src_x;
3840 surface->pending.buffer_viewport.buffer.src_y = src_y;
3841 surface->pending.buffer_viewport.buffer.src_width = src_width;
3842 surface->pending.buffer_viewport.buffer.src_height = src_height;
3843 surface->pending.buffer_viewport.surface.width = dst_width;
3844 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003845 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003846}
3847
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003848static void
3849viewport_set_source(struct wl_client *client,
3850 struct wl_resource *resource,
3851 wl_fixed_t src_x,
3852 wl_fixed_t src_y,
3853 wl_fixed_t src_width,
3854 wl_fixed_t src_height)
3855{
3856 struct weston_surface *surface =
3857 wl_resource_get_user_data(resource);
3858
3859 assert(surface->viewport_resource != NULL);
3860
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003861 if (src_width == wl_fixed_from_int(-1) &&
3862 src_height == wl_fixed_from_int(-1)) {
3863 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003864 surface->pending.buffer_viewport.buffer.src_width =
3865 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003866 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003867 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003868 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003869
3870 if (src_width <= 0 || src_height <= 0) {
3871 wl_resource_post_error(resource,
3872 WL_VIEWPORT_ERROR_BAD_VALUE,
3873 "source size must be positive (%fx%f)",
3874 wl_fixed_to_double(src_width),
3875 wl_fixed_to_double(src_height));
3876 return;
3877 }
3878
3879 surface->pending.buffer_viewport.buffer.src_x = src_x;
3880 surface->pending.buffer_viewport.buffer.src_y = src_y;
3881 surface->pending.buffer_viewport.buffer.src_width = src_width;
3882 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003883 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003884}
3885
3886static void
3887viewport_set_destination(struct wl_client *client,
3888 struct wl_resource *resource,
3889 int32_t dst_width,
3890 int32_t dst_height)
3891{
3892 struct weston_surface *surface =
3893 wl_resource_get_user_data(resource);
3894
3895 assert(surface->viewport_resource != NULL);
3896
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003897 if (dst_width == -1 && dst_height == -1) {
3898 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003899 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003900 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003901 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003902 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003903
3904 if (dst_width <= 0 || dst_height <= 0) {
3905 wl_resource_post_error(resource,
3906 WL_VIEWPORT_ERROR_BAD_VALUE,
3907 "destination size must be positive (%dx%d)",
3908 dst_width, dst_height);
3909 return;
3910 }
3911
3912 surface->pending.buffer_viewport.surface.width = dst_width;
3913 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003914 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003915}
3916
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003917static const struct wl_viewport_interface viewport_interface = {
3918 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003919 viewport_set,
3920 viewport_set_source,
3921 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003922};
3923
3924static void
3925scaler_destroy(struct wl_client *client,
3926 struct wl_resource *resource)
3927{
3928 wl_resource_destroy(resource);
3929}
3930
3931static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003932scaler_get_viewport(struct wl_client *client,
3933 struct wl_resource *scaler,
3934 uint32_t id,
3935 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003936{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003937 int version = wl_resource_get_version(scaler);
3938 struct weston_surface *surface =
3939 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003940 struct wl_resource *resource;
3941
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003942 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003943 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003944 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3945 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003946 return;
3947 }
3948
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003949 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003950 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003951 if (resource == NULL) {
3952 wl_client_post_no_memory(client);
3953 return;
3954 }
3955
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003956 wl_resource_set_implementation(resource, &viewport_interface,
3957 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003958
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003959 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003960}
3961
3962static const struct wl_scaler_interface scaler_interface = {
3963 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003964 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003965};
3966
3967static void
3968bind_scaler(struct wl_client *client,
3969 void *data, uint32_t version, uint32_t id)
3970{
3971 struct wl_resource *resource;
3972
3973 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003974 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003975 if (resource == NULL) {
3976 wl_client_post_no_memory(client);
3977 return;
3978 }
3979
3980 wl_resource_set_implementation(resource, &scaler_interface,
3981 NULL, NULL);
3982}
3983
3984static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003985destroy_presentation_feedback(struct wl_resource *feedback_resource)
3986{
3987 struct weston_presentation_feedback *feedback;
3988
3989 feedback = wl_resource_get_user_data(feedback_resource);
3990
3991 wl_list_remove(&feedback->link);
3992 free(feedback);
3993}
3994
3995static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003996presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3997{
3998 wl_resource_destroy(resource);
3999}
4000
4001static void
4002presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004003 struct wl_resource *presentation_resource,
4004 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004005 uint32_t callback)
4006{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004007 struct weston_surface *surface;
4008 struct weston_presentation_feedback *feedback;
4009
4010 surface = wl_resource_get_user_data(surface_resource);
4011
Bryce Harringtonde16d892014-11-20 22:21:57 -08004012 feedback = zalloc(sizeof *feedback);
4013 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004014 goto err_calloc;
4015
4016 feedback->resource = wl_resource_create(client,
4017 &presentation_feedback_interface,
4018 1, callback);
4019 if (!feedback->resource)
4020 goto err_create;
4021
4022 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4023 destroy_presentation_feedback);
4024
4025 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4026
4027 return;
4028
4029err_create:
4030 free(feedback);
4031
4032err_calloc:
4033 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004034}
4035
4036static const struct presentation_interface presentation_implementation = {
4037 presentation_destroy,
4038 presentation_feedback
4039};
4040
4041static void
4042bind_presentation(struct wl_client *client,
4043 void *data, uint32_t version, uint32_t id)
4044{
4045 struct weston_compositor *compositor = data;
4046 struct wl_resource *resource;
4047
4048 resource = wl_resource_create(client, &presentation_interface,
4049 MIN(version, 1), id);
4050 if (resource == NULL) {
4051 wl_client_post_no_memory(client);
4052 return;
4053 }
4054
4055 wl_resource_set_implementation(resource, &presentation_implementation,
4056 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004057 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004058}
4059
4060static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004061compositor_bind(struct wl_client *client,
4062 void *data, uint32_t version, uint32_t id)
4063{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004064 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004065 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004066
Jason Ekstranda85118c2013-06-27 20:17:02 -05004067 resource = wl_resource_create(client, &wl_compositor_interface,
4068 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004069 if (resource == NULL) {
4070 wl_client_post_no_memory(client);
4071 return;
4072 }
4073
4074 wl_resource_set_implementation(resource, &compositor_interface,
4075 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004076}
4077
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004078static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004079log_uname(void)
4080{
4081 struct utsname usys;
4082
4083 uname(&usys);
4084
4085 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4086 usys.version, usys.machine);
4087}
4088
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004089WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004090weston_environment_get_fd(const char *env)
4091{
4092 char *e, *end;
4093 int fd, flags;
4094
4095 e = getenv(env);
4096 if (!e)
4097 return -1;
4098 fd = strtol(e, &end, 0);
4099 if (*end != '\0')
4100 return -1;
4101
4102 flags = fcntl(fd, F_GETFD);
4103 if (flags == -1)
4104 return -1;
4105
4106 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4107 unsetenv(env);
4108
4109 return fd;
4110}
4111
Pekka Paalanenb5026542014-11-12 15:09:24 +02004112static void
4113timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4114 uint32_t key, void *data)
4115{
4116 struct weston_compositor *compositor = data;
4117
4118 if (weston_timeline_enabled_)
4119 weston_timeline_close();
4120 else
4121 weston_timeline_open(compositor);
4122}
4123
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004124WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004125weston_compositor_init(struct weston_compositor *ec,
4126 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004127 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004128 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004129{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004130 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004131 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004132 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004133
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004134 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004135 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004136 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004137 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004138 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004139 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004140 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004141 wl_signal_init(&ec->idle_signal);
4142 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004143 wl_signal_init(&ec->show_input_panel_signal);
4144 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004145 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004146 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004147 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004148 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004149 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004150 wl_signal_init(&ec->session_signal);
4151 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004152
Casey Dahlin58ba1372012-04-19 22:50:08 -04004153 ec->output_id_pool = 0;
4154
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004155 if (!wl_global_create(display, &wl_compositor_interface, 3,
4156 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004157 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004158
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004159 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4160 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004161 return -1;
4162
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004163 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004164 ec, bind_scaler))
4165 return -1;
4166
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004167 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4168 ec, bind_presentation))
4169 return -1;
4170
Jason Ekstranda7af7042013-10-12 22:38:11 -05004171 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004172 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004173 wl_list_init(&ec->layer_list);
4174 wl_list_init(&ec->seat_list);
4175 wl_list_init(&ec->output_list);
4176 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004177 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004178 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004179 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004180 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004181 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004182
Xiong Zhang97116532013-10-23 13:58:31 +08004183 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004184 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004185
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004186 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4187 weston_config_section_get_string(s, "keymap_rules",
4188 (char **) &xkb_names.rules, NULL);
4189 weston_config_section_get_string(s, "keymap_model",
4190 (char **) &xkb_names.model, NULL);
4191 weston_config_section_get_string(s, "keymap_layout",
4192 (char **) &xkb_names.layout, NULL);
4193 weston_config_section_get_string(s, "keymap_variant",
4194 (char **) &xkb_names.variant, NULL);
4195 weston_config_section_get_string(s, "keymap_options",
4196 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004197
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004198 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4199 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004200
Jonny Lamb66a41a02014-08-12 14:58:25 +02004201 weston_config_section_get_int(s, "repeat-rate",
4202 &ec->kb_repeat_rate, 40);
4203 weston_config_section_get_int(s, "repeat-delay",
4204 &ec->kb_repeat_delay, 400);
4205
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004206 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004207
4208 wl_data_device_manager_init(ec->wl_display);
4209
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004210 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004211
Daniel Stone725c2c32012-06-22 14:04:36 +01004212 loop = wl_display_get_event_loop(ec->wl_display);
4213 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4214 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4215
4216 ec->input_loop = wl_event_loop_create();
4217
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004218 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4219 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4220
Pekka Paalanenb5026542014-11-12 15:09:24 +02004221 weston_compositor_add_debug_binding(ec, KEY_T,
4222 timeline_key_binding_handler, ec);
4223
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004224 weston_compositor_schedule_repaint(ec);
4225
Daniel Stone725c2c32012-06-22 14:04:36 +01004226 return 0;
4227}
4228
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004229WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004230weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004231{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004232 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004233
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004234 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004235 if (ec->input_loop_source)
4236 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004237
Matt Roper361d2ad2011-08-29 13:52:23 -07004238 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004239 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004240 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004241
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004242 if (ec->renderer)
4243 ec->renderer->destroy(ec);
4244
Daniel Stone325fc2d2012-05-30 16:31:58 +01004245 weston_binding_list_destroy_all(&ec->key_binding_list);
4246 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004247 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004248 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004249 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004250
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004251 weston_plane_release(&ec->primary_plane);
4252
Jonas Ådahlc97af922012-03-28 22:36:09 +02004253 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004254
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004255 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004256}
4257
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004258WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004259weston_compositor_exit_with_code(struct weston_compositor *compositor,
4260 int exit_code)
4261{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004262 if (compositor->exit_code == EXIT_SUCCESS)
4263 compositor->exit_code = exit_code;
4264
Frederic Plourdec336f062014-10-29 14:44:33 -04004265 wl_display_terminate(compositor->wl_display);
4266}
4267
4268WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004269weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4270 const struct weston_pointer_grab_interface *interface)
4271{
4272 struct weston_seat *seat;
4273
4274 ec->default_pointer_grab = interface;
4275 wl_list_for_each(seat, &ec->seat_list, link) {
4276 if (seat->pointer) {
4277 weston_pointer_set_default_grab(seat->pointer,
4278 interface);
4279 }
4280 }
4281}
4282
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004283WL_EXPORT int
4284weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4285 clockid_t clk_id)
4286{
4287 struct timespec ts;
4288
4289 if (clock_gettime(clk_id, &ts) < 0)
4290 return -1;
4291
4292 compositor->presentation_clock = clk_id;
4293
4294 return 0;
4295}
4296
4297/*
4298 * For choosing the software clock, when the display hardware or API
4299 * does not expose a compatible presentation timestamp.
4300 */
4301WL_EXPORT int
4302weston_compositor_set_presentation_clock_software(
4303 struct weston_compositor *compositor)
4304{
4305 /* In order of preference */
4306 static const clockid_t clocks[] = {
4307 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4308 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4309 CLOCK_MONOTONIC, /* no jumps, may crawl */
4310 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4311 CLOCK_REALTIME /* may jump and crawl */
4312 };
4313 unsigned i;
4314
4315 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4316 if (weston_compositor_set_presentation_clock(compositor,
4317 clocks[i]) == 0)
4318 return 0;
4319
4320 weston_log("Error: no suitable presentation clock available.\n");
4321
4322 return -1;
4323}
4324
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004325WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004326weston_version(int *major, int *minor, int *micro)
4327{
4328 *major = WESTON_VERSION_MAJOR;
4329 *minor = WESTON_VERSION_MINOR;
4330 *micro = WESTON_VERSION_MICRO;
4331}
4332
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004333static const char *
4334clock_name(clockid_t clk_id)
4335{
4336 static const char *names[] = {
4337 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4338 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4339 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4340 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4341 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4342 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4343 };
4344
4345 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4346 return "unknown";
4347
4348 return names[clk_id];
4349}
4350
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004351static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004352 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004353 const char *desc;
4354} capability_strings[] = {
4355 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004356 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004357};
4358
4359static void
4360weston_compositor_log_capabilities(struct weston_compositor *compositor)
4361{
4362 unsigned i;
4363 int yes;
4364
4365 weston_log("Compositor capabilities:\n");
4366 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4367 yes = compositor->capabilities & capability_strings[i].bit;
4368 weston_log_continue(STAMP_SPACE "%s %s\n",
4369 capability_strings[i].desc,
4370 yes ? "yes" : "no");
4371 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004372
4373 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4374 clock_name(compositor->presentation_clock),
4375 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004376}
4377
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004378static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004379{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004380 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004381
Martin Minarik6d118362012-06-07 18:01:59 +02004382 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004383 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004384
4385 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004386}
4387
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004388#ifdef HAVE_LIBUNWIND
4389
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004390static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004391print_backtrace(void)
4392{
4393 unw_cursor_t cursor;
4394 unw_context_t context;
4395 unw_word_t off;
4396 unw_proc_info_t pip;
4397 int ret, i = 0;
4398 char procname[256];
4399 const char *filename;
4400 Dl_info dlinfo;
4401
4402 pip.unwind_info = NULL;
4403 ret = unw_getcontext(&context);
4404 if (ret) {
4405 weston_log("unw_getcontext: %d\n", ret);
4406 return;
4407 }
4408
4409 ret = unw_init_local(&cursor, &context);
4410 if (ret) {
4411 weston_log("unw_init_local: %d\n", ret);
4412 return;
4413 }
4414
4415 ret = unw_step(&cursor);
4416 while (ret > 0) {
4417 ret = unw_get_proc_info(&cursor, &pip);
4418 if (ret) {
4419 weston_log("unw_get_proc_info: %d\n", ret);
4420 break;
4421 }
4422
4423 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4424 if (ret && ret != -UNW_ENOMEM) {
4425 if (ret != -UNW_EUNSPEC)
4426 weston_log("unw_get_proc_name: %d\n", ret);
4427 procname[0] = '?';
4428 procname[1] = 0;
4429 }
4430
4431 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4432 *dlinfo.dli_fname)
4433 filename = dlinfo.dli_fname;
4434 else
4435 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004436
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004437 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4438 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4439
4440 ret = unw_step(&cursor);
4441 if (ret < 0)
4442 weston_log("unw_step: %d\n", ret);
4443 }
4444}
4445
4446#else
4447
4448static void
4449print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004450{
4451 void *buffer[32];
4452 int i, count;
4453 Dl_info info;
4454
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004455 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4456 for (i = 0; i < count; i++) {
4457 dladdr(buffer[i], &info);
4458 weston_log(" [%016lx] %s (%s)\n",
4459 (long) buffer[i],
4460 info.dli_sname ? info.dli_sname : "--",
4461 info.dli_fname);
4462 }
4463}
4464
4465#endif
4466
4467static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004468on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004469{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004470 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004471 * then call the backend restore function, which will switch
4472 * back to the vt we launched from or ungrab X etc and then
4473 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004474 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004475 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004476 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004477
Peter Maatmane5b42e42013-03-27 22:38:53 +01004478 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004479
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004480 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004481
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004482 segv_compositor->restore(segv_compositor);
4483
4484 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004485}
4486
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004487WL_EXPORT void *
4488weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004489{
4490 char path[PATH_MAX];
4491 void *module, *init;
4492
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004493 if (name == NULL)
4494 return NULL;
4495
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004496 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004497 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004498 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004499 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004500
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004501 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4502 if (module) {
4503 weston_log("Module '%s' already loaded\n", path);
4504 dlclose(module);
4505 return NULL;
4506 }
4507
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004508 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004509 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004510 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004511 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004512 return NULL;
4513 }
4514
4515 init = dlsym(module, entrypoint);
4516 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004517 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004518 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004519 return NULL;
4520 }
4521
4522 return init;
4523}
4524
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004525static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004526load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004527 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004528{
4529 const char *p, *end;
4530 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004531 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004532 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004533
4534 if (modules == NULL)
4535 return 0;
4536
4537 p = modules;
4538 while (*p) {
4539 end = strchrnul(p, ',');
4540 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004541 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004542 if (!module_init)
4543 return -1;
4544 if (module_init(ec, argc, argv) < 0)
4545 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004546 p = end;
4547 while (*p == ',')
4548 p++;
4549
4550 }
4551
4552 return 0;
4553}
4554
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004555static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004556 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4557
4558static const char xdg_wrong_message[] =
4559 "fatal: environment variable XDG_RUNTIME_DIR\n"
4560 "is set to \"%s\", which is not a directory.\n";
4561
4562static const char xdg_wrong_mode_message[] =
4563 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004564 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4565 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004566
4567static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004568 "Refer to your distribution on how to get it, or\n"
4569 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4570 "on how to implement it.\n";
4571
Martin Minarik37032f82012-06-18 20:15:18 +02004572static void
4573verify_xdg_runtime_dir(void)
4574{
4575 char *dir = getenv("XDG_RUNTIME_DIR");
4576 struct stat s;
4577
4578 if (!dir) {
4579 weston_log(xdg_error_message);
4580 weston_log_continue(xdg_detail_message);
4581 exit(EXIT_FAILURE);
4582 }
4583
4584 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4585 weston_log(xdg_wrong_message, dir);
4586 weston_log_continue(xdg_detail_message);
4587 exit(EXIT_FAILURE);
4588 }
4589
4590 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4591 weston_log(xdg_wrong_mode_message,
4592 dir, s.st_mode & 0777, s.st_uid);
4593 weston_log_continue(xdg_detail_message);
4594 }
4595}
4596
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004597static int
4598usage(int error_code)
4599{
4600 fprintf(stderr,
4601 "Usage: weston [OPTIONS]\n\n"
4602 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4603 "Weston supports multiple backends, and depending on which backend is in use\n"
4604 "different options will be accepted.\n\n"
4605
4606
4607 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004608 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004609 " -B, --backend=MODULE\tBackend module, one of\n"
4610#if defined(BUILD_DRM_COMPOSITOR)
4611 "\t\t\t\tdrm-backend.so\n"
4612#endif
4613#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004614 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004615#endif
4616#if defined(BUILD_X11_COMPOSITOR)
4617 "\t\t\t\tx11-backend.so\n"
4618#endif
4619#if defined(BUILD_WAYLAND_COMPOSITOR)
4620 "\t\t\t\twayland-backend.so\n"
4621#endif
4622#if defined(BUILD_RDP_COMPOSITOR)
4623 "\t\t\t\trdp-backend.so\n"
4624#endif
4625#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4626 "\t\t\t\trpi-backend.so\n"
4627#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004628 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004629 " -S, --socket=NAME\tName of socket to listen on\n"
4630 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004631 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004632 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004633 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004634 " -h, --help\t\tThis help message\n\n");
4635
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004636#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004637 fprintf(stderr,
4638 "Options for drm-backend.so:\n\n"
4639 " --connector=ID\tBring up only this connector\n"
4640 " --seat=SEAT\t\tThe seat that weston should run on\n"
4641 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004642 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004643 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004644#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004645
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004646#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004647 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004648 "Options for fbdev-backend.so:\n\n"
4649 " --tty=TTY\t\tThe tty to use\n"
4650 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004651#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004652
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004653#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004654 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004655 "Options for x11-backend.so:\n\n"
4656 " --width=WIDTH\t\tWidth of X window\n"
4657 " --height=HEIGHT\tHeight of X window\n"
4658 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004659 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004660 " --output-count=COUNT\tCreate multiple outputs\n"
4661 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004662#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004663
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004664#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004665 fprintf(stderr,
4666 "Options for wayland-backend.so:\n\n"
4667 " --width=WIDTH\t\tWidth of Wayland surface\n"
4668 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004669 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004670 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004671 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004672 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004673 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004674 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004675#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004676
Pekka Paalanene31e0532013-05-22 18:03:07 +03004677#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4678 fprintf(stderr,
4679 "Options for rpi-backend.so:\n\n"
4680 " --tty=TTY\t\tThe tty to use\n"
4681 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4682 " --transform=TR\tThe output transformation, TR is one of:\n"
4683 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004684 " --opaque-regions\tEnable support for opaque regions, can be "
4685 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004686 "\n");
4687#endif
4688
Hardeningc077a842013-07-08 00:51:35 +02004689#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004690 fprintf(stderr,
4691 "Options for rdp-backend.so:\n\n"
4692 " --width=WIDTH\t\tWidth of desktop\n"
4693 " --height=HEIGHT\tHeight of desktop\n"
4694 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4695 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004696 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004697 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4698 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4699 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4700 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4701 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004702#endif
4703
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004704#if defined(BUILD_HEADLESS_COMPOSITOR)
4705 fprintf(stderr,
4706 "Options for headless-backend.so:\n\n"
4707 " --width=WIDTH\t\tWidth of memory surface\n"
4708 " --height=HEIGHT\tHeight of memory surface\n"
4709 " --transform=TR\tThe output transformation, TR is one of:\n"
4710 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4711 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4712#endif
4713
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004714 exit(error_code);
4715}
4716
Peter Maatmane5b42e42013-03-27 22:38:53 +01004717static void
4718catch_signals(void)
4719{
4720 struct sigaction action;
4721
4722 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4723 action.sa_sigaction = on_caught_signal;
4724 sigemptyset(&action.sa_mask);
4725 sigaction(SIGSEGV, &action, NULL);
4726 sigaction(SIGABRT, &action, NULL);
4727}
4728
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004729static void
4730handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4731{
4732 struct wl_client *client = data;
4733
4734 weston_log("Primary client died. Closing...\n");
4735
4736 wl_display_terminate(wl_client_get_display(client));
4737}
4738
Ryo Munakatad8deff62014-09-06 07:32:05 +09004739static char *
4740weston_choose_default_backend(void)
4741{
4742 char *backend = NULL;
4743
4744 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4745 backend = strdup("wayland-backend.so");
4746 else if (getenv("DISPLAY"))
4747 backend = strdup("x11-backend.so");
4748 else
4749 backend = strdup(WESTON_NATIVE_BACKEND);
4750
4751 return backend;
4752}
4753
4754static int
4755weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4756{
4757 if (socket_name) {
4758 if (wl_display_add_socket(display, socket_name)) {
4759 weston_log("fatal: failed to add socket: %m\n");
4760 return -1;
4761 }
4762 } else {
4763 socket_name = wl_display_add_socket_auto(display);
4764 if (!socket_name) {
4765 weston_log("fatal: failed to add socket: %m\n");
4766 return -1;
4767 }
4768 }
4769
4770 setenv("WAYLAND_DISPLAY", socket_name, 1);
4771
4772 return 0;
4773}
4774
Derek Foreman64a3df02014-10-23 12:24:18 -05004775static const struct { const char *name; uint32_t token; } transforms[] = {
4776 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4777 { "90", WL_OUTPUT_TRANSFORM_90 },
4778 { "180", WL_OUTPUT_TRANSFORM_180 },
4779 { "270", WL_OUTPUT_TRANSFORM_270 },
4780 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4781 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4782 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4783 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4784};
4785
4786WL_EXPORT int
4787weston_parse_transform(const char *transform, uint32_t *out)
4788{
4789 unsigned int i;
4790
4791 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4792 if (strcmp(transforms[i].name, transform) == 0) {
4793 *out = transforms[i].token;
4794 return 0;
4795 }
4796
4797 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4798 return -1;
4799}
4800
4801WL_EXPORT const char *
4802weston_transform_to_string(uint32_t output_transform)
4803{
4804 unsigned int i;
4805
4806 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4807 if (transforms[i].token == output_transform)
4808 return transforms[i].name;
4809
4810 return "<illegal value>";
4811}
4812
4813
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004814int main(int argc, char *argv[])
4815{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004816 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004817 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004818 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004819 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004820 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004821 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004822 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004823 int *argc, char *argv[],
4824 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004825 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004826 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004827 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004828 char *modules = NULL;
4829 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004830 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004831 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004832 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004833 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004834 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004835 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004836 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004837 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004838 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004839 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004840 struct wl_client *primary_client;
4841 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004842 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004843
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004844 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004845 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4846 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004847 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4848 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004849 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004850 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004851 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004852 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004853 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004854 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004855
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004856 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004857
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004858 if (help)
4859 usage(EXIT_SUCCESS);
4860
Scott Moreau12245142012-08-29 15:15:58 -06004861 if (version) {
4862 printf(PACKAGE_STRING "\n");
4863 return EXIT_SUCCESS;
4864 }
4865
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004866 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004867
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004868 weston_log("%s\n"
4869 STAMP_SPACE "%s\n"
4870 STAMP_SPACE "Bug reports to: %s\n"
4871 STAMP_SPACE "Build: %s\n",
4872 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004873 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004874 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004875
Martin Minarik37032f82012-06-18 20:15:18 +02004876 verify_xdg_runtime_dir();
4877
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004878 display = wl_display_create();
4879
Tiago Vignatti2116b892011-08-08 05:52:59 -07004880 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004881 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4882 display);
4883 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4884 display);
4885 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4886 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004887
4888 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004889 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4890 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004891
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304892 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4893 ret = EXIT_FAILURE;
4894 goto out_signals;
4895 }
4896
Pekka Paalanen588bee12014-05-07 16:26:25 +03004897 if (noconfig == 0)
4898 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004899 if (config != NULL) {
4900 weston_log("Using config file '%s'\n",
4901 weston_config_get_full_path(config));
4902 } else {
4903 weston_log("Starting with no config file.\n");
4904 }
4905 section = weston_config_get_section(config, "core", NULL, NULL);
4906
Ryo Munakata03faed22014-09-06 07:32:51 +09004907 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004908 weston_config_section_get_string(section, "backend", &backend,
4909 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004910 if (!backend)
4911 backend = weston_choose_default_backend();
4912 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004913
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004914 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304915 if (!backend_init) {
4916 ret = EXIT_FAILURE;
4917 goto out_signals;
4918 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004919
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004920 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004921 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004922 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304923 ret = EXIT_FAILURE;
4924 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004925 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004926
Peter Maatmane5b42e42013-03-27 22:38:53 +01004927 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004928 segv_compositor = ec;
4929
Frederic Plourde4a84c832014-10-30 15:06:34 -04004930 if (idle_time < 0)
4931 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4932 if (idle_time < 0)
4933 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004934 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004935 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004936 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004937
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004938 for (i = 1; i < argc; i++)
4939 weston_log("fatal: unhandled option: %s\n", argv[i]);
4940 if (argc > 1) {
4941 ret = EXIT_FAILURE;
4942 goto out;
4943 }
4944
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004945 weston_compositor_log_capabilities(ec);
4946
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004947 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4948 if (server_socket) {
4949 weston_log("Running with single client\n");
4950 fd = strtol(server_socket, &end, 0);
4951 if (*end != '\0')
4952 fd = -1;
4953 } else {
4954 fd = -1;
4955 }
4956
4957 if (fd != -1) {
4958 primary_client = wl_client_create(display, fd);
4959 if (!primary_client) {
4960 weston_log("fatal: failed to add client: %m\n");
4961 ret = EXIT_FAILURE;
4962 goto out;
4963 }
4964 primary_client_destroyed.notify =
4965 handle_primary_client_destroyed;
4966 wl_client_add_destroy_listener(primary_client,
4967 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004968 } else if (weston_create_listening_socket(display, socket_name)) {
4969 ret = EXIT_FAILURE;
4970 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004971 }
4972
Ryo Munakata03faed22014-09-06 07:32:51 +09004973 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004974 weston_config_section_get_string(section, "shell", &shell,
4975 "desktop-shell.so");
4976
Ryo Munakata03faed22014-09-06 07:32:51 +09004977 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004978 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004979
4980 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004981 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004982 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004983
4984 if (load_modules(ec, option_modules, &argc, argv) < 0)
4985 goto out;
4986
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004987 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4988 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4989 if (numlock_on) {
4990 wl_list_for_each(seat, &ec->seat_list, link) {
4991 if (seat->keyboard)
4992 weston_keyboard_set_locks(seat->keyboard,
4993 WESTON_NUM_LOCK,
4994 WESTON_NUM_LOCK);
4995 }
4996 }
4997
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004998 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004999
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04005000 wl_display_run(display);
5001
Frederic Plourdec336f062014-10-29 14:44:33 -04005002 /* Allow for setting return exit code after
5003 * wl_display_run returns normally. This is
5004 * useful for devs/testers and automated tests
5005 * that want to indicate failure status to
5006 * testing infrastructure above
5007 */
5008 ret = ec->exit_code;
5009
Ryo Munakata03faed22014-09-06 07:32:51 +09005010out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005011 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005012 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005013
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005014 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005015
Daniel Stone855028f2012-05-01 20:37:10 +01005016 weston_compositor_xkb_destroy(ec);
5017
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005018 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305019
5020out_signals:
5021 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5022 if (signals[i])
5023 wl_event_source_remove(signals[i]);
5024
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005025 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005026
Martin Minarik19e6f262012-06-07 13:08:46 +02005027 weston_log_file_close();
5028
Ryo Munakata03faed22014-09-06 07:32:51 +09005029 free(backend);
5030 free(shell);
5031 free(socket_name);
5032 free(option_modules);
5033 free(log);
5034 free(modules);
5035
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005036 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005037}