blob: c2c975d53e5355510ccf496d60e5a7a44cbb2b7f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004 * Copyright © 2012-2014 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Pekka Paalanenb5026542014-11-12 15:09:24 +020056#include "timeline.h"
57
Kristian Høgsberg82863022010-06-04 21:52:02 -040058#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010059#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040060#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030061#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040062#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050063#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050064
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040066static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067
68static int
69sigchld_handler(int signal_number, void *data)
70{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050071 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 int status;
73 pid_t pid;
74
Pekka Paalanen23ade622014-08-27 13:31:26 +030075 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
76 wl_list_for_each(p, &child_process_list, link) {
77 if (p->pid == pid)
78 break;
79 }
Bill Spitzak027b9622012-03-17 15:22:03 -070080
Pekka Paalanen23ade622014-08-27 13:31:26 +030081 if (&p->link == &child_process_list) {
82 weston_log("unknown child process exited\n");
83 continue;
84 }
85
86 wl_list_remove(&p->link);
87 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040088 }
89
Pekka Paalanen23ade622014-08-27 13:31:26 +030090 if (pid < 0 && errno != ECHILD)
91 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040092
93 return 1;
94}
95
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096static void
Alexander Larsson0b135062013-05-28 16:23:36 +020097weston_output_transform_scale_init(struct weston_output *output,
98 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020099
Rob Bradford27b17932013-06-26 18:08:46 +0100100static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500101weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100102
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600103static void weston_mode_switch_finish(struct weston_output *output,
104 int mode_changed,
105 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800106{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200108 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600110 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200111
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200112 pixman_region32_init(&old_output_region);
113 pixman_region32_copy(&old_output_region, &output->region);
114
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200116 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
118 pixman_region32_init(&output->previous_damage);
119 pixman_region32_init_rect(&output->region, output->x, output->y,
120 output->width, output->height);
121
122 weston_output_update_matrix(output);
123
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200124 /* If a pointer falls outside the outputs new geometry, move it to its
125 * lower-right corner */
126 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400127 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200128 int32_t x, y;
129
130 if (!pointer)
131 continue;
132
133 x = wl_fixed_to_int(pointer->x);
134 y = wl_fixed_to_int(pointer->y);
135
136 if (!pixman_region32_contains_point(&old_output_region,
137 x, y, NULL) ||
138 pixman_region32_contains_point(&output->region,
139 x, y, NULL))
140 continue;
141
142 if (x >= output->x + output->width)
143 x = output->x + output->width - 1;
144 if (y >= output->y + output->height)
145 y = output->y + output->height - 1;
146
147 pointer->x = wl_fixed_from_int(x);
148 pointer->y = wl_fixed_from_int(y);
149 }
150
151 pixman_region32_fini(&old_output_region);
152
Derek Foremandd4cd332014-11-10 10:29:59 -0600153 if (!mode_changed && !scale_changed)
154 return;
155
Hardening57388e42013-09-18 23:56:36 +0200156 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600157 wl_resource_for_each(resource, &output->resource_list) {
158 if (mode_changed) {
159 wl_output_send_mode(resource,
160 output->current_mode->flags,
161 output->current_mode->width,
162 output->current_mode->height,
163 output->current_mode->refresh);
164 }
Hardening57388e42013-09-18 23:56:36 +0200165
Derek Foreman41bdc272014-11-05 13:26:57 -0600166 version = wl_resource_get_version(resource);
167 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600168 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200169
Derek Foreman41bdc272014-11-05 13:26:57 -0600170 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
171 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600172 }
173}
174
175WL_EXPORT int
176weston_output_mode_set_native(struct weston_output *output,
177 struct weston_mode *mode,
178 int32_t scale)
179{
180 int ret;
181 int mode_changed = 0, scale_changed = 0;
182
183 if (!output->switch_mode)
184 return -1;
185
186 if (!output->original_mode) {
187 mode_changed = 1;
188 ret = output->switch_mode(output, mode);
189 if (ret < 0)
190 return ret;
191 if (output->current_scale != scale) {
192 scale_changed = 1;
193 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200194 }
195 }
196
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600197 output->native_mode = mode;
198 output->native_scale = scale;
199
200 weston_mode_switch_finish(output, mode_changed, scale_changed);
201
202 return 0;
203}
204
205WL_EXPORT int
206weston_output_mode_switch_to_native(struct weston_output *output)
207{
208 int ret;
209 int mode_changed = 0, scale_changed = 0;
210
211 if (!output->switch_mode)
212 return -1;
213
214 if (!output->original_mode) {
215 weston_log("already in the native mode\n");
216 return -1;
217 }
218 /* the non fullscreen clients haven't seen a mode set since we
219 * switched into a temporary, so we need to notify them if the
220 * mode at that time is different from the native mode now.
221 */
222 mode_changed = (output->original_mode != output->native_mode);
223 scale_changed = (output->original_scale != output->native_scale);
224
225 ret = output->switch_mode(output, output->native_mode);
226 if (ret < 0)
227 return ret;
228
229 output->current_scale = output->native_scale;
230
231 output->original_mode = NULL;
232 output->original_scale = 0;
233
234 weston_mode_switch_finish(output, mode_changed, scale_changed);
235
236 return 0;
237}
238
239WL_EXPORT int
240weston_output_mode_switch_to_temporary(struct weston_output *output,
241 struct weston_mode *mode,
242 int32_t scale)
243{
244 int ret;
245
246 if (!output->switch_mode)
247 return -1;
248
249 /* original_mode is the last mode non full screen clients have seen,
250 * so we shouldn't change it if we already have one set.
251 */
252 if (!output->original_mode) {
253 output->original_mode = output->native_mode;
254 output->original_scale = output->native_scale;
255 }
256 ret = output->switch_mode(output, mode);
257 if (ret < 0)
258 return ret;
259
260 output->current_scale = scale;
261
262 weston_mode_switch_finish(output, 0, 0);
263
264 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800265}
266
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500268weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400269{
270 wl_list_insert(&child_process_list, &process->link);
271}
272
Benjamin Franzke06286262011-05-06 19:12:33 +0200273static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274child_client_exec(int sockfd, const char *path)
275{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500276 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200278 sigset_t allsigs;
279
280 /* do not give our signal mask to the new process */
281 sigfillset(&allsigs);
282 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100284 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
285 if (seteuid(getuid()) == -1) {
286 weston_log("compositor: failed seteuid\n");
287 return;
288 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500289
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
291 * non-CLOEXEC fd to pass through exec. */
292 clientfd = dup(sockfd);
293 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500295 return;
296 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500298 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200299 setenv("WAYLAND_SOCKET", s, 1);
300
301 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200302 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 path);
304}
305
306WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500307weston_client_launch(struct weston_compositor *compositor,
308 struct weston_process *proc,
309 const char *path,
310 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311{
312 int sv[2];
313 pid_t pid;
314 struct wl_client *client;
315
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300316 weston_log("launching '%s'\n", path);
317
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300318 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200319 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320 "socketpair failed while launching '%s': %m\n",
321 path);
322 return NULL;
323 }
324
325 pid = fork();
326 if (pid == -1) {
327 close(sv[0]);
328 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 "fork failed while launching '%s': %m\n", path);
331 return NULL;
332 }
333
334 if (pid == 0) {
335 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700336 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200337 }
338
339 close(sv[1]);
340
341 client = wl_client_create(compositor->wl_display, sv[0]);
342 if (!client) {
343 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200344 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200345 "wl_client_create failed while launching '%s'.\n",
346 path);
347 return NULL;
348 }
349
350 proc->pid = pid;
351 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500352 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200353
354 return client;
355}
356
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300357struct process_info {
358 struct weston_process proc;
359 char *path;
360};
361
362static void
363process_handle_sigchld(struct weston_process *process, int status)
364{
365 struct process_info *pinfo =
366 container_of(process, struct process_info, proc);
367
368 /*
369 * There are no guarantees whether this runs before or after
370 * the wl_client destructor.
371 */
372
373 if (WIFEXITED(status)) {
374 weston_log("%s exited with status %d\n", pinfo->path,
375 WEXITSTATUS(status));
376 } else if (WIFSIGNALED(status)) {
377 weston_log("%s died on signal %d\n", pinfo->path,
378 WTERMSIG(status));
379 } else {
380 weston_log("%s disappeared\n", pinfo->path);
381 }
382
383 free(pinfo->path);
384 free(pinfo);
385}
386
387WL_EXPORT struct wl_client *
388weston_client_start(struct weston_compositor *compositor, const char *path)
389{
390 struct process_info *pinfo;
391 struct wl_client *client;
392
393 pinfo = zalloc(sizeof *pinfo);
394 if (!pinfo)
395 return NULL;
396
397 pinfo->path = strdup(path);
398 if (!pinfo->path)
399 goto out_free;
400
401 client = weston_client_launch(compositor, &pinfo->proc, path,
402 process_handle_sigchld);
403 if (!client)
404 goto out_str;
405
406 return client;
407
408out_str:
409 free(pinfo->path);
410
411out_free:
412 free(pinfo);
413
414 return NULL;
415}
416
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200417static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300418region_init_infinite(pixman_region32_t *region)
419{
420 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
421 UINT32_MAX, UINT32_MAX);
422}
423
Pekka Paalanene67858b2013-04-25 13:57:42 +0300424static struct weston_subsurface *
425weston_surface_to_subsurface(struct weston_surface *surface);
426
Jason Ekstranda7af7042013-10-12 22:38:11 -0500427WL_EXPORT struct weston_view *
428weston_view_create(struct weston_surface *surface)
429{
430 struct weston_view *view;
431
Bryce Harringtonde16d892014-11-20 22:21:57 -0800432 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 if (view == NULL)
434 return NULL;
435
436 view->surface = surface;
437
Jason Ekstranda7af7042013-10-12 22:38:11 -0500438 /* Assign to surface */
439 wl_list_insert(&surface->views, &view->surface_link);
440
441 wl_signal_init(&view->destroy_signal);
442 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300443 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300446 pixman_region32_init(&view->transform.masked_boundingbox);
447 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448
449 view->alpha = 1.0;
450 pixman_region32_init(&view->transform.opaque);
451
452 wl_list_init(&view->geometry.transformation_list);
453 wl_list_insert(&view->geometry.transformation_list,
454 &view->transform.position.link);
455 weston_matrix_init(&view->transform.position.matrix);
456 wl_list_init(&view->geometry.child_list);
457 pixman_region32_init(&view->transform.boundingbox);
458 view->transform.dirty = 1;
459
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 return view;
461}
462
Jason Ekstrand108865d2014-06-26 10:04:49 -0700463struct weston_frame_callback {
464 struct wl_resource *resource;
465 struct wl_list link;
466};
467
Pekka Paalanen133e4392014-09-23 22:08:46 -0400468struct weston_presentation_feedback {
469 struct wl_resource *resource;
470
471 /* XXX: could use just wl_resource_get_link() instead */
472 struct wl_list link;
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200473
474 /* The per-surface feedback flags */
475 uint32_t psf_flags;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400476};
477
478static void
479weston_presentation_feedback_discard(
480 struct weston_presentation_feedback *feedback)
481{
482 presentation_feedback_send_discarded(feedback->resource);
483 wl_resource_destroy(feedback->resource);
484}
485
486static void
487weston_presentation_feedback_discard_list(struct wl_list *list)
488{
489 struct weston_presentation_feedback *feedback, *tmp;
490
491 wl_list_for_each_safe(feedback, tmp, list, link)
492 weston_presentation_feedback_discard(feedback);
493}
494
495static void
496weston_presentation_feedback_present(
497 struct weston_presentation_feedback *feedback,
498 struct weston_output *output,
499 uint32_t refresh_nsec,
500 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200501 uint64_t seq,
502 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400503{
504 struct wl_client *client = wl_resource_get_client(feedback->resource);
505 struct wl_resource *o;
506 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400507
508 wl_resource_for_each(o, &output->resource_list) {
509 if (wl_resource_get_client(o) != client)
510 continue;
511
512 presentation_feedback_send_sync_output(feedback->resource, o);
513 }
514
515 secs = ts->tv_sec;
516 presentation_feedback_send_presented(feedback->resource,
517 secs >> 32, secs & 0xffffffff,
518 ts->tv_nsec,
519 refresh_nsec,
520 seq >> 32, seq & 0xffffffff,
Pekka Paalanenbf0e0312014-12-17 16:20:41 +0200521 flags | feedback->psf_flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400522 wl_resource_destroy(feedback->resource);
523}
524
525static void
526weston_presentation_feedback_present_list(struct wl_list *list,
527 struct weston_output *output,
528 uint32_t refresh_nsec,
529 const struct timespec *ts,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200530 uint64_t seq,
531 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400532{
533 struct weston_presentation_feedback *feedback, *tmp;
534
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200535 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
536 wl_list_empty(list));
537
Pekka Paalanen133e4392014-09-23 22:08:46 -0400538 wl_list_for_each_safe(feedback, tmp, list, link)
539 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200540 refresh_nsec, ts, seq,
541 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400542}
543
Jason Ekstrand7b982072014-05-20 14:33:03 -0500544static void
545surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
546{
547 struct weston_surface_state *state =
548 container_of(listener, struct weston_surface_state,
549 buffer_destroy_listener);
550
551 state->buffer = NULL;
552}
553
554static void
555weston_surface_state_init(struct weston_surface_state *state)
556{
557 state->newly_attached = 0;
558 state->buffer = NULL;
559 state->buffer_destroy_listener.notify =
560 surface_state_handle_buffer_destroy;
561 state->sx = 0;
562 state->sy = 0;
563
564 pixman_region32_init(&state->damage);
565 pixman_region32_init(&state->opaque);
566 region_init_infinite(&state->input);
567
568 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400569 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500570
571 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
572 state->buffer_viewport.buffer.scale = 1;
573 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
574 state->buffer_viewport.surface.width = -1;
575 state->buffer_viewport.changed = 0;
576}
577
578static void
579weston_surface_state_fini(struct weston_surface_state *state)
580{
581 struct weston_frame_callback *cb, *next;
582
583 wl_list_for_each_safe(cb, next,
584 &state->frame_callback_list, link)
585 wl_resource_destroy(cb->resource);
586
Pekka Paalanen133e4392014-09-23 22:08:46 -0400587 weston_presentation_feedback_discard_list(&state->feedback_list);
588
Jason Ekstrand7b982072014-05-20 14:33:03 -0500589 pixman_region32_fini(&state->input);
590 pixman_region32_fini(&state->opaque);
591 pixman_region32_fini(&state->damage);
592
593 if (state->buffer)
594 wl_list_remove(&state->buffer_destroy_listener.link);
595 state->buffer = NULL;
596}
597
598static void
599weston_surface_state_set_buffer(struct weston_surface_state *state,
600 struct weston_buffer *buffer)
601{
602 if (state->buffer == buffer)
603 return;
604
605 if (state->buffer)
606 wl_list_remove(&state->buffer_destroy_listener.link);
607 state->buffer = buffer;
608 if (state->buffer)
609 wl_signal_add(&state->buffer->destroy_signal,
610 &state->buffer_destroy_listener);
611}
612
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500613WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500614weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500615{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500616 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400617
Bryce Harringtonde16d892014-11-20 22:21:57 -0800618 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400619 if (surface == NULL)
620 return NULL;
621
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500622 wl_signal_init(&surface->destroy_signal);
623
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500624 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200625 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400626
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200627 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
628 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200629 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
630 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500631
632 weston_surface_state_init(&surface->pending);
633
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400634 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500635 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300636 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400637
Jason Ekstranda7af7042013-10-12 22:38:11 -0500638 wl_list_init(&surface->views);
639
640 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400641 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500642
Pekka Paalanene67858b2013-04-25 13:57:42 +0300643 wl_list_init(&surface->subsurface_list);
644 wl_list_init(&surface->subsurface_list_pending);
645
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400646 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500647}
648
Alex Wu8811bf92012-02-28 18:07:54 +0800649WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500650weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200651 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100653 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500654}
655
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400656WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500657weston_view_to_global_float(struct weston_view *view,
658 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200659{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200661 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
662
Jason Ekstranda7af7042013-10-12 22:38:11 -0500663 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200664
665 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200666 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700667 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200668 v.f[3]);
669 *x = 0;
670 *y = 0;
671 return;
672 }
673
674 *x = v.f[0] / v.f[3];
675 *y = v.f[1] / v.f[3];
676 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500677 *x = sx + view->geometry.x;
678 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200679 }
680}
681
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500682WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200683weston_transformed_coord(int width, int height,
684 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200685 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200686 float sx, float sy, float *bx, float *by)
687{
688 switch (transform) {
689 case WL_OUTPUT_TRANSFORM_NORMAL:
690 default:
691 *bx = sx;
692 *by = sy;
693 break;
694 case WL_OUTPUT_TRANSFORM_FLIPPED:
695 *bx = width - sx;
696 *by = sy;
697 break;
698 case WL_OUTPUT_TRANSFORM_90:
699 *bx = height - sy;
700 *by = sx;
701 break;
702 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
703 *bx = height - sy;
704 *by = width - sx;
705 break;
706 case WL_OUTPUT_TRANSFORM_180:
707 *bx = width - sx;
708 *by = height - sy;
709 break;
710 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
711 *bx = sx;
712 *by = height - sy;
713 break;
714 case WL_OUTPUT_TRANSFORM_270:
715 *bx = sy;
716 *by = width - sx;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
719 *bx = sy;
720 *by = sx;
721 break;
722 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200723
724 *bx *= scale;
725 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200726}
727
728WL_EXPORT pixman_box32_t
729weston_transformed_rect(int width, int height,
730 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200731 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200732 pixman_box32_t rect)
733{
734 float x1, x2, y1, y2;
735
736 pixman_box32_t ret;
737
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200740 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741 rect.x2, rect.y2, &x2, &y2);
742
743 if (x1 <= x2) {
744 ret.x1 = x1;
745 ret.x2 = x2;
746 } else {
747 ret.x1 = x2;
748 ret.x2 = x1;
749 }
750
751 if (y1 <= y2) {
752 ret.y1 = y1;
753 ret.y2 = y2;
754 } else {
755 ret.y1 = y2;
756 ret.y2 = y1;
757 }
758
759 return ret;
760}
761
762WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500763weston_transformed_region(int width, int height,
764 enum wl_output_transform transform,
765 int32_t scale,
766 pixman_region32_t *src, pixman_region32_t *dest)
767{
768 pixman_box32_t *src_rects, *dest_rects;
769 int nrects, i;
770
771 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
772 if (src != dest)
773 pixman_region32_copy(dest, src);
774 return;
775 }
776
777 src_rects = pixman_region32_rectangles(src, &nrects);
778 dest_rects = malloc(nrects * sizeof(*dest_rects));
779 if (!dest_rects)
780 return;
781
782 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
783 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
784 } else {
785 for (i = 0; i < nrects; i++) {
786 switch (transform) {
787 default:
788 case WL_OUTPUT_TRANSFORM_NORMAL:
789 dest_rects[i].x1 = src_rects[i].x1;
790 dest_rects[i].y1 = src_rects[i].y1;
791 dest_rects[i].x2 = src_rects[i].x2;
792 dest_rects[i].y2 = src_rects[i].y2;
793 break;
794 case WL_OUTPUT_TRANSFORM_90:
795 dest_rects[i].x1 = height - src_rects[i].y2;
796 dest_rects[i].y1 = src_rects[i].x1;
797 dest_rects[i].x2 = height - src_rects[i].y1;
798 dest_rects[i].y2 = src_rects[i].x2;
799 break;
800 case WL_OUTPUT_TRANSFORM_180:
801 dest_rects[i].x1 = width - src_rects[i].x2;
802 dest_rects[i].y1 = height - src_rects[i].y2;
803 dest_rects[i].x2 = width - src_rects[i].x1;
804 dest_rects[i].y2 = height - src_rects[i].y1;
805 break;
806 case WL_OUTPUT_TRANSFORM_270:
807 dest_rects[i].x1 = src_rects[i].y1;
808 dest_rects[i].y1 = width - src_rects[i].x2;
809 dest_rects[i].x2 = src_rects[i].y2;
810 dest_rects[i].y2 = width - src_rects[i].x1;
811 break;
812 case WL_OUTPUT_TRANSFORM_FLIPPED:
813 dest_rects[i].x1 = width - src_rects[i].x2;
814 dest_rects[i].y1 = src_rects[i].y1;
815 dest_rects[i].x2 = width - src_rects[i].x1;
816 dest_rects[i].y2 = src_rects[i].y2;
817 break;
818 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
819 dest_rects[i].x1 = height - src_rects[i].y2;
820 dest_rects[i].y1 = width - src_rects[i].x2;
821 dest_rects[i].x2 = height - src_rects[i].y1;
822 dest_rects[i].y2 = width - src_rects[i].x1;
823 break;
824 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
825 dest_rects[i].x1 = src_rects[i].x1;
826 dest_rects[i].y1 = height - src_rects[i].y2;
827 dest_rects[i].x2 = src_rects[i].x2;
828 dest_rects[i].y2 = height - src_rects[i].y1;
829 break;
830 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
831 dest_rects[i].x1 = src_rects[i].y1;
832 dest_rects[i].y1 = src_rects[i].x1;
833 dest_rects[i].x2 = src_rects[i].y2;
834 dest_rects[i].y2 = src_rects[i].x2;
835 break;
836 }
837 }
838 }
839
840 if (scale != 1) {
841 for (i = 0; i < nrects; i++) {
842 dest_rects[i].x1 *= scale;
843 dest_rects[i].x2 *= scale;
844 dest_rects[i].y1 *= scale;
845 dest_rects[i].y2 *= scale;
846 }
847 }
848
849 pixman_region32_clear(dest);
850 pixman_region32_init_rects(dest, dest_rects, nrects);
851 free(dest_rects);
852}
853
Jonny Lamb74130762013-11-26 18:19:46 +0100854static void
855scaler_surface_to_buffer(struct weston_surface *surface,
856 float sx, float sy, float *bx, float *by)
857{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200858 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200859 double src_width, src_height;
860 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200861
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200862 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
863 if (vp->surface.width == -1) {
864 *bx = sx;
865 *by = sy;
866 return;
867 }
Jonny Lamb74130762013-11-26 18:19:46 +0100868
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200869 src_x = 0.0;
870 src_y = 0.0;
871 src_width = surface->width_from_buffer;
872 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100873 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200874 src_x = wl_fixed_to_double(vp->buffer.src_x);
875 src_y = wl_fixed_to_double(vp->buffer.src_y);
876 src_width = wl_fixed_to_double(vp->buffer.src_width);
877 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100878 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200879
880 *bx = sx * src_width / surface->width + src_x;
881 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100882}
883
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500884WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200885weston_surface_to_buffer_float(struct weston_surface *surface,
886 float sx, float sy, float *bx, float *by)
887{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200888 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
889
Jonny Lamb74130762013-11-26 18:19:46 +0100890 /* first transform coordinates if the scaler is set */
891 scaler_surface_to_buffer(surface, sx, sy, bx, by);
892
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500893 weston_transformed_coord(surface->width_from_buffer,
894 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200895 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100896 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200897}
898
Alexander Larsson4ea95522013-05-22 14:41:37 +0200899WL_EXPORT void
900weston_surface_to_buffer(struct weston_surface *surface,
901 int sx, int sy, int *bx, int *by)
902{
903 float bxf, byf;
904
Jonny Lamb74130762013-11-26 18:19:46 +0100905 weston_surface_to_buffer_float(surface,
906 sx, sy, &bxf, &byf);
907
Alexander Larsson4ea95522013-05-22 14:41:37 +0200908 *bx = floorf(bxf);
909 *by = floorf(byf);
910}
911
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200912WL_EXPORT pixman_box32_t
913weston_surface_to_buffer_rect(struct weston_surface *surface,
914 pixman_box32_t rect)
915{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200916 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100917 float xf, yf;
918
919 /* first transform box coordinates if the scaler is set */
920 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
921 rect.x1 = floorf(xf);
922 rect.y1 = floorf(yf);
923
924 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
925 rect.x2 = floorf(xf);
926 rect.y2 = floorf(yf);
927
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500928 return weston_transformed_rect(surface->width_from_buffer,
929 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200930 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200931 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200932}
933
934WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500935weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936 struct weston_plane *plane)
937{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400939 return;
940
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 weston_view_damage_below(view);
942 view->plane = plane;
943 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400944}
945
946WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200948{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500949 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200950
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500951 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300952 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800954 if (view->plane)
955 pixman_region32_union(&view->plane->damage,
956 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500957 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800958 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200959}
960
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400961static void
962weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
963{
964 uint32_t different = es->output_mask ^ mask;
965 uint32_t entered = mask & different;
966 uint32_t left = es->output_mask & different;
967 struct weston_output *output;
968 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500969 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400970
971 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500972 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400973 return;
974 if (different == 0)
975 return;
976
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500977 client = wl_resource_get_client(es->resource);
978
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400979 wl_list_for_each(output, &es->compositor->output_list, link) {
980 if (1 << output->id & different)
981 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500982 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400983 client);
984 if (resource == NULL)
985 continue;
986 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500987 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400988 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500989 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400990 }
991}
992
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200993
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400994static void
995weston_surface_assign_output(struct weston_surface *es)
996{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 struct weston_output *new_output;
998 struct weston_view *view;
999 pixman_region32_t region;
1000 uint32_t max, area, mask;
1001 pixman_box32_t *e;
1002
1003 new_output = NULL;
1004 max = 0;
1005 mask = 0;
1006 pixman_region32_init(&region);
1007 wl_list_for_each(view, &es->views, surface_link) {
1008 if (!view->output)
1009 continue;
1010
1011 pixman_region32_intersect(&region, &view->transform.boundingbox,
1012 &view->output->region);
1013
1014 e = pixman_region32_extents(&region);
1015 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1016
1017 mask |= view->output_mask;
1018
1019 if (area >= max) {
1020 new_output = view->output;
1021 max = area;
1022 }
1023 }
1024 pixman_region32_fini(&region);
1025
1026 es->output = new_output;
1027 weston_surface_update_output_mask(es, mask);
1028}
1029
1030static void
1031weston_view_assign_output(struct weston_view *ev)
1032{
1033 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001034 struct weston_output *output, *new_output;
1035 pixman_region32_t region;
1036 uint32_t max, area, mask;
1037 pixman_box32_t *e;
1038
1039 new_output = NULL;
1040 max = 0;
1041 mask = 0;
1042 pixman_region32_init(&region);
1043 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001045 &output->region);
1046
1047 e = pixman_region32_extents(&region);
1048 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1049
1050 if (area > 0)
1051 mask |= 1 << output->id;
1052
1053 if (area >= max) {
1054 new_output = output;
1055 max = area;
1056 }
1057 }
1058 pixman_region32_fini(&region);
1059
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 ev->output = new_output;
1061 ev->output_mask = mask;
1062
1063 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001064}
1065
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001066static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001067view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1068 int32_t width, int32_t height,
1069 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001070{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001071 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1072 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001073 int32_t s[4][2] = {
1074 { sx, sy },
1075 { sx, sy + height },
1076 { sx + width, sy },
1077 { sx + width, sy + height }
1078 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001079 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001080 int i;
1081
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001082 if (width == 0 || height == 0) {
1083 /* avoid rounding empty bbox to 1x1 */
1084 pixman_region32_init(bbox);
1085 return;
1086 }
1087
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001088 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001089 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001090 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001091 if (x < min_x)
1092 min_x = x;
1093 if (x > max_x)
1094 max_x = x;
1095 if (y < min_y)
1096 min_y = y;
1097 if (y > max_y)
1098 max_y = y;
1099 }
1100
Pekka Paalanen219b9822012-02-08 15:38:37 +02001101 int_x = floorf(min_x);
1102 int_y = floorf(min_y);
1103 pixman_region32_init_rect(bbox, int_x, int_y,
1104 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001105}
1106
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001107static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001109{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001111
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001112 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 view->geometry.x = roundf(view->geometry.x);
1114 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001115
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001116 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1118 view->transform.position.matrix.d[12] = view->geometry.x;
1119 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001122
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 view->transform.inverse = view->transform.position.matrix;
1124 view->transform.inverse.d[12] = -view->geometry.x;
1125 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 pixman_region32_init_rect(&view->transform.boundingbox,
1128 view->geometry.x,
1129 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001130 view->surface->width,
1131 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 if (view->alpha == 1.0) {
1134 pixman_region32_copy(&view->transform.opaque,
1135 &view->surface->opaque);
1136 pixman_region32_translate(&view->transform.opaque,
1137 view->geometry.x,
1138 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001139 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001140}
1141
1142static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 struct weston_view *parent = view->geometry.parent;
1146 struct weston_matrix *matrix = &view->transform.matrix;
1147 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001148 struct weston_transform *tform;
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001151
1152 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1154 view->transform.position.matrix.d[12] = view->geometry.x;
1155 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156
1157 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001159 weston_matrix_multiply(matrix, &tform->matrix);
1160
Pekka Paalanen483243f2013-03-08 14:56:50 +02001161 if (parent)
1162 weston_matrix_multiply(matrix, &parent->transform.matrix);
1163
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001164 if (weston_matrix_invert(inverse, matrix) < 0) {
1165 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 weston_log("error: weston_view %p"
1167 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001168 return -1;
1169 }
1170
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001171 view_compute_bbox(view, 0, 0,
1172 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001174
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001175 return 0;
1176}
1177
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001178static struct weston_layer *
1179get_view_layer(struct weston_view *view)
1180{
1181 if (view->parent_view)
1182 return get_view_layer(view->parent_view);
1183 return view->layer_link.layer;
1184}
1185
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001186WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001190 struct weston_layer *layer;
1191 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001192
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001194 return;
1195
Pekka Paalanen483243f2013-03-08 14:56:50 +02001196 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001200
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 pixman_region32_fini(&view->transform.boundingbox);
1204 pixman_region32_fini(&view->transform.opaque);
1205 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001206
Pekka Paalanencd403622012-01-25 13:37:39 +02001207 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 if (view->geometry.transformation_list.next ==
1209 &view->transform.position.link &&
1210 view->geometry.transformation_list.prev ==
1211 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001212 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001214 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 if (weston_view_update_transform_enable(view) < 0)
1216 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001217 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001218
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001219 layer = get_view_layer(view);
1220 if (layer) {
1221 pixman_region32_init_with_extents(&mask, &layer->mask);
1222 pixman_region32_intersect(&view->transform.masked_boundingbox,
1223 &view->transform.boundingbox, &mask);
1224 pixman_region32_intersect(&view->transform.masked_opaque,
1225 &view->transform.opaque, &mask);
1226 pixman_region32_fini(&mask);
1227 }
1228
Jason Ekstranda7af7042013-10-12 22:38:11 -05001229 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 wl_signal_emit(&view->surface->compositor->transform_signal,
1234 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001235}
1236
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001237WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001239{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001241
1242 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001243 * The invariant: if view->geometry.dirty, then all views
1244 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001245 * Corollary: if not parent->geometry.dirty, then all ancestors
1246 * are not dirty.
1247 */
1248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001250 return;
1251
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001253
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001257}
1258
1259WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001260weston_view_to_global_fixed(struct weston_view *view,
1261 wl_fixed_t vx, wl_fixed_t vy,
1262 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001263{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001264 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001265
Jason Ekstranda7af7042013-10-12 22:38:11 -05001266 weston_view_to_global_float(view,
1267 wl_fixed_to_double(vx),
1268 wl_fixed_to_double(vy),
1269 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001270 *x = wl_fixed_from_double(xf);
1271 *y = wl_fixed_from_double(yf);
1272}
1273
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001274WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275weston_view_from_global_float(struct weston_view *view,
1276 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001277{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001279 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001282
1283 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001284 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001286 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 *vx = 0;
1288 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001289 return;
1290 }
1291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 *vx = v.f[0] / v.f[3];
1293 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001294 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 *vx = x - view->geometry.x;
1296 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001297 }
1298}
1299
1300WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301weston_view_from_global_fixed(struct weston_view *view,
1302 wl_fixed_t x, wl_fixed_t y,
1303 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001304{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 weston_view_from_global_float(view,
1308 wl_fixed_to_double(x),
1309 wl_fixed_to_double(y),
1310 &vxf, &vyf);
1311 *vx = wl_fixed_from_double(vxf);
1312 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001313}
1314
1315WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316weston_view_from_global(struct weston_view *view,
1317 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001318{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001320
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1322 *vx = floorf(vxf);
1323 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001324}
1325
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001326WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001327weston_surface_schedule_repaint(struct weston_surface *surface)
1328{
1329 struct weston_output *output;
1330
1331 wl_list_for_each(output, &surface->compositor->output_list, link)
1332 if (surface->output_mask & (1 << output->id))
1333 weston_output_schedule_repaint(output);
1334}
1335
1336WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337weston_view_schedule_repaint(struct weston_view *view)
1338{
1339 struct weston_output *output;
1340
1341 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1342 if (view->output_mask & (1 << output->id))
1343 weston_output_schedule_repaint(output);
1344}
1345
1346WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001347weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001348{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001349 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350 0, 0, surface->width,
1351 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001352
Kristian Høgsberg98238702012-08-03 16:29:12 -04001353 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001354}
1355
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001356WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001358{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001359 if (view->geometry.x == x && view->geometry.y == y)
1360 return;
1361
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 view->geometry.x = x;
1363 view->geometry.y = y;
1364 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001365}
1366
Pekka Paalanen483243f2013-03-08 14:56:50 +02001367static void
1368transform_parent_handle_parent_destroy(struct wl_listener *listener,
1369 void *data)
1370{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 struct weston_view *view =
1372 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001373 geometry.parent_destroy_listener);
1374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001376}
1377
1378WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379weston_view_set_transform_parent(struct weston_view *view,
1380 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001381{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 if (view->geometry.parent) {
1383 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1384 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001385 }
1386
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001390 transform_parent_handle_parent_destroy;
1391 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001392 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001393 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001394 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001396 }
1397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 weston_view_geometry_dirty(view);
1399}
1400
Derek Foreman280e7dd2014-10-03 13:13:42 -05001401WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001402weston_view_is_mapped(struct weston_view *view)
1403{
1404 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001405 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001407 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001408}
1409
Derek Foreman280e7dd2014-10-03 13:13:42 -05001410WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001411weston_surface_is_mapped(struct weston_surface *surface)
1412{
1413 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001414 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001415 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001416 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001417}
1418
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001419static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001420surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001421{
1422 struct weston_view *view;
1423
1424 if (surface->width == width && surface->height == height)
1425 return;
1426
1427 surface->width = width;
1428 surface->height = height;
1429
1430 wl_list_for_each(view, &surface->views, surface_link)
1431 weston_view_geometry_dirty(view);
1432}
1433
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001434WL_EXPORT void
1435weston_surface_set_size(struct weston_surface *surface,
1436 int32_t width, int32_t height)
1437{
1438 assert(!surface->resource);
1439 surface_set_size(surface, width, height);
1440}
1441
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001442static int
1443fixed_round_up_to_int(wl_fixed_t f)
1444{
1445 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1446}
1447
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001448static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001449weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001450{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001451 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001452 int32_t width, height;
1453
1454 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001455 surface->width_from_buffer = 0;
1456 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001457 return;
1458 }
1459
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001460 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001461 case WL_OUTPUT_TRANSFORM_90:
1462 case WL_OUTPUT_TRANSFORM_270:
1463 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1464 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001465 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1466 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001467 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001468 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001469 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1470 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001471 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001472 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001473
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001474 surface->width_from_buffer = width;
1475 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001476}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001477
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001478static void
1479weston_surface_update_size(struct weston_surface *surface)
1480{
1481 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1482 int32_t width, height;
1483
1484 width = surface->width_from_buffer;
1485 height = surface->height_from_buffer;
1486
1487 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001488 surface_set_size(surface,
1489 vp->surface.width, vp->surface.height);
1490 return;
1491 }
1492
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001493 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001494 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1495 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1496
1497 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001498 return;
1499 }
1500
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001501 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001502}
1503
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001504WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001505weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001506{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001507 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001508
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001509 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001510
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001511 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001512}
1513
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514WL_EXPORT struct weston_view *
1515weston_compositor_pick_view(struct weston_compositor *compositor,
1516 wl_fixed_t x, wl_fixed_t y,
1517 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001518{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001520 int ix = wl_fixed_to_int(x);
1521 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001522
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523 wl_list_for_each(view, &compositor->view_list, link) {
1524 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001525 if (pixman_region32_contains_point(
1526 &view->transform.masked_boundingbox,
1527 ix, iy, NULL) &&
1528 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 wl_fixed_to_int(*vx),
1530 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001531 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001533 }
1534
1535 return NULL;
1536}
1537
1538static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001539weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001540{
Daniel Stone37816df2012-05-16 18:45:18 +01001541 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001542
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001543 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001544 return;
1545
Daniel Stone37816df2012-05-16 18:45:18 +01001546 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001547 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001548}
1549
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001550WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001552{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001553 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 if (!weston_view_is_mapped(view))
1556 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001557
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558 weston_view_damage_below(view);
1559 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001560 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001561 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 wl_list_remove(&view->link);
1563 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 view->output_mask = 0;
1565 weston_surface_assign_output(view->surface);
1566
1567 if (weston_surface_is_mapped(view->surface))
1568 return;
1569
1570 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1571 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001572 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001574 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001575 NULL,
1576 wl_fixed_from_int(0),
1577 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001579 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001580 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001582
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583WL_EXPORT void
1584weston_surface_unmap(struct weston_surface *surface)
1585{
1586 struct weston_view *view;
1587
1588 wl_list_for_each(view, &surface->views, surface_link)
1589 weston_view_unmap(view);
1590 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001591}
1592
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001593static void
1594weston_surface_reset_pending_buffer(struct weston_surface *surface)
1595{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001596 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001597 surface->pending.sx = 0;
1598 surface->pending.sy = 0;
1599 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001600 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001601}
1602
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603WL_EXPORT void
1604weston_view_destroy(struct weston_view *view)
1605{
1606 wl_signal_emit(&view->destroy_signal, view);
1607
1608 assert(wl_list_empty(&view->geometry.child_list));
1609
1610 if (weston_view_is_mapped(view)) {
1611 weston_view_unmap(view);
1612 weston_compositor_build_view_list(view->surface->compositor);
1613 }
1614
1615 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001616 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617
1618 pixman_region32_fini(&view->clip);
1619 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001620 pixman_region32_fini(&view->transform.masked_boundingbox);
1621 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622
1623 weston_view_set_transform_parent(view, NULL);
1624
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625 wl_list_remove(&view->surface_link);
1626
1627 free(view);
1628}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001629
1630WL_EXPORT void
1631weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001632{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001633 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001635
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001636 if (--surface->ref_count > 0)
1637 return;
1638
1639 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1640
Pekka Paalanene67858b2013-04-25 13:57:42 +03001641 assert(wl_list_empty(&surface->subsurface_list_pending));
1642 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001643
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1645 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001646
Jason Ekstrand7b982072014-05-20 14:33:03 -05001647 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001648
Pekka Paalanende685b82012-12-04 15:58:12 +02001649 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001650
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001651 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001652 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001653 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001654
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001655 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001656 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001657
Pekka Paalanen133e4392014-09-23 22:08:46 -04001658 weston_presentation_feedback_discard_list(&surface->feedback_list);
1659
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001660 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001661}
1662
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001663static void
1664destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001665{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001666 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001667
Giulio Camuffo0d379742013-11-15 22:06:15 +01001668 /* Set the resource to NULL, since we don't want to leave a
1669 * dangling pointer if the surface was refcounted and survives
1670 * the weston_surface_destroy() call. */
1671 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001672 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001673}
1674
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001675static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001676weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1677{
1678 struct weston_buffer *buffer =
1679 container_of(listener, struct weston_buffer, destroy_listener);
1680
1681 wl_signal_emit(&buffer->destroy_signal, buffer);
1682 free(buffer);
1683}
1684
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001685WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001686weston_buffer_from_resource(struct wl_resource *resource)
1687{
1688 struct weston_buffer *buffer;
1689 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001690
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001691 listener = wl_resource_get_destroy_listener(resource,
1692 weston_buffer_destroy_handler);
1693
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001694 if (listener)
1695 return container_of(listener, struct weston_buffer,
1696 destroy_listener);
1697
1698 buffer = zalloc(sizeof *buffer);
1699 if (buffer == NULL)
1700 return NULL;
1701
1702 buffer->resource = resource;
1703 wl_signal_init(&buffer->destroy_signal);
1704 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001705 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001706 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001707
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001708 return buffer;
1709}
1710
1711static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001712weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1713 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001714{
Pekka Paalanende685b82012-12-04 15:58:12 +02001715 struct weston_buffer_reference *ref =
1716 container_of(listener, struct weston_buffer_reference,
1717 destroy_listener);
1718
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001719 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001720 ref->buffer = NULL;
1721}
1722
1723WL_EXPORT void
1724weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001725 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001726{
1727 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001728 ref->buffer->busy_count--;
1729 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001730 assert(wl_resource_get_client(ref->buffer->resource));
1731 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001732 WL_BUFFER_RELEASE);
1733 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001734 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001735 }
1736
Pekka Paalanende685b82012-12-04 15:58:12 +02001737 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001738 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001739 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001740 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001741 }
1742
Pekka Paalanende685b82012-12-04 15:58:12 +02001743 ref->buffer = buffer;
1744 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1745}
1746
1747static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001748weston_surface_attach(struct weston_surface *surface,
1749 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001750{
1751 weston_buffer_reference(&surface->buffer_ref, buffer);
1752
Pekka Paalanena6421c42012-12-04 15:58:10 +02001753 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001754 if (weston_surface_is_mapped(surface))
1755 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001756 }
1757
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001758 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001759
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001760 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001761 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001762}
1763
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001764WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001765weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001766{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001767 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001768
1769 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001770 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001771}
1772
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001773WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001774weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001775{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001776 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001777
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001778 pixman_region32_union(&compositor->primary_plane.damage,
1779 &compositor->primary_plane.damage,
1780 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001781 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001782}
1783
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001784static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001786{
Pekka Paalanende685b82012-12-04 15:58:12 +02001787 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001788 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001789 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001790
Pekka Paalanenb5026542014-11-12 15:09:24 +02001791 if (weston_timeline_enabled_ &&
1792 pixman_region32_not_empty(&surface->damage))
1793 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1794 TLP_OUTPUT(surface->output), TLP_END);
1795
Jason Ekstrandef540082014-06-26 10:37:36 -07001796 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001797}
1798
1799static void
1800view_accumulate_damage(struct weston_view *view,
1801 pixman_region32_t *opaque)
1802{
1803 pixman_region32_t damage;
1804
1805 pixman_region32_init(&damage);
1806 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001807 pixman_box32_t *extents;
1808
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 extents = pixman_region32_extents(&view->surface->damage);
1810 view_compute_bbox(view, extents->x1, extents->y1,
1811 extents->x2 - extents->x1,
1812 extents->y2 - extents->y1,
1813 &damage);
1814 pixman_region32_translate(&damage,
1815 -view->plane->x,
1816 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001817 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 pixman_region32_copy(&damage, &view->surface->damage);
1819 pixman_region32_translate(&damage,
1820 view->geometry.x - view->plane->x,
1821 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001822 }
1823
Jason Ekstranda7af7042013-10-12 22:38:11 -05001824 pixman_region32_subtract(&damage, &damage, opaque);
1825 pixman_region32_union(&view->plane->damage,
1826 &view->plane->damage, &damage);
1827 pixman_region32_fini(&damage);
1828 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001829 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001830}
1831
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001832static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001833compositor_accumulate_damage(struct weston_compositor *ec)
1834{
1835 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001837 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001838
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001839 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001840
1841 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001842 pixman_region32_copy(&plane->clip, &clip);
1843
1844 pixman_region32_init(&opaque);
1845
Jason Ekstranda7af7042013-10-12 22:38:11 -05001846 wl_list_for_each(ev, &ec->view_list, link) {
1847 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001848 continue;
1849
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001851 }
1852
1853 pixman_region32_union(&clip, &clip, &opaque);
1854 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001855 }
1856
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001857 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001858
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859 wl_list_for_each(ev, &ec->view_list, link)
1860 ev->surface->touched = 0;
1861
1862 wl_list_for_each(ev, &ec->view_list, link) {
1863 if (ev->surface->touched)
1864 continue;
1865 ev->surface->touched = 1;
1866
1867 surface_flush_damage(ev->surface);
1868
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001869 /* Both the renderer and the backend have seen the buffer
1870 * by now. If renderer needs the buffer, it has its own
1871 * reference set. If the backend wants to keep the buffer
1872 * around for migrating the surface into a non-primary plane
1873 * later, keep_buffer is true. Otherwise, drop the core
1874 * reference now, and allow early buffer release. This enables
1875 * clients to use single-buffering.
1876 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001877 if (!ev->surface->keep_buffer)
1878 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001879 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001880}
1881
1882static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001884{
1885 struct weston_subsurface *sub;
1886
Pekka Paalanene67858b2013-04-25 13:57:42 +03001887 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001889 continue;
1890
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1892 wl_list_init(&sub->surface->views);
1893
1894 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001895 }
1896}
1897
1898static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001899surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001900{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 struct weston_subsurface *sub;
1902 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001903
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1905 if (sub->surface == surface)
1906 continue;
1907
George Kiagiadakised04d382014-06-13 18:10:26 +02001908 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1909 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001911 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001912
1913 surface_free_unused_subsurface_views(sub->surface);
1914 }
1915}
1916
1917static void
1918view_list_add_subsurface_view(struct weston_compositor *compositor,
1919 struct weston_subsurface *sub,
1920 struct weston_view *parent)
1921{
1922 struct weston_subsurface *child;
1923 struct weston_view *view = NULL, *iv;
1924
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001925 if (!weston_surface_is_mapped(sub->surface))
1926 return;
1927
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1929 if (iv->geometry.parent == parent) {
1930 view = iv;
1931 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001932 }
1933 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934
1935 if (view) {
1936 /* Put it back in the surface's list of views */
1937 wl_list_remove(&view->surface_link);
1938 wl_list_insert(&sub->surface->views, &view->surface_link);
1939 } else {
1940 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001941 weston_view_set_position(view,
1942 sub->position.x,
1943 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 weston_view_set_transform_parent(view, parent);
1945 }
1946
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001947 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001948 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949
Pekka Paalanenb188e912013-11-19 14:03:35 +02001950 if (wl_list_empty(&sub->surface->subsurface_list)) {
1951 wl_list_insert(compositor->view_list.prev, &view->link);
1952 return;
1953 }
1954
1955 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1956 if (child->surface == sub->surface)
1957 wl_list_insert(compositor->view_list.prev, &view->link);
1958 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001960 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001961}
1962
1963static void
1964view_list_add(struct weston_compositor *compositor,
1965 struct weston_view *view)
1966{
1967 struct weston_subsurface *sub;
1968
1969 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970
Pekka Paalanenb188e912013-11-19 14:03:35 +02001971 if (wl_list_empty(&view->surface->subsurface_list)) {
1972 wl_list_insert(compositor->view_list.prev, &view->link);
1973 return;
1974 }
1975
1976 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1977 if (sub->surface == view->surface)
1978 wl_list_insert(compositor->view_list.prev, &view->link);
1979 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001980 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001981 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982}
1983
1984static void
1985weston_compositor_build_view_list(struct weston_compositor *compositor)
1986{
1987 struct weston_view *view;
1988 struct weston_layer *layer;
1989
1990 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001991 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001992 surface_stash_subsurface_views(view->surface);
1993
1994 wl_list_init(&compositor->view_list);
1995 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001996 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 view_list_add(compositor, view);
1998 }
1999 }
2000
2001 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002002 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002004}
2005
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002006static void
2007weston_output_take_feedback_list(struct weston_output *output,
2008 struct weston_surface *surface)
2009{
2010 struct weston_view *view;
2011 struct weston_presentation_feedback *feedback;
2012 uint32_t flags = 0xffffffff;
2013
2014 if (wl_list_empty(&surface->feedback_list))
2015 return;
2016
2017 /* All views must have the flag for the flag to survive. */
2018 wl_list_for_each(view, &surface->views, surface_link) {
2019 /* ignore views that are not on this output at all */
2020 if (view->output_mask & (1u << output->id))
2021 flags &= view->psf_flags;
2022 }
2023
2024 wl_list_for_each(feedback, &surface->feedback_list, link)
2025 feedback->psf_flags = flags;
2026
2027 wl_list_insert_list(&output->feedback_list, &surface->feedback_list);
2028 wl_list_init(&surface->feedback_list);
2029}
2030
David Herrmann1edf44c2013-10-22 17:11:26 +02002031static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002032weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002033{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002034 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002036 struct weston_animation *animation, *next;
2037 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002038 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002039 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002040 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002041
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002042 if (output->destroying)
2043 return 0;
2044
Pekka Paalanenb5026542014-11-12 15:09:24 +02002045 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2046
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002047 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002048 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002049
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002050 if (output->assign_planes && !output->disable_planes) {
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002051 output->assign_planes(output);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002052 } else {
2053 wl_list_for_each(ev, &ec->view_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002054 weston_view_move_to_plane(ev, &ec->primary_plane);
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002055 ev->psf_flags = 0;
2056 }
2057 }
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002058
Pekka Paalanene67858b2013-04-25 13:57:42 +03002059 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 wl_list_for_each(ev, &ec->view_list, link) {
2061 /* Note: This operation is safe to do multiple times on the
2062 * same surface.
2063 */
2064 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002065 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 &ev->surface->frame_callback_list);
2067 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002068
Pekka Paalanenbf0e0312014-12-17 16:20:41 +02002069 weston_output_take_feedback_list(output, ev->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002070 }
2071 }
2072
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002073 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002074
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002075 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002076 pixman_region32_intersect(&output_damage,
2077 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002078 pixman_region32_subtract(&output_damage,
2079 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002080
Scott Moreauccbf29d2012-02-22 14:21:41 -07002081 if (output->dirty)
2082 weston_output_update_matrix(output);
2083
David Herrmann1edf44c2013-10-22 17:11:26 +02002084 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002085
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002086 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002087
Kristian Høgsbergef044142011-06-21 15:02:12 -04002088 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002089
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002090 weston_compositor_repick(ec);
2091 wl_event_loop_dispatch(ec->input_loop, 0);
2092
Jonas Ådahldb773762012-06-13 00:01:21 +02002093 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002094 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002095 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002096 }
2097
Scott Moreaud64cf212012-06-08 19:40:54 -06002098 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002099 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002100 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002101 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002102
Pekka Paalanenb5026542014-11-12 15:09:24 +02002103 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2104
David Herrmann1edf44c2013-10-22 17:11:26 +02002105 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002106}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002107
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002108static int
2109weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002110{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002111 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002112
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002113 wl_event_loop_dispatch(compositor->input_loop, 0);
2114
2115 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002116}
2117
2118WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002119weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002120 const struct timespec *stamp,
2121 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002122{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002123 struct weston_compositor *compositor = output->compositor;
2124 struct wl_event_loop *loop =
2125 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002126 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002127 uint32_t refresh_nsec;
2128
Pekka Paalanenb5026542014-11-12 15:09:24 +02002129 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2130 TLP_VBLANK(stamp), TLP_END);
2131
Pekka Paalanen133e4392014-09-23 22:08:46 -04002132 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2133 weston_presentation_feedback_present_list(&output->feedback_list,
2134 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002135 output->msc,
2136 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002137
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002138 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002139
2140 if (output->repaint_needed &&
2141 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2142 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002143 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002144 if (!r)
2145 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002146 }
2147
2148 output->repaint_scheduled = 0;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002149 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2150
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002151 if (compositor->input_loop_source)
2152 return;
2153
2154 fd = wl_event_loop_get_fd(compositor->input_loop);
2155 compositor->input_loop_source =
2156 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2157 weston_compositor_read_input, compositor);
2158}
2159
2160static void
2161idle_repaint(void *data)
2162{
2163 struct weston_output *output = data;
2164
Jonas Ådahle5a12252013-04-05 23:07:11 +02002165 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002166}
2167
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002168WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002169weston_layer_entry_insert(struct weston_layer_entry *list,
2170 struct weston_layer_entry *entry)
2171{
2172 wl_list_insert(&list->link, &entry->link);
2173 entry->layer = list->layer;
2174}
2175
2176WL_EXPORT void
2177weston_layer_entry_remove(struct weston_layer_entry *entry)
2178{
2179 wl_list_remove(&entry->link);
2180 wl_list_init(&entry->link);
2181 entry->layer = NULL;
2182}
2183
2184WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002185weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2186{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002187 wl_list_init(&layer->view_list.link);
2188 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002189 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002190 if (below != NULL)
2191 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002192}
2193
2194WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002195weston_layer_set_mask(struct weston_layer *layer,
2196 int x, int y, int width, int height)
2197{
2198 struct weston_view *view;
2199
2200 layer->mask.x1 = x;
2201 layer->mask.x2 = x + width;
2202 layer->mask.y1 = y;
2203 layer->mask.y2 = y + height;
2204
2205 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2206 weston_view_geometry_dirty(view);
2207 }
2208}
2209
2210WL_EXPORT void
2211weston_layer_set_mask_infinite(struct weston_layer *layer)
2212{
2213 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2214 UINT32_MAX, UINT32_MAX);
2215}
2216
2217WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002218weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002219{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002220 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002221 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002222
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002223 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2224 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002225 return;
2226
Pekka Paalanenb5026542014-11-12 15:09:24 +02002227 if (!output->repaint_needed)
2228 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2229
Kristian Høgsbergef044142011-06-21 15:02:12 -04002230 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002231 output->repaint_needed = 1;
2232 if (output->repaint_scheduled)
2233 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002234
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002235 wl_event_loop_add_idle(loop, idle_repaint, output);
2236 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002237 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2238
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002239
2240 if (compositor->input_loop_source) {
2241 wl_event_source_remove(compositor->input_loop_source);
2242 compositor->input_loop_source = NULL;
2243 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002244}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002245
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002246WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002247weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2248{
2249 struct weston_output *output;
2250
2251 wl_list_for_each(output, &compositor->output_list, link)
2252 weston_output_schedule_repaint(output);
2253}
2254
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002255static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002256surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002257{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002258 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002259}
2260
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002261static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002262surface_attach(struct wl_client *client,
2263 struct wl_resource *resource,
2264 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2265{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002266 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002267 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002268
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002269 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002270 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002271 if (buffer == NULL) {
2272 wl_client_post_no_memory(client);
2273 return;
2274 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002275 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002276
Pekka Paalanende685b82012-12-04 15:58:12 +02002277 /* Attach, attach, without commit in between does not send
2278 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002279 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002280
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002281 surface->pending.sx = sx;
2282 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002283 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002284}
2285
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002286static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002287surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002288 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002289 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002290{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002291 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002292
Pekka Paalanen8e159182012-10-10 12:49:25 +03002293 pixman_region32_union_rect(&surface->pending.damage,
2294 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002295 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002296}
2297
Kristian Høgsberg33418202011-08-16 23:01:28 -04002298static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002299destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002300{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002301 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002302
2303 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002304 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002305}
2306
2307static void
2308surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002309 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002310{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002311 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002312 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002313
2314 cb = malloc(sizeof *cb);
2315 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002316 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002317 return;
2318 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002319
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002320 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2321 callback);
2322 if (cb->resource == NULL) {
2323 free(cb);
2324 wl_resource_post_no_memory(resource);
2325 return;
2326 }
2327
Jason Ekstranda85118c2013-06-27 20:17:02 -05002328 wl_resource_set_implementation(cb->resource, NULL, cb,
2329 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002330
Pekka Paalanenbc106382012-10-10 12:49:31 +03002331 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002332}
2333
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002334static void
2335surface_set_opaque_region(struct wl_client *client,
2336 struct wl_resource *resource,
2337 struct wl_resource *region_resource)
2338{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002339 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002340 struct weston_region *region;
2341
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002342 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002343 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002344 pixman_region32_copy(&surface->pending.opaque,
2345 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002346 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002347 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002348 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002349}
2350
2351static void
2352surface_set_input_region(struct wl_client *client,
2353 struct wl_resource *resource,
2354 struct wl_resource *region_resource)
2355{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002356 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002357 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002358
2359 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002360 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002361 pixman_region32_copy(&surface->pending.input,
2362 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002363 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002364 pixman_region32_fini(&surface->pending.input);
2365 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002366 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002367}
2368
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002369static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002370weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002371{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002372 struct weston_subsurface *sub;
2373
2374 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2375 parent_link_pending) {
2376 wl_list_remove(&sub->parent_link);
2377 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2378 }
2379}
2380
2381static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002382weston_surface_commit_state(struct weston_surface *surface,
2383 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002384{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002385 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002386 pixman_region32_t opaque;
2387
Alexander Larsson4ea95522013-05-22 14:41:37 +02002388 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002389 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002390 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002391 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002392
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002393 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002394 if (state->newly_attached)
2395 weston_surface_attach(surface, state->buffer);
2396 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002397
Jason Ekstrand7b982072014-05-20 14:33:03 -05002398 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002399 weston_surface_update_size(surface);
2400 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002401 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002402 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002403
Jason Ekstrand7b982072014-05-20 14:33:03 -05002404 state->sx = 0;
2405 state->sy = 0;
2406 state->newly_attached = 0;
2407 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002408
2409 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002410 if (weston_timeline_enabled_ &&
2411 pixman_region32_not_empty(&state->damage))
2412 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002413 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002414 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002415 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002416 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002417 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002418
2419 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002420 pixman_region32_init(&opaque);
2421 pixman_region32_intersect_rect(&opaque, &state->opaque,
2422 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002423
2424 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2425 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002426 wl_list_for_each(view, &surface->views, surface_link)
2427 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002428 }
2429
2430 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002431
2432 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002433 pixman_region32_intersect_rect(&surface->input, &state->input,
2434 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002435
Pekka Paalanenbc106382012-10-10 12:49:31 +03002436 /* wl_surface.frame */
2437 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002438 &state->frame_callback_list);
2439 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002440
2441 /* XXX:
2442 * What should happen with a feedback request, if there
2443 * is no wl_buffer attached for this commit?
2444 */
2445
2446 /* presentation.feedback */
2447 wl_list_insert_list(&surface->feedback_list,
2448 &state->feedback_list);
2449 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002450}
2451
2452static void
2453weston_surface_commit(struct weston_surface *surface)
2454{
2455 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002456
Pekka Paalanene67858b2013-04-25 13:57:42 +03002457 weston_surface_commit_subsurface_order(surface);
2458
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002459 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002460}
2461
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002462static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002463weston_subsurface_commit(struct weston_subsurface *sub);
2464
2465static void
2466weston_subsurface_parent_commit(struct weston_subsurface *sub,
2467 int parent_is_synchronized);
2468
2469static void
2470surface_commit(struct wl_client *client, struct wl_resource *resource)
2471{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002472 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2474
2475 if (sub) {
2476 weston_subsurface_commit(sub);
2477 return;
2478 }
2479
2480 weston_surface_commit(surface);
2481
2482 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2483 if (sub->surface != surface)
2484 weston_subsurface_parent_commit(sub, 0);
2485 }
2486}
2487
2488static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002489surface_set_buffer_transform(struct wl_client *client,
2490 struct wl_resource *resource, int transform)
2491{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002492 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002493
Jonny Lamba55f1392014-05-30 12:07:15 +02002494 /* if wl_output.transform grows more members this will need to be updated. */
2495 if (transform < 0 ||
2496 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2497 wl_resource_post_error(resource,
2498 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2499 "buffer transform must be a valid transform "
2500 "('%d' specified)", transform);
2501 return;
2502 }
2503
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002504 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002505 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002506}
2507
Alexander Larsson4ea95522013-05-22 14:41:37 +02002508static void
2509surface_set_buffer_scale(struct wl_client *client,
2510 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002511 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002512{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002513 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002514
Jonny Lamba55f1392014-05-30 12:07:15 +02002515 if (scale < 1) {
2516 wl_resource_post_error(resource,
2517 WL_SURFACE_ERROR_INVALID_SCALE,
2518 "buffer scale must be at least one "
2519 "('%d' specified)", scale);
2520 return;
2521 }
2522
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002523 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002524 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002525}
2526
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002527static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002528 surface_destroy,
2529 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002530 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002531 surface_frame,
2532 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002533 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002534 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002535 surface_set_buffer_transform,
2536 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002537};
2538
2539static void
2540compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002541 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002542{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002543 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002544 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002545
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002546 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002547 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002548 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002549 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002550 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002551
Jason Ekstranda85118c2013-06-27 20:17:02 -05002552 surface->resource =
2553 wl_resource_create(client, &wl_surface_interface,
2554 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002555 if (surface->resource == NULL) {
2556 weston_surface_destroy(surface);
2557 wl_resource_post_no_memory(resource);
2558 return;
2559 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002560 wl_resource_set_implementation(surface->resource, &surface_interface,
2561 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002562
2563 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002564}
2565
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002566static void
2567destroy_region(struct wl_resource *resource)
2568{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002569 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002570
2571 pixman_region32_fini(&region->region);
2572 free(region);
2573}
2574
2575static void
2576region_destroy(struct wl_client *client, struct wl_resource *resource)
2577{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002578 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579}
2580
2581static void
2582region_add(struct wl_client *client, struct wl_resource *resource,
2583 int32_t x, int32_t y, int32_t width, int32_t height)
2584{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002585 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002586
2587 pixman_region32_union_rect(&region->region, &region->region,
2588 x, y, width, height);
2589}
2590
2591static void
2592region_subtract(struct wl_client *client, struct wl_resource *resource,
2593 int32_t x, int32_t y, int32_t width, int32_t height)
2594{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002595 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596 pixman_region32_t rect;
2597
2598 pixman_region32_init_rect(&rect, x, y, width, height);
2599 pixman_region32_subtract(&region->region, &region->region, &rect);
2600 pixman_region32_fini(&rect);
2601}
2602
2603static const struct wl_region_interface region_interface = {
2604 region_destroy,
2605 region_add,
2606 region_subtract
2607};
2608
2609static void
2610compositor_create_region(struct wl_client *client,
2611 struct wl_resource *resource, uint32_t id)
2612{
2613 struct weston_region *region;
2614
2615 region = malloc(sizeof *region);
2616 if (region == NULL) {
2617 wl_resource_post_no_memory(resource);
2618 return;
2619 }
2620
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002621 pixman_region32_init(&region->region);
2622
Jason Ekstranda85118c2013-06-27 20:17:02 -05002623 region->resource =
2624 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002625 if (region->resource == NULL) {
2626 free(region);
2627 wl_resource_post_no_memory(resource);
2628 return;
2629 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002630 wl_resource_set_implementation(region->resource, &region_interface,
2631 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002632}
2633
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002634static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002635 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002636 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002637};
2638
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002639static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002640weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2641{
2642 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643
Jason Ekstrand7b982072014-05-20 14:33:03 -05002644 weston_surface_commit_state(surface, &sub->cached);
2645 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002646
2647 weston_surface_commit_subsurface_order(surface);
2648
2649 weston_surface_schedule_repaint(surface);
2650
Jason Ekstrand7b982072014-05-20 14:33:03 -05002651 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002652}
2653
2654static void
2655weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2656{
2657 struct weston_surface *surface = sub->surface;
2658
2659 /*
2660 * If this commit would cause the surface to move by the
2661 * attach(dx, dy) parameters, the old damage region must be
2662 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002663 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664 */
2665 pixman_region32_translate(&sub->cached.damage,
2666 -surface->pending.sx, -surface->pending.sy);
2667 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2668 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002669 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002670
2671 if (surface->pending.newly_attached) {
2672 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002673 weston_surface_state_set_buffer(&sub->cached,
2674 surface->pending.buffer);
2675 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002677 weston_presentation_feedback_discard_list(
2678 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002679 }
2680 sub->cached.sx += surface->pending.sx;
2681 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002682
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002683 sub->cached.buffer_viewport.changed |=
2684 surface->pending.buffer_viewport.changed;
2685 sub->cached.buffer_viewport.buffer =
2686 surface->pending.buffer_viewport.buffer;
2687 sub->cached.buffer_viewport.surface =
2688 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002689
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002690 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002691
2692 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2693
2694 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2695
2696 wl_list_insert_list(&sub->cached.frame_callback_list,
2697 &surface->pending.frame_callback_list);
2698 wl_list_init(&surface->pending.frame_callback_list);
2699
Pekka Paalanen133e4392014-09-23 22:08:46 -04002700 wl_list_insert_list(&sub->cached.feedback_list,
2701 &surface->pending.feedback_list);
2702 wl_list_init(&surface->pending.feedback_list);
2703
Jason Ekstrand7b982072014-05-20 14:33:03 -05002704 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002705}
2706
Derek Foreman280e7dd2014-10-03 13:13:42 -05002707static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002708weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2709{
2710 while (sub) {
2711 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002712 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002713
2714 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002715 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002716
2717 sub = weston_surface_to_subsurface(sub->parent);
2718 }
2719
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002720 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002721}
2722
2723static void
2724weston_subsurface_commit(struct weston_subsurface *sub)
2725{
2726 struct weston_surface *surface = sub->surface;
2727 struct weston_subsurface *tmp;
2728
2729 /* Recursive check for effectively synchronized. */
2730 if (weston_subsurface_is_synchronized(sub)) {
2731 weston_subsurface_commit_to_cache(sub);
2732 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002733 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002734 /* flush accumulated state from cache */
2735 weston_subsurface_commit_to_cache(sub);
2736 weston_subsurface_commit_from_cache(sub);
2737 } else {
2738 weston_surface_commit(surface);
2739 }
2740
2741 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2742 if (tmp->surface != surface)
2743 weston_subsurface_parent_commit(tmp, 0);
2744 }
2745 }
2746}
2747
2748static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002749weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750{
2751 struct weston_surface *surface = sub->surface;
2752 struct weston_subsurface *tmp;
2753
Pekka Paalanene67858b2013-04-25 13:57:42 +03002754 /* From now on, commit_from_cache the whole sub-tree, regardless of
2755 * the synchronized mode of each child. This sub-surface or some
2756 * of its ancestors were synchronized, so we are synchronized
2757 * all the way down.
2758 */
2759
Jason Ekstrand7b982072014-05-20 14:33:03 -05002760 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002761 weston_subsurface_commit_from_cache(sub);
2762
2763 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2764 if (tmp->surface != surface)
2765 weston_subsurface_parent_commit(tmp, 1);
2766 }
2767}
2768
2769static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002770weston_subsurface_parent_commit(struct weston_subsurface *sub,
2771 int parent_is_synchronized)
2772{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002774 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002775 wl_list_for_each(view, &sub->surface->views, surface_link)
2776 weston_view_set_position(view,
2777 sub->position.x,
2778 sub->position.y);
2779
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002780 sub->position.set = 0;
2781 }
2782
2783 if (parent_is_synchronized || sub->synchronized)
2784 weston_subsurface_synchronized_commit(sub);
2785}
2786
Pekka Paalanen8274d902014-08-06 19:36:51 +03002787static int
2788subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2789{
2790 return snprintf(buf, len, "sub-surface");
2791}
2792
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002793static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002794subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002795{
2796 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002798
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002800 weston_view_set_position(view,
2801 view->geometry.x + dx,
2802 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002803
2804 /* No need to check parent mappedness, because if parent is not
2805 * mapped, parent is not in a visible layer, so this sub-surface
2806 * will not be drawn either.
2807 */
2808 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002809 struct weston_output *output;
2810
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002811 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002812 * because that would call it also for the parent surface,
2813 * which might not be mapped yet. That would lead to
2814 * inconsistent state, where the window could never be
2815 * mapped.
2816 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002817 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002818 * weston_surface_is_mapped() return true, so that when the
2819 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002820 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002821 */
2822 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002823 output = container_of(compositor->output_list.next,
2824 struct weston_output, link);
2825
2826 surface->output = output;
2827 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828 }
2829}
2830
2831static struct weston_subsurface *
2832weston_surface_to_subsurface(struct weston_surface *surface)
2833{
2834 if (surface->configure == subsurface_configure)
2835 return surface->configure_private;
2836
2837 return NULL;
2838}
2839
Pekka Paalanen01388e22013-04-25 13:57:44 +03002840WL_EXPORT struct weston_surface *
2841weston_surface_get_main_surface(struct weston_surface *surface)
2842{
2843 struct weston_subsurface *sub;
2844
2845 while (surface && (sub = weston_surface_to_subsurface(surface)))
2846 surface = sub->parent;
2847
2848 return surface;
2849}
2850
Pekka Paalanen50b67472014-10-01 15:02:41 +03002851WL_EXPORT int
2852weston_surface_set_role(struct weston_surface *surface,
2853 const char *role_name,
2854 struct wl_resource *error_resource,
2855 uint32_t error_code)
2856{
2857 assert(role_name);
2858
2859 if (surface->role_name == NULL ||
2860 surface->role_name == role_name ||
2861 strcmp(surface->role_name, role_name) == 0) {
2862 surface->role_name = role_name;
2863
2864 return 0;
2865 }
2866
2867 wl_resource_post_error(error_resource, error_code,
2868 "Cannot assign role %s to wl_surface@%d,"
2869 " already has role %s\n",
2870 role_name,
2871 wl_resource_get_id(surface->resource),
2872 surface->role_name);
2873 return -1;
2874}
2875
Pekka Paalanen8274d902014-08-06 19:36:51 +03002876WL_EXPORT void
2877weston_surface_set_label_func(struct weston_surface *surface,
2878 int (*desc)(struct weston_surface *,
2879 char *, size_t))
2880{
2881 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002882 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002883}
2884
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885static void
2886subsurface_set_position(struct wl_client *client,
2887 struct wl_resource *resource, int32_t x, int32_t y)
2888{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002889 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890
2891 if (!sub)
2892 return;
2893
2894 sub->position.x = x;
2895 sub->position.y = y;
2896 sub->position.set = 1;
2897}
2898
2899static struct weston_subsurface *
2900subsurface_from_surface(struct weston_surface *surface)
2901{
2902 struct weston_subsurface *sub;
2903
2904 sub = weston_surface_to_subsurface(surface);
2905 if (sub)
2906 return sub;
2907
2908 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2909 if (sub->surface == surface)
2910 return sub;
2911
2912 return NULL;
2913}
2914
2915static struct weston_subsurface *
2916subsurface_sibling_check(struct weston_subsurface *sub,
2917 struct weston_surface *surface,
2918 const char *request)
2919{
2920 struct weston_subsurface *sibling;
2921
2922 sibling = subsurface_from_surface(surface);
2923
2924 if (!sibling) {
2925 wl_resource_post_error(sub->resource,
2926 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2927 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002928 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929 return NULL;
2930 }
2931
2932 if (sibling->parent != sub->parent) {
2933 wl_resource_post_error(sub->resource,
2934 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2935 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002936 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002937 return NULL;
2938 }
2939
2940 return sibling;
2941}
2942
2943static void
2944subsurface_place_above(struct wl_client *client,
2945 struct wl_resource *resource,
2946 struct wl_resource *sibling_resource)
2947{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002948 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002949 struct weston_surface *surface =
2950 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002951 struct weston_subsurface *sibling;
2952
2953 if (!sub)
2954 return;
2955
2956 sibling = subsurface_sibling_check(sub, surface, "place_above");
2957 if (!sibling)
2958 return;
2959
2960 wl_list_remove(&sub->parent_link_pending);
2961 wl_list_insert(sibling->parent_link_pending.prev,
2962 &sub->parent_link_pending);
2963}
2964
2965static void
2966subsurface_place_below(struct wl_client *client,
2967 struct wl_resource *resource,
2968 struct wl_resource *sibling_resource)
2969{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002970 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002971 struct weston_surface *surface =
2972 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002973 struct weston_subsurface *sibling;
2974
2975 if (!sub)
2976 return;
2977
2978 sibling = subsurface_sibling_check(sub, surface, "place_below");
2979 if (!sibling)
2980 return;
2981
2982 wl_list_remove(&sub->parent_link_pending);
2983 wl_list_insert(&sibling->parent_link_pending,
2984 &sub->parent_link_pending);
2985}
2986
2987static void
2988subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2989{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002990 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002991
2992 if (sub)
2993 sub->synchronized = 1;
2994}
2995
2996static void
2997subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2998{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002999 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003000
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003001 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03003002 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03003003
3004 /* If sub became effectively desynchronized, flush. */
3005 if (!weston_subsurface_is_synchronized(sub))
3006 weston_subsurface_synchronized_commit(sub);
3007 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03003008}
3009
3010static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03003011weston_subsurface_unlink_parent(struct weston_subsurface *sub)
3012{
3013 wl_list_remove(&sub->parent_link);
3014 wl_list_remove(&sub->parent_link_pending);
3015 wl_list_remove(&sub->parent_destroy_listener.link);
3016 sub->parent = NULL;
3017}
3018
3019static void
3020weston_subsurface_destroy(struct weston_subsurface *sub);
3021
3022static void
3023subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
3024{
3025 struct weston_subsurface *sub =
3026 container_of(listener, struct weston_subsurface,
3027 surface_destroy_listener);
3028 assert(data == &sub->surface->resource);
3029
3030 /* The protocol object (wl_resource) is left inert. */
3031 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003032 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003033
3034 weston_subsurface_destroy(sub);
3035}
3036
3037static void
3038subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3039{
3040 struct weston_subsurface *sub =
3041 container_of(listener, struct weston_subsurface,
3042 parent_destroy_listener);
3043 assert(data == &sub->parent->resource);
3044 assert(sub->surface != sub->parent);
3045
3046 if (weston_surface_is_mapped(sub->surface))
3047 weston_surface_unmap(sub->surface);
3048
3049 weston_subsurface_unlink_parent(sub);
3050}
3051
3052static void
3053subsurface_resource_destroy(struct wl_resource *resource)
3054{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003055 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003056
3057 if (sub)
3058 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003059}
3060
3061static void
3062subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3063{
3064 wl_resource_destroy(resource);
3065}
3066
3067static void
3068weston_subsurface_link_parent(struct weston_subsurface *sub,
3069 struct weston_surface *parent)
3070{
3071 sub->parent = parent;
3072 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003073 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003074 &sub->parent_destroy_listener);
3075
3076 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3077 wl_list_insert(&parent->subsurface_list_pending,
3078 &sub->parent_link_pending);
3079}
3080
3081static void
3082weston_subsurface_link_surface(struct weston_subsurface *sub,
3083 struct weston_surface *surface)
3084{
3085 sub->surface = surface;
3086 sub->surface_destroy_listener.notify =
3087 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003088 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089 &sub->surface_destroy_listener);
3090}
3091
3092static void
3093weston_subsurface_destroy(struct weston_subsurface *sub)
3094{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003095 struct weston_view *view, *next;
3096
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097 assert(sub->surface);
3098
3099 if (sub->resource) {
3100 assert(weston_surface_to_subsurface(sub->surface) == sub);
3101 assert(sub->parent_destroy_listener.notify ==
3102 subsurface_handle_parent_destroy);
3103
George Kiagiadakised04d382014-06-13 18:10:26 +02003104 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3105 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003106 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003107 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003108
Pekka Paalanene67858b2013-04-25 13:57:42 +03003109 if (sub->parent)
3110 weston_subsurface_unlink_parent(sub);
3111
Jason Ekstrand7b982072014-05-20 14:33:03 -05003112 weston_surface_state_fini(&sub->cached);
3113 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003114
3115 sub->surface->configure = NULL;
3116 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003117 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003118 } else {
3119 /* the dummy weston_subsurface for the parent itself */
3120 assert(sub->parent_destroy_listener.notify == NULL);
3121 wl_list_remove(&sub->parent_link);
3122 wl_list_remove(&sub->parent_link_pending);
3123 }
3124
3125 wl_list_remove(&sub->surface_destroy_listener.link);
3126 free(sub);
3127}
3128
3129static const struct wl_subsurface_interface subsurface_implementation = {
3130 subsurface_destroy,
3131 subsurface_set_position,
3132 subsurface_place_above,
3133 subsurface_place_below,
3134 subsurface_set_sync,
3135 subsurface_set_desync
3136};
3137
3138static struct weston_subsurface *
3139weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3140 struct weston_surface *parent)
3141{
3142 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003143 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144
Bryce Harringtonde16d892014-11-20 22:21:57 -08003145 sub = zalloc(sizeof *sub);
3146 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003147 return NULL;
3148
Jason Ekstranda7af7042013-10-12 22:38:11 -05003149 wl_list_init(&sub->unused_views);
3150
Jason Ekstranda85118c2013-06-27 20:17:02 -05003151 sub->resource =
3152 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153 if (!sub->resource) {
3154 free(sub);
3155 return NULL;
3156 }
3157
Jason Ekstranda85118c2013-06-27 20:17:02 -05003158 wl_resource_set_implementation(sub->resource,
3159 &subsurface_implementation,
3160 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003161 weston_subsurface_link_surface(sub, surface);
3162 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003163 weston_surface_state_init(&sub->cached);
3164 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003165 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166
3167 return sub;
3168}
3169
3170/* Create a dummy subsurface for having the parent itself in its
3171 * sub-surface lists. Makes stacking order manipulation easy.
3172 */
3173static struct weston_subsurface *
3174weston_subsurface_create_for_parent(struct weston_surface *parent)
3175{
3176 struct weston_subsurface *sub;
3177
Bryce Harringtonde16d892014-11-20 22:21:57 -08003178 sub = zalloc(sizeof *sub);
3179 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003180 return NULL;
3181
3182 weston_subsurface_link_surface(sub, parent);
3183 sub->parent = parent;
3184 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3185 wl_list_insert(&parent->subsurface_list_pending,
3186 &sub->parent_link_pending);
3187
3188 return sub;
3189}
3190
3191static void
3192subcompositor_get_subsurface(struct wl_client *client,
3193 struct wl_resource *resource,
3194 uint32_t id,
3195 struct wl_resource *surface_resource,
3196 struct wl_resource *parent_resource)
3197{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003198 struct weston_surface *surface =
3199 wl_resource_get_user_data(surface_resource);
3200 struct weston_surface *parent =
3201 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003202 struct weston_subsurface *sub;
3203 static const char where[] = "get_subsurface: wl_subsurface@";
3204
3205 if (surface == parent) {
3206 wl_resource_post_error(resource,
3207 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3208 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003209 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003210 return;
3211 }
3212
3213 if (weston_surface_to_subsurface(surface)) {
3214 wl_resource_post_error(resource,
3215 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3216 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003217 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003218 return;
3219 }
3220
Pekka Paalanen50b67472014-10-01 15:02:41 +03003221 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3222 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003223 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003224
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003225 if (weston_surface_get_main_surface(parent) == surface) {
3226 wl_resource_post_error(resource,
3227 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3228 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003229 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003230 return;
3231 }
3232
Pekka Paalanene67858b2013-04-25 13:57:42 +03003233 /* make sure the parent is in its own list */
3234 if (wl_list_empty(&parent->subsurface_list)) {
3235 if (!weston_subsurface_create_for_parent(parent)) {
3236 wl_resource_post_no_memory(resource);
3237 return;
3238 }
3239 }
3240
3241 sub = weston_subsurface_create(id, surface, parent);
3242 if (!sub) {
3243 wl_resource_post_no_memory(resource);
3244 return;
3245 }
3246
3247 surface->configure = subsurface_configure;
3248 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003249 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003250}
3251
3252static void
3253subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3254{
3255 wl_resource_destroy(resource);
3256}
3257
3258static const struct wl_subcompositor_interface subcompositor_interface = {
3259 subcompositor_destroy,
3260 subcompositor_get_subsurface
3261};
3262
3263static void
3264bind_subcompositor(struct wl_client *client,
3265 void *data, uint32_t version, uint32_t id)
3266{
3267 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003268 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003269
Jason Ekstranda85118c2013-06-27 20:17:02 -05003270 resource =
3271 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003272 if (resource == NULL) {
3273 wl_client_post_no_memory(client);
3274 return;
3275 }
3276 wl_resource_set_implementation(resource, &subcompositor_interface,
3277 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003278}
3279
3280static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003281weston_compositor_dpms(struct weston_compositor *compositor,
3282 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003283{
3284 struct weston_output *output;
3285
3286 wl_list_for_each(output, &compositor->output_list, link)
3287 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003288 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003289}
3290
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003291WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003292weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003293{
Neil Roberts8b62e202013-09-30 13:14:47 +01003294 uint32_t old_state = compositor->state;
3295
3296 /* The state needs to be changed before emitting the wake
3297 * signal because that may try to schedule a repaint which
3298 * will not work if the compositor is still sleeping */
3299 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3300
3301 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003302 case WESTON_COMPOSITOR_SLEEPING:
3303 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3304 /* fall through */
3305 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003306 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003307 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003308 /* fall through */
3309 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003310 wl_event_source_timer_update(compositor->idle_source,
3311 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003312 }
3313}
3314
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003315WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003316weston_compositor_offscreen(struct weston_compositor *compositor)
3317{
3318 switch (compositor->state) {
3319 case WESTON_COMPOSITOR_OFFSCREEN:
3320 return;
3321 case WESTON_COMPOSITOR_SLEEPING:
3322 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3323 /* fall through */
3324 default:
3325 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3326 wl_event_source_timer_update(compositor->idle_source, 0);
3327 }
3328}
3329
3330WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003331weston_compositor_sleep(struct weston_compositor *compositor)
3332{
3333 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3334 return;
3335
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003336 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003337 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3338 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3339}
3340
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003341static int
3342idle_handler(void *data)
3343{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003344 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003345
3346 if (compositor->idle_inhibit)
3347 return 1;
3348
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003349 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003350 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003351
3352 return 1;
3353}
3354
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003355WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003356weston_plane_init(struct weston_plane *plane,
3357 struct weston_compositor *ec,
3358 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003359{
3360 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003361 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003362 plane->x = x;
3363 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003364 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003365
3366 /* Init the link so that the call to wl_list_remove() when releasing
3367 * the plane without ever stacking doesn't lead to a crash */
3368 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003369}
3370
3371WL_EXPORT void
3372weston_plane_release(struct weston_plane *plane)
3373{
Xiong Zhang97116532013-10-23 13:58:31 +08003374 struct weston_view *view;
3375
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003376 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003377 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003378
Xiong Zhang97116532013-10-23 13:58:31 +08003379 wl_list_for_each(view, &plane->compositor->view_list, link) {
3380 if (view->plane == plane)
3381 view->plane = NULL;
3382 }
3383
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003384 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003385}
3386
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003387WL_EXPORT void
3388weston_compositor_stack_plane(struct weston_compositor *ec,
3389 struct weston_plane *plane,
3390 struct weston_plane *above)
3391{
3392 if (above)
3393 wl_list_insert(above->link.prev, &plane->link);
3394 else
3395 wl_list_insert(&ec->plane_list, &plane->link);
3396}
3397
Casey Dahlin9074db52012-04-19 22:50:09 -04003398static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003399{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003400 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003401}
3402
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003403static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003404bind_output(struct wl_client *client,
3405 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003406{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003407 struct weston_output *output = data;
3408 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003409 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003410
Jason Ekstranda85118c2013-06-27 20:17:02 -05003411 resource = wl_resource_create(client, &wl_output_interface,
3412 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003413 if (resource == NULL) {
3414 wl_client_post_no_memory(client);
3415 return;
3416 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003417
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003418 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003419 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003420
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003421 wl_output_send_geometry(resource,
3422 output->x,
3423 output->y,
3424 output->mm_width,
3425 output->mm_height,
3426 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003427 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003428 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003429 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003430 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003431 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003432
3433 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003434 wl_output_send_mode(resource,
3435 mode->flags,
3436 mode->width,
3437 mode->height,
3438 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003439 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003440
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003441 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003442 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003443}
3444
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003445/* Move other outputs when one is removed so the space remains contiguos. */
3446static void
3447weston_compositor_remove_output(struct weston_compositor *compositor,
3448 struct weston_output *remove_output)
3449{
3450 struct weston_output *output;
3451 int offset = 0;
3452
3453 wl_list_for_each(output, &compositor->output_list, link) {
3454 if (output == remove_output) {
3455 offset = output->width;
3456 continue;
3457 }
3458
3459 if (offset > 0) {
3460 weston_output_move(output,
3461 output->x - offset, output->y);
3462 output->dirty = 1;
3463 }
3464 }
3465}
3466
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003467WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003468weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003469{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003470 struct wl_resource *resource;
3471
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003472 output->destroying = 1;
3473
Pekka Paalanen133e4392014-09-23 22:08:46 -04003474 weston_presentation_feedback_discard_list(&output->feedback_list);
3475
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003476 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003477 wl_list_remove(&output->link);
3478
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003479 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003480 wl_signal_emit(&output->destroy_signal, output);
3481
Richard Hughesafe690c2013-05-02 10:10:04 +01003482 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003483 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003484 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003485 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003486
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003487 wl_resource_for_each(resource, &output->resource_list) {
3488 wl_resource_set_destructor(resource, NULL);
3489 }
3490
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003491 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003492}
3493
Scott Moreau1bad5db2012-08-18 01:04:05 -06003494static void
3495weston_output_compute_transform(struct weston_output *output)
3496{
3497 struct weston_matrix transform;
3498 int flip;
3499
3500 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003501 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003502
3503 switch(output->transform) {
3504 case WL_OUTPUT_TRANSFORM_FLIPPED:
3505 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3506 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3507 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003508 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003509 flip = -1;
3510 break;
3511 default:
3512 flip = 1;
3513 break;
3514 }
3515
3516 switch(output->transform) {
3517 case WL_OUTPUT_TRANSFORM_NORMAL:
3518 case WL_OUTPUT_TRANSFORM_FLIPPED:
3519 transform.d[0] = flip;
3520 transform.d[1] = 0;
3521 transform.d[4] = 0;
3522 transform.d[5] = 1;
3523 break;
3524 case WL_OUTPUT_TRANSFORM_90:
3525 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3526 transform.d[0] = 0;
3527 transform.d[1] = -flip;
3528 transform.d[4] = 1;
3529 transform.d[5] = 0;
3530 break;
3531 case WL_OUTPUT_TRANSFORM_180:
3532 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3533 transform.d[0] = -flip;
3534 transform.d[1] = 0;
3535 transform.d[4] = 0;
3536 transform.d[5] = -1;
3537 break;
3538 case WL_OUTPUT_TRANSFORM_270:
3539 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3540 transform.d[0] = 0;
3541 transform.d[1] = flip;
3542 transform.d[4] = -1;
3543 transform.d[5] = 0;
3544 break;
3545 default:
3546 break;
3547 }
3548
3549 weston_matrix_multiply(&output->matrix, &transform);
3550}
3551
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003552WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003553weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003554{
Scott Moreau850ca422012-05-21 15:21:25 -06003555 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003556
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003557 weston_matrix_init(&output->matrix);
3558 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003559 -(output->x + output->width / 2.0),
3560 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003561
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003562 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003563 2.0 / output->width,
3564 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003565
Scott Moreauccbf29d2012-02-22 14:21:41 -07003566 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003567 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003568 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003569 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3570 output->zoom.trans_y, 0);
3571 weston_matrix_scale(&output->matrix, magnification,
3572 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003573 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003574
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003575 weston_output_compute_transform(output);
3576
Scott Moreauccbf29d2012-02-22 14:21:41 -07003577 output->dirty = 0;
3578}
3579
Scott Moreau1bad5db2012-08-18 01:04:05 -06003580static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003581weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003582{
3583 output->transform = transform;
3584
3585 switch (transform) {
3586 case WL_OUTPUT_TRANSFORM_90:
3587 case WL_OUTPUT_TRANSFORM_270:
3588 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3589 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3590 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003591 output->width = output->current_mode->height;
3592 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003593 break;
3594 case WL_OUTPUT_TRANSFORM_NORMAL:
3595 case WL_OUTPUT_TRANSFORM_180:
3596 case WL_OUTPUT_TRANSFORM_FLIPPED:
3597 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003598 output->width = output->current_mode->width;
3599 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003600 break;
3601 default:
3602 break;
3603 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003604
Hardening57388e42013-09-18 23:56:36 +02003605 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003606 output->width /= scale;
3607 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003608}
3609
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003610static void
3611weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003612{
3613 output->x = x;
3614 output->y = y;
3615
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003616 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003617 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003618 output->width,
3619 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003620}
3621
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003622WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003623weston_output_move(struct weston_output *output, int x, int y)
3624{
3625 pixman_region32_t old_region;
3626 struct wl_resource *resource;
3627
3628 output->move_x = x - output->x;
3629 output->move_y = y - output->y;
3630
3631 if (output->move_x == 0 && output->move_y == 0)
3632 return;
3633
3634 pixman_region32_init(&old_region);
3635 pixman_region32_copy(&old_region, &output->region);
3636
3637 weston_output_init_geometry(output, x, y);
3638
3639 output->dirty = 1;
3640
3641 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003642 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003643
3644 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003645 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003646 wl_output_send_geometry(resource,
3647 output->x,
3648 output->y,
3649 output->mm_width,
3650 output->mm_height,
3651 output->subpixel,
3652 output->make,
3653 output->model,
3654 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003655
3656 if (wl_resource_get_version(resource) >= 2)
3657 wl_output_send_done(resource);
3658 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003659}
3660
3661WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003662weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003663 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003664 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003665{
3666 output->compositor = c;
3667 output->x = x;
3668 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003669 output->mm_width = mm_width;
3670 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003671 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003672 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003673
Alexander Larsson0b135062013-05-28 16:23:36 +02003674 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003675 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003676
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003677 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003678 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003679
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003680 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003681 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003682 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003683 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003684 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003685
Casey Dahlin58ba1372012-04-19 22:50:08 -04003686 output->id = ffs(~output->compositor->output_id_pool) - 1;
3687 output->compositor->output_id_pool |= 1 << output->id;
3688
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003689 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003690 wl_global_create(c->wl_display, &wl_output_interface, 2,
3691 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003692 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003693}
3694
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003695WL_EXPORT void
3696weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003697 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003698 wl_fixed_t *x, wl_fixed_t *y)
3699{
3700 wl_fixed_t tx, ty;
3701 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003702 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003703
Hardeningff39efa2013-09-18 23:56:35 +02003704 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3705 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003706
3707 switch(output->transform) {
3708 case WL_OUTPUT_TRANSFORM_NORMAL:
3709 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003710 tx = device_x;
3711 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003712 break;
3713 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003714 tx = device_y;
3715 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003716 break;
3717 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003718 tx = width - device_x;
3719 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003720 break;
3721 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003722 tx = width - device_y;
3723 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003724 break;
3725 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003726 tx = width - device_x;
3727 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003728 break;
3729 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003730 tx = width - device_y;
3731 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003732 break;
3733 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003734 tx = device_x;
3735 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003736 break;
3737 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003738 tx = device_y;
3739 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003740 break;
3741 }
3742
Neil Robertseb5a2002014-05-01 16:13:55 +01003743 tx /= output->current_scale;
3744 ty /= output->current_scale;
3745
3746 if (output->zoom.active) {
3747 zoom_scale = output->zoom.spring_z.current;
3748 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3749 output->width / 2.0f *
3750 (zoom_scale + output->zoom.trans_x));
3751 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3752 output->height / 2.0f *
3753 (zoom_scale + output->zoom.trans_y));
3754 tx = wl_fixed_from_double(zx);
3755 ty = wl_fixed_from_double(zy);
3756 }
3757
3758 *x = tx + wl_fixed_from_int(output->x);
3759 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003760}
3761
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003762static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003763destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003764{
Jonny Lamb74130762013-11-26 18:19:46 +01003765 struct weston_surface *surface =
3766 wl_resource_get_user_data(resource);
3767
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003768 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003769 surface->pending.buffer_viewport.buffer.src_width =
3770 wl_fixed_from_int(-1);
3771 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003772 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003773}
3774
3775static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003776viewport_destroy(struct wl_client *client,
3777 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003778{
3779 wl_resource_destroy(resource);
3780}
3781
3782static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003783viewport_set(struct wl_client *client,
3784 struct wl_resource *resource,
3785 wl_fixed_t src_x,
3786 wl_fixed_t src_y,
3787 wl_fixed_t src_width,
3788 wl_fixed_t src_height,
3789 int32_t dst_width,
3790 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003791{
Jonny Lamb74130762013-11-26 18:19:46 +01003792 struct weston_surface *surface =
3793 wl_resource_get_user_data(resource);
3794
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003795 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003796
Jonny Lamb8ae35902013-11-26 18:19:45 +01003797 if (wl_fixed_to_double(src_width) < 0 ||
3798 wl_fixed_to_double(src_height) < 0) {
3799 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003800 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003801 "source dimensions must be non-negative (%fx%f)",
3802 wl_fixed_to_double(src_width),
3803 wl_fixed_to_double(src_height));
3804 return;
3805 }
3806
3807 if (dst_width <= 0 || dst_height <= 0) {
3808 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003809 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003810 "destination dimensions must be positive (%dx%d)",
3811 dst_width, dst_height);
3812 return;
3813 }
3814
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003815 surface->pending.buffer_viewport.buffer.src_x = src_x;
3816 surface->pending.buffer_viewport.buffer.src_y = src_y;
3817 surface->pending.buffer_viewport.buffer.src_width = src_width;
3818 surface->pending.buffer_viewport.buffer.src_height = src_height;
3819 surface->pending.buffer_viewport.surface.width = dst_width;
3820 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003821 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003822}
3823
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003824static void
3825viewport_set_source(struct wl_client *client,
3826 struct wl_resource *resource,
3827 wl_fixed_t src_x,
3828 wl_fixed_t src_y,
3829 wl_fixed_t src_width,
3830 wl_fixed_t src_height)
3831{
3832 struct weston_surface *surface =
3833 wl_resource_get_user_data(resource);
3834
3835 assert(surface->viewport_resource != NULL);
3836
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003837 if (src_width == wl_fixed_from_int(-1) &&
3838 src_height == wl_fixed_from_int(-1)) {
3839 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003840 surface->pending.buffer_viewport.buffer.src_width =
3841 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003842 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003843 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003844 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003845
3846 if (src_width <= 0 || src_height <= 0) {
3847 wl_resource_post_error(resource,
3848 WL_VIEWPORT_ERROR_BAD_VALUE,
3849 "source size must be positive (%fx%f)",
3850 wl_fixed_to_double(src_width),
3851 wl_fixed_to_double(src_height));
3852 return;
3853 }
3854
3855 surface->pending.buffer_viewport.buffer.src_x = src_x;
3856 surface->pending.buffer_viewport.buffer.src_y = src_y;
3857 surface->pending.buffer_viewport.buffer.src_width = src_width;
3858 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003859 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003860}
3861
3862static void
3863viewport_set_destination(struct wl_client *client,
3864 struct wl_resource *resource,
3865 int32_t dst_width,
3866 int32_t dst_height)
3867{
3868 struct weston_surface *surface =
3869 wl_resource_get_user_data(resource);
3870
3871 assert(surface->viewport_resource != NULL);
3872
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003873 if (dst_width == -1 && dst_height == -1) {
3874 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003875 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003876 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003877 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003878 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003879
3880 if (dst_width <= 0 || dst_height <= 0) {
3881 wl_resource_post_error(resource,
3882 WL_VIEWPORT_ERROR_BAD_VALUE,
3883 "destination size must be positive (%dx%d)",
3884 dst_width, dst_height);
3885 return;
3886 }
3887
3888 surface->pending.buffer_viewport.surface.width = dst_width;
3889 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003890 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003891}
3892
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003893static const struct wl_viewport_interface viewport_interface = {
3894 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003895 viewport_set,
3896 viewport_set_source,
3897 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003898};
3899
3900static void
3901scaler_destroy(struct wl_client *client,
3902 struct wl_resource *resource)
3903{
3904 wl_resource_destroy(resource);
3905}
3906
3907static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003908scaler_get_viewport(struct wl_client *client,
3909 struct wl_resource *scaler,
3910 uint32_t id,
3911 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003912{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003913 int version = wl_resource_get_version(scaler);
3914 struct weston_surface *surface =
3915 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003916 struct wl_resource *resource;
3917
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003918 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003919 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003920 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3921 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003922 return;
3923 }
3924
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003925 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003926 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003927 if (resource == NULL) {
3928 wl_client_post_no_memory(client);
3929 return;
3930 }
3931
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003932 wl_resource_set_implementation(resource, &viewport_interface,
3933 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003934
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003935 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003936}
3937
3938static const struct wl_scaler_interface scaler_interface = {
3939 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003940 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003941};
3942
3943static void
3944bind_scaler(struct wl_client *client,
3945 void *data, uint32_t version, uint32_t id)
3946{
3947 struct wl_resource *resource;
3948
3949 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003950 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003951 if (resource == NULL) {
3952 wl_client_post_no_memory(client);
3953 return;
3954 }
3955
3956 wl_resource_set_implementation(resource, &scaler_interface,
3957 NULL, NULL);
3958}
3959
3960static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003961destroy_presentation_feedback(struct wl_resource *feedback_resource)
3962{
3963 struct weston_presentation_feedback *feedback;
3964
3965 feedback = wl_resource_get_user_data(feedback_resource);
3966
3967 wl_list_remove(&feedback->link);
3968 free(feedback);
3969}
3970
3971static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003972presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3973{
3974 wl_resource_destroy(resource);
3975}
3976
3977static void
3978presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003979 struct wl_resource *presentation_resource,
3980 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003981 uint32_t callback)
3982{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003983 struct weston_surface *surface;
3984 struct weston_presentation_feedback *feedback;
3985
3986 surface = wl_resource_get_user_data(surface_resource);
3987
Bryce Harringtonde16d892014-11-20 22:21:57 -08003988 feedback = zalloc(sizeof *feedback);
3989 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04003990 goto err_calloc;
3991
3992 feedback->resource = wl_resource_create(client,
3993 &presentation_feedback_interface,
3994 1, callback);
3995 if (!feedback->resource)
3996 goto err_create;
3997
3998 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3999 destroy_presentation_feedback);
4000
4001 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
4002
4003 return;
4004
4005err_create:
4006 free(feedback);
4007
4008err_calloc:
4009 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004010}
4011
4012static const struct presentation_interface presentation_implementation = {
4013 presentation_destroy,
4014 presentation_feedback
4015};
4016
4017static void
4018bind_presentation(struct wl_client *client,
4019 void *data, uint32_t version, uint32_t id)
4020{
4021 struct weston_compositor *compositor = data;
4022 struct wl_resource *resource;
4023
4024 resource = wl_resource_create(client, &presentation_interface,
4025 MIN(version, 1), id);
4026 if (resource == NULL) {
4027 wl_client_post_no_memory(client);
4028 return;
4029 }
4030
4031 wl_resource_set_implementation(resource, &presentation_implementation,
4032 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004033 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004034}
4035
4036static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004037compositor_bind(struct wl_client *client,
4038 void *data, uint32_t version, uint32_t id)
4039{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004040 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004041 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004042
Jason Ekstranda85118c2013-06-27 20:17:02 -05004043 resource = wl_resource_create(client, &wl_compositor_interface,
4044 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004045 if (resource == NULL) {
4046 wl_client_post_no_memory(client);
4047 return;
4048 }
4049
4050 wl_resource_set_implementation(resource, &compositor_interface,
4051 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004052}
4053
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004054static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004055log_uname(void)
4056{
4057 struct utsname usys;
4058
4059 uname(&usys);
4060
4061 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4062 usys.version, usys.machine);
4063}
4064
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004065WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004066weston_environment_get_fd(const char *env)
4067{
4068 char *e, *end;
4069 int fd, flags;
4070
4071 e = getenv(env);
4072 if (!e)
4073 return -1;
4074 fd = strtol(e, &end, 0);
4075 if (*end != '\0')
4076 return -1;
4077
4078 flags = fcntl(fd, F_GETFD);
4079 if (flags == -1)
4080 return -1;
4081
4082 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4083 unsetenv(env);
4084
4085 return fd;
4086}
4087
Pekka Paalanenb5026542014-11-12 15:09:24 +02004088static void
4089timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4090 uint32_t key, void *data)
4091{
4092 struct weston_compositor *compositor = data;
4093
4094 if (weston_timeline_enabled_)
4095 weston_timeline_close();
4096 else
4097 weston_timeline_open(compositor);
4098}
4099
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004100WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004101weston_compositor_init(struct weston_compositor *ec,
4102 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004103 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004104 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004105{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004106 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004107 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004108 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004109
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004110 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004111 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004112 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004113 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004114 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004115 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004116 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004117 wl_signal_init(&ec->idle_signal);
4118 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004119 wl_signal_init(&ec->show_input_panel_signal);
4120 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004121 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004122 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004123 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004124 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004125 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004126 wl_signal_init(&ec->session_signal);
4127 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004128
Casey Dahlin58ba1372012-04-19 22:50:08 -04004129 ec->output_id_pool = 0;
4130
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004131 if (!wl_global_create(display, &wl_compositor_interface, 3,
4132 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004133 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004134
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004135 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4136 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004137 return -1;
4138
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004139 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004140 ec, bind_scaler))
4141 return -1;
4142
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004143 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4144 ec, bind_presentation))
4145 return -1;
4146
Jason Ekstranda7af7042013-10-12 22:38:11 -05004147 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004148 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004149 wl_list_init(&ec->layer_list);
4150 wl_list_init(&ec->seat_list);
4151 wl_list_init(&ec->output_list);
4152 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004153 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004154 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004155 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004156 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004157 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004158
Xiong Zhang97116532013-10-23 13:58:31 +08004159 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004160 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004161
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004162 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4163 weston_config_section_get_string(s, "keymap_rules",
4164 (char **) &xkb_names.rules, NULL);
4165 weston_config_section_get_string(s, "keymap_model",
4166 (char **) &xkb_names.model, NULL);
4167 weston_config_section_get_string(s, "keymap_layout",
4168 (char **) &xkb_names.layout, NULL);
4169 weston_config_section_get_string(s, "keymap_variant",
4170 (char **) &xkb_names.variant, NULL);
4171 weston_config_section_get_string(s, "keymap_options",
4172 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004173
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004174 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4175 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004176
Jonny Lamb66a41a02014-08-12 14:58:25 +02004177 weston_config_section_get_int(s, "repeat-rate",
4178 &ec->kb_repeat_rate, 40);
4179 weston_config_section_get_int(s, "repeat-delay",
4180 &ec->kb_repeat_delay, 400);
4181
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004182 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004183
4184 wl_data_device_manager_init(ec->wl_display);
4185
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004186 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004187
Daniel Stone725c2c32012-06-22 14:04:36 +01004188 loop = wl_display_get_event_loop(ec->wl_display);
4189 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4190 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4191
4192 ec->input_loop = wl_event_loop_create();
4193
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004194 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4195 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4196
Pekka Paalanenb5026542014-11-12 15:09:24 +02004197 weston_compositor_add_debug_binding(ec, KEY_T,
4198 timeline_key_binding_handler, ec);
4199
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004200 weston_compositor_schedule_repaint(ec);
4201
Daniel Stone725c2c32012-06-22 14:04:36 +01004202 return 0;
4203}
4204
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004205WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004206weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004207{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004208 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004209
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004210 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004211 if (ec->input_loop_source)
4212 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004213
Matt Roper361d2ad2011-08-29 13:52:23 -07004214 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004215 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004216 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004217
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004218 if (ec->renderer)
4219 ec->renderer->destroy(ec);
4220
Daniel Stone325fc2d2012-05-30 16:31:58 +01004221 weston_binding_list_destroy_all(&ec->key_binding_list);
4222 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004223 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004224 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004225 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004226
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004227 weston_plane_release(&ec->primary_plane);
4228
Jonas Ådahlc97af922012-03-28 22:36:09 +02004229 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004230
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004231 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004232}
4233
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004234WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004235weston_compositor_exit_with_code(struct weston_compositor *compositor,
4236 int exit_code)
4237{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004238 if (compositor->exit_code == EXIT_SUCCESS)
4239 compositor->exit_code = exit_code;
4240
Frederic Plourdec336f062014-10-29 14:44:33 -04004241 wl_display_terminate(compositor->wl_display);
4242}
4243
4244WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004245weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4246 const struct weston_pointer_grab_interface *interface)
4247{
4248 struct weston_seat *seat;
4249
4250 ec->default_pointer_grab = interface;
4251 wl_list_for_each(seat, &ec->seat_list, link) {
4252 if (seat->pointer) {
4253 weston_pointer_set_default_grab(seat->pointer,
4254 interface);
4255 }
4256 }
4257}
4258
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004259WL_EXPORT int
4260weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4261 clockid_t clk_id)
4262{
4263 struct timespec ts;
4264
4265 if (clock_gettime(clk_id, &ts) < 0)
4266 return -1;
4267
4268 compositor->presentation_clock = clk_id;
4269
4270 return 0;
4271}
4272
4273/*
4274 * For choosing the software clock, when the display hardware or API
4275 * does not expose a compatible presentation timestamp.
4276 */
4277WL_EXPORT int
4278weston_compositor_set_presentation_clock_software(
4279 struct weston_compositor *compositor)
4280{
4281 /* In order of preference */
4282 static const clockid_t clocks[] = {
4283 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4284 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4285 CLOCK_MONOTONIC, /* no jumps, may crawl */
4286 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4287 CLOCK_REALTIME /* may jump and crawl */
4288 };
4289 unsigned i;
4290
4291 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4292 if (weston_compositor_set_presentation_clock(compositor,
4293 clocks[i]) == 0)
4294 return 0;
4295
4296 weston_log("Error: no suitable presentation clock available.\n");
4297
4298 return -1;
4299}
4300
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004301WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004302weston_version(int *major, int *minor, int *micro)
4303{
4304 *major = WESTON_VERSION_MAJOR;
4305 *minor = WESTON_VERSION_MINOR;
4306 *micro = WESTON_VERSION_MICRO;
4307}
4308
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004309static const char *
4310clock_name(clockid_t clk_id)
4311{
4312 static const char *names[] = {
4313 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4314 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4315 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4316 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4317 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4318 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4319 };
4320
4321 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4322 return "unknown";
4323
4324 return names[clk_id];
4325}
4326
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004327static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004328 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004329 const char *desc;
4330} capability_strings[] = {
4331 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004332 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004333};
4334
4335static void
4336weston_compositor_log_capabilities(struct weston_compositor *compositor)
4337{
4338 unsigned i;
4339 int yes;
4340
4341 weston_log("Compositor capabilities:\n");
4342 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4343 yes = compositor->capabilities & capability_strings[i].bit;
4344 weston_log_continue(STAMP_SPACE "%s %s\n",
4345 capability_strings[i].desc,
4346 yes ? "yes" : "no");
4347 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004348
4349 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4350 clock_name(compositor->presentation_clock),
4351 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004352}
4353
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004354static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004355{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004356 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004357
Martin Minarik6d118362012-06-07 18:01:59 +02004358 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004359 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004360
4361 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004362}
4363
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004364#ifdef HAVE_LIBUNWIND
4365
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004366static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004367print_backtrace(void)
4368{
4369 unw_cursor_t cursor;
4370 unw_context_t context;
4371 unw_word_t off;
4372 unw_proc_info_t pip;
4373 int ret, i = 0;
4374 char procname[256];
4375 const char *filename;
4376 Dl_info dlinfo;
4377
4378 pip.unwind_info = NULL;
4379 ret = unw_getcontext(&context);
4380 if (ret) {
4381 weston_log("unw_getcontext: %d\n", ret);
4382 return;
4383 }
4384
4385 ret = unw_init_local(&cursor, &context);
4386 if (ret) {
4387 weston_log("unw_init_local: %d\n", ret);
4388 return;
4389 }
4390
4391 ret = unw_step(&cursor);
4392 while (ret > 0) {
4393 ret = unw_get_proc_info(&cursor, &pip);
4394 if (ret) {
4395 weston_log("unw_get_proc_info: %d\n", ret);
4396 break;
4397 }
4398
4399 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4400 if (ret && ret != -UNW_ENOMEM) {
4401 if (ret != -UNW_EUNSPEC)
4402 weston_log("unw_get_proc_name: %d\n", ret);
4403 procname[0] = '?';
4404 procname[1] = 0;
4405 }
4406
4407 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4408 *dlinfo.dli_fname)
4409 filename = dlinfo.dli_fname;
4410 else
4411 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004412
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004413 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4414 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4415
4416 ret = unw_step(&cursor);
4417 if (ret < 0)
4418 weston_log("unw_step: %d\n", ret);
4419 }
4420}
4421
4422#else
4423
4424static void
4425print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004426{
4427 void *buffer[32];
4428 int i, count;
4429 Dl_info info;
4430
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004431 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4432 for (i = 0; i < count; i++) {
4433 dladdr(buffer[i], &info);
4434 weston_log(" [%016lx] %s (%s)\n",
4435 (long) buffer[i],
4436 info.dli_sname ? info.dli_sname : "--",
4437 info.dli_fname);
4438 }
4439}
4440
4441#endif
4442
4443static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004444on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004445{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004446 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004447 * then call the backend restore function, which will switch
4448 * back to the vt we launched from or ungrab X etc and then
4449 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004450 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004451 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004452 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004453
Peter Maatmane5b42e42013-03-27 22:38:53 +01004454 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004455
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004456 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004457
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004458 segv_compositor->restore(segv_compositor);
4459
4460 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004461}
4462
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004463WL_EXPORT void *
4464weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004465{
4466 char path[PATH_MAX];
4467 void *module, *init;
4468
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004469 if (name == NULL)
4470 return NULL;
4471
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004472 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004473 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004474 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004475 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004476
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004477 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4478 if (module) {
4479 weston_log("Module '%s' already loaded\n", path);
4480 dlclose(module);
4481 return NULL;
4482 }
4483
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004484 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004485 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004486 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004487 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004488 return NULL;
4489 }
4490
4491 init = dlsym(module, entrypoint);
4492 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004493 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004494 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004495 return NULL;
4496 }
4497
4498 return init;
4499}
4500
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004501static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004502load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004503 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004504{
4505 const char *p, *end;
4506 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004507 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004508 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004509
4510 if (modules == NULL)
4511 return 0;
4512
4513 p = modules;
4514 while (*p) {
4515 end = strchrnul(p, ',');
4516 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004517 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004518 if (!module_init)
4519 return -1;
4520 if (module_init(ec, argc, argv) < 0)
4521 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004522 p = end;
4523 while (*p == ',')
4524 p++;
4525
4526 }
4527
4528 return 0;
4529}
4530
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004531static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004532 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4533
4534static const char xdg_wrong_message[] =
4535 "fatal: environment variable XDG_RUNTIME_DIR\n"
4536 "is set to \"%s\", which is not a directory.\n";
4537
4538static const char xdg_wrong_mode_message[] =
4539 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004540 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4541 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004542
4543static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004544 "Refer to your distribution on how to get it, or\n"
4545 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4546 "on how to implement it.\n";
4547
Martin Minarik37032f82012-06-18 20:15:18 +02004548static void
4549verify_xdg_runtime_dir(void)
4550{
4551 char *dir = getenv("XDG_RUNTIME_DIR");
4552 struct stat s;
4553
4554 if (!dir) {
4555 weston_log(xdg_error_message);
4556 weston_log_continue(xdg_detail_message);
4557 exit(EXIT_FAILURE);
4558 }
4559
4560 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4561 weston_log(xdg_wrong_message, dir);
4562 weston_log_continue(xdg_detail_message);
4563 exit(EXIT_FAILURE);
4564 }
4565
4566 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4567 weston_log(xdg_wrong_mode_message,
4568 dir, s.st_mode & 0777, s.st_uid);
4569 weston_log_continue(xdg_detail_message);
4570 }
4571}
4572
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004573static int
4574usage(int error_code)
4575{
4576 fprintf(stderr,
4577 "Usage: weston [OPTIONS]\n\n"
4578 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4579 "Weston supports multiple backends, and depending on which backend is in use\n"
4580 "different options will be accepted.\n\n"
4581
4582
4583 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004584 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004585 " -B, --backend=MODULE\tBackend module, one of\n"
4586#if defined(BUILD_DRM_COMPOSITOR)
4587 "\t\t\t\tdrm-backend.so\n"
4588#endif
4589#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004590 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004591#endif
4592#if defined(BUILD_X11_COMPOSITOR)
4593 "\t\t\t\tx11-backend.so\n"
4594#endif
4595#if defined(BUILD_WAYLAND_COMPOSITOR)
4596 "\t\t\t\twayland-backend.so\n"
4597#endif
4598#if defined(BUILD_RDP_COMPOSITOR)
4599 "\t\t\t\trdp-backend.so\n"
4600#endif
4601#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4602 "\t\t\t\trpi-backend.so\n"
4603#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004604 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004605 " -S, --socket=NAME\tName of socket to listen on\n"
4606 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004607 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004608 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004609 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004610 " -h, --help\t\tThis help message\n\n");
4611
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004612#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004613 fprintf(stderr,
4614 "Options for drm-backend.so:\n\n"
4615 " --connector=ID\tBring up only this connector\n"
4616 " --seat=SEAT\t\tThe seat that weston should run on\n"
4617 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004618 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004619 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004620#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004621
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004622#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004623 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004624 "Options for fbdev-backend.so:\n\n"
4625 " --tty=TTY\t\tThe tty to use\n"
4626 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004627#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004628
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004629#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004630 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004631 "Options for x11-backend.so:\n\n"
4632 " --width=WIDTH\t\tWidth of X window\n"
4633 " --height=HEIGHT\tHeight of X window\n"
4634 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004635 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004636 " --output-count=COUNT\tCreate multiple outputs\n"
4637 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004638#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004639
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004640#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004641 fprintf(stderr,
4642 "Options for wayland-backend.so:\n\n"
4643 " --width=WIDTH\t\tWidth of Wayland surface\n"
4644 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004645 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004646 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004647 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004648 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004649 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004650 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004651#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004652
Pekka Paalanene31e0532013-05-22 18:03:07 +03004653#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4654 fprintf(stderr,
4655 "Options for rpi-backend.so:\n\n"
4656 " --tty=TTY\t\tThe tty to use\n"
4657 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4658 " --transform=TR\tThe output transformation, TR is one of:\n"
4659 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004660 " --opaque-regions\tEnable support for opaque regions, can be "
4661 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004662 "\n");
4663#endif
4664
Hardeningc077a842013-07-08 00:51:35 +02004665#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004666 fprintf(stderr,
4667 "Options for rdp-backend.so:\n\n"
4668 " --width=WIDTH\t\tWidth of desktop\n"
4669 " --height=HEIGHT\tHeight of desktop\n"
4670 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4671 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004672 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004673 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4674 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4675 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4676 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4677 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004678#endif
4679
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004680#if defined(BUILD_HEADLESS_COMPOSITOR)
4681 fprintf(stderr,
4682 "Options for headless-backend.so:\n\n"
4683 " --width=WIDTH\t\tWidth of memory surface\n"
4684 " --height=HEIGHT\tHeight of memory surface\n"
4685 " --transform=TR\tThe output transformation, TR is one of:\n"
4686 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4687 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4688#endif
4689
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004690 exit(error_code);
4691}
4692
Peter Maatmane5b42e42013-03-27 22:38:53 +01004693static void
4694catch_signals(void)
4695{
4696 struct sigaction action;
4697
4698 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4699 action.sa_sigaction = on_caught_signal;
4700 sigemptyset(&action.sa_mask);
4701 sigaction(SIGSEGV, &action, NULL);
4702 sigaction(SIGABRT, &action, NULL);
4703}
4704
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004705static void
4706handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4707{
4708 struct wl_client *client = data;
4709
4710 weston_log("Primary client died. Closing...\n");
4711
4712 wl_display_terminate(wl_client_get_display(client));
4713}
4714
Ryo Munakatad8deff62014-09-06 07:32:05 +09004715static char *
4716weston_choose_default_backend(void)
4717{
4718 char *backend = NULL;
4719
4720 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4721 backend = strdup("wayland-backend.so");
4722 else if (getenv("DISPLAY"))
4723 backend = strdup("x11-backend.so");
4724 else
4725 backend = strdup(WESTON_NATIVE_BACKEND);
4726
4727 return backend;
4728}
4729
4730static int
4731weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4732{
4733 if (socket_name) {
4734 if (wl_display_add_socket(display, socket_name)) {
4735 weston_log("fatal: failed to add socket: %m\n");
4736 return -1;
4737 }
4738 } else {
4739 socket_name = wl_display_add_socket_auto(display);
4740 if (!socket_name) {
4741 weston_log("fatal: failed to add socket: %m\n");
4742 return -1;
4743 }
4744 }
4745
4746 setenv("WAYLAND_DISPLAY", socket_name, 1);
4747
4748 return 0;
4749}
4750
Derek Foreman64a3df02014-10-23 12:24:18 -05004751static const struct { const char *name; uint32_t token; } transforms[] = {
4752 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4753 { "90", WL_OUTPUT_TRANSFORM_90 },
4754 { "180", WL_OUTPUT_TRANSFORM_180 },
4755 { "270", WL_OUTPUT_TRANSFORM_270 },
4756 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4757 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4758 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4759 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4760};
4761
4762WL_EXPORT int
4763weston_parse_transform(const char *transform, uint32_t *out)
4764{
4765 unsigned int i;
4766
4767 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4768 if (strcmp(transforms[i].name, transform) == 0) {
4769 *out = transforms[i].token;
4770 return 0;
4771 }
4772
4773 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4774 return -1;
4775}
4776
4777WL_EXPORT const char *
4778weston_transform_to_string(uint32_t output_transform)
4779{
4780 unsigned int i;
4781
4782 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4783 if (transforms[i].token == output_transform)
4784 return transforms[i].name;
4785
4786 return "<illegal value>";
4787}
4788
4789
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004790int main(int argc, char *argv[])
4791{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004792 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004793 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004794 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004795 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004796 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004797 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004798 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004799 int *argc, char *argv[],
4800 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004801 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004802 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004803 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004804 char *modules = NULL;
4805 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004806 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004807 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004808 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004809 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004810 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004811 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004812 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004813 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004814 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004815 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004816 struct wl_client *primary_client;
4817 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004818 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004819
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004820 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004821 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4822 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004823 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4824 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004825 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004826 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004827 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004828 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004829 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004830 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004831
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004832 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004833
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004834 if (help)
4835 usage(EXIT_SUCCESS);
4836
Scott Moreau12245142012-08-29 15:15:58 -06004837 if (version) {
4838 printf(PACKAGE_STRING "\n");
4839 return EXIT_SUCCESS;
4840 }
4841
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004842 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004843
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004844 weston_log("%s\n"
4845 STAMP_SPACE "%s\n"
4846 STAMP_SPACE "Bug reports to: %s\n"
4847 STAMP_SPACE "Build: %s\n",
4848 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004849 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004850 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004851
Martin Minarik37032f82012-06-18 20:15:18 +02004852 verify_xdg_runtime_dir();
4853
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004854 display = wl_display_create();
4855
Tiago Vignatti2116b892011-08-08 05:52:59 -07004856 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004857 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4858 display);
4859 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4860 display);
4861 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4862 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004863
4864 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004865 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4866 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004867
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304868 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4869 ret = EXIT_FAILURE;
4870 goto out_signals;
4871 }
4872
Pekka Paalanen588bee12014-05-07 16:26:25 +03004873 if (noconfig == 0)
4874 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004875 if (config != NULL) {
4876 weston_log("Using config file '%s'\n",
4877 weston_config_get_full_path(config));
4878 } else {
4879 weston_log("Starting with no config file.\n");
4880 }
4881 section = weston_config_get_section(config, "core", NULL, NULL);
4882
Ryo Munakata03faed22014-09-06 07:32:51 +09004883 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004884 weston_config_section_get_string(section, "backend", &backend,
4885 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004886 if (!backend)
4887 backend = weston_choose_default_backend();
4888 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004889
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004890 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304891 if (!backend_init) {
4892 ret = EXIT_FAILURE;
4893 goto out_signals;
4894 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004895
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004896 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004897 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004898 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304899 ret = EXIT_FAILURE;
4900 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004901 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004902
Peter Maatmane5b42e42013-03-27 22:38:53 +01004903 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004904 segv_compositor = ec;
4905
Frederic Plourde4a84c832014-10-30 15:06:34 -04004906 if (idle_time < 0)
4907 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4908 if (idle_time < 0)
4909 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004910 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004911 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004912 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004913
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004914 for (i = 1; i < argc; i++)
4915 weston_log("fatal: unhandled option: %s\n", argv[i]);
4916 if (argc > 1) {
4917 ret = EXIT_FAILURE;
4918 goto out;
4919 }
4920
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004921 weston_compositor_log_capabilities(ec);
4922
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004923 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4924 if (server_socket) {
4925 weston_log("Running with single client\n");
4926 fd = strtol(server_socket, &end, 0);
4927 if (*end != '\0')
4928 fd = -1;
4929 } else {
4930 fd = -1;
4931 }
4932
4933 if (fd != -1) {
4934 primary_client = wl_client_create(display, fd);
4935 if (!primary_client) {
4936 weston_log("fatal: failed to add client: %m\n");
4937 ret = EXIT_FAILURE;
4938 goto out;
4939 }
4940 primary_client_destroyed.notify =
4941 handle_primary_client_destroyed;
4942 wl_client_add_destroy_listener(primary_client,
4943 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004944 } else if (weston_create_listening_socket(display, socket_name)) {
4945 ret = EXIT_FAILURE;
4946 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004947 }
4948
Ryo Munakata03faed22014-09-06 07:32:51 +09004949 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004950 weston_config_section_get_string(section, "shell", &shell,
4951 "desktop-shell.so");
4952
Ryo Munakata03faed22014-09-06 07:32:51 +09004953 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004954 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004955
4956 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004957 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004958 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004959
4960 if (load_modules(ec, option_modules, &argc, argv) < 0)
4961 goto out;
4962
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004963 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4964 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4965 if (numlock_on) {
4966 wl_list_for_each(seat, &ec->seat_list, link) {
4967 if (seat->keyboard)
4968 weston_keyboard_set_locks(seat->keyboard,
4969 WESTON_NUM_LOCK,
4970 WESTON_NUM_LOCK);
4971 }
4972 }
4973
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004974 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004975
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004976 wl_display_run(display);
4977
Frederic Plourdec336f062014-10-29 14:44:33 -04004978 /* Allow for setting return exit code after
4979 * wl_display_run returns normally. This is
4980 * useful for devs/testers and automated tests
4981 * that want to indicate failure status to
4982 * testing infrastructure above
4983 */
4984 ret = ec->exit_code;
4985
Ryo Munakata03faed22014-09-06 07:32:51 +09004986out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004987 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004988 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004989
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004990 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004991
Daniel Stone855028f2012-05-01 20:37:10 +01004992 weston_compositor_xkb_destroy(ec);
4993
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004994 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304995
4996out_signals:
4997 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4998 if (signals[i])
4999 wl_event_source_remove(signals[i]);
5000
Tiago Vignatti9e2be082011-12-19 00:04:46 +02005001 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05005002
Martin Minarik19e6f262012-06-07 13:08:46 +02005003 weston_log_file_close();
5004
Ryo Munakata03faed22014-09-06 07:32:51 +09005005 free(backend);
5006 free(shell);
5007 free(socket_name);
5008 free(option_modules);
5009 free(log);
5010 free(modules);
5011
Pekka Paalanen3ab72692012-06-08 17:27:28 +03005012 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04005013}