blob: 1f6700d932487c4ff8f071697af8a630f8320252 [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
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-server-protocol.h"
Pekka Paalanen31f7d782014-09-23 22:08:43 -040058#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030059#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040060#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050061#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040064static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065
66static int
67sigchld_handler(int signal_number, void *data)
68{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040070 int status;
71 pid_t pid;
72
Pekka Paalanen23ade622014-08-27 13:31:26 +030073 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
74 wl_list_for_each(p, &child_process_list, link) {
75 if (p->pid == pid)
76 break;
77 }
Bill Spitzak027b9622012-03-17 15:22:03 -070078
Pekka Paalanen23ade622014-08-27 13:31:26 +030079 if (&p->link == &child_process_list) {
80 weston_log("unknown child process exited\n");
81 continue;
82 }
83
84 wl_list_remove(&p->link);
85 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040086 }
87
Pekka Paalanen23ade622014-08-27 13:31:26 +030088 if (pid < 0 && errno != ECHILD)
89 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040090
91 return 1;
92}
93
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094static void
Alexander Larsson0b135062013-05-28 16:23:36 +020095weston_output_transform_scale_init(struct weston_output *output,
96 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020097
Rob Bradford27b17932013-06-26 18:08:46 +010098static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050099weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100100
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600101static void weston_mode_switch_finish(struct weston_output *output,
102 int mode_changed,
103 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800104{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200105 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200106 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 pixman_region32_t old_output_region;
Alexander Larsson355748e2013-05-28 16:23:38 +0200108
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200109 pixman_region32_init(&old_output_region);
110 pixman_region32_copy(&old_output_region, &output->region);
111
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200112 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200113 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
115 pixman_region32_init(&output->previous_damage);
116 pixman_region32_init_rect(&output->region, output->x, output->y,
117 output->width, output->height);
118
119 weston_output_update_matrix(output);
120
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200121 /* If a pointer falls outside the outputs new geometry, move it to its
122 * lower-right corner */
123 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400124 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200125 int32_t x, y;
126
127 if (!pointer)
128 continue;
129
130 x = wl_fixed_to_int(pointer->x);
131 y = wl_fixed_to_int(pointer->y);
132
133 if (!pixman_region32_contains_point(&old_output_region,
134 x, y, NULL) ||
135 pixman_region32_contains_point(&output->region,
136 x, y, NULL))
137 continue;
138
139 if (x >= output->x + output->width)
140 x = output->x + output->width - 1;
141 if (y >= output->y + output->height)
142 y = output->y + output->height - 1;
143
144 pointer->x = wl_fixed_from_int(x);
145 pointer->y = wl_fixed_from_int(y);
146 }
147
148 pixman_region32_fini(&old_output_region);
149
Hardening57388e42013-09-18 23:56:36 +0200150 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600151 wl_resource_for_each(resource, &output->resource_list) {
152 if (mode_changed) {
153 wl_output_send_mode(resource,
154 output->current_mode->flags,
155 output->current_mode->width,
156 output->current_mode->height,
157 output->current_mode->refresh);
158 }
Hardening57388e42013-09-18 23:56:36 +0200159
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600160 if (scale_changed)
161 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200162
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600163 if (wl_resource_get_version(resource) >= 2)
164 wl_output_send_done(resource);
165 }
166}
167
168WL_EXPORT int
169weston_output_mode_set_native(struct weston_output *output,
170 struct weston_mode *mode,
171 int32_t scale)
172{
173 int ret;
174 int mode_changed = 0, scale_changed = 0;
175
176 if (!output->switch_mode)
177 return -1;
178
179 if (!output->original_mode) {
180 mode_changed = 1;
181 ret = output->switch_mode(output, mode);
182 if (ret < 0)
183 return ret;
184 if (output->current_scale != scale) {
185 scale_changed = 1;
186 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200187 }
188 }
189
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600190 output->native_mode = mode;
191 output->native_scale = scale;
192
193 weston_mode_switch_finish(output, mode_changed, scale_changed);
194
195 return 0;
196}
197
198WL_EXPORT int
199weston_output_mode_switch_to_native(struct weston_output *output)
200{
201 int ret;
202 int mode_changed = 0, scale_changed = 0;
203
204 if (!output->switch_mode)
205 return -1;
206
207 if (!output->original_mode) {
208 weston_log("already in the native mode\n");
209 return -1;
210 }
211 /* the non fullscreen clients haven't seen a mode set since we
212 * switched into a temporary, so we need to notify them if the
213 * mode at that time is different from the native mode now.
214 */
215 mode_changed = (output->original_mode != output->native_mode);
216 scale_changed = (output->original_scale != output->native_scale);
217
218 ret = output->switch_mode(output, output->native_mode);
219 if (ret < 0)
220 return ret;
221
222 output->current_scale = output->native_scale;
223
224 output->original_mode = NULL;
225 output->original_scale = 0;
226
227 weston_mode_switch_finish(output, mode_changed, scale_changed);
228
229 return 0;
230}
231
232WL_EXPORT int
233weston_output_mode_switch_to_temporary(struct weston_output *output,
234 struct weston_mode *mode,
235 int32_t scale)
236{
237 int ret;
238
239 if (!output->switch_mode)
240 return -1;
241
242 /* original_mode is the last mode non full screen clients have seen,
243 * so we shouldn't change it if we already have one set.
244 */
245 if (!output->original_mode) {
246 output->original_mode = output->native_mode;
247 output->original_scale = output->native_scale;
248 }
249 ret = output->switch_mode(output, mode);
250 if (ret < 0)
251 return ret;
252
253 output->current_scale = scale;
254
255 weston_mode_switch_finish(output, 0, 0);
256
257 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800258}
259
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400260WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500261weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400262{
263 wl_list_insert(&child_process_list, &process->link);
264}
265
Benjamin Franzke06286262011-05-06 19:12:33 +0200266static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200267child_client_exec(int sockfd, const char *path)
268{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500269 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200270 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200271 sigset_t allsigs;
272
273 /* do not give our signal mask to the new process */
274 sigfillset(&allsigs);
275 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100277 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
278 if (seteuid(getuid()) == -1) {
279 weston_log("compositor: failed seteuid\n");
280 return;
281 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500282
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500283 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
284 * non-CLOEXEC fd to pass through exec. */
285 clientfd = dup(sockfd);
286 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200287 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500288 return;
289 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200290
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500291 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200292 setenv("WAYLAND_SOCKET", s, 1);
293
294 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296 path);
297}
298
299WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500300weston_client_launch(struct weston_compositor *compositor,
301 struct weston_process *proc,
302 const char *path,
303 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200304{
305 int sv[2];
306 pid_t pid;
307 struct wl_client *client;
308
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300309 weston_log("launching '%s'\n", path);
310
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300311 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200312 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200313 "socketpair failed while launching '%s': %m\n",
314 path);
315 return NULL;
316 }
317
318 pid = fork();
319 if (pid == -1) {
320 close(sv[0]);
321 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200322 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200323 "fork failed while launching '%s': %m\n", path);
324 return NULL;
325 }
326
327 if (pid == 0) {
328 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700329 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200330 }
331
332 close(sv[1]);
333
334 client = wl_client_create(compositor->wl_display, sv[0]);
335 if (!client) {
336 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200337 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200338 "wl_client_create failed while launching '%s'.\n",
339 path);
340 return NULL;
341 }
342
343 proc->pid = pid;
344 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500345 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200346
347 return client;
348}
349
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300350struct process_info {
351 struct weston_process proc;
352 char *path;
353};
354
355static void
356process_handle_sigchld(struct weston_process *process, int status)
357{
358 struct process_info *pinfo =
359 container_of(process, struct process_info, proc);
360
361 /*
362 * There are no guarantees whether this runs before or after
363 * the wl_client destructor.
364 */
365
366 if (WIFEXITED(status)) {
367 weston_log("%s exited with status %d\n", pinfo->path,
368 WEXITSTATUS(status));
369 } else if (WIFSIGNALED(status)) {
370 weston_log("%s died on signal %d\n", pinfo->path,
371 WTERMSIG(status));
372 } else {
373 weston_log("%s disappeared\n", pinfo->path);
374 }
375
376 free(pinfo->path);
377 free(pinfo);
378}
379
380WL_EXPORT struct wl_client *
381weston_client_start(struct weston_compositor *compositor, const char *path)
382{
383 struct process_info *pinfo;
384 struct wl_client *client;
385
386 pinfo = zalloc(sizeof *pinfo);
387 if (!pinfo)
388 return NULL;
389
390 pinfo->path = strdup(path);
391 if (!pinfo->path)
392 goto out_free;
393
394 client = weston_client_launch(compositor, &pinfo->proc, path,
395 process_handle_sigchld);
396 if (!client)
397 goto out_str;
398
399 return client;
400
401out_str:
402 free(pinfo->path);
403
404out_free:
405 free(pinfo);
406
407 return NULL;
408}
409
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200410static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300411region_init_infinite(pixman_region32_t *region)
412{
413 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
414 UINT32_MAX, UINT32_MAX);
415}
416
Pekka Paalanene67858b2013-04-25 13:57:42 +0300417static struct weston_subsurface *
418weston_surface_to_subsurface(struct weston_surface *surface);
419
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420WL_EXPORT struct weston_view *
421weston_view_create(struct weston_surface *surface)
422{
423 struct weston_view *view;
424
425 view = calloc(1, sizeof *view);
426 if (view == NULL)
427 return NULL;
428
429 view->surface = surface;
430
Jason Ekstranda7af7042013-10-12 22:38:11 -0500431 /* Assign to surface */
432 wl_list_insert(&surface->views, &view->surface_link);
433
434 wl_signal_init(&view->destroy_signal);
435 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300436 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500437
Xiong Zhang97116532013-10-23 13:58:31 +0800438 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300439 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300440 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500441
442 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300443 pixman_region32_init(&view->transform.masked_boundingbox);
444 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445
446 view->alpha = 1.0;
447 pixman_region32_init(&view->transform.opaque);
448
449 wl_list_init(&view->geometry.transformation_list);
450 wl_list_insert(&view->geometry.transformation_list,
451 &view->transform.position.link);
452 weston_matrix_init(&view->transform.position.matrix);
453 wl_list_init(&view->geometry.child_list);
454 pixman_region32_init(&view->transform.boundingbox);
455 view->transform.dirty = 1;
456
457 view->output = NULL;
458
459 return view;
460}
461
Jason Ekstrand108865d2014-06-26 10:04:49 -0700462struct weston_frame_callback {
463 struct wl_resource *resource;
464 struct wl_list link;
465};
466
Pekka Paalanen133e4392014-09-23 22:08:46 -0400467struct weston_presentation_feedback {
468 struct wl_resource *resource;
469
470 /* XXX: could use just wl_resource_get_link() instead */
471 struct wl_list link;
472};
473
474static void
475weston_presentation_feedback_discard(
476 struct weston_presentation_feedback *feedback)
477{
478 presentation_feedback_send_discarded(feedback->resource);
479 wl_resource_destroy(feedback->resource);
480}
481
482static void
483weston_presentation_feedback_discard_list(struct wl_list *list)
484{
485 struct weston_presentation_feedback *feedback, *tmp;
486
487 wl_list_for_each_safe(feedback, tmp, list, link)
488 weston_presentation_feedback_discard(feedback);
489}
490
491static void
492weston_presentation_feedback_present(
493 struct weston_presentation_feedback *feedback,
494 struct weston_output *output,
495 uint32_t refresh_nsec,
496 const struct timespec *ts,
497 uint64_t seq)
498{
499 struct wl_client *client = wl_resource_get_client(feedback->resource);
500 struct wl_resource *o;
501 uint64_t secs;
502 uint32_t flags = 0;
503
504 wl_resource_for_each(o, &output->resource_list) {
505 if (wl_resource_get_client(o) != client)
506 continue;
507
508 presentation_feedback_send_sync_output(feedback->resource, o);
509 }
510
511 secs = ts->tv_sec;
512 presentation_feedback_send_presented(feedback->resource,
513 secs >> 32, secs & 0xffffffff,
514 ts->tv_nsec,
515 refresh_nsec,
516 seq >> 32, seq & 0xffffffff,
517 flags);
518 wl_resource_destroy(feedback->resource);
519}
520
521static void
522weston_presentation_feedback_present_list(struct wl_list *list,
523 struct weston_output *output,
524 uint32_t refresh_nsec,
525 const struct timespec *ts,
526 uint64_t seq)
527{
528 struct weston_presentation_feedback *feedback, *tmp;
529
530 wl_list_for_each_safe(feedback, tmp, list, link)
531 weston_presentation_feedback_present(feedback, output,
532 refresh_nsec, ts, seq);
533}
534
Jason Ekstrand7b982072014-05-20 14:33:03 -0500535static void
536surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
537{
538 struct weston_surface_state *state =
539 container_of(listener, struct weston_surface_state,
540 buffer_destroy_listener);
541
542 state->buffer = NULL;
543}
544
545static void
546weston_surface_state_init(struct weston_surface_state *state)
547{
548 state->newly_attached = 0;
549 state->buffer = NULL;
550 state->buffer_destroy_listener.notify =
551 surface_state_handle_buffer_destroy;
552 state->sx = 0;
553 state->sy = 0;
554
555 pixman_region32_init(&state->damage);
556 pixman_region32_init(&state->opaque);
557 region_init_infinite(&state->input);
558
559 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400560 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500561
562 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
563 state->buffer_viewport.buffer.scale = 1;
564 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
565 state->buffer_viewport.surface.width = -1;
566 state->buffer_viewport.changed = 0;
567}
568
569static void
570weston_surface_state_fini(struct weston_surface_state *state)
571{
572 struct weston_frame_callback *cb, *next;
573
574 wl_list_for_each_safe(cb, next,
575 &state->frame_callback_list, link)
576 wl_resource_destroy(cb->resource);
577
Pekka Paalanen133e4392014-09-23 22:08:46 -0400578 weston_presentation_feedback_discard_list(&state->feedback_list);
579
Jason Ekstrand7b982072014-05-20 14:33:03 -0500580 pixman_region32_fini(&state->input);
581 pixman_region32_fini(&state->opaque);
582 pixman_region32_fini(&state->damage);
583
584 if (state->buffer)
585 wl_list_remove(&state->buffer_destroy_listener.link);
586 state->buffer = NULL;
587}
588
589static void
590weston_surface_state_set_buffer(struct weston_surface_state *state,
591 struct weston_buffer *buffer)
592{
593 if (state->buffer == buffer)
594 return;
595
596 if (state->buffer)
597 wl_list_remove(&state->buffer_destroy_listener.link);
598 state->buffer = buffer;
599 if (state->buffer)
600 wl_signal_add(&state->buffer->destroy_signal,
601 &state->buffer_destroy_listener);
602}
603
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500604WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500605weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500606{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500607 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400608
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200609 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400610 if (surface == NULL)
611 return NULL;
612
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500613 wl_signal_init(&surface->destroy_signal);
614
615 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500616
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500617 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200618 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400619
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200620 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
621 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200622 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
623 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500624
625 weston_surface_state_init(&surface->pending);
626
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400627 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200628
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200629 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100630
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
Jason Ekstrandef540082014-06-26 10:37:36 -07001788 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789}
1790
1791static void
1792view_accumulate_damage(struct weston_view *view,
1793 pixman_region32_t *opaque)
1794{
1795 pixman_region32_t damage;
1796
1797 pixman_region32_init(&damage);
1798 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001799 pixman_box32_t *extents;
1800
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 extents = pixman_region32_extents(&view->surface->damage);
1802 view_compute_bbox(view, extents->x1, extents->y1,
1803 extents->x2 - extents->x1,
1804 extents->y2 - extents->y1,
1805 &damage);
1806 pixman_region32_translate(&damage,
1807 -view->plane->x,
1808 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001809 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 pixman_region32_copy(&damage, &view->surface->damage);
1811 pixman_region32_translate(&damage,
1812 view->geometry.x - view->plane->x,
1813 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001814 }
1815
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 pixman_region32_subtract(&damage, &damage, opaque);
1817 pixman_region32_union(&view->plane->damage,
1818 &view->plane->damage, &damage);
1819 pixman_region32_fini(&damage);
1820 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001821 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001822}
1823
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001824static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001825compositor_accumulate_damage(struct weston_compositor *ec)
1826{
1827 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001828 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001829 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001830
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001831 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001832
1833 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001834 pixman_region32_copy(&plane->clip, &clip);
1835
1836 pixman_region32_init(&opaque);
1837
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 wl_list_for_each(ev, &ec->view_list, link) {
1839 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001840 continue;
1841
Jason Ekstranda7af7042013-10-12 22:38:11 -05001842 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001843 }
1844
1845 pixman_region32_union(&clip, &clip, &opaque);
1846 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001847 }
1848
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001849 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001850
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851 wl_list_for_each(ev, &ec->view_list, link)
1852 ev->surface->touched = 0;
1853
1854 wl_list_for_each(ev, &ec->view_list, link) {
1855 if (ev->surface->touched)
1856 continue;
1857 ev->surface->touched = 1;
1858
1859 surface_flush_damage(ev->surface);
1860
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001861 /* Both the renderer and the backend have seen the buffer
1862 * by now. If renderer needs the buffer, it has its own
1863 * reference set. If the backend wants to keep the buffer
1864 * around for migrating the surface into a non-primary plane
1865 * later, keep_buffer is true. Otherwise, drop the core
1866 * reference now, and allow early buffer release. This enables
1867 * clients to use single-buffering.
1868 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 if (!ev->surface->keep_buffer)
1870 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001871 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001872}
1873
1874static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001875surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001876{
1877 struct weston_subsurface *sub;
1878
Pekka Paalanene67858b2013-04-25 13:57:42 +03001879 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881 continue;
1882
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1884 wl_list_init(&sub->surface->views);
1885
1886 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001887 }
1888}
1889
1890static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001891surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001892{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893 struct weston_subsurface *sub;
1894 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001895
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1897 if (sub->surface == surface)
1898 continue;
1899
George Kiagiadakised04d382014-06-13 18:10:26 +02001900 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1901 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001902 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001903 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904
1905 surface_free_unused_subsurface_views(sub->surface);
1906 }
1907}
1908
1909static void
1910view_list_add_subsurface_view(struct weston_compositor *compositor,
1911 struct weston_subsurface *sub,
1912 struct weston_view *parent)
1913{
1914 struct weston_subsurface *child;
1915 struct weston_view *view = NULL, *iv;
1916
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001917 if (!weston_surface_is_mapped(sub->surface))
1918 return;
1919
Jason Ekstranda7af7042013-10-12 22:38:11 -05001920 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1921 if (iv->geometry.parent == parent) {
1922 view = iv;
1923 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001924 }
1925 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001926
1927 if (view) {
1928 /* Put it back in the surface's list of views */
1929 wl_list_remove(&view->surface_link);
1930 wl_list_insert(&sub->surface->views, &view->surface_link);
1931 } else {
1932 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001933 weston_view_set_position(view,
1934 sub->position.x,
1935 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001936 weston_view_set_transform_parent(view, parent);
1937 }
1938
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001939 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001940 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001941
Pekka Paalanenb188e912013-11-19 14:03:35 +02001942 if (wl_list_empty(&sub->surface->subsurface_list)) {
1943 wl_list_insert(compositor->view_list.prev, &view->link);
1944 return;
1945 }
1946
1947 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1948 if (child->surface == sub->surface)
1949 wl_list_insert(compositor->view_list.prev, &view->link);
1950 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001951 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001952 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953}
1954
1955static void
1956view_list_add(struct weston_compositor *compositor,
1957 struct weston_view *view)
1958{
1959 struct weston_subsurface *sub;
1960
1961 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962
Pekka Paalanenb188e912013-11-19 14:03:35 +02001963 if (wl_list_empty(&view->surface->subsurface_list)) {
1964 wl_list_insert(compositor->view_list.prev, &view->link);
1965 return;
1966 }
1967
1968 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1969 if (sub->surface == view->surface)
1970 wl_list_insert(compositor->view_list.prev, &view->link);
1971 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001972 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001973 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974}
1975
1976static void
1977weston_compositor_build_view_list(struct weston_compositor *compositor)
1978{
1979 struct weston_view *view;
1980 struct weston_layer *layer;
1981
1982 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001983 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 surface_stash_subsurface_views(view->surface);
1985
1986 wl_list_init(&compositor->view_list);
1987 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001988 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 view_list_add(compositor, view);
1990 }
1991 }
1992
1993 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001994 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001996}
1997
David Herrmann1edf44c2013-10-22 17:11:26 +02001998static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001999weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002000{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002001 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002002 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002003 struct weston_animation *animation, *next;
2004 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002005 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002006 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002007 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002008
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002009 if (output->destroying)
2010 return 0;
2011
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002012 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002013 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002014
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002015 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002016 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002017 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 wl_list_for_each(ev, &ec->view_list, link)
2019 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002020
Pekka Paalanene67858b2013-04-25 13:57:42 +03002021 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 wl_list_for_each(ev, &ec->view_list, link) {
2023 /* Note: This operation is safe to do multiple times on the
2024 * same surface.
2025 */
2026 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002027 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002028 &ev->surface->frame_callback_list);
2029 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002030
2031 wl_list_insert_list(&output->feedback_list,
2032 &ev->surface->feedback_list);
2033 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002034 }
2035 }
2036
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002037 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002038
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002039 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002040 pixman_region32_intersect(&output_damage,
2041 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002042 pixman_region32_subtract(&output_damage,
2043 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002044
Scott Moreauccbf29d2012-02-22 14:21:41 -07002045 if (output->dirty)
2046 weston_output_update_matrix(output);
2047
David Herrmann1edf44c2013-10-22 17:11:26 +02002048 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002049
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002050 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002051
Kristian Høgsbergef044142011-06-21 15:02:12 -04002052 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002053
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002054 weston_compositor_repick(ec);
2055 wl_event_loop_dispatch(ec->input_loop, 0);
2056
Jonas Ådahldb773762012-06-13 00:01:21 +02002057 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002058 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002059 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002060 }
2061
Scott Moreaud64cf212012-06-08 19:40:54 -06002062 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002063 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002064 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002065 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002066
2067 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002068}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002069
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002070static int
2071weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002072{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002073 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002074
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002075 wl_event_loop_dispatch(compositor->input_loop, 0);
2076
2077 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002078}
2079
2080WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002081weston_output_finish_frame(struct weston_output *output,
2082 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002083{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002084 struct weston_compositor *compositor = output->compositor;
2085 struct wl_event_loop *loop =
2086 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002087 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002088 uint32_t refresh_nsec;
2089
2090 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2091 weston_presentation_feedback_present_list(&output->feedback_list,
2092 output, refresh_nsec, stamp,
Pekka Paalanen641307c2014-09-23 22:08:47 -04002093 output->msc);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002094
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002095 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002096
2097 if (output->repaint_needed &&
2098 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2099 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002100 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002101 if (!r)
2102 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002103 }
2104
2105 output->repaint_scheduled = 0;
2106 if (compositor->input_loop_source)
2107 return;
2108
2109 fd = wl_event_loop_get_fd(compositor->input_loop);
2110 compositor->input_loop_source =
2111 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2112 weston_compositor_read_input, compositor);
2113}
2114
2115static void
2116idle_repaint(void *data)
2117{
2118 struct weston_output *output = data;
2119
Jonas Ådahle5a12252013-04-05 23:07:11 +02002120 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002121}
2122
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002123WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002124weston_layer_entry_insert(struct weston_layer_entry *list,
2125 struct weston_layer_entry *entry)
2126{
2127 wl_list_insert(&list->link, &entry->link);
2128 entry->layer = list->layer;
2129}
2130
2131WL_EXPORT void
2132weston_layer_entry_remove(struct weston_layer_entry *entry)
2133{
2134 wl_list_remove(&entry->link);
2135 wl_list_init(&entry->link);
2136 entry->layer = NULL;
2137}
2138
2139WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002140weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2141{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002142 wl_list_init(&layer->view_list.link);
2143 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002144 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002145 if (below != NULL)
2146 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002147}
2148
2149WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002150weston_layer_set_mask(struct weston_layer *layer,
2151 int x, int y, int width, int height)
2152{
2153 struct weston_view *view;
2154
2155 layer->mask.x1 = x;
2156 layer->mask.x2 = x + width;
2157 layer->mask.y1 = y;
2158 layer->mask.y2 = y + height;
2159
2160 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2161 weston_view_geometry_dirty(view);
2162 }
2163}
2164
2165WL_EXPORT void
2166weston_layer_set_mask_infinite(struct weston_layer *layer)
2167{
2168 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2169 UINT32_MAX, UINT32_MAX);
2170}
2171
2172WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002173weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002174{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002175 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002176 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002177
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002178 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2179 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002180 return;
2181
Kristian Høgsbergef044142011-06-21 15:02:12 -04002182 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002183 output->repaint_needed = 1;
2184 if (output->repaint_scheduled)
2185 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002186
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002187 wl_event_loop_add_idle(loop, idle_repaint, output);
2188 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002189
2190 if (compositor->input_loop_source) {
2191 wl_event_source_remove(compositor->input_loop_source);
2192 compositor->input_loop_source = NULL;
2193 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002194}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002195
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002196WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002197weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2198{
2199 struct weston_output *output;
2200
2201 wl_list_for_each(output, &compositor->output_list, link)
2202 weston_output_schedule_repaint(output);
2203}
2204
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002205static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002206surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002207{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002208 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002209}
2210
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002211static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002212surface_attach(struct wl_client *client,
2213 struct wl_resource *resource,
2214 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2215{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002216 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002217 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002218
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002219 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002220 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002221 if (buffer == NULL) {
2222 wl_client_post_no_memory(client);
2223 return;
2224 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002225 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002226
Pekka Paalanende685b82012-12-04 15:58:12 +02002227 /* Attach, attach, without commit in between does not send
2228 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002229 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002230
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002231 surface->pending.sx = sx;
2232 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002233 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002234}
2235
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002236static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002237surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002238 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002239 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002240{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002241 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002242
Pekka Paalanen8e159182012-10-10 12:49:25 +03002243 pixman_region32_union_rect(&surface->pending.damage,
2244 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002245 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002246}
2247
Kristian Høgsberg33418202011-08-16 23:01:28 -04002248static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002249destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002250{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002251 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002252
2253 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002254 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002255}
2256
2257static void
2258surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002259 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002260{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002261 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002262 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002263
2264 cb = malloc(sizeof *cb);
2265 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002266 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002267 return;
2268 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002269
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002270 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2271 callback);
2272 if (cb->resource == NULL) {
2273 free(cb);
2274 wl_resource_post_no_memory(resource);
2275 return;
2276 }
2277
Jason Ekstranda85118c2013-06-27 20:17:02 -05002278 wl_resource_set_implementation(cb->resource, NULL, cb,
2279 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002280
Pekka Paalanenbc106382012-10-10 12:49:31 +03002281 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002282}
2283
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002284static void
2285surface_set_opaque_region(struct wl_client *client,
2286 struct wl_resource *resource,
2287 struct wl_resource *region_resource)
2288{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002289 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002290 struct weston_region *region;
2291
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002292 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002293 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002294 pixman_region32_copy(&surface->pending.opaque,
2295 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002296 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002297 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002298 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002299}
2300
2301static void
2302surface_set_input_region(struct wl_client *client,
2303 struct wl_resource *resource,
2304 struct wl_resource *region_resource)
2305{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002306 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002307 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002308
2309 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002310 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002311 pixman_region32_copy(&surface->pending.input,
2312 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002313 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002314 pixman_region32_fini(&surface->pending.input);
2315 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002316 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002317}
2318
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002319static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002320weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002321{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322 struct weston_subsurface *sub;
2323
2324 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2325 parent_link_pending) {
2326 wl_list_remove(&sub->parent_link);
2327 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2328 }
2329}
2330
2331static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002332weston_surface_commit_state(struct weston_surface *surface,
2333 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002334{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002336 pixman_region32_t opaque;
2337
Alexander Larsson4ea95522013-05-22 14:41:37 +02002338 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002339 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002340 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002341 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002342
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002343 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002344 if (state->newly_attached)
2345 weston_surface_attach(surface, state->buffer);
2346 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002347
Jason Ekstrand7b982072014-05-20 14:33:03 -05002348 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002349 weston_surface_update_size(surface);
2350 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002351 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002352 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002353
Jason Ekstrand7b982072014-05-20 14:33:03 -05002354 state->sx = 0;
2355 state->sy = 0;
2356 state->newly_attached = 0;
2357 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002358
2359 /* wl_surface.damage */
2360 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002361 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002362 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002363 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002364 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002365
2366 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002367 pixman_region32_init(&opaque);
2368 pixman_region32_intersect_rect(&opaque, &state->opaque,
2369 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002370
2371 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2372 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 wl_list_for_each(view, &surface->views, surface_link)
2374 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002375 }
2376
2377 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002378
2379 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002380 pixman_region32_intersect_rect(&surface->input, &state->input,
2381 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002382
Pekka Paalanenbc106382012-10-10 12:49:31 +03002383 /* wl_surface.frame */
2384 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002385 &state->frame_callback_list);
2386 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002387
2388 /* XXX:
2389 * What should happen with a feedback request, if there
2390 * is no wl_buffer attached for this commit?
2391 */
2392
2393 /* presentation.feedback */
2394 wl_list_insert_list(&surface->feedback_list,
2395 &state->feedback_list);
2396 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002397}
2398
2399static void
2400weston_surface_commit(struct weston_surface *surface)
2401{
2402 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002403
Pekka Paalanene67858b2013-04-25 13:57:42 +03002404 weston_surface_commit_subsurface_order(surface);
2405
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002406 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002407}
2408
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002409static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002410weston_subsurface_commit(struct weston_subsurface *sub);
2411
2412static void
2413weston_subsurface_parent_commit(struct weston_subsurface *sub,
2414 int parent_is_synchronized);
2415
2416static void
2417surface_commit(struct wl_client *client, struct wl_resource *resource)
2418{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002419 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002420 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2421
2422 if (sub) {
2423 weston_subsurface_commit(sub);
2424 return;
2425 }
2426
2427 weston_surface_commit(surface);
2428
2429 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2430 if (sub->surface != surface)
2431 weston_subsurface_parent_commit(sub, 0);
2432 }
2433}
2434
2435static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002436surface_set_buffer_transform(struct wl_client *client,
2437 struct wl_resource *resource, int transform)
2438{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002439 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002440
Jonny Lamba55f1392014-05-30 12:07:15 +02002441 /* if wl_output.transform grows more members this will need to be updated. */
2442 if (transform < 0 ||
2443 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2444 wl_resource_post_error(resource,
2445 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2446 "buffer transform must be a valid transform "
2447 "('%d' specified)", transform);
2448 return;
2449 }
2450
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002451 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002452 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002453}
2454
Alexander Larsson4ea95522013-05-22 14:41:37 +02002455static void
2456surface_set_buffer_scale(struct wl_client *client,
2457 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002458 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002459{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002460 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002461
Jonny Lamba55f1392014-05-30 12:07:15 +02002462 if (scale < 1) {
2463 wl_resource_post_error(resource,
2464 WL_SURFACE_ERROR_INVALID_SCALE,
2465 "buffer scale must be at least one "
2466 "('%d' specified)", scale);
2467 return;
2468 }
2469
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002470 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002471 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002472}
2473
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002474static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002475 surface_destroy,
2476 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002477 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002478 surface_frame,
2479 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002480 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002481 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002482 surface_set_buffer_transform,
2483 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002484};
2485
2486static void
2487compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002488 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002489{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002490 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002491 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002492
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002493 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002494 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002495 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002496 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002497 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002498
Jason Ekstranda85118c2013-06-27 20:17:02 -05002499 surface->resource =
2500 wl_resource_create(client, &wl_surface_interface,
2501 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002502 if (surface->resource == NULL) {
2503 weston_surface_destroy(surface);
2504 wl_resource_post_no_memory(resource);
2505 return;
2506 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002507 wl_resource_set_implementation(surface->resource, &surface_interface,
2508 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002509
2510 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002511}
2512
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002513static void
2514destroy_region(struct wl_resource *resource)
2515{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002516 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002517
2518 pixman_region32_fini(&region->region);
2519 free(region);
2520}
2521
2522static void
2523region_destroy(struct wl_client *client, struct wl_resource *resource)
2524{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002525 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002526}
2527
2528static void
2529region_add(struct wl_client *client, struct wl_resource *resource,
2530 int32_t x, int32_t y, int32_t width, int32_t height)
2531{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002532 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002533
2534 pixman_region32_union_rect(&region->region, &region->region,
2535 x, y, width, height);
2536}
2537
2538static void
2539region_subtract(struct wl_client *client, struct wl_resource *resource,
2540 int32_t x, int32_t y, int32_t width, int32_t height)
2541{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002542 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002543 pixman_region32_t rect;
2544
2545 pixman_region32_init_rect(&rect, x, y, width, height);
2546 pixman_region32_subtract(&region->region, &region->region, &rect);
2547 pixman_region32_fini(&rect);
2548}
2549
2550static const struct wl_region_interface region_interface = {
2551 region_destroy,
2552 region_add,
2553 region_subtract
2554};
2555
2556static void
2557compositor_create_region(struct wl_client *client,
2558 struct wl_resource *resource, uint32_t id)
2559{
2560 struct weston_region *region;
2561
2562 region = malloc(sizeof *region);
2563 if (region == NULL) {
2564 wl_resource_post_no_memory(resource);
2565 return;
2566 }
2567
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002568 pixman_region32_init(&region->region);
2569
Jason Ekstranda85118c2013-06-27 20:17:02 -05002570 region->resource =
2571 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002572 if (region->resource == NULL) {
2573 free(region);
2574 wl_resource_post_no_memory(resource);
2575 return;
2576 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002577 wl_resource_set_implementation(region->resource, &region_interface,
2578 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002579}
2580
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002581static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002582 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002583 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002584};
2585
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002586static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002587weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2588{
2589 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002590
Jason Ekstrand7b982072014-05-20 14:33:03 -05002591 weston_surface_commit_state(surface, &sub->cached);
2592 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002593
2594 weston_surface_commit_subsurface_order(surface);
2595
2596 weston_surface_schedule_repaint(surface);
2597
Jason Ekstrand7b982072014-05-20 14:33:03 -05002598 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002599}
2600
2601static void
2602weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2603{
2604 struct weston_surface *surface = sub->surface;
2605
2606 /*
2607 * If this commit would cause the surface to move by the
2608 * attach(dx, dy) parameters, the old damage region must be
2609 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002610 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002611 */
2612 pixman_region32_translate(&sub->cached.damage,
2613 -surface->pending.sx, -surface->pending.sy);
2614 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2615 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002616 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002617
2618 if (surface->pending.newly_attached) {
2619 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002620 weston_surface_state_set_buffer(&sub->cached,
2621 surface->pending.buffer);
2622 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002623 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002624 weston_presentation_feedback_discard_list(
2625 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002626 }
2627 sub->cached.sx += surface->pending.sx;
2628 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002629
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002630 sub->cached.buffer_viewport.changed |=
2631 surface->pending.buffer_viewport.changed;
2632 sub->cached.buffer_viewport.buffer =
2633 surface->pending.buffer_viewport.buffer;
2634 sub->cached.buffer_viewport.surface =
2635 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002636
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002637 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002638
2639 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2640
2641 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2642
2643 wl_list_insert_list(&sub->cached.frame_callback_list,
2644 &surface->pending.frame_callback_list);
2645 wl_list_init(&surface->pending.frame_callback_list);
2646
Pekka Paalanen133e4392014-09-23 22:08:46 -04002647 wl_list_insert_list(&sub->cached.feedback_list,
2648 &surface->pending.feedback_list);
2649 wl_list_init(&surface->pending.feedback_list);
2650
Jason Ekstrand7b982072014-05-20 14:33:03 -05002651 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002652}
2653
Derek Foreman280e7dd2014-10-03 13:13:42 -05002654static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002655weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2656{
2657 while (sub) {
2658 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002659 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660
2661 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002662 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002663
2664 sub = weston_surface_to_subsurface(sub->parent);
2665 }
2666
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002667 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002668}
2669
2670static void
2671weston_subsurface_commit(struct weston_subsurface *sub)
2672{
2673 struct weston_surface *surface = sub->surface;
2674 struct weston_subsurface *tmp;
2675
2676 /* Recursive check for effectively synchronized. */
2677 if (weston_subsurface_is_synchronized(sub)) {
2678 weston_subsurface_commit_to_cache(sub);
2679 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002680 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002681 /* flush accumulated state from cache */
2682 weston_subsurface_commit_to_cache(sub);
2683 weston_subsurface_commit_from_cache(sub);
2684 } else {
2685 weston_surface_commit(surface);
2686 }
2687
2688 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2689 if (tmp->surface != surface)
2690 weston_subsurface_parent_commit(tmp, 0);
2691 }
2692 }
2693}
2694
2695static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002696weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002697{
2698 struct weston_surface *surface = sub->surface;
2699 struct weston_subsurface *tmp;
2700
Pekka Paalanene67858b2013-04-25 13:57:42 +03002701 /* From now on, commit_from_cache the whole sub-tree, regardless of
2702 * the synchronized mode of each child. This sub-surface or some
2703 * of its ancestors were synchronized, so we are synchronized
2704 * all the way down.
2705 */
2706
Jason Ekstrand7b982072014-05-20 14:33:03 -05002707 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002708 weston_subsurface_commit_from_cache(sub);
2709
2710 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2711 if (tmp->surface != surface)
2712 weston_subsurface_parent_commit(tmp, 1);
2713 }
2714}
2715
2716static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002717weston_subsurface_parent_commit(struct weston_subsurface *sub,
2718 int parent_is_synchronized)
2719{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002720 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002721 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 wl_list_for_each(view, &sub->surface->views, surface_link)
2723 weston_view_set_position(view,
2724 sub->position.x,
2725 sub->position.y);
2726
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002727 sub->position.set = 0;
2728 }
2729
2730 if (parent_is_synchronized || sub->synchronized)
2731 weston_subsurface_synchronized_commit(sub);
2732}
2733
2734static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002735subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002736{
2737 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002739
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002741 weston_view_set_position(view,
2742 view->geometry.x + dx,
2743 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002744
2745 /* No need to check parent mappedness, because if parent is not
2746 * mapped, parent is not in a visible layer, so this sub-surface
2747 * will not be drawn either.
2748 */
2749 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002750 struct weston_output *output;
2751
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002752 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002753 * because that would call it also for the parent surface,
2754 * which might not be mapped yet. That would lead to
2755 * inconsistent state, where the window could never be
2756 * mapped.
2757 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002758 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002759 * weston_surface_is_mapped() return true, so that when the
2760 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002761 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002762 */
2763 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002764 output = container_of(compositor->output_list.next,
2765 struct weston_output, link);
2766
2767 surface->output = output;
2768 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002769 }
2770}
2771
2772static struct weston_subsurface *
2773weston_surface_to_subsurface(struct weston_surface *surface)
2774{
2775 if (surface->configure == subsurface_configure)
2776 return surface->configure_private;
2777
2778 return NULL;
2779}
2780
Pekka Paalanen01388e22013-04-25 13:57:44 +03002781WL_EXPORT struct weston_surface *
2782weston_surface_get_main_surface(struct weston_surface *surface)
2783{
2784 struct weston_subsurface *sub;
2785
2786 while (surface && (sub = weston_surface_to_subsurface(surface)))
2787 surface = sub->parent;
2788
2789 return surface;
2790}
2791
Pekka Paalanen50b67472014-10-01 15:02:41 +03002792WL_EXPORT int
2793weston_surface_set_role(struct weston_surface *surface,
2794 const char *role_name,
2795 struct wl_resource *error_resource,
2796 uint32_t error_code)
2797{
2798 assert(role_name);
2799
2800 if (surface->role_name == NULL ||
2801 surface->role_name == role_name ||
2802 strcmp(surface->role_name, role_name) == 0) {
2803 surface->role_name = role_name;
2804
2805 return 0;
2806 }
2807
2808 wl_resource_post_error(error_resource, error_code,
2809 "Cannot assign role %s to wl_surface@%d,"
2810 " already has role %s\n",
2811 role_name,
2812 wl_resource_get_id(surface->resource),
2813 surface->role_name);
2814 return -1;
2815}
2816
Pekka Paalanene67858b2013-04-25 13:57:42 +03002817static void
2818subsurface_set_position(struct wl_client *client,
2819 struct wl_resource *resource, int32_t x, int32_t y)
2820{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002821 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002822
2823 if (!sub)
2824 return;
2825
2826 sub->position.x = x;
2827 sub->position.y = y;
2828 sub->position.set = 1;
2829}
2830
2831static struct weston_subsurface *
2832subsurface_from_surface(struct weston_surface *surface)
2833{
2834 struct weston_subsurface *sub;
2835
2836 sub = weston_surface_to_subsurface(surface);
2837 if (sub)
2838 return sub;
2839
2840 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2841 if (sub->surface == surface)
2842 return sub;
2843
2844 return NULL;
2845}
2846
2847static struct weston_subsurface *
2848subsurface_sibling_check(struct weston_subsurface *sub,
2849 struct weston_surface *surface,
2850 const char *request)
2851{
2852 struct weston_subsurface *sibling;
2853
2854 sibling = subsurface_from_surface(surface);
2855
2856 if (!sibling) {
2857 wl_resource_post_error(sub->resource,
2858 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2859 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002860 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861 return NULL;
2862 }
2863
2864 if (sibling->parent != sub->parent) {
2865 wl_resource_post_error(sub->resource,
2866 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2867 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002868 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002869 return NULL;
2870 }
2871
2872 return sibling;
2873}
2874
2875static void
2876subsurface_place_above(struct wl_client *client,
2877 struct wl_resource *resource,
2878 struct wl_resource *sibling_resource)
2879{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002880 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002881 struct weston_surface *surface =
2882 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002883 struct weston_subsurface *sibling;
2884
2885 if (!sub)
2886 return;
2887
2888 sibling = subsurface_sibling_check(sub, surface, "place_above");
2889 if (!sibling)
2890 return;
2891
2892 wl_list_remove(&sub->parent_link_pending);
2893 wl_list_insert(sibling->parent_link_pending.prev,
2894 &sub->parent_link_pending);
2895}
2896
2897static void
2898subsurface_place_below(struct wl_client *client,
2899 struct wl_resource *resource,
2900 struct wl_resource *sibling_resource)
2901{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002902 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002903 struct weston_surface *surface =
2904 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002905 struct weston_subsurface *sibling;
2906
2907 if (!sub)
2908 return;
2909
2910 sibling = subsurface_sibling_check(sub, surface, "place_below");
2911 if (!sibling)
2912 return;
2913
2914 wl_list_remove(&sub->parent_link_pending);
2915 wl_list_insert(&sibling->parent_link_pending,
2916 &sub->parent_link_pending);
2917}
2918
2919static void
2920subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2921{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002922 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002923
2924 if (sub)
2925 sub->synchronized = 1;
2926}
2927
2928static void
2929subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2930{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002931 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002932
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002933 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002934 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002935
2936 /* If sub became effectively desynchronized, flush. */
2937 if (!weston_subsurface_is_synchronized(sub))
2938 weston_subsurface_synchronized_commit(sub);
2939 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002940}
2941
2942static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002943weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2944{
2945 wl_list_remove(&sub->parent_link);
2946 wl_list_remove(&sub->parent_link_pending);
2947 wl_list_remove(&sub->parent_destroy_listener.link);
2948 sub->parent = NULL;
2949}
2950
2951static void
2952weston_subsurface_destroy(struct weston_subsurface *sub);
2953
2954static void
2955subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2956{
2957 struct weston_subsurface *sub =
2958 container_of(listener, struct weston_subsurface,
2959 surface_destroy_listener);
2960 assert(data == &sub->surface->resource);
2961
2962 /* The protocol object (wl_resource) is left inert. */
2963 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002964 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002965
2966 weston_subsurface_destroy(sub);
2967}
2968
2969static void
2970subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2971{
2972 struct weston_subsurface *sub =
2973 container_of(listener, struct weston_subsurface,
2974 parent_destroy_listener);
2975 assert(data == &sub->parent->resource);
2976 assert(sub->surface != sub->parent);
2977
2978 if (weston_surface_is_mapped(sub->surface))
2979 weston_surface_unmap(sub->surface);
2980
2981 weston_subsurface_unlink_parent(sub);
2982}
2983
2984static void
2985subsurface_resource_destroy(struct wl_resource *resource)
2986{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002987 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002988
2989 if (sub)
2990 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002991}
2992
2993static void
2994subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2995{
2996 wl_resource_destroy(resource);
2997}
2998
2999static void
3000weston_subsurface_link_parent(struct weston_subsurface *sub,
3001 struct weston_surface *parent)
3002{
3003 sub->parent = parent;
3004 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003005 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003006 &sub->parent_destroy_listener);
3007
3008 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3009 wl_list_insert(&parent->subsurface_list_pending,
3010 &sub->parent_link_pending);
3011}
3012
3013static void
3014weston_subsurface_link_surface(struct weston_subsurface *sub,
3015 struct weston_surface *surface)
3016{
3017 sub->surface = surface;
3018 sub->surface_destroy_listener.notify =
3019 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003020 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003021 &sub->surface_destroy_listener);
3022}
3023
3024static void
3025weston_subsurface_destroy(struct weston_subsurface *sub)
3026{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003027 struct weston_view *view, *next;
3028
Pekka Paalanene67858b2013-04-25 13:57:42 +03003029 assert(sub->surface);
3030
3031 if (sub->resource) {
3032 assert(weston_surface_to_subsurface(sub->surface) == sub);
3033 assert(sub->parent_destroy_listener.notify ==
3034 subsurface_handle_parent_destroy);
3035
George Kiagiadakised04d382014-06-13 18:10:26 +02003036 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3037 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003038 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003039 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040
Pekka Paalanene67858b2013-04-25 13:57:42 +03003041 if (sub->parent)
3042 weston_subsurface_unlink_parent(sub);
3043
Jason Ekstrand7b982072014-05-20 14:33:03 -05003044 weston_surface_state_fini(&sub->cached);
3045 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003046
3047 sub->surface->configure = NULL;
3048 sub->surface->configure_private = NULL;
3049 } else {
3050 /* the dummy weston_subsurface for the parent itself */
3051 assert(sub->parent_destroy_listener.notify == NULL);
3052 wl_list_remove(&sub->parent_link);
3053 wl_list_remove(&sub->parent_link_pending);
3054 }
3055
3056 wl_list_remove(&sub->surface_destroy_listener.link);
3057 free(sub);
3058}
3059
3060static const struct wl_subsurface_interface subsurface_implementation = {
3061 subsurface_destroy,
3062 subsurface_set_position,
3063 subsurface_place_above,
3064 subsurface_place_below,
3065 subsurface_set_sync,
3066 subsurface_set_desync
3067};
3068
3069static struct weston_subsurface *
3070weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3071 struct weston_surface *parent)
3072{
3073 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003074 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003075
3076 sub = calloc(1, sizeof *sub);
3077 if (!sub)
3078 return NULL;
3079
Jason Ekstranda7af7042013-10-12 22:38:11 -05003080 wl_list_init(&sub->unused_views);
3081
Jason Ekstranda85118c2013-06-27 20:17:02 -05003082 sub->resource =
3083 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003084 if (!sub->resource) {
3085 free(sub);
3086 return NULL;
3087 }
3088
Jason Ekstranda85118c2013-06-27 20:17:02 -05003089 wl_resource_set_implementation(sub->resource,
3090 &subsurface_implementation,
3091 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003092 weston_subsurface_link_surface(sub, surface);
3093 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003094 weston_surface_state_init(&sub->cached);
3095 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003096 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097
3098 return sub;
3099}
3100
3101/* Create a dummy subsurface for having the parent itself in its
3102 * sub-surface lists. Makes stacking order manipulation easy.
3103 */
3104static struct weston_subsurface *
3105weston_subsurface_create_for_parent(struct weston_surface *parent)
3106{
3107 struct weston_subsurface *sub;
3108
3109 sub = calloc(1, sizeof *sub);
3110 if (!sub)
3111 return NULL;
3112
3113 weston_subsurface_link_surface(sub, parent);
3114 sub->parent = parent;
3115 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3116 wl_list_insert(&parent->subsurface_list_pending,
3117 &sub->parent_link_pending);
3118
3119 return sub;
3120}
3121
3122static void
3123subcompositor_get_subsurface(struct wl_client *client,
3124 struct wl_resource *resource,
3125 uint32_t id,
3126 struct wl_resource *surface_resource,
3127 struct wl_resource *parent_resource)
3128{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003129 struct weston_surface *surface =
3130 wl_resource_get_user_data(surface_resource);
3131 struct weston_surface *parent =
3132 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003133 struct weston_subsurface *sub;
3134 static const char where[] = "get_subsurface: wl_subsurface@";
3135
3136 if (surface == parent) {
3137 wl_resource_post_error(resource,
3138 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3139 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003140 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003141 return;
3142 }
3143
3144 if (weston_surface_to_subsurface(surface)) {
3145 wl_resource_post_error(resource,
3146 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3147 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003148 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003149 return;
3150 }
3151
Pekka Paalanen50b67472014-10-01 15:02:41 +03003152 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3153 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003155
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003156 if (weston_surface_get_main_surface(parent) == surface) {
3157 wl_resource_post_error(resource,
3158 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3159 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003160 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003161 return;
3162 }
3163
Pekka Paalanene67858b2013-04-25 13:57:42 +03003164 /* make sure the parent is in its own list */
3165 if (wl_list_empty(&parent->subsurface_list)) {
3166 if (!weston_subsurface_create_for_parent(parent)) {
3167 wl_resource_post_no_memory(resource);
3168 return;
3169 }
3170 }
3171
3172 sub = weston_subsurface_create(id, surface, parent);
3173 if (!sub) {
3174 wl_resource_post_no_memory(resource);
3175 return;
3176 }
3177
3178 surface->configure = subsurface_configure;
3179 surface->configure_private = sub;
3180}
3181
3182static void
3183subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3184{
3185 wl_resource_destroy(resource);
3186}
3187
3188static const struct wl_subcompositor_interface subcompositor_interface = {
3189 subcompositor_destroy,
3190 subcompositor_get_subsurface
3191};
3192
3193static void
3194bind_subcompositor(struct wl_client *client,
3195 void *data, uint32_t version, uint32_t id)
3196{
3197 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003198 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003199
Jason Ekstranda85118c2013-06-27 20:17:02 -05003200 resource =
3201 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003202 if (resource == NULL) {
3203 wl_client_post_no_memory(client);
3204 return;
3205 }
3206 wl_resource_set_implementation(resource, &subcompositor_interface,
3207 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003208}
3209
3210static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003211weston_compositor_dpms(struct weston_compositor *compositor,
3212 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003213{
3214 struct weston_output *output;
3215
3216 wl_list_for_each(output, &compositor->output_list, link)
3217 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003218 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003219}
3220
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003221WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003222weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003223{
Neil Roberts8b62e202013-09-30 13:14:47 +01003224 uint32_t old_state = compositor->state;
3225
3226 /* The state needs to be changed before emitting the wake
3227 * signal because that may try to schedule a repaint which
3228 * will not work if the compositor is still sleeping */
3229 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3230
3231 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003232 case WESTON_COMPOSITOR_SLEEPING:
3233 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3234 /* fall through */
3235 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003236 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003237 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003238 /* fall through */
3239 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003240 wl_event_source_timer_update(compositor->idle_source,
3241 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003242 }
3243}
3244
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003245WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003246weston_compositor_offscreen(struct weston_compositor *compositor)
3247{
3248 switch (compositor->state) {
3249 case WESTON_COMPOSITOR_OFFSCREEN:
3250 return;
3251 case WESTON_COMPOSITOR_SLEEPING:
3252 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3253 /* fall through */
3254 default:
3255 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3256 wl_event_source_timer_update(compositor->idle_source, 0);
3257 }
3258}
3259
3260WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003261weston_compositor_sleep(struct weston_compositor *compositor)
3262{
3263 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3264 return;
3265
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003266 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003267 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3268 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3269}
3270
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003271static int
3272idle_handler(void *data)
3273{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003274 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003275
3276 if (compositor->idle_inhibit)
3277 return 1;
3278
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003279 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003280 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003281
3282 return 1;
3283}
3284
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003285WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003286weston_plane_init(struct weston_plane *plane,
3287 struct weston_compositor *ec,
3288 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003289{
3290 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003291 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003292 plane->x = x;
3293 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003294 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003295
3296 /* Init the link so that the call to wl_list_remove() when releasing
3297 * the plane without ever stacking doesn't lead to a crash */
3298 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003299}
3300
3301WL_EXPORT void
3302weston_plane_release(struct weston_plane *plane)
3303{
Xiong Zhang97116532013-10-23 13:58:31 +08003304 struct weston_view *view;
3305
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003306 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003307 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003308
Xiong Zhang97116532013-10-23 13:58:31 +08003309 wl_list_for_each(view, &plane->compositor->view_list, link) {
3310 if (view->plane == plane)
3311 view->plane = NULL;
3312 }
3313
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003314 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003315}
3316
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003317WL_EXPORT void
3318weston_compositor_stack_plane(struct weston_compositor *ec,
3319 struct weston_plane *plane,
3320 struct weston_plane *above)
3321{
3322 if (above)
3323 wl_list_insert(above->link.prev, &plane->link);
3324 else
3325 wl_list_insert(&ec->plane_list, &plane->link);
3326}
3327
Casey Dahlin9074db52012-04-19 22:50:09 -04003328static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003329{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003330 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003331}
3332
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003333static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003334bind_output(struct wl_client *client,
3335 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003336{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003337 struct weston_output *output = data;
3338 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003339 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003340
Jason Ekstranda85118c2013-06-27 20:17:02 -05003341 resource = wl_resource_create(client, &wl_output_interface,
3342 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003343 if (resource == NULL) {
3344 wl_client_post_no_memory(client);
3345 return;
3346 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003347
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003348 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003349 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003350
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003351 wl_output_send_geometry(resource,
3352 output->x,
3353 output->y,
3354 output->mm_width,
3355 output->mm_height,
3356 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003357 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003358 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003359 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003360 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003361 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003362
3363 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003364 wl_output_send_mode(resource,
3365 mode->flags,
3366 mode->width,
3367 mode->height,
3368 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003369 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003370
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003371 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003372 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003373}
3374
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003375/* Move other outputs when one is removed so the space remains contiguos. */
3376static void
3377weston_compositor_remove_output(struct weston_compositor *compositor,
3378 struct weston_output *remove_output)
3379{
3380 struct weston_output *output;
3381 int offset = 0;
3382
3383 wl_list_for_each(output, &compositor->output_list, link) {
3384 if (output == remove_output) {
3385 offset = output->width;
3386 continue;
3387 }
3388
3389 if (offset > 0) {
3390 weston_output_move(output,
3391 output->x - offset, output->y);
3392 output->dirty = 1;
3393 }
3394 }
3395}
3396
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003397WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003398weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003399{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003400 struct wl_resource *resource;
3401
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003402 output->destroying = 1;
3403
Pekka Paalanen133e4392014-09-23 22:08:46 -04003404 weston_presentation_feedback_discard_list(&output->feedback_list);
3405
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003406 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003407 wl_list_remove(&output->link);
3408
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003409 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003410 wl_signal_emit(&output->destroy_signal, output);
3411
Richard Hughesafe690c2013-05-02 10:10:04 +01003412 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003413 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003414 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003415 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003416
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003417 wl_resource_for_each(resource, &output->resource_list) {
3418 wl_resource_set_destructor(resource, NULL);
3419 }
3420
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003421 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003422}
3423
Scott Moreau1bad5db2012-08-18 01:04:05 -06003424static void
3425weston_output_compute_transform(struct weston_output *output)
3426{
3427 struct weston_matrix transform;
3428 int flip;
3429
3430 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003431 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003432
3433 switch(output->transform) {
3434 case WL_OUTPUT_TRANSFORM_FLIPPED:
3435 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3436 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3437 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003438 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003439 flip = -1;
3440 break;
3441 default:
3442 flip = 1;
3443 break;
3444 }
3445
3446 switch(output->transform) {
3447 case WL_OUTPUT_TRANSFORM_NORMAL:
3448 case WL_OUTPUT_TRANSFORM_FLIPPED:
3449 transform.d[0] = flip;
3450 transform.d[1] = 0;
3451 transform.d[4] = 0;
3452 transform.d[5] = 1;
3453 break;
3454 case WL_OUTPUT_TRANSFORM_90:
3455 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3456 transform.d[0] = 0;
3457 transform.d[1] = -flip;
3458 transform.d[4] = 1;
3459 transform.d[5] = 0;
3460 break;
3461 case WL_OUTPUT_TRANSFORM_180:
3462 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3463 transform.d[0] = -flip;
3464 transform.d[1] = 0;
3465 transform.d[4] = 0;
3466 transform.d[5] = -1;
3467 break;
3468 case WL_OUTPUT_TRANSFORM_270:
3469 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3470 transform.d[0] = 0;
3471 transform.d[1] = flip;
3472 transform.d[4] = -1;
3473 transform.d[5] = 0;
3474 break;
3475 default:
3476 break;
3477 }
3478
3479 weston_matrix_multiply(&output->matrix, &transform);
3480}
3481
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003482WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003483weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003484{
Scott Moreau850ca422012-05-21 15:21:25 -06003485 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003486
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003487 weston_matrix_init(&output->matrix);
3488 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003489 -(output->x + output->width / 2.0),
3490 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003491
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003492 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003493 2.0 / output->width,
3494 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003495
Scott Moreauccbf29d2012-02-22 14:21:41 -07003496 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003497 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003498 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003499 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3500 output->zoom.trans_y, 0);
3501 weston_matrix_scale(&output->matrix, magnification,
3502 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003503 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003504
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003505 weston_output_compute_transform(output);
3506
Scott Moreauccbf29d2012-02-22 14:21:41 -07003507 output->dirty = 0;
3508}
3509
Scott Moreau1bad5db2012-08-18 01:04:05 -06003510static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003511weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003512{
3513 output->transform = transform;
3514
3515 switch (transform) {
3516 case WL_OUTPUT_TRANSFORM_90:
3517 case WL_OUTPUT_TRANSFORM_270:
3518 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3519 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3520 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003521 output->width = output->current_mode->height;
3522 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003523 break;
3524 case WL_OUTPUT_TRANSFORM_NORMAL:
3525 case WL_OUTPUT_TRANSFORM_180:
3526 case WL_OUTPUT_TRANSFORM_FLIPPED:
3527 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003528 output->width = output->current_mode->width;
3529 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003530 break;
3531 default:
3532 break;
3533 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003534
Hardening57388e42013-09-18 23:56:36 +02003535 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003536 output->width /= scale;
3537 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003538}
3539
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003540static void
3541weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003542{
3543 output->x = x;
3544 output->y = y;
3545
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003546 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003547 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003548 output->width,
3549 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003550}
3551
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003552WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003553weston_output_move(struct weston_output *output, int x, int y)
3554{
3555 pixman_region32_t old_region;
3556 struct wl_resource *resource;
3557
3558 output->move_x = x - output->x;
3559 output->move_y = y - output->y;
3560
3561 if (output->move_x == 0 && output->move_y == 0)
3562 return;
3563
3564 pixman_region32_init(&old_region);
3565 pixman_region32_copy(&old_region, &output->region);
3566
3567 weston_output_init_geometry(output, x, y);
3568
3569 output->dirty = 1;
3570
3571 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003572 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003573
3574 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003575 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003576 wl_output_send_geometry(resource,
3577 output->x,
3578 output->y,
3579 output->mm_width,
3580 output->mm_height,
3581 output->subpixel,
3582 output->make,
3583 output->model,
3584 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003585
3586 if (wl_resource_get_version(resource) >= 2)
3587 wl_output_send_done(resource);
3588 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003589}
3590
3591WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003592weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003593 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003594 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003595{
3596 output->compositor = c;
3597 output->x = x;
3598 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003599 output->mm_width = mm_width;
3600 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003601 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003602 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003603
Alexander Larsson0b135062013-05-28 16:23:36 +02003604 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003605 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003606
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003607 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003608 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003609
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003610 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003611 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003612 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003613 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003614 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003615
Casey Dahlin58ba1372012-04-19 22:50:08 -04003616 output->id = ffs(~output->compositor->output_id_pool) - 1;
3617 output->compositor->output_id_pool |= 1 << output->id;
3618
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003619 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003620 wl_global_create(c->wl_display, &wl_output_interface, 2,
3621 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003622 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003623}
3624
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003625WL_EXPORT void
3626weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003627 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003628 wl_fixed_t *x, wl_fixed_t *y)
3629{
3630 wl_fixed_t tx, ty;
3631 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003632 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003633
Hardeningff39efa2013-09-18 23:56:35 +02003634 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3635 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003636
3637 switch(output->transform) {
3638 case WL_OUTPUT_TRANSFORM_NORMAL:
3639 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003640 tx = device_x;
3641 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003642 break;
3643 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003644 tx = device_y;
3645 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003646 break;
3647 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003648 tx = width - device_x;
3649 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003650 break;
3651 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003652 tx = width - device_y;
3653 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003654 break;
3655 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003656 tx = width - device_x;
3657 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003658 break;
3659 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003660 tx = width - device_y;
3661 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003662 break;
3663 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003664 tx = device_x;
3665 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003666 break;
3667 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003668 tx = device_y;
3669 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003670 break;
3671 }
3672
Neil Robertseb5a2002014-05-01 16:13:55 +01003673 tx /= output->current_scale;
3674 ty /= output->current_scale;
3675
3676 if (output->zoom.active) {
3677 zoom_scale = output->zoom.spring_z.current;
3678 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3679 output->width / 2.0f *
3680 (zoom_scale + output->zoom.trans_x));
3681 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3682 output->height / 2.0f *
3683 (zoom_scale + output->zoom.trans_y));
3684 tx = wl_fixed_from_double(zx);
3685 ty = wl_fixed_from_double(zy);
3686 }
3687
3688 *x = tx + wl_fixed_from_int(output->x);
3689 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003690}
3691
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003692static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003693destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003694{
Jonny Lamb74130762013-11-26 18:19:46 +01003695 struct weston_surface *surface =
3696 wl_resource_get_user_data(resource);
3697
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003698 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003699 surface->pending.buffer_viewport.buffer.src_width =
3700 wl_fixed_from_int(-1);
3701 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003702 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003703}
3704
3705static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003706viewport_destroy(struct wl_client *client,
3707 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003708{
3709 wl_resource_destroy(resource);
3710}
3711
3712static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003713viewport_set(struct wl_client *client,
3714 struct wl_resource *resource,
3715 wl_fixed_t src_x,
3716 wl_fixed_t src_y,
3717 wl_fixed_t src_width,
3718 wl_fixed_t src_height,
3719 int32_t dst_width,
3720 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003721{
Jonny Lamb74130762013-11-26 18:19:46 +01003722 struct weston_surface *surface =
3723 wl_resource_get_user_data(resource);
3724
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003725 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003726
Jonny Lamb8ae35902013-11-26 18:19:45 +01003727 if (wl_fixed_to_double(src_width) < 0 ||
3728 wl_fixed_to_double(src_height) < 0) {
3729 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003730 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003731 "source dimensions must be non-negative (%fx%f)",
3732 wl_fixed_to_double(src_width),
3733 wl_fixed_to_double(src_height));
3734 return;
3735 }
3736
3737 if (dst_width <= 0 || dst_height <= 0) {
3738 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003739 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003740 "destination dimensions must be positive (%dx%d)",
3741 dst_width, dst_height);
3742 return;
3743 }
3744
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003745 surface->pending.buffer_viewport.buffer.src_x = src_x;
3746 surface->pending.buffer_viewport.buffer.src_y = src_y;
3747 surface->pending.buffer_viewport.buffer.src_width = src_width;
3748 surface->pending.buffer_viewport.buffer.src_height = src_height;
3749 surface->pending.buffer_viewport.surface.width = dst_width;
3750 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003751 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003752}
3753
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003754static void
3755viewport_set_source(struct wl_client *client,
3756 struct wl_resource *resource,
3757 wl_fixed_t src_x,
3758 wl_fixed_t src_y,
3759 wl_fixed_t src_width,
3760 wl_fixed_t src_height)
3761{
3762 struct weston_surface *surface =
3763 wl_resource_get_user_data(resource);
3764
3765 assert(surface->viewport_resource != NULL);
3766
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003767 if (src_width == wl_fixed_from_int(-1) &&
3768 src_height == wl_fixed_from_int(-1)) {
3769 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003770 surface->pending.buffer_viewport.buffer.src_width =
3771 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003772 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003773 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003774 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003775
3776 if (src_width <= 0 || src_height <= 0) {
3777 wl_resource_post_error(resource,
3778 WL_VIEWPORT_ERROR_BAD_VALUE,
3779 "source size must be positive (%fx%f)",
3780 wl_fixed_to_double(src_width),
3781 wl_fixed_to_double(src_height));
3782 return;
3783 }
3784
3785 surface->pending.buffer_viewport.buffer.src_x = src_x;
3786 surface->pending.buffer_viewport.buffer.src_y = src_y;
3787 surface->pending.buffer_viewport.buffer.src_width = src_width;
3788 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003789 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003790}
3791
3792static void
3793viewport_set_destination(struct wl_client *client,
3794 struct wl_resource *resource,
3795 int32_t dst_width,
3796 int32_t dst_height)
3797{
3798 struct weston_surface *surface =
3799 wl_resource_get_user_data(resource);
3800
3801 assert(surface->viewport_resource != NULL);
3802
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003803 if (dst_width == -1 && dst_height == -1) {
3804 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003805 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003806 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003807 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003808 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003809
3810 if (dst_width <= 0 || dst_height <= 0) {
3811 wl_resource_post_error(resource,
3812 WL_VIEWPORT_ERROR_BAD_VALUE,
3813 "destination size must be positive (%dx%d)",
3814 dst_width, dst_height);
3815 return;
3816 }
3817
3818 surface->pending.buffer_viewport.surface.width = dst_width;
3819 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003820 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003821}
3822
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003823static const struct wl_viewport_interface viewport_interface = {
3824 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003825 viewport_set,
3826 viewport_set_source,
3827 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003828};
3829
3830static void
3831scaler_destroy(struct wl_client *client,
3832 struct wl_resource *resource)
3833{
3834 wl_resource_destroy(resource);
3835}
3836
3837static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003838scaler_get_viewport(struct wl_client *client,
3839 struct wl_resource *scaler,
3840 uint32_t id,
3841 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003842{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003843 int version = wl_resource_get_version(scaler);
3844 struct weston_surface *surface =
3845 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003846 struct wl_resource *resource;
3847
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003848 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003849 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003850 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3851 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003852 return;
3853 }
3854
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003855 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003856 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003857 if (resource == NULL) {
3858 wl_client_post_no_memory(client);
3859 return;
3860 }
3861
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003862 wl_resource_set_implementation(resource, &viewport_interface,
3863 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003864
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003865 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003866}
3867
3868static const struct wl_scaler_interface scaler_interface = {
3869 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003870 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003871};
3872
3873static void
3874bind_scaler(struct wl_client *client,
3875 void *data, uint32_t version, uint32_t id)
3876{
3877 struct wl_resource *resource;
3878
3879 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003880 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003881 if (resource == NULL) {
3882 wl_client_post_no_memory(client);
3883 return;
3884 }
3885
3886 wl_resource_set_implementation(resource, &scaler_interface,
3887 NULL, NULL);
3888}
3889
3890static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003891destroy_presentation_feedback(struct wl_resource *feedback_resource)
3892{
3893 struct weston_presentation_feedback *feedback;
3894
3895 feedback = wl_resource_get_user_data(feedback_resource);
3896
3897 wl_list_remove(&feedback->link);
3898 free(feedback);
3899}
3900
3901static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003902presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3903{
3904 wl_resource_destroy(resource);
3905}
3906
3907static void
3908presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003909 struct wl_resource *presentation_resource,
3910 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003911 uint32_t callback)
3912{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003913 struct weston_surface *surface;
3914 struct weston_presentation_feedback *feedback;
3915
3916 surface = wl_resource_get_user_data(surface_resource);
3917
3918 feedback = calloc(1, sizeof *feedback);
3919 if (!feedback)
3920 goto err_calloc;
3921
3922 feedback->resource = wl_resource_create(client,
3923 &presentation_feedback_interface,
3924 1, callback);
3925 if (!feedback->resource)
3926 goto err_create;
3927
3928 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3929 destroy_presentation_feedback);
3930
3931 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3932
3933 return;
3934
3935err_create:
3936 free(feedback);
3937
3938err_calloc:
3939 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003940}
3941
3942static const struct presentation_interface presentation_implementation = {
3943 presentation_destroy,
3944 presentation_feedback
3945};
3946
3947static void
3948bind_presentation(struct wl_client *client,
3949 void *data, uint32_t version, uint32_t id)
3950{
3951 struct weston_compositor *compositor = data;
3952 struct wl_resource *resource;
3953
3954 resource = wl_resource_create(client, &presentation_interface,
3955 MIN(version, 1), id);
3956 if (resource == NULL) {
3957 wl_client_post_no_memory(client);
3958 return;
3959 }
3960
3961 wl_resource_set_implementation(resource, &presentation_implementation,
3962 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003963 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003964}
3965
3966static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003967compositor_bind(struct wl_client *client,
3968 void *data, uint32_t version, uint32_t id)
3969{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003970 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003971 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003972
Jason Ekstranda85118c2013-06-27 20:17:02 -05003973 resource = wl_resource_create(client, &wl_compositor_interface,
3974 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003975 if (resource == NULL) {
3976 wl_client_post_no_memory(client);
3977 return;
3978 }
3979
3980 wl_resource_set_implementation(resource, &compositor_interface,
3981 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003982}
3983
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003984static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003985log_uname(void)
3986{
3987 struct utsname usys;
3988
3989 uname(&usys);
3990
3991 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3992 usys.version, usys.machine);
3993}
3994
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003995WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003996weston_environment_get_fd(const char *env)
3997{
3998 char *e, *end;
3999 int fd, flags;
4000
4001 e = getenv(env);
4002 if (!e)
4003 return -1;
4004 fd = strtol(e, &end, 0);
4005 if (*end != '\0')
4006 return -1;
4007
4008 flags = fcntl(fd, F_GETFD);
4009 if (flags == -1)
4010 return -1;
4011
4012 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4013 unsetenv(env);
4014
4015 return fd;
4016}
4017
4018WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004019weston_compositor_init(struct weston_compositor *ec,
4020 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004021 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004022 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004023{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004024 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004025 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004026 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004027
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004028 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004029 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004030 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004031 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004032 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004033 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004034 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004035 wl_signal_init(&ec->idle_signal);
4036 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004037 wl_signal_init(&ec->show_input_panel_signal);
4038 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004039 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004040 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004041 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004042 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004043 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004044 wl_signal_init(&ec->session_signal);
4045 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004046
Casey Dahlin58ba1372012-04-19 22:50:08 -04004047 ec->output_id_pool = 0;
4048
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004049 if (!wl_global_create(display, &wl_compositor_interface, 3,
4050 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004051 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004052
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004053 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4054 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004055 return -1;
4056
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004057 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004058 ec, bind_scaler))
4059 return -1;
4060
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004061 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4062 ec, bind_presentation))
4063 return -1;
4064
Jason Ekstranda7af7042013-10-12 22:38:11 -05004065 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004066 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004067 wl_list_init(&ec->layer_list);
4068 wl_list_init(&ec->seat_list);
4069 wl_list_init(&ec->output_list);
4070 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004071 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004072 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004073 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004074 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004075 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004076
Xiong Zhang97116532013-10-23 13:58:31 +08004077 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004078 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004079
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004080 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4081 weston_config_section_get_string(s, "keymap_rules",
4082 (char **) &xkb_names.rules, NULL);
4083 weston_config_section_get_string(s, "keymap_model",
4084 (char **) &xkb_names.model, NULL);
4085 weston_config_section_get_string(s, "keymap_layout",
4086 (char **) &xkb_names.layout, NULL);
4087 weston_config_section_get_string(s, "keymap_variant",
4088 (char **) &xkb_names.variant, NULL);
4089 weston_config_section_get_string(s, "keymap_options",
4090 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004091
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004092 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4093 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004094
Jonny Lamb66a41a02014-08-12 14:58:25 +02004095 weston_config_section_get_int(s, "repeat-rate",
4096 &ec->kb_repeat_rate, 40);
4097 weston_config_section_get_int(s, "repeat-delay",
4098 &ec->kb_repeat_delay, 400);
4099
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004100 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004101
4102 wl_data_device_manager_init(ec->wl_display);
4103
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004104 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004105
Daniel Stone725c2c32012-06-22 14:04:36 +01004106 loop = wl_display_get_event_loop(ec->wl_display);
4107 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4108 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4109
4110 ec->input_loop = wl_event_loop_create();
4111
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004112 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4113 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4114
4115 weston_compositor_schedule_repaint(ec);
4116
Daniel Stone725c2c32012-06-22 14:04:36 +01004117 return 0;
4118}
4119
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004120WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004121weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004122{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004123 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004124
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004125 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004126 if (ec->input_loop_source)
4127 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004128
Matt Roper361d2ad2011-08-29 13:52:23 -07004129 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004130 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004131 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004132
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004133 if (ec->renderer)
4134 ec->renderer->destroy(ec);
4135
Daniel Stone325fc2d2012-05-30 16:31:58 +01004136 weston_binding_list_destroy_all(&ec->key_binding_list);
4137 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004138 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004139 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004140 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004141
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004142 weston_plane_release(&ec->primary_plane);
4143
Jonas Ådahlc97af922012-03-28 22:36:09 +02004144 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004145
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004146 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004147}
4148
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004149WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004150weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4151 const struct weston_pointer_grab_interface *interface)
4152{
4153 struct weston_seat *seat;
4154
4155 ec->default_pointer_grab = interface;
4156 wl_list_for_each(seat, &ec->seat_list, link) {
4157 if (seat->pointer) {
4158 weston_pointer_set_default_grab(seat->pointer,
4159 interface);
4160 }
4161 }
4162}
4163
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004164WL_EXPORT int
4165weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4166 clockid_t clk_id)
4167{
4168 struct timespec ts;
4169
4170 if (clock_gettime(clk_id, &ts) < 0)
4171 return -1;
4172
4173 compositor->presentation_clock = clk_id;
4174
4175 return 0;
4176}
4177
4178/*
4179 * For choosing the software clock, when the display hardware or API
4180 * does not expose a compatible presentation timestamp.
4181 */
4182WL_EXPORT int
4183weston_compositor_set_presentation_clock_software(
4184 struct weston_compositor *compositor)
4185{
4186 /* In order of preference */
4187 static const clockid_t clocks[] = {
4188 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4189 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4190 CLOCK_MONOTONIC, /* no jumps, may crawl */
4191 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4192 CLOCK_REALTIME /* may jump and crawl */
4193 };
4194 unsigned i;
4195
4196 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4197 if (weston_compositor_set_presentation_clock(compositor,
4198 clocks[i]) == 0)
4199 return 0;
4200
4201 weston_log("Error: no suitable presentation clock available.\n");
4202
4203 return -1;
4204}
4205
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004206WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004207weston_version(int *major, int *minor, int *micro)
4208{
4209 *major = WESTON_VERSION_MAJOR;
4210 *minor = WESTON_VERSION_MINOR;
4211 *micro = WESTON_VERSION_MICRO;
4212}
4213
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004214static const char *
4215clock_name(clockid_t clk_id)
4216{
4217 static const char *names[] = {
4218 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4219 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4220 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4221 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4222 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4223 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4224 };
4225
4226 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4227 return "unknown";
4228
4229 return names[clk_id];
4230}
4231
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004232static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004233 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004234 const char *desc;
4235} capability_strings[] = {
4236 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004237 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004238};
4239
4240static void
4241weston_compositor_log_capabilities(struct weston_compositor *compositor)
4242{
4243 unsigned i;
4244 int yes;
4245
4246 weston_log("Compositor capabilities:\n");
4247 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4248 yes = compositor->capabilities & capability_strings[i].bit;
4249 weston_log_continue(STAMP_SPACE "%s %s\n",
4250 capability_strings[i].desc,
4251 yes ? "yes" : "no");
4252 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004253
4254 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4255 clock_name(compositor->presentation_clock),
4256 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004257}
4258
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004259static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004260{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004261 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004262
Martin Minarik6d118362012-06-07 18:01:59 +02004263 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004264 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004265
4266 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004267}
4268
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004269#ifdef HAVE_LIBUNWIND
4270
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004271static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004272print_backtrace(void)
4273{
4274 unw_cursor_t cursor;
4275 unw_context_t context;
4276 unw_word_t off;
4277 unw_proc_info_t pip;
4278 int ret, i = 0;
4279 char procname[256];
4280 const char *filename;
4281 Dl_info dlinfo;
4282
4283 pip.unwind_info = NULL;
4284 ret = unw_getcontext(&context);
4285 if (ret) {
4286 weston_log("unw_getcontext: %d\n", ret);
4287 return;
4288 }
4289
4290 ret = unw_init_local(&cursor, &context);
4291 if (ret) {
4292 weston_log("unw_init_local: %d\n", ret);
4293 return;
4294 }
4295
4296 ret = unw_step(&cursor);
4297 while (ret > 0) {
4298 ret = unw_get_proc_info(&cursor, &pip);
4299 if (ret) {
4300 weston_log("unw_get_proc_info: %d\n", ret);
4301 break;
4302 }
4303
4304 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4305 if (ret && ret != -UNW_ENOMEM) {
4306 if (ret != -UNW_EUNSPEC)
4307 weston_log("unw_get_proc_name: %d\n", ret);
4308 procname[0] = '?';
4309 procname[1] = 0;
4310 }
4311
4312 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4313 *dlinfo.dli_fname)
4314 filename = dlinfo.dli_fname;
4315 else
4316 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004317
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004318 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4319 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4320
4321 ret = unw_step(&cursor);
4322 if (ret < 0)
4323 weston_log("unw_step: %d\n", ret);
4324 }
4325}
4326
4327#else
4328
4329static void
4330print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004331{
4332 void *buffer[32];
4333 int i, count;
4334 Dl_info info;
4335
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004336 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4337 for (i = 0; i < count; i++) {
4338 dladdr(buffer[i], &info);
4339 weston_log(" [%016lx] %s (%s)\n",
4340 (long) buffer[i],
4341 info.dli_sname ? info.dli_sname : "--",
4342 info.dli_fname);
4343 }
4344}
4345
4346#endif
4347
4348static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004349on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004350{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004351 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004352 * then call the backend restore function, which will switch
4353 * back to the vt we launched from or ungrab X etc and then
4354 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004355 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004356 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004357 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004358
Peter Maatmane5b42e42013-03-27 22:38:53 +01004359 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004360
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004361 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004362
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004363 segv_compositor->restore(segv_compositor);
4364
4365 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004366}
4367
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004368WL_EXPORT void *
4369weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004370{
4371 char path[PATH_MAX];
4372 void *module, *init;
4373
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004374 if (name == NULL)
4375 return NULL;
4376
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004377 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004378 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004379 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004380 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004381
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004382 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4383 if (module) {
4384 weston_log("Module '%s' already loaded\n", path);
4385 dlclose(module);
4386 return NULL;
4387 }
4388
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004389 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004390 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004391 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004392 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004393 return NULL;
4394 }
4395
4396 init = dlsym(module, entrypoint);
4397 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004398 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004399 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004400 return NULL;
4401 }
4402
4403 return init;
4404}
4405
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004406static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004407load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004408 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004409{
4410 const char *p, *end;
4411 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004412 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004413 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004414
4415 if (modules == NULL)
4416 return 0;
4417
4418 p = modules;
4419 while (*p) {
4420 end = strchrnul(p, ',');
4421 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004422 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004423 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004424 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004425 p = end;
4426 while (*p == ',')
4427 p++;
4428
4429 }
4430
4431 return 0;
4432}
4433
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004434static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004435 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4436
4437static const char xdg_wrong_message[] =
4438 "fatal: environment variable XDG_RUNTIME_DIR\n"
4439 "is set to \"%s\", which is not a directory.\n";
4440
4441static const char xdg_wrong_mode_message[] =
4442 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004443 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4444 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004445
4446static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004447 "Refer to your distribution on how to get it, or\n"
4448 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4449 "on how to implement it.\n";
4450
Martin Minarik37032f82012-06-18 20:15:18 +02004451static void
4452verify_xdg_runtime_dir(void)
4453{
4454 char *dir = getenv("XDG_RUNTIME_DIR");
4455 struct stat s;
4456
4457 if (!dir) {
4458 weston_log(xdg_error_message);
4459 weston_log_continue(xdg_detail_message);
4460 exit(EXIT_FAILURE);
4461 }
4462
4463 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4464 weston_log(xdg_wrong_message, dir);
4465 weston_log_continue(xdg_detail_message);
4466 exit(EXIT_FAILURE);
4467 }
4468
4469 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4470 weston_log(xdg_wrong_mode_message,
4471 dir, s.st_mode & 0777, s.st_uid);
4472 weston_log_continue(xdg_detail_message);
4473 }
4474}
4475
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004476static int
4477usage(int error_code)
4478{
4479 fprintf(stderr,
4480 "Usage: weston [OPTIONS]\n\n"
4481 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4482 "Weston supports multiple backends, and depending on which backend is in use\n"
4483 "different options will be accepted.\n\n"
4484
4485
4486 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004487 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004488 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004489 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4490 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004491 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004492 " -S, --socket=NAME\tName of socket to listen on\n"
4493 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004494 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004495 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004496 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004497 " -h, --help\t\tThis help message\n\n");
4498
4499 fprintf(stderr,
4500 "Options for drm-backend.so:\n\n"
4501 " --connector=ID\tBring up only this connector\n"
4502 " --seat=SEAT\t\tThe seat that weston should run on\n"
4503 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004504 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004505 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4506
4507 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004508 "Options for fbdev-backend.so:\n\n"
4509 " --tty=TTY\t\tThe tty to use\n"
4510 " --device=DEVICE\tThe framebuffer device to use\n\n");
4511
4512 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004513 "Options for x11-backend.so:\n\n"
4514 " --width=WIDTH\t\tWidth of X window\n"
4515 " --height=HEIGHT\tHeight of X window\n"
4516 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004517 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004518 " --output-count=COUNT\tCreate multiple outputs\n"
4519 " --no-input\t\tDont create input devices\n\n");
4520
4521 fprintf(stderr,
4522 "Options for wayland-backend.so:\n\n"
4523 " --width=WIDTH\t\tWidth of Wayland surface\n"
4524 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004525 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004526 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004527 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004528 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004529 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004530 " --display=DISPLAY\tWayland display to connect to\n\n");
4531
Pekka Paalanene31e0532013-05-22 18:03:07 +03004532#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4533 fprintf(stderr,
4534 "Options for rpi-backend.so:\n\n"
4535 " --tty=TTY\t\tThe tty to use\n"
4536 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4537 " --transform=TR\tThe output transformation, TR is one of:\n"
4538 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004539 " --opaque-regions\tEnable support for opaque regions, can be "
4540 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004541 "\n");
4542#endif
4543
Hardeningc077a842013-07-08 00:51:35 +02004544#if defined(BUILD_RDP_COMPOSITOR)
4545 fprintf(stderr,
4546 "Options for rdp-backend.so:\n\n"
4547 " --width=WIDTH\t\tWidth of desktop\n"
4548 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004549 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4550 " --address=ADDR\tThe address to bind\n"
4551 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004552 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004553 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4554 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4555 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4556 "\n");
4557#endif
4558
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004559 exit(error_code);
4560}
4561
Peter Maatmane5b42e42013-03-27 22:38:53 +01004562static void
4563catch_signals(void)
4564{
4565 struct sigaction action;
4566
4567 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4568 action.sa_sigaction = on_caught_signal;
4569 sigemptyset(&action.sa_mask);
4570 sigaction(SIGSEGV, &action, NULL);
4571 sigaction(SIGABRT, &action, NULL);
4572}
4573
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004574static void
4575handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4576{
4577 struct wl_client *client = data;
4578
4579 weston_log("Primary client died. Closing...\n");
4580
4581 wl_display_terminate(wl_client_get_display(client));
4582}
4583
Ryo Munakatad8deff62014-09-06 07:32:05 +09004584static char *
4585weston_choose_default_backend(void)
4586{
4587 char *backend = NULL;
4588
4589 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4590 backend = strdup("wayland-backend.so");
4591 else if (getenv("DISPLAY"))
4592 backend = strdup("x11-backend.so");
4593 else
4594 backend = strdup(WESTON_NATIVE_BACKEND);
4595
4596 return backend;
4597}
4598
4599static int
4600weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4601{
4602 if (socket_name) {
4603 if (wl_display_add_socket(display, socket_name)) {
4604 weston_log("fatal: failed to add socket: %m\n");
4605 return -1;
4606 }
4607 } else {
4608 socket_name = wl_display_add_socket_auto(display);
4609 if (!socket_name) {
4610 weston_log("fatal: failed to add socket: %m\n");
4611 return -1;
4612 }
4613 }
4614
4615 setenv("WAYLAND_DISPLAY", socket_name, 1);
4616
4617 return 0;
4618}
4619
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004620int main(int argc, char *argv[])
4621{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004622 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004623 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004624 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004625 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004626 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004627 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004628 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004629 int *argc, char *argv[],
4630 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004631 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004632 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004633 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004634 char *modules = NULL;
4635 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004636 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004637 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004638 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004639 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004640 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004641 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004642 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004643 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004644 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004645 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004646 struct wl_client *primary_client;
4647 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004648 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004649
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004650 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004651 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4652 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004653 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4654 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004655 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004656 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004657 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004658 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004659 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004660 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004661
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004662 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004663
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004664 if (help)
4665 usage(EXIT_SUCCESS);
4666
Scott Moreau12245142012-08-29 15:15:58 -06004667 if (version) {
4668 printf(PACKAGE_STRING "\n");
4669 return EXIT_SUCCESS;
4670 }
4671
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004672 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004673
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004674 weston_log("%s\n"
4675 STAMP_SPACE "%s\n"
4676 STAMP_SPACE "Bug reports to: %s\n"
4677 STAMP_SPACE "Build: %s\n",
4678 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004679 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004680 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004681
Martin Minarik37032f82012-06-18 20:15:18 +02004682 verify_xdg_runtime_dir();
4683
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004684 display = wl_display_create();
4685
Tiago Vignatti2116b892011-08-08 05:52:59 -07004686 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004687 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4688 display);
4689 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4690 display);
4691 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4692 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004693
4694 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004695 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4696 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004697
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304698 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4699 ret = EXIT_FAILURE;
4700 goto out_signals;
4701 }
4702
Pekka Paalanen588bee12014-05-07 16:26:25 +03004703 if (noconfig == 0)
4704 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004705 if (config != NULL) {
4706 weston_log("Using config file '%s'\n",
4707 weston_config_get_full_path(config));
4708 } else {
4709 weston_log("Starting with no config file.\n");
4710 }
4711 section = weston_config_get_section(config, "core", NULL, NULL);
4712
Ryo Munakata03faed22014-09-06 07:32:51 +09004713 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004714 weston_config_section_get_string(section, "backend", &backend,
4715 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004716 if (!backend)
4717 backend = weston_choose_default_backend();
4718 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004719
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004720 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304721 if (!backend_init) {
4722 ret = EXIT_FAILURE;
4723 goto out_signals;
4724 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004725
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004726 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004727 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004728 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304729 ret = EXIT_FAILURE;
4730 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004731 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004732
Peter Maatmane5b42e42013-03-27 22:38:53 +01004733 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004734 segv_compositor = ec;
4735
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004736 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004737 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004738
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004739 for (i = 1; i < argc; i++)
4740 weston_log("fatal: unhandled option: %s\n", argv[i]);
4741 if (argc > 1) {
4742 ret = EXIT_FAILURE;
4743 goto out;
4744 }
4745
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004746 weston_compositor_log_capabilities(ec);
4747
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004748 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4749 if (server_socket) {
4750 weston_log("Running with single client\n");
4751 fd = strtol(server_socket, &end, 0);
4752 if (*end != '\0')
4753 fd = -1;
4754 } else {
4755 fd = -1;
4756 }
4757
4758 if (fd != -1) {
4759 primary_client = wl_client_create(display, fd);
4760 if (!primary_client) {
4761 weston_log("fatal: failed to add client: %m\n");
4762 ret = EXIT_FAILURE;
4763 goto out;
4764 }
4765 primary_client_destroyed.notify =
4766 handle_primary_client_destroyed;
4767 wl_client_add_destroy_listener(primary_client,
4768 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004769 } else if (weston_create_listening_socket(display, socket_name)) {
4770 ret = EXIT_FAILURE;
4771 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004772 }
4773
Ryo Munakata03faed22014-09-06 07:32:51 +09004774 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004775 weston_config_section_get_string(section, "shell", &shell,
4776 "desktop-shell.so");
4777
Ryo Munakata03faed22014-09-06 07:32:51 +09004778 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004779 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004780
4781 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004782 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004783 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004784
4785 if (load_modules(ec, option_modules, &argc, argv) < 0)
4786 goto out;
4787
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004788 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4789 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4790 if (numlock_on) {
4791 wl_list_for_each(seat, &ec->seat_list, link) {
4792 if (seat->keyboard)
4793 weston_keyboard_set_locks(seat->keyboard,
4794 WESTON_NUM_LOCK,
4795 WESTON_NUM_LOCK);
4796 }
4797 }
4798
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004799 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004800
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004801 wl_display_run(display);
4802
Ryo Munakata03faed22014-09-06 07:32:51 +09004803out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004804 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004805 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004806
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004807 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004808
Daniel Stone855028f2012-05-01 20:37:10 +01004809 weston_compositor_xkb_destroy(ec);
4810
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004811 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304812
4813out_signals:
4814 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4815 if (signals[i])
4816 wl_event_source_remove(signals[i]);
4817
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004818 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004819
Martin Minarik19e6f262012-06-07 13:08:46 +02004820 weston_log_file_close();
4821
Ryo Munakata03faed22014-09-06 07:32:51 +09004822 free(backend);
4823 free(shell);
4824 free(socket_name);
4825 free(option_modules);
4826 free(log);
4827 free(modules);
4828
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004829 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004830}