blob: 8085b05577943d3f1d578ad73355b93b53bfeee6 [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,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200498 uint64_t seq,
499 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400500{
501 struct wl_client *client = wl_resource_get_client(feedback->resource);
502 struct wl_resource *o;
503 uint64_t secs;
Pekka Paalanen133e4392014-09-23 22:08:46 -0400504
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,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200527 uint64_t seq,
528 uint32_t flags)
Pekka Paalanen133e4392014-09-23 22:08:46 -0400529{
530 struct weston_presentation_feedback *feedback, *tmp;
531
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200532 assert(!(flags & PRESENTATION_FEEDBACK_INVALID) ||
533 wl_list_empty(list));
534
Pekka Paalanen133e4392014-09-23 22:08:46 -0400535 wl_list_for_each_safe(feedback, tmp, list, link)
536 weston_presentation_feedback_present(feedback, output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200537 refresh_nsec, ts, seq,
538 flags);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400539}
540
Jason Ekstrand7b982072014-05-20 14:33:03 -0500541static void
542surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
543{
544 struct weston_surface_state *state =
545 container_of(listener, struct weston_surface_state,
546 buffer_destroy_listener);
547
548 state->buffer = NULL;
549}
550
551static void
552weston_surface_state_init(struct weston_surface_state *state)
553{
554 state->newly_attached = 0;
555 state->buffer = NULL;
556 state->buffer_destroy_listener.notify =
557 surface_state_handle_buffer_destroy;
558 state->sx = 0;
559 state->sy = 0;
560
561 pixman_region32_init(&state->damage);
562 pixman_region32_init(&state->opaque);
563 region_init_infinite(&state->input);
564
565 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400566 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500567
568 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
569 state->buffer_viewport.buffer.scale = 1;
570 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
571 state->buffer_viewport.surface.width = -1;
572 state->buffer_viewport.changed = 0;
573}
574
575static void
576weston_surface_state_fini(struct weston_surface_state *state)
577{
578 struct weston_frame_callback *cb, *next;
579
580 wl_list_for_each_safe(cb, next,
581 &state->frame_callback_list, link)
582 wl_resource_destroy(cb->resource);
583
Pekka Paalanen133e4392014-09-23 22:08:46 -0400584 weston_presentation_feedback_discard_list(&state->feedback_list);
585
Jason Ekstrand7b982072014-05-20 14:33:03 -0500586 pixman_region32_fini(&state->input);
587 pixman_region32_fini(&state->opaque);
588 pixman_region32_fini(&state->damage);
589
590 if (state->buffer)
591 wl_list_remove(&state->buffer_destroy_listener.link);
592 state->buffer = NULL;
593}
594
595static void
596weston_surface_state_set_buffer(struct weston_surface_state *state,
597 struct weston_buffer *buffer)
598{
599 if (state->buffer == buffer)
600 return;
601
602 if (state->buffer)
603 wl_list_remove(&state->buffer_destroy_listener.link);
604 state->buffer = buffer;
605 if (state->buffer)
606 wl_signal_add(&state->buffer->destroy_signal,
607 &state->buffer_destroy_listener);
608}
609
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500610WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500611weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500612{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500613 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400614
Bryce Harringtonde16d892014-11-20 22:21:57 -0800615 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400616 if (surface == NULL)
617 return NULL;
618
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500619 wl_signal_init(&surface->destroy_signal);
620
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500621 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200622 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400623
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200624 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
625 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200626 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
627 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500628
629 weston_surface_state_init(&surface->pending);
630
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400631 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500632 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300633 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400634
Jason Ekstranda7af7042013-10-12 22:38:11 -0500635 wl_list_init(&surface->views);
636
637 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400638 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500639
Pekka Paalanene67858b2013-04-25 13:57:42 +0300640 wl_list_init(&surface->subsurface_list);
641 wl_list_init(&surface->subsurface_list_pending);
642
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400643 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500644}
645
Alex Wu8811bf92012-02-28 18:07:54 +0800646WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500647weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200648 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500649{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100650 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500651}
652
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400653WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500654weston_view_to_global_float(struct weston_view *view,
655 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200656{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500657 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200658 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
659
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200661
662 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200663 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700664 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200665 v.f[3]);
666 *x = 0;
667 *y = 0;
668 return;
669 }
670
671 *x = v.f[0] / v.f[3];
672 *y = v.f[1] / v.f[3];
673 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500674 *x = sx + view->geometry.x;
675 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200676 }
677}
678
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500679WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200680weston_transformed_coord(int width, int height,
681 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200682 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200683 float sx, float sy, float *bx, float *by)
684{
685 switch (transform) {
686 case WL_OUTPUT_TRANSFORM_NORMAL:
687 default:
688 *bx = sx;
689 *by = sy;
690 break;
691 case WL_OUTPUT_TRANSFORM_FLIPPED:
692 *bx = width - sx;
693 *by = sy;
694 break;
695 case WL_OUTPUT_TRANSFORM_90:
696 *bx = height - sy;
697 *by = sx;
698 break;
699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
700 *bx = height - sy;
701 *by = width - sx;
702 break;
703 case WL_OUTPUT_TRANSFORM_180:
704 *bx = width - sx;
705 *by = height - sy;
706 break;
707 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
708 *bx = sx;
709 *by = height - sy;
710 break;
711 case WL_OUTPUT_TRANSFORM_270:
712 *bx = sy;
713 *by = width - sx;
714 break;
715 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
716 *bx = sy;
717 *by = sx;
718 break;
719 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200720
721 *bx *= scale;
722 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200723}
724
725WL_EXPORT pixman_box32_t
726weston_transformed_rect(int width, int height,
727 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200728 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200729 pixman_box32_t rect)
730{
731 float x1, x2, y1, y2;
732
733 pixman_box32_t ret;
734
Alexander Larsson4ea95522013-05-22 14:41:37 +0200735 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200736 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200737 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200738 rect.x2, rect.y2, &x2, &y2);
739
740 if (x1 <= x2) {
741 ret.x1 = x1;
742 ret.x2 = x2;
743 } else {
744 ret.x1 = x2;
745 ret.x2 = x1;
746 }
747
748 if (y1 <= y2) {
749 ret.y1 = y1;
750 ret.y2 = y2;
751 } else {
752 ret.y1 = y2;
753 ret.y2 = y1;
754 }
755
756 return ret;
757}
758
759WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500760weston_transformed_region(int width, int height,
761 enum wl_output_transform transform,
762 int32_t scale,
763 pixman_region32_t *src, pixman_region32_t *dest)
764{
765 pixman_box32_t *src_rects, *dest_rects;
766 int nrects, i;
767
768 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
769 if (src != dest)
770 pixman_region32_copy(dest, src);
771 return;
772 }
773
774 src_rects = pixman_region32_rectangles(src, &nrects);
775 dest_rects = malloc(nrects * sizeof(*dest_rects));
776 if (!dest_rects)
777 return;
778
779 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
780 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
781 } else {
782 for (i = 0; i < nrects; i++) {
783 switch (transform) {
784 default:
785 case WL_OUTPUT_TRANSFORM_NORMAL:
786 dest_rects[i].x1 = src_rects[i].x1;
787 dest_rects[i].y1 = src_rects[i].y1;
788 dest_rects[i].x2 = src_rects[i].x2;
789 dest_rects[i].y2 = src_rects[i].y2;
790 break;
791 case WL_OUTPUT_TRANSFORM_90:
792 dest_rects[i].x1 = height - src_rects[i].y2;
793 dest_rects[i].y1 = src_rects[i].x1;
794 dest_rects[i].x2 = height - src_rects[i].y1;
795 dest_rects[i].y2 = src_rects[i].x2;
796 break;
797 case WL_OUTPUT_TRANSFORM_180:
798 dest_rects[i].x1 = width - src_rects[i].x2;
799 dest_rects[i].y1 = height - src_rects[i].y2;
800 dest_rects[i].x2 = width - src_rects[i].x1;
801 dest_rects[i].y2 = height - src_rects[i].y1;
802 break;
803 case WL_OUTPUT_TRANSFORM_270:
804 dest_rects[i].x1 = src_rects[i].y1;
805 dest_rects[i].y1 = width - src_rects[i].x2;
806 dest_rects[i].x2 = src_rects[i].y2;
807 dest_rects[i].y2 = width - src_rects[i].x1;
808 break;
809 case WL_OUTPUT_TRANSFORM_FLIPPED:
810 dest_rects[i].x1 = width - src_rects[i].x2;
811 dest_rects[i].y1 = src_rects[i].y1;
812 dest_rects[i].x2 = width - src_rects[i].x1;
813 dest_rects[i].y2 = src_rects[i].y2;
814 break;
815 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
816 dest_rects[i].x1 = height - src_rects[i].y2;
817 dest_rects[i].y1 = width - src_rects[i].x2;
818 dest_rects[i].x2 = height - src_rects[i].y1;
819 dest_rects[i].y2 = width - src_rects[i].x1;
820 break;
821 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
822 dest_rects[i].x1 = src_rects[i].x1;
823 dest_rects[i].y1 = height - src_rects[i].y2;
824 dest_rects[i].x2 = src_rects[i].x2;
825 dest_rects[i].y2 = height - src_rects[i].y1;
826 break;
827 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
828 dest_rects[i].x1 = src_rects[i].y1;
829 dest_rects[i].y1 = src_rects[i].x1;
830 dest_rects[i].x2 = src_rects[i].y2;
831 dest_rects[i].y2 = src_rects[i].x2;
832 break;
833 }
834 }
835 }
836
837 if (scale != 1) {
838 for (i = 0; i < nrects; i++) {
839 dest_rects[i].x1 *= scale;
840 dest_rects[i].x2 *= scale;
841 dest_rects[i].y1 *= scale;
842 dest_rects[i].y2 *= scale;
843 }
844 }
845
846 pixman_region32_clear(dest);
847 pixman_region32_init_rects(dest, dest_rects, nrects);
848 free(dest_rects);
849}
850
Jonny Lamb74130762013-11-26 18:19:46 +0100851static void
852scaler_surface_to_buffer(struct weston_surface *surface,
853 float sx, float sy, float *bx, float *by)
854{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200855 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200856 double src_width, src_height;
857 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200858
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200859 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
860 if (vp->surface.width == -1) {
861 *bx = sx;
862 *by = sy;
863 return;
864 }
Jonny Lamb74130762013-11-26 18:19:46 +0100865
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200866 src_x = 0.0;
867 src_y = 0.0;
868 src_width = surface->width_from_buffer;
869 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100870 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200871 src_x = wl_fixed_to_double(vp->buffer.src_x);
872 src_y = wl_fixed_to_double(vp->buffer.src_y);
873 src_width = wl_fixed_to_double(vp->buffer.src_width);
874 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100875 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200876
877 *bx = sx * src_width / surface->width + src_x;
878 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100879}
880
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500881WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200882weston_surface_to_buffer_float(struct weston_surface *surface,
883 float sx, float sy, float *bx, float *by)
884{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
886
Jonny Lamb74130762013-11-26 18:19:46 +0100887 /* first transform coordinates if the scaler is set */
888 scaler_surface_to_buffer(surface, sx, sy, bx, by);
889
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500890 weston_transformed_coord(surface->width_from_buffer,
891 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200892 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100893 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200894}
895
Alexander Larsson4ea95522013-05-22 14:41:37 +0200896WL_EXPORT void
897weston_surface_to_buffer(struct weston_surface *surface,
898 int sx, int sy, int *bx, int *by)
899{
900 float bxf, byf;
901
Jonny Lamb74130762013-11-26 18:19:46 +0100902 weston_surface_to_buffer_float(surface,
903 sx, sy, &bxf, &byf);
904
Alexander Larsson4ea95522013-05-22 14:41:37 +0200905 *bx = floorf(bxf);
906 *by = floorf(byf);
907}
908
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200909WL_EXPORT pixman_box32_t
910weston_surface_to_buffer_rect(struct weston_surface *surface,
911 pixman_box32_t rect)
912{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200913 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100914 float xf, yf;
915
916 /* first transform box coordinates if the scaler is set */
917 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
918 rect.x1 = floorf(xf);
919 rect.y1 = floorf(yf);
920
921 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
922 rect.x2 = floorf(xf);
923 rect.y2 = floorf(yf);
924
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500925 return weston_transformed_rect(surface->width_from_buffer,
926 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200927 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200928 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200929}
930
931WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400933 struct weston_plane *plane)
934{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500935 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400936 return;
937
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 weston_view_damage_below(view);
939 view->plane = plane;
940 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400941}
942
943WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200945{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500946 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200947
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500948 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300949 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500950 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800951 if (view->plane)
952 pixman_region32_union(&view->plane->damage,
953 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500954 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800955 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200956}
957
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400958static void
959weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
960{
961 uint32_t different = es->output_mask ^ mask;
962 uint32_t entered = mask & different;
963 uint32_t left = es->output_mask & different;
964 struct weston_output *output;
965 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500966 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400967
968 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500969 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400970 return;
971 if (different == 0)
972 return;
973
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500974 client = wl_resource_get_client(es->resource);
975
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400976 wl_list_for_each(output, &es->compositor->output_list, link) {
977 if (1 << output->id & different)
978 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500979 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400980 client);
981 if (resource == NULL)
982 continue;
983 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500984 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400985 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500986 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400987 }
988}
989
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200990
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400991static void
992weston_surface_assign_output(struct weston_surface *es)
993{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 struct weston_output *new_output;
995 struct weston_view *view;
996 pixman_region32_t region;
997 uint32_t max, area, mask;
998 pixman_box32_t *e;
999
1000 new_output = NULL;
1001 max = 0;
1002 mask = 0;
1003 pixman_region32_init(&region);
1004 wl_list_for_each(view, &es->views, surface_link) {
1005 if (!view->output)
1006 continue;
1007
1008 pixman_region32_intersect(&region, &view->transform.boundingbox,
1009 &view->output->region);
1010
1011 e = pixman_region32_extents(&region);
1012 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1013
1014 mask |= view->output_mask;
1015
1016 if (area >= max) {
1017 new_output = view->output;
1018 max = area;
1019 }
1020 }
1021 pixman_region32_fini(&region);
1022
1023 es->output = new_output;
1024 weston_surface_update_output_mask(es, mask);
1025}
1026
1027static void
1028weston_view_assign_output(struct weston_view *ev)
1029{
1030 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001031 struct weston_output *output, *new_output;
1032 pixman_region32_t region;
1033 uint32_t max, area, mask;
1034 pixman_box32_t *e;
1035
1036 new_output = NULL;
1037 max = 0;
1038 mask = 0;
1039 pixman_region32_init(&region);
1040 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001042 &output->region);
1043
1044 e = pixman_region32_extents(&region);
1045 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1046
1047 if (area > 0)
1048 mask |= 1 << output->id;
1049
1050 if (area >= max) {
1051 new_output = output;
1052 max = area;
1053 }
1054 }
1055 pixman_region32_fini(&region);
1056
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 ev->output = new_output;
1058 ev->output_mask = mask;
1059
1060 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001061}
1062
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001063static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1065 int32_t width, int32_t height,
1066 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001067{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001068 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1069 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001070 int32_t s[4][2] = {
1071 { sx, sy },
1072 { sx, sy + height },
1073 { sx + width, sy },
1074 { sx + width, sy + height }
1075 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001076 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001077 int i;
1078
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001079 if (width == 0 || height == 0) {
1080 /* avoid rounding empty bbox to 1x1 */
1081 pixman_region32_init(bbox);
1082 return;
1083 }
1084
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001085 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001086 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001088 if (x < min_x)
1089 min_x = x;
1090 if (x > max_x)
1091 max_x = x;
1092 if (y < min_y)
1093 min_y = y;
1094 if (y > max_y)
1095 max_y = y;
1096 }
1097
Pekka Paalanen219b9822012-02-08 15:38:37 +02001098 int_x = floorf(min_x);
1099 int_y = floorf(min_y);
1100 pixman_region32_init_rect(bbox, int_x, int_y,
1101 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001102}
1103
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001104static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001106{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001108
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001109 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 view->geometry.x = roundf(view->geometry.x);
1111 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001112
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001113 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1115 view->transform.position.matrix.d[12] = view->geometry.x;
1116 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001117
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001119
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 view->transform.inverse = view->transform.position.matrix;
1121 view->transform.inverse.d[12] = -view->geometry.x;
1122 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001123
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 pixman_region32_init_rect(&view->transform.boundingbox,
1125 view->geometry.x,
1126 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001127 view->surface->width,
1128 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 if (view->alpha == 1.0) {
1131 pixman_region32_copy(&view->transform.opaque,
1132 &view->surface->opaque);
1133 pixman_region32_translate(&view->transform.opaque,
1134 view->geometry.x,
1135 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001136 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001137}
1138
1139static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001141{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 struct weston_view *parent = view->geometry.parent;
1143 struct weston_matrix *matrix = &view->transform.matrix;
1144 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001145 struct weston_transform *tform;
1146
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001148
1149 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1151 view->transform.position.matrix.d[12] = view->geometry.x;
1152 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001153
1154 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001156 weston_matrix_multiply(matrix, &tform->matrix);
1157
Pekka Paalanen483243f2013-03-08 14:56:50 +02001158 if (parent)
1159 weston_matrix_multiply(matrix, &parent->transform.matrix);
1160
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001161 if (weston_matrix_invert(inverse, matrix) < 0) {
1162 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 weston_log("error: weston_view %p"
1164 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001165 return -1;
1166 }
1167
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001168 view_compute_bbox(view, 0, 0,
1169 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001171
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001172 return 0;
1173}
1174
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001175static struct weston_layer *
1176get_view_layer(struct weston_view *view)
1177{
1178 if (view->parent_view)
1179 return get_view_layer(view->parent_view);
1180 return view->layer_link.layer;
1181}
1182
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001183WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001185{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001187 struct weston_layer *layer;
1188 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001189
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001191 return;
1192
Pekka Paalanen483243f2013-03-08 14:56:50 +02001193 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001195
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001197
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 pixman_region32_fini(&view->transform.boundingbox);
1201 pixman_region32_fini(&view->transform.opaque);
1202 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001203
Pekka Paalanencd403622012-01-25 13:37:39 +02001204 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 if (view->geometry.transformation_list.next ==
1206 &view->transform.position.link &&
1207 view->geometry.transformation_list.prev ==
1208 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001209 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001211 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 if (weston_view_update_transform_enable(view) < 0)
1213 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001214 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001215
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001216 layer = get_view_layer(view);
1217 if (layer) {
1218 pixman_region32_init_with_extents(&mask, &layer->mask);
1219 pixman_region32_intersect(&view->transform.masked_boundingbox,
1220 &view->transform.boundingbox, &mask);
1221 pixman_region32_intersect(&view->transform.masked_opaque,
1222 &view->transform.opaque, &mask);
1223 pixman_region32_fini(&mask);
1224 }
1225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 wl_signal_emit(&view->surface->compositor->transform_signal,
1231 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001232}
1233
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001234WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001236{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001238
1239 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240 * The invariant: if view->geometry.dirty, then all views
1241 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001242 * Corollary: if not parent->geometry.dirty, then all ancestors
1243 * are not dirty.
1244 */
1245
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001247 return;
1248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001250
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001252 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001254}
1255
1256WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257weston_view_to_global_fixed(struct weston_view *view,
1258 wl_fixed_t vx, wl_fixed_t vy,
1259 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001260{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001261 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001262
Jason Ekstranda7af7042013-10-12 22:38:11 -05001263 weston_view_to_global_float(view,
1264 wl_fixed_to_double(vx),
1265 wl_fixed_to_double(vy),
1266 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001267 *x = wl_fixed_from_double(xf);
1268 *y = wl_fixed_from_double(yf);
1269}
1270
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001271WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272weston_view_from_global_float(struct weston_view *view,
1273 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001274{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001276 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001279
1280 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001281 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001283 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 *vx = 0;
1285 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001286 return;
1287 }
1288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 *vx = v.f[0] / v.f[3];
1290 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001291 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 *vx = x - view->geometry.x;
1293 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001294 }
1295}
1296
1297WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298weston_view_from_global_fixed(struct weston_view *view,
1299 wl_fixed_t x, wl_fixed_t y,
1300 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001301{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 weston_view_from_global_float(view,
1305 wl_fixed_to_double(x),
1306 wl_fixed_to_double(y),
1307 &vxf, &vyf);
1308 *vx = wl_fixed_from_double(vxf);
1309 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001310}
1311
1312WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001313weston_view_from_global(struct weston_view *view,
1314 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001315{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001317
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1319 *vx = floorf(vxf);
1320 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001321}
1322
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001323WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001324weston_surface_schedule_repaint(struct weston_surface *surface)
1325{
1326 struct weston_output *output;
1327
1328 wl_list_for_each(output, &surface->compositor->output_list, link)
1329 if (surface->output_mask & (1 << output->id))
1330 weston_output_schedule_repaint(output);
1331}
1332
1333WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334weston_view_schedule_repaint(struct weston_view *view)
1335{
1336 struct weston_output *output;
1337
1338 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1339 if (view->output_mask & (1 << output->id))
1340 weston_output_schedule_repaint(output);
1341}
1342
1343WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001344weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001345{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001346 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 0, 0, surface->width,
1348 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001349
Kristian Høgsberg98238702012-08-03 16:29:12 -04001350 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001351}
1352
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001353WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001355{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001356 if (view->geometry.x == x && view->geometry.y == y)
1357 return;
1358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 view->geometry.x = x;
1360 view->geometry.y = y;
1361 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001362}
1363
Pekka Paalanen483243f2013-03-08 14:56:50 +02001364static void
1365transform_parent_handle_parent_destroy(struct wl_listener *listener,
1366 void *data)
1367{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368 struct weston_view *view =
1369 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001370 geometry.parent_destroy_listener);
1371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001373}
1374
1375WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001376weston_view_set_transform_parent(struct weston_view *view,
1377 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001378{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 if (view->geometry.parent) {
1380 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1381 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001382 }
1383
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001387 transform_parent_handle_parent_destroy;
1388 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001389 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001391 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001393 }
1394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_view_geometry_dirty(view);
1396}
1397
Derek Foreman280e7dd2014-10-03 13:13:42 -05001398WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399weston_view_is_mapped(struct weston_view *view)
1400{
1401 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001402 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001404 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001405}
1406
Derek Foreman280e7dd2014-10-03 13:13:42 -05001407WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001408weston_surface_is_mapped(struct weston_surface *surface)
1409{
1410 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001411 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001412 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001413 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001414}
1415
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001416static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001417surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001418{
1419 struct weston_view *view;
1420
1421 if (surface->width == width && surface->height == height)
1422 return;
1423
1424 surface->width = width;
1425 surface->height = height;
1426
1427 wl_list_for_each(view, &surface->views, surface_link)
1428 weston_view_geometry_dirty(view);
1429}
1430
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001431WL_EXPORT void
1432weston_surface_set_size(struct weston_surface *surface,
1433 int32_t width, int32_t height)
1434{
1435 assert(!surface->resource);
1436 surface_set_size(surface, width, height);
1437}
1438
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001439static int
1440fixed_round_up_to_int(wl_fixed_t f)
1441{
1442 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1443}
1444
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001445static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001446weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001447{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001448 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001449 int32_t width, height;
1450
1451 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001452 surface->width_from_buffer = 0;
1453 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001454 return;
1455 }
1456
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001457 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001458 case WL_OUTPUT_TRANSFORM_90:
1459 case WL_OUTPUT_TRANSFORM_270:
1460 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1461 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001462 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1463 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001464 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001465 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001466 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1467 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001468 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001469 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001470
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001471 surface->width_from_buffer = width;
1472 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001473}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001474
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001475static void
1476weston_surface_update_size(struct weston_surface *surface)
1477{
1478 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1479 int32_t width, height;
1480
1481 width = surface->width_from_buffer;
1482 height = surface->height_from_buffer;
1483
1484 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001485 surface_set_size(surface,
1486 vp->surface.width, vp->surface.height);
1487 return;
1488 }
1489
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001490 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001491 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1492 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1493
1494 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001495 return;
1496 }
1497
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001498 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001499}
1500
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001501WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001502weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001503{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001504 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001505
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001506 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001507
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001508 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001509}
1510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511WL_EXPORT struct weston_view *
1512weston_compositor_pick_view(struct weston_compositor *compositor,
1513 wl_fixed_t x, wl_fixed_t y,
1514 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001515{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001517 int ix = wl_fixed_to_int(x);
1518 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 wl_list_for_each(view, &compositor->view_list, link) {
1521 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001522 if (pixman_region32_contains_point(
1523 &view->transform.masked_boundingbox,
1524 ix, iy, NULL) &&
1525 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001526 wl_fixed_to_int(*vx),
1527 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001528 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001529 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001530 }
1531
1532 return NULL;
1533}
1534
1535static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001537{
Daniel Stone37816df2012-05-16 18:45:18 +01001538 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001539
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001540 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001541 return;
1542
Daniel Stone37816df2012-05-16 18:45:18 +01001543 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001544 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001545}
1546
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001547WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001549{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001550 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001551
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 if (!weston_view_is_mapped(view))
1553 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 weston_view_damage_below(view);
1556 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001557 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001558 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 wl_list_remove(&view->link);
1560 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 view->output_mask = 0;
1562 weston_surface_assign_output(view->surface);
1563
1564 if (weston_surface_is_mapped(view->surface))
1565 return;
1566
1567 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1568 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001569 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001571 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001572 NULL,
1573 wl_fixed_from_int(0),
1574 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001576 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001577 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001579
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580WL_EXPORT void
1581weston_surface_unmap(struct weston_surface *surface)
1582{
1583 struct weston_view *view;
1584
1585 wl_list_for_each(view, &surface->views, surface_link)
1586 weston_view_unmap(view);
1587 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001588}
1589
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001590static void
1591weston_surface_reset_pending_buffer(struct weston_surface *surface)
1592{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001593 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001594 surface->pending.sx = 0;
1595 surface->pending.sy = 0;
1596 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001597 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001598}
1599
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600WL_EXPORT void
1601weston_view_destroy(struct weston_view *view)
1602{
1603 wl_signal_emit(&view->destroy_signal, view);
1604
1605 assert(wl_list_empty(&view->geometry.child_list));
1606
1607 if (weston_view_is_mapped(view)) {
1608 weston_view_unmap(view);
1609 weston_compositor_build_view_list(view->surface->compositor);
1610 }
1611
1612 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001613 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614
1615 pixman_region32_fini(&view->clip);
1616 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001617 pixman_region32_fini(&view->transform.masked_boundingbox);
1618 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619
1620 weston_view_set_transform_parent(view, NULL);
1621
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622 wl_list_remove(&view->surface_link);
1623
1624 free(view);
1625}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001626
1627WL_EXPORT void
1628weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001629{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001630 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001632
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001633 if (--surface->ref_count > 0)
1634 return;
1635
1636 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1637
Pekka Paalanene67858b2013-04-25 13:57:42 +03001638 assert(wl_list_empty(&surface->subsurface_list_pending));
1639 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001640
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1642 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001643
Jason Ekstrand7b982072014-05-20 14:33:03 -05001644 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001645
Pekka Paalanende685b82012-12-04 15:58:12 +02001646 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001647
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001648 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001649 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001650 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001651
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001652 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001653 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001654
Pekka Paalanen133e4392014-09-23 22:08:46 -04001655 weston_presentation_feedback_discard_list(&surface->feedback_list);
1656
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001657 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001658}
1659
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001660static void
1661destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001662{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001663 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001664
Giulio Camuffo0d379742013-11-15 22:06:15 +01001665 /* Set the resource to NULL, since we don't want to leave a
1666 * dangling pointer if the surface was refcounted and survives
1667 * the weston_surface_destroy() call. */
1668 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001669 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001670}
1671
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001672static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001673weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1674{
1675 struct weston_buffer *buffer =
1676 container_of(listener, struct weston_buffer, destroy_listener);
1677
1678 wl_signal_emit(&buffer->destroy_signal, buffer);
1679 free(buffer);
1680}
1681
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001682WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001683weston_buffer_from_resource(struct wl_resource *resource)
1684{
1685 struct weston_buffer *buffer;
1686 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001687
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001688 listener = wl_resource_get_destroy_listener(resource,
1689 weston_buffer_destroy_handler);
1690
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001691 if (listener)
1692 return container_of(listener, struct weston_buffer,
1693 destroy_listener);
1694
1695 buffer = zalloc(sizeof *buffer);
1696 if (buffer == NULL)
1697 return NULL;
1698
1699 buffer->resource = resource;
1700 wl_signal_init(&buffer->destroy_signal);
1701 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001702 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001703 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001704
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001705 return buffer;
1706}
1707
1708static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001709weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1710 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001711{
Pekka Paalanende685b82012-12-04 15:58:12 +02001712 struct weston_buffer_reference *ref =
1713 container_of(listener, struct weston_buffer_reference,
1714 destroy_listener);
1715
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001716 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001717 ref->buffer = NULL;
1718}
1719
1720WL_EXPORT void
1721weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001722 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001723{
1724 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001725 ref->buffer->busy_count--;
1726 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001727 assert(wl_resource_get_client(ref->buffer->resource));
1728 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001729 WL_BUFFER_RELEASE);
1730 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001731 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001732 }
1733
Pekka Paalanende685b82012-12-04 15:58:12 +02001734 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001735 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001736 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001737 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001738 }
1739
Pekka Paalanende685b82012-12-04 15:58:12 +02001740 ref->buffer = buffer;
1741 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1742}
1743
1744static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001745weston_surface_attach(struct weston_surface *surface,
1746 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001747{
1748 weston_buffer_reference(&surface->buffer_ref, buffer);
1749
Pekka Paalanena6421c42012-12-04 15:58:10 +02001750 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001751 if (weston_surface_is_mapped(surface))
1752 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001753 }
1754
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001755 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001756
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001757 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001758 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001759}
1760
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001761WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001762weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001763{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001765
1766 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001767 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001768}
1769
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001770WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001771weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001774
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001775 pixman_region32_union(&compositor->primary_plane.damage,
1776 &compositor->primary_plane.damage,
1777 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001778 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001779}
1780
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001781static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001783{
Pekka Paalanende685b82012-12-04 15:58:12 +02001784 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001785 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001786 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001787
Pekka Paalanenb5026542014-11-12 15:09:24 +02001788 if (weston_timeline_enabled_ &&
1789 pixman_region32_not_empty(&surface->damage))
1790 TL_POINT("core_flush_damage", TLP_SURFACE(surface),
1791 TLP_OUTPUT(surface->output), TLP_END);
1792
Jason Ekstrandef540082014-06-26 10:37:36 -07001793 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794}
1795
1796static void
1797view_accumulate_damage(struct weston_view *view,
1798 pixman_region32_t *opaque)
1799{
1800 pixman_region32_t damage;
1801
1802 pixman_region32_init(&damage);
1803 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001804 pixman_box32_t *extents;
1805
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 extents = pixman_region32_extents(&view->surface->damage);
1807 view_compute_bbox(view, extents->x1, extents->y1,
1808 extents->x2 - extents->x1,
1809 extents->y2 - extents->y1,
1810 &damage);
1811 pixman_region32_translate(&damage,
1812 -view->plane->x,
1813 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001814 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 pixman_region32_copy(&damage, &view->surface->damage);
1816 pixman_region32_translate(&damage,
1817 view->geometry.x - view->plane->x,
1818 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001819 }
1820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 pixman_region32_subtract(&damage, &damage, opaque);
1822 pixman_region32_union(&view->plane->damage,
1823 &view->plane->damage, &damage);
1824 pixman_region32_fini(&damage);
1825 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001826 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001827}
1828
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001829static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001830compositor_accumulate_damage(struct weston_compositor *ec)
1831{
1832 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001834 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001835
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001836 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001837
1838 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001839 pixman_region32_copy(&plane->clip, &clip);
1840
1841 pixman_region32_init(&opaque);
1842
Jason Ekstranda7af7042013-10-12 22:38:11 -05001843 wl_list_for_each(ev, &ec->view_list, link) {
1844 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001845 continue;
1846
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001848 }
1849
1850 pixman_region32_union(&clip, &clip, &opaque);
1851 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001852 }
1853
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001854 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001855
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 wl_list_for_each(ev, &ec->view_list, link)
1857 ev->surface->touched = 0;
1858
1859 wl_list_for_each(ev, &ec->view_list, link) {
1860 if (ev->surface->touched)
1861 continue;
1862 ev->surface->touched = 1;
1863
1864 surface_flush_damage(ev->surface);
1865
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001866 /* Both the renderer and the backend have seen the buffer
1867 * by now. If renderer needs the buffer, it has its own
1868 * reference set. If the backend wants to keep the buffer
1869 * around for migrating the surface into a non-primary plane
1870 * later, keep_buffer is true. Otherwise, drop the core
1871 * reference now, and allow early buffer release. This enables
1872 * clients to use single-buffering.
1873 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874 if (!ev->surface->keep_buffer)
1875 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001876 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001877}
1878
1879static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881{
1882 struct weston_subsurface *sub;
1883
Pekka Paalanene67858b2013-04-25 13:57:42 +03001884 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001885 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001886 continue;
1887
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1889 wl_list_init(&sub->surface->views);
1890
1891 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001892 }
1893}
1894
1895static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001897{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 struct weston_subsurface *sub;
1899 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001900
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1902 if (sub->surface == surface)
1903 continue;
1904
George Kiagiadakised04d382014-06-13 18:10:26 +02001905 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1906 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001907 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001908 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909
1910 surface_free_unused_subsurface_views(sub->surface);
1911 }
1912}
1913
1914static void
1915view_list_add_subsurface_view(struct weston_compositor *compositor,
1916 struct weston_subsurface *sub,
1917 struct weston_view *parent)
1918{
1919 struct weston_subsurface *child;
1920 struct weston_view *view = NULL, *iv;
1921
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001922 if (!weston_surface_is_mapped(sub->surface))
1923 return;
1924
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1926 if (iv->geometry.parent == parent) {
1927 view = iv;
1928 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001929 }
1930 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931
1932 if (view) {
1933 /* Put it back in the surface's list of views */
1934 wl_list_remove(&view->surface_link);
1935 wl_list_insert(&sub->surface->views, &view->surface_link);
1936 } else {
1937 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001938 weston_view_set_position(view,
1939 sub->position.x,
1940 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001941 weston_view_set_transform_parent(view, parent);
1942 }
1943
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001944 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946
Pekka Paalanenb188e912013-11-19 14:03:35 +02001947 if (wl_list_empty(&sub->surface->subsurface_list)) {
1948 wl_list_insert(compositor->view_list.prev, &view->link);
1949 return;
1950 }
1951
1952 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1953 if (child->surface == sub->surface)
1954 wl_list_insert(compositor->view_list.prev, &view->link);
1955 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001957 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958}
1959
1960static void
1961view_list_add(struct weston_compositor *compositor,
1962 struct weston_view *view)
1963{
1964 struct weston_subsurface *sub;
1965
1966 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001967
Pekka Paalanenb188e912013-11-19 14:03:35 +02001968 if (wl_list_empty(&view->surface->subsurface_list)) {
1969 wl_list_insert(compositor->view_list.prev, &view->link);
1970 return;
1971 }
1972
1973 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1974 if (sub->surface == view->surface)
1975 wl_list_insert(compositor->view_list.prev, &view->link);
1976 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001978 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979}
1980
1981static void
1982weston_compositor_build_view_list(struct weston_compositor *compositor)
1983{
1984 struct weston_view *view;
1985 struct weston_layer *layer;
1986
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 surface_stash_subsurface_views(view->surface);
1990
1991 wl_list_init(&compositor->view_list);
1992 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001993 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001994 view_list_add(compositor, view);
1995 }
1996 }
1997
1998 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001999 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002001}
2002
David Herrmann1edf44c2013-10-22 17:11:26 +02002003static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002004weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002005{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002006 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002008 struct weston_animation *animation, *next;
2009 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002010 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002011 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002012 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002013
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002014 if (output->destroying)
2015 return 0;
2016
Pekka Paalanenb5026542014-11-12 15:09:24 +02002017 TL_POINT("core_repaint_begin", TLP_OUTPUT(output), TLP_END);
2018
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002019 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002021
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002022 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002023 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002024 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 wl_list_for_each(ev, &ec->view_list, link)
2026 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002027
Pekka Paalanene67858b2013-04-25 13:57:42 +03002028 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002029 wl_list_for_each(ev, &ec->view_list, link) {
2030 /* Note: This operation is safe to do multiple times on the
2031 * same surface.
2032 */
2033 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002034 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002035 &ev->surface->frame_callback_list);
2036 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002037
2038 wl_list_insert_list(&output->feedback_list,
2039 &ev->surface->feedback_list);
2040 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002041 }
2042 }
2043
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002044 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002045
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002046 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002047 pixman_region32_intersect(&output_damage,
2048 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002049 pixman_region32_subtract(&output_damage,
2050 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002051
Scott Moreauccbf29d2012-02-22 14:21:41 -07002052 if (output->dirty)
2053 weston_output_update_matrix(output);
2054
David Herrmann1edf44c2013-10-22 17:11:26 +02002055 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002056
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002057 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002058
Kristian Høgsbergef044142011-06-21 15:02:12 -04002059 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002060
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002061 weston_compositor_repick(ec);
2062 wl_event_loop_dispatch(ec->input_loop, 0);
2063
Jonas Ådahldb773762012-06-13 00:01:21 +02002064 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002065 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002066 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002067 }
2068
Scott Moreaud64cf212012-06-08 19:40:54 -06002069 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002070 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002071 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002072 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002073
Pekka Paalanenb5026542014-11-12 15:09:24 +02002074 TL_POINT("core_repaint_posted", TLP_OUTPUT(output), TLP_END);
2075
David Herrmann1edf44c2013-10-22 17:11:26 +02002076 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002077}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002078
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002079static int
2080weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002081{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002082 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002083
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002084 wl_event_loop_dispatch(compositor->input_loop, 0);
2085
2086 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002087}
2088
2089WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002090weston_output_finish_frame(struct weston_output *output,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002091 const struct timespec *stamp,
2092 uint32_t presented_flags)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002093{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002094 struct weston_compositor *compositor = output->compositor;
2095 struct wl_event_loop *loop =
2096 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002097 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002098 uint32_t refresh_nsec;
2099
Pekka Paalanenb5026542014-11-12 15:09:24 +02002100 TL_POINT("core_repaint_finished", TLP_OUTPUT(output),
2101 TLP_VBLANK(stamp), TLP_END);
2102
Pekka Paalanen133e4392014-09-23 22:08:46 -04002103 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2104 weston_presentation_feedback_present_list(&output->feedback_list,
2105 output, refresh_nsec, stamp,
Pekka Paalanen363aa7b2014-12-17 16:20:40 +02002106 output->msc,
2107 presented_flags);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002108
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002109 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002110
2111 if (output->repaint_needed &&
2112 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2113 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002114 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002115 if (!r)
2116 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002117 }
2118
2119 output->repaint_scheduled = 0;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002120 TL_POINT("core_repaint_exit_loop", TLP_OUTPUT(output), TLP_END);
2121
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002122 if (compositor->input_loop_source)
2123 return;
2124
2125 fd = wl_event_loop_get_fd(compositor->input_loop);
2126 compositor->input_loop_source =
2127 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2128 weston_compositor_read_input, compositor);
2129}
2130
2131static void
2132idle_repaint(void *data)
2133{
2134 struct weston_output *output = data;
2135
Jonas Ådahle5a12252013-04-05 23:07:11 +02002136 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002137}
2138
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002139WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002140weston_layer_entry_insert(struct weston_layer_entry *list,
2141 struct weston_layer_entry *entry)
2142{
2143 wl_list_insert(&list->link, &entry->link);
2144 entry->layer = list->layer;
2145}
2146
2147WL_EXPORT void
2148weston_layer_entry_remove(struct weston_layer_entry *entry)
2149{
2150 wl_list_remove(&entry->link);
2151 wl_list_init(&entry->link);
2152 entry->layer = NULL;
2153}
2154
2155WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002156weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2157{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002158 wl_list_init(&layer->view_list.link);
2159 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002160 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002161 if (below != NULL)
2162 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002163}
2164
2165WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002166weston_layer_set_mask(struct weston_layer *layer,
2167 int x, int y, int width, int height)
2168{
2169 struct weston_view *view;
2170
2171 layer->mask.x1 = x;
2172 layer->mask.x2 = x + width;
2173 layer->mask.y1 = y;
2174 layer->mask.y2 = y + height;
2175
2176 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2177 weston_view_geometry_dirty(view);
2178 }
2179}
2180
2181WL_EXPORT void
2182weston_layer_set_mask_infinite(struct weston_layer *layer)
2183{
2184 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2185 UINT32_MAX, UINT32_MAX);
2186}
2187
2188WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002189weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002190{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002191 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002192 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002193
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002194 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2195 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002196 return;
2197
Pekka Paalanenb5026542014-11-12 15:09:24 +02002198 if (!output->repaint_needed)
2199 TL_POINT("core_repaint_req", TLP_OUTPUT(output), TLP_END);
2200
Kristian Høgsbergef044142011-06-21 15:02:12 -04002201 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002202 output->repaint_needed = 1;
2203 if (output->repaint_scheduled)
2204 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002205
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002206 wl_event_loop_add_idle(loop, idle_repaint, output);
2207 output->repaint_scheduled = 1;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002208 TL_POINT("core_repaint_enter_loop", TLP_OUTPUT(output), TLP_END);
2209
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002210
2211 if (compositor->input_loop_source) {
2212 wl_event_source_remove(compositor->input_loop_source);
2213 compositor->input_loop_source = NULL;
2214 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002215}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002216
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002217WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002218weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2219{
2220 struct weston_output *output;
2221
2222 wl_list_for_each(output, &compositor->output_list, link)
2223 weston_output_schedule_repaint(output);
2224}
2225
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002226static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002227surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002228{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002229 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002230}
2231
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002232static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002233surface_attach(struct wl_client *client,
2234 struct wl_resource *resource,
2235 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2236{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002237 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002238 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002239
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002240 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002241 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002242 if (buffer == NULL) {
2243 wl_client_post_no_memory(client);
2244 return;
2245 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002246 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002247
Pekka Paalanende685b82012-12-04 15:58:12 +02002248 /* Attach, attach, without commit in between does not send
2249 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002250 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002251
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002252 surface->pending.sx = sx;
2253 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002254 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002255}
2256
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002257static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002258surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002259 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002260 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002261{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002262 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002263
Pekka Paalanen8e159182012-10-10 12:49:25 +03002264 pixman_region32_union_rect(&surface->pending.damage,
2265 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002266 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002267}
2268
Kristian Høgsberg33418202011-08-16 23:01:28 -04002269static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002270destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002271{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002272 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002273
2274 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002275 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002276}
2277
2278static void
2279surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002280 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002281{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002282 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002283 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002284
2285 cb = malloc(sizeof *cb);
2286 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002287 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002288 return;
2289 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002290
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002291 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2292 callback);
2293 if (cb->resource == NULL) {
2294 free(cb);
2295 wl_resource_post_no_memory(resource);
2296 return;
2297 }
2298
Jason Ekstranda85118c2013-06-27 20:17:02 -05002299 wl_resource_set_implementation(cb->resource, NULL, cb,
2300 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002301
Pekka Paalanenbc106382012-10-10 12:49:31 +03002302 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002303}
2304
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002305static void
2306surface_set_opaque_region(struct wl_client *client,
2307 struct wl_resource *resource,
2308 struct wl_resource *region_resource)
2309{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002310 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002311 struct weston_region *region;
2312
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002313 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002314 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002315 pixman_region32_copy(&surface->pending.opaque,
2316 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002317 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002318 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002319 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002320}
2321
2322static void
2323surface_set_input_region(struct wl_client *client,
2324 struct wl_resource *resource,
2325 struct wl_resource *region_resource)
2326{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002327 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002328 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002329
2330 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002331 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002332 pixman_region32_copy(&surface->pending.input,
2333 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002334 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002335 pixman_region32_fini(&surface->pending.input);
2336 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002337 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002338}
2339
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002340static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002341weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002342{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002343 struct weston_subsurface *sub;
2344
2345 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2346 parent_link_pending) {
2347 wl_list_remove(&sub->parent_link);
2348 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2349 }
2350}
2351
2352static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002353weston_surface_commit_state(struct weston_surface *surface,
2354 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002355{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002356 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002357 pixman_region32_t opaque;
2358
Alexander Larsson4ea95522013-05-22 14:41:37 +02002359 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002360 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002361 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002362 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002363
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002364 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002365 if (state->newly_attached)
2366 weston_surface_attach(surface, state->buffer);
2367 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002368
Jason Ekstrand7b982072014-05-20 14:33:03 -05002369 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002370 weston_surface_update_size(surface);
2371 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002372 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002373 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002374
Jason Ekstrand7b982072014-05-20 14:33:03 -05002375 state->sx = 0;
2376 state->sy = 0;
2377 state->newly_attached = 0;
2378 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002379
2380 /* wl_surface.damage */
Pekka Paalanenb5026542014-11-12 15:09:24 +02002381 if (weston_timeline_enabled_ &&
2382 pixman_region32_not_empty(&state->damage))
2383 TL_POINT("core_commit_damage", TLP_SURFACE(surface), TLP_END);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002384 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002385 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002386 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002387 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002388 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002389
2390 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002391 pixman_region32_init(&opaque);
2392 pixman_region32_intersect_rect(&opaque, &state->opaque,
2393 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002394
2395 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2396 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397 wl_list_for_each(view, &surface->views, surface_link)
2398 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002399 }
2400
2401 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002402
2403 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002404 pixman_region32_intersect_rect(&surface->input, &state->input,
2405 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002406
Pekka Paalanenbc106382012-10-10 12:49:31 +03002407 /* wl_surface.frame */
2408 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002409 &state->frame_callback_list);
2410 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002411
2412 /* XXX:
2413 * What should happen with a feedback request, if there
2414 * is no wl_buffer attached for this commit?
2415 */
2416
2417 /* presentation.feedback */
2418 wl_list_insert_list(&surface->feedback_list,
2419 &state->feedback_list);
2420 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002421}
2422
2423static void
2424weston_surface_commit(struct weston_surface *surface)
2425{
2426 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002427
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428 weston_surface_commit_subsurface_order(surface);
2429
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002430 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002431}
2432
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002433static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002434weston_subsurface_commit(struct weston_subsurface *sub);
2435
2436static void
2437weston_subsurface_parent_commit(struct weston_subsurface *sub,
2438 int parent_is_synchronized);
2439
2440static void
2441surface_commit(struct wl_client *client, struct wl_resource *resource)
2442{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002443 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002444 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2445
2446 if (sub) {
2447 weston_subsurface_commit(sub);
2448 return;
2449 }
2450
2451 weston_surface_commit(surface);
2452
2453 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2454 if (sub->surface != surface)
2455 weston_subsurface_parent_commit(sub, 0);
2456 }
2457}
2458
2459static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002460surface_set_buffer_transform(struct wl_client *client,
2461 struct wl_resource *resource, int transform)
2462{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002463 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002464
Jonny Lamba55f1392014-05-30 12:07:15 +02002465 /* if wl_output.transform grows more members this will need to be updated. */
2466 if (transform < 0 ||
2467 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2468 wl_resource_post_error(resource,
2469 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2470 "buffer transform must be a valid transform "
2471 "('%d' specified)", transform);
2472 return;
2473 }
2474
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002475 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002476 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002477}
2478
Alexander Larsson4ea95522013-05-22 14:41:37 +02002479static void
2480surface_set_buffer_scale(struct wl_client *client,
2481 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002482 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002483{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002484 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002485
Jonny Lamba55f1392014-05-30 12:07:15 +02002486 if (scale < 1) {
2487 wl_resource_post_error(resource,
2488 WL_SURFACE_ERROR_INVALID_SCALE,
2489 "buffer scale must be at least one "
2490 "('%d' specified)", scale);
2491 return;
2492 }
2493
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002494 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002495 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002496}
2497
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002498static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002499 surface_destroy,
2500 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002501 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002502 surface_frame,
2503 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002504 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002505 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002506 surface_set_buffer_transform,
2507 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002508};
2509
2510static void
2511compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002512 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002513{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002514 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002515 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002516
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002517 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002518 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002519 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002520 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002521 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002522
Jason Ekstranda85118c2013-06-27 20:17:02 -05002523 surface->resource =
2524 wl_resource_create(client, &wl_surface_interface,
2525 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002526 if (surface->resource == NULL) {
2527 weston_surface_destroy(surface);
2528 wl_resource_post_no_memory(resource);
2529 return;
2530 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002531 wl_resource_set_implementation(surface->resource, &surface_interface,
2532 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002533
2534 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002535}
2536
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002537static void
2538destroy_region(struct wl_resource *resource)
2539{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002540 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002541
2542 pixman_region32_fini(&region->region);
2543 free(region);
2544}
2545
2546static void
2547region_destroy(struct wl_client *client, struct wl_resource *resource)
2548{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002549 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002550}
2551
2552static void
2553region_add(struct wl_client *client, struct wl_resource *resource,
2554 int32_t x, int32_t y, int32_t width, int32_t height)
2555{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002556 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002557
2558 pixman_region32_union_rect(&region->region, &region->region,
2559 x, y, width, height);
2560}
2561
2562static void
2563region_subtract(struct wl_client *client, struct wl_resource *resource,
2564 int32_t x, int32_t y, int32_t width, int32_t height)
2565{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002566 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002567 pixman_region32_t rect;
2568
2569 pixman_region32_init_rect(&rect, x, y, width, height);
2570 pixman_region32_subtract(&region->region, &region->region, &rect);
2571 pixman_region32_fini(&rect);
2572}
2573
2574static const struct wl_region_interface region_interface = {
2575 region_destroy,
2576 region_add,
2577 region_subtract
2578};
2579
2580static void
2581compositor_create_region(struct wl_client *client,
2582 struct wl_resource *resource, uint32_t id)
2583{
2584 struct weston_region *region;
2585
2586 region = malloc(sizeof *region);
2587 if (region == NULL) {
2588 wl_resource_post_no_memory(resource);
2589 return;
2590 }
2591
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002592 pixman_region32_init(&region->region);
2593
Jason Ekstranda85118c2013-06-27 20:17:02 -05002594 region->resource =
2595 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002596 if (region->resource == NULL) {
2597 free(region);
2598 wl_resource_post_no_memory(resource);
2599 return;
2600 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002601 wl_resource_set_implementation(region->resource, &region_interface,
2602 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002603}
2604
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002605static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002606 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002607 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002608};
2609
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002610static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002611weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2612{
2613 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002614
Jason Ekstrand7b982072014-05-20 14:33:03 -05002615 weston_surface_commit_state(surface, &sub->cached);
2616 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002617
2618 weston_surface_commit_subsurface_order(surface);
2619
2620 weston_surface_schedule_repaint(surface);
2621
Jason Ekstrand7b982072014-05-20 14:33:03 -05002622 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002623}
2624
2625static void
2626weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2627{
2628 struct weston_surface *surface = sub->surface;
2629
2630 /*
2631 * If this commit would cause the surface to move by the
2632 * attach(dx, dy) parameters, the old damage region must be
2633 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002634 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002635 */
2636 pixman_region32_translate(&sub->cached.damage,
2637 -surface->pending.sx, -surface->pending.sy);
2638 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2639 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002640 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641
2642 if (surface->pending.newly_attached) {
2643 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002644 weston_surface_state_set_buffer(&sub->cached,
2645 surface->pending.buffer);
2646 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002647 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002648 weston_presentation_feedback_discard_list(
2649 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002650 }
2651 sub->cached.sx += surface->pending.sx;
2652 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002653
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002654 sub->cached.buffer_viewport.changed |=
2655 surface->pending.buffer_viewport.changed;
2656 sub->cached.buffer_viewport.buffer =
2657 surface->pending.buffer_viewport.buffer;
2658 sub->cached.buffer_viewport.surface =
2659 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002661 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662
2663 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2664
2665 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2666
2667 wl_list_insert_list(&sub->cached.frame_callback_list,
2668 &surface->pending.frame_callback_list);
2669 wl_list_init(&surface->pending.frame_callback_list);
2670
Pekka Paalanen133e4392014-09-23 22:08:46 -04002671 wl_list_insert_list(&sub->cached.feedback_list,
2672 &surface->pending.feedback_list);
2673 wl_list_init(&surface->pending.feedback_list);
2674
Jason Ekstrand7b982072014-05-20 14:33:03 -05002675 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676}
2677
Derek Foreman280e7dd2014-10-03 13:13:42 -05002678static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002679weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2680{
2681 while (sub) {
2682 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002683 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002684
2685 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002686 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002687
2688 sub = weston_surface_to_subsurface(sub->parent);
2689 }
2690
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002691 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692}
2693
2694static void
2695weston_subsurface_commit(struct weston_subsurface *sub)
2696{
2697 struct weston_surface *surface = sub->surface;
2698 struct weston_subsurface *tmp;
2699
2700 /* Recursive check for effectively synchronized. */
2701 if (weston_subsurface_is_synchronized(sub)) {
2702 weston_subsurface_commit_to_cache(sub);
2703 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002704 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002705 /* flush accumulated state from cache */
2706 weston_subsurface_commit_to_cache(sub);
2707 weston_subsurface_commit_from_cache(sub);
2708 } else {
2709 weston_surface_commit(surface);
2710 }
2711
2712 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2713 if (tmp->surface != surface)
2714 weston_subsurface_parent_commit(tmp, 0);
2715 }
2716 }
2717}
2718
2719static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002720weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002721{
2722 struct weston_surface *surface = sub->surface;
2723 struct weston_subsurface *tmp;
2724
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725 /* From now on, commit_from_cache the whole sub-tree, regardless of
2726 * the synchronized mode of each child. This sub-surface or some
2727 * of its ancestors were synchronized, so we are synchronized
2728 * all the way down.
2729 */
2730
Jason Ekstrand7b982072014-05-20 14:33:03 -05002731 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732 weston_subsurface_commit_from_cache(sub);
2733
2734 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2735 if (tmp->surface != surface)
2736 weston_subsurface_parent_commit(tmp, 1);
2737 }
2738}
2739
2740static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002741weston_subsurface_parent_commit(struct weston_subsurface *sub,
2742 int parent_is_synchronized)
2743{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002744 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002745 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002746 wl_list_for_each(view, &sub->surface->views, surface_link)
2747 weston_view_set_position(view,
2748 sub->position.x,
2749 sub->position.y);
2750
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002751 sub->position.set = 0;
2752 }
2753
2754 if (parent_is_synchronized || sub->synchronized)
2755 weston_subsurface_synchronized_commit(sub);
2756}
2757
Pekka Paalanen8274d902014-08-06 19:36:51 +03002758static int
2759subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2760{
2761 return snprintf(buf, len, "sub-surface");
2762}
2763
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002764static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002765subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002766{
2767 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002768 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002769
Jason Ekstranda7af7042013-10-12 22:38:11 -05002770 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002771 weston_view_set_position(view,
2772 view->geometry.x + dx,
2773 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774
2775 /* No need to check parent mappedness, because if parent is not
2776 * mapped, parent is not in a visible layer, so this sub-surface
2777 * will not be drawn either.
2778 */
2779 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002780 struct weston_output *output;
2781
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002782 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002783 * because that would call it also for the parent surface,
2784 * which might not be mapped yet. That would lead to
2785 * inconsistent state, where the window could never be
2786 * mapped.
2787 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002788 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002789 * weston_surface_is_mapped() return true, so that when the
2790 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002791 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792 */
2793 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002794 output = container_of(compositor->output_list.next,
2795 struct weston_output, link);
2796
2797 surface->output = output;
2798 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002799 }
2800}
2801
2802static struct weston_subsurface *
2803weston_surface_to_subsurface(struct weston_surface *surface)
2804{
2805 if (surface->configure == subsurface_configure)
2806 return surface->configure_private;
2807
2808 return NULL;
2809}
2810
Pekka Paalanen01388e22013-04-25 13:57:44 +03002811WL_EXPORT struct weston_surface *
2812weston_surface_get_main_surface(struct weston_surface *surface)
2813{
2814 struct weston_subsurface *sub;
2815
2816 while (surface && (sub = weston_surface_to_subsurface(surface)))
2817 surface = sub->parent;
2818
2819 return surface;
2820}
2821
Pekka Paalanen50b67472014-10-01 15:02:41 +03002822WL_EXPORT int
2823weston_surface_set_role(struct weston_surface *surface,
2824 const char *role_name,
2825 struct wl_resource *error_resource,
2826 uint32_t error_code)
2827{
2828 assert(role_name);
2829
2830 if (surface->role_name == NULL ||
2831 surface->role_name == role_name ||
2832 strcmp(surface->role_name, role_name) == 0) {
2833 surface->role_name = role_name;
2834
2835 return 0;
2836 }
2837
2838 wl_resource_post_error(error_resource, error_code,
2839 "Cannot assign role %s to wl_surface@%d,"
2840 " already has role %s\n",
2841 role_name,
2842 wl_resource_get_id(surface->resource),
2843 surface->role_name);
2844 return -1;
2845}
2846
Pekka Paalanen8274d902014-08-06 19:36:51 +03002847WL_EXPORT void
2848weston_surface_set_label_func(struct weston_surface *surface,
2849 int (*desc)(struct weston_surface *,
2850 char *, size_t))
2851{
2852 surface->get_label = desc;
Pekka Paalanenb5026542014-11-12 15:09:24 +02002853 surface->timeline.force_refresh = 1;
Pekka Paalanen8274d902014-08-06 19:36:51 +03002854}
2855
Pekka Paalanene67858b2013-04-25 13:57:42 +03002856static void
2857subsurface_set_position(struct wl_client *client,
2858 struct wl_resource *resource, int32_t x, int32_t y)
2859{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002860 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861
2862 if (!sub)
2863 return;
2864
2865 sub->position.x = x;
2866 sub->position.y = y;
2867 sub->position.set = 1;
2868}
2869
2870static struct weston_subsurface *
2871subsurface_from_surface(struct weston_surface *surface)
2872{
2873 struct weston_subsurface *sub;
2874
2875 sub = weston_surface_to_subsurface(surface);
2876 if (sub)
2877 return sub;
2878
2879 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2880 if (sub->surface == surface)
2881 return sub;
2882
2883 return NULL;
2884}
2885
2886static struct weston_subsurface *
2887subsurface_sibling_check(struct weston_subsurface *sub,
2888 struct weston_surface *surface,
2889 const char *request)
2890{
2891 struct weston_subsurface *sibling;
2892
2893 sibling = subsurface_from_surface(surface);
2894
2895 if (!sibling) {
2896 wl_resource_post_error(sub->resource,
2897 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2898 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002899 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002900 return NULL;
2901 }
2902
2903 if (sibling->parent != sub->parent) {
2904 wl_resource_post_error(sub->resource,
2905 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2906 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002907 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002908 return NULL;
2909 }
2910
2911 return sibling;
2912}
2913
2914static void
2915subsurface_place_above(struct wl_client *client,
2916 struct wl_resource *resource,
2917 struct wl_resource *sibling_resource)
2918{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002919 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002920 struct weston_surface *surface =
2921 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002922 struct weston_subsurface *sibling;
2923
2924 if (!sub)
2925 return;
2926
2927 sibling = subsurface_sibling_check(sub, surface, "place_above");
2928 if (!sibling)
2929 return;
2930
2931 wl_list_remove(&sub->parent_link_pending);
2932 wl_list_insert(sibling->parent_link_pending.prev,
2933 &sub->parent_link_pending);
2934}
2935
2936static void
2937subsurface_place_below(struct wl_client *client,
2938 struct wl_resource *resource,
2939 struct wl_resource *sibling_resource)
2940{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002941 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002942 struct weston_surface *surface =
2943 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002944 struct weston_subsurface *sibling;
2945
2946 if (!sub)
2947 return;
2948
2949 sibling = subsurface_sibling_check(sub, surface, "place_below");
2950 if (!sibling)
2951 return;
2952
2953 wl_list_remove(&sub->parent_link_pending);
2954 wl_list_insert(&sibling->parent_link_pending,
2955 &sub->parent_link_pending);
2956}
2957
2958static void
2959subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2960{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002961 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002962
2963 if (sub)
2964 sub->synchronized = 1;
2965}
2966
2967static void
2968subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2969{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002970 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002971
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002972 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002973 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002974
2975 /* If sub became effectively desynchronized, flush. */
2976 if (!weston_subsurface_is_synchronized(sub))
2977 weston_subsurface_synchronized_commit(sub);
2978 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002979}
2980
2981static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002982weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2983{
2984 wl_list_remove(&sub->parent_link);
2985 wl_list_remove(&sub->parent_link_pending);
2986 wl_list_remove(&sub->parent_destroy_listener.link);
2987 sub->parent = NULL;
2988}
2989
2990static void
2991weston_subsurface_destroy(struct weston_subsurface *sub);
2992
2993static void
2994subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2995{
2996 struct weston_subsurface *sub =
2997 container_of(listener, struct weston_subsurface,
2998 surface_destroy_listener);
2999 assert(data == &sub->surface->resource);
3000
3001 /* The protocol object (wl_resource) is left inert. */
3002 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003003 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003004
3005 weston_subsurface_destroy(sub);
3006}
3007
3008static void
3009subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
3010{
3011 struct weston_subsurface *sub =
3012 container_of(listener, struct weston_subsurface,
3013 parent_destroy_listener);
3014 assert(data == &sub->parent->resource);
3015 assert(sub->surface != sub->parent);
3016
3017 if (weston_surface_is_mapped(sub->surface))
3018 weston_surface_unmap(sub->surface);
3019
3020 weston_subsurface_unlink_parent(sub);
3021}
3022
3023static void
3024subsurface_resource_destroy(struct wl_resource *resource)
3025{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003026 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003027
3028 if (sub)
3029 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003030}
3031
3032static void
3033subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3034{
3035 wl_resource_destroy(resource);
3036}
3037
3038static void
3039weston_subsurface_link_parent(struct weston_subsurface *sub,
3040 struct weston_surface *parent)
3041{
3042 sub->parent = parent;
3043 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003044 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003045 &sub->parent_destroy_listener);
3046
3047 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3048 wl_list_insert(&parent->subsurface_list_pending,
3049 &sub->parent_link_pending);
3050}
3051
3052static void
3053weston_subsurface_link_surface(struct weston_subsurface *sub,
3054 struct weston_surface *surface)
3055{
3056 sub->surface = surface;
3057 sub->surface_destroy_listener.notify =
3058 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003059 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003060 &sub->surface_destroy_listener);
3061}
3062
3063static void
3064weston_subsurface_destroy(struct weston_subsurface *sub)
3065{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003066 struct weston_view *view, *next;
3067
Pekka Paalanene67858b2013-04-25 13:57:42 +03003068 assert(sub->surface);
3069
3070 if (sub->resource) {
3071 assert(weston_surface_to_subsurface(sub->surface) == sub);
3072 assert(sub->parent_destroy_listener.notify ==
3073 subsurface_handle_parent_destroy);
3074
George Kiagiadakised04d382014-06-13 18:10:26 +02003075 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3076 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003077 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003078 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003079
Pekka Paalanene67858b2013-04-25 13:57:42 +03003080 if (sub->parent)
3081 weston_subsurface_unlink_parent(sub);
3082
Jason Ekstrand7b982072014-05-20 14:33:03 -05003083 weston_surface_state_fini(&sub->cached);
3084 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003085
3086 sub->surface->configure = NULL;
3087 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003088 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089 } else {
3090 /* the dummy weston_subsurface for the parent itself */
3091 assert(sub->parent_destroy_listener.notify == NULL);
3092 wl_list_remove(&sub->parent_link);
3093 wl_list_remove(&sub->parent_link_pending);
3094 }
3095
3096 wl_list_remove(&sub->surface_destroy_listener.link);
3097 free(sub);
3098}
3099
3100static const struct wl_subsurface_interface subsurface_implementation = {
3101 subsurface_destroy,
3102 subsurface_set_position,
3103 subsurface_place_above,
3104 subsurface_place_below,
3105 subsurface_set_sync,
3106 subsurface_set_desync
3107};
3108
3109static struct weston_subsurface *
3110weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3111 struct weston_surface *parent)
3112{
3113 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003114 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003115
Bryce Harringtonde16d892014-11-20 22:21:57 -08003116 sub = zalloc(sizeof *sub);
3117 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003118 return NULL;
3119
Jason Ekstranda7af7042013-10-12 22:38:11 -05003120 wl_list_init(&sub->unused_views);
3121
Jason Ekstranda85118c2013-06-27 20:17:02 -05003122 sub->resource =
3123 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003124 if (!sub->resource) {
3125 free(sub);
3126 return NULL;
3127 }
3128
Jason Ekstranda85118c2013-06-27 20:17:02 -05003129 wl_resource_set_implementation(sub->resource,
3130 &subsurface_implementation,
3131 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003132 weston_subsurface_link_surface(sub, surface);
3133 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003134 weston_surface_state_init(&sub->cached);
3135 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003136 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003137
3138 return sub;
3139}
3140
3141/* Create a dummy subsurface for having the parent itself in its
3142 * sub-surface lists. Makes stacking order manipulation easy.
3143 */
3144static struct weston_subsurface *
3145weston_subsurface_create_for_parent(struct weston_surface *parent)
3146{
3147 struct weston_subsurface *sub;
3148
Bryce Harringtonde16d892014-11-20 22:21:57 -08003149 sub = zalloc(sizeof *sub);
3150 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151 return NULL;
3152
3153 weston_subsurface_link_surface(sub, parent);
3154 sub->parent = parent;
3155 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3156 wl_list_insert(&parent->subsurface_list_pending,
3157 &sub->parent_link_pending);
3158
3159 return sub;
3160}
3161
3162static void
3163subcompositor_get_subsurface(struct wl_client *client,
3164 struct wl_resource *resource,
3165 uint32_t id,
3166 struct wl_resource *surface_resource,
3167 struct wl_resource *parent_resource)
3168{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003169 struct weston_surface *surface =
3170 wl_resource_get_user_data(surface_resource);
3171 struct weston_surface *parent =
3172 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003173 struct weston_subsurface *sub;
3174 static const char where[] = "get_subsurface: wl_subsurface@";
3175
3176 if (surface == parent) {
3177 wl_resource_post_error(resource,
3178 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3179 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003180 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003181 return;
3182 }
3183
3184 if (weston_surface_to_subsurface(surface)) {
3185 wl_resource_post_error(resource,
3186 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3187 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003188 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189 return;
3190 }
3191
Pekka Paalanen50b67472014-10-01 15:02:41 +03003192 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3193 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003194 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003195
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003196 if (weston_surface_get_main_surface(parent) == surface) {
3197 wl_resource_post_error(resource,
3198 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3199 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003200 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003201 return;
3202 }
3203
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204 /* make sure the parent is in its own list */
3205 if (wl_list_empty(&parent->subsurface_list)) {
3206 if (!weston_subsurface_create_for_parent(parent)) {
3207 wl_resource_post_no_memory(resource);
3208 return;
3209 }
3210 }
3211
3212 sub = weston_subsurface_create(id, surface, parent);
3213 if (!sub) {
3214 wl_resource_post_no_memory(resource);
3215 return;
3216 }
3217
3218 surface->configure = subsurface_configure;
3219 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003220 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003221}
3222
3223static void
3224subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3225{
3226 wl_resource_destroy(resource);
3227}
3228
3229static const struct wl_subcompositor_interface subcompositor_interface = {
3230 subcompositor_destroy,
3231 subcompositor_get_subsurface
3232};
3233
3234static void
3235bind_subcompositor(struct wl_client *client,
3236 void *data, uint32_t version, uint32_t id)
3237{
3238 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003239 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003240
Jason Ekstranda85118c2013-06-27 20:17:02 -05003241 resource =
3242 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003243 if (resource == NULL) {
3244 wl_client_post_no_memory(client);
3245 return;
3246 }
3247 wl_resource_set_implementation(resource, &subcompositor_interface,
3248 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003249}
3250
3251static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003252weston_compositor_dpms(struct weston_compositor *compositor,
3253 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003254{
3255 struct weston_output *output;
3256
3257 wl_list_for_each(output, &compositor->output_list, link)
3258 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003259 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003260}
3261
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003262WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003263weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003264{
Neil Roberts8b62e202013-09-30 13:14:47 +01003265 uint32_t old_state = compositor->state;
3266
3267 /* The state needs to be changed before emitting the wake
3268 * signal because that may try to schedule a repaint which
3269 * will not work if the compositor is still sleeping */
3270 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3271
3272 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003273 case WESTON_COMPOSITOR_SLEEPING:
3274 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3275 /* fall through */
3276 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003277 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003278 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003279 /* fall through */
3280 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003281 wl_event_source_timer_update(compositor->idle_source,
3282 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003283 }
3284}
3285
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003286WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003287weston_compositor_offscreen(struct weston_compositor *compositor)
3288{
3289 switch (compositor->state) {
3290 case WESTON_COMPOSITOR_OFFSCREEN:
3291 return;
3292 case WESTON_COMPOSITOR_SLEEPING:
3293 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3294 /* fall through */
3295 default:
3296 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3297 wl_event_source_timer_update(compositor->idle_source, 0);
3298 }
3299}
3300
3301WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003302weston_compositor_sleep(struct weston_compositor *compositor)
3303{
3304 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3305 return;
3306
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003307 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003308 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3309 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3310}
3311
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003312static int
3313idle_handler(void *data)
3314{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003315 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003316
3317 if (compositor->idle_inhibit)
3318 return 1;
3319
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003320 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003321 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003322
3323 return 1;
3324}
3325
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003326WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003327weston_plane_init(struct weston_plane *plane,
3328 struct weston_compositor *ec,
3329 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003330{
3331 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003332 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003333 plane->x = x;
3334 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003335 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003336
3337 /* Init the link so that the call to wl_list_remove() when releasing
3338 * the plane without ever stacking doesn't lead to a crash */
3339 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003340}
3341
3342WL_EXPORT void
3343weston_plane_release(struct weston_plane *plane)
3344{
Xiong Zhang97116532013-10-23 13:58:31 +08003345 struct weston_view *view;
3346
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003347 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003348 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003349
Xiong Zhang97116532013-10-23 13:58:31 +08003350 wl_list_for_each(view, &plane->compositor->view_list, link) {
3351 if (view->plane == plane)
3352 view->plane = NULL;
3353 }
3354
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003355 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003356}
3357
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003358WL_EXPORT void
3359weston_compositor_stack_plane(struct weston_compositor *ec,
3360 struct weston_plane *plane,
3361 struct weston_plane *above)
3362{
3363 if (above)
3364 wl_list_insert(above->link.prev, &plane->link);
3365 else
3366 wl_list_insert(&ec->plane_list, &plane->link);
3367}
3368
Casey Dahlin9074db52012-04-19 22:50:09 -04003369static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003370{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003371 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003372}
3373
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003374static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003375bind_output(struct wl_client *client,
3376 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003377{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003378 struct weston_output *output = data;
3379 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003380 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003381
Jason Ekstranda85118c2013-06-27 20:17:02 -05003382 resource = wl_resource_create(client, &wl_output_interface,
3383 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003384 if (resource == NULL) {
3385 wl_client_post_no_memory(client);
3386 return;
3387 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003388
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003389 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003390 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003391
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003392 wl_output_send_geometry(resource,
3393 output->x,
3394 output->y,
3395 output->mm_width,
3396 output->mm_height,
3397 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003398 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003399 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003400 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003401 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003402 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003403
3404 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003405 wl_output_send_mode(resource,
3406 mode->flags,
3407 mode->width,
3408 mode->height,
3409 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003410 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003411
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003412 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003413 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003414}
3415
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003416/* Move other outputs when one is removed so the space remains contiguos. */
3417static void
3418weston_compositor_remove_output(struct weston_compositor *compositor,
3419 struct weston_output *remove_output)
3420{
3421 struct weston_output *output;
3422 int offset = 0;
3423
3424 wl_list_for_each(output, &compositor->output_list, link) {
3425 if (output == remove_output) {
3426 offset = output->width;
3427 continue;
3428 }
3429
3430 if (offset > 0) {
3431 weston_output_move(output,
3432 output->x - offset, output->y);
3433 output->dirty = 1;
3434 }
3435 }
3436}
3437
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003438WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003439weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003440{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003441 struct wl_resource *resource;
3442
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003443 output->destroying = 1;
3444
Pekka Paalanen133e4392014-09-23 22:08:46 -04003445 weston_presentation_feedback_discard_list(&output->feedback_list);
3446
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003447 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003448 wl_list_remove(&output->link);
3449
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003450 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003451 wl_signal_emit(&output->destroy_signal, output);
3452
Richard Hughesafe690c2013-05-02 10:10:04 +01003453 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003454 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003455 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003456 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003457
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003458 wl_resource_for_each(resource, &output->resource_list) {
3459 wl_resource_set_destructor(resource, NULL);
3460 }
3461
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003462 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003463}
3464
Scott Moreau1bad5db2012-08-18 01:04:05 -06003465static void
3466weston_output_compute_transform(struct weston_output *output)
3467{
3468 struct weston_matrix transform;
3469 int flip;
3470
3471 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003472 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003473
3474 switch(output->transform) {
3475 case WL_OUTPUT_TRANSFORM_FLIPPED:
3476 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3477 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3478 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003479 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003480 flip = -1;
3481 break;
3482 default:
3483 flip = 1;
3484 break;
3485 }
3486
3487 switch(output->transform) {
3488 case WL_OUTPUT_TRANSFORM_NORMAL:
3489 case WL_OUTPUT_TRANSFORM_FLIPPED:
3490 transform.d[0] = flip;
3491 transform.d[1] = 0;
3492 transform.d[4] = 0;
3493 transform.d[5] = 1;
3494 break;
3495 case WL_OUTPUT_TRANSFORM_90:
3496 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3497 transform.d[0] = 0;
3498 transform.d[1] = -flip;
3499 transform.d[4] = 1;
3500 transform.d[5] = 0;
3501 break;
3502 case WL_OUTPUT_TRANSFORM_180:
3503 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3504 transform.d[0] = -flip;
3505 transform.d[1] = 0;
3506 transform.d[4] = 0;
3507 transform.d[5] = -1;
3508 break;
3509 case WL_OUTPUT_TRANSFORM_270:
3510 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3511 transform.d[0] = 0;
3512 transform.d[1] = flip;
3513 transform.d[4] = -1;
3514 transform.d[5] = 0;
3515 break;
3516 default:
3517 break;
3518 }
3519
3520 weston_matrix_multiply(&output->matrix, &transform);
3521}
3522
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003523WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003524weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003525{
Scott Moreau850ca422012-05-21 15:21:25 -06003526 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003527
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003528 weston_matrix_init(&output->matrix);
3529 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003530 -(output->x + output->width / 2.0),
3531 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003532
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003533 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003534 2.0 / output->width,
3535 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003536
Scott Moreauccbf29d2012-02-22 14:21:41 -07003537 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003538 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003539 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003540 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3541 output->zoom.trans_y, 0);
3542 weston_matrix_scale(&output->matrix, magnification,
3543 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003544 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003545
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003546 weston_output_compute_transform(output);
3547
Scott Moreauccbf29d2012-02-22 14:21:41 -07003548 output->dirty = 0;
3549}
3550
Scott Moreau1bad5db2012-08-18 01:04:05 -06003551static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003552weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003553{
3554 output->transform = transform;
3555
3556 switch (transform) {
3557 case WL_OUTPUT_TRANSFORM_90:
3558 case WL_OUTPUT_TRANSFORM_270:
3559 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3560 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3561 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003562 output->width = output->current_mode->height;
3563 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003564 break;
3565 case WL_OUTPUT_TRANSFORM_NORMAL:
3566 case WL_OUTPUT_TRANSFORM_180:
3567 case WL_OUTPUT_TRANSFORM_FLIPPED:
3568 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003569 output->width = output->current_mode->width;
3570 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003571 break;
3572 default:
3573 break;
3574 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003575
Hardening57388e42013-09-18 23:56:36 +02003576 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003577 output->width /= scale;
3578 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003579}
3580
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003581static void
3582weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003583{
3584 output->x = x;
3585 output->y = y;
3586
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003587 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003588 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003589 output->width,
3590 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003591}
3592
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003593WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003594weston_output_move(struct weston_output *output, int x, int y)
3595{
3596 pixman_region32_t old_region;
3597 struct wl_resource *resource;
3598
3599 output->move_x = x - output->x;
3600 output->move_y = y - output->y;
3601
3602 if (output->move_x == 0 && output->move_y == 0)
3603 return;
3604
3605 pixman_region32_init(&old_region);
3606 pixman_region32_copy(&old_region, &output->region);
3607
3608 weston_output_init_geometry(output, x, y);
3609
3610 output->dirty = 1;
3611
3612 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003613 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003614
3615 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003616 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003617 wl_output_send_geometry(resource,
3618 output->x,
3619 output->y,
3620 output->mm_width,
3621 output->mm_height,
3622 output->subpixel,
3623 output->make,
3624 output->model,
3625 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003626
3627 if (wl_resource_get_version(resource) >= 2)
3628 wl_output_send_done(resource);
3629 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003630}
3631
3632WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003633weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003634 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003635 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003636{
3637 output->compositor = c;
3638 output->x = x;
3639 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003640 output->mm_width = mm_width;
3641 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003642 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003643 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003644
Alexander Larsson0b135062013-05-28 16:23:36 +02003645 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003646 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003647
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003648 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003649 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003650
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003651 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003652 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003653 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003654 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003655 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003656
Casey Dahlin58ba1372012-04-19 22:50:08 -04003657 output->id = ffs(~output->compositor->output_id_pool) - 1;
3658 output->compositor->output_id_pool |= 1 << output->id;
3659
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003660 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003661 wl_global_create(c->wl_display, &wl_output_interface, 2,
3662 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003663 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003664}
3665
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003666WL_EXPORT void
3667weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003668 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003669 wl_fixed_t *x, wl_fixed_t *y)
3670{
3671 wl_fixed_t tx, ty;
3672 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003673 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003674
Hardeningff39efa2013-09-18 23:56:35 +02003675 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3676 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003677
3678 switch(output->transform) {
3679 case WL_OUTPUT_TRANSFORM_NORMAL:
3680 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003681 tx = device_x;
3682 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003683 break;
3684 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003685 tx = device_y;
3686 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003687 break;
3688 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003689 tx = width - device_x;
3690 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003691 break;
3692 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003693 tx = width - device_y;
3694 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003695 break;
3696 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003697 tx = width - device_x;
3698 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003699 break;
3700 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003701 tx = width - device_y;
3702 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003703 break;
3704 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003705 tx = device_x;
3706 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003707 break;
3708 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003709 tx = device_y;
3710 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003711 break;
3712 }
3713
Neil Robertseb5a2002014-05-01 16:13:55 +01003714 tx /= output->current_scale;
3715 ty /= output->current_scale;
3716
3717 if (output->zoom.active) {
3718 zoom_scale = output->zoom.spring_z.current;
3719 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3720 output->width / 2.0f *
3721 (zoom_scale + output->zoom.trans_x));
3722 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3723 output->height / 2.0f *
3724 (zoom_scale + output->zoom.trans_y));
3725 tx = wl_fixed_from_double(zx);
3726 ty = wl_fixed_from_double(zy);
3727 }
3728
3729 *x = tx + wl_fixed_from_int(output->x);
3730 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003731}
3732
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003733static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003734destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003735{
Jonny Lamb74130762013-11-26 18:19:46 +01003736 struct weston_surface *surface =
3737 wl_resource_get_user_data(resource);
3738
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003739 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003740 surface->pending.buffer_viewport.buffer.src_width =
3741 wl_fixed_from_int(-1);
3742 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003743 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003744}
3745
3746static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003747viewport_destroy(struct wl_client *client,
3748 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003749{
3750 wl_resource_destroy(resource);
3751}
3752
3753static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003754viewport_set(struct wl_client *client,
3755 struct wl_resource *resource,
3756 wl_fixed_t src_x,
3757 wl_fixed_t src_y,
3758 wl_fixed_t src_width,
3759 wl_fixed_t src_height,
3760 int32_t dst_width,
3761 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003762{
Jonny Lamb74130762013-11-26 18:19:46 +01003763 struct weston_surface *surface =
3764 wl_resource_get_user_data(resource);
3765
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003766 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003767
Jonny Lamb8ae35902013-11-26 18:19:45 +01003768 if (wl_fixed_to_double(src_width) < 0 ||
3769 wl_fixed_to_double(src_height) < 0) {
3770 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003771 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003772 "source dimensions must be non-negative (%fx%f)",
3773 wl_fixed_to_double(src_width),
3774 wl_fixed_to_double(src_height));
3775 return;
3776 }
3777
3778 if (dst_width <= 0 || dst_height <= 0) {
3779 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003780 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003781 "destination dimensions must be positive (%dx%d)",
3782 dst_width, dst_height);
3783 return;
3784 }
3785
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003786 surface->pending.buffer_viewport.buffer.src_x = src_x;
3787 surface->pending.buffer_viewport.buffer.src_y = src_y;
3788 surface->pending.buffer_viewport.buffer.src_width = src_width;
3789 surface->pending.buffer_viewport.buffer.src_height = src_height;
3790 surface->pending.buffer_viewport.surface.width = dst_width;
3791 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003792 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003793}
3794
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003795static void
3796viewport_set_source(struct wl_client *client,
3797 struct wl_resource *resource,
3798 wl_fixed_t src_x,
3799 wl_fixed_t src_y,
3800 wl_fixed_t src_width,
3801 wl_fixed_t src_height)
3802{
3803 struct weston_surface *surface =
3804 wl_resource_get_user_data(resource);
3805
3806 assert(surface->viewport_resource != NULL);
3807
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003808 if (src_width == wl_fixed_from_int(-1) &&
3809 src_height == wl_fixed_from_int(-1)) {
3810 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003811 surface->pending.buffer_viewport.buffer.src_width =
3812 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003813 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003814 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003815 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003816
3817 if (src_width <= 0 || src_height <= 0) {
3818 wl_resource_post_error(resource,
3819 WL_VIEWPORT_ERROR_BAD_VALUE,
3820 "source size must be positive (%fx%f)",
3821 wl_fixed_to_double(src_width),
3822 wl_fixed_to_double(src_height));
3823 return;
3824 }
3825
3826 surface->pending.buffer_viewport.buffer.src_x = src_x;
3827 surface->pending.buffer_viewport.buffer.src_y = src_y;
3828 surface->pending.buffer_viewport.buffer.src_width = src_width;
3829 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003830 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003831}
3832
3833static void
3834viewport_set_destination(struct wl_client *client,
3835 struct wl_resource *resource,
3836 int32_t dst_width,
3837 int32_t dst_height)
3838{
3839 struct weston_surface *surface =
3840 wl_resource_get_user_data(resource);
3841
3842 assert(surface->viewport_resource != NULL);
3843
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003844 if (dst_width == -1 && dst_height == -1) {
3845 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003846 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003847 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003848 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003849 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003850
3851 if (dst_width <= 0 || dst_height <= 0) {
3852 wl_resource_post_error(resource,
3853 WL_VIEWPORT_ERROR_BAD_VALUE,
3854 "destination size must be positive (%dx%d)",
3855 dst_width, dst_height);
3856 return;
3857 }
3858
3859 surface->pending.buffer_viewport.surface.width = dst_width;
3860 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003861 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003862}
3863
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003864static const struct wl_viewport_interface viewport_interface = {
3865 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003866 viewport_set,
3867 viewport_set_source,
3868 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003869};
3870
3871static void
3872scaler_destroy(struct wl_client *client,
3873 struct wl_resource *resource)
3874{
3875 wl_resource_destroy(resource);
3876}
3877
3878static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003879scaler_get_viewport(struct wl_client *client,
3880 struct wl_resource *scaler,
3881 uint32_t id,
3882 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003883{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003884 int version = wl_resource_get_version(scaler);
3885 struct weston_surface *surface =
3886 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003887 struct wl_resource *resource;
3888
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003889 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003890 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003891 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3892 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003893 return;
3894 }
3895
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003896 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003897 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003898 if (resource == NULL) {
3899 wl_client_post_no_memory(client);
3900 return;
3901 }
3902
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003903 wl_resource_set_implementation(resource, &viewport_interface,
3904 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003905
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003906 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003907}
3908
3909static const struct wl_scaler_interface scaler_interface = {
3910 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003911 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003912};
3913
3914static void
3915bind_scaler(struct wl_client *client,
3916 void *data, uint32_t version, uint32_t id)
3917{
3918 struct wl_resource *resource;
3919
3920 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003921 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003922 if (resource == NULL) {
3923 wl_client_post_no_memory(client);
3924 return;
3925 }
3926
3927 wl_resource_set_implementation(resource, &scaler_interface,
3928 NULL, NULL);
3929}
3930
3931static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003932destroy_presentation_feedback(struct wl_resource *feedback_resource)
3933{
3934 struct weston_presentation_feedback *feedback;
3935
3936 feedback = wl_resource_get_user_data(feedback_resource);
3937
3938 wl_list_remove(&feedback->link);
3939 free(feedback);
3940}
3941
3942static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003943presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3944{
3945 wl_resource_destroy(resource);
3946}
3947
3948static void
3949presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003950 struct wl_resource *presentation_resource,
3951 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003952 uint32_t callback)
3953{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003954 struct weston_surface *surface;
3955 struct weston_presentation_feedback *feedback;
3956
3957 surface = wl_resource_get_user_data(surface_resource);
3958
Bryce Harringtonde16d892014-11-20 22:21:57 -08003959 feedback = zalloc(sizeof *feedback);
3960 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04003961 goto err_calloc;
3962
3963 feedback->resource = wl_resource_create(client,
3964 &presentation_feedback_interface,
3965 1, callback);
3966 if (!feedback->resource)
3967 goto err_create;
3968
3969 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3970 destroy_presentation_feedback);
3971
3972 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3973
3974 return;
3975
3976err_create:
3977 free(feedback);
3978
3979err_calloc:
3980 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003981}
3982
3983static const struct presentation_interface presentation_implementation = {
3984 presentation_destroy,
3985 presentation_feedback
3986};
3987
3988static void
3989bind_presentation(struct wl_client *client,
3990 void *data, uint32_t version, uint32_t id)
3991{
3992 struct weston_compositor *compositor = data;
3993 struct wl_resource *resource;
3994
3995 resource = wl_resource_create(client, &presentation_interface,
3996 MIN(version, 1), id);
3997 if (resource == NULL) {
3998 wl_client_post_no_memory(client);
3999 return;
4000 }
4001
4002 wl_resource_set_implementation(resource, &presentation_implementation,
4003 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004004 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004005}
4006
4007static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05004008compositor_bind(struct wl_client *client,
4009 void *data, uint32_t version, uint32_t id)
4010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004011 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05004012 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05004013
Jason Ekstranda85118c2013-06-27 20:17:02 -05004014 resource = wl_resource_create(client, &wl_compositor_interface,
4015 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07004016 if (resource == NULL) {
4017 wl_client_post_no_memory(client);
4018 return;
4019 }
4020
4021 wl_resource_set_implementation(resource, &compositor_interface,
4022 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05004023}
4024
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04004025static void
Martin Minarikf12c2872012-06-11 00:57:39 +02004026log_uname(void)
4027{
4028 struct utsname usys;
4029
4030 uname(&usys);
4031
4032 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4033 usys.version, usys.machine);
4034}
4035
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004036WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004037weston_environment_get_fd(const char *env)
4038{
4039 char *e, *end;
4040 int fd, flags;
4041
4042 e = getenv(env);
4043 if (!e)
4044 return -1;
4045 fd = strtol(e, &end, 0);
4046 if (*end != '\0')
4047 return -1;
4048
4049 flags = fcntl(fd, F_GETFD);
4050 if (flags == -1)
4051 return -1;
4052
4053 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4054 unsetenv(env);
4055
4056 return fd;
4057}
4058
Pekka Paalanenb5026542014-11-12 15:09:24 +02004059static void
4060timeline_key_binding_handler(struct weston_seat *seat, uint32_t time,
4061 uint32_t key, void *data)
4062{
4063 struct weston_compositor *compositor = data;
4064
4065 if (weston_timeline_enabled_)
4066 weston_timeline_close();
4067 else
4068 weston_timeline_open(compositor);
4069}
4070
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004071WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004072weston_compositor_init(struct weston_compositor *ec,
4073 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004074 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004075 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004076{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004077 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004078 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004079 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004080
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004081 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004082 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004083 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004084 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004085 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004086 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004087 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004088 wl_signal_init(&ec->idle_signal);
4089 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004090 wl_signal_init(&ec->show_input_panel_signal);
4091 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004092 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004093 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004094 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004095 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004096 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004097 wl_signal_init(&ec->session_signal);
4098 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004099
Casey Dahlin58ba1372012-04-19 22:50:08 -04004100 ec->output_id_pool = 0;
4101
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004102 if (!wl_global_create(display, &wl_compositor_interface, 3,
4103 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004104 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004105
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004106 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4107 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004108 return -1;
4109
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004110 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004111 ec, bind_scaler))
4112 return -1;
4113
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004114 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4115 ec, bind_presentation))
4116 return -1;
4117
Jason Ekstranda7af7042013-10-12 22:38:11 -05004118 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004119 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004120 wl_list_init(&ec->layer_list);
4121 wl_list_init(&ec->seat_list);
4122 wl_list_init(&ec->output_list);
4123 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004124 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004125 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004126 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004127 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004128 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004129
Xiong Zhang97116532013-10-23 13:58:31 +08004130 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004131 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004132
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004133 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4134 weston_config_section_get_string(s, "keymap_rules",
4135 (char **) &xkb_names.rules, NULL);
4136 weston_config_section_get_string(s, "keymap_model",
4137 (char **) &xkb_names.model, NULL);
4138 weston_config_section_get_string(s, "keymap_layout",
4139 (char **) &xkb_names.layout, NULL);
4140 weston_config_section_get_string(s, "keymap_variant",
4141 (char **) &xkb_names.variant, NULL);
4142 weston_config_section_get_string(s, "keymap_options",
4143 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004144
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004145 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4146 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004147
Jonny Lamb66a41a02014-08-12 14:58:25 +02004148 weston_config_section_get_int(s, "repeat-rate",
4149 &ec->kb_repeat_rate, 40);
4150 weston_config_section_get_int(s, "repeat-delay",
4151 &ec->kb_repeat_delay, 400);
4152
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004153 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004154
4155 wl_data_device_manager_init(ec->wl_display);
4156
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004157 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004158
Daniel Stone725c2c32012-06-22 14:04:36 +01004159 loop = wl_display_get_event_loop(ec->wl_display);
4160 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4161 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4162
4163 ec->input_loop = wl_event_loop_create();
4164
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004165 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4166 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4167
Pekka Paalanenb5026542014-11-12 15:09:24 +02004168 weston_compositor_add_debug_binding(ec, KEY_T,
4169 timeline_key_binding_handler, ec);
4170
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004171 weston_compositor_schedule_repaint(ec);
4172
Daniel Stone725c2c32012-06-22 14:04:36 +01004173 return 0;
4174}
4175
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004176WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004177weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004178{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004179 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004180
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004181 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004182 if (ec->input_loop_source)
4183 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004184
Matt Roper361d2ad2011-08-29 13:52:23 -07004185 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004186 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004187 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004188
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004189 if (ec->renderer)
4190 ec->renderer->destroy(ec);
4191
Daniel Stone325fc2d2012-05-30 16:31:58 +01004192 weston_binding_list_destroy_all(&ec->key_binding_list);
4193 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004194 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004195 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004196 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004197
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004198 weston_plane_release(&ec->primary_plane);
4199
Jonas Ådahlc97af922012-03-28 22:36:09 +02004200 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004201
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004202 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004203}
4204
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004205WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004206weston_compositor_exit_with_code(struct weston_compositor *compositor,
4207 int exit_code)
4208{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004209 if (compositor->exit_code == EXIT_SUCCESS)
4210 compositor->exit_code = exit_code;
4211
Frederic Plourdec336f062014-10-29 14:44:33 -04004212 wl_display_terminate(compositor->wl_display);
4213}
4214
4215WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004216weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4217 const struct weston_pointer_grab_interface *interface)
4218{
4219 struct weston_seat *seat;
4220
4221 ec->default_pointer_grab = interface;
4222 wl_list_for_each(seat, &ec->seat_list, link) {
4223 if (seat->pointer) {
4224 weston_pointer_set_default_grab(seat->pointer,
4225 interface);
4226 }
4227 }
4228}
4229
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004230WL_EXPORT int
4231weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4232 clockid_t clk_id)
4233{
4234 struct timespec ts;
4235
4236 if (clock_gettime(clk_id, &ts) < 0)
4237 return -1;
4238
4239 compositor->presentation_clock = clk_id;
4240
4241 return 0;
4242}
4243
4244/*
4245 * For choosing the software clock, when the display hardware or API
4246 * does not expose a compatible presentation timestamp.
4247 */
4248WL_EXPORT int
4249weston_compositor_set_presentation_clock_software(
4250 struct weston_compositor *compositor)
4251{
4252 /* In order of preference */
4253 static const clockid_t clocks[] = {
4254 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4255 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4256 CLOCK_MONOTONIC, /* no jumps, may crawl */
4257 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4258 CLOCK_REALTIME /* may jump and crawl */
4259 };
4260 unsigned i;
4261
4262 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4263 if (weston_compositor_set_presentation_clock(compositor,
4264 clocks[i]) == 0)
4265 return 0;
4266
4267 weston_log("Error: no suitable presentation clock available.\n");
4268
4269 return -1;
4270}
4271
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004272WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004273weston_version(int *major, int *minor, int *micro)
4274{
4275 *major = WESTON_VERSION_MAJOR;
4276 *minor = WESTON_VERSION_MINOR;
4277 *micro = WESTON_VERSION_MICRO;
4278}
4279
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004280static const char *
4281clock_name(clockid_t clk_id)
4282{
4283 static const char *names[] = {
4284 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4285 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4286 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4287 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4288 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4289 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4290 };
4291
4292 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4293 return "unknown";
4294
4295 return names[clk_id];
4296}
4297
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004298static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004299 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004300 const char *desc;
4301} capability_strings[] = {
4302 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004303 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004304};
4305
4306static void
4307weston_compositor_log_capabilities(struct weston_compositor *compositor)
4308{
4309 unsigned i;
4310 int yes;
4311
4312 weston_log("Compositor capabilities:\n");
4313 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4314 yes = compositor->capabilities & capability_strings[i].bit;
4315 weston_log_continue(STAMP_SPACE "%s %s\n",
4316 capability_strings[i].desc,
4317 yes ? "yes" : "no");
4318 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004319
4320 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4321 clock_name(compositor->presentation_clock),
4322 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004323}
4324
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004325static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004326{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004327 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004328
Martin Minarik6d118362012-06-07 18:01:59 +02004329 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004330 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004331
4332 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004333}
4334
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004335#ifdef HAVE_LIBUNWIND
4336
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004337static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004338print_backtrace(void)
4339{
4340 unw_cursor_t cursor;
4341 unw_context_t context;
4342 unw_word_t off;
4343 unw_proc_info_t pip;
4344 int ret, i = 0;
4345 char procname[256];
4346 const char *filename;
4347 Dl_info dlinfo;
4348
4349 pip.unwind_info = NULL;
4350 ret = unw_getcontext(&context);
4351 if (ret) {
4352 weston_log("unw_getcontext: %d\n", ret);
4353 return;
4354 }
4355
4356 ret = unw_init_local(&cursor, &context);
4357 if (ret) {
4358 weston_log("unw_init_local: %d\n", ret);
4359 return;
4360 }
4361
4362 ret = unw_step(&cursor);
4363 while (ret > 0) {
4364 ret = unw_get_proc_info(&cursor, &pip);
4365 if (ret) {
4366 weston_log("unw_get_proc_info: %d\n", ret);
4367 break;
4368 }
4369
4370 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4371 if (ret && ret != -UNW_ENOMEM) {
4372 if (ret != -UNW_EUNSPEC)
4373 weston_log("unw_get_proc_name: %d\n", ret);
4374 procname[0] = '?';
4375 procname[1] = 0;
4376 }
4377
4378 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4379 *dlinfo.dli_fname)
4380 filename = dlinfo.dli_fname;
4381 else
4382 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004383
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004384 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4385 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4386
4387 ret = unw_step(&cursor);
4388 if (ret < 0)
4389 weston_log("unw_step: %d\n", ret);
4390 }
4391}
4392
4393#else
4394
4395static void
4396print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004397{
4398 void *buffer[32];
4399 int i, count;
4400 Dl_info info;
4401
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004402 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4403 for (i = 0; i < count; i++) {
4404 dladdr(buffer[i], &info);
4405 weston_log(" [%016lx] %s (%s)\n",
4406 (long) buffer[i],
4407 info.dli_sname ? info.dli_sname : "--",
4408 info.dli_fname);
4409 }
4410}
4411
4412#endif
4413
4414static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004415on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004416{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004417 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004418 * then call the backend restore function, which will switch
4419 * back to the vt we launched from or ungrab X etc and then
4420 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004421 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004422 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004423 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004424
Peter Maatmane5b42e42013-03-27 22:38:53 +01004425 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004426
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004427 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004428
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004429 segv_compositor->restore(segv_compositor);
4430
4431 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004432}
4433
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004434WL_EXPORT void *
4435weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004436{
4437 char path[PATH_MAX];
4438 void *module, *init;
4439
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004440 if (name == NULL)
4441 return NULL;
4442
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004443 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004444 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004445 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004446 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004447
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004448 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4449 if (module) {
4450 weston_log("Module '%s' already loaded\n", path);
4451 dlclose(module);
4452 return NULL;
4453 }
4454
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004455 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004456 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004457 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004458 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004459 return NULL;
4460 }
4461
4462 init = dlsym(module, entrypoint);
4463 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004464 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004465 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004466 return NULL;
4467 }
4468
4469 return init;
4470}
4471
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004472static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004473load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004474 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004475{
4476 const char *p, *end;
4477 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004478 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004479 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004480
4481 if (modules == NULL)
4482 return 0;
4483
4484 p = modules;
4485 while (*p) {
4486 end = strchrnul(p, ',');
4487 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004488 module_init = weston_load_module(buffer, "module_init");
Ondřej Majerech01e98b62014-12-06 02:49:17 +01004489 if (!module_init)
4490 return -1;
4491 if (module_init(ec, argc, argv) < 0)
4492 return -1;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004493 p = end;
4494 while (*p == ',')
4495 p++;
4496
4497 }
4498
4499 return 0;
4500}
4501
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004502static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004503 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4504
4505static const char xdg_wrong_message[] =
4506 "fatal: environment variable XDG_RUNTIME_DIR\n"
4507 "is set to \"%s\", which is not a directory.\n";
4508
4509static const char xdg_wrong_mode_message[] =
4510 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004511 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4512 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004513
4514static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004515 "Refer to your distribution on how to get it, or\n"
4516 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4517 "on how to implement it.\n";
4518
Martin Minarik37032f82012-06-18 20:15:18 +02004519static void
4520verify_xdg_runtime_dir(void)
4521{
4522 char *dir = getenv("XDG_RUNTIME_DIR");
4523 struct stat s;
4524
4525 if (!dir) {
4526 weston_log(xdg_error_message);
4527 weston_log_continue(xdg_detail_message);
4528 exit(EXIT_FAILURE);
4529 }
4530
4531 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4532 weston_log(xdg_wrong_message, dir);
4533 weston_log_continue(xdg_detail_message);
4534 exit(EXIT_FAILURE);
4535 }
4536
4537 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4538 weston_log(xdg_wrong_mode_message,
4539 dir, s.st_mode & 0777, s.st_uid);
4540 weston_log_continue(xdg_detail_message);
4541 }
4542}
4543
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004544static int
4545usage(int error_code)
4546{
4547 fprintf(stderr,
4548 "Usage: weston [OPTIONS]\n\n"
4549 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4550 "Weston supports multiple backends, and depending on which backend is in use\n"
4551 "different options will be accepted.\n\n"
4552
4553
4554 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004555 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004556 " -B, --backend=MODULE\tBackend module, one of\n"
4557#if defined(BUILD_DRM_COMPOSITOR)
4558 "\t\t\t\tdrm-backend.so\n"
4559#endif
4560#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004561 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004562#endif
4563#if defined(BUILD_X11_COMPOSITOR)
4564 "\t\t\t\tx11-backend.so\n"
4565#endif
4566#if defined(BUILD_WAYLAND_COMPOSITOR)
4567 "\t\t\t\twayland-backend.so\n"
4568#endif
4569#if defined(BUILD_RDP_COMPOSITOR)
4570 "\t\t\t\trdp-backend.so\n"
4571#endif
4572#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4573 "\t\t\t\trpi-backend.so\n"
4574#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004575 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004576 " -S, --socket=NAME\tName of socket to listen on\n"
4577 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004578 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004579 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004580 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004581 " -h, --help\t\tThis help message\n\n");
4582
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004583#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004584 fprintf(stderr,
4585 "Options for drm-backend.so:\n\n"
4586 " --connector=ID\tBring up only this connector\n"
4587 " --seat=SEAT\t\tThe seat that weston should run on\n"
4588 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004589 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004590 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004591#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004592
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004593#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004594 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004595 "Options for fbdev-backend.so:\n\n"
4596 " --tty=TTY\t\tThe tty to use\n"
4597 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004598#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004599
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004600#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004601 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004602 "Options for x11-backend.so:\n\n"
4603 " --width=WIDTH\t\tWidth of X window\n"
4604 " --height=HEIGHT\tHeight of X window\n"
4605 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004606 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004607 " --output-count=COUNT\tCreate multiple outputs\n"
4608 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004609#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004610
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004611#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004612 fprintf(stderr,
4613 "Options for wayland-backend.so:\n\n"
4614 " --width=WIDTH\t\tWidth of Wayland surface\n"
4615 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004616 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004617 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004618 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004619 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004620 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004621 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004622#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004623
Pekka Paalanene31e0532013-05-22 18:03:07 +03004624#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4625 fprintf(stderr,
4626 "Options for rpi-backend.so:\n\n"
4627 " --tty=TTY\t\tThe tty to use\n"
4628 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4629 " --transform=TR\tThe output transformation, TR is one of:\n"
4630 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004631 " --opaque-regions\tEnable support for opaque regions, can be "
4632 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004633 "\n");
4634#endif
4635
Hardeningc077a842013-07-08 00:51:35 +02004636#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004637 fprintf(stderr,
4638 "Options for rdp-backend.so:\n\n"
4639 " --width=WIDTH\t\tWidth of desktop\n"
4640 " --height=HEIGHT\tHeight of desktop\n"
4641 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4642 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004643 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004644 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4645 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4646 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4647 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4648 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004649#endif
4650
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004651#if defined(BUILD_HEADLESS_COMPOSITOR)
4652 fprintf(stderr,
4653 "Options for headless-backend.so:\n\n"
4654 " --width=WIDTH\t\tWidth of memory surface\n"
4655 " --height=HEIGHT\tHeight of memory surface\n"
4656 " --transform=TR\tThe output transformation, TR is one of:\n"
4657 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4658 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4659#endif
4660
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004661 exit(error_code);
4662}
4663
Peter Maatmane5b42e42013-03-27 22:38:53 +01004664static void
4665catch_signals(void)
4666{
4667 struct sigaction action;
4668
4669 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4670 action.sa_sigaction = on_caught_signal;
4671 sigemptyset(&action.sa_mask);
4672 sigaction(SIGSEGV, &action, NULL);
4673 sigaction(SIGABRT, &action, NULL);
4674}
4675
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004676static void
4677handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4678{
4679 struct wl_client *client = data;
4680
4681 weston_log("Primary client died. Closing...\n");
4682
4683 wl_display_terminate(wl_client_get_display(client));
4684}
4685
Ryo Munakatad8deff62014-09-06 07:32:05 +09004686static char *
4687weston_choose_default_backend(void)
4688{
4689 char *backend = NULL;
4690
4691 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4692 backend = strdup("wayland-backend.so");
4693 else if (getenv("DISPLAY"))
4694 backend = strdup("x11-backend.so");
4695 else
4696 backend = strdup(WESTON_NATIVE_BACKEND);
4697
4698 return backend;
4699}
4700
4701static int
4702weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4703{
4704 if (socket_name) {
4705 if (wl_display_add_socket(display, socket_name)) {
4706 weston_log("fatal: failed to add socket: %m\n");
4707 return -1;
4708 }
4709 } else {
4710 socket_name = wl_display_add_socket_auto(display);
4711 if (!socket_name) {
4712 weston_log("fatal: failed to add socket: %m\n");
4713 return -1;
4714 }
4715 }
4716
4717 setenv("WAYLAND_DISPLAY", socket_name, 1);
4718
4719 return 0;
4720}
4721
Derek Foreman64a3df02014-10-23 12:24:18 -05004722static const struct { const char *name; uint32_t token; } transforms[] = {
4723 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4724 { "90", WL_OUTPUT_TRANSFORM_90 },
4725 { "180", WL_OUTPUT_TRANSFORM_180 },
4726 { "270", WL_OUTPUT_TRANSFORM_270 },
4727 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4728 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4729 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4730 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4731};
4732
4733WL_EXPORT int
4734weston_parse_transform(const char *transform, uint32_t *out)
4735{
4736 unsigned int i;
4737
4738 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4739 if (strcmp(transforms[i].name, transform) == 0) {
4740 *out = transforms[i].token;
4741 return 0;
4742 }
4743
4744 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4745 return -1;
4746}
4747
4748WL_EXPORT const char *
4749weston_transform_to_string(uint32_t output_transform)
4750{
4751 unsigned int i;
4752
4753 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4754 if (transforms[i].token == output_transform)
4755 return transforms[i].name;
4756
4757 return "<illegal value>";
4758}
4759
4760
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004761int main(int argc, char *argv[])
4762{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004763 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004764 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004765 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004766 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004767 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004768 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004769 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004770 int *argc, char *argv[],
4771 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004772 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004773 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004774 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004775 char *modules = NULL;
4776 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004777 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004778 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004779 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004780 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004781 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004782 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004783 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004784 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004785 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004786 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004787 struct wl_client *primary_client;
4788 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004789 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004790
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004791 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004792 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4793 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004794 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4795 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004796 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004797 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004798 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004799 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004800 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004801 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004802
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004803 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004804
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004805 if (help)
4806 usage(EXIT_SUCCESS);
4807
Scott Moreau12245142012-08-29 15:15:58 -06004808 if (version) {
4809 printf(PACKAGE_STRING "\n");
4810 return EXIT_SUCCESS;
4811 }
4812
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004813 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004814
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004815 weston_log("%s\n"
4816 STAMP_SPACE "%s\n"
4817 STAMP_SPACE "Bug reports to: %s\n"
4818 STAMP_SPACE "Build: %s\n",
4819 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004820 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004821 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004822
Martin Minarik37032f82012-06-18 20:15:18 +02004823 verify_xdg_runtime_dir();
4824
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004825 display = wl_display_create();
4826
Tiago Vignatti2116b892011-08-08 05:52:59 -07004827 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004828 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4829 display);
4830 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4831 display);
4832 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4833 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004834
4835 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004836 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4837 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004838
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304839 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4840 ret = EXIT_FAILURE;
4841 goto out_signals;
4842 }
4843
Pekka Paalanen588bee12014-05-07 16:26:25 +03004844 if (noconfig == 0)
4845 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004846 if (config != NULL) {
4847 weston_log("Using config file '%s'\n",
4848 weston_config_get_full_path(config));
4849 } else {
4850 weston_log("Starting with no config file.\n");
4851 }
4852 section = weston_config_get_section(config, "core", NULL, NULL);
4853
Ryo Munakata03faed22014-09-06 07:32:51 +09004854 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004855 weston_config_section_get_string(section, "backend", &backend,
4856 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004857 if (!backend)
4858 backend = weston_choose_default_backend();
4859 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004860
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004861 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304862 if (!backend_init) {
4863 ret = EXIT_FAILURE;
4864 goto out_signals;
4865 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004866
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004867 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004868 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004869 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304870 ret = EXIT_FAILURE;
4871 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004872 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004873
Peter Maatmane5b42e42013-03-27 22:38:53 +01004874 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004875 segv_compositor = ec;
4876
Frederic Plourde4a84c832014-10-30 15:06:34 -04004877 if (idle_time < 0)
4878 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4879 if (idle_time < 0)
4880 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004881 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004882 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004883 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004884
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004885 for (i = 1; i < argc; i++)
4886 weston_log("fatal: unhandled option: %s\n", argv[i]);
4887 if (argc > 1) {
4888 ret = EXIT_FAILURE;
4889 goto out;
4890 }
4891
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004892 weston_compositor_log_capabilities(ec);
4893
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004894 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4895 if (server_socket) {
4896 weston_log("Running with single client\n");
4897 fd = strtol(server_socket, &end, 0);
4898 if (*end != '\0')
4899 fd = -1;
4900 } else {
4901 fd = -1;
4902 }
4903
4904 if (fd != -1) {
4905 primary_client = wl_client_create(display, fd);
4906 if (!primary_client) {
4907 weston_log("fatal: failed to add client: %m\n");
4908 ret = EXIT_FAILURE;
4909 goto out;
4910 }
4911 primary_client_destroyed.notify =
4912 handle_primary_client_destroyed;
4913 wl_client_add_destroy_listener(primary_client,
4914 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004915 } else if (weston_create_listening_socket(display, socket_name)) {
4916 ret = EXIT_FAILURE;
4917 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004918 }
4919
Ryo Munakata03faed22014-09-06 07:32:51 +09004920 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004921 weston_config_section_get_string(section, "shell", &shell,
4922 "desktop-shell.so");
4923
Ryo Munakata03faed22014-09-06 07:32:51 +09004924 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004925 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004926
4927 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004928 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004929 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004930
4931 if (load_modules(ec, option_modules, &argc, argv) < 0)
4932 goto out;
4933
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004934 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4935 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4936 if (numlock_on) {
4937 wl_list_for_each(seat, &ec->seat_list, link) {
4938 if (seat->keyboard)
4939 weston_keyboard_set_locks(seat->keyboard,
4940 WESTON_NUM_LOCK,
4941 WESTON_NUM_LOCK);
4942 }
4943 }
4944
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004945 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004946
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004947 wl_display_run(display);
4948
Frederic Plourdec336f062014-10-29 14:44:33 -04004949 /* Allow for setting return exit code after
4950 * wl_display_run returns normally. This is
4951 * useful for devs/testers and automated tests
4952 * that want to indicate failure status to
4953 * testing infrastructure above
4954 */
4955 ret = ec->exit_code;
4956
Ryo Munakata03faed22014-09-06 07:32:51 +09004957out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004958 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004959 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004960
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004961 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004962
Daniel Stone855028f2012-05-01 20:37:10 +01004963 weston_compositor_xkb_destroy(ec);
4964
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004965 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304966
4967out_signals:
4968 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4969 if (signals[i])
4970 wl_event_source_remove(signals[i]);
4971
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004972 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004973
Martin Minarik19e6f262012-06-07 13:08:46 +02004974 weston_log_file_close();
4975
Ryo Munakata03faed22014-09-06 07:32:51 +09004976 free(backend);
4977 free(shell);
4978 free(socket_name);
4979 free(option_modules);
4980 free(log);
4981 free(modules);
4982
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004983 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004984}