blob: 0cb140e718419d009f9592f919a3315631ba2437 [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);
446
447 view->alpha = 1.0;
448 pixman_region32_init(&view->transform.opaque);
449
450 wl_list_init(&view->geometry.transformation_list);
451 wl_list_insert(&view->geometry.transformation_list,
452 &view->transform.position.link);
453 weston_matrix_init(&view->transform.position.matrix);
454 wl_list_init(&view->geometry.child_list);
455 pixman_region32_init(&view->transform.boundingbox);
456 view->transform.dirty = 1;
457
Jason Ekstranda7af7042013-10-12 22:38:11 -0500458 return view;
459}
460
Jason Ekstrand108865d2014-06-26 10:04:49 -0700461struct weston_frame_callback {
462 struct wl_resource *resource;
463 struct wl_list link;
464};
465
Pekka Paalanen133e4392014-09-23 22:08:46 -0400466struct weston_presentation_feedback {
467 struct wl_resource *resource;
468
469 /* XXX: could use just wl_resource_get_link() instead */
470 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200471
472 /* The per-surface feedback flags */
473 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400474};
475
476static void
477weston_presentation_feedback_discard(
478 struct weston_presentation_feedback *feedback)
479{
480 presentation_feedback_send_discarded(feedback->resource);
481 wl_resource_destroy(feedback->resource);
482}
483
484static void
485weston_presentation_feedback_discard_list(struct wl_list *list)
486{
487 struct weston_presentation_feedback *feedback, *tmp;
488
489 wl_list_for_each_safe(feedback, tmp, list, link)
490 weston_presentation_feedback_discard(feedback);
491}
492
493static void
494weston_presentation_feedback_present(
495 struct weston_presentation_feedback *feedback,
496 struct weston_output *output,
497 uint32_t refresh_nsec,
498 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200499 uint64_t seq,
500 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400501{
502 struct wl_client *client = wl_resource_get_client(feedback->resource);
503 struct wl_resource *o;
504 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400505
506 wl_resource_for_each(o, &output->resource_list) {
507 if (wl_resource_get_client(o) != client)
508 continue;
509
510 presentation_feedback_send_sync_output(feedback->resource, o);
511 }
512
513 secs = ts->tv_sec;
514 presentation_feedback_send_presented(feedback->resource,
515 secs >> 32, secs & 0xffffffff,
516 ts->tv_nsec,
517 refresh_nsec,
518 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200519 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400520 wl_resource_destroy(feedback->resource);
521}
522
523static void
524weston_presentation_feedback_present_list(struct wl_list *list,
525 struct weston_output *output,
526 uint32_t refresh_nsec,
527 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200528 uint64_t seq,
529 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400530{
531 struct weston_presentation_feedback *feedback, *tmp;
532
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200533 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
534 wl_list_empty(list));
535
Pekka Paalanen133e4392014-09-23 22:08:46 -0400536 wl_list_for_each_safe(feedback, tmp, list, link)
537 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200538 refresh_nsec, ts, seq,
539 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400540}
541
Jason Ekstrand7b982072014-05-20 14:33:03 -0500542static void
543surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
544{
545 struct weston_surface_state *state =
546 container_of(listener, struct weston_surface_state,
547 buffer_destroy_listener);
548
549 state->buffer = NULL;
550}
551
552static void
553weston_surface_state_init(struct weston_surface_state *state)
554{
555 state->newly_attached = 0;
556 state->buffer = NULL;
557 state->buffer_destroy_listener.notify =
558 surface_state_handle_buffer_destroy;
559 state->sx = 0;
560 state->sy = 0;
561
562 pixman_region32_init(&state->damage);
563 pixman_region32_init(&state->opaque);
564 region_init_infinite(&state->input);
565
566 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400567 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500568
569 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
570 state->buffer_viewport.buffer.scale = 1;
571 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
572 state->buffer_viewport.surface.width = -1;
573 state->buffer_viewport.changed = 0;
574}
575
576static void
577weston_surface_state_fini(struct weston_surface_state *state)
578{
579 struct weston_frame_callback *cb, *next;
580
581 wl_list_for_each_safe(cb, next,
582 &state->frame_callback_list, link)
583 wl_resource_destroy(cb->resource);
584
Pekka Paalanen133e4392014-09-23 22:08:46 -0400585 weston_presentation_feedback_discard_list(&state->feedback_list);
586
Jason Ekstrand7b982072014-05-20 14:33:03 -0500587 pixman_region32_fini(&state->input);
588 pixman_region32_fini(&state->opaque);
589 pixman_region32_fini(&state->damage);
590
591 if (state->buffer)
592 wl_list_remove(&state->buffer_destroy_listener.link);
593 state->buffer = NULL;
594}
595
596static void
597weston_surface_state_set_buffer(struct weston_surface_state *state,
598 struct weston_buffer *buffer)
599{
600 if (state->buffer == buffer)
601 return;
602
603 if (state->buffer)
604 wl_list_remove(&state->buffer_destroy_listener.link);
605 state->buffer = buffer;
606 if (state->buffer)
607 wl_signal_add(&state->buffer->destroy_signal,
608 &state->buffer_destroy_listener);
609}
610
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500611WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500612weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500613{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500614 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615
Bryce Harringtonde16d892014-11-20 22:21:57 -0800616 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400617 if (surface == NULL)
618 return NULL;
619
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500620 wl_signal_init(&surface->destroy_signal);
621
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500622 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200623 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400624
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200625 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
626 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200627 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
628 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500629
630 weston_surface_state_init(&surface->pending);
631
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400632 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500633 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300634 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400635
Jason Ekstranda7af7042013-10-12 22:38:11 -0500636 wl_list_init(&surface->views);
637
638 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400639 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500640
Pekka Paalanene67858b2013-04-25 13:57:42 +0300641 wl_list_init(&surface->subsurface_list);
642 wl_list_init(&surface->subsurface_list_pending);
643
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400644 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500645}
646
Alex Wu8811bf92012-02-28 18:07:54 +0800647WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500648weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200649 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500650{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100651 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652}
653
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400654WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500655weston_view_to_global_float(struct weston_view *view,
656 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200657{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500658 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200659 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
660
Jason Ekstranda7af7042013-10-12 22:38:11 -0500661 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200662
663 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200664 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700665 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200666 v.f[3]);
667 *x = 0;
668 *y = 0;
669 return;
670 }
671
672 *x = v.f[0] / v.f[3];
673 *y = v.f[1] / v.f[3];
674 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675 *x = sx + view->geometry.x;
676 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200677 }
678}
679
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500680WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200681weston_transformed_coord(int width, int height,
682 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200683 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200684 float sx, float sy, float *bx, float *by)
685{
686 switch (transform) {
687 case WL_OUTPUT_TRANSFORM_NORMAL:
688 default:
689 *bx = sx;
690 *by = sy;
691 break;
692 case WL_OUTPUT_TRANSFORM_FLIPPED:
693 *bx = width - sx;
694 *by = sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_90:
697 *bx = height - sy;
698 *by = sx;
699 break;
700 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
701 *bx = height - sy;
702 *by = width - sx;
703 break;
704 case WL_OUTPUT_TRANSFORM_180:
705 *bx = width - sx;
706 *by = height - sy;
707 break;
708 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
709 *bx = sx;
710 *by = height - sy;
711 break;
712 case WL_OUTPUT_TRANSFORM_270:
713 *bx = sy;
714 *by = width - sx;
715 break;
716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
717 *bx = sy;
718 *by = sx;
719 break;
720 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200721
722 *bx *= scale;
723 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200724}
725
726WL_EXPORT pixman_box32_t
727weston_transformed_rect(int width, int height,
728 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200729 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200730 pixman_box32_t rect)
731{
732 float x1, x2, y1, y2;
733
734 pixman_box32_t ret;
735
Alexander Larsson4ea95522013-05-22 14:41:37 +0200736 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200737 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200738 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200739 rect.x2, rect.y2, &x2, &y2);
740
741 if (x1 <= x2) {
742 ret.x1 = x1;
743 ret.x2 = x2;
744 } else {
745 ret.x1 = x2;
746 ret.x2 = x1;
747 }
748
749 if (y1 <= y2) {
750 ret.y1 = y1;
751 ret.y2 = y2;
752 } else {
753 ret.y1 = y2;
754 ret.y2 = y1;
755 }
756
757 return ret;
758}
759
760WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500761weston_transformed_region(int width, int height,
762 enum wl_output_transform transform,
763 int32_t scale,
764 pixman_region32_t *src, pixman_region32_t *dest)
765{
766 pixman_box32_t *src_rects, *dest_rects;
767 int nrects, i;
768
769 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
770 if (src != dest)
771 pixman_region32_copy(dest, src);
772 return;
773 }
774
775 src_rects = pixman_region32_rectangles(src, &nrects);
776 dest_rects = malloc(nrects * sizeof(*dest_rects));
777 if (!dest_rects)
778 return;
779
780 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
781 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
782 } else {
783 for (i = 0; i < nrects; i++) {
784 switch (transform) {
785 default:
786 case WL_OUTPUT_TRANSFORM_NORMAL:
787 dest_rects[i].x1 = src_rects[i].x1;
788 dest_rects[i].y1 = src_rects[i].y1;
789 dest_rects[i].x2 = src_rects[i].x2;
790 dest_rects[i].y2 = src_rects[i].y2;
791 break;
792 case WL_OUTPUT_TRANSFORM_90:
793 dest_rects[i].x1 = height - src_rects[i].y2;
794 dest_rects[i].y1 = src_rects[i].x1;
795 dest_rects[i].x2 = height - src_rects[i].y1;
796 dest_rects[i].y2 = src_rects[i].x2;
797 break;
798 case WL_OUTPUT_TRANSFORM_180:
799 dest_rects[i].x1 = width - src_rects[i].x2;
800 dest_rects[i].y1 = height - src_rects[i].y2;
801 dest_rects[i].x2 = width - src_rects[i].x1;
802 dest_rects[i].y2 = height - src_rects[i].y1;
803 break;
804 case WL_OUTPUT_TRANSFORM_270:
805 dest_rects[i].x1 = src_rects[i].y1;
806 dest_rects[i].y1 = width - src_rects[i].x2;
807 dest_rects[i].x2 = src_rects[i].y2;
808 dest_rects[i].y2 = width - src_rects[i].x1;
809 break;
810 case WL_OUTPUT_TRANSFORM_FLIPPED:
811 dest_rects[i].x1 = width - src_rects[i].x2;
812 dest_rects[i].y1 = src_rects[i].y1;
813 dest_rects[i].x2 = width - src_rects[i].x1;
814 dest_rects[i].y2 = src_rects[i].y2;
815 break;
816 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
817 dest_rects[i].x1 = height - src_rects[i].y2;
818 dest_rects[i].y1 = width - src_rects[i].x2;
819 dest_rects[i].x2 = height - src_rects[i].y1;
820 dest_rects[i].y2 = width - src_rects[i].x1;
821 break;
822 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
823 dest_rects[i].x1 = src_rects[i].x1;
824 dest_rects[i].y1 = height - src_rects[i].y2;
825 dest_rects[i].x2 = src_rects[i].x2;
826 dest_rects[i].y2 = height - src_rects[i].y1;
827 break;
828 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
829 dest_rects[i].x1 = src_rects[i].y1;
830 dest_rects[i].y1 = src_rects[i].x1;
831 dest_rects[i].x2 = src_rects[i].y2;
832 dest_rects[i].y2 = src_rects[i].x2;
833 break;
834 }
835 }
836 }
837
838 if (scale != 1) {
839 for (i = 0; i < nrects; i++) {
840 dest_rects[i].x1 *= scale;
841 dest_rects[i].x2 *= scale;
842 dest_rects[i].y1 *= scale;
843 dest_rects[i].y2 *= scale;
844 }
845 }
846
847 pixman_region32_clear(dest);
848 pixman_region32_init_rects(dest, dest_rects, nrects);
849 free(dest_rects);
850}
851
Jonny Lamb74130762013-11-26 18:19:46 +0100852static void
853scaler_surface_to_buffer(struct weston_surface *surface,
854 float sx, float sy, float *bx, float *by)
855{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200856 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200857 double src_width, src_height;
858 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200859
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200860 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
861 if (vp->surface.width == -1) {
862 *bx = sx;
863 *by = sy;
864 return;
865 }
Jonny Lamb74130762013-11-26 18:19:46 +0100866
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200867 src_x = 0.0;
868 src_y = 0.0;
869 src_width = surface->width_from_buffer;
870 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100871 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200872 src_x = wl_fixed_to_double(vp->buffer.src_x);
873 src_y = wl_fixed_to_double(vp->buffer.src_y);
874 src_width = wl_fixed_to_double(vp->buffer.src_width);
875 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100876 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200877
878 *bx = sx * src_width / surface->width + src_x;
879 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100880}
881
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500882WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200883weston_surface_to_buffer_float(struct weston_surface *surface,
884 float sx, float sy, float *bx, float *by)
885{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200886 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
887
Jonny Lamb74130762013-11-26 18:19:46 +0100888 /* first transform coordinates if the scaler is set */
889 scaler_surface_to_buffer(surface, sx, sy, bx, by);
890
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500891 weston_transformed_coord(surface->width_from_buffer,
892 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200893 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100894 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200895}
896
Alexander Larsson4ea95522013-05-22 14:41:37 +0200897WL_EXPORT void
898weston_surface_to_buffer(struct weston_surface *surface,
899 int sx, int sy, int *bx, int *by)
900{
901 float bxf, byf;
902
Jonny Lamb74130762013-11-26 18:19:46 +0100903 weston_surface_to_buffer_float(surface,
904 sx, sy, &bxf, &byf);
905
Alexander Larsson4ea95522013-05-22 14:41:37 +0200906 *bx = floorf(bxf);
907 *by = floorf(byf);
908}
909
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200910WL_EXPORT pixman_box32_t
911weston_surface_to_buffer_rect(struct weston_surface *surface,
912 pixman_box32_t rect)
913{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200914 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100915 float xf, yf;
916
917 /* first transform box coordinates if the scaler is set */
918 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
919 rect.x1 = floorf(xf);
920 rect.y1 = floorf(yf);
921
922 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
923 rect.x2 = floorf(xf);
924 rect.y2 = floorf(yf);
925
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500926 return weston_transformed_rect(surface->width_from_buffer,
927 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200928 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200929 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200930}
931
932WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400934 struct weston_plane *plane)
935{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400937 return;
938
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 weston_view_damage_below(view);
940 view->plane = plane;
941 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400942}
943
Pekka Paalanen51723d52015-02-17 13:10:01 +0200944/** Inflict damage on the plane where the view is visible.
945 *
946 * \param view The view that causes the damage.
947 *
948 * If the view is currently on a plane (including the primary plane),
949 * take the view's boundingbox, subtract all the opaque views that cover it,
950 * and add the remaining region as damage to the plane. This corresponds
951 * to the damage inflicted to the plane if this view disappeared.
952 *
953 * A repaint is scheduled for this view.
954 *
955 * The region of all opaque views covering this view is stored in
956 * weston_view::clip and updated by view_accumulate_damage() during
957 * weston_output_repaint(). Specifically, that region matches the
958 * scenegraph as it was last painted.
959 */
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400960WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200962{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500963 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200964
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500965 pixman_region32_init(&damage);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +0200966 pixman_region32_subtract(&damage, &view->transform.boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800968 if (view->plane)
969 pixman_region32_union(&view->plane->damage,
970 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500971 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800972 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200973}
974
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400975static void
976weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
977{
978 uint32_t different = es->output_mask ^ mask;
979 uint32_t entered = mask & different;
980 uint32_t left = es->output_mask & different;
981 struct weston_output *output;
982 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500983 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400984
985 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500986 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400987 return;
988 if (different == 0)
989 return;
990
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500991 client = wl_resource_get_client(es->resource);
992
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400993 wl_list_for_each(output, &es->compositor->output_list, link) {
994 if (1 << output->id & different)
995 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500996 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400997 client);
998 if (resource == NULL)
999 continue;
1000 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001001 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001002 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001003 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001004 }
1005}
1006
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001007
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001008static void
1009weston_surface_assign_output(struct weston_surface *es)
1010{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 struct weston_output *new_output;
1012 struct weston_view *view;
1013 pixman_region32_t region;
1014 uint32_t max, area, mask;
1015 pixman_box32_t *e;
1016
1017 new_output = NULL;
1018 max = 0;
1019 mask = 0;
1020 pixman_region32_init(&region);
1021 wl_list_for_each(view, &es->views, surface_link) {
1022 if (!view->output)
1023 continue;
1024
1025 pixman_region32_intersect(&region, &view->transform.boundingbox,
1026 &view->output->region);
1027
1028 e = pixman_region32_extents(&region);
1029 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1030
1031 mask |= view->output_mask;
1032
1033 if (area >= max) {
1034 new_output = view->output;
1035 max = area;
1036 }
1037 }
1038 pixman_region32_fini(&region);
1039
1040 es->output = new_output;
1041 weston_surface_update_output_mask(es, mask);
1042}
1043
1044static void
1045weston_view_assign_output(struct weston_view *ev)
1046{
1047 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001048 struct weston_output *output, *new_output;
1049 pixman_region32_t region;
1050 uint32_t max, area, mask;
1051 pixman_box32_t *e;
1052
1053 new_output = NULL;
1054 max = 0;
1055 mask = 0;
1056 pixman_region32_init(&region);
1057 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001059 &output->region);
1060
1061 e = pixman_region32_extents(&region);
1062 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1063
1064 if (area > 0)
1065 mask |= 1 << output->id;
1066
1067 if (area >= max) {
1068 new_output = output;
1069 max = area;
1070 }
1071 }
1072 pixman_region32_fini(&region);
1073
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 ev->output = new_output;
1075 ev->output_mask = mask;
1076
1077 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001078}
1079
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001080static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1082 int32_t width, int32_t height,
1083 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001084{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001085 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1086 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001087 int32_t s[4][2] = {
1088 { sx, sy },
1089 { sx, sy + height },
1090 { sx + width, sy },
1091 { sx + width, sy + height }
1092 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001093 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001094 int i;
1095
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001096 if (width == 0 || height == 0) {
1097 /* avoid rounding empty bbox to 1x1 */
1098 pixman_region32_init(bbox);
1099 return;
1100 }
1101
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001102 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001103 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001105 if (x < min_x)
1106 min_x = x;
1107 if (x > max_x)
1108 max_x = x;
1109 if (y < min_y)
1110 min_y = y;
1111 if (y > max_y)
1112 max_y = y;
1113 }
1114
Pekka Paalanen219b9822012-02-08 15:38:37 +02001115 int_x = floorf(min_x);
1116 int_y = floorf(min_y);
1117 pixman_region32_init_rect(bbox, int_x, int_y,
1118 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001119}
1120
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001121static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001123{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001125
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001126 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 view->geometry.x = roundf(view->geometry.x);
1128 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001129
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001130 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1132 view->transform.position.matrix.d[12] = view->geometry.x;
1133 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001136
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 view->transform.inverse = view->transform.position.matrix;
1138 view->transform.inverse.d[12] = -view->geometry.x;
1139 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 pixman_region32_init_rect(&view->transform.boundingbox,
1142 view->geometry.x,
1143 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001144 view->surface->width,
1145 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 if (view->alpha == 1.0) {
1148 pixman_region32_copy(&view->transform.opaque,
1149 &view->surface->opaque);
1150 pixman_region32_translate(&view->transform.opaque,
1151 view->geometry.x,
1152 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001153 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001154}
1155
1156static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001158{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 struct weston_view *parent = view->geometry.parent;
1160 struct weston_matrix *matrix = &view->transform.matrix;
1161 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001162 struct weston_transform *tform;
1163
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001165
1166 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001167 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1168 view->transform.position.matrix.d[12] = view->geometry.x;
1169 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001170
1171 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001173 weston_matrix_multiply(matrix, &tform->matrix);
1174
Pekka Paalanen483243f2013-03-08 14:56:50 +02001175 if (parent)
1176 weston_matrix_multiply(matrix, &parent->transform.matrix);
1177
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001178 if (weston_matrix_invert(inverse, matrix) < 0) {
1179 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 weston_log("error: weston_view %p"
1181 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001182 return -1;
1183 }
1184
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001185 view_compute_bbox(view, 0, 0,
1186 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001188
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001189 return 0;
1190}
1191
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001192static struct weston_layer *
1193get_view_layer(struct weston_view *view)
1194{
1195 if (view->parent_view)
1196 return get_view_layer(view->parent_view);
1197 return view->layer_link.layer;
1198}
1199
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001200WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001202{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001204 struct weston_layer *layer;
1205 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001208 return;
1209
Pekka Paalanen483243f2013-03-08 14:56:50 +02001210 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001212
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001214
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001216
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 pixman_region32_fini(&view->transform.boundingbox);
1218 pixman_region32_fini(&view->transform.opaque);
1219 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001220
Pekka Paalanencd403622012-01-25 13:37:39 +02001221 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001222 if (view->geometry.transformation_list.next ==
1223 &view->transform.position.link &&
1224 view->geometry.transformation_list.prev ==
1225 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001226 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001228 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 if (weston_view_update_transform_enable(view) < 0)
1230 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001231 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001232
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001233 layer = get_view_layer(view);
1234 if (layer) {
1235 pixman_region32_init_with_extents(&mask, &layer->mask);
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001236 pixman_region32_intersect(&view->transform.boundingbox,
1237 &view->transform.boundingbox, &mask);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001238 pixman_region32_intersect(&view->transform.opaque,
1239 &view->transform.opaque, &mask);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001240 pixman_region32_fini(&mask);
1241 }
1242
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_signal_emit(&view->surface->compositor->transform_signal,
1248 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001249}
1250
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001251WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001253{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255
1256 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 * The invariant: if view->geometry.dirty, then all views
1258 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001259 * Corollary: if not parent->geometry.dirty, then all ancestors
1260 * are not dirty.
1261 */
1262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001264 return;
1265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001269 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001271}
1272
1273WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274weston_view_to_global_fixed(struct weston_view *view,
1275 wl_fixed_t vx, wl_fixed_t vy,
1276 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001277{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001278 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 weston_view_to_global_float(view,
1281 wl_fixed_to_double(vx),
1282 wl_fixed_to_double(vy),
1283 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001284 *x = wl_fixed_from_double(xf);
1285 *y = wl_fixed_from_double(yf);
1286}
1287
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001288WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289weston_view_from_global_float(struct weston_view *view,
1290 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001291{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001293 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001296
1297 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001298 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001300 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 *vx = 0;
1302 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001303 return;
1304 }
1305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 *vx = v.f[0] / v.f[3];
1307 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001308 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 *vx = x - view->geometry.x;
1310 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001311 }
1312}
1313
1314WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315weston_view_from_global_fixed(struct weston_view *view,
1316 wl_fixed_t x, wl_fixed_t y,
1317 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001318{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_from_global_float(view,
1322 wl_fixed_to_double(x),
1323 wl_fixed_to_double(y),
1324 &vxf, &vyf);
1325 *vx = wl_fixed_from_double(vxf);
1326 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001327}
1328
1329WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330weston_view_from_global(struct weston_view *view,
1331 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001332{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001334
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1336 *vx = floorf(vxf);
1337 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001338}
1339
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001340WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001341weston_surface_schedule_repaint(struct weston_surface *surface)
1342{
1343 struct weston_output *output;
1344
1345 wl_list_for_each(output, &surface->compositor->output_list, link)
1346 if (surface->output_mask & (1 << output->id))
1347 weston_output_schedule_repaint(output);
1348}
1349
1350WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351weston_view_schedule_repaint(struct weston_view *view)
1352{
1353 struct weston_output *output;
1354
1355 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1356 if (view->output_mask & (1 << output->id))
1357 weston_output_schedule_repaint(output);
1358}
1359
1360WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001361weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001362{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001363 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 0, 0, surface->width,
1365 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001366
Kristian Høgsberg98238702012-08-03 16:29:12 -04001367 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001368}
1369
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001370WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001372{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001373 if (view->geometry.x == x && view->geometry.y == y)
1374 return;
1375
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376 view->geometry.x = x;
1377 view->geometry.y = y;
1378 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001379}
1380
Pekka Paalanen483243f2013-03-08 14:56:50 +02001381static void
1382transform_parent_handle_parent_destroy(struct wl_listener *listener,
1383 void *data)
1384{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 struct weston_view *view =
1386 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001387 geometry.parent_destroy_listener);
1388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001390}
1391
1392WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393weston_view_set_transform_parent(struct weston_view *view,
1394 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001395{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 if (view->geometry.parent) {
1397 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1398 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001399 }
1400
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001402
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001404 transform_parent_handle_parent_destroy;
1405 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001406 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001407 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001408 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001409 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001410 }
1411
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412 weston_view_geometry_dirty(view);
1413}
1414
Derek Foreman280e7dd2014-10-03 13:13:42 -05001415WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416weston_view_is_mapped(struct weston_view *view)
1417{
1418 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001419 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001421 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001422}
1423
Derek Foreman280e7dd2014-10-03 13:13:42 -05001424WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001425weston_surface_is_mapped(struct weston_surface *surface)
1426{
1427 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001428 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001429 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001430 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001431}
1432
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001433static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001434surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001435{
1436 struct weston_view *view;
1437
1438 if (surface->width == width && surface->height == height)
1439 return;
1440
1441 surface->width = width;
1442 surface->height = height;
1443
1444 wl_list_for_each(view, &surface->views, surface_link)
1445 weston_view_geometry_dirty(view);
1446}
1447
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001448WL_EXPORT void
1449weston_surface_set_size(struct weston_surface *surface,
1450 int32_t width, int32_t height)
1451{
1452 assert(!surface->resource);
1453 surface_set_size(surface, width, height);
1454}
1455
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001456static int
1457fixed_round_up_to_int(wl_fixed_t f)
1458{
1459 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1460}
1461
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001462static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001463weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001464{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001465 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001466 int32_t width, height;
1467
1468 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001469 surface->width_from_buffer = 0;
1470 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001471 return;
1472 }
1473
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001474 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001475 case WL_OUTPUT_TRANSFORM_90:
1476 case WL_OUTPUT_TRANSFORM_270:
1477 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1478 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001479 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1480 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001481 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001482 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001483 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1484 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001485 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001486 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001487
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001488 surface->width_from_buffer = width;
1489 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001490}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001491
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001492static void
1493weston_surface_update_size(struct weston_surface *surface)
1494{
1495 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1496 int32_t width, height;
1497
1498 width = surface->width_from_buffer;
1499 height = surface->height_from_buffer;
1500
1501 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001502 surface_set_size(surface,
1503 vp->surface.width, vp->surface.height);
1504 return;
1505 }
1506
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001507 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001508 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1509 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1510
1511 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001512 return;
1513 }
1514
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001515 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001516}
1517
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001518WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001519weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001520{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001521 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001522
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001523 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001524
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001525 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001526}
1527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528WL_EXPORT struct weston_view *
1529weston_compositor_pick_view(struct weston_compositor *compositor,
1530 wl_fixed_t x, wl_fixed_t y,
1531 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001532{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001534 int ix = wl_fixed_to_int(x);
1535 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001536
Jason Ekstranda7af7042013-10-12 22:38:11 -05001537 wl_list_for_each(view, &compositor->view_list, link) {
1538 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001539 if (pixman_region32_contains_point(
Pekka Paalanen25c0ca52015-02-19 11:15:33 +02001540 &view->transform.boundingbox, ix, iy, NULL) &&
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001541 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 wl_fixed_to_int(*vx),
1543 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001544 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001546 }
1547
1548 return NULL;
1549}
1550
1551static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001552weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001553{
Daniel Stone37816df2012-05-16 18:45:18 +01001554 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001555
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001556 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001557 return;
1558
Daniel Stone37816df2012-05-16 18:45:18 +01001559 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001560 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001561}
1562
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001563WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001565{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001566 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001567
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 if (!weston_view_is_mapped(view))
1569 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001570
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571 weston_view_damage_below(view);
1572 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001573 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001574 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 wl_list_remove(&view->link);
1576 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 view->output_mask = 0;
1578 weston_surface_assign_output(view->surface);
1579
1580 if (weston_surface_is_mapped(view->surface))
1581 return;
1582
1583 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1584 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001585 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001587 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001588 NULL,
1589 wl_fixed_from_int(0),
1590 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001592 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001593 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001595
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596WL_EXPORT void
1597weston_surface_unmap(struct weston_surface *surface)
1598{
1599 struct weston_view *view;
1600
1601 wl_list_for_each(view, &surface->views, surface_link)
1602 weston_view_unmap(view);
1603 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001604}
1605
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001606static void
1607weston_surface_reset_pending_buffer(struct weston_surface *surface)
1608{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001609 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001610 surface->pending.sx = 0;
1611 surface->pending.sy = 0;
1612 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001613 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001614}
1615
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616WL_EXPORT void
1617weston_view_destroy(struct weston_view *view)
1618{
1619 wl_signal_emit(&view->destroy_signal, view);
1620
1621 assert(wl_list_empty(&view->geometry.child_list));
1622
1623 if (weston_view_is_mapped(view)) {
1624 weston_view_unmap(view);
1625 weston_compositor_build_view_list(view->surface->compositor);
1626 }
1627
1628 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001629 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630
1631 pixman_region32_fini(&view->clip);
1632 pixman_region32_fini(&view->transform.boundingbox);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001633 pixman_region32_fini(&view->transform.opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634
1635 weston_view_set_transform_parent(view, NULL);
1636
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637 wl_list_remove(&view->surface_link);
1638
1639 free(view);
1640}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001641
1642WL_EXPORT void
1643weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001644{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001645 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001647
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001648 if (--surface->ref_count > 0)
1649 return;
1650
1651 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1652
Pekka Paalanene67858b2013-04-25 13:57:42 +03001653 assert(wl_list_empty(&surface->subsurface_list_pending));
1654 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001655
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1657 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001658
Jason Ekstrand7b982072014-05-20 14:33:03 -05001659 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001660
Pekka Paalanende685b82012-12-04 15:58:12 +02001661 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001662
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001663 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001664 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001665 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001666
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001667 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001668 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001669
Pekka Paalanen133e4392014-09-23 22:08:46 -04001670 weston_presentation_feedback_discard_list(&surface->feedback_list);
1671
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001672 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001673}
1674
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001675static void
1676destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001677{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001678 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001679
Giulio Camuffo0d379742013-11-15 22:06:15 +01001680 /* Set the resource to NULL, since we don't want to leave a
1681 * dangling pointer if the surface was refcounted and survives
1682 * the weston_surface_destroy() call. */
1683 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001684 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001685}
1686
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001687static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001688weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1689{
1690 struct weston_buffer *buffer =
1691 container_of(listener, struct weston_buffer, destroy_listener);
1692
1693 wl_signal_emit(&buffer->destroy_signal, buffer);
1694 free(buffer);
1695}
1696
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001697WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001698weston_buffer_from_resource(struct wl_resource *resource)
1699{
1700 struct weston_buffer *buffer;
1701 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001702
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001703 listener = wl_resource_get_destroy_listener(resource,
1704 weston_buffer_destroy_handler);
1705
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001706 if (listener)
1707 return container_of(listener, struct weston_buffer,
1708 destroy_listener);
1709
1710 buffer = zalloc(sizeof *buffer);
1711 if (buffer == NULL)
1712 return NULL;
1713
1714 buffer->resource = resource;
1715 wl_signal_init(&buffer->destroy_signal);
1716 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001717 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001718 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001719
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001720 return buffer;
1721}
1722
1723static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001724weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1725 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001726{
Pekka Paalanende685b82012-12-04 15:58:12 +02001727 struct weston_buffer_reference *ref =
1728 container_of(listener, struct weston_buffer_reference,
1729 destroy_listener);
1730
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001731 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001732 ref->buffer = NULL;
1733}
1734
1735WL_EXPORT void
1736weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001737 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001738{
1739 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001740 ref->buffer->busy_count--;
1741 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001742 assert(wl_resource_get_client(ref->buffer->resource));
1743 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001744 WL_BUFFER_RELEASE);
1745 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001746 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001747 }
1748
Pekka Paalanende685b82012-12-04 15:58:12 +02001749 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001750 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001751 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001752 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001753 }
1754
Pekka Paalanende685b82012-12-04 15:58:12 +02001755 ref->buffer = buffer;
1756 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1757}
1758
1759static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001760weston_surface_attach(struct weston_surface *surface,
1761 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001762{
1763 weston_buffer_reference(&surface->buffer_ref, buffer);
1764
Pekka Paalanena6421c42012-12-04 15:58:10 +02001765 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001766 if (weston_surface_is_mapped(surface))
1767 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001768 }
1769
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001770 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001771
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001772 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001773 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001774}
1775
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001776WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001777weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001778{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001779 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001780
1781 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001783}
1784
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001785WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001786weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001787{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001788 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001789
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001790 pixman_region32_union(&compositor->primary_plane.damage,
1791 &compositor->primary_plane.damage,
1792 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001793 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001794}
1795
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001796static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001798{
Pekka Paalanende685b82012-12-04 15:58:12 +02001799 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001800 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001801 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001802
Pekka Paalanenb5026542014-11-12 15:09:24 +02001803 if (weston_timeline_enabled_ &&
1804 pixman_region32_not_empty(&surface->damage))
1805 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1806 TLP_OUTPUT(surface->output), TLP_END);
1807
Jason Ekstrandef540082014-06-26 10:37:36 -07001808 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809}
1810
1811static void
1812view_accumulate_damage(struct weston_view *view,
1813 pixman_region32_t *opaque)
1814{
1815 pixman_region32_t damage;
1816
1817 pixman_region32_init(&damage);
1818 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001819 pixman_box32_t *extents;
1820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 extents = pixman_region32_extents(&view->surface->damage);
1822 view_compute_bbox(view, extents->x1, extents->y1,
1823 extents->x2 - extents->x1,
1824 extents->y2 - extents->y1,
1825 &damage);
1826 pixman_region32_translate(&damage,
1827 -view->plane->x,
1828 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001829 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 pixman_region32_copy(&damage, &view->surface->damage);
1831 pixman_region32_translate(&damage,
1832 view->geometry.x - view->plane->x,
1833 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001834 }
1835
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 pixman_region32_subtract(&damage, &damage, opaque);
1837 pixman_region32_union(&view->plane->damage,
1838 &view->plane->damage, &damage);
1839 pixman_region32_fini(&damage);
1840 pixman_region32_copy(&view->clip, opaque);
Pekka Paalanen8844bf22015-02-18 16:30:47 +02001841 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001842}
1843
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001844static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001845compositor_accumulate_damage(struct weston_compositor *ec)
1846{
1847 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001848 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001849 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001850
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001851 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001852
1853 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001854 pixman_region32_copy(&plane->clip, &clip);
1855
1856 pixman_region32_init(&opaque);
1857
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 wl_list_for_each(ev, &ec->view_list, link) {
1859 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001860 continue;
1861
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001863 }
1864
1865 pixman_region32_union(&clip, &clip, &opaque);
1866 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001867 }
1868
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001869 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001870
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 wl_list_for_each(ev, &ec->view_list, link)
1872 ev->surface->touched = 0;
1873
1874 wl_list_for_each(ev, &ec->view_list, link) {
1875 if (ev->surface->touched)
1876 continue;
1877 ev->surface->touched = 1;
1878
1879 surface_flush_damage(ev->surface);
1880
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001881 /* Both the renderer and the backend have seen the buffer
1882 * by now. If renderer needs the buffer, it has its own
1883 * reference set. If the backend wants to keep the buffer
1884 * around for migrating the surface into a non-primary plane
1885 * later, keep_buffer is true. Otherwise, drop the core
1886 * reference now, and allow early buffer release. This enables
1887 * clients to use single-buffering.
1888 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889 if (!ev->surface->keep_buffer)
1890 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001891 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001892}
1893
1894static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001896{
1897 struct weston_subsurface *sub;
1898
Pekka Paalanene67858b2013-04-25 13:57:42 +03001899 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001900 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001901 continue;
1902
Jason Ekstranda7af7042013-10-12 22:38:11 -05001903 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1904 wl_list_init(&sub->surface->views);
1905
1906 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001907 }
1908}
1909
1910static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001911surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001912{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 struct weston_subsurface *sub;
1914 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001915
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1917 if (sub->surface == surface)
1918 continue;
1919
George Kiagiadakised04d382014-06-13 18:10:26 +02001920 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1921 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001923 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001924
1925 surface_free_unused_subsurface_views(sub->surface);
1926 }
1927}
1928
1929static void
1930view_list_add_subsurface_view(struct weston_compositor *compositor,
1931 struct weston_subsurface *sub,
1932 struct weston_view *parent)
1933{
1934 struct weston_subsurface *child;
1935 struct weston_view *view = NULL, *iv;
1936
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001937 if (!weston_surface_is_mapped(sub->surface))
1938 return;
1939
Jason Ekstranda7af7042013-10-12 22:38:11 -05001940 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1941 if (iv->geometry.parent == parent) {
1942 view = iv;
1943 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001944 }
1945 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946
1947 if (view) {
1948 /* Put it back in the surface's list of views */
1949 wl_list_remove(&view->surface_link);
1950 wl_list_insert(&sub->surface->views, &view->surface_link);
1951 } else {
1952 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001953 weston_view_set_position(view,
1954 sub->position.x,
1955 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956 weston_view_set_transform_parent(view, parent);
1957 }
1958
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001959 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001960 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001961
Pekka Paalanenb188e912013-11-19 14:03:35 +02001962 if (wl_list_empty(&sub->surface->subsurface_list)) {
1963 wl_list_insert(compositor->view_list.prev, &view->link);
1964 return;
1965 }
1966
1967 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1968 if (child->surface == sub->surface)
1969 wl_list_insert(compositor->view_list.prev, &view->link);
1970 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001971 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001972 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001973}
1974
1975static void
1976view_list_add(struct weston_compositor *compositor,
1977 struct weston_view *view)
1978{
1979 struct weston_subsurface *sub;
1980
1981 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982
Pekka Paalanenb188e912013-11-19 14:03:35 +02001983 if (wl_list_empty(&view->surface->subsurface_list)) {
1984 wl_list_insert(compositor->view_list.prev, &view->link);
1985 return;
1986 }
1987
1988 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1989 if (sub->surface == view->surface)
1990 wl_list_insert(compositor->view_list.prev, &view->link);
1991 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001992 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001993 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001994}
1995
1996static void
1997weston_compositor_build_view_list(struct weston_compositor *compositor)
1998{
1999 struct weston_view *view;
2000 struct weston_layer *layer;
2001
2002 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002003 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 surface_stash_subsurface_views(view->surface);
2005
2006 wl_list_init(&compositor->view_list);
2007 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002008 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002009 view_list_add(compositor, view);
2010 }
2011 }
2012
2013 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002014 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002016}
2017
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002018static void
2019weston_output_take_feedback_list(struct weston_output *output,
2020 struct weston_surface *surface)
2021{
2022 struct weston_view *view;
2023 struct weston_presentation_feedback *feedback;
2024 uint32_t flags = 0xffffffff;
2025
2026 if (wl_list_empty(&surface->feedback_list))
2027 return;
2028
2029 /* All views must have the flag for the flag to survive. */
2030 wl_list_for_each(view, &surface->views, surface_link) {
2031 /* ignore views that are not on this output at all */
2032 if (view->output_mask & (1u << output->id))
2033 flags &= view->psf_flags;
2034 }
2035
2036 wl_list_for_each(feedback, &surface->feedback_list, link)
2037 feedback->psf_flags = flags;
2038
2039 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2040 wl_list_init(&surface->feedback_list);
2041}
2042
David Herrmann1edf44c2013-10-22 17:11:26 +02002043static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002044weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002045{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002046 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002048 struct weston_animation *animation, *next;
2049 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002050 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002051 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002052 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002053
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002054 if (output->destroying)
2055 return 0;
2056
Pekka Paalanenb5026542014-11-12 15:09:24 +02002057 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2058
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002059 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002061
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002062 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002063 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002064 } else {
2065 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002067 ev->psf_flags = 0;
2068 }
2069 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002070
Pekka Paalanene67858b2013-04-25 13:57:42 +03002071 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002072 wl_list_for_each(ev, &ec->view_list, link) {
2073 /* Note: This operation is safe to do multiple times on the
2074 * same surface.
2075 */
2076 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002077 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002078 &ev->surface->frame_callback_list);
2079 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002080
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002081 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082 }
2083 }
2084
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002085 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002086
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002087 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002088 pixman_region32_intersect(&output_damage,
2089 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002090 pixman_region32_subtract(&output_damage,
2091 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002092
Scott Moreauccbf29d2012-02-22 14:21:41 -07002093 if (output->dirty)
2094 weston_output_update_matrix(output);
2095
David Herrmann1edf44c2013-10-22 17:11:26 +02002096 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002097
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002098 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002099
Kristian Høgsbergef044142011-06-21 15:02:12 -04002100 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002101
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002102 weston_compositor_repick(ec);
2103 wl_event_loop_dispatch(ec->input_loop, 0);
2104
Jonas Ådahldb773762012-06-13 00:01:21 +02002105 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002106 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002107 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002108 }
2109
Scott Moreaud64cf212012-06-08 19:40:54 -06002110 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002111 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002112 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002113 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002114
Pekka Paalanenb5026542014-11-12 15:09:24 +02002115 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2116
David Herrmann1edf44c2013-10-22 17:11:26 +02002117 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002118}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002119
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002120static int
2121weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002122{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002123 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002124
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002125 wl_event_loop_dispatch(compositor->input_loop, 0);
2126
2127 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002128}
2129
Pekka Paalanen82919792014-05-21 13:51:49 +03002130static void
2131weston_output_schedule_repaint_reset(struct weston_output *output)
2132{
2133 struct weston_compositor *compositor = output->compositor;
2134 struct wl_event_loop *loop;
2135 int fd;
2136
2137 output->repaint_scheduled = 0;
2138 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2139
2140 if (compositor->input_loop_source)
2141 return;
2142
2143 loop = wl_display_get_event_loop(compositor->wl_display);
2144 fd = wl_event_loop_get_fd(compositor->input_loop);
2145 compositor->input_loop_source =
2146 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2147 weston_compositor_read_input, compositor);
2148}
2149
Kristian Høgsbergef044142011-06-21 15:02:12 -04002150WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002151weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002152 const struct timespec *stamp,
2153 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002154{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002155 struct weston_compositor *compositor = output->compositor;
Pekka Paalanen82919792014-05-21 13:51:49 +03002156 int r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002157 uint32_t refresh_nsec;
2158
Pekka Paalanenb5026542014-11-12 15:09:24 +02002159 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2160 TLP_VBLANK(stamp), TLP_END);
2161
Pekka Paalanen133e4392014-09-23 22:08:46 -04002162 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2163 weston_presentation_feedback_present_list(&output->feedback_list,
2164 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002165 output->msc,
2166 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002167
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002168 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002169
2170 if (output->repaint_needed &&
2171 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2172 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002173 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002174 if (!r)
2175 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002176 }
2177
Pekka Paalanen82919792014-05-21 13:51:49 +03002178 weston_output_schedule_repaint_reset(output);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002179}
2180
2181static void
2182idle_repaint(void *data)
2183{
2184 struct weston_output *output = data;
2185
Jonas Ådahle5a12252013-04-05 23:07:11 +02002186 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002187}
2188
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002189WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002190weston_layer_entry_insert(struct weston_layer_entry *list,
2191 struct weston_layer_entry *entry)
2192{
2193 wl_list_insert(&list->link, &entry->link);
2194 entry->layer = list->layer;
2195}
2196
2197WL_EXPORT void
2198weston_layer_entry_remove(struct weston_layer_entry *entry)
2199{
2200 wl_list_remove(&entry->link);
2201 wl_list_init(&entry->link);
2202 entry->layer = NULL;
2203}
2204
2205WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002206weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2207{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002208 wl_list_init(&layer->view_list.link);
2209 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002210 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002211 if (below != NULL)
2212 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002213}
2214
2215WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002216weston_layer_set_mask(struct weston_layer *layer,
2217 int x, int y, int width, int height)
2218{
2219 struct weston_view *view;
2220
2221 layer->mask.x1 = x;
2222 layer->mask.x2 = x + width;
2223 layer->mask.y1 = y;
2224 layer->mask.y2 = y + height;
2225
2226 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2227 weston_view_geometry_dirty(view);
2228 }
2229}
2230
2231WL_EXPORT void
2232weston_layer_set_mask_infinite(struct weston_layer *layer)
2233{
2234 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2235 UINT32_MAX, UINT32_MAX);
2236}
2237
2238WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002239weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002240{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002241 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002242 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002243
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002244 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2245 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002246 return;
2247
Pekka Paalanenb5026542014-11-12 15:09:24 +02002248 if (!output->repaint_needed)
2249 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2250
Kristian Høgsbergef044142011-06-21 15:02:12 -04002251 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002252 output->repaint_needed = 1;
2253 if (output->repaint_scheduled)
2254 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002255
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002256 wl_event_loop_add_idle(loop, idle_repaint, output);
2257 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002258 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2259
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002260
2261 if (compositor->input_loop_source) {
2262 wl_event_source_remove(compositor->input_loop_source);
2263 compositor->input_loop_source = NULL;
2264 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002265}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002266
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002267WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002268weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2269{
2270 struct weston_output *output;
2271
2272 wl_list_for_each(output, &compositor->output_list, link)
2273 weston_output_schedule_repaint(output);
2274}
2275
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002276static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002277surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002278{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002279 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002280}
2281
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002282static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002283surface_attach(struct wl_client *client,
2284 struct wl_resource *resource,
2285 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2286{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002287 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002288 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002289
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002290 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002291 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002292 if (buffer == NULL) {
2293 wl_client_post_no_memory(client);
2294 return;
2295 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002296 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002297
Pekka Paalanende685b82012-12-04 15:58:12 +02002298 /* Attach, attach, without commit in between does not send
2299 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002300 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002301
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002302 surface->pending.sx = sx;
2303 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002304 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002305}
2306
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002307static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002308surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002309 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002310 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002311{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002312 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002313
Pekka Paalanen8e159182012-10-10 12:49:25 +03002314 pixman_region32_union_rect(&surface->pending.damage,
2315 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002316 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002317}
2318
Kristian Høgsberg33418202011-08-16 23:01:28 -04002319static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002320destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002321{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002322 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002323
2324 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002325 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002326}
2327
2328static void
2329surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002330 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002331{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002332 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002333 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002334
2335 cb = malloc(sizeof *cb);
2336 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002337 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002338 return;
2339 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002340
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002341 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2342 callback);
2343 if (cb->resource == NULL) {
2344 free(cb);
2345 wl_resource_post_no_memory(resource);
2346 return;
2347 }
2348
Jason Ekstranda85118c2013-06-27 20:17:02 -05002349 wl_resource_set_implementation(cb->resource, NULL, cb,
2350 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002351
Pekka Paalanenbc106382012-10-10 12:49:31 +03002352 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002353}
2354
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002355static void
2356surface_set_opaque_region(struct wl_client *client,
2357 struct wl_resource *resource,
2358 struct wl_resource *region_resource)
2359{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002360 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002361 struct weston_region *region;
2362
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002363 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002364 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002365 pixman_region32_copy(&surface->pending.opaque,
2366 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002367 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002368 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002369 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002370}
2371
2372static void
2373surface_set_input_region(struct wl_client *client,
2374 struct wl_resource *resource,
2375 struct wl_resource *region_resource)
2376{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002377 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002378 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002379
2380 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002381 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002382 pixman_region32_copy(&surface->pending.input,
2383 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002384 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002385 pixman_region32_fini(&surface->pending.input);
2386 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002387 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002388}
2389
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002390static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002391weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002392{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002393 struct weston_subsurface *sub;
2394
2395 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2396 parent_link_pending) {
2397 wl_list_remove(&sub->parent_link);
2398 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2399 }
2400}
2401
2402static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002403weston_surface_commit_state(struct weston_surface *surface,
2404 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002405{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002406 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002407 pixman_region32_t opaque;
2408
Alexander Larsson4ea95522013-05-22 14:41:37 +02002409 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002410 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002411 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002412 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002413
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002414 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002415 if (state->newly_attached)
2416 weston_surface_attach(surface, state->buffer);
2417 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002418
Jason Ekstrand7b982072014-05-20 14:33:03 -05002419 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002420 weston_surface_update_size(surface);
2421 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002422 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002423 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002424
Jason Ekstrand7b982072014-05-20 14:33:03 -05002425 state->sx = 0;
2426 state->sy = 0;
2427 state->newly_attached = 0;
2428 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002429
2430 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002431 if (weston_timeline_enabled_ &&
2432 pixman_region32_not_empty(&state->damage))
2433 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002434 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002435 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002436 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002437 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002438 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002439
2440 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002441 pixman_region32_init(&opaque);
2442 pixman_region32_intersect_rect(&opaque, &state->opaque,
2443 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002444
2445 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2446 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002447 wl_list_for_each(view, &surface->views, surface_link)
2448 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002449 }
2450
2451 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002452
2453 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002454 pixman_region32_intersect_rect(&surface->input, &state->input,
2455 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002456
Pekka Paalanenbc106382012-10-10 12:49:31 +03002457 /* wl_surface.frame */
2458 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002459 &state->frame_callback_list);
2460 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002461
2462 /* XXX:
2463 * What should happen with a feedback request, if there
2464 * is no wl_buffer attached for this commit?
2465 */
2466
2467 /* presentation.feedback */
2468 wl_list_insert_list(&surface->feedback_list,
2469 &state->feedback_list);
2470 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002471}
2472
2473static void
2474weston_surface_commit(struct weston_surface *surface)
2475{
2476 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002477
Pekka Paalanene67858b2013-04-25 13:57:42 +03002478 weston_surface_commit_subsurface_order(surface);
2479
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002480 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002481}
2482
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002483static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002484weston_subsurface_commit(struct weston_subsurface *sub);
2485
2486static void
2487weston_subsurface_parent_commit(struct weston_subsurface *sub,
2488 int parent_is_synchronized);
2489
2490static void
2491surface_commit(struct wl_client *client, struct wl_resource *resource)
2492{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002493 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002494 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2495
2496 if (sub) {
2497 weston_subsurface_commit(sub);
2498 return;
2499 }
2500
2501 weston_surface_commit(surface);
2502
2503 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2504 if (sub->surface != surface)
2505 weston_subsurface_parent_commit(sub, 0);
2506 }
2507}
2508
2509static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002510surface_set_buffer_transform(struct wl_client *client,
2511 struct wl_resource *resource, int transform)
2512{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002513 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002514
Jonny Lamba55f1392014-05-30 12:07:15 +02002515 /* if wl_output.transform grows more members this will need to be updated. */
2516 if (transform < 0 ||
2517 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2518 wl_resource_post_error(resource,
2519 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2520 "buffer transform must be a valid transform "
2521 "('%d' specified)", transform);
2522 return;
2523 }
2524
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002525 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002526 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002527}
2528
Alexander Larsson4ea95522013-05-22 14:41:37 +02002529static void
2530surface_set_buffer_scale(struct wl_client *client,
2531 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002532 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002533{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002534 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002535
Jonny Lamba55f1392014-05-30 12:07:15 +02002536 if (scale < 1) {
2537 wl_resource_post_error(resource,
2538 WL_SURFACE_ERROR_INVALID_SCALE,
2539 "buffer scale must be at least one "
2540 "('%d' specified)", scale);
2541 return;
2542 }
2543
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002544 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002545 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002546}
2547
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002548static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002549 surface_destroy,
2550 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002551 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002552 surface_frame,
2553 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002554 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002555 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002556 surface_set_buffer_transform,
2557 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002558};
2559
2560static void
2561compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002562 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002563{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002564 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002565 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002566
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002567 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002568 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002569 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002570 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002571 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002572
Jason Ekstranda85118c2013-06-27 20:17:02 -05002573 surface->resource =
2574 wl_resource_create(client, &wl_surface_interface,
2575 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002576 if (surface->resource == NULL) {
2577 weston_surface_destroy(surface);
2578 wl_resource_post_no_memory(resource);
2579 return;
2580 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002581 wl_resource_set_implementation(surface->resource, &surface_interface,
2582 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002583
2584 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002585}
2586
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002587static void
2588destroy_region(struct wl_resource *resource)
2589{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002590 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002591
2592 pixman_region32_fini(&region->region);
2593 free(region);
2594}
2595
2596static void
2597region_destroy(struct wl_client *client, struct wl_resource *resource)
2598{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002599 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600}
2601
2602static void
2603region_add(struct wl_client *client, struct wl_resource *resource,
2604 int32_t x, int32_t y, int32_t width, int32_t height)
2605{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002606 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002607
2608 pixman_region32_union_rect(&region->region, &region->region,
2609 x, y, width, height);
2610}
2611
2612static void
2613region_subtract(struct wl_client *client, struct wl_resource *resource,
2614 int32_t x, int32_t y, int32_t width, int32_t height)
2615{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002616 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002617 pixman_region32_t rect;
2618
2619 pixman_region32_init_rect(&rect, x, y, width, height);
2620 pixman_region32_subtract(&region->region, &region->region, &rect);
2621 pixman_region32_fini(&rect);
2622}
2623
2624static const struct wl_region_interface region_interface = {
2625 region_destroy,
2626 region_add,
2627 region_subtract
2628};
2629
2630static void
2631compositor_create_region(struct wl_client *client,
2632 struct wl_resource *resource, uint32_t id)
2633{
2634 struct weston_region *region;
2635
2636 region = malloc(sizeof *region);
2637 if (region == NULL) {
2638 wl_resource_post_no_memory(resource);
2639 return;
2640 }
2641
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002642 pixman_region32_init(&region->region);
2643
Jason Ekstranda85118c2013-06-27 20:17:02 -05002644 region->resource =
2645 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002646 if (region->resource == NULL) {
2647 free(region);
2648 wl_resource_post_no_memory(resource);
2649 return;
2650 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002651 wl_resource_set_implementation(region->resource, &region_interface,
2652 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002653}
2654
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002655static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002656 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002657 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002658};
2659
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002660static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002661weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2662{
2663 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664
Jason Ekstrand7b982072014-05-20 14:33:03 -05002665 weston_surface_commit_state(surface, &sub->cached);
2666 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002667
2668 weston_surface_commit_subsurface_order(surface);
2669
2670 weston_surface_schedule_repaint(surface);
2671
Jason Ekstrand7b982072014-05-20 14:33:03 -05002672 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002673}
2674
2675static void
2676weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2677{
2678 struct weston_surface *surface = sub->surface;
2679
2680 /*
2681 * If this commit would cause the surface to move by the
2682 * attach(dx, dy) parameters, the old damage region must be
2683 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002684 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002685 */
2686 pixman_region32_translate(&sub->cached.damage,
2687 -surface->pending.sx, -surface->pending.sy);
2688 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2689 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002690 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002691
2692 if (surface->pending.newly_attached) {
2693 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002694 weston_surface_state_set_buffer(&sub->cached,
2695 surface->pending.buffer);
2696 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002697 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002698 weston_presentation_feedback_discard_list(
2699 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002700 }
2701 sub->cached.sx += surface->pending.sx;
2702 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002703
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002704 sub->cached.buffer_viewport.changed |=
2705 surface->pending.buffer_viewport.changed;
2706 sub->cached.buffer_viewport.buffer =
2707 surface->pending.buffer_viewport.buffer;
2708 sub->cached.buffer_viewport.surface =
2709 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002710
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002711 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002712
2713 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2714
2715 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2716
2717 wl_list_insert_list(&sub->cached.frame_callback_list,
2718 &surface->pending.frame_callback_list);
2719 wl_list_init(&surface->pending.frame_callback_list);
2720
Pekka Paalanen133e4392014-09-23 22:08:46 -04002721 wl_list_insert_list(&sub->cached.feedback_list,
2722 &surface->pending.feedback_list);
2723 wl_list_init(&surface->pending.feedback_list);
2724
Jason Ekstrand7b982072014-05-20 14:33:03 -05002725 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002726}
2727
Derek Foreman280e7dd2014-10-03 13:13:42 -05002728static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2730{
2731 while (sub) {
2732 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002733 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002734
2735 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002736 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002737
2738 sub = weston_surface_to_subsurface(sub->parent);
2739 }
2740
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002741 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002742}
2743
2744static void
2745weston_subsurface_commit(struct weston_subsurface *sub)
2746{
2747 struct weston_surface *surface = sub->surface;
2748 struct weston_subsurface *tmp;
2749
2750 /* Recursive check for effectively synchronized. */
2751 if (weston_subsurface_is_synchronized(sub)) {
2752 weston_subsurface_commit_to_cache(sub);
2753 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002754 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002755 /* flush accumulated state from cache */
2756 weston_subsurface_commit_to_cache(sub);
2757 weston_subsurface_commit_from_cache(sub);
2758 } else {
2759 weston_surface_commit(surface);
2760 }
2761
2762 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2763 if (tmp->surface != surface)
2764 weston_subsurface_parent_commit(tmp, 0);
2765 }
2766 }
2767}
2768
2769static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002770weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771{
2772 struct weston_surface *surface = sub->surface;
2773 struct weston_subsurface *tmp;
2774
Pekka Paalanene67858b2013-04-25 13:57:42 +03002775 /* From now on, commit_from_cache the whole sub-tree, regardless of
2776 * the synchronized mode of each child. This sub-surface or some
2777 * of its ancestors were synchronized, so we are synchronized
2778 * all the way down.
2779 */
2780
Jason Ekstrand7b982072014-05-20 14:33:03 -05002781 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 weston_subsurface_commit_from_cache(sub);
2783
2784 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2785 if (tmp->surface != surface)
2786 weston_subsurface_parent_commit(tmp, 1);
2787 }
2788}
2789
2790static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002791weston_subsurface_parent_commit(struct weston_subsurface *sub,
2792 int parent_is_synchronized)
2793{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002794 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002795 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002796 wl_list_for_each(view, &sub->surface->views, surface_link)
2797 weston_view_set_position(view,
2798 sub->position.x,
2799 sub->position.y);
2800
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002801 sub->position.set = 0;
2802 }
2803
2804 if (parent_is_synchronized || sub->synchronized)
2805 weston_subsurface_synchronized_commit(sub);
2806}
2807
Pekka Paalanen8274d902014-08-06 19:36:51 +03002808static int
2809subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2810{
2811 return snprintf(buf, len, "sub-surface");
2812}
2813
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002814static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002815subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002816{
2817 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002818 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002821 weston_view_set_position(view,
2822 view->geometry.x + dx,
2823 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824
2825 /* No need to check parent mappedness, because if parent is not
2826 * mapped, parent is not in a visible layer, so this sub-surface
2827 * will not be drawn either.
2828 */
2829 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002830 struct weston_output *output;
2831
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002832 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833 * because that would call it also for the parent surface,
2834 * which might not be mapped yet. That would lead to
2835 * inconsistent state, where the window could never be
2836 * mapped.
2837 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002838 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002839 * weston_surface_is_mapped() return true, so that when the
2840 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002841 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002842 */
2843 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002844 output = container_of(compositor->output_list.next,
2845 struct weston_output, link);
2846
2847 surface->output = output;
2848 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002849 }
2850}
2851
2852static struct weston_subsurface *
2853weston_surface_to_subsurface(struct weston_surface *surface)
2854{
2855 if (surface->configure == subsurface_configure)
2856 return surface->configure_private;
2857
2858 return NULL;
2859}
2860
Pekka Paalanen01388e22013-04-25 13:57:44 +03002861WL_EXPORT struct weston_surface *
2862weston_surface_get_main_surface(struct weston_surface *surface)
2863{
2864 struct weston_subsurface *sub;
2865
2866 while (surface && (sub = weston_surface_to_subsurface(surface)))
2867 surface = sub->parent;
2868
2869 return surface;
2870}
2871
Pekka Paalanen50b67472014-10-01 15:02:41 +03002872WL_EXPORT int
2873weston_surface_set_role(struct weston_surface *surface,
2874 const char *role_name,
2875 struct wl_resource *error_resource,
2876 uint32_t error_code)
2877{
2878 assert(role_name);
2879
2880 if (surface->role_name == NULL ||
2881 surface->role_name == role_name ||
2882 strcmp(surface->role_name, role_name) == 0) {
2883 surface->role_name = role_name;
2884
2885 return 0;
2886 }
2887
2888 wl_resource_post_error(error_resource, error_code,
2889 "Cannot assign role %s to wl_surface@%d,"
2890 " already has role %s\n",
2891 role_name,
2892 wl_resource_get_id(surface->resource),
2893 surface->role_name);
2894 return -1;
2895}
2896
Pekka Paalanen8274d902014-08-06 19:36:51 +03002897WL_EXPORT void
2898weston_surface_set_label_func(struct weston_surface *surface,
2899 int (*desc)(struct weston_surface *,
2900 char *, size_t))
2901{
2902 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002903 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002904}
2905
Pekka Paalanene67858b2013-04-25 13:57:42 +03002906static void
2907subsurface_set_position(struct wl_client *client,
2908 struct wl_resource *resource, int32_t x, int32_t y)
2909{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002910 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002911
2912 if (!sub)
2913 return;
2914
2915 sub->position.x = x;
2916 sub->position.y = y;
2917 sub->position.set = 1;
2918}
2919
2920static struct weston_subsurface *
2921subsurface_from_surface(struct weston_surface *surface)
2922{
2923 struct weston_subsurface *sub;
2924
2925 sub = weston_surface_to_subsurface(surface);
2926 if (sub)
2927 return sub;
2928
2929 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2930 if (sub->surface == surface)
2931 return sub;
2932
2933 return NULL;
2934}
2935
2936static struct weston_subsurface *
2937subsurface_sibling_check(struct weston_subsurface *sub,
2938 struct weston_surface *surface,
2939 const char *request)
2940{
2941 struct weston_subsurface *sibling;
2942
2943 sibling = subsurface_from_surface(surface);
2944
2945 if (!sibling) {
2946 wl_resource_post_error(sub->resource,
2947 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2948 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002949 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002950 return NULL;
2951 }
2952
2953 if (sibling->parent != sub->parent) {
2954 wl_resource_post_error(sub->resource,
2955 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2956 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002957 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002958 return NULL;
2959 }
2960
2961 return sibling;
2962}
2963
2964static void
2965subsurface_place_above(struct wl_client *client,
2966 struct wl_resource *resource,
2967 struct wl_resource *sibling_resource)
2968{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002969 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002970 struct weston_surface *surface =
2971 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002972 struct weston_subsurface *sibling;
2973
2974 if (!sub)
2975 return;
2976
2977 sibling = subsurface_sibling_check(sub, surface, "place_above");
2978 if (!sibling)
2979 return;
2980
2981 wl_list_remove(&sub->parent_link_pending);
2982 wl_list_insert(sibling->parent_link_pending.prev,
2983 &sub->parent_link_pending);
2984}
2985
2986static void
2987subsurface_place_below(struct wl_client *client,
2988 struct wl_resource *resource,
2989 struct wl_resource *sibling_resource)
2990{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002991 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002992 struct weston_surface *surface =
2993 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002994 struct weston_subsurface *sibling;
2995
2996 if (!sub)
2997 return;
2998
2999 sibling = subsurface_sibling_check(sub, surface, "place_below");
3000 if (!sibling)
3001 return;
3002
3003 wl_list_remove(&sub->parent_link_pending);
3004 wl_list_insert(&sibling->parent_link_pending,
3005 &sub->parent_link_pending);
3006}
3007
3008static void
3009subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
3010{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003011 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003012
3013 if (sub)
3014 sub->synchronized = 1;
3015}
3016
3017static void
3018subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
3019{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003020 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003021
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003022 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003023 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003024
3025 /* If sub became effectively desynchronized, flush. */
3026 if (!weston_subsurface_is_synchronized(sub))
3027 weston_subsurface_synchronized_commit(sub);
3028 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003029}
3030
3031static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003032weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3033{
3034 wl_list_remove(&sub->parent_link);
3035 wl_list_remove(&sub->parent_link_pending);
3036 wl_list_remove(&sub->parent_destroy_listener.link);
3037 sub->parent = NULL;
3038}
3039
3040static void
3041weston_subsurface_destroy(struct weston_subsurface *sub);
3042
3043static void
3044subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3045{
3046 struct weston_subsurface *sub =
3047 container_of(listener, struct weston_subsurface,
3048 surface_destroy_listener);
3049 assert(data == &sub->surface->resource);
3050
3051 /* The protocol object (wl_resource) is left inert. */
3052 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003053 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003054
3055 weston_subsurface_destroy(sub);
3056}
3057
3058static void
3059subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3060{
3061 struct weston_subsurface *sub =
3062 container_of(listener, struct weston_subsurface,
3063 parent_destroy_listener);
3064 assert(data == &sub->parent->resource);
3065 assert(sub->surface != sub->parent);
3066
3067 if (weston_surface_is_mapped(sub->surface))
3068 weston_surface_unmap(sub->surface);
3069
3070 weston_subsurface_unlink_parent(sub);
3071}
3072
3073static void
3074subsurface_resource_destroy(struct wl_resource *resource)
3075{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003076 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077
3078 if (sub)
3079 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003080}
3081
3082static void
3083subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3084{
3085 wl_resource_destroy(resource);
3086}
3087
3088static void
3089weston_subsurface_link_parent(struct weston_subsurface *sub,
3090 struct weston_surface *parent)
3091{
3092 sub->parent = parent;
3093 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003094 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003095 &sub->parent_destroy_listener);
3096
3097 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3098 wl_list_insert(&parent->subsurface_list_pending,
3099 &sub->parent_link_pending);
3100}
3101
3102static void
3103weston_subsurface_link_surface(struct weston_subsurface *sub,
3104 struct weston_surface *surface)
3105{
3106 sub->surface = surface;
3107 sub->surface_destroy_listener.notify =
3108 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003109 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003110 &sub->surface_destroy_listener);
3111}
3112
3113static void
3114weston_subsurface_destroy(struct weston_subsurface *sub)
3115{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003116 struct weston_view *view, *next;
3117
Pekka Paalanene67858b2013-04-25 13:57:42 +03003118 assert(sub->surface);
3119
3120 if (sub->resource) {
3121 assert(weston_surface_to_subsurface(sub->surface) == sub);
3122 assert(sub->parent_destroy_listener.notify ==
3123 subsurface_handle_parent_destroy);
3124
George Kiagiadakised04d382014-06-13 18:10:26 +02003125 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3126 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003127 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003128 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003129
Pekka Paalanene67858b2013-04-25 13:57:42 +03003130 if (sub->parent)
3131 weston_subsurface_unlink_parent(sub);
3132
Jason Ekstrand7b982072014-05-20 14:33:03 -05003133 weston_surface_state_fini(&sub->cached);
3134 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003135
3136 sub->surface->configure = NULL;
3137 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003138 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003139 } else {
3140 /* the dummy weston_subsurface for the parent itself */
3141 assert(sub->parent_destroy_listener.notify == NULL);
3142 wl_list_remove(&sub->parent_link);
3143 wl_list_remove(&sub->parent_link_pending);
3144 }
3145
3146 wl_list_remove(&sub->surface_destroy_listener.link);
3147 free(sub);
3148}
3149
3150static const struct wl_subsurface_interface subsurface_implementation = {
3151 subsurface_destroy,
3152 subsurface_set_position,
3153 subsurface_place_above,
3154 subsurface_place_below,
3155 subsurface_set_sync,
3156 subsurface_set_desync
3157};
3158
3159static struct weston_subsurface *
3160weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3161 struct weston_surface *parent)
3162{
3163 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003164 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165
Bryce Harringtonde16d892014-11-20 22:21:57 -08003166 sub = zalloc(sizeof *sub);
3167 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003168 return NULL;
3169
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 wl_list_init(&sub->unused_views);
3171
Jason Ekstranda85118c2013-06-27 20:17:02 -05003172 sub->resource =
3173 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003174 if (!sub->resource) {
3175 free(sub);
3176 return NULL;
3177 }
3178
Jason Ekstranda85118c2013-06-27 20:17:02 -05003179 wl_resource_set_implementation(sub->resource,
3180 &subsurface_implementation,
3181 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182 weston_subsurface_link_surface(sub, surface);
3183 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003184 weston_surface_state_init(&sub->cached);
3185 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003186 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003187
3188 return sub;
3189}
3190
3191/* Create a dummy subsurface for having the parent itself in its
3192 * sub-surface lists. Makes stacking order manipulation easy.
3193 */
3194static struct weston_subsurface *
3195weston_subsurface_create_for_parent(struct weston_surface *parent)
3196{
3197 struct weston_subsurface *sub;
3198
Bryce Harringtonde16d892014-11-20 22:21:57 -08003199 sub = zalloc(sizeof *sub);
3200 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003201 return NULL;
3202
3203 weston_subsurface_link_surface(sub, parent);
3204 sub->parent = parent;
3205 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3206 wl_list_insert(&parent->subsurface_list_pending,
3207 &sub->parent_link_pending);
3208
3209 return sub;
3210}
3211
3212static void
3213subcompositor_get_subsurface(struct wl_client *client,
3214 struct wl_resource *resource,
3215 uint32_t id,
3216 struct wl_resource *surface_resource,
3217 struct wl_resource *parent_resource)
3218{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003219 struct weston_surface *surface =
3220 wl_resource_get_user_data(surface_resource);
3221 struct weston_surface *parent =
3222 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003223 struct weston_subsurface *sub;
3224 static const char where[] = "get_subsurface: wl_subsurface@";
3225
3226 if (surface == parent) {
3227 wl_resource_post_error(resource,
3228 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3229 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003230 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003231 return;
3232 }
3233
3234 if (weston_surface_to_subsurface(surface)) {
3235 wl_resource_post_error(resource,
3236 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3237 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003238 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003239 return;
3240 }
3241
Pekka Paalanen50b67472014-10-01 15:02:41 +03003242 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3243 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003244 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003245
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003246 if (weston_surface_get_main_surface(parent) == surface) {
3247 wl_resource_post_error(resource,
3248 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3249 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003250 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003251 return;
3252 }
3253
Pekka Paalanene67858b2013-04-25 13:57:42 +03003254 /* make sure the parent is in its own list */
3255 if (wl_list_empty(&parent->subsurface_list)) {
3256 if (!weston_subsurface_create_for_parent(parent)) {
3257 wl_resource_post_no_memory(resource);
3258 return;
3259 }
3260 }
3261
3262 sub = weston_subsurface_create(id, surface, parent);
3263 if (!sub) {
3264 wl_resource_post_no_memory(resource);
3265 return;
3266 }
3267
3268 surface->configure = subsurface_configure;
3269 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003270 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003271}
3272
3273static void
3274subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3275{
3276 wl_resource_destroy(resource);
3277}
3278
3279static const struct wl_subcompositor_interface subcompositor_interface = {
3280 subcompositor_destroy,
3281 subcompositor_get_subsurface
3282};
3283
3284static void
3285bind_subcompositor(struct wl_client *client,
3286 void *data, uint32_t version, uint32_t id)
3287{
3288 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003289 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003290
Jason Ekstranda85118c2013-06-27 20:17:02 -05003291 resource =
3292 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003293 if (resource == NULL) {
3294 wl_client_post_no_memory(client);
3295 return;
3296 }
3297 wl_resource_set_implementation(resource, &subcompositor_interface,
3298 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003299}
3300
3301static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003302weston_compositor_dpms(struct weston_compositor *compositor,
3303 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003304{
3305 struct weston_output *output;
3306
3307 wl_list_for_each(output, &compositor->output_list, link)
3308 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003309 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003310}
3311
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003312WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003313weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003314{
Neil Roberts8b62e202013-09-30 13:14:47 +01003315 uint32_t old_state = compositor->state;
3316
3317 /* The state needs to be changed before emitting the wake
3318 * signal because that may try to schedule a repaint which
3319 * will not work if the compositor is still sleeping */
3320 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3321
3322 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003323 case WESTON_COMPOSITOR_SLEEPING:
3324 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3325 /* fall through */
3326 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003327 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003328 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003329 /* fall through */
3330 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003331 wl_event_source_timer_update(compositor->idle_source,
3332 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003333 }
3334}
3335
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003336WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003337weston_compositor_offscreen(struct weston_compositor *compositor)
3338{
3339 switch (compositor->state) {
3340 case WESTON_COMPOSITOR_OFFSCREEN:
3341 return;
3342 case WESTON_COMPOSITOR_SLEEPING:
3343 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3344 /* fall through */
3345 default:
3346 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3347 wl_event_source_timer_update(compositor->idle_source, 0);
3348 }
3349}
3350
3351WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003352weston_compositor_sleep(struct weston_compositor *compositor)
3353{
3354 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3355 return;
3356
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003357 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003358 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3359 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3360}
3361
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003362static int
3363idle_handler(void *data)
3364{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003365 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003366
3367 if (compositor->idle_inhibit)
3368 return 1;
3369
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003370 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003371 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003372
3373 return 1;
3374}
3375
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003376WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003377weston_plane_init(struct weston_plane *plane,
3378 struct weston_compositor *ec,
3379 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003380{
3381 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003382 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003383 plane->x = x;
3384 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003385 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003386
3387 /* Init the link so that the call to wl_list_remove() when releasing
3388 * the plane without ever stacking doesn't lead to a crash */
3389 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003390}
3391
3392WL_EXPORT void
3393weston_plane_release(struct weston_plane *plane)
3394{
Xiong Zhang97116532013-10-23 13:58:31 +08003395 struct weston_view *view;
3396
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003397 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003398 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003399
Xiong Zhang97116532013-10-23 13:58:31 +08003400 wl_list_for_each(view, &plane->compositor->view_list, link) {
3401 if (view->plane == plane)
3402 view->plane = NULL;
3403 }
3404
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003405 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003406}
3407
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003408WL_EXPORT void
3409weston_compositor_stack_plane(struct weston_compositor *ec,
3410 struct weston_plane *plane,
3411 struct weston_plane *above)
3412{
3413 if (above)
3414 wl_list_insert(above->link.prev, &plane->link);
3415 else
3416 wl_list_insert(&ec->plane_list, &plane->link);
3417}
3418
Casey Dahlin9074db52012-04-19 22:50:09 -04003419static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003420{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003421 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003422}
3423
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003424static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003425bind_output(struct wl_client *client,
3426 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003427{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003428 struct weston_output *output = data;
3429 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003430 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003431
Jason Ekstranda85118c2013-06-27 20:17:02 -05003432 resource = wl_resource_create(client, &wl_output_interface,
3433 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003434 if (resource == NULL) {
3435 wl_client_post_no_memory(client);
3436 return;
3437 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003438
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003439 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003440 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003441
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003442 wl_output_send_geometry(resource,
3443 output->x,
3444 output->y,
3445 output->mm_width,
3446 output->mm_height,
3447 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003448 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003449 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003450 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003451 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003452 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003453
3454 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003455 wl_output_send_mode(resource,
3456 mode->flags,
3457 mode->width,
3458 mode->height,
3459 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003460 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003461
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003462 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003463 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003464}
3465
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003466/* Move other outputs when one is removed so the space remains contiguos. */
3467static void
3468weston_compositor_remove_output(struct weston_compositor *compositor,
3469 struct weston_output *remove_output)
3470{
3471 struct weston_output *output;
3472 int offset = 0;
3473
3474 wl_list_for_each(output, &compositor->output_list, link) {
3475 if (output == remove_output) {
3476 offset = output->width;
3477 continue;
3478 }
3479
3480 if (offset > 0) {
3481 weston_output_move(output,
3482 output->x - offset, output->y);
3483 output->dirty = 1;
3484 }
3485 }
3486}
3487
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003488WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003489weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003490{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003491 struct wl_resource *resource;
3492
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003493 output->destroying = 1;
3494
Pekka Paalanen133e4392014-09-23 22:08:46 -04003495 weston_presentation_feedback_discard_list(&output->feedback_list);
3496
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003497 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003498 wl_list_remove(&output->link);
3499
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003500 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003501 wl_signal_emit(&output->destroy_signal, output);
3502
Richard Hughesafe690c2013-05-02 10:10:04 +01003503 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003504 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003505 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003506 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003507
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003508 wl_resource_for_each(resource, &output->resource_list) {
3509 wl_resource_set_destructor(resource, NULL);
3510 }
3511
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003512 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003513}
3514
Scott Moreau1bad5db2012-08-18 01:04:05 -06003515static void
3516weston_output_compute_transform(struct weston_output *output)
3517{
3518 struct weston_matrix transform;
3519 int flip;
3520
3521 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003522 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003523
3524 switch(output->transform) {
3525 case WL_OUTPUT_TRANSFORM_FLIPPED:
3526 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3527 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3528 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003529 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003530 flip = -1;
3531 break;
3532 default:
3533 flip = 1;
3534 break;
3535 }
3536
3537 switch(output->transform) {
3538 case WL_OUTPUT_TRANSFORM_NORMAL:
3539 case WL_OUTPUT_TRANSFORM_FLIPPED:
3540 transform.d[0] = flip;
3541 transform.d[1] = 0;
3542 transform.d[4] = 0;
3543 transform.d[5] = 1;
3544 break;
3545 case WL_OUTPUT_TRANSFORM_90:
3546 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3547 transform.d[0] = 0;
3548 transform.d[1] = -flip;
3549 transform.d[4] = 1;
3550 transform.d[5] = 0;
3551 break;
3552 case WL_OUTPUT_TRANSFORM_180:
3553 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3554 transform.d[0] = -flip;
3555 transform.d[1] = 0;
3556 transform.d[4] = 0;
3557 transform.d[5] = -1;
3558 break;
3559 case WL_OUTPUT_TRANSFORM_270:
3560 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3561 transform.d[0] = 0;
3562 transform.d[1] = flip;
3563 transform.d[4] = -1;
3564 transform.d[5] = 0;
3565 break;
3566 default:
3567 break;
3568 }
3569
3570 weston_matrix_multiply(&output->matrix, &transform);
3571}
3572
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003573WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003574weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003575{
Scott Moreau850ca422012-05-21 15:21:25 -06003576 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003577
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003578 weston_matrix_init(&output->matrix);
3579 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003580 -(output->x + output->width / 2.0),
3581 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003582
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003583 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003584 2.0 / output->width,
3585 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003586
Scott Moreauccbf29d2012-02-22 14:21:41 -07003587 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003588 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003589 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003590 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3591 output->zoom.trans_y, 0);
3592 weston_matrix_scale(&output->matrix, magnification,
3593 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003594 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003595
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003596 weston_output_compute_transform(output);
3597
Scott Moreauccbf29d2012-02-22 14:21:41 -07003598 output->dirty = 0;
3599}
3600
Scott Moreau1bad5db2012-08-18 01:04:05 -06003601static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003602weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003603{
3604 output->transform = transform;
3605
3606 switch (transform) {
3607 case WL_OUTPUT_TRANSFORM_90:
3608 case WL_OUTPUT_TRANSFORM_270:
3609 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3610 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3611 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003612 output->width = output->current_mode->height;
3613 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003614 break;
3615 case WL_OUTPUT_TRANSFORM_NORMAL:
3616 case WL_OUTPUT_TRANSFORM_180:
3617 case WL_OUTPUT_TRANSFORM_FLIPPED:
3618 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003619 output->width = output->current_mode->width;
3620 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003621 break;
3622 default:
3623 break;
3624 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003625
Hardening57388e42013-09-18 23:56:36 +02003626 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003627 output->width /= scale;
3628 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003629}
3630
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003631static void
3632weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003633{
3634 output->x = x;
3635 output->y = y;
3636
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003637 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003638 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003639 output->width,
3640 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003641}
3642
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003643WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003644weston_output_move(struct weston_output *output, int x, int y)
3645{
3646 pixman_region32_t old_region;
3647 struct wl_resource *resource;
3648
3649 output->move_x = x - output->x;
3650 output->move_y = y - output->y;
3651
3652 if (output->move_x == 0 && output->move_y == 0)
3653 return;
3654
3655 pixman_region32_init(&old_region);
3656 pixman_region32_copy(&old_region, &output->region);
3657
3658 weston_output_init_geometry(output, x, y);
3659
3660 output->dirty = 1;
3661
3662 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003663 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003664
3665 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003666 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003667 wl_output_send_geometry(resource,
3668 output->x,
3669 output->y,
3670 output->mm_width,
3671 output->mm_height,
3672 output->subpixel,
3673 output->make,
3674 output->model,
3675 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003676
3677 if (wl_resource_get_version(resource) >= 2)
3678 wl_output_send_done(resource);
3679 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003680}
3681
3682WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003683weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003684 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003685 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003686{
3687 output->compositor = c;
3688 output->x = x;
3689 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003690 output->mm_width = mm_width;
3691 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003692 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003693 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003694
Alexander Larsson0b135062013-05-28 16:23:36 +02003695 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003696 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003697
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003698 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003699 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003700
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003701 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003702 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003703 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003704 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003705 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003706
Casey Dahlin58ba1372012-04-19 22:50:08 -04003707 output->id = ffs(~output->compositor->output_id_pool) - 1;
3708 output->compositor->output_id_pool |= 1 << output->id;
3709
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003710 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003711 wl_global_create(c->wl_display, &wl_output_interface, 2,
3712 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003713 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003714}
3715
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003716WL_EXPORT void
3717weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003718 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003719 wl_fixed_t *x, wl_fixed_t *y)
3720{
3721 wl_fixed_t tx, ty;
3722 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003723 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003724
Hardeningff39efa2013-09-18 23:56:35 +02003725 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3726 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003727
3728 switch(output->transform) {
3729 case WL_OUTPUT_TRANSFORM_NORMAL:
3730 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003731 tx = device_x;
3732 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003733 break;
3734 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003735 tx = device_y;
3736 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003737 break;
3738 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003739 tx = width - device_x;
3740 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003741 break;
3742 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003743 tx = width - device_y;
3744 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003745 break;
3746 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003747 tx = width - device_x;
3748 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003749 break;
3750 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003751 tx = width - device_y;
3752 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003753 break;
3754 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003755 tx = device_x;
3756 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003757 break;
3758 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003759 tx = device_y;
3760 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003761 break;
3762 }
3763
Neil Robertseb5a2002014-05-01 16:13:55 +01003764 tx /= output->current_scale;
3765 ty /= output->current_scale;
3766
3767 if (output->zoom.active) {
3768 zoom_scale = output->zoom.spring_z.current;
3769 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3770 output->width / 2.0f *
3771 (zoom_scale + output->zoom.trans_x));
3772 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3773 output->height / 2.0f *
3774 (zoom_scale + output->zoom.trans_y));
3775 tx = wl_fixed_from_double(zx);
3776 ty = wl_fixed_from_double(zy);
3777 }
3778
3779 *x = tx + wl_fixed_from_int(output->x);
3780 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003781}
3782
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003783static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003784destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003785{
Jonny Lamb74130762013-11-26 18:19:46 +01003786 struct weston_surface *surface =
3787 wl_resource_get_user_data(resource);
3788
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003789 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003790 surface->pending.buffer_viewport.buffer.src_width =
3791 wl_fixed_from_int(-1);
3792 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003793 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003794}
3795
3796static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003797viewport_destroy(struct wl_client *client,
3798 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003799{
3800 wl_resource_destroy(resource);
3801}
3802
3803static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003804viewport_set(struct wl_client *client,
3805 struct wl_resource *resource,
3806 wl_fixed_t src_x,
3807 wl_fixed_t src_y,
3808 wl_fixed_t src_width,
3809 wl_fixed_t src_height,
3810 int32_t dst_width,
3811 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003812{
Jonny Lamb74130762013-11-26 18:19:46 +01003813 struct weston_surface *surface =
3814 wl_resource_get_user_data(resource);
3815
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003816 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003817
Jonny Lamb8ae35902013-11-26 18:19:45 +01003818 if (wl_fixed_to_double(src_width) < 0 ||
3819 wl_fixed_to_double(src_height) < 0) {
3820 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003821 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003822 "source dimensions must be non-negative (%fx%f)",
3823 wl_fixed_to_double(src_width),
3824 wl_fixed_to_double(src_height));
3825 return;
3826 }
3827
3828 if (dst_width <= 0 || dst_height <= 0) {
3829 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003830 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003831 "destination dimensions must be positive (%dx%d)",
3832 dst_width, dst_height);
3833 return;
3834 }
3835
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003836 surface->pending.buffer_viewport.buffer.src_x = src_x;
3837 surface->pending.buffer_viewport.buffer.src_y = src_y;
3838 surface->pending.buffer_viewport.buffer.src_width = src_width;
3839 surface->pending.buffer_viewport.buffer.src_height = src_height;
3840 surface->pending.buffer_viewport.surface.width = dst_width;
3841 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003842 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003843}
3844
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003845static void
3846viewport_set_source(struct wl_client *client,
3847 struct wl_resource *resource,
3848 wl_fixed_t src_x,
3849 wl_fixed_t src_y,
3850 wl_fixed_t src_width,
3851 wl_fixed_t src_height)
3852{
3853 struct weston_surface *surface =
3854 wl_resource_get_user_data(resource);
3855
3856 assert(surface->viewport_resource != NULL);
3857
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003858 if (src_width == wl_fixed_from_int(-1) &&
3859 src_height == wl_fixed_from_int(-1)) {
3860 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003861 surface->pending.buffer_viewport.buffer.src_width =
3862 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003863 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003864 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003865 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003866
3867 if (src_width <= 0 || src_height <= 0) {
3868 wl_resource_post_error(resource,
3869 WL_VIEWPORT_ERROR_BAD_VALUE,
3870 "source size must be positive (%fx%f)",
3871 wl_fixed_to_double(src_width),
3872 wl_fixed_to_double(src_height));
3873 return;
3874 }
3875
3876 surface->pending.buffer_viewport.buffer.src_x = src_x;
3877 surface->pending.buffer_viewport.buffer.src_y = src_y;
3878 surface->pending.buffer_viewport.buffer.src_width = src_width;
3879 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003880 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003881}
3882
3883static void
3884viewport_set_destination(struct wl_client *client,
3885 struct wl_resource *resource,
3886 int32_t dst_width,
3887 int32_t dst_height)
3888{
3889 struct weston_surface *surface =
3890 wl_resource_get_user_data(resource);
3891
3892 assert(surface->viewport_resource != NULL);
3893
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003894 if (dst_width == -1 && dst_height == -1) {
3895 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003896 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003897 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003898 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003899 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003900
3901 if (dst_width <= 0 || dst_height <= 0) {
3902 wl_resource_post_error(resource,
3903 WL_VIEWPORT_ERROR_BAD_VALUE,
3904 "destination size must be positive (%dx%d)",
3905 dst_width, dst_height);
3906 return;
3907 }
3908
3909 surface->pending.buffer_viewport.surface.width = dst_width;
3910 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003911 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003912}
3913
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003914static const struct wl_viewport_interface viewport_interface = {
3915 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003916 viewport_set,
3917 viewport_set_source,
3918 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003919};
3920
3921static void
3922scaler_destroy(struct wl_client *client,
3923 struct wl_resource *resource)
3924{
3925 wl_resource_destroy(resource);
3926}
3927
3928static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003929scaler_get_viewport(struct wl_client *client,
3930 struct wl_resource *scaler,
3931 uint32_t id,
3932 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003933{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003934 int version = wl_resource_get_version(scaler);
3935 struct weston_surface *surface =
3936 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003937 struct wl_resource *resource;
3938
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003939 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003940 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003941 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3942 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003943 return;
3944 }
3945
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003946 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003947 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003948 if (resource == NULL) {
3949 wl_client_post_no_memory(client);
3950 return;
3951 }
3952
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003953 wl_resource_set_implementation(resource, &viewport_interface,
3954 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003955
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003956 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003957}
3958
3959static const struct wl_scaler_interface scaler_interface = {
3960 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003961 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003962};
3963
3964static void
3965bind_scaler(struct wl_client *client,
3966 void *data, uint32_t version, uint32_t id)
3967{
3968 struct wl_resource *resource;
3969
3970 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003971 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003972 if (resource == NULL) {
3973 wl_client_post_no_memory(client);
3974 return;
3975 }
3976
3977 wl_resource_set_implementation(resource, &scaler_interface,
3978 NULL, NULL);
3979}
3980
3981static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003982destroy_presentation_feedback(struct wl_resource *feedback_resource)
3983{
3984 struct weston_presentation_feedback *feedback;
3985
3986 feedback = wl_resource_get_user_data(feedback_resource);
3987
3988 wl_list_remove(&feedback->link);
3989 free(feedback);
3990}
3991
3992static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003993presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3994{
3995 wl_resource_destroy(resource);
3996}
3997
3998static void
3999presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04004000 struct wl_resource *presentation_resource,
4001 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004002 uint32_t callback)
4003{
Pekka Paalanen133e4392014-09-23 22:08:46 -04004004 struct weston_surface *surface;
4005 struct weston_presentation_feedback *feedback;
4006
4007 surface = wl_resource_get_user_data(surface_resource);
4008
Bryce Harringtonde16d892014-11-20 22:21:57 -08004009 feedback = zalloc(sizeof *feedback);
4010 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04004011 goto err_calloc;
4012
4013 feedback->resource = wl_resource_create(client,
4014 &presentation_feedback_interface,
4015 1, callback);
4016 if (!feedback->resource)
4017 goto err_create;
4018
4019 wl_resource_set_implementation(feedback->resource, NULL, feedback,
4020 destroy_presentation_feedback);
4021
4022 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4023
4024 return;
4025
4026err_create:
4027 free(feedback);
4028
4029err_calloc:
4030 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004031}
4032
4033static const struct presentation_interface presentation_implementation = {
4034 presentation_destroy,
4035 presentation_feedback
4036};
4037
4038static void
4039bind_presentation(struct wl_client *client,
4040 void *data, uint32_t version, uint32_t id)
4041{
4042 struct weston_compositor *compositor = data;
4043 struct wl_resource *resource;
4044
4045 resource = wl_resource_create(client, &presentation_interface,
4046 MIN(version, 1), id);
4047 if (resource == NULL) {
4048 wl_client_post_no_memory(client);
4049 return;
4050 }
4051
4052 wl_resource_set_implementation(resource, &presentation_implementation,
4053 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004054 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004055}
4056
4057static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004058compositor_bind(struct wl_client *client,
4059 void *data, uint32_t version, uint32_t id)
4060{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004061 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004062 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004063
Jason Ekstranda85118c2013-06-27 20:17:02 -05004064 resource = wl_resource_create(client, &wl_compositor_interface,
4065 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004066 if (resource == NULL) {
4067 wl_client_post_no_memory(client);
4068 return;
4069 }
4070
4071 wl_resource_set_implementation(resource, &compositor_interface,
4072 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004073}
4074
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004075static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004076log_uname(void)
4077{
4078 struct utsname usys;
4079
4080 uname(&usys);
4081
4082 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4083 usys.version, usys.machine);
4084}
4085
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004086WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004087weston_environment_get_fd(const char *env)
4088{
4089 char *e, *end;
4090 int fd, flags;
4091
4092 e = getenv(env);
4093 if (!e)
4094 return -1;
4095 fd = strtol(e, &end, 0);
4096 if (*end != '\0')
4097 return -1;
4098
4099 flags = fcntl(fd, F_GETFD);
4100 if (flags == -1)
4101 return -1;
4102
4103 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4104 unsetenv(env);
4105
4106 return fd;
4107}
4108
Pekka Paalanenb5026542014-11-12 15:09:24 +02004109static void
4110timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4111 uint32_t key, void *data)
4112{
4113 struct weston_compositor *compositor = data;
4114
4115 if (weston_timeline_enabled_)
4116 weston_timeline_close();
4117 else
4118 weston_timeline_open(compositor);
4119}
4120
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004121WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004122weston_compositor_init(struct weston_compositor *ec,
4123 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004124 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004125 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004126{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004127 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004128 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004129 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004130
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004131 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004132 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004133 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004134 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004135 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004136 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004137 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004138 wl_signal_init(&ec->idle_signal);
4139 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004140 wl_signal_init(&ec->show_input_panel_signal);
4141 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004142 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004143 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004144 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004145 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004146 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004147 wl_signal_init(&ec->session_signal);
4148 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004149
Casey Dahlin58ba1372012-04-19 22:50:08 -04004150 ec->output_id_pool = 0;
4151
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004152 if (!wl_global_create(display, &wl_compositor_interface, 3,
4153 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004154 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004155
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004156 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4157 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004158 return -1;
4159
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004160 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004161 ec, bind_scaler))
4162 return -1;
4163
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004164 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4165 ec, bind_presentation))
4166 return -1;
4167
Jason Ekstranda7af7042013-10-12 22:38:11 -05004168 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004169 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004170 wl_list_init(&ec->layer_list);
4171 wl_list_init(&ec->seat_list);
4172 wl_list_init(&ec->output_list);
4173 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004174 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004175 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004176 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004177 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004178 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004179
Xiong Zhang97116532013-10-23 13:58:31 +08004180 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004181 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004182
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004183 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4184 weston_config_section_get_string(s, "keymap_rules",
4185 (char **) &xkb_names.rules, NULL);
4186 weston_config_section_get_string(s, "keymap_model",
4187 (char **) &xkb_names.model, NULL);
4188 weston_config_section_get_string(s, "keymap_layout",
4189 (char **) &xkb_names.layout, NULL);
4190 weston_config_section_get_string(s, "keymap_variant",
4191 (char **) &xkb_names.variant, NULL);
4192 weston_config_section_get_string(s, "keymap_options",
4193 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004194
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004195 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4196 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004197
Jonny Lamb66a41a02014-08-12 14:58:25 +02004198 weston_config_section_get_int(s, "repeat-rate",
4199 &ec->kb_repeat_rate, 40);
4200 weston_config_section_get_int(s, "repeat-delay",
4201 &ec->kb_repeat_delay, 400);
4202
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004203 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004204
4205 wl_data_device_manager_init(ec->wl_display);
4206
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004207 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004208
Daniel Stone725c2c32012-06-22 14:04:36 +01004209 loop = wl_display_get_event_loop(ec->wl_display);
4210 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4211 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4212
4213 ec->input_loop = wl_event_loop_create();
4214
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004215 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4216 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4217
Pekka Paalanenb5026542014-11-12 15:09:24 +02004218 weston_compositor_add_debug_binding(ec, KEY_T,
4219 timeline_key_binding_handler, ec);
4220
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004221 weston_compositor_schedule_repaint(ec);
4222
Daniel Stone725c2c32012-06-22 14:04:36 +01004223 return 0;
4224}
4225
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004226WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004227weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004228{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004229 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004230
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004231 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004232 if (ec->input_loop_source)
4233 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004234
Matt Roper361d2ad2011-08-29 13:52:23 -07004235 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004236 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004237 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004238
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004239 if (ec->renderer)
4240 ec->renderer->destroy(ec);
4241
Daniel Stone325fc2d2012-05-30 16:31:58 +01004242 weston_binding_list_destroy_all(&ec->key_binding_list);
4243 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004244 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004245 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004246 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004247
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004248 weston_plane_release(&ec->primary_plane);
4249
Jonas Ådahlc97af922012-03-28 22:36:09 +02004250 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004251
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004252 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004253}
4254
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004255WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004256weston_compositor_exit_with_code(struct weston_compositor *compositor,
4257 int exit_code)
4258{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004259 if (compositor->exit_code == EXIT_SUCCESS)
4260 compositor->exit_code = exit_code;
4261
Frederic Plourdec336f062014-10-29 14:44:33 -04004262 wl_display_terminate(compositor->wl_display);
4263}
4264
4265WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004266weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4267 const struct weston_pointer_grab_interface *interface)
4268{
4269 struct weston_seat *seat;
4270
4271 ec->default_pointer_grab = interface;
4272 wl_list_for_each(seat, &ec->seat_list, link) {
4273 if (seat->pointer) {
4274 weston_pointer_set_default_grab(seat->pointer,
4275 interface);
4276 }
4277 }
4278}
4279
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004280WL_EXPORT int
4281weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4282 clockid_t clk_id)
4283{
4284 struct timespec ts;
4285
4286 if (clock_gettime(clk_id, &ts) < 0)
4287 return -1;
4288
4289 compositor->presentation_clock = clk_id;
4290
4291 return 0;
4292}
4293
4294/*
4295 * For choosing the software clock, when the display hardware or API
4296 * does not expose a compatible presentation timestamp.
4297 */
4298WL_EXPORT int
4299weston_compositor_set_presentation_clock_software(
4300 struct weston_compositor *compositor)
4301{
4302 /* In order of preference */
4303 static const clockid_t clocks[] = {
4304 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4305 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4306 CLOCK_MONOTONIC, /* no jumps, may crawl */
4307 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4308 CLOCK_REALTIME /* may jump and crawl */
4309 };
4310 unsigned i;
4311
4312 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4313 if (weston_compositor_set_presentation_clock(compositor,
4314 clocks[i]) == 0)
4315 return 0;
4316
4317 weston_log("Error: no suitable presentation clock available.\n");
4318
4319 return -1;
4320}
4321
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004322WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004323weston_version(int *major, int *minor, int *micro)
4324{
4325 *major = WESTON_VERSION_MAJOR;
4326 *minor = WESTON_VERSION_MINOR;
4327 *micro = WESTON_VERSION_MICRO;
4328}
4329
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004330static const char *
4331clock_name(clockid_t clk_id)
4332{
4333 static const char *names[] = {
4334 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4335 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4336 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4337 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4338 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4339 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4340 };
4341
4342 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4343 return "unknown";
4344
4345 return names[clk_id];
4346}
4347
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004348static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004349 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004350 const char *desc;
4351} capability_strings[] = {
4352 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004353 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004354};
4355
4356static void
4357weston_compositor_log_capabilities(struct weston_compositor *compositor)
4358{
4359 unsigned i;
4360 int yes;
4361
4362 weston_log("Compositor capabilities:\n");
4363 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4364 yes = compositor->capabilities & capability_strings[i].bit;
4365 weston_log_continue(STAMP_SPACE "%s %s\n",
4366 capability_strings[i].desc,
4367 yes ? "yes" : "no");
4368 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004369
4370 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4371 clock_name(compositor->presentation_clock),
4372 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004373}
4374
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004375static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004376{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004377 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004378
Martin Minarik6d118362012-06-07 18:01:59 +02004379 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004380 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004381
4382 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004383}
4384
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004385#ifdef HAVE_LIBUNWIND
4386
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004387static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004388print_backtrace(void)
4389{
4390 unw_cursor_t cursor;
4391 unw_context_t context;
4392 unw_word_t off;
4393 unw_proc_info_t pip;
4394 int ret, i = 0;
4395 char procname[256];
4396 const char *filename;
4397 Dl_info dlinfo;
4398
4399 pip.unwind_info = NULL;
4400 ret = unw_getcontext(&context);
4401 if (ret) {
4402 weston_log("unw_getcontext: %d\n", ret);
4403 return;
4404 }
4405
4406 ret = unw_init_local(&cursor, &context);
4407 if (ret) {
4408 weston_log("unw_init_local: %d\n", ret);
4409 return;
4410 }
4411
4412 ret = unw_step(&cursor);
4413 while (ret > 0) {
4414 ret = unw_get_proc_info(&cursor, &pip);
4415 if (ret) {
4416 weston_log("unw_get_proc_info: %d\n", ret);
4417 break;
4418 }
4419
4420 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4421 if (ret && ret != -UNW_ENOMEM) {
4422 if (ret != -UNW_EUNSPEC)
4423 weston_log("unw_get_proc_name: %d\n", ret);
4424 procname[0] = '?';
4425 procname[1] = 0;
4426 }
4427
4428 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4429 *dlinfo.dli_fname)
4430 filename = dlinfo.dli_fname;
4431 else
4432 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004433
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004434 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4435 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4436
4437 ret = unw_step(&cursor);
4438 if (ret < 0)
4439 weston_log("unw_step: %d\n", ret);
4440 }
4441}
4442
4443#else
4444
4445static void
4446print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004447{
4448 void *buffer[32];
4449 int i, count;
4450 Dl_info info;
4451
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004452 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4453 for (i = 0; i < count; i++) {
4454 dladdr(buffer[i], &info);
4455 weston_log(" [%016lx] %s (%s)\n",
4456 (long) buffer[i],
4457 info.dli_sname ? info.dli_sname : "--",
4458 info.dli_fname);
4459 }
4460}
4461
4462#endif
4463
4464static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004465on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004466{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004467 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004468 * then call the backend restore function, which will switch
4469 * back to the vt we launched from or ungrab X etc and then
4470 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004471 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004472 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004473 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004474
Peter Maatmane5b42e42013-03-27 22:38:53 +01004475 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004476
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004477 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004478
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004479 segv_compositor->restore(segv_compositor);
4480
4481 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004482}
4483
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004484WL_EXPORT void *
4485weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004486{
4487 char path[PATH_MAX];
4488 void *module, *init;
4489
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004490 if (name == NULL)
4491 return NULL;
4492
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004493 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004494 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004495 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004496 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004497
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004498 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4499 if (module) {
4500 weston_log("Module '%s' already loaded\n", path);
4501 dlclose(module);
4502 return NULL;
4503 }
4504
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004505 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004506 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004507 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004508 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004509 return NULL;
4510 }
4511
4512 init = dlsym(module, entrypoint);
4513 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004514 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004515 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004516 return NULL;
4517 }
4518
4519 return init;
4520}
4521
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004522static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004523load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004524 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004525{
4526 const char *p, *end;
4527 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004528 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004529 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004530
4531 if (modules == NULL)
4532 return 0;
4533
4534 p = modules;
4535 while (*p) {
4536 end = strchrnul(p, ',');
4537 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004538 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004539 if (!module_init)
4540 return -1;
4541 if (module_init(ec, argc, argv) < 0)
4542 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004543 p = end;
4544 while (*p == ',')
4545 p++;
4546
4547 }
4548
4549 return 0;
4550}
4551
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004552static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004553 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4554
4555static const char xdg_wrong_message[] =
4556 "fatal: environment variable XDG_RUNTIME_DIR\n"
4557 "is set to \"%s\", which is not a directory.\n";
4558
4559static const char xdg_wrong_mode_message[] =
4560 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004561 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4562 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004563
4564static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004565 "Refer to your distribution on how to get it, or\n"
4566 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4567 "on how to implement it.\n";
4568
Martin Minarik37032f82012-06-18 20:15:18 +02004569static void
4570verify_xdg_runtime_dir(void)
4571{
4572 char *dir = getenv("XDG_RUNTIME_DIR");
4573 struct stat s;
4574
4575 if (!dir) {
4576 weston_log(xdg_error_message);
4577 weston_log_continue(xdg_detail_message);
4578 exit(EXIT_FAILURE);
4579 }
4580
4581 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4582 weston_log(xdg_wrong_message, dir);
4583 weston_log_continue(xdg_detail_message);
4584 exit(EXIT_FAILURE);
4585 }
4586
4587 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4588 weston_log(xdg_wrong_mode_message,
4589 dir, s.st_mode & 0777, s.st_uid);
4590 weston_log_continue(xdg_detail_message);
4591 }
4592}
4593
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004594static int
4595usage(int error_code)
4596{
4597 fprintf(stderr,
4598 "Usage: weston [OPTIONS]\n\n"
4599 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4600 "Weston supports multiple backends, and depending on which backend is in use\n"
4601 "different options will be accepted.\n\n"
4602
4603
4604 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004605 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004606 " -B, --backend=MODULE\tBackend module, one of\n"
4607#if defined(BUILD_DRM_COMPOSITOR)
4608 "\t\t\t\tdrm-backend.so\n"
4609#endif
4610#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004611 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004612#endif
4613#if defined(BUILD_X11_COMPOSITOR)
4614 "\t\t\t\tx11-backend.so\n"
4615#endif
4616#if defined(BUILD_WAYLAND_COMPOSITOR)
4617 "\t\t\t\twayland-backend.so\n"
4618#endif
4619#if defined(BUILD_RDP_COMPOSITOR)
4620 "\t\t\t\trdp-backend.so\n"
4621#endif
4622#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4623 "\t\t\t\trpi-backend.so\n"
4624#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004625 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004626 " -S, --socket=NAME\tName of socket to listen on\n"
4627 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004628 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004629 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004630 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004631 " -h, --help\t\tThis help message\n\n");
4632
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004633#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004634 fprintf(stderr,
4635 "Options for drm-backend.so:\n\n"
4636 " --connector=ID\tBring up only this connector\n"
4637 " --seat=SEAT\t\tThe seat that weston should run on\n"
4638 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004639 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004640 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004641#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004642
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004643#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004644 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004645 "Options for fbdev-backend.so:\n\n"
4646 " --tty=TTY\t\tThe tty to use\n"
4647 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004648#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004649
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004650#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004651 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004652 "Options for x11-backend.so:\n\n"
4653 " --width=WIDTH\t\tWidth of X window\n"
4654 " --height=HEIGHT\tHeight of X window\n"
4655 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004656 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004657 " --output-count=COUNT\tCreate multiple outputs\n"
4658 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004659#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004660
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004661#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004662 fprintf(stderr,
4663 "Options for wayland-backend.so:\n\n"
4664 " --width=WIDTH\t\tWidth of Wayland surface\n"
4665 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004666 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004667 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004668 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004669 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004670 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004671 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004672#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004673
Pekka Paalanene31e0532013-05-22 18:03:07 +03004674#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4675 fprintf(stderr,
4676 "Options for rpi-backend.so:\n\n"
4677 " --tty=TTY\t\tThe tty to use\n"
4678 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4679 " --transform=TR\tThe output transformation, TR is one of:\n"
4680 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004681 " --opaque-regions\tEnable support for opaque regions, can be "
4682 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004683 "\n");
4684#endif
4685
Hardeningc077a842013-07-08 00:51:35 +02004686#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004687 fprintf(stderr,
4688 "Options for rdp-backend.so:\n\n"
4689 " --width=WIDTH\t\tWidth of desktop\n"
4690 " --height=HEIGHT\tHeight of desktop\n"
4691 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4692 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004693 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004694 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4695 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4696 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4697 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4698 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004699#endif
4700
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004701#if defined(BUILD_HEADLESS_COMPOSITOR)
4702 fprintf(stderr,
4703 "Options for headless-backend.so:\n\n"
4704 " --width=WIDTH\t\tWidth of memory surface\n"
4705 " --height=HEIGHT\tHeight of memory surface\n"
4706 " --transform=TR\tThe output transformation, TR is one of:\n"
4707 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4708 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4709#endif
4710
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004711 exit(error_code);
4712}
4713
Peter Maatmane5b42e42013-03-27 22:38:53 +01004714static void
4715catch_signals(void)
4716{
4717 struct sigaction action;
4718
4719 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4720 action.sa_sigaction = on_caught_signal;
4721 sigemptyset(&action.sa_mask);
4722 sigaction(SIGSEGV, &action, NULL);
4723 sigaction(SIGABRT, &action, NULL);
4724}
4725
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004726static void
4727handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4728{
4729 struct wl_client *client = data;
4730
4731 weston_log("Primary client died. Closing...\n");
4732
4733 wl_display_terminate(wl_client_get_display(client));
4734}
4735
Ryo Munakatad8deff62014-09-06 07:32:05 +09004736static char *
4737weston_choose_default_backend(void)
4738{
4739 char *backend = NULL;
4740
4741 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4742 backend = strdup("wayland-backend.so");
4743 else if (getenv("DISPLAY"))
4744 backend = strdup("x11-backend.so");
4745 else
4746 backend = strdup(WESTON_NATIVE_BACKEND);
4747
4748 return backend;
4749}
4750
4751static int
4752weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4753{
4754 if (socket_name) {
4755 if (wl_display_add_socket(display, socket_name)) {
4756 weston_log("fatal: failed to add socket: %m\n");
4757 return -1;
4758 }
4759 } else {
4760 socket_name = wl_display_add_socket_auto(display);
4761 if (!socket_name) {
4762 weston_log("fatal: failed to add socket: %m\n");
4763 return -1;
4764 }
4765 }
4766
4767 setenv("WAYLAND_DISPLAY", socket_name, 1);
4768
4769 return 0;
4770}
4771
Derek Foreman64a3df02014-10-23 12:24:18 -05004772static const struct { const char *name; uint32_t token; } transforms[] = {
4773 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4774 { "90", WL_OUTPUT_TRANSFORM_90 },
4775 { "180", WL_OUTPUT_TRANSFORM_180 },
4776 { "270", WL_OUTPUT_TRANSFORM_270 },
4777 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4778 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4779 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4780 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4781};
4782
4783WL_EXPORT int
4784weston_parse_transform(const char *transform, uint32_t *out)
4785{
4786 unsigned int i;
4787
4788 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4789 if (strcmp(transforms[i].name, transform) == 0) {
4790 *out = transforms[i].token;
4791 return 0;
4792 }
4793
4794 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4795 return -1;
4796}
4797
4798WL_EXPORT const char *
4799weston_transform_to_string(uint32_t output_transform)
4800{
4801 unsigned int i;
4802
4803 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4804 if (transforms[i].token == output_transform)
4805 return transforms[i].name;
4806
4807 return "<illegal value>";
4808}
4809
4810
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004811int main(int argc, char *argv[])
4812{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004813 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004814 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004815 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004816 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004817 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004818 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004819 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004820 int *argc, char *argv[],
4821 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004822 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004823 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004824 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004825 char *modules = NULL;
4826 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004827 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004828 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004829 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004830 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004831 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004832 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004833 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004834 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004835 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004836 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004837 struct wl_client *primary_client;
4838 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004839 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004840
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004841 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004842 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4843 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004844 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4845 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004846 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004847 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004848 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004849 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004850 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004851 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004852
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004853 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004854
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004855 if (help)
4856 usage(EXIT_SUCCESS);
4857
Scott Moreau12245142012-08-29 15:15:58 -06004858 if (version) {
4859 printf(PACKAGE_STRING "\n");
4860 return EXIT_SUCCESS;
4861 }
4862
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004863 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004864
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004865 weston_log("%s\n"
4866 STAMP_SPACE "%s\n"
4867 STAMP_SPACE "Bug reports to: %s\n"
4868 STAMP_SPACE "Build: %s\n",
4869 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004870 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004871 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004872
Martin Minarik37032f82012-06-18 20:15:18 +02004873 verify_xdg_runtime_dir();
4874
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004875 display = wl_display_create();
4876
Tiago Vignatti2116b892011-08-08 05:52:59 -07004877 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004878 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4879 display);
4880 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4881 display);
4882 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4883 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004884
4885 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004886 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4887 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004888
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304889 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4890 ret = EXIT_FAILURE;
4891 goto out_signals;
4892 }
4893
Pekka Paalanen588bee12014-05-07 16:26:25 +03004894 if (noconfig == 0)
4895 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004896 if (config != NULL) {
4897 weston_log("Using config file '%s'\n",
4898 weston_config_get_full_path(config));
4899 } else {
4900 weston_log("Starting with no config file.\n");
4901 }
4902 section = weston_config_get_section(config, "core", NULL, NULL);
4903
Ryo Munakata03faed22014-09-06 07:32:51 +09004904 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004905 weston_config_section_get_string(section, "backend", &backend,
4906 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004907 if (!backend)
4908 backend = weston_choose_default_backend();
4909 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004910
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004911 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304912 if (!backend_init) {
4913 ret = EXIT_FAILURE;
4914 goto out_signals;
4915 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004916
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004917 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004918 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004919 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304920 ret = EXIT_FAILURE;
4921 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004922 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004923
Peter Maatmane5b42e42013-03-27 22:38:53 +01004924 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004925 segv_compositor = ec;
4926
Frederic Plourde4a84c832014-10-30 15:06:34 -04004927 if (idle_time < 0)
4928 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4929 if (idle_time < 0)
4930 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004931 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004932 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004933 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004934
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004935 for (i = 1; i < argc; i++)
4936 weston_log("fatal: unhandled option: %s\n", argv[i]);
4937 if (argc > 1) {
4938 ret = EXIT_FAILURE;
4939 goto out;
4940 }
4941
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004942 weston_compositor_log_capabilities(ec);
4943
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004944 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4945 if (server_socket) {
4946 weston_log("Running with single client\n");
4947 fd = strtol(server_socket, &end, 0);
4948 if (*end != '\0')
4949 fd = -1;
4950 } else {
4951 fd = -1;
4952 }
4953
4954 if (fd != -1) {
4955 primary_client = wl_client_create(display, fd);
4956 if (!primary_client) {
4957 weston_log("fatal: failed to add client: %m\n");
4958 ret = EXIT_FAILURE;
4959 goto out;
4960 }
4961 primary_client_destroyed.notify =
4962 handle_primary_client_destroyed;
4963 wl_client_add_destroy_listener(primary_client,
4964 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004965 } else if (weston_create_listening_socket(display, socket_name)) {
4966 ret = EXIT_FAILURE;
4967 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004968 }
4969
Ryo Munakata03faed22014-09-06 07:32:51 +09004970 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004971 weston_config_section_get_string(section, "shell", &shell,
4972 "desktop-shell.so");
4973
Ryo Munakata03faed22014-09-06 07:32:51 +09004974 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004975 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004976
4977 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004978 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004979 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004980
4981 if (load_modules(ec, option_modules, &argc, argv) < 0)
4982 goto out;
4983
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004984 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4985 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4986 if (numlock_on) {
4987 wl_list_for_each(seat, &ec->seat_list, link) {
4988 if (seat->keyboard)
4989 weston_keyboard_set_locks(seat->keyboard,
4990 WESTON_NUM_LOCK,
4991 WESTON_NUM_LOCK);
4992 }
4993 }
4994
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004995 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004996
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004997 wl_display_run(display);
4998
Frederic Plourdec336f062014-10-29 14:44:33 -04004999 /* Allow for setting return exit code after
5000 * wl_display_run returns normally. This is
5001 * useful for devs/testers and automated tests
5002 * that want to indicate failure status to
5003 * testing infrastructure above
5004 */
5005 ret = ec->exit_code;
5006
Ryo Munakata03faed22014-09-06 07:32:51 +09005007out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005008 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01005009 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02005010
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04005011 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02005012
Daniel Stone855028f2012-05-01 20:37:10 +01005013 weston_compositor_xkb_destroy(ec);
5014
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005015 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05305016
5017out_signals:
5018 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
5019 if (signals[i])
5020 wl_event_source_remove(signals[i]);
5021
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005022 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005023
Martin Minarik19e6f262012-06-07 13:08:46 +02005024 weston_log_file_close();
5025
Ryo Munakata03faed22014-09-06 07:32:51 +09005026 free(backend);
5027 free(shell);
5028 free(socket_name);
5029 free(option_modules);
5030 free(log);
5031 free(modules);
5032
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005033 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005034}