blob: b84658a8a04aab4ef89bd73c877a4f13641d25a6 [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;
473};
474
475static void
476weston_presentation_feedback_discard(
477 struct weston_presentation_feedback *feedback)
478{
479 presentation_feedback_send_discarded(feedback->resource);
480 wl_resource_destroy(feedback->resource);
481}
482
483static void
484weston_presentation_feedback_discard_list(struct wl_list *list)
485{
486 struct weston_presentation_feedback *feedback, *tmp;
487
488 wl_list_for_each_safe(feedback, tmp, list, link)
489 weston_presentation_feedback_discard(feedback);
490}
491
492static void
493weston_presentation_feedback_present(
494 struct weston_presentation_feedback *feedback,
495 struct weston_output *output,
496 uint32_t refresh_nsec,
497 const struct timespec *ts,
498 uint64_t seq)
499{
500 struct wl_client *client = wl_resource_get_client(feedback->resource);
501 struct wl_resource *o;
502 uint64_t secs;
503 uint32_t flags = 0;
504
505 wl_resource_for_each(o, &output->resource_list) {
506 if (wl_resource_get_client(o) != client)
507 continue;
508
509 presentation_feedback_send_sync_output(feedback->resource, o);
510 }
511
512 secs = ts->tv_sec;
513 presentation_feedback_send_presented(feedback->resource,
514 secs >> 32, secs & 0xffffffff,
515 ts->tv_nsec,
516 refresh_nsec,
517 seq >> 32, seq & 0xffffffff,
518 flags);
519 wl_resource_destroy(feedback->resource);
520}
521
522static void
523weston_presentation_feedback_present_list(struct wl_list *list,
524 struct weston_output *output,
525 uint32_t refresh_nsec,
526 const struct timespec *ts,
527 uint64_t seq)
528{
529 struct weston_presentation_feedback *feedback, *tmp;
530
531 wl_list_for_each_safe(feedback, tmp, list, link)
532 weston_presentation_feedback_present(feedback, output,
533 refresh_nsec, ts, seq);
534}
535
Jason Ekstrand7b982072014-05-20 14:33:03 -0500536static void
537surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
538{
539 struct weston_surface_state *state =
540 container_of(listener, struct weston_surface_state,
541 buffer_destroy_listener);
542
543 state->buffer = NULL;
544}
545
546static void
547weston_surface_state_init(struct weston_surface_state *state)
548{
549 state->newly_attached = 0;
550 state->buffer = NULL;
551 state->buffer_destroy_listener.notify =
552 surface_state_handle_buffer_destroy;
553 state->sx = 0;
554 state->sy = 0;
555
556 pixman_region32_init(&state->damage);
557 pixman_region32_init(&state->opaque);
558 region_init_infinite(&state->input);
559
560 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400561 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500562
563 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
564 state->buffer_viewport.buffer.scale = 1;
565 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
566 state->buffer_viewport.surface.width = -1;
567 state->buffer_viewport.changed = 0;
568}
569
570static void
571weston_surface_state_fini(struct weston_surface_state *state)
572{
573 struct weston_frame_callback *cb, *next;
574
575 wl_list_for_each_safe(cb, next,
576 &state->frame_callback_list, link)
577 wl_resource_destroy(cb->resource);
578
Pekka Paalanen133e4392014-09-23 22:08:46 -0400579 weston_presentation_feedback_discard_list(&state->feedback_list);
580
Jason Ekstrand7b982072014-05-20 14:33:03 -0500581 pixman_region32_fini(&state->input);
582 pixman_region32_fini(&state->opaque);
583 pixman_region32_fini(&state->damage);
584
585 if (state->buffer)
586 wl_list_remove(&state->buffer_destroy_listener.link);
587 state->buffer = NULL;
588}
589
590static void
591weston_surface_state_set_buffer(struct weston_surface_state *state,
592 struct weston_buffer *buffer)
593{
594 if (state->buffer == buffer)
595 return;
596
597 if (state->buffer)
598 wl_list_remove(&state->buffer_destroy_listener.link);
599 state->buffer = buffer;
600 if (state->buffer)
601 wl_signal_add(&state->buffer->destroy_signal,
602 &state->buffer_destroy_listener);
603}
604
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500605WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500606weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500607{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500608 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400609
Bryce Harringtonde16d892014-11-20 22:21:57 -0800610 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400611 if (surface == NULL)
612 return NULL;
613
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500614 wl_signal_init(&surface->destroy_signal);
615
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500616 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200617 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400618
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200619 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
620 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200621 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
622 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500623
624 weston_surface_state_init(&surface->pending);
625
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400626 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500627 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300628 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400629
Jason Ekstranda7af7042013-10-12 22:38:11 -0500630 wl_list_init(&surface->views);
631
632 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400633 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500634
Pekka Paalanene67858b2013-04-25 13:57:42 +0300635 wl_list_init(&surface->subsurface_list);
636 wl_list_init(&surface->subsurface_list_pending);
637
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400638 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500639}
640
Alex Wu8811bf92012-02-28 18:07:54 +0800641WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500642weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200643 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500644{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100645 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500646}
647
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400648WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649weston_view_to_global_float(struct weston_view *view,
650 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200651{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500652 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200653 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
654
Jason Ekstranda7af7042013-10-12 22:38:11 -0500655 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200656
657 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200658 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700659 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200660 v.f[3]);
661 *x = 0;
662 *y = 0;
663 return;
664 }
665
666 *x = v.f[0] / v.f[3];
667 *y = v.f[1] / v.f[3];
668 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500669 *x = sx + view->geometry.x;
670 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200671 }
672}
673
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500674WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200675weston_transformed_coord(int width, int height,
676 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200677 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200678 float sx, float sy, float *bx, float *by)
679{
680 switch (transform) {
681 case WL_OUTPUT_TRANSFORM_NORMAL:
682 default:
683 *bx = sx;
684 *by = sy;
685 break;
686 case WL_OUTPUT_TRANSFORM_FLIPPED:
687 *bx = width - sx;
688 *by = sy;
689 break;
690 case WL_OUTPUT_TRANSFORM_90:
691 *bx = height - sy;
692 *by = sx;
693 break;
694 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
695 *bx = height - sy;
696 *by = width - sx;
697 break;
698 case WL_OUTPUT_TRANSFORM_180:
699 *bx = width - sx;
700 *by = height - sy;
701 break;
702 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
703 *bx = sx;
704 *by = height - sy;
705 break;
706 case WL_OUTPUT_TRANSFORM_270:
707 *bx = sy;
708 *by = width - sx;
709 break;
710 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
711 *bx = sy;
712 *by = sx;
713 break;
714 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200715
716 *bx *= scale;
717 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200718}
719
720WL_EXPORT pixman_box32_t
721weston_transformed_rect(int width, int height,
722 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200723 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200724 pixman_box32_t rect)
725{
726 float x1, x2, y1, y2;
727
728 pixman_box32_t ret;
729
Alexander Larsson4ea95522013-05-22 14:41:37 +0200730 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200731 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200732 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200733 rect.x2, rect.y2, &x2, &y2);
734
735 if (x1 <= x2) {
736 ret.x1 = x1;
737 ret.x2 = x2;
738 } else {
739 ret.x1 = x2;
740 ret.x2 = x1;
741 }
742
743 if (y1 <= y2) {
744 ret.y1 = y1;
745 ret.y2 = y2;
746 } else {
747 ret.y1 = y2;
748 ret.y2 = y1;
749 }
750
751 return ret;
752}
753
754WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500755weston_transformed_region(int width, int height,
756 enum wl_output_transform transform,
757 int32_t scale,
758 pixman_region32_t *src, pixman_region32_t *dest)
759{
760 pixman_box32_t *src_rects, *dest_rects;
761 int nrects, i;
762
763 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
764 if (src != dest)
765 pixman_region32_copy(dest, src);
766 return;
767 }
768
769 src_rects = pixman_region32_rectangles(src, &nrects);
770 dest_rects = malloc(nrects * sizeof(*dest_rects));
771 if (!dest_rects)
772 return;
773
774 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
775 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
776 } else {
777 for (i = 0; i < nrects; i++) {
778 switch (transform) {
779 default:
780 case WL_OUTPUT_TRANSFORM_NORMAL:
781 dest_rects[i].x1 = src_rects[i].x1;
782 dest_rects[i].y1 = src_rects[i].y1;
783 dest_rects[i].x2 = src_rects[i].x2;
784 dest_rects[i].y2 = src_rects[i].y2;
785 break;
786 case WL_OUTPUT_TRANSFORM_90:
787 dest_rects[i].x1 = height - src_rects[i].y2;
788 dest_rects[i].y1 = src_rects[i].x1;
789 dest_rects[i].x2 = height - src_rects[i].y1;
790 dest_rects[i].y2 = src_rects[i].x2;
791 break;
792 case WL_OUTPUT_TRANSFORM_180:
793 dest_rects[i].x1 = width - src_rects[i].x2;
794 dest_rects[i].y1 = height - src_rects[i].y2;
795 dest_rects[i].x2 = width - src_rects[i].x1;
796 dest_rects[i].y2 = height - src_rects[i].y1;
797 break;
798 case WL_OUTPUT_TRANSFORM_270:
799 dest_rects[i].x1 = src_rects[i].y1;
800 dest_rects[i].y1 = width - src_rects[i].x2;
801 dest_rects[i].x2 = src_rects[i].y2;
802 dest_rects[i].y2 = width - src_rects[i].x1;
803 break;
804 case WL_OUTPUT_TRANSFORM_FLIPPED:
805 dest_rects[i].x1 = width - src_rects[i].x2;
806 dest_rects[i].y1 = src_rects[i].y1;
807 dest_rects[i].x2 = width - src_rects[i].x1;
808 dest_rects[i].y2 = src_rects[i].y2;
809 break;
810 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
811 dest_rects[i].x1 = height - src_rects[i].y2;
812 dest_rects[i].y1 = width - src_rects[i].x2;
813 dest_rects[i].x2 = height - src_rects[i].y1;
814 dest_rects[i].y2 = width - src_rects[i].x1;
815 break;
816 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
817 dest_rects[i].x1 = src_rects[i].x1;
818 dest_rects[i].y1 = height - src_rects[i].y2;
819 dest_rects[i].x2 = src_rects[i].x2;
820 dest_rects[i].y2 = height - src_rects[i].y1;
821 break;
822 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
823 dest_rects[i].x1 = src_rects[i].y1;
824 dest_rects[i].y1 = src_rects[i].x1;
825 dest_rects[i].x2 = src_rects[i].y2;
826 dest_rects[i].y2 = src_rects[i].x2;
827 break;
828 }
829 }
830 }
831
832 if (scale != 1) {
833 for (i = 0; i < nrects; i++) {
834 dest_rects[i].x1 *= scale;
835 dest_rects[i].x2 *= scale;
836 dest_rects[i].y1 *= scale;
837 dest_rects[i].y2 *= scale;
838 }
839 }
840
841 pixman_region32_clear(dest);
842 pixman_region32_init_rects(dest, dest_rects, nrects);
843 free(dest_rects);
844}
845
Jonny Lamb74130762013-11-26 18:19:46 +0100846static void
847scaler_surface_to_buffer(struct weston_surface *surface,
848 float sx, float sy, float *bx, float *by)
849{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200850 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200851 double src_width, src_height;
852 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200853
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200854 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
855 if (vp->surface.width == -1) {
856 *bx = sx;
857 *by = sy;
858 return;
859 }
Jonny Lamb74130762013-11-26 18:19:46 +0100860
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 src_x = 0.0;
862 src_y = 0.0;
863 src_width = surface->width_from_buffer;
864 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100865 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200866 src_x = wl_fixed_to_double(vp->buffer.src_x);
867 src_y = wl_fixed_to_double(vp->buffer.src_y);
868 src_width = wl_fixed_to_double(vp->buffer.src_width);
869 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100870 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200871
872 *bx = sx * src_width / surface->width + src_x;
873 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100874}
875
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500876WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200877weston_surface_to_buffer_float(struct weston_surface *surface,
878 float sx, float sy, float *bx, float *by)
879{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200880 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
881
Jonny Lamb74130762013-11-26 18:19:46 +0100882 /* first transform coordinates if the scaler is set */
883 scaler_surface_to_buffer(surface, sx, sy, bx, by);
884
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500885 weston_transformed_coord(surface->width_from_buffer,
886 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200887 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100888 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200889}
890
Alexander Larsson4ea95522013-05-22 14:41:37 +0200891WL_EXPORT void
892weston_surface_to_buffer(struct weston_surface *surface,
893 int sx, int sy, int *bx, int *by)
894{
895 float bxf, byf;
896
Jonny Lamb74130762013-11-26 18:19:46 +0100897 weston_surface_to_buffer_float(surface,
898 sx, sy, &bxf, &byf);
899
Alexander Larsson4ea95522013-05-22 14:41:37 +0200900 *bx = floorf(bxf);
901 *by = floorf(byf);
902}
903
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200904WL_EXPORT pixman_box32_t
905weston_surface_to_buffer_rect(struct weston_surface *surface,
906 pixman_box32_t rect)
907{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200908 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100909 float xf, yf;
910
911 /* first transform box coordinates if the scaler is set */
912 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
913 rect.x1 = floorf(xf);
914 rect.y1 = floorf(yf);
915
916 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
917 rect.x2 = floorf(xf);
918 rect.y2 = floorf(yf);
919
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500920 return weston_transformed_rect(surface->width_from_buffer,
921 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200922 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200923 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200924}
925
926WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400928 struct weston_plane *plane)
929{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400931 return;
932
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 weston_view_damage_below(view);
934 view->plane = plane;
935 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936}
937
938WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200940{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500941 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200942
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500943 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300944 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800946 if (view->plane)
947 pixman_region32_union(&view->plane->damage,
948 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500949 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800950 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200951}
952
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400953static void
954weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
955{
956 uint32_t different = es->output_mask ^ mask;
957 uint32_t entered = mask & different;
958 uint32_t left = es->output_mask & different;
959 struct weston_output *output;
960 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500961 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400962
963 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500964 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400965 return;
966 if (different == 0)
967 return;
968
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500969 client = wl_resource_get_client(es->resource);
970
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400971 wl_list_for_each(output, &es->compositor->output_list, link) {
972 if (1 << output->id & different)
973 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500974 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400975 client);
976 if (resource == NULL)
977 continue;
978 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500979 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400980 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500981 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400982 }
983}
984
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200985
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400986static void
987weston_surface_assign_output(struct weston_surface *es)
988{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 struct weston_output *new_output;
990 struct weston_view *view;
991 pixman_region32_t region;
992 uint32_t max, area, mask;
993 pixman_box32_t *e;
994
995 new_output = NULL;
996 max = 0;
997 mask = 0;
998 pixman_region32_init(&region);
999 wl_list_for_each(view, &es->views, surface_link) {
1000 if (!view->output)
1001 continue;
1002
1003 pixman_region32_intersect(&region, &view->transform.boundingbox,
1004 &view->output->region);
1005
1006 e = pixman_region32_extents(&region);
1007 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1008
1009 mask |= view->output_mask;
1010
1011 if (area >= max) {
1012 new_output = view->output;
1013 max = area;
1014 }
1015 }
1016 pixman_region32_fini(&region);
1017
1018 es->output = new_output;
1019 weston_surface_update_output_mask(es, mask);
1020}
1021
1022static void
1023weston_view_assign_output(struct weston_view *ev)
1024{
1025 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001026 struct weston_output *output, *new_output;
1027 pixman_region32_t region;
1028 uint32_t max, area, mask;
1029 pixman_box32_t *e;
1030
1031 new_output = NULL;
1032 max = 0;
1033 mask = 0;
1034 pixman_region32_init(&region);
1035 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001037 &output->region);
1038
1039 e = pixman_region32_extents(&region);
1040 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1041
1042 if (area > 0)
1043 mask |= 1 << output->id;
1044
1045 if (area >= max) {
1046 new_output = output;
1047 max = area;
1048 }
1049 }
1050 pixman_region32_fini(&region);
1051
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 ev->output = new_output;
1053 ev->output_mask = mask;
1054
1055 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001056}
1057
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001058static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1060 int32_t width, int32_t height,
1061 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001062{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001063 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1064 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001065 int32_t s[4][2] = {
1066 { sx, sy },
1067 { sx, sy + height },
1068 { sx + width, sy },
1069 { sx + width, sy + height }
1070 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001071 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001072 int i;
1073
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001074 if (width == 0 || height == 0) {
1075 /* avoid rounding empty bbox to 1x1 */
1076 pixman_region32_init(bbox);
1077 return;
1078 }
1079
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001080 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001081 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001083 if (x < min_x)
1084 min_x = x;
1085 if (x > max_x)
1086 max_x = x;
1087 if (y < min_y)
1088 min_y = y;
1089 if (y > max_y)
1090 max_y = y;
1091 }
1092
Pekka Paalanen219b9822012-02-08 15:38:37 +02001093 int_x = floorf(min_x);
1094 int_y = floorf(min_y);
1095 pixman_region32_init_rect(bbox, int_x, int_y,
1096 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001097}
1098
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001099static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001101{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001103
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001104 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 view->geometry.x = roundf(view->geometry.x);
1106 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001107
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001108 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1110 view->transform.position.matrix.d[12] = view->geometry.x;
1111 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001112
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 view->transform.inverse = view->transform.position.matrix;
1116 view->transform.inverse.d[12] = -view->geometry.x;
1117 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001118
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 pixman_region32_init_rect(&view->transform.boundingbox,
1120 view->geometry.x,
1121 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001122 view->surface->width,
1123 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001124
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 if (view->alpha == 1.0) {
1126 pixman_region32_copy(&view->transform.opaque,
1127 &view->surface->opaque);
1128 pixman_region32_translate(&view->transform.opaque,
1129 view->geometry.x,
1130 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001131 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001132}
1133
1134static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001136{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 struct weston_view *parent = view->geometry.parent;
1138 struct weston_matrix *matrix = &view->transform.matrix;
1139 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001140 struct weston_transform *tform;
1141
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001143
1144 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1146 view->transform.position.matrix.d[12] = view->geometry.x;
1147 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001148
1149 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001151 weston_matrix_multiply(matrix, &tform->matrix);
1152
Pekka Paalanen483243f2013-03-08 14:56:50 +02001153 if (parent)
1154 weston_matrix_multiply(matrix, &parent->transform.matrix);
1155
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156 if (weston_matrix_invert(inverse, matrix) < 0) {
1157 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 weston_log("error: weston_view %p"
1159 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001160 return -1;
1161 }
1162
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001163 view_compute_bbox(view, 0, 0,
1164 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001166
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001167 return 0;
1168}
1169
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001170static struct weston_layer *
1171get_view_layer(struct weston_view *view)
1172{
1173 if (view->parent_view)
1174 return get_view_layer(view->parent_view);
1175 return view->layer_link.layer;
1176}
1177
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001178WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001182 struct weston_layer *layer;
1183 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001184
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001186 return;
1187
Pekka Paalanen483243f2013-03-08 14:56:50 +02001188 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001192
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001194
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 pixman_region32_fini(&view->transform.boundingbox);
1196 pixman_region32_fini(&view->transform.opaque);
1197 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001198
Pekka Paalanencd403622012-01-25 13:37:39 +02001199 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 if (view->geometry.transformation_list.next ==
1201 &view->transform.position.link &&
1202 view->geometry.transformation_list.prev ==
1203 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001204 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001206 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 if (weston_view_update_transform_enable(view) < 0)
1208 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001209 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001210
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001211 layer = get_view_layer(view);
1212 if (layer) {
1213 pixman_region32_init_with_extents(&mask, &layer->mask);
1214 pixman_region32_intersect(&view->transform.masked_boundingbox,
1215 &view->transform.boundingbox, &mask);
1216 pixman_region32_intersect(&view->transform.masked_opaque,
1217 &view->transform.opaque, &mask);
1218 pixman_region32_fini(&mask);
1219 }
1220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001222
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001224
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 wl_signal_emit(&view->surface->compositor->transform_signal,
1226 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001227}
1228
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001229WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001231{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001233
1234 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 * The invariant: if view->geometry.dirty, then all views
1236 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001237 * Corollary: if not parent->geometry.dirty, then all ancestors
1238 * are not dirty.
1239 */
1240
Jason Ekstranda7af7042013-10-12 22:38:11 -05001241 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001242 return;
1243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001247 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001249}
1250
1251WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252weston_view_to_global_fixed(struct weston_view *view,
1253 wl_fixed_t vx, wl_fixed_t vy,
1254 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001255{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001256 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 weston_view_to_global_float(view,
1259 wl_fixed_to_double(vx),
1260 wl_fixed_to_double(vy),
1261 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001262 *x = wl_fixed_from_double(xf);
1263 *y = wl_fixed_from_double(yf);
1264}
1265
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001266WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267weston_view_from_global_float(struct weston_view *view,
1268 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001269{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001271 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001274
1275 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001276 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001278 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 *vx = 0;
1280 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001281 return;
1282 }
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 *vx = v.f[0] / v.f[3];
1285 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001286 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 *vx = x - view->geometry.x;
1288 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001289 }
1290}
1291
1292WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293weston_view_from_global_fixed(struct weston_view *view,
1294 wl_fixed_t x, wl_fixed_t y,
1295 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001296{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001298
Jason Ekstranda7af7042013-10-12 22:38:11 -05001299 weston_view_from_global_float(view,
1300 wl_fixed_to_double(x),
1301 wl_fixed_to_double(y),
1302 &vxf, &vyf);
1303 *vx = wl_fixed_from_double(vxf);
1304 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001305}
1306
1307WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308weston_view_from_global(struct weston_view *view,
1309 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001310{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001312
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1314 *vx = floorf(vxf);
1315 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001316}
1317
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001318WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001319weston_surface_schedule_repaint(struct weston_surface *surface)
1320{
1321 struct weston_output *output;
1322
1323 wl_list_for_each(output, &surface->compositor->output_list, link)
1324 if (surface->output_mask & (1 << output->id))
1325 weston_output_schedule_repaint(output);
1326}
1327
1328WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329weston_view_schedule_repaint(struct weston_view *view)
1330{
1331 struct weston_output *output;
1332
1333 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1334 if (view->output_mask & (1 << output->id))
1335 weston_output_schedule_repaint(output);
1336}
1337
1338WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001339weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001340{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001341 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 0, 0, surface->width,
1343 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001344
Kristian Høgsberg98238702012-08-03 16:29:12 -04001345 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001346}
1347
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001348WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001350{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001351 if (view->geometry.x == x && view->geometry.y == y)
1352 return;
1353
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 view->geometry.x = x;
1355 view->geometry.y = y;
1356 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001357}
1358
Pekka Paalanen483243f2013-03-08 14:56:50 +02001359static void
1360transform_parent_handle_parent_destroy(struct wl_listener *listener,
1361 void *data)
1362{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001363 struct weston_view *view =
1364 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001365 geometry.parent_destroy_listener);
1366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001368}
1369
1370WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371weston_view_set_transform_parent(struct weston_view *view,
1372 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001373{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 if (view->geometry.parent) {
1375 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1376 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001377 }
1378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382 transform_parent_handle_parent_destroy;
1383 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001384 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001386 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001388 }
1389
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 weston_view_geometry_dirty(view);
1391}
1392
Derek Foreman280e7dd2014-10-03 13:13:42 -05001393WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394weston_view_is_mapped(struct weston_view *view)
1395{
1396 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001397 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001399 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001400}
1401
Derek Foreman280e7dd2014-10-03 13:13:42 -05001402WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001403weston_surface_is_mapped(struct weston_surface *surface)
1404{
1405 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001406 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001407 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001408 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001409}
1410
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001411static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001412surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001413{
1414 struct weston_view *view;
1415
1416 if (surface->width == width && surface->height == height)
1417 return;
1418
1419 surface->width = width;
1420 surface->height = height;
1421
1422 wl_list_for_each(view, &surface->views, surface_link)
1423 weston_view_geometry_dirty(view);
1424}
1425
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001426WL_EXPORT void
1427weston_surface_set_size(struct weston_surface *surface,
1428 int32_t width, int32_t height)
1429{
1430 assert(!surface->resource);
1431 surface_set_size(surface, width, height);
1432}
1433
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001434static int
1435fixed_round_up_to_int(wl_fixed_t f)
1436{
1437 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1438}
1439
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001440static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001441weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001442{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001443 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001444 int32_t width, height;
1445
1446 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001447 surface->width_from_buffer = 0;
1448 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001449 return;
1450 }
1451
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001452 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001453 case WL_OUTPUT_TRANSFORM_90:
1454 case WL_OUTPUT_TRANSFORM_270:
1455 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1456 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001457 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1458 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001459 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001460 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001461 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1462 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001463 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001464 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001465
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001466 surface->width_from_buffer = width;
1467 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001468}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001469
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001470static void
1471weston_surface_update_size(struct weston_surface *surface)
1472{
1473 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1474 int32_t width, height;
1475
1476 width = surface->width_from_buffer;
1477 height = surface->height_from_buffer;
1478
1479 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001480 surface_set_size(surface,
1481 vp->surface.width, vp->surface.height);
1482 return;
1483 }
1484
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001485 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001486 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1487 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1488
1489 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001490 return;
1491 }
1492
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001493 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001494}
1495
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001496WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001497weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001498{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001499 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001500
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001501 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001502
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001503 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001504}
1505
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506WL_EXPORT struct weston_view *
1507weston_compositor_pick_view(struct weston_compositor *compositor,
1508 wl_fixed_t x, wl_fixed_t y,
1509 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001510{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001512 int ix = wl_fixed_to_int(x);
1513 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001514
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 wl_list_for_each(view, &compositor->view_list, link) {
1516 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001517 if (pixman_region32_contains_point(
1518 &view->transform.masked_boundingbox,
1519 ix, iy, NULL) &&
1520 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 wl_fixed_to_int(*vx),
1522 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001523 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001525 }
1526
1527 return NULL;
1528}
1529
1530static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001531weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001532{
Daniel Stone37816df2012-05-16 18:45:18 +01001533 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001534
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001535 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001536 return;
1537
Daniel Stone37816df2012-05-16 18:45:18 +01001538 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001539 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001540}
1541
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001542WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001544{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001545 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001546
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 if (!weston_view_is_mapped(view))
1548 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001549
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 weston_view_damage_below(view);
1551 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001552 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001553 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 wl_list_remove(&view->link);
1555 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 view->output_mask = 0;
1557 weston_surface_assign_output(view->surface);
1558
1559 if (weston_surface_is_mapped(view->surface))
1560 return;
1561
1562 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1563 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001564 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001566 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001567 NULL,
1568 wl_fixed_from_int(0),
1569 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001571 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001572 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575WL_EXPORT void
1576weston_surface_unmap(struct weston_surface *surface)
1577{
1578 struct weston_view *view;
1579
1580 wl_list_for_each(view, &surface->views, surface_link)
1581 weston_view_unmap(view);
1582 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001583}
1584
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001585static void
1586weston_surface_reset_pending_buffer(struct weston_surface *surface)
1587{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001588 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001589 surface->pending.sx = 0;
1590 surface->pending.sy = 0;
1591 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001592 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001593}
1594
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595WL_EXPORT void
1596weston_view_destroy(struct weston_view *view)
1597{
1598 wl_signal_emit(&view->destroy_signal, view);
1599
1600 assert(wl_list_empty(&view->geometry.child_list));
1601
1602 if (weston_view_is_mapped(view)) {
1603 weston_view_unmap(view);
1604 weston_compositor_build_view_list(view->surface->compositor);
1605 }
1606
1607 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001608 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609
1610 pixman_region32_fini(&view->clip);
1611 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001612 pixman_region32_fini(&view->transform.masked_boundingbox);
1613 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614
1615 weston_view_set_transform_parent(view, NULL);
1616
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 wl_list_remove(&view->surface_link);
1618
1619 free(view);
1620}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001621
1622WL_EXPORT void
1623weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001624{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001625 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001626 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001627
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001628 if (--surface->ref_count > 0)
1629 return;
1630
1631 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1632
Pekka Paalanene67858b2013-04-25 13:57:42 +03001633 assert(wl_list_empty(&surface->subsurface_list_pending));
1634 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001635
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1637 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001638
Jason Ekstrand7b982072014-05-20 14:33:03 -05001639 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001640
Pekka Paalanende685b82012-12-04 15:58:12 +02001641 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001642
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001643 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001644 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001645 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001646
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001647 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001648 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001649
Pekka Paalanen133e4392014-09-23 22:08:46 -04001650 weston_presentation_feedback_discard_list(&surface->feedback_list);
1651
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001652 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001653}
1654
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001655static void
1656destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001657{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001658 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001659
Giulio Camuffo0d379742013-11-15 22:06:15 +01001660 /* Set the resource to NULL, since we don't want to leave a
1661 * dangling pointer if the surface was refcounted and survives
1662 * the weston_surface_destroy() call. */
1663 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001664 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001665}
1666
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001667static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001668weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1669{
1670 struct weston_buffer *buffer =
1671 container_of(listener, struct weston_buffer, destroy_listener);
1672
1673 wl_signal_emit(&buffer->destroy_signal, buffer);
1674 free(buffer);
1675}
1676
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001677WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001678weston_buffer_from_resource(struct wl_resource *resource)
1679{
1680 struct weston_buffer *buffer;
1681 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001682
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001683 listener = wl_resource_get_destroy_listener(resource,
1684 weston_buffer_destroy_handler);
1685
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001686 if (listener)
1687 return container_of(listener, struct weston_buffer,
1688 destroy_listener);
1689
1690 buffer = zalloc(sizeof *buffer);
1691 if (buffer == NULL)
1692 return NULL;
1693
1694 buffer->resource = resource;
1695 wl_signal_init(&buffer->destroy_signal);
1696 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001697 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001698 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001699
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001700 return buffer;
1701}
1702
1703static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001704weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1705 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001706{
Pekka Paalanende685b82012-12-04 15:58:12 +02001707 struct weston_buffer_reference *ref =
1708 container_of(listener, struct weston_buffer_reference,
1709 destroy_listener);
1710
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001711 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001712 ref->buffer = NULL;
1713}
1714
1715WL_EXPORT void
1716weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001717 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001718{
1719 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001720 ref->buffer->busy_count--;
1721 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001722 assert(wl_resource_get_client(ref->buffer->resource));
1723 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001724 WL_BUFFER_RELEASE);
1725 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001726 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001727 }
1728
Pekka Paalanende685b82012-12-04 15:58:12 +02001729 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001730 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001731 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001732 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001733 }
1734
Pekka Paalanende685b82012-12-04 15:58:12 +02001735 ref->buffer = buffer;
1736 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1737}
1738
1739static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001740weston_surface_attach(struct weston_surface *surface,
1741 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001742{
1743 weston_buffer_reference(&surface->buffer_ref, buffer);
1744
Pekka Paalanena6421c42012-12-04 15:58:10 +02001745 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001746 if (weston_surface_is_mapped(surface))
1747 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001748 }
1749
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001750 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001751
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001752 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001753 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001754}
1755
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001756WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001757weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001758{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001759 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001760
1761 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001762 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001763}
1764
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001765WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001766weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001767{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001768 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001769
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001770 pixman_region32_union(&compositor->primary_plane.damage,
1771 &compositor->primary_plane.damage,
1772 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001773 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001774}
1775
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001776static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001778{
Pekka Paalanende685b82012-12-04 15:58:12 +02001779 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001780 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001781 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001782
Pekka Paalanenb5026542014-11-12 15:09:24 +02001783 if (weston_timeline_enabled_ &&
1784 pixman_region32_not_empty(&surface->damage))
1785 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1786 TLP_OUTPUT(surface->output), TLP_END);
1787
Jason Ekstrandef540082014-06-26 10:37:36 -07001788 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789}
1790
1791static void
1792view_accumulate_damage(struct weston_view *view,
1793 pixman_region32_t *opaque)
1794{
1795 pixman_region32_t damage;
1796
1797 pixman_region32_init(&damage);
1798 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001799 pixman_box32_t *extents;
1800
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 extents = pixman_region32_extents(&view->surface->damage);
1802 view_compute_bbox(view, extents->x1, extents->y1,
1803 extents->x2 - extents->x1,
1804 extents->y2 - extents->y1,
1805 &damage);
1806 pixman_region32_translate(&damage,
1807 -view->plane->x,
1808 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001809 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 pixman_region32_copy(&damage, &view->surface->damage);
1811 pixman_region32_translate(&damage,
1812 view->geometry.x - view->plane->x,
1813 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001814 }
1815
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 pixman_region32_subtract(&damage, &damage, opaque);
1817 pixman_region32_union(&view->plane->damage,
1818 &view->plane->damage, &damage);
1819 pixman_region32_fini(&damage);
1820 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001821 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001822}
1823
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001824static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001825compositor_accumulate_damage(struct weston_compositor *ec)
1826{
1827 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001829 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001830
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001831 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001832
1833 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001834 pixman_region32_copy(&plane->clip, &clip);
1835
1836 pixman_region32_init(&opaque);
1837
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 wl_list_for_each(ev, &ec->view_list, link) {
1839 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001840 continue;
1841
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001843 }
1844
1845 pixman_region32_union(&clip, &clip, &opaque);
1846 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001847 }
1848
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001849 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001850
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851 wl_list_for_each(ev, &ec->view_list, link)
1852 ev->surface->touched = 0;
1853
1854 wl_list_for_each(ev, &ec->view_list, link) {
1855 if (ev->surface->touched)
1856 continue;
1857 ev->surface->touched = 1;
1858
1859 surface_flush_damage(ev->surface);
1860
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001861 /* Both the renderer and the backend have seen the buffer
1862 * by now. If renderer needs the buffer, it has its own
1863 * reference set. If the backend wants to keep the buffer
1864 * around for migrating the surface into a non-primary plane
1865 * later, keep_buffer is true. Otherwise, drop the core
1866 * reference now, and allow early buffer release. This enables
1867 * clients to use single-buffering.
1868 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 if (!ev->surface->keep_buffer)
1870 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001871 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001872}
1873
1874static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001876{
1877 struct weston_subsurface *sub;
1878
Pekka Paalanene67858b2013-04-25 13:57:42 +03001879 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881 continue;
1882
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1884 wl_list_init(&sub->surface->views);
1885
1886 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001887 }
1888}
1889
1890static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001892{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893 struct weston_subsurface *sub;
1894 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001895
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1897 if (sub->surface == surface)
1898 continue;
1899
George Kiagiadakised04d382014-06-13 18:10:26 +02001900 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1901 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001902 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001903 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904
1905 surface_free_unused_subsurface_views(sub->surface);
1906 }
1907}
1908
1909static void
1910view_list_add_subsurface_view(struct weston_compositor *compositor,
1911 struct weston_subsurface *sub,
1912 struct weston_view *parent)
1913{
1914 struct weston_subsurface *child;
1915 struct weston_view *view = NULL, *iv;
1916
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001917 if (!weston_surface_is_mapped(sub->surface))
1918 return;
1919
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1921 if (iv->geometry.parent == parent) {
1922 view = iv;
1923 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001924 }
1925 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001926
1927 if (view) {
1928 /* Put it back in the surface's list of views */
1929 wl_list_remove(&view->surface_link);
1930 wl_list_insert(&sub->surface->views, &view->surface_link);
1931 } else {
1932 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001933 weston_view_set_position(view,
1934 sub->position.x,
1935 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001936 weston_view_set_transform_parent(view, parent);
1937 }
1938
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001939 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001940 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001941
Pekka Paalanenb188e912013-11-19 14:03:35 +02001942 if (wl_list_empty(&sub->surface->subsurface_list)) {
1943 wl_list_insert(compositor->view_list.prev, &view->link);
1944 return;
1945 }
1946
1947 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1948 if (child->surface == sub->surface)
1949 wl_list_insert(compositor->view_list.prev, &view->link);
1950 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001951 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001952 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953}
1954
1955static void
1956view_list_add(struct weston_compositor *compositor,
1957 struct weston_view *view)
1958{
1959 struct weston_subsurface *sub;
1960
1961 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962
Pekka Paalanenb188e912013-11-19 14:03:35 +02001963 if (wl_list_empty(&view->surface->subsurface_list)) {
1964 wl_list_insert(compositor->view_list.prev, &view->link);
1965 return;
1966 }
1967
1968 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1969 if (sub->surface == view->surface)
1970 wl_list_insert(compositor->view_list.prev, &view->link);
1971 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001973 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974}
1975
1976static void
1977weston_compositor_build_view_list(struct weston_compositor *compositor)
1978{
1979 struct weston_view *view;
1980 struct weston_layer *layer;
1981
1982 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001983 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 surface_stash_subsurface_views(view->surface);
1985
1986 wl_list_init(&compositor->view_list);
1987 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001988 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 view_list_add(compositor, view);
1990 }
1991 }
1992
1993 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001994 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001996}
1997
David Herrmann1edf44c2013-10-22 17:11:26 +02001998static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001999weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002000{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002003 struct weston_animation *animation, *next;
2004 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002005 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002006 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002007 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002008
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002009 if (output->destroying)
2010 return 0;
2011
Pekka Paalanenb5026542014-11-12 15:09:24 +02002012 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2013
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002014 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002016
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002017 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002018 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002019 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 wl_list_for_each(ev, &ec->view_list, link)
2021 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002022
Pekka Paalanene67858b2013-04-25 13:57:42 +03002023 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 wl_list_for_each(ev, &ec->view_list, link) {
2025 /* Note: This operation is safe to do multiple times on the
2026 * same surface.
2027 */
2028 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002029 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 &ev->surface->frame_callback_list);
2031 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002032
2033 wl_list_insert_list(&output->feedback_list,
2034 &ev->surface->feedback_list);
2035 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002036 }
2037 }
2038
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002039 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002040
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002041 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002042 pixman_region32_intersect(&output_damage,
2043 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002044 pixman_region32_subtract(&output_damage,
2045 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002046
Scott Moreauccbf29d2012-02-22 14:21:41 -07002047 if (output->dirty)
2048 weston_output_update_matrix(output);
2049
David Herrmann1edf44c2013-10-22 17:11:26 +02002050 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002051
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002052 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002053
Kristian Høgsbergef044142011-06-21 15:02:12 -04002054 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002055
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002056 weston_compositor_repick(ec);
2057 wl_event_loop_dispatch(ec->input_loop, 0);
2058
Jonas Ådahldb773762012-06-13 00:01:21 +02002059 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002060 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002061 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002062 }
2063
Scott Moreaud64cf212012-06-08 19:40:54 -06002064 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002065 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002066 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002067 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002068
Pekka Paalanenb5026542014-11-12 15:09:24 +02002069 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2070
David Herrmann1edf44c2013-10-22 17:11:26 +02002071 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002072}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002073
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002074static int
2075weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002076{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002077 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002078
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002079 wl_event_loop_dispatch(compositor->input_loop, 0);
2080
2081 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002082}
2083
2084WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002085weston_output_finish_frame(struct weston_output *output,
2086 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002087{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002088 struct weston_compositor *compositor = output->compositor;
2089 struct wl_event_loop *loop =
2090 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002091 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002092 uint32_t refresh_nsec;
2093
Pekka Paalanenb5026542014-11-12 15:09:24 +02002094 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2095 TLP_VBLANK(stamp), TLP_END);
2096
Pekka Paalanen133e4392014-09-23 22:08:46 -04002097 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2098 weston_presentation_feedback_present_list(&output->feedback_list,
2099 output, refresh_nsec, stamp,
Pekka Paalanen641307c2014-09-23 22:08:47 -04002100 output->msc);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002101
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002102 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002103
2104 if (output->repaint_needed &&
2105 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2106 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002107 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002108 if (!r)
2109 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002110 }
2111
2112 output->repaint_scheduled = 0;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002113 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2114
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002115 if (compositor->input_loop_source)
2116 return;
2117
2118 fd = wl_event_loop_get_fd(compositor->input_loop);
2119 compositor->input_loop_source =
2120 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2121 weston_compositor_read_input, compositor);
2122}
2123
2124static void
2125idle_repaint(void *data)
2126{
2127 struct weston_output *output = data;
2128
Jonas Ådahle5a12252013-04-05 23:07:11 +02002129 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002130}
2131
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002132WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002133weston_layer_entry_insert(struct weston_layer_entry *list,
2134 struct weston_layer_entry *entry)
2135{
2136 wl_list_insert(&list->link, &entry->link);
2137 entry->layer = list->layer;
2138}
2139
2140WL_EXPORT void
2141weston_layer_entry_remove(struct weston_layer_entry *entry)
2142{
2143 wl_list_remove(&entry->link);
2144 wl_list_init(&entry->link);
2145 entry->layer = NULL;
2146}
2147
2148WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002149weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2150{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002151 wl_list_init(&layer->view_list.link);
2152 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002153 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002154 if (below != NULL)
2155 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002156}
2157
2158WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002159weston_layer_set_mask(struct weston_layer *layer,
2160 int x, int y, int width, int height)
2161{
2162 struct weston_view *view;
2163
2164 layer->mask.x1 = x;
2165 layer->mask.x2 = x + width;
2166 layer->mask.y1 = y;
2167 layer->mask.y2 = y + height;
2168
2169 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2170 weston_view_geometry_dirty(view);
2171 }
2172}
2173
2174WL_EXPORT void
2175weston_layer_set_mask_infinite(struct weston_layer *layer)
2176{
2177 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2178 UINT32_MAX, UINT32_MAX);
2179}
2180
2181WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002182weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002183{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002184 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002185 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002186
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002187 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2188 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002189 return;
2190
Pekka Paalanenb5026542014-11-12 15:09:24 +02002191 if (!output->repaint_needed)
2192 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2193
Kristian Høgsbergef044142011-06-21 15:02:12 -04002194 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002195 output->repaint_needed = 1;
2196 if (output->repaint_scheduled)
2197 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002198
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002199 wl_event_loop_add_idle(loop, idle_repaint, output);
2200 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002201 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2202
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002203
2204 if (compositor->input_loop_source) {
2205 wl_event_source_remove(compositor->input_loop_source);
2206 compositor->input_loop_source = NULL;
2207 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002208}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002209
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002210WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002211weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2212{
2213 struct weston_output *output;
2214
2215 wl_list_for_each(output, &compositor->output_list, link)
2216 weston_output_schedule_repaint(output);
2217}
2218
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002219static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002220surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002221{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002222 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002223}
2224
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002225static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002226surface_attach(struct wl_client *client,
2227 struct wl_resource *resource,
2228 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2229{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002230 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002231 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002232
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002233 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002234 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002235 if (buffer == NULL) {
2236 wl_client_post_no_memory(client);
2237 return;
2238 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002239 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002240
Pekka Paalanende685b82012-12-04 15:58:12 +02002241 /* Attach, attach, without commit in between does not send
2242 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002243 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002244
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002245 surface->pending.sx = sx;
2246 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002247 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002248}
2249
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002250static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002251surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002252 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002253 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002254{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002255 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002256
Pekka Paalanen8e159182012-10-10 12:49:25 +03002257 pixman_region32_union_rect(&surface->pending.damage,
2258 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002259 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002260}
2261
Kristian Høgsberg33418202011-08-16 23:01:28 -04002262static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002263destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002264{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002265 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002266
2267 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002268 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002269}
2270
2271static void
2272surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002273 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002274{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002275 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002276 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002277
2278 cb = malloc(sizeof *cb);
2279 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002280 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002281 return;
2282 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002283
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002284 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2285 callback);
2286 if (cb->resource == NULL) {
2287 free(cb);
2288 wl_resource_post_no_memory(resource);
2289 return;
2290 }
2291
Jason Ekstranda85118c2013-06-27 20:17:02 -05002292 wl_resource_set_implementation(cb->resource, NULL, cb,
2293 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002294
Pekka Paalanenbc106382012-10-10 12:49:31 +03002295 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002296}
2297
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002298static void
2299surface_set_opaque_region(struct wl_client *client,
2300 struct wl_resource *resource,
2301 struct wl_resource *region_resource)
2302{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002303 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002304 struct weston_region *region;
2305
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002306 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002307 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002308 pixman_region32_copy(&surface->pending.opaque,
2309 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002310 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002311 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002312 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002313}
2314
2315static void
2316surface_set_input_region(struct wl_client *client,
2317 struct wl_resource *resource,
2318 struct wl_resource *region_resource)
2319{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002320 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002321 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002322
2323 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002324 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002325 pixman_region32_copy(&surface->pending.input,
2326 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002327 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002328 pixman_region32_fini(&surface->pending.input);
2329 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002330 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002331}
2332
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002333static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002334weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002335{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002336 struct weston_subsurface *sub;
2337
2338 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2339 parent_link_pending) {
2340 wl_list_remove(&sub->parent_link);
2341 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2342 }
2343}
2344
2345static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002346weston_surface_commit_state(struct weston_surface *surface,
2347 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002348{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002349 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002350 pixman_region32_t opaque;
2351
Alexander Larsson4ea95522013-05-22 14:41:37 +02002352 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002353 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002354 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002355 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002356
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002357 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002358 if (state->newly_attached)
2359 weston_surface_attach(surface, state->buffer);
2360 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002361
Jason Ekstrand7b982072014-05-20 14:33:03 -05002362 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002363 weston_surface_update_size(surface);
2364 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002365 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002366 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002367
Jason Ekstrand7b982072014-05-20 14:33:03 -05002368 state->sx = 0;
2369 state->sy = 0;
2370 state->newly_attached = 0;
2371 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002372
2373 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002374 if (weston_timeline_enabled_ &&
2375 pixman_region32_not_empty(&state->damage))
2376 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002377 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002378 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002379 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002380 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002381 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002382
2383 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002384 pixman_region32_init(&opaque);
2385 pixman_region32_intersect_rect(&opaque, &state->opaque,
2386 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002387
2388 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2389 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002390 wl_list_for_each(view, &surface->views, surface_link)
2391 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002392 }
2393
2394 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002395
2396 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002397 pixman_region32_intersect_rect(&surface->input, &state->input,
2398 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002399
Pekka Paalanenbc106382012-10-10 12:49:31 +03002400 /* wl_surface.frame */
2401 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002402 &state->frame_callback_list);
2403 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002404
2405 /* XXX:
2406 * What should happen with a feedback request, if there
2407 * is no wl_buffer attached for this commit?
2408 */
2409
2410 /* presentation.feedback */
2411 wl_list_insert_list(&surface->feedback_list,
2412 &state->feedback_list);
2413 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002414}
2415
2416static void
2417weston_surface_commit(struct weston_surface *surface)
2418{
2419 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002420
Pekka Paalanene67858b2013-04-25 13:57:42 +03002421 weston_surface_commit_subsurface_order(surface);
2422
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002423 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002424}
2425
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002426static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002427weston_subsurface_commit(struct weston_subsurface *sub);
2428
2429static void
2430weston_subsurface_parent_commit(struct weston_subsurface *sub,
2431 int parent_is_synchronized);
2432
2433static void
2434surface_commit(struct wl_client *client, struct wl_resource *resource)
2435{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002436 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002437 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2438
2439 if (sub) {
2440 weston_subsurface_commit(sub);
2441 return;
2442 }
2443
2444 weston_surface_commit(surface);
2445
2446 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2447 if (sub->surface != surface)
2448 weston_subsurface_parent_commit(sub, 0);
2449 }
2450}
2451
2452static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002453surface_set_buffer_transform(struct wl_client *client,
2454 struct wl_resource *resource, int transform)
2455{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002456 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002457
Jonny Lamba55f1392014-05-30 12:07:15 +02002458 /* if wl_output.transform grows more members this will need to be updated. */
2459 if (transform < 0 ||
2460 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2461 wl_resource_post_error(resource,
2462 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2463 "buffer transform must be a valid transform "
2464 "('%d' specified)", transform);
2465 return;
2466 }
2467
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002468 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002469 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002470}
2471
Alexander Larsson4ea95522013-05-22 14:41:37 +02002472static void
2473surface_set_buffer_scale(struct wl_client *client,
2474 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002475 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002476{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002477 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002478
Jonny Lamba55f1392014-05-30 12:07:15 +02002479 if (scale < 1) {
2480 wl_resource_post_error(resource,
2481 WL_SURFACE_ERROR_INVALID_SCALE,
2482 "buffer scale must be at least one "
2483 "('%d' specified)", scale);
2484 return;
2485 }
2486
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002487 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002488 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002489}
2490
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002491static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002492 surface_destroy,
2493 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002494 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002495 surface_frame,
2496 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002497 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002498 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002499 surface_set_buffer_transform,
2500 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002501};
2502
2503static void
2504compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002505 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002506{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002507 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002508 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002509
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002510 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002511 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002512 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002513 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002514 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002515
Jason Ekstranda85118c2013-06-27 20:17:02 -05002516 surface->resource =
2517 wl_resource_create(client, &wl_surface_interface,
2518 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002519 if (surface->resource == NULL) {
2520 weston_surface_destroy(surface);
2521 wl_resource_post_no_memory(resource);
2522 return;
2523 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002524 wl_resource_set_implementation(surface->resource, &surface_interface,
2525 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002526
2527 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002528}
2529
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002530static void
2531destroy_region(struct wl_resource *resource)
2532{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002533 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002534
2535 pixman_region32_fini(&region->region);
2536 free(region);
2537}
2538
2539static void
2540region_destroy(struct wl_client *client, struct wl_resource *resource)
2541{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002542 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002543}
2544
2545static void
2546region_add(struct wl_client *client, struct wl_resource *resource,
2547 int32_t x, int32_t y, int32_t width, int32_t height)
2548{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002549 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002550
2551 pixman_region32_union_rect(&region->region, &region->region,
2552 x, y, width, height);
2553}
2554
2555static void
2556region_subtract(struct wl_client *client, struct wl_resource *resource,
2557 int32_t x, int32_t y, int32_t width, int32_t height)
2558{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002559 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002560 pixman_region32_t rect;
2561
2562 pixman_region32_init_rect(&rect, x, y, width, height);
2563 pixman_region32_subtract(&region->region, &region->region, &rect);
2564 pixman_region32_fini(&rect);
2565}
2566
2567static const struct wl_region_interface region_interface = {
2568 region_destroy,
2569 region_add,
2570 region_subtract
2571};
2572
2573static void
2574compositor_create_region(struct wl_client *client,
2575 struct wl_resource *resource, uint32_t id)
2576{
2577 struct weston_region *region;
2578
2579 region = malloc(sizeof *region);
2580 if (region == NULL) {
2581 wl_resource_post_no_memory(resource);
2582 return;
2583 }
2584
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002585 pixman_region32_init(&region->region);
2586
Jason Ekstranda85118c2013-06-27 20:17:02 -05002587 region->resource =
2588 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002589 if (region->resource == NULL) {
2590 free(region);
2591 wl_resource_post_no_memory(resource);
2592 return;
2593 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002594 wl_resource_set_implementation(region->resource, &region_interface,
2595 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002596}
2597
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002598static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002599 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002600 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002601};
2602
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002603static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2605{
2606 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002607
Jason Ekstrand7b982072014-05-20 14:33:03 -05002608 weston_surface_commit_state(surface, &sub->cached);
2609 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610
2611 weston_surface_commit_subsurface_order(surface);
2612
2613 weston_surface_schedule_repaint(surface);
2614
Jason Ekstrand7b982072014-05-20 14:33:03 -05002615 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002616}
2617
2618static void
2619weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2620{
2621 struct weston_surface *surface = sub->surface;
2622
2623 /*
2624 * If this commit would cause the surface to move by the
2625 * attach(dx, dy) parameters, the old damage region must be
2626 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002627 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002628 */
2629 pixman_region32_translate(&sub->cached.damage,
2630 -surface->pending.sx, -surface->pending.sy);
2631 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2632 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002633 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634
2635 if (surface->pending.newly_attached) {
2636 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002637 weston_surface_state_set_buffer(&sub->cached,
2638 surface->pending.buffer);
2639 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002640 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002641 weston_presentation_feedback_discard_list(
2642 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 }
2644 sub->cached.sx += surface->pending.sx;
2645 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002646
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002647 sub->cached.buffer_viewport.changed |=
2648 surface->pending.buffer_viewport.changed;
2649 sub->cached.buffer_viewport.buffer =
2650 surface->pending.buffer_viewport.buffer;
2651 sub->cached.buffer_viewport.surface =
2652 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002653
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002654 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002655
2656 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2657
2658 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2659
2660 wl_list_insert_list(&sub->cached.frame_callback_list,
2661 &surface->pending.frame_callback_list);
2662 wl_list_init(&surface->pending.frame_callback_list);
2663
Pekka Paalanen133e4392014-09-23 22:08:46 -04002664 wl_list_insert_list(&sub->cached.feedback_list,
2665 &surface->pending.feedback_list);
2666 wl_list_init(&surface->pending.feedback_list);
2667
Jason Ekstrand7b982072014-05-20 14:33:03 -05002668 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002669}
2670
Derek Foreman280e7dd2014-10-03 13:13:42 -05002671static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002672weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2673{
2674 while (sub) {
2675 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002676 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002677
2678 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002679 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002680
2681 sub = weston_surface_to_subsurface(sub->parent);
2682 }
2683
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002684 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002685}
2686
2687static void
2688weston_subsurface_commit(struct weston_subsurface *sub)
2689{
2690 struct weston_surface *surface = sub->surface;
2691 struct weston_subsurface *tmp;
2692
2693 /* Recursive check for effectively synchronized. */
2694 if (weston_subsurface_is_synchronized(sub)) {
2695 weston_subsurface_commit_to_cache(sub);
2696 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002697 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002698 /* flush accumulated state from cache */
2699 weston_subsurface_commit_to_cache(sub);
2700 weston_subsurface_commit_from_cache(sub);
2701 } else {
2702 weston_surface_commit(surface);
2703 }
2704
2705 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2706 if (tmp->surface != surface)
2707 weston_subsurface_parent_commit(tmp, 0);
2708 }
2709 }
2710}
2711
2712static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002713weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002714{
2715 struct weston_surface *surface = sub->surface;
2716 struct weston_subsurface *tmp;
2717
Pekka Paalanene67858b2013-04-25 13:57:42 +03002718 /* From now on, commit_from_cache the whole sub-tree, regardless of
2719 * the synchronized mode of each child. This sub-surface or some
2720 * of its ancestors were synchronized, so we are synchronized
2721 * all the way down.
2722 */
2723
Jason Ekstrand7b982072014-05-20 14:33:03 -05002724 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725 weston_subsurface_commit_from_cache(sub);
2726
2727 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2728 if (tmp->surface != surface)
2729 weston_subsurface_parent_commit(tmp, 1);
2730 }
2731}
2732
2733static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002734weston_subsurface_parent_commit(struct weston_subsurface *sub,
2735 int parent_is_synchronized)
2736{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002738 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 wl_list_for_each(view, &sub->surface->views, surface_link)
2740 weston_view_set_position(view,
2741 sub->position.x,
2742 sub->position.y);
2743
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002744 sub->position.set = 0;
2745 }
2746
2747 if (parent_is_synchronized || sub->synchronized)
2748 weston_subsurface_synchronized_commit(sub);
2749}
2750
Pekka Paalanen8274d902014-08-06 19:36:51 +03002751static int
2752subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2753{
2754 return snprintf(buf, len, "sub-surface");
2755}
2756
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002757static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002758subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002759{
2760 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002761 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002762
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002764 weston_view_set_position(view,
2765 view->geometry.x + dx,
2766 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767
2768 /* No need to check parent mappedness, because if parent is not
2769 * mapped, parent is not in a visible layer, so this sub-surface
2770 * will not be drawn either.
2771 */
2772 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002773 struct weston_output *output;
2774
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002775 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002776 * because that would call it also for the parent surface,
2777 * which might not be mapped yet. That would lead to
2778 * inconsistent state, where the window could never be
2779 * mapped.
2780 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002781 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 * weston_surface_is_mapped() return true, so that when the
2783 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002784 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002785 */
2786 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002787 output = container_of(compositor->output_list.next,
2788 struct weston_output, link);
2789
2790 surface->output = output;
2791 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792 }
2793}
2794
2795static struct weston_subsurface *
2796weston_surface_to_subsurface(struct weston_surface *surface)
2797{
2798 if (surface->configure == subsurface_configure)
2799 return surface->configure_private;
2800
2801 return NULL;
2802}
2803
Pekka Paalanen01388e22013-04-25 13:57:44 +03002804WL_EXPORT struct weston_surface *
2805weston_surface_get_main_surface(struct weston_surface *surface)
2806{
2807 struct weston_subsurface *sub;
2808
2809 while (surface && (sub = weston_surface_to_subsurface(surface)))
2810 surface = sub->parent;
2811
2812 return surface;
2813}
2814
Pekka Paalanen50b67472014-10-01 15:02:41 +03002815WL_EXPORT int
2816weston_surface_set_role(struct weston_surface *surface,
2817 const char *role_name,
2818 struct wl_resource *error_resource,
2819 uint32_t error_code)
2820{
2821 assert(role_name);
2822
2823 if (surface->role_name == NULL ||
2824 surface->role_name == role_name ||
2825 strcmp(surface->role_name, role_name) == 0) {
2826 surface->role_name = role_name;
2827
2828 return 0;
2829 }
2830
2831 wl_resource_post_error(error_resource, error_code,
2832 "Cannot assign role %s to wl_surface@%d,"
2833 " already has role %s\n",
2834 role_name,
2835 wl_resource_get_id(surface->resource),
2836 surface->role_name);
2837 return -1;
2838}
2839
Pekka Paalanen8274d902014-08-06 19:36:51 +03002840WL_EXPORT void
2841weston_surface_set_label_func(struct weston_surface *surface,
2842 int (*desc)(struct weston_surface *,
2843 char *, size_t))
2844{
2845 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002846 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002847}
2848
Pekka Paalanene67858b2013-04-25 13:57:42 +03002849static void
2850subsurface_set_position(struct wl_client *client,
2851 struct wl_resource *resource, int32_t x, int32_t y)
2852{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002853 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002854
2855 if (!sub)
2856 return;
2857
2858 sub->position.x = x;
2859 sub->position.y = y;
2860 sub->position.set = 1;
2861}
2862
2863static struct weston_subsurface *
2864subsurface_from_surface(struct weston_surface *surface)
2865{
2866 struct weston_subsurface *sub;
2867
2868 sub = weston_surface_to_subsurface(surface);
2869 if (sub)
2870 return sub;
2871
2872 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2873 if (sub->surface == surface)
2874 return sub;
2875
2876 return NULL;
2877}
2878
2879static struct weston_subsurface *
2880subsurface_sibling_check(struct weston_subsurface *sub,
2881 struct weston_surface *surface,
2882 const char *request)
2883{
2884 struct weston_subsurface *sibling;
2885
2886 sibling = subsurface_from_surface(surface);
2887
2888 if (!sibling) {
2889 wl_resource_post_error(sub->resource,
2890 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2891 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002892 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002893 return NULL;
2894 }
2895
2896 if (sibling->parent != sub->parent) {
2897 wl_resource_post_error(sub->resource,
2898 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2899 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002900 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002901 return NULL;
2902 }
2903
2904 return sibling;
2905}
2906
2907static void
2908subsurface_place_above(struct wl_client *client,
2909 struct wl_resource *resource,
2910 struct wl_resource *sibling_resource)
2911{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002912 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002913 struct weston_surface *surface =
2914 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002915 struct weston_subsurface *sibling;
2916
2917 if (!sub)
2918 return;
2919
2920 sibling = subsurface_sibling_check(sub, surface, "place_above");
2921 if (!sibling)
2922 return;
2923
2924 wl_list_remove(&sub->parent_link_pending);
2925 wl_list_insert(sibling->parent_link_pending.prev,
2926 &sub->parent_link_pending);
2927}
2928
2929static void
2930subsurface_place_below(struct wl_client *client,
2931 struct wl_resource *resource,
2932 struct wl_resource *sibling_resource)
2933{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002934 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002935 struct weston_surface *surface =
2936 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002937 struct weston_subsurface *sibling;
2938
2939 if (!sub)
2940 return;
2941
2942 sibling = subsurface_sibling_check(sub, surface, "place_below");
2943 if (!sibling)
2944 return;
2945
2946 wl_list_remove(&sub->parent_link_pending);
2947 wl_list_insert(&sibling->parent_link_pending,
2948 &sub->parent_link_pending);
2949}
2950
2951static void
2952subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2953{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002954 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002955
2956 if (sub)
2957 sub->synchronized = 1;
2958}
2959
2960static void
2961subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2962{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002963 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002964
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002965 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002966 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002967
2968 /* If sub became effectively desynchronized, flush. */
2969 if (!weston_subsurface_is_synchronized(sub))
2970 weston_subsurface_synchronized_commit(sub);
2971 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002972}
2973
2974static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002975weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2976{
2977 wl_list_remove(&sub->parent_link);
2978 wl_list_remove(&sub->parent_link_pending);
2979 wl_list_remove(&sub->parent_destroy_listener.link);
2980 sub->parent = NULL;
2981}
2982
2983static void
2984weston_subsurface_destroy(struct weston_subsurface *sub);
2985
2986static void
2987subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2988{
2989 struct weston_subsurface *sub =
2990 container_of(listener, struct weston_subsurface,
2991 surface_destroy_listener);
2992 assert(data == &sub->surface->resource);
2993
2994 /* The protocol object (wl_resource) is left inert. */
2995 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002996 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002997
2998 weston_subsurface_destroy(sub);
2999}
3000
3001static void
3002subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3003{
3004 struct weston_subsurface *sub =
3005 container_of(listener, struct weston_subsurface,
3006 parent_destroy_listener);
3007 assert(data == &sub->parent->resource);
3008 assert(sub->surface != sub->parent);
3009
3010 if (weston_surface_is_mapped(sub->surface))
3011 weston_surface_unmap(sub->surface);
3012
3013 weston_subsurface_unlink_parent(sub);
3014}
3015
3016static void
3017subsurface_resource_destroy(struct wl_resource *resource)
3018{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003019 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003020
3021 if (sub)
3022 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003023}
3024
3025static void
3026subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3027{
3028 wl_resource_destroy(resource);
3029}
3030
3031static void
3032weston_subsurface_link_parent(struct weston_subsurface *sub,
3033 struct weston_surface *parent)
3034{
3035 sub->parent = parent;
3036 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003037 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003038 &sub->parent_destroy_listener);
3039
3040 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3041 wl_list_insert(&parent->subsurface_list_pending,
3042 &sub->parent_link_pending);
3043}
3044
3045static void
3046weston_subsurface_link_surface(struct weston_subsurface *sub,
3047 struct weston_surface *surface)
3048{
3049 sub->surface = surface;
3050 sub->surface_destroy_listener.notify =
3051 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003052 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003053 &sub->surface_destroy_listener);
3054}
3055
3056static void
3057weston_subsurface_destroy(struct weston_subsurface *sub)
3058{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003059 struct weston_view *view, *next;
3060
Pekka Paalanene67858b2013-04-25 13:57:42 +03003061 assert(sub->surface);
3062
3063 if (sub->resource) {
3064 assert(weston_surface_to_subsurface(sub->surface) == sub);
3065 assert(sub->parent_destroy_listener.notify ==
3066 subsurface_handle_parent_destroy);
3067
George Kiagiadakised04d382014-06-13 18:10:26 +02003068 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3069 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003070 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003071 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003072
Pekka Paalanene67858b2013-04-25 13:57:42 +03003073 if (sub->parent)
3074 weston_subsurface_unlink_parent(sub);
3075
Jason Ekstrand7b982072014-05-20 14:33:03 -05003076 weston_surface_state_fini(&sub->cached);
3077 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003078
3079 sub->surface->configure = NULL;
3080 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003081 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003082 } else {
3083 /* the dummy weston_subsurface for the parent itself */
3084 assert(sub->parent_destroy_listener.notify == NULL);
3085 wl_list_remove(&sub->parent_link);
3086 wl_list_remove(&sub->parent_link_pending);
3087 }
3088
3089 wl_list_remove(&sub->surface_destroy_listener.link);
3090 free(sub);
3091}
3092
3093static const struct wl_subsurface_interface subsurface_implementation = {
3094 subsurface_destroy,
3095 subsurface_set_position,
3096 subsurface_place_above,
3097 subsurface_place_below,
3098 subsurface_set_sync,
3099 subsurface_set_desync
3100};
3101
3102static struct weston_subsurface *
3103weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3104 struct weston_surface *parent)
3105{
3106 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003107 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003108
Bryce Harringtonde16d892014-11-20 22:21:57 -08003109 sub = zalloc(sizeof *sub);
3110 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003111 return NULL;
3112
Jason Ekstranda7af7042013-10-12 22:38:11 -05003113 wl_list_init(&sub->unused_views);
3114
Jason Ekstranda85118c2013-06-27 20:17:02 -05003115 sub->resource =
3116 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003117 if (!sub->resource) {
3118 free(sub);
3119 return NULL;
3120 }
3121
Jason Ekstranda85118c2013-06-27 20:17:02 -05003122 wl_resource_set_implementation(sub->resource,
3123 &subsurface_implementation,
3124 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003125 weston_subsurface_link_surface(sub, surface);
3126 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003127 weston_surface_state_init(&sub->cached);
3128 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003129 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003130
3131 return sub;
3132}
3133
3134/* Create a dummy subsurface for having the parent itself in its
3135 * sub-surface lists. Makes stacking order manipulation easy.
3136 */
3137static struct weston_subsurface *
3138weston_subsurface_create_for_parent(struct weston_surface *parent)
3139{
3140 struct weston_subsurface *sub;
3141
Bryce Harringtonde16d892014-11-20 22:21:57 -08003142 sub = zalloc(sizeof *sub);
3143 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003144 return NULL;
3145
3146 weston_subsurface_link_surface(sub, parent);
3147 sub->parent = parent;
3148 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3149 wl_list_insert(&parent->subsurface_list_pending,
3150 &sub->parent_link_pending);
3151
3152 return sub;
3153}
3154
3155static void
3156subcompositor_get_subsurface(struct wl_client *client,
3157 struct wl_resource *resource,
3158 uint32_t id,
3159 struct wl_resource *surface_resource,
3160 struct wl_resource *parent_resource)
3161{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003162 struct weston_surface *surface =
3163 wl_resource_get_user_data(surface_resource);
3164 struct weston_surface *parent =
3165 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166 struct weston_subsurface *sub;
3167 static const char where[] = "get_subsurface: wl_subsurface@";
3168
3169 if (surface == parent) {
3170 wl_resource_post_error(resource,
3171 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3172 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003173 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003174 return;
3175 }
3176
3177 if (weston_surface_to_subsurface(surface)) {
3178 wl_resource_post_error(resource,
3179 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3180 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003181 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003182 return;
3183 }
3184
Pekka Paalanen50b67472014-10-01 15:02:41 +03003185 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3186 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003187 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003188
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003189 if (weston_surface_get_main_surface(parent) == surface) {
3190 wl_resource_post_error(resource,
3191 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3192 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003193 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003194 return;
3195 }
3196
Pekka Paalanene67858b2013-04-25 13:57:42 +03003197 /* make sure the parent is in its own list */
3198 if (wl_list_empty(&parent->subsurface_list)) {
3199 if (!weston_subsurface_create_for_parent(parent)) {
3200 wl_resource_post_no_memory(resource);
3201 return;
3202 }
3203 }
3204
3205 sub = weston_subsurface_create(id, surface, parent);
3206 if (!sub) {
3207 wl_resource_post_no_memory(resource);
3208 return;
3209 }
3210
3211 surface->configure = subsurface_configure;
3212 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003213 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003214}
3215
3216static void
3217subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3218{
3219 wl_resource_destroy(resource);
3220}
3221
3222static const struct wl_subcompositor_interface subcompositor_interface = {
3223 subcompositor_destroy,
3224 subcompositor_get_subsurface
3225};
3226
3227static void
3228bind_subcompositor(struct wl_client *client,
3229 void *data, uint32_t version, uint32_t id)
3230{
3231 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003232 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003233
Jason Ekstranda85118c2013-06-27 20:17:02 -05003234 resource =
3235 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003236 if (resource == NULL) {
3237 wl_client_post_no_memory(client);
3238 return;
3239 }
3240 wl_resource_set_implementation(resource, &subcompositor_interface,
3241 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003242}
3243
3244static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003245weston_compositor_dpms(struct weston_compositor *compositor,
3246 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003247{
3248 struct weston_output *output;
3249
3250 wl_list_for_each(output, &compositor->output_list, link)
3251 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003252 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003253}
3254
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003255WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003256weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003257{
Neil Roberts8b62e202013-09-30 13:14:47 +01003258 uint32_t old_state = compositor->state;
3259
3260 /* The state needs to be changed before emitting the wake
3261 * signal because that may try to schedule a repaint which
3262 * will not work if the compositor is still sleeping */
3263 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3264
3265 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003266 case WESTON_COMPOSITOR_SLEEPING:
3267 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3268 /* fall through */
3269 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003270 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003271 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003272 /* fall through */
3273 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003274 wl_event_source_timer_update(compositor->idle_source,
3275 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003276 }
3277}
3278
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003279WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003280weston_compositor_offscreen(struct weston_compositor *compositor)
3281{
3282 switch (compositor->state) {
3283 case WESTON_COMPOSITOR_OFFSCREEN:
3284 return;
3285 case WESTON_COMPOSITOR_SLEEPING:
3286 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3287 /* fall through */
3288 default:
3289 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3290 wl_event_source_timer_update(compositor->idle_source, 0);
3291 }
3292}
3293
3294WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003295weston_compositor_sleep(struct weston_compositor *compositor)
3296{
3297 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3298 return;
3299
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003300 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003301 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3302 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3303}
3304
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003305static int
3306idle_handler(void *data)
3307{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003308 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003309
3310 if (compositor->idle_inhibit)
3311 return 1;
3312
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003313 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003314 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003315
3316 return 1;
3317}
3318
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003319WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003320weston_plane_init(struct weston_plane *plane,
3321 struct weston_compositor *ec,
3322 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003323{
3324 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003325 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003326 plane->x = x;
3327 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003328 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003329
3330 /* Init the link so that the call to wl_list_remove() when releasing
3331 * the plane without ever stacking doesn't lead to a crash */
3332 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003333}
3334
3335WL_EXPORT void
3336weston_plane_release(struct weston_plane *plane)
3337{
Xiong Zhang97116532013-10-23 13:58:31 +08003338 struct weston_view *view;
3339
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003340 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003341 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003342
Xiong Zhang97116532013-10-23 13:58:31 +08003343 wl_list_for_each(view, &plane->compositor->view_list, link) {
3344 if (view->plane == plane)
3345 view->plane = NULL;
3346 }
3347
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003348 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003349}
3350
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003351WL_EXPORT void
3352weston_compositor_stack_plane(struct weston_compositor *ec,
3353 struct weston_plane *plane,
3354 struct weston_plane *above)
3355{
3356 if (above)
3357 wl_list_insert(above->link.prev, &plane->link);
3358 else
3359 wl_list_insert(&ec->plane_list, &plane->link);
3360}
3361
Casey Dahlin9074db52012-04-19 22:50:09 -04003362static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003363{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003364 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003365}
3366
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003367static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003368bind_output(struct wl_client *client,
3369 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003371 struct weston_output *output = data;
3372 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003373 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003374
Jason Ekstranda85118c2013-06-27 20:17:02 -05003375 resource = wl_resource_create(client, &wl_output_interface,
3376 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003377 if (resource == NULL) {
3378 wl_client_post_no_memory(client);
3379 return;
3380 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003381
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003382 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003383 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003384
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003385 wl_output_send_geometry(resource,
3386 output->x,
3387 output->y,
3388 output->mm_width,
3389 output->mm_height,
3390 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003391 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003392 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003393 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003394 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003395 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003396
3397 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003398 wl_output_send_mode(resource,
3399 mode->flags,
3400 mode->width,
3401 mode->height,
3402 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003403 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003404
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003405 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003406 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003407}
3408
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003409/* Move other outputs when one is removed so the space remains contiguos. */
3410static void
3411weston_compositor_remove_output(struct weston_compositor *compositor,
3412 struct weston_output *remove_output)
3413{
3414 struct weston_output *output;
3415 int offset = 0;
3416
3417 wl_list_for_each(output, &compositor->output_list, link) {
3418 if (output == remove_output) {
3419 offset = output->width;
3420 continue;
3421 }
3422
3423 if (offset > 0) {
3424 weston_output_move(output,
3425 output->x - offset, output->y);
3426 output->dirty = 1;
3427 }
3428 }
3429}
3430
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003431WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003432weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003433{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003434 struct wl_resource *resource;
3435
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003436 output->destroying = 1;
3437
Pekka Paalanen133e4392014-09-23 22:08:46 -04003438 weston_presentation_feedback_discard_list(&output->feedback_list);
3439
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003440 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003441 wl_list_remove(&output->link);
3442
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003443 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003444 wl_signal_emit(&output->destroy_signal, output);
3445
Richard Hughesafe690c2013-05-02 10:10:04 +01003446 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003447 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003448 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003449 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003450
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003451 wl_resource_for_each(resource, &output->resource_list) {
3452 wl_resource_set_destructor(resource, NULL);
3453 }
3454
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003455 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003456}
3457
Scott Moreau1bad5db2012-08-18 01:04:05 -06003458static void
3459weston_output_compute_transform(struct weston_output *output)
3460{
3461 struct weston_matrix transform;
3462 int flip;
3463
3464 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003465 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003466
3467 switch(output->transform) {
3468 case WL_OUTPUT_TRANSFORM_FLIPPED:
3469 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3470 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3471 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003472 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003473 flip = -1;
3474 break;
3475 default:
3476 flip = 1;
3477 break;
3478 }
3479
3480 switch(output->transform) {
3481 case WL_OUTPUT_TRANSFORM_NORMAL:
3482 case WL_OUTPUT_TRANSFORM_FLIPPED:
3483 transform.d[0] = flip;
3484 transform.d[1] = 0;
3485 transform.d[4] = 0;
3486 transform.d[5] = 1;
3487 break;
3488 case WL_OUTPUT_TRANSFORM_90:
3489 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3490 transform.d[0] = 0;
3491 transform.d[1] = -flip;
3492 transform.d[4] = 1;
3493 transform.d[5] = 0;
3494 break;
3495 case WL_OUTPUT_TRANSFORM_180:
3496 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3497 transform.d[0] = -flip;
3498 transform.d[1] = 0;
3499 transform.d[4] = 0;
3500 transform.d[5] = -1;
3501 break;
3502 case WL_OUTPUT_TRANSFORM_270:
3503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3504 transform.d[0] = 0;
3505 transform.d[1] = flip;
3506 transform.d[4] = -1;
3507 transform.d[5] = 0;
3508 break;
3509 default:
3510 break;
3511 }
3512
3513 weston_matrix_multiply(&output->matrix, &transform);
3514}
3515
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003516WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003517weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003518{
Scott Moreau850ca422012-05-21 15:21:25 -06003519 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003520
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003521 weston_matrix_init(&output->matrix);
3522 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003523 -(output->x + output->width / 2.0),
3524 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003525
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003526 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003527 2.0 / output->width,
3528 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003529
Scott Moreauccbf29d2012-02-22 14:21:41 -07003530 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003531 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003532 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003533 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3534 output->zoom.trans_y, 0);
3535 weston_matrix_scale(&output->matrix, magnification,
3536 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003537 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003538
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003539 weston_output_compute_transform(output);
3540
Scott Moreauccbf29d2012-02-22 14:21:41 -07003541 output->dirty = 0;
3542}
3543
Scott Moreau1bad5db2012-08-18 01:04:05 -06003544static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003545weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003546{
3547 output->transform = transform;
3548
3549 switch (transform) {
3550 case WL_OUTPUT_TRANSFORM_90:
3551 case WL_OUTPUT_TRANSFORM_270:
3552 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3553 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3554 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003555 output->width = output->current_mode->height;
3556 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003557 break;
3558 case WL_OUTPUT_TRANSFORM_NORMAL:
3559 case WL_OUTPUT_TRANSFORM_180:
3560 case WL_OUTPUT_TRANSFORM_FLIPPED:
3561 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003562 output->width = output->current_mode->width;
3563 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003564 break;
3565 default:
3566 break;
3567 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003568
Hardening57388e42013-09-18 23:56:36 +02003569 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003570 output->width /= scale;
3571 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003572}
3573
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003574static void
3575weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003576{
3577 output->x = x;
3578 output->y = y;
3579
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003580 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003581 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003582 output->width,
3583 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003584}
3585
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003586WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003587weston_output_move(struct weston_output *output, int x, int y)
3588{
3589 pixman_region32_t old_region;
3590 struct wl_resource *resource;
3591
3592 output->move_x = x - output->x;
3593 output->move_y = y - output->y;
3594
3595 if (output->move_x == 0 && output->move_y == 0)
3596 return;
3597
3598 pixman_region32_init(&old_region);
3599 pixman_region32_copy(&old_region, &output->region);
3600
3601 weston_output_init_geometry(output, x, y);
3602
3603 output->dirty = 1;
3604
3605 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003606 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003607
3608 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003609 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003610 wl_output_send_geometry(resource,
3611 output->x,
3612 output->y,
3613 output->mm_width,
3614 output->mm_height,
3615 output->subpixel,
3616 output->make,
3617 output->model,
3618 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003619
3620 if (wl_resource_get_version(resource) >= 2)
3621 wl_output_send_done(resource);
3622 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003623}
3624
3625WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003626weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003627 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003628 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003629{
3630 output->compositor = c;
3631 output->x = x;
3632 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003633 output->mm_width = mm_width;
3634 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003635 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003636 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003637
Alexander Larsson0b135062013-05-28 16:23:36 +02003638 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003639 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003640
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003641 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003642 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003643
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003644 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003645 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003646 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003647 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003648 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003649
Casey Dahlin58ba1372012-04-19 22:50:08 -04003650 output->id = ffs(~output->compositor->output_id_pool) - 1;
3651 output->compositor->output_id_pool |= 1 << output->id;
3652
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003653 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003654 wl_global_create(c->wl_display, &wl_output_interface, 2,
3655 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003656 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003657}
3658
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003659WL_EXPORT void
3660weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003661 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003662 wl_fixed_t *x, wl_fixed_t *y)
3663{
3664 wl_fixed_t tx, ty;
3665 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003666 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003667
Hardeningff39efa2013-09-18 23:56:35 +02003668 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3669 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003670
3671 switch(output->transform) {
3672 case WL_OUTPUT_TRANSFORM_NORMAL:
3673 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003674 tx = device_x;
3675 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003676 break;
3677 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003678 tx = device_y;
3679 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003680 break;
3681 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003682 tx = width - device_x;
3683 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003684 break;
3685 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003686 tx = width - device_y;
3687 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003688 break;
3689 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003690 tx = width - device_x;
3691 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003692 break;
3693 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003694 tx = width - device_y;
3695 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003696 break;
3697 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003698 tx = device_x;
3699 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003700 break;
3701 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003702 tx = device_y;
3703 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003704 break;
3705 }
3706
Neil Robertseb5a2002014-05-01 16:13:55 +01003707 tx /= output->current_scale;
3708 ty /= output->current_scale;
3709
3710 if (output->zoom.active) {
3711 zoom_scale = output->zoom.spring_z.current;
3712 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3713 output->width / 2.0f *
3714 (zoom_scale + output->zoom.trans_x));
3715 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3716 output->height / 2.0f *
3717 (zoom_scale + output->zoom.trans_y));
3718 tx = wl_fixed_from_double(zx);
3719 ty = wl_fixed_from_double(zy);
3720 }
3721
3722 *x = tx + wl_fixed_from_int(output->x);
3723 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003724}
3725
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003726static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003727destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003728{
Jonny Lamb74130762013-11-26 18:19:46 +01003729 struct weston_surface *surface =
3730 wl_resource_get_user_data(resource);
3731
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003732 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003733 surface->pending.buffer_viewport.buffer.src_width =
3734 wl_fixed_from_int(-1);
3735 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003736 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003737}
3738
3739static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003740viewport_destroy(struct wl_client *client,
3741 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003742{
3743 wl_resource_destroy(resource);
3744}
3745
3746static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003747viewport_set(struct wl_client *client,
3748 struct wl_resource *resource,
3749 wl_fixed_t src_x,
3750 wl_fixed_t src_y,
3751 wl_fixed_t src_width,
3752 wl_fixed_t src_height,
3753 int32_t dst_width,
3754 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003755{
Jonny Lamb74130762013-11-26 18:19:46 +01003756 struct weston_surface *surface =
3757 wl_resource_get_user_data(resource);
3758
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003759 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003760
Jonny Lamb8ae35902013-11-26 18:19:45 +01003761 if (wl_fixed_to_double(src_width) < 0 ||
3762 wl_fixed_to_double(src_height) < 0) {
3763 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003764 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003765 "source dimensions must be non-negative (%fx%f)",
3766 wl_fixed_to_double(src_width),
3767 wl_fixed_to_double(src_height));
3768 return;
3769 }
3770
3771 if (dst_width <= 0 || dst_height <= 0) {
3772 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003773 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003774 "destination dimensions must be positive (%dx%d)",
3775 dst_width, dst_height);
3776 return;
3777 }
3778
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003779 surface->pending.buffer_viewport.buffer.src_x = src_x;
3780 surface->pending.buffer_viewport.buffer.src_y = src_y;
3781 surface->pending.buffer_viewport.buffer.src_width = src_width;
3782 surface->pending.buffer_viewport.buffer.src_height = src_height;
3783 surface->pending.buffer_viewport.surface.width = dst_width;
3784 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003785 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003786}
3787
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003788static void
3789viewport_set_source(struct wl_client *client,
3790 struct wl_resource *resource,
3791 wl_fixed_t src_x,
3792 wl_fixed_t src_y,
3793 wl_fixed_t src_width,
3794 wl_fixed_t src_height)
3795{
3796 struct weston_surface *surface =
3797 wl_resource_get_user_data(resource);
3798
3799 assert(surface->viewport_resource != NULL);
3800
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003801 if (src_width == wl_fixed_from_int(-1) &&
3802 src_height == wl_fixed_from_int(-1)) {
3803 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003804 surface->pending.buffer_viewport.buffer.src_width =
3805 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003806 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003807 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003808 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003809
3810 if (src_width <= 0 || src_height <= 0) {
3811 wl_resource_post_error(resource,
3812 WL_VIEWPORT_ERROR_BAD_VALUE,
3813 "source size must be positive (%fx%f)",
3814 wl_fixed_to_double(src_width),
3815 wl_fixed_to_double(src_height));
3816 return;
3817 }
3818
3819 surface->pending.buffer_viewport.buffer.src_x = src_x;
3820 surface->pending.buffer_viewport.buffer.src_y = src_y;
3821 surface->pending.buffer_viewport.buffer.src_width = src_width;
3822 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003823 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003824}
3825
3826static void
3827viewport_set_destination(struct wl_client *client,
3828 struct wl_resource *resource,
3829 int32_t dst_width,
3830 int32_t dst_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 (dst_width == -1 && dst_height == -1) {
3838 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003839 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003840 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003841 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003842 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003843
3844 if (dst_width <= 0 || dst_height <= 0) {
3845 wl_resource_post_error(resource,
3846 WL_VIEWPORT_ERROR_BAD_VALUE,
3847 "destination size must be positive (%dx%d)",
3848 dst_width, dst_height);
3849 return;
3850 }
3851
3852 surface->pending.buffer_viewport.surface.width = dst_width;
3853 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003854 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003855}
3856
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003857static const struct wl_viewport_interface viewport_interface = {
3858 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003859 viewport_set,
3860 viewport_set_source,
3861 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003862};
3863
3864static void
3865scaler_destroy(struct wl_client *client,
3866 struct wl_resource *resource)
3867{
3868 wl_resource_destroy(resource);
3869}
3870
3871static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003872scaler_get_viewport(struct wl_client *client,
3873 struct wl_resource *scaler,
3874 uint32_t id,
3875 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003876{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003877 int version = wl_resource_get_version(scaler);
3878 struct weston_surface *surface =
3879 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003880 struct wl_resource *resource;
3881
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003882 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003883 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003884 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3885 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003886 return;
3887 }
3888
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003889 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003890 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003891 if (resource == NULL) {
3892 wl_client_post_no_memory(client);
3893 return;
3894 }
3895
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003896 wl_resource_set_implementation(resource, &viewport_interface,
3897 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003898
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003899 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003900}
3901
3902static const struct wl_scaler_interface scaler_interface = {
3903 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003904 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003905};
3906
3907static void
3908bind_scaler(struct wl_client *client,
3909 void *data, uint32_t version, uint32_t id)
3910{
3911 struct wl_resource *resource;
3912
3913 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003914 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003915 if (resource == NULL) {
3916 wl_client_post_no_memory(client);
3917 return;
3918 }
3919
3920 wl_resource_set_implementation(resource, &scaler_interface,
3921 NULL, NULL);
3922}
3923
3924static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003925destroy_presentation_feedback(struct wl_resource *feedback_resource)
3926{
3927 struct weston_presentation_feedback *feedback;
3928
3929 feedback = wl_resource_get_user_data(feedback_resource);
3930
3931 wl_list_remove(&feedback->link);
3932 free(feedback);
3933}
3934
3935static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003936presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3937{
3938 wl_resource_destroy(resource);
3939}
3940
3941static void
3942presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003943 struct wl_resource *presentation_resource,
3944 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003945 uint32_t callback)
3946{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003947 struct weston_surface *surface;
3948 struct weston_presentation_feedback *feedback;
3949
3950 surface = wl_resource_get_user_data(surface_resource);
3951
Bryce Harringtonde16d892014-11-20 22:21:57 -08003952 feedback = zalloc(sizeof *feedback);
3953 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04003954 goto err_calloc;
3955
3956 feedback->resource = wl_resource_create(client,
3957 &presentation_feedback_interface,
3958 1, callback);
3959 if (!feedback->resource)
3960 goto err_create;
3961
3962 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3963 destroy_presentation_feedback);
3964
3965 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3966
3967 return;
3968
3969err_create:
3970 free(feedback);
3971
3972err_calloc:
3973 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003974}
3975
3976static const struct presentation_interface presentation_implementation = {
3977 presentation_destroy,
3978 presentation_feedback
3979};
3980
3981static void
3982bind_presentation(struct wl_client *client,
3983 void *data, uint32_t version, uint32_t id)
3984{
3985 struct weston_compositor *compositor = data;
3986 struct wl_resource *resource;
3987
3988 resource = wl_resource_create(client, &presentation_interface,
3989 MIN(version, 1), id);
3990 if (resource == NULL) {
3991 wl_client_post_no_memory(client);
3992 return;
3993 }
3994
3995 wl_resource_set_implementation(resource, &presentation_implementation,
3996 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003997 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003998}
3999
4000static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004001compositor_bind(struct wl_client *client,
4002 void *data, uint32_t version, uint32_t id)
4003{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004004 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004005 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004006
Jason Ekstranda85118c2013-06-27 20:17:02 -05004007 resource = wl_resource_create(client, &wl_compositor_interface,
4008 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004009 if (resource == NULL) {
4010 wl_client_post_no_memory(client);
4011 return;
4012 }
4013
4014 wl_resource_set_implementation(resource, &compositor_interface,
4015 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004016}
4017
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004018static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004019log_uname(void)
4020{
4021 struct utsname usys;
4022
4023 uname(&usys);
4024
4025 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4026 usys.version, usys.machine);
4027}
4028
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004029WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004030weston_environment_get_fd(const char *env)
4031{
4032 char *e, *end;
4033 int fd, flags;
4034
4035 e = getenv(env);
4036 if (!e)
4037 return -1;
4038 fd = strtol(e, &end, 0);
4039 if (*end != '\0')
4040 return -1;
4041
4042 flags = fcntl(fd, F_GETFD);
4043 if (flags == -1)
4044 return -1;
4045
4046 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4047 unsetenv(env);
4048
4049 return fd;
4050}
4051
Pekka Paalanenb5026542014-11-12 15:09:24 +02004052static void
4053timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4054 uint32_t key, void *data)
4055{
4056 struct weston_compositor *compositor = data;
4057
4058 if (weston_timeline_enabled_)
4059 weston_timeline_close();
4060 else
4061 weston_timeline_open(compositor);
4062}
4063
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004064WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004065weston_compositor_init(struct weston_compositor *ec,
4066 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004067 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004068 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004069{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004070 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004071 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004072 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004073
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004074 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004075 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004076 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004077 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004078 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004079 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004080 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004081 wl_signal_init(&ec->idle_signal);
4082 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004083 wl_signal_init(&ec->show_input_panel_signal);
4084 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004085 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004086 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004087 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004088 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004089 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004090 wl_signal_init(&ec->session_signal);
4091 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004092
Casey Dahlin58ba1372012-04-19 22:50:08 -04004093 ec->output_id_pool = 0;
4094
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004095 if (!wl_global_create(display, &wl_compositor_interface, 3,
4096 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004097 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004098
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004099 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4100 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004101 return -1;
4102
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004103 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004104 ec, bind_scaler))
4105 return -1;
4106
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004107 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4108 ec, bind_presentation))
4109 return -1;
4110
Jason Ekstranda7af7042013-10-12 22:38:11 -05004111 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004112 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004113 wl_list_init(&ec->layer_list);
4114 wl_list_init(&ec->seat_list);
4115 wl_list_init(&ec->output_list);
4116 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004117 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004118 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004119 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004120 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004121 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004122
Xiong Zhang97116532013-10-23 13:58:31 +08004123 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004124 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004125
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004126 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4127 weston_config_section_get_string(s, "keymap_rules",
4128 (char **) &xkb_names.rules, NULL);
4129 weston_config_section_get_string(s, "keymap_model",
4130 (char **) &xkb_names.model, NULL);
4131 weston_config_section_get_string(s, "keymap_layout",
4132 (char **) &xkb_names.layout, NULL);
4133 weston_config_section_get_string(s, "keymap_variant",
4134 (char **) &xkb_names.variant, NULL);
4135 weston_config_section_get_string(s, "keymap_options",
4136 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004137
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004138 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4139 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004140
Jonny Lamb66a41a02014-08-12 14:58:25 +02004141 weston_config_section_get_int(s, "repeat-rate",
4142 &ec->kb_repeat_rate, 40);
4143 weston_config_section_get_int(s, "repeat-delay",
4144 &ec->kb_repeat_delay, 400);
4145
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004146 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004147
4148 wl_data_device_manager_init(ec->wl_display);
4149
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004150 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004151
Daniel Stone725c2c32012-06-22 14:04:36 +01004152 loop = wl_display_get_event_loop(ec->wl_display);
4153 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4154 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4155
4156 ec->input_loop = wl_event_loop_create();
4157
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004158 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4159 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4160
Pekka Paalanenb5026542014-11-12 15:09:24 +02004161 weston_compositor_add_debug_binding(ec, KEY_T,
4162 timeline_key_binding_handler, ec);
4163
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004164 weston_compositor_schedule_repaint(ec);
4165
Daniel Stone725c2c32012-06-22 14:04:36 +01004166 return 0;
4167}
4168
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004169WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004170weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004171{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004172 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004173
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004174 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004175 if (ec->input_loop_source)
4176 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004177
Matt Roper361d2ad2011-08-29 13:52:23 -07004178 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004179 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004180 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004181
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004182 if (ec->renderer)
4183 ec->renderer->destroy(ec);
4184
Daniel Stone325fc2d2012-05-30 16:31:58 +01004185 weston_binding_list_destroy_all(&ec->key_binding_list);
4186 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004187 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004188 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004189 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004190
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004191 weston_plane_release(&ec->primary_plane);
4192
Jonas Ådahlc97af922012-03-28 22:36:09 +02004193 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004194
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004195 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004196}
4197
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004198WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004199weston_compositor_exit_with_code(struct weston_compositor *compositor,
4200 int exit_code)
4201{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004202 if (compositor->exit_code == EXIT_SUCCESS)
4203 compositor->exit_code = exit_code;
4204
Frederic Plourdec336f062014-10-29 14:44:33 -04004205 wl_display_terminate(compositor->wl_display);
4206}
4207
4208WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004209weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4210 const struct weston_pointer_grab_interface *interface)
4211{
4212 struct weston_seat *seat;
4213
4214 ec->default_pointer_grab = interface;
4215 wl_list_for_each(seat, &ec->seat_list, link) {
4216 if (seat->pointer) {
4217 weston_pointer_set_default_grab(seat->pointer,
4218 interface);
4219 }
4220 }
4221}
4222
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004223WL_EXPORT int
4224weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4225 clockid_t clk_id)
4226{
4227 struct timespec ts;
4228
4229 if (clock_gettime(clk_id, &ts) < 0)
4230 return -1;
4231
4232 compositor->presentation_clock = clk_id;
4233
4234 return 0;
4235}
4236
4237/*
4238 * For choosing the software clock, when the display hardware or API
4239 * does not expose a compatible presentation timestamp.
4240 */
4241WL_EXPORT int
4242weston_compositor_set_presentation_clock_software(
4243 struct weston_compositor *compositor)
4244{
4245 /* In order of preference */
4246 static const clockid_t clocks[] = {
4247 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4248 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4249 CLOCK_MONOTONIC, /* no jumps, may crawl */
4250 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4251 CLOCK_REALTIME /* may jump and crawl */
4252 };
4253 unsigned i;
4254
4255 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4256 if (weston_compositor_set_presentation_clock(compositor,
4257 clocks[i]) == 0)
4258 return 0;
4259
4260 weston_log("Error: no suitable presentation clock available.\n");
4261
4262 return -1;
4263}
4264
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004265WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004266weston_version(int *major, int *minor, int *micro)
4267{
4268 *major = WESTON_VERSION_MAJOR;
4269 *minor = WESTON_VERSION_MINOR;
4270 *micro = WESTON_VERSION_MICRO;
4271}
4272
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004273static const char *
4274clock_name(clockid_t clk_id)
4275{
4276 static const char *names[] = {
4277 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4278 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4279 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4280 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4281 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4282 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4283 };
4284
4285 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4286 return "unknown";
4287
4288 return names[clk_id];
4289}
4290
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004291static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004292 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004293 const char *desc;
4294} capability_strings[] = {
4295 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004296 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004297};
4298
4299static void
4300weston_compositor_log_capabilities(struct weston_compositor *compositor)
4301{
4302 unsigned i;
4303 int yes;
4304
4305 weston_log("Compositor capabilities:\n");
4306 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4307 yes = compositor->capabilities & capability_strings[i].bit;
4308 weston_log_continue(STAMP_SPACE "%s %s\n",
4309 capability_strings[i].desc,
4310 yes ? "yes" : "no");
4311 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004312
4313 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4314 clock_name(compositor->presentation_clock),
4315 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004316}
4317
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004318static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004319{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004320 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004321
Martin Minarik6d118362012-06-07 18:01:59 +02004322 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004323 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004324
4325 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004326}
4327
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004328#ifdef HAVE_LIBUNWIND
4329
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004330static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004331print_backtrace(void)
4332{
4333 unw_cursor_t cursor;
4334 unw_context_t context;
4335 unw_word_t off;
4336 unw_proc_info_t pip;
4337 int ret, i = 0;
4338 char procname[256];
4339 const char *filename;
4340 Dl_info dlinfo;
4341
4342 pip.unwind_info = NULL;
4343 ret = unw_getcontext(&context);
4344 if (ret) {
4345 weston_log("unw_getcontext: %d\n", ret);
4346 return;
4347 }
4348
4349 ret = unw_init_local(&cursor, &context);
4350 if (ret) {
4351 weston_log("unw_init_local: %d\n", ret);
4352 return;
4353 }
4354
4355 ret = unw_step(&cursor);
4356 while (ret > 0) {
4357 ret = unw_get_proc_info(&cursor, &pip);
4358 if (ret) {
4359 weston_log("unw_get_proc_info: %d\n", ret);
4360 break;
4361 }
4362
4363 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4364 if (ret && ret != -UNW_ENOMEM) {
4365 if (ret != -UNW_EUNSPEC)
4366 weston_log("unw_get_proc_name: %d\n", ret);
4367 procname[0] = '?';
4368 procname[1] = 0;
4369 }
4370
4371 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4372 *dlinfo.dli_fname)
4373 filename = dlinfo.dli_fname;
4374 else
4375 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004376
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004377 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4378 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4379
4380 ret = unw_step(&cursor);
4381 if (ret < 0)
4382 weston_log("unw_step: %d\n", ret);
4383 }
4384}
4385
4386#else
4387
4388static void
4389print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004390{
4391 void *buffer[32];
4392 int i, count;
4393 Dl_info info;
4394
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004395 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4396 for (i = 0; i < count; i++) {
4397 dladdr(buffer[i], &info);
4398 weston_log(" [%016lx] %s (%s)\n",
4399 (long) buffer[i],
4400 info.dli_sname ? info.dli_sname : "--",
4401 info.dli_fname);
4402 }
4403}
4404
4405#endif
4406
4407static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004408on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004409{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004410 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004411 * then call the backend restore function, which will switch
4412 * back to the vt we launched from or ungrab X etc and then
4413 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004414 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004415 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004416 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004417
Peter Maatmane5b42e42013-03-27 22:38:53 +01004418 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004419
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004420 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004421
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004422 segv_compositor->restore(segv_compositor);
4423
4424 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004425}
4426
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004427WL_EXPORT void *
4428weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004429{
4430 char path[PATH_MAX];
4431 void *module, *init;
4432
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004433 if (name == NULL)
4434 return NULL;
4435
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004436 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004437 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004438 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004439 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004440
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004441 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4442 if (module) {
4443 weston_log("Module '%s' already loaded\n", path);
4444 dlclose(module);
4445 return NULL;
4446 }
4447
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004448 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004449 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004450 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004451 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004452 return NULL;
4453 }
4454
4455 init = dlsym(module, entrypoint);
4456 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004457 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004458 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004459 return NULL;
4460 }
4461
4462 return init;
4463}
4464
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004465static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004466load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004467 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004468{
4469 const char *p, *end;
4470 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004471 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004472 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004473
4474 if (modules == NULL)
4475 return 0;
4476
4477 p = modules;
4478 while (*p) {
4479 end = strchrnul(p, ',');
4480 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004481 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004482 if (!module_init)
4483 return -1;
4484 if (module_init(ec, argc, argv) < 0)
4485 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004486 p = end;
4487 while (*p == ',')
4488 p++;
4489
4490 }
4491
4492 return 0;
4493}
4494
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004495static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004496 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4497
4498static const char xdg_wrong_message[] =
4499 "fatal: environment variable XDG_RUNTIME_DIR\n"
4500 "is set to \"%s\", which is not a directory.\n";
4501
4502static const char xdg_wrong_mode_message[] =
4503 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004504 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4505 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004506
4507static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004508 "Refer to your distribution on how to get it, or\n"
4509 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4510 "on how to implement it.\n";
4511
Martin Minarik37032f82012-06-18 20:15:18 +02004512static void
4513verify_xdg_runtime_dir(void)
4514{
4515 char *dir = getenv("XDG_RUNTIME_DIR");
4516 struct stat s;
4517
4518 if (!dir) {
4519 weston_log(xdg_error_message);
4520 weston_log_continue(xdg_detail_message);
4521 exit(EXIT_FAILURE);
4522 }
4523
4524 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4525 weston_log(xdg_wrong_message, dir);
4526 weston_log_continue(xdg_detail_message);
4527 exit(EXIT_FAILURE);
4528 }
4529
4530 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4531 weston_log(xdg_wrong_mode_message,
4532 dir, s.st_mode & 0777, s.st_uid);
4533 weston_log_continue(xdg_detail_message);
4534 }
4535}
4536
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004537static int
4538usage(int error_code)
4539{
4540 fprintf(stderr,
4541 "Usage: weston [OPTIONS]\n\n"
4542 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4543 "Weston supports multiple backends, and depending on which backend is in use\n"
4544 "different options will be accepted.\n\n"
4545
4546
4547 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004548 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004549 " -B, --backend=MODULE\tBackend module, one of\n"
4550#if defined(BUILD_DRM_COMPOSITOR)
4551 "\t\t\t\tdrm-backend.so\n"
4552#endif
4553#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004554 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004555#endif
4556#if defined(BUILD_X11_COMPOSITOR)
4557 "\t\t\t\tx11-backend.so\n"
4558#endif
4559#if defined(BUILD_WAYLAND_COMPOSITOR)
4560 "\t\t\t\twayland-backend.so\n"
4561#endif
4562#if defined(BUILD_RDP_COMPOSITOR)
4563 "\t\t\t\trdp-backend.so\n"
4564#endif
4565#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4566 "\t\t\t\trpi-backend.so\n"
4567#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004568 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004569 " -S, --socket=NAME\tName of socket to listen on\n"
4570 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004571 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004572 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004573 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004574 " -h, --help\t\tThis help message\n\n");
4575
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004576#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004577 fprintf(stderr,
4578 "Options for drm-backend.so:\n\n"
4579 " --connector=ID\tBring up only this connector\n"
4580 " --seat=SEAT\t\tThe seat that weston should run on\n"
4581 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004582 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004583 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004584#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004585
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004586#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004587 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004588 "Options for fbdev-backend.so:\n\n"
4589 " --tty=TTY\t\tThe tty to use\n"
4590 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004591#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004592
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004593#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004594 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004595 "Options for x11-backend.so:\n\n"
4596 " --width=WIDTH\t\tWidth of X window\n"
4597 " --height=HEIGHT\tHeight of X window\n"
4598 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004599 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004600 " --output-count=COUNT\tCreate multiple outputs\n"
4601 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004602#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004603
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004604#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004605 fprintf(stderr,
4606 "Options for wayland-backend.so:\n\n"
4607 " --width=WIDTH\t\tWidth of Wayland surface\n"
4608 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004609 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004610 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004611 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004612 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004613 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004614 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004615#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004616
Pekka Paalanene31e0532013-05-22 18:03:07 +03004617#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4618 fprintf(stderr,
4619 "Options for rpi-backend.so:\n\n"
4620 " --tty=TTY\t\tThe tty to use\n"
4621 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4622 " --transform=TR\tThe output transformation, TR is one of:\n"
4623 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004624 " --opaque-regions\tEnable support for opaque regions, can be "
4625 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004626 "\n");
4627#endif
4628
Hardeningc077a842013-07-08 00:51:35 +02004629#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004630 fprintf(stderr,
4631 "Options for rdp-backend.so:\n\n"
4632 " --width=WIDTH\t\tWidth of desktop\n"
4633 " --height=HEIGHT\tHeight of desktop\n"
4634 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4635 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004636 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004637 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4638 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4639 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4640 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4641 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004642#endif
4643
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004644#if defined(BUILD_HEADLESS_COMPOSITOR)
4645 fprintf(stderr,
4646 "Options for headless-backend.so:\n\n"
4647 " --width=WIDTH\t\tWidth of memory surface\n"
4648 " --height=HEIGHT\tHeight of memory surface\n"
4649 " --transform=TR\tThe output transformation, TR is one of:\n"
4650 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4651 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4652#endif
4653
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004654 exit(error_code);
4655}
4656
Peter Maatmane5b42e42013-03-27 22:38:53 +01004657static void
4658catch_signals(void)
4659{
4660 struct sigaction action;
4661
4662 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4663 action.sa_sigaction = on_caught_signal;
4664 sigemptyset(&action.sa_mask);
4665 sigaction(SIGSEGV, &action, NULL);
4666 sigaction(SIGABRT, &action, NULL);
4667}
4668
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004669static void
4670handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4671{
4672 struct wl_client *client = data;
4673
4674 weston_log("Primary client died. Closing...\n");
4675
4676 wl_display_terminate(wl_client_get_display(client));
4677}
4678
Ryo Munakatad8deff62014-09-06 07:32:05 +09004679static char *
4680weston_choose_default_backend(void)
4681{
4682 char *backend = NULL;
4683
4684 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4685 backend = strdup("wayland-backend.so");
4686 else if (getenv("DISPLAY"))
4687 backend = strdup("x11-backend.so");
4688 else
4689 backend = strdup(WESTON_NATIVE_BACKEND);
4690
4691 return backend;
4692}
4693
4694static int
4695weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4696{
4697 if (socket_name) {
4698 if (wl_display_add_socket(display, socket_name)) {
4699 weston_log("fatal: failed to add socket: %m\n");
4700 return -1;
4701 }
4702 } else {
4703 socket_name = wl_display_add_socket_auto(display);
4704 if (!socket_name) {
4705 weston_log("fatal: failed to add socket: %m\n");
4706 return -1;
4707 }
4708 }
4709
4710 setenv("WAYLAND_DISPLAY", socket_name, 1);
4711
4712 return 0;
4713}
4714
Derek Foreman64a3df02014-10-23 12:24:18 -05004715static const struct { const char *name; uint32_t token; } transforms[] = {
4716 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4717 { "90", WL_OUTPUT_TRANSFORM_90 },
4718 { "180", WL_OUTPUT_TRANSFORM_180 },
4719 { "270", WL_OUTPUT_TRANSFORM_270 },
4720 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4721 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4722 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4723 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4724};
4725
4726WL_EXPORT int
4727weston_parse_transform(const char *transform, uint32_t *out)
4728{
4729 unsigned int i;
4730
4731 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4732 if (strcmp(transforms[i].name, transform) == 0) {
4733 *out = transforms[i].token;
4734 return 0;
4735 }
4736
4737 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4738 return -1;
4739}
4740
4741WL_EXPORT const char *
4742weston_transform_to_string(uint32_t output_transform)
4743{
4744 unsigned int i;
4745
4746 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4747 if (transforms[i].token == output_transform)
4748 return transforms[i].name;
4749
4750 return "<illegal value>";
4751}
4752
4753
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004754int main(int argc, char *argv[])
4755{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004756 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004757 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004758 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004759 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004760 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004761 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004762 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004763 int *argc, char *argv[],
4764 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004765 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004766 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004767 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004768 char *modules = NULL;
4769 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004770 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004771 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004772 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004773 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004774 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004775 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004776 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004777 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004778 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004779 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004780 struct wl_client *primary_client;
4781 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004782 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004783
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004784 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004785 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4786 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004787 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4788 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004789 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004790 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004791 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004792 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004793 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004794 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004795
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004796 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004797
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004798 if (help)
4799 usage(EXIT_SUCCESS);
4800
Scott Moreau12245142012-08-29 15:15:58 -06004801 if (version) {
4802 printf(PACKAGE_STRING "\n");
4803 return EXIT_SUCCESS;
4804 }
4805
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004806 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004807
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004808 weston_log("%s\n"
4809 STAMP_SPACE "%s\n"
4810 STAMP_SPACE "Bug reports to: %s\n"
4811 STAMP_SPACE "Build: %s\n",
4812 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004813 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004814 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004815
Martin Minarik37032f82012-06-18 20:15:18 +02004816 verify_xdg_runtime_dir();
4817
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004818 display = wl_display_create();
4819
Tiago Vignatti2116b892011-08-08 05:52:59 -07004820 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004821 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4822 display);
4823 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4824 display);
4825 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4826 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004827
4828 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004829 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4830 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004831
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304832 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4833 ret = EXIT_FAILURE;
4834 goto out_signals;
4835 }
4836
Pekka Paalanen588bee12014-05-07 16:26:25 +03004837 if (noconfig == 0)
4838 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004839 if (config != NULL) {
4840 weston_log("Using config file '%s'\n",
4841 weston_config_get_full_path(config));
4842 } else {
4843 weston_log("Starting with no config file.\n");
4844 }
4845 section = weston_config_get_section(config, "core", NULL, NULL);
4846
Ryo Munakata03faed22014-09-06 07:32:51 +09004847 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004848 weston_config_section_get_string(section, "backend", &backend,
4849 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004850 if (!backend)
4851 backend = weston_choose_default_backend();
4852 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004853
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004854 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304855 if (!backend_init) {
4856 ret = EXIT_FAILURE;
4857 goto out_signals;
4858 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004859
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004860 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004861 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004862 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304863 ret = EXIT_FAILURE;
4864 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004865 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004866
Peter Maatmane5b42e42013-03-27 22:38:53 +01004867 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004868 segv_compositor = ec;
4869
Frederic Plourde4a84c832014-10-30 15:06:34 -04004870 if (idle_time < 0)
4871 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4872 if (idle_time < 0)
4873 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004874 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004875 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004876 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004877
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004878 for (i = 1; i < argc; i++)
4879 weston_log("fatal: unhandled option: %s\n", argv[i]);
4880 if (argc > 1) {
4881 ret = EXIT_FAILURE;
4882 goto out;
4883 }
4884
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004885 weston_compositor_log_capabilities(ec);
4886
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004887 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4888 if (server_socket) {
4889 weston_log("Running with single client\n");
4890 fd = strtol(server_socket, &end, 0);
4891 if (*end != '\0')
4892 fd = -1;
4893 } else {
4894 fd = -1;
4895 }
4896
4897 if (fd != -1) {
4898 primary_client = wl_client_create(display, fd);
4899 if (!primary_client) {
4900 weston_log("fatal: failed to add client: %m\n");
4901 ret = EXIT_FAILURE;
4902 goto out;
4903 }
4904 primary_client_destroyed.notify =
4905 handle_primary_client_destroyed;
4906 wl_client_add_destroy_listener(primary_client,
4907 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004908 } else if (weston_create_listening_socket(display, socket_name)) {
4909 ret = EXIT_FAILURE;
4910 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004911 }
4912
Ryo Munakata03faed22014-09-06 07:32:51 +09004913 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004914 weston_config_section_get_string(section, "shell", &shell,
4915 "desktop-shell.so");
4916
Ryo Munakata03faed22014-09-06 07:32:51 +09004917 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004918 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004919
4920 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004921 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004922 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004923
4924 if (load_modules(ec, option_modules, &argc, argv) < 0)
4925 goto out;
4926
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004927 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4928 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4929 if (numlock_on) {
4930 wl_list_for_each(seat, &ec->seat_list, link) {
4931 if (seat->keyboard)
4932 weston_keyboard_set_locks(seat->keyboard,
4933 WESTON_NUM_LOCK,
4934 WESTON_NUM_LOCK);
4935 }
4936 }
4937
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004938 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004939
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004940 wl_display_run(display);
4941
Frederic Plourdec336f062014-10-29 14:44:33 -04004942 /* Allow for setting return exit code after
4943 * wl_display_run returns normally. This is
4944 * useful for devs/testers and automated tests
4945 * that want to indicate failure status to
4946 * testing infrastructure above
4947 */
4948 ret = ec->exit_code;
4949
Ryo Munakata03faed22014-09-06 07:32:51 +09004950out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004951 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004952 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004953
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004954 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004955
Daniel Stone855028f2012-05-01 20:37:10 +01004956 weston_compositor_xkb_destroy(ec);
4957
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004958 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304959
4960out_signals:
4961 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4962 if (signals[i])
4963 wl_event_source_remove(signals[i]);
4964
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004965 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004966
Martin Minarik19e6f262012-06-07 13:08:46 +02004967 weston_log_file_close();
4968
Ryo Munakata03faed22014-09-06 07:32:51 +09004969 free(backend);
4970 free(shell);
4971 free(socket_name);
4972 free(option_modules);
4973 free(log);
4974 free(modules);
4975
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004976 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004977}