blob: 9dcabe3f30a001ee8f685d9c7137f7eb483c3939 [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;
Derek Foreman41bdc272014-11-05 13:26:57 -0600108 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200109
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200110 pixman_region32_init(&old_output_region);
111 pixman_region32_copy(&old_output_region, &output->region);
112
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200114 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115
116 pixman_region32_init(&output->previous_damage);
117 pixman_region32_init_rect(&output->region, output->x, output->y,
118 output->width, output->height);
119
120 weston_output_update_matrix(output);
121
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200122 /* If a pointer falls outside the outputs new geometry, move it to its
123 * lower-right corner */
124 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400125 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 int32_t x, y;
127
128 if (!pointer)
129 continue;
130
131 x = wl_fixed_to_int(pointer->x);
132 y = wl_fixed_to_int(pointer->y);
133
134 if (!pixman_region32_contains_point(&old_output_region,
135 x, y, NULL) ||
136 pixman_region32_contains_point(&output->region,
137 x, y, NULL))
138 continue;
139
140 if (x >= output->x + output->width)
141 x = output->x + output->width - 1;
142 if (y >= output->y + output->height)
143 y = output->y + output->height - 1;
144
145 pointer->x = wl_fixed_from_int(x);
146 pointer->y = wl_fixed_from_int(y);
147 }
148
149 pixman_region32_fini(&old_output_region);
150
Hardening57388e42013-09-18 23:56:36 +0200151 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600152 wl_resource_for_each(resource, &output->resource_list) {
153 if (mode_changed) {
154 wl_output_send_mode(resource,
155 output->current_mode->flags,
156 output->current_mode->width,
157 output->current_mode->height,
158 output->current_mode->refresh);
159 }
Hardening57388e42013-09-18 23:56:36 +0200160
Derek Foreman41bdc272014-11-05 13:26:57 -0600161 version = wl_resource_get_version(resource);
162 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600163 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200164
Derek Foreman41bdc272014-11-05 13:26:57 -0600165 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
166 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600167 }
168}
169
170WL_EXPORT int
171weston_output_mode_set_native(struct weston_output *output,
172 struct weston_mode *mode,
173 int32_t scale)
174{
175 int ret;
176 int mode_changed = 0, scale_changed = 0;
177
178 if (!output->switch_mode)
179 return -1;
180
181 if (!output->original_mode) {
182 mode_changed = 1;
183 ret = output->switch_mode(output, mode);
184 if (ret < 0)
185 return ret;
186 if (output->current_scale != scale) {
187 scale_changed = 1;
188 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200189 }
190 }
191
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600192 output->native_mode = mode;
193 output->native_scale = scale;
194
195 weston_mode_switch_finish(output, mode_changed, scale_changed);
196
197 return 0;
198}
199
200WL_EXPORT int
201weston_output_mode_switch_to_native(struct weston_output *output)
202{
203 int ret;
204 int mode_changed = 0, scale_changed = 0;
205
206 if (!output->switch_mode)
207 return -1;
208
209 if (!output->original_mode) {
210 weston_log("already in the native mode\n");
211 return -1;
212 }
213 /* the non fullscreen clients haven't seen a mode set since we
214 * switched into a temporary, so we need to notify them if the
215 * mode at that time is different from the native mode now.
216 */
217 mode_changed = (output->original_mode != output->native_mode);
218 scale_changed = (output->original_scale != output->native_scale);
219
220 ret = output->switch_mode(output, output->native_mode);
221 if (ret < 0)
222 return ret;
223
224 output->current_scale = output->native_scale;
225
226 output->original_mode = NULL;
227 output->original_scale = 0;
228
229 weston_mode_switch_finish(output, mode_changed, scale_changed);
230
231 return 0;
232}
233
234WL_EXPORT int
235weston_output_mode_switch_to_temporary(struct weston_output *output,
236 struct weston_mode *mode,
237 int32_t scale)
238{
239 int ret;
240
241 if (!output->switch_mode)
242 return -1;
243
244 /* original_mode is the last mode non full screen clients have seen,
245 * so we shouldn't change it if we already have one set.
246 */
247 if (!output->original_mode) {
248 output->original_mode = output->native_mode;
249 output->original_scale = output->native_scale;
250 }
251 ret = output->switch_mode(output, mode);
252 if (ret < 0)
253 return ret;
254
255 output->current_scale = scale;
256
257 weston_mode_switch_finish(output, 0, 0);
258
259 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800260}
261
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400262WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500263weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400264{
265 wl_list_insert(&child_process_list, &process->link);
266}
267
Benjamin Franzke06286262011-05-06 19:12:33 +0200268static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200269child_client_exec(int sockfd, const char *path)
270{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500271 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200272 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200273 sigset_t allsigs;
274
275 /* do not give our signal mask to the new process */
276 sigfillset(&allsigs);
277 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200278
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100279 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
280 if (seteuid(getuid()) == -1) {
281 weston_log("compositor: failed seteuid\n");
282 return;
283 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500284
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500285 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
286 * non-CLOEXEC fd to pass through exec. */
287 clientfd = dup(sockfd);
288 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200289 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500290 return;
291 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200292
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500293 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200294 setenv("WAYLAND_SOCKET", s, 1);
295
296 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200298 path);
299}
300
301WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500302weston_client_launch(struct weston_compositor *compositor,
303 struct weston_process *proc,
304 const char *path,
305 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200306{
307 int sv[2];
308 pid_t pid;
309 struct wl_client *client;
310
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300311 weston_log("launching '%s'\n", path);
312
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300313 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200314 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200315 "socketpair failed while launching '%s': %m\n",
316 path);
317 return NULL;
318 }
319
320 pid = fork();
321 if (pid == -1) {
322 close(sv[0]);
323 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200324 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200325 "fork failed while launching '%s': %m\n", path);
326 return NULL;
327 }
328
329 if (pid == 0) {
330 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700331 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200332 }
333
334 close(sv[1]);
335
336 client = wl_client_create(compositor->wl_display, sv[0]);
337 if (!client) {
338 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200339 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200340 "wl_client_create failed while launching '%s'.\n",
341 path);
342 return NULL;
343 }
344
345 proc->pid = pid;
346 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500347 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200348
349 return client;
350}
351
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300352struct process_info {
353 struct weston_process proc;
354 char *path;
355};
356
357static void
358process_handle_sigchld(struct weston_process *process, int status)
359{
360 struct process_info *pinfo =
361 container_of(process, struct process_info, proc);
362
363 /*
364 * There are no guarantees whether this runs before or after
365 * the wl_client destructor.
366 */
367
368 if (WIFEXITED(status)) {
369 weston_log("%s exited with status %d\n", pinfo->path,
370 WEXITSTATUS(status));
371 } else if (WIFSIGNALED(status)) {
372 weston_log("%s died on signal %d\n", pinfo->path,
373 WTERMSIG(status));
374 } else {
375 weston_log("%s disappeared\n", pinfo->path);
376 }
377
378 free(pinfo->path);
379 free(pinfo);
380}
381
382WL_EXPORT struct wl_client *
383weston_client_start(struct weston_compositor *compositor, const char *path)
384{
385 struct process_info *pinfo;
386 struct wl_client *client;
387
388 pinfo = zalloc(sizeof *pinfo);
389 if (!pinfo)
390 return NULL;
391
392 pinfo->path = strdup(path);
393 if (!pinfo->path)
394 goto out_free;
395
396 client = weston_client_launch(compositor, &pinfo->proc, path,
397 process_handle_sigchld);
398 if (!client)
399 goto out_str;
400
401 return client;
402
403out_str:
404 free(pinfo->path);
405
406out_free:
407 free(pinfo);
408
409 return NULL;
410}
411
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200412static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300413region_init_infinite(pixman_region32_t *region)
414{
415 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
416 UINT32_MAX, UINT32_MAX);
417}
418
Pekka Paalanene67858b2013-04-25 13:57:42 +0300419static struct weston_subsurface *
420weston_surface_to_subsurface(struct weston_surface *surface);
421
Jason Ekstranda7af7042013-10-12 22:38:11 -0500422WL_EXPORT struct weston_view *
423weston_view_create(struct weston_surface *surface)
424{
425 struct weston_view *view;
426
427 view = calloc(1, sizeof *view);
428 if (view == NULL)
429 return NULL;
430
431 view->surface = surface;
432
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433 /* Assign to surface */
434 wl_list_insert(&surface->views, &view->surface_link);
435
436 wl_signal_init(&view->destroy_signal);
437 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300438 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500439
Xiong Zhang97116532013-10-23 13:58:31 +0800440 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300441 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300442 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443
444 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300445 pixman_region32_init(&view->transform.masked_boundingbox);
446 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500447
448 view->alpha = 1.0;
449 pixman_region32_init(&view->transform.opaque);
450
451 wl_list_init(&view->geometry.transformation_list);
452 wl_list_insert(&view->geometry.transformation_list,
453 &view->transform.position.link);
454 weston_matrix_init(&view->transform.position.matrix);
455 wl_list_init(&view->geometry.child_list);
456 pixman_region32_init(&view->transform.boundingbox);
457 view->transform.dirty = 1;
458
459 view->output = NULL;
460
461 return view;
462}
463
Jason Ekstrand108865d2014-06-26 10:04:49 -0700464struct weston_frame_callback {
465 struct wl_resource *resource;
466 struct wl_list link;
467};
468
Pekka Paalanen133e4392014-09-23 22:08:46 -0400469struct weston_presentation_feedback {
470 struct wl_resource *resource;
471
472 /* XXX: could use just wl_resource_get_link() instead */
473 struct wl_list link;
474};
475
476static void
477weston_presentation_feedback_discard(
478 struct weston_presentation_feedback *feedback)
479{
480 presentation_feedback_send_discarded(feedback->resource);
481 wl_resource_destroy(feedback->resource);
482}
483
484static void
485weston_presentation_feedback_discard_list(struct wl_list *list)
486{
487 struct weston_presentation_feedback *feedback, *tmp;
488
489 wl_list_for_each_safe(feedback, tmp, list, link)
490 weston_presentation_feedback_discard(feedback);
491}
492
493static void
494weston_presentation_feedback_present(
495 struct weston_presentation_feedback *feedback,
496 struct weston_output *output,
497 uint32_t refresh_nsec,
498 const struct timespec *ts,
499 uint64_t seq)
500{
501 struct wl_client *client = wl_resource_get_client(feedback->resource);
502 struct wl_resource *o;
503 uint64_t secs;
504 uint32_t flags = 0;
505
506 wl_resource_for_each(o, &output->resource_list) {
507 if (wl_resource_get_client(o) != client)
508 continue;
509
510 presentation_feedback_send_sync_output(feedback->resource, o);
511 }
512
513 secs = ts->tv_sec;
514 presentation_feedback_send_presented(feedback->resource,
515 secs >> 32, secs & 0xffffffff,
516 ts->tv_nsec,
517 refresh_nsec,
518 seq >> 32, seq & 0xffffffff,
519 flags);
520 wl_resource_destroy(feedback->resource);
521}
522
523static void
524weston_presentation_feedback_present_list(struct wl_list *list,
525 struct weston_output *output,
526 uint32_t refresh_nsec,
527 const struct timespec *ts,
528 uint64_t seq)
529{
530 struct weston_presentation_feedback *feedback, *tmp;
531
532 wl_list_for_each_safe(feedback, tmp, list, link)
533 weston_presentation_feedback_present(feedback, output,
534 refresh_nsec, ts, seq);
535}
536
Jason Ekstrand7b982072014-05-20 14:33:03 -0500537static void
538surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
539{
540 struct weston_surface_state *state =
541 container_of(listener, struct weston_surface_state,
542 buffer_destroy_listener);
543
544 state->buffer = NULL;
545}
546
547static void
548weston_surface_state_init(struct weston_surface_state *state)
549{
550 state->newly_attached = 0;
551 state->buffer = NULL;
552 state->buffer_destroy_listener.notify =
553 surface_state_handle_buffer_destroy;
554 state->sx = 0;
555 state->sy = 0;
556
557 pixman_region32_init(&state->damage);
558 pixman_region32_init(&state->opaque);
559 region_init_infinite(&state->input);
560
561 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400562 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500563
564 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
565 state->buffer_viewport.buffer.scale = 1;
566 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
567 state->buffer_viewport.surface.width = -1;
568 state->buffer_viewport.changed = 0;
569}
570
571static void
572weston_surface_state_fini(struct weston_surface_state *state)
573{
574 struct weston_frame_callback *cb, *next;
575
576 wl_list_for_each_safe(cb, next,
577 &state->frame_callback_list, link)
578 wl_resource_destroy(cb->resource);
579
Pekka Paalanen133e4392014-09-23 22:08:46 -0400580 weston_presentation_feedback_discard_list(&state->feedback_list);
581
Jason Ekstrand7b982072014-05-20 14:33:03 -0500582 pixman_region32_fini(&state->input);
583 pixman_region32_fini(&state->opaque);
584 pixman_region32_fini(&state->damage);
585
586 if (state->buffer)
587 wl_list_remove(&state->buffer_destroy_listener.link);
588 state->buffer = NULL;
589}
590
591static void
592weston_surface_state_set_buffer(struct weston_surface_state *state,
593 struct weston_buffer *buffer)
594{
595 if (state->buffer == buffer)
596 return;
597
598 if (state->buffer)
599 wl_list_remove(&state->buffer_destroy_listener.link);
600 state->buffer = buffer;
601 if (state->buffer)
602 wl_signal_add(&state->buffer->destroy_signal,
603 &state->buffer_destroy_listener);
604}
605
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500606WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500607weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500608{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500609 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400610
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200611 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400612 if (surface == NULL)
613 return NULL;
614
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500615 wl_signal_init(&surface->destroy_signal);
616
617 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500618
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500619 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200620 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400621
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200622 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
623 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200624 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
625 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500626
627 weston_surface_state_init(&surface->pending);
628
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400629 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200630
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200631 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100632
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400633 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500634 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300635 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400636
Jason Ekstranda7af7042013-10-12 22:38:11 -0500637 wl_list_init(&surface->views);
638
639 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400640 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500641
Pekka Paalanene67858b2013-04-25 13:57:42 +0300642 wl_list_init(&surface->subsurface_list);
643 wl_list_init(&surface->subsurface_list_pending);
644
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400645 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500646}
647
Alex Wu8811bf92012-02-28 18:07:54 +0800648WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500649weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200650 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500651{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100652 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500653}
654
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400655WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500656weston_view_to_global_float(struct weston_view *view,
657 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200658{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200660 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
661
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663
664 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200665 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700666 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200667 v.f[3]);
668 *x = 0;
669 *y = 0;
670 return;
671 }
672
673 *x = v.f[0] / v.f[3];
674 *y = v.f[1] / v.f[3];
675 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500676 *x = sx + view->geometry.x;
677 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200678 }
679}
680
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500681WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200682weston_transformed_coord(int width, int height,
683 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200684 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685 float sx, float sy, float *bx, float *by)
686{
687 switch (transform) {
688 case WL_OUTPUT_TRANSFORM_NORMAL:
689 default:
690 *bx = sx;
691 *by = sy;
692 break;
693 case WL_OUTPUT_TRANSFORM_FLIPPED:
694 *bx = width - sx;
695 *by = sy;
696 break;
697 case WL_OUTPUT_TRANSFORM_90:
698 *bx = height - sy;
699 *by = sx;
700 break;
701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
702 *bx = height - sy;
703 *by = width - sx;
704 break;
705 case WL_OUTPUT_TRANSFORM_180:
706 *bx = width - sx;
707 *by = height - sy;
708 break;
709 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
710 *bx = sx;
711 *by = height - sy;
712 break;
713 case WL_OUTPUT_TRANSFORM_270:
714 *bx = sy;
715 *by = width - sx;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
718 *bx = sy;
719 *by = sx;
720 break;
721 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200722
723 *bx *= scale;
724 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200725}
726
727WL_EXPORT pixman_box32_t
728weston_transformed_rect(int width, int height,
729 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200730 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200731 pixman_box32_t rect)
732{
733 float x1, x2, y1, y2;
734
735 pixman_box32_t ret;
736
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200739 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200740 rect.x2, rect.y2, &x2, &y2);
741
742 if (x1 <= x2) {
743 ret.x1 = x1;
744 ret.x2 = x2;
745 } else {
746 ret.x1 = x2;
747 ret.x2 = x1;
748 }
749
750 if (y1 <= y2) {
751 ret.y1 = y1;
752 ret.y2 = y2;
753 } else {
754 ret.y1 = y2;
755 ret.y2 = y1;
756 }
757
758 return ret;
759}
760
761WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500762weston_transformed_region(int width, int height,
763 enum wl_output_transform transform,
764 int32_t scale,
765 pixman_region32_t *src, pixman_region32_t *dest)
766{
767 pixman_box32_t *src_rects, *dest_rects;
768 int nrects, i;
769
770 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
771 if (src != dest)
772 pixman_region32_copy(dest, src);
773 return;
774 }
775
776 src_rects = pixman_region32_rectangles(src, &nrects);
777 dest_rects = malloc(nrects * sizeof(*dest_rects));
778 if (!dest_rects)
779 return;
780
781 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
782 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
783 } else {
784 for (i = 0; i < nrects; i++) {
785 switch (transform) {
786 default:
787 case WL_OUTPUT_TRANSFORM_NORMAL:
788 dest_rects[i].x1 = src_rects[i].x1;
789 dest_rects[i].y1 = src_rects[i].y1;
790 dest_rects[i].x2 = src_rects[i].x2;
791 dest_rects[i].y2 = src_rects[i].y2;
792 break;
793 case WL_OUTPUT_TRANSFORM_90:
794 dest_rects[i].x1 = height - src_rects[i].y2;
795 dest_rects[i].y1 = src_rects[i].x1;
796 dest_rects[i].x2 = height - src_rects[i].y1;
797 dest_rects[i].y2 = src_rects[i].x2;
798 break;
799 case WL_OUTPUT_TRANSFORM_180:
800 dest_rects[i].x1 = width - src_rects[i].x2;
801 dest_rects[i].y1 = height - src_rects[i].y2;
802 dest_rects[i].x2 = width - src_rects[i].x1;
803 dest_rects[i].y2 = height - src_rects[i].y1;
804 break;
805 case WL_OUTPUT_TRANSFORM_270:
806 dest_rects[i].x1 = src_rects[i].y1;
807 dest_rects[i].y1 = width - src_rects[i].x2;
808 dest_rects[i].x2 = src_rects[i].y2;
809 dest_rects[i].y2 = width - src_rects[i].x1;
810 break;
811 case WL_OUTPUT_TRANSFORM_FLIPPED:
812 dest_rects[i].x1 = width - src_rects[i].x2;
813 dest_rects[i].y1 = src_rects[i].y1;
814 dest_rects[i].x2 = width - src_rects[i].x1;
815 dest_rects[i].y2 = src_rects[i].y2;
816 break;
817 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
818 dest_rects[i].x1 = height - src_rects[i].y2;
819 dest_rects[i].y1 = width - src_rects[i].x2;
820 dest_rects[i].x2 = height - src_rects[i].y1;
821 dest_rects[i].y2 = width - src_rects[i].x1;
822 break;
823 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
824 dest_rects[i].x1 = src_rects[i].x1;
825 dest_rects[i].y1 = height - src_rects[i].y2;
826 dest_rects[i].x2 = src_rects[i].x2;
827 dest_rects[i].y2 = height - src_rects[i].y1;
828 break;
829 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
830 dest_rects[i].x1 = src_rects[i].y1;
831 dest_rects[i].y1 = src_rects[i].x1;
832 dest_rects[i].x2 = src_rects[i].y2;
833 dest_rects[i].y2 = src_rects[i].x2;
834 break;
835 }
836 }
837 }
838
839 if (scale != 1) {
840 for (i = 0; i < nrects; i++) {
841 dest_rects[i].x1 *= scale;
842 dest_rects[i].x2 *= scale;
843 dest_rects[i].y1 *= scale;
844 dest_rects[i].y2 *= scale;
845 }
846 }
847
848 pixman_region32_clear(dest);
849 pixman_region32_init_rects(dest, dest_rects, nrects);
850 free(dest_rects);
851}
852
Jonny Lamb74130762013-11-26 18:19:46 +0100853static void
854scaler_surface_to_buffer(struct weston_surface *surface,
855 float sx, float sy, float *bx, float *by)
856{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200857 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200858 double src_width, src_height;
859 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
862 if (vp->surface.width == -1) {
863 *bx = sx;
864 *by = sy;
865 return;
866 }
Jonny Lamb74130762013-11-26 18:19:46 +0100867
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200868 src_x = 0.0;
869 src_y = 0.0;
870 src_width = surface->width_from_buffer;
871 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100872 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200873 src_x = wl_fixed_to_double(vp->buffer.src_x);
874 src_y = wl_fixed_to_double(vp->buffer.src_y);
875 src_width = wl_fixed_to_double(vp->buffer.src_width);
876 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100877 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200878
879 *bx = sx * src_width / surface->width + src_x;
880 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100881}
882
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500883WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200884weston_surface_to_buffer_float(struct weston_surface *surface,
885 float sx, float sy, float *bx, float *by)
886{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200887 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
888
Jonny Lamb74130762013-11-26 18:19:46 +0100889 /* first transform coordinates if the scaler is set */
890 scaler_surface_to_buffer(surface, sx, sy, bx, by);
891
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500892 weston_transformed_coord(surface->width_from_buffer,
893 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200894 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100895 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200896}
897
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898WL_EXPORT void
899weston_surface_to_buffer(struct weston_surface *surface,
900 int sx, int sy, int *bx, int *by)
901{
902 float bxf, byf;
903
Jonny Lamb74130762013-11-26 18:19:46 +0100904 weston_surface_to_buffer_float(surface,
905 sx, sy, &bxf, &byf);
906
Alexander Larsson4ea95522013-05-22 14:41:37 +0200907 *bx = floorf(bxf);
908 *by = floorf(byf);
909}
910
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200911WL_EXPORT pixman_box32_t
912weston_surface_to_buffer_rect(struct weston_surface *surface,
913 pixman_box32_t rect)
914{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200915 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100916 float xf, yf;
917
918 /* first transform box coordinates if the scaler is set */
919 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
920 rect.x1 = floorf(xf);
921 rect.y1 = floorf(yf);
922
923 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
924 rect.x2 = floorf(xf);
925 rect.y2 = floorf(yf);
926
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500927 return weston_transformed_rect(surface->width_from_buffer,
928 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200929 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200930 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200931}
932
933WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400935 struct weston_plane *plane)
936{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938 return;
939
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 weston_view_damage_below(view);
941 view->plane = plane;
942 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400943}
944
945WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200947{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500948 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200949
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500950 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300951 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800953 if (view->plane)
954 pixman_region32_union(&view->plane->damage,
955 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500956 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800957 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200958}
959
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400960static void
961weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
962{
963 uint32_t different = es->output_mask ^ mask;
964 uint32_t entered = mask & different;
965 uint32_t left = es->output_mask & different;
966 struct weston_output *output;
967 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500968 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400969
970 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500971 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400972 return;
973 if (different == 0)
974 return;
975
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500976 client = wl_resource_get_client(es->resource);
977
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400978 wl_list_for_each(output, &es->compositor->output_list, link) {
979 if (1 << output->id & different)
980 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500981 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400982 client);
983 if (resource == NULL)
984 continue;
985 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500986 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400987 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500988 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400989 }
990}
991
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200992
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400993static void
994weston_surface_assign_output(struct weston_surface *es)
995{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 struct weston_output *new_output;
997 struct weston_view *view;
998 pixman_region32_t region;
999 uint32_t max, area, mask;
1000 pixman_box32_t *e;
1001
1002 new_output = NULL;
1003 max = 0;
1004 mask = 0;
1005 pixman_region32_init(&region);
1006 wl_list_for_each(view, &es->views, surface_link) {
1007 if (!view->output)
1008 continue;
1009
1010 pixman_region32_intersect(&region, &view->transform.boundingbox,
1011 &view->output->region);
1012
1013 e = pixman_region32_extents(&region);
1014 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1015
1016 mask |= view->output_mask;
1017
1018 if (area >= max) {
1019 new_output = view->output;
1020 max = area;
1021 }
1022 }
1023 pixman_region32_fini(&region);
1024
1025 es->output = new_output;
1026 weston_surface_update_output_mask(es, mask);
1027}
1028
1029static void
1030weston_view_assign_output(struct weston_view *ev)
1031{
1032 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001033 struct weston_output *output, *new_output;
1034 pixman_region32_t region;
1035 uint32_t max, area, mask;
1036 pixman_box32_t *e;
1037
1038 new_output = NULL;
1039 max = 0;
1040 mask = 0;
1041 pixman_region32_init(&region);
1042 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001044 &output->region);
1045
1046 e = pixman_region32_extents(&region);
1047 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1048
1049 if (area > 0)
1050 mask |= 1 << output->id;
1051
1052 if (area >= max) {
1053 new_output = output;
1054 max = area;
1055 }
1056 }
1057 pixman_region32_fini(&region);
1058
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 ev->output = new_output;
1060 ev->output_mask = mask;
1061
1062 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001063}
1064
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001065static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1067 int32_t width, int32_t height,
1068 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001069{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001070 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1071 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001072 int32_t s[4][2] = {
1073 { sx, sy },
1074 { sx, sy + height },
1075 { sx + width, sy },
1076 { sx + width, sy + height }
1077 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001078 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001079 int i;
1080
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001081 if (width == 0 || height == 0) {
1082 /* avoid rounding empty bbox to 1x1 */
1083 pixman_region32_init(bbox);
1084 return;
1085 }
1086
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001087 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001088 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090 if (x < min_x)
1091 min_x = x;
1092 if (x > max_x)
1093 max_x = x;
1094 if (y < min_y)
1095 min_y = y;
1096 if (y > max_y)
1097 max_y = y;
1098 }
1099
Pekka Paalanen219b9822012-02-08 15:38:37 +02001100 int_x = floorf(min_x);
1101 int_y = floorf(min_y);
1102 pixman_region32_init_rect(bbox, int_x, int_y,
1103 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001104}
1105
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001106static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001110
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001111 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 view->geometry.x = roundf(view->geometry.x);
1113 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001114
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001115 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1117 view->transform.position.matrix.d[12] = view->geometry.x;
1118 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001119
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001121
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 view->transform.inverse = view->transform.position.matrix;
1123 view->transform.inverse.d[12] = -view->geometry.x;
1124 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001125
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 pixman_region32_init_rect(&view->transform.boundingbox,
1127 view->geometry.x,
1128 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001129 view->surface->width,
1130 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001131
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 if (view->alpha == 1.0) {
1133 pixman_region32_copy(&view->transform.opaque,
1134 &view->surface->opaque);
1135 pixman_region32_translate(&view->transform.opaque,
1136 view->geometry.x,
1137 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001138 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001139}
1140
1141static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001143{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 struct weston_view *parent = view->geometry.parent;
1145 struct weston_matrix *matrix = &view->transform.matrix;
1146 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001147 struct weston_transform *tform;
1148
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001150
1151 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1153 view->transform.position.matrix.d[12] = view->geometry.x;
1154 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001155
1156 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158 weston_matrix_multiply(matrix, &tform->matrix);
1159
Pekka Paalanen483243f2013-03-08 14:56:50 +02001160 if (parent)
1161 weston_matrix_multiply(matrix, &parent->transform.matrix);
1162
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001163 if (weston_matrix_invert(inverse, matrix) < 0) {
1164 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 weston_log("error: weston_view %p"
1166 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001167 return -1;
1168 }
1169
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001170 view_compute_bbox(view, 0, 0,
1171 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001173
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001174 return 0;
1175}
1176
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001177static struct weston_layer *
1178get_view_layer(struct weston_view *view)
1179{
1180 if (view->parent_view)
1181 return get_view_layer(view->parent_view);
1182 return view->layer_link.layer;
1183}
1184
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001185WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001187{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001189 struct weston_layer *layer;
1190 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001193 return;
1194
Pekka Paalanen483243f2013-03-08 14:56:50 +02001195 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001197
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001201
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 pixman_region32_fini(&view->transform.boundingbox);
1203 pixman_region32_fini(&view->transform.opaque);
1204 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001205
Pekka Paalanencd403622012-01-25 13:37:39 +02001206 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 if (view->geometry.transformation_list.next ==
1208 &view->transform.position.link &&
1209 view->geometry.transformation_list.prev ==
1210 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001211 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001213 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214 if (weston_view_update_transform_enable(view) < 0)
1215 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001216 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001217
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001218 layer = get_view_layer(view);
1219 if (layer) {
1220 pixman_region32_init_with_extents(&mask, &layer->mask);
1221 pixman_region32_intersect(&view->transform.masked_boundingbox,
1222 &view->transform.boundingbox, &mask);
1223 pixman_region32_intersect(&view->transform.masked_opaque,
1224 &view->transform.opaque, &mask);
1225 pixman_region32_fini(&mask);
1226 }
1227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001231
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 wl_signal_emit(&view->surface->compositor->transform_signal,
1233 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001234}
1235
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001236WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001238{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001240
1241 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 * The invariant: if view->geometry.dirty, then all views
1243 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001244 * Corollary: if not parent->geometry.dirty, then all ancestors
1245 * are not dirty.
1246 */
1247
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001249 return;
1250
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001252
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001254 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001256}
1257
1258WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259weston_view_to_global_fixed(struct weston_view *view,
1260 wl_fixed_t vx, wl_fixed_t vy,
1261 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001262{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001263 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001264
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265 weston_view_to_global_float(view,
1266 wl_fixed_to_double(vx),
1267 wl_fixed_to_double(vy),
1268 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001269 *x = wl_fixed_from_double(xf);
1270 *y = wl_fixed_from_double(yf);
1271}
1272
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001273WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274weston_view_from_global_float(struct weston_view *view,
1275 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001276{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001278 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001281
1282 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001283 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001285 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 *vx = 0;
1287 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001288 return;
1289 }
1290
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 *vx = v.f[0] / v.f[3];
1292 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001293 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 *vx = x - view->geometry.x;
1295 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001296 }
1297}
1298
1299WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300weston_view_from_global_fixed(struct weston_view *view,
1301 wl_fixed_t x, wl_fixed_t y,
1302 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001303{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 weston_view_from_global_float(view,
1307 wl_fixed_to_double(x),
1308 wl_fixed_to_double(y),
1309 &vxf, &vyf);
1310 *vx = wl_fixed_from_double(vxf);
1311 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001312}
1313
1314WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315weston_view_from_global(struct weston_view *view,
1316 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001317{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1321 *vx = floorf(vxf);
1322 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001323}
1324
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001325WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001326weston_surface_schedule_repaint(struct weston_surface *surface)
1327{
1328 struct weston_output *output;
1329
1330 wl_list_for_each(output, &surface->compositor->output_list, link)
1331 if (surface->output_mask & (1 << output->id))
1332 weston_output_schedule_repaint(output);
1333}
1334
1335WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336weston_view_schedule_repaint(struct weston_view *view)
1337{
1338 struct weston_output *output;
1339
1340 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1341 if (view->output_mask & (1 << output->id))
1342 weston_output_schedule_repaint(output);
1343}
1344
1345WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001346weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001347{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001348 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 0, 0, surface->width,
1350 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001351
Kristian Høgsberg98238702012-08-03 16:29:12 -04001352 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001353}
1354
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001355WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001357{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001358 if (view->geometry.x == x && view->geometry.y == y)
1359 return;
1360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 view->geometry.x = x;
1362 view->geometry.y = y;
1363 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001364}
1365
Pekka Paalanen483243f2013-03-08 14:56:50 +02001366static void
1367transform_parent_handle_parent_destroy(struct wl_listener *listener,
1368 void *data)
1369{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 struct weston_view *view =
1371 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001372 geometry.parent_destroy_listener);
1373
Jason Ekstranda7af7042013-10-12 22:38:11 -05001374 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001375}
1376
1377WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378weston_view_set_transform_parent(struct weston_view *view,
1379 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001380{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 if (view->geometry.parent) {
1382 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1383 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001384 }
1385
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001387
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001389 transform_parent_handle_parent_destroy;
1390 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001391 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001393 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001395 }
1396
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 weston_view_geometry_dirty(view);
1398}
1399
Derek Foreman280e7dd2014-10-03 13:13:42 -05001400WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401weston_view_is_mapped(struct weston_view *view)
1402{
1403 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001404 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001406 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001407}
1408
Derek Foreman280e7dd2014-10-03 13:13:42 -05001409WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001410weston_surface_is_mapped(struct weston_surface *surface)
1411{
1412 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001413 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001414 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001415 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001416}
1417
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001418static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001419surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001420{
1421 struct weston_view *view;
1422
1423 if (surface->width == width && surface->height == height)
1424 return;
1425
1426 surface->width = width;
1427 surface->height = height;
1428
1429 wl_list_for_each(view, &surface->views, surface_link)
1430 weston_view_geometry_dirty(view);
1431}
1432
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001433WL_EXPORT void
1434weston_surface_set_size(struct weston_surface *surface,
1435 int32_t width, int32_t height)
1436{
1437 assert(!surface->resource);
1438 surface_set_size(surface, width, height);
1439}
1440
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001441static int
1442fixed_round_up_to_int(wl_fixed_t f)
1443{
1444 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1445}
1446
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001447static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001448weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001449{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001450 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001451 int32_t width, height;
1452
1453 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001454 surface->width_from_buffer = 0;
1455 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001456 return;
1457 }
1458
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001459 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001460 case WL_OUTPUT_TRANSFORM_90:
1461 case WL_OUTPUT_TRANSFORM_270:
1462 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1463 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001464 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1465 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001466 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001467 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001468 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1469 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001470 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001471 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001472
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001473 surface->width_from_buffer = width;
1474 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001475}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001476
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001477static void
1478weston_surface_update_size(struct weston_surface *surface)
1479{
1480 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1481 int32_t width, height;
1482
1483 width = surface->width_from_buffer;
1484 height = surface->height_from_buffer;
1485
1486 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001487 surface_set_size(surface,
1488 vp->surface.width, vp->surface.height);
1489 return;
1490 }
1491
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001492 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001493 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1494 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1495
1496 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001497 return;
1498 }
1499
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001500 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001501}
1502
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001503WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001505{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001506 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001507
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001508 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001509
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001510 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001511}
1512
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513WL_EXPORT struct weston_view *
1514weston_compositor_pick_view(struct weston_compositor *compositor,
1515 wl_fixed_t x, wl_fixed_t y,
1516 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001517{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001519 int ix = wl_fixed_to_int(x);
1520 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 wl_list_for_each(view, &compositor->view_list, link) {
1523 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001524 if (pixman_region32_contains_point(
1525 &view->transform.masked_boundingbox,
1526 ix, iy, NULL) &&
1527 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 wl_fixed_to_int(*vx),
1529 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001530 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001532 }
1533
1534 return NULL;
1535}
1536
1537static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001539{
Daniel Stone37816df2012-05-16 18:45:18 +01001540 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001541
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001542 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001543 return;
1544
Daniel Stone37816df2012-05-16 18:45:18 +01001545 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001546 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001547}
1548
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001549WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001551{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001552 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001553
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 if (!weston_view_is_mapped(view))
1555 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 weston_view_damage_below(view);
1558 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001559 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001560 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001561 wl_list_remove(&view->link);
1562 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 view->output_mask = 0;
1564 weston_surface_assign_output(view->surface);
1565
1566 if (weston_surface_is_mapped(view->surface))
1567 return;
1568
1569 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1570 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001571 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001573 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001574 NULL,
1575 wl_fixed_from_int(0),
1576 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001578 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001579 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582WL_EXPORT void
1583weston_surface_unmap(struct weston_surface *surface)
1584{
1585 struct weston_view *view;
1586
1587 wl_list_for_each(view, &surface->views, surface_link)
1588 weston_view_unmap(view);
1589 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001590}
1591
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001592static void
1593weston_surface_reset_pending_buffer(struct weston_surface *surface)
1594{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001595 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001596 surface->pending.sx = 0;
1597 surface->pending.sy = 0;
1598 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001599 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001600}
1601
Jason Ekstranda7af7042013-10-12 22:38:11 -05001602WL_EXPORT void
1603weston_view_destroy(struct weston_view *view)
1604{
1605 wl_signal_emit(&view->destroy_signal, view);
1606
1607 assert(wl_list_empty(&view->geometry.child_list));
1608
1609 if (weston_view_is_mapped(view)) {
1610 weston_view_unmap(view);
1611 weston_compositor_build_view_list(view->surface->compositor);
1612 }
1613
1614 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001615 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616
1617 pixman_region32_fini(&view->clip);
1618 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001619 pixman_region32_fini(&view->transform.masked_boundingbox);
1620 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001621
1622 weston_view_set_transform_parent(view, NULL);
1623
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 wl_list_remove(&view->surface_link);
1625
1626 free(view);
1627}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001628
1629WL_EXPORT void
1630weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001631{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001632 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001634
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001635 if (--surface->ref_count > 0)
1636 return;
1637
1638 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1639
Pekka Paalanene67858b2013-04-25 13:57:42 +03001640 assert(wl_list_empty(&surface->subsurface_list_pending));
1641 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001642
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1644 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001645
Jason Ekstrand7b982072014-05-20 14:33:03 -05001646 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001647
Pekka Paalanende685b82012-12-04 15:58:12 +02001648 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001649
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001650 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001651 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001652 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001653
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001654 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001655 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001656
Pekka Paalanen133e4392014-09-23 22:08:46 -04001657 weston_presentation_feedback_discard_list(&surface->feedback_list);
1658
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001659 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001660}
1661
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001662static void
1663destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001664{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001665 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001666
Giulio Camuffo0d379742013-11-15 22:06:15 +01001667 /* Set the resource to NULL, since we don't want to leave a
1668 * dangling pointer if the surface was refcounted and survives
1669 * the weston_surface_destroy() call. */
1670 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001671 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001672}
1673
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001674static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001675weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1676{
1677 struct weston_buffer *buffer =
1678 container_of(listener, struct weston_buffer, destroy_listener);
1679
1680 wl_signal_emit(&buffer->destroy_signal, buffer);
1681 free(buffer);
1682}
1683
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001684WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001685weston_buffer_from_resource(struct wl_resource *resource)
1686{
1687 struct weston_buffer *buffer;
1688 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001689
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001690 listener = wl_resource_get_destroy_listener(resource,
1691 weston_buffer_destroy_handler);
1692
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001693 if (listener)
1694 return container_of(listener, struct weston_buffer,
1695 destroy_listener);
1696
1697 buffer = zalloc(sizeof *buffer);
1698 if (buffer == NULL)
1699 return NULL;
1700
1701 buffer->resource = resource;
1702 wl_signal_init(&buffer->destroy_signal);
1703 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001704 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001705 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001706
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001707 return buffer;
1708}
1709
1710static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001711weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1712 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001713{
Pekka Paalanende685b82012-12-04 15:58:12 +02001714 struct weston_buffer_reference *ref =
1715 container_of(listener, struct weston_buffer_reference,
1716 destroy_listener);
1717
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001718 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001719 ref->buffer = NULL;
1720}
1721
1722WL_EXPORT void
1723weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001724 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001725{
1726 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001727 ref->buffer->busy_count--;
1728 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001729 assert(wl_resource_get_client(ref->buffer->resource));
1730 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001731 WL_BUFFER_RELEASE);
1732 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001733 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001734 }
1735
Pekka Paalanende685b82012-12-04 15:58:12 +02001736 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001737 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001738 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001739 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001740 }
1741
Pekka Paalanende685b82012-12-04 15:58:12 +02001742 ref->buffer = buffer;
1743 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1744}
1745
1746static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001747weston_surface_attach(struct weston_surface *surface,
1748 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001749{
1750 weston_buffer_reference(&surface->buffer_ref, buffer);
1751
Pekka Paalanena6421c42012-12-04 15:58:10 +02001752 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001753 if (weston_surface_is_mapped(surface))
1754 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001755 }
1756
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001757 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001758
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001759 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001760 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001761}
1762
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001763WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001765{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001766 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001767
1768 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001769 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001770}
1771
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001772WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001774{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001775 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001776
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001777 pixman_region32_union(&compositor->primary_plane.damage,
1778 &compositor->primary_plane.damage,
1779 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001780 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001781}
1782
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001783static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001785{
Pekka Paalanende685b82012-12-04 15:58:12 +02001786 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001787 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001788 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001789
Jason Ekstrandef540082014-06-26 10:37:36 -07001790 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791}
1792
1793static void
1794view_accumulate_damage(struct weston_view *view,
1795 pixman_region32_t *opaque)
1796{
1797 pixman_region32_t damage;
1798
1799 pixman_region32_init(&damage);
1800 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001801 pixman_box32_t *extents;
1802
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803 extents = pixman_region32_extents(&view->surface->damage);
1804 view_compute_bbox(view, extents->x1, extents->y1,
1805 extents->x2 - extents->x1,
1806 extents->y2 - extents->y1,
1807 &damage);
1808 pixman_region32_translate(&damage,
1809 -view->plane->x,
1810 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001811 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001812 pixman_region32_copy(&damage, &view->surface->damage);
1813 pixman_region32_translate(&damage,
1814 view->geometry.x - view->plane->x,
1815 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001816 }
1817
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 pixman_region32_subtract(&damage, &damage, opaque);
1819 pixman_region32_union(&view->plane->damage,
1820 &view->plane->damage, &damage);
1821 pixman_region32_fini(&damage);
1822 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001823 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001824}
1825
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001826static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001827compositor_accumulate_damage(struct weston_compositor *ec)
1828{
1829 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001830 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001831 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001832
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001833 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001834
1835 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001836 pixman_region32_copy(&plane->clip, &clip);
1837
1838 pixman_region32_init(&opaque);
1839
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 wl_list_for_each(ev, &ec->view_list, link) {
1841 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001842 continue;
1843
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001845 }
1846
1847 pixman_region32_union(&clip, &clip, &opaque);
1848 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001849 }
1850
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001851 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001852
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853 wl_list_for_each(ev, &ec->view_list, link)
1854 ev->surface->touched = 0;
1855
1856 wl_list_for_each(ev, &ec->view_list, link) {
1857 if (ev->surface->touched)
1858 continue;
1859 ev->surface->touched = 1;
1860
1861 surface_flush_damage(ev->surface);
1862
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001863 /* Both the renderer and the backend have seen the buffer
1864 * by now. If renderer needs the buffer, it has its own
1865 * reference set. If the backend wants to keep the buffer
1866 * around for migrating the surface into a non-primary plane
1867 * later, keep_buffer is true. Otherwise, drop the core
1868 * reference now, and allow early buffer release. This enables
1869 * clients to use single-buffering.
1870 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 if (!ev->surface->keep_buffer)
1872 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001873 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001874}
1875
1876static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001877surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001878{
1879 struct weston_subsurface *sub;
1880
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001882 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001883 continue;
1884
Jason Ekstranda7af7042013-10-12 22:38:11 -05001885 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1886 wl_list_init(&sub->surface->views);
1887
1888 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001889 }
1890}
1891
1892static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001893surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001894{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 struct weston_subsurface *sub;
1896 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001897
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1899 if (sub->surface == surface)
1900 continue;
1901
George Kiagiadakised04d382014-06-13 18:10:26 +02001902 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1903 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001904 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001905 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001906
1907 surface_free_unused_subsurface_views(sub->surface);
1908 }
1909}
1910
1911static void
1912view_list_add_subsurface_view(struct weston_compositor *compositor,
1913 struct weston_subsurface *sub,
1914 struct weston_view *parent)
1915{
1916 struct weston_subsurface *child;
1917 struct weston_view *view = NULL, *iv;
1918
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001919 if (!weston_surface_is_mapped(sub->surface))
1920 return;
1921
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1923 if (iv->geometry.parent == parent) {
1924 view = iv;
1925 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001926 }
1927 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928
1929 if (view) {
1930 /* Put it back in the surface's list of views */
1931 wl_list_remove(&view->surface_link);
1932 wl_list_insert(&sub->surface->views, &view->surface_link);
1933 } else {
1934 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001935 weston_view_set_position(view,
1936 sub->position.x,
1937 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001938 weston_view_set_transform_parent(view, parent);
1939 }
1940
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001941 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001942 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001943
Pekka Paalanenb188e912013-11-19 14:03:35 +02001944 if (wl_list_empty(&sub->surface->subsurface_list)) {
1945 wl_list_insert(compositor->view_list.prev, &view->link);
1946 return;
1947 }
1948
1949 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1950 if (child->surface == sub->surface)
1951 wl_list_insert(compositor->view_list.prev, &view->link);
1952 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001953 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001954 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955}
1956
1957static void
1958view_list_add(struct weston_compositor *compositor,
1959 struct weston_view *view)
1960{
1961 struct weston_subsurface *sub;
1962
1963 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964
Pekka Paalanenb188e912013-11-19 14:03:35 +02001965 if (wl_list_empty(&view->surface->subsurface_list)) {
1966 wl_list_insert(compositor->view_list.prev, &view->link);
1967 return;
1968 }
1969
1970 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1971 if (sub->surface == view->surface)
1972 wl_list_insert(compositor->view_list.prev, &view->link);
1973 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001974 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001975 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976}
1977
1978static void
1979weston_compositor_build_view_list(struct weston_compositor *compositor)
1980{
1981 struct weston_view *view;
1982 struct weston_layer *layer;
1983
1984 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001985 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001986 surface_stash_subsurface_views(view->surface);
1987
1988 wl_list_init(&compositor->view_list);
1989 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001990 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991 view_list_add(compositor, view);
1992 }
1993 }
1994
1995 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001996 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001998}
1999
David Herrmann1edf44c2013-10-22 17:11:26 +02002000static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002001weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002002{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002003 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002004 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002005 struct weston_animation *animation, *next;
2006 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002007 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002008 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002009 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002010
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002011 if (output->destroying)
2012 return 0;
2013
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002014 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002016
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002017 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002018 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002019 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002020 wl_list_for_each(ev, &ec->view_list, link)
2021 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002022
Pekka Paalanene67858b2013-04-25 13:57:42 +03002023 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002024 wl_list_for_each(ev, &ec->view_list, link) {
2025 /* Note: This operation is safe to do multiple times on the
2026 * same surface.
2027 */
2028 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002029 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002030 &ev->surface->frame_callback_list);
2031 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002032
2033 wl_list_insert_list(&output->feedback_list,
2034 &ev->surface->feedback_list);
2035 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002036 }
2037 }
2038
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002039 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002040
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002041 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002042 pixman_region32_intersect(&output_damage,
2043 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002044 pixman_region32_subtract(&output_damage,
2045 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002046
Scott Moreauccbf29d2012-02-22 14:21:41 -07002047 if (output->dirty)
2048 weston_output_update_matrix(output);
2049
David Herrmann1edf44c2013-10-22 17:11:26 +02002050 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002051
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002052 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002053
Kristian Høgsbergef044142011-06-21 15:02:12 -04002054 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002055
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002056 weston_compositor_repick(ec);
2057 wl_event_loop_dispatch(ec->input_loop, 0);
2058
Jonas Ådahldb773762012-06-13 00:01:21 +02002059 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002060 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002061 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002062 }
2063
Scott Moreaud64cf212012-06-08 19:40:54 -06002064 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002065 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002066 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002067 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002068
2069 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002070}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002071
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002072static int
2073weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002074{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002075 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002076
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002077 wl_event_loop_dispatch(compositor->input_loop, 0);
2078
2079 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002080}
2081
2082WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002083weston_output_finish_frame(struct weston_output *output,
2084 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002085{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002086 struct weston_compositor *compositor = output->compositor;
2087 struct wl_event_loop *loop =
2088 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002089 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002090 uint32_t refresh_nsec;
2091
2092 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2093 weston_presentation_feedback_present_list(&output->feedback_list,
2094 output, refresh_nsec, stamp,
Pekka Paalanen641307c2014-09-23 22:08:47 -04002095 output->msc);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002096
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002097 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002098
2099 if (output->repaint_needed &&
2100 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2101 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002102 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002103 if (!r)
2104 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002105 }
2106
2107 output->repaint_scheduled = 0;
2108 if (compositor->input_loop_source)
2109 return;
2110
2111 fd = wl_event_loop_get_fd(compositor->input_loop);
2112 compositor->input_loop_source =
2113 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2114 weston_compositor_read_input, compositor);
2115}
2116
2117static void
2118idle_repaint(void *data)
2119{
2120 struct weston_output *output = data;
2121
Jonas Ådahle5a12252013-04-05 23:07:11 +02002122 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002123}
2124
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002125WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002126weston_layer_entry_insert(struct weston_layer_entry *list,
2127 struct weston_layer_entry *entry)
2128{
2129 wl_list_insert(&list->link, &entry->link);
2130 entry->layer = list->layer;
2131}
2132
2133WL_EXPORT void
2134weston_layer_entry_remove(struct weston_layer_entry *entry)
2135{
2136 wl_list_remove(&entry->link);
2137 wl_list_init(&entry->link);
2138 entry->layer = NULL;
2139}
2140
2141WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002142weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2143{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002144 wl_list_init(&layer->view_list.link);
2145 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002146 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002147 if (below != NULL)
2148 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002149}
2150
2151WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002152weston_layer_set_mask(struct weston_layer *layer,
2153 int x, int y, int width, int height)
2154{
2155 struct weston_view *view;
2156
2157 layer->mask.x1 = x;
2158 layer->mask.x2 = x + width;
2159 layer->mask.y1 = y;
2160 layer->mask.y2 = y + height;
2161
2162 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2163 weston_view_geometry_dirty(view);
2164 }
2165}
2166
2167WL_EXPORT void
2168weston_layer_set_mask_infinite(struct weston_layer *layer)
2169{
2170 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2171 UINT32_MAX, UINT32_MAX);
2172}
2173
2174WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002175weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002176{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002177 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002178 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002179
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002180 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2181 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002182 return;
2183
Kristian Høgsbergef044142011-06-21 15:02:12 -04002184 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002185 output->repaint_needed = 1;
2186 if (output->repaint_scheduled)
2187 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002188
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002189 wl_event_loop_add_idle(loop, idle_repaint, output);
2190 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002191
2192 if (compositor->input_loop_source) {
2193 wl_event_source_remove(compositor->input_loop_source);
2194 compositor->input_loop_source = NULL;
2195 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002196}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002197
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002198WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002199weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2200{
2201 struct weston_output *output;
2202
2203 wl_list_for_each(output, &compositor->output_list, link)
2204 weston_output_schedule_repaint(output);
2205}
2206
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002207static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002208surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002209{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002210 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002211}
2212
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002213static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002214surface_attach(struct wl_client *client,
2215 struct wl_resource *resource,
2216 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2217{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002218 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002219 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002220
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002221 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002222 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002223 if (buffer == NULL) {
2224 wl_client_post_no_memory(client);
2225 return;
2226 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002227 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002228
Pekka Paalanende685b82012-12-04 15:58:12 +02002229 /* Attach, attach, without commit in between does not send
2230 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002231 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002232
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002233 surface->pending.sx = sx;
2234 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002235 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002236}
2237
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002238static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002239surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002240 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002241 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002242{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002243 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002244
Pekka Paalanen8e159182012-10-10 12:49:25 +03002245 pixman_region32_union_rect(&surface->pending.damage,
2246 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002247 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002248}
2249
Kristian Høgsberg33418202011-08-16 23:01:28 -04002250static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002251destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002252{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002253 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002254
2255 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002256 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002257}
2258
2259static void
2260surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002261 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002262{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002263 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002264 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002265
2266 cb = malloc(sizeof *cb);
2267 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002268 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002269 return;
2270 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002271
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002272 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2273 callback);
2274 if (cb->resource == NULL) {
2275 free(cb);
2276 wl_resource_post_no_memory(resource);
2277 return;
2278 }
2279
Jason Ekstranda85118c2013-06-27 20:17:02 -05002280 wl_resource_set_implementation(cb->resource, NULL, cb,
2281 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002282
Pekka Paalanenbc106382012-10-10 12:49:31 +03002283 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002284}
2285
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002286static void
2287surface_set_opaque_region(struct wl_client *client,
2288 struct wl_resource *resource,
2289 struct wl_resource *region_resource)
2290{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002291 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002292 struct weston_region *region;
2293
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002294 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002295 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002296 pixman_region32_copy(&surface->pending.opaque,
2297 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002298 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002299 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002300 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002301}
2302
2303static void
2304surface_set_input_region(struct wl_client *client,
2305 struct wl_resource *resource,
2306 struct wl_resource *region_resource)
2307{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002308 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002309 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002310
2311 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002312 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002313 pixman_region32_copy(&surface->pending.input,
2314 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002315 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002316 pixman_region32_fini(&surface->pending.input);
2317 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002318 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002319}
2320
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002321static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002323{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002324 struct weston_subsurface *sub;
2325
2326 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2327 parent_link_pending) {
2328 wl_list_remove(&sub->parent_link);
2329 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2330 }
2331}
2332
2333static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002334weston_surface_commit_state(struct weston_surface *surface,
2335 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002336{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002337 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002338 pixman_region32_t opaque;
2339
Alexander Larsson4ea95522013-05-22 14:41:37 +02002340 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002341 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002342 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002343 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002344
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002345 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002346 if (state->newly_attached)
2347 weston_surface_attach(surface, state->buffer);
2348 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002349
Jason Ekstrand7b982072014-05-20 14:33:03 -05002350 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002351 weston_surface_update_size(surface);
2352 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002353 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002354 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002355
Jason Ekstrand7b982072014-05-20 14:33:03 -05002356 state->sx = 0;
2357 state->sy = 0;
2358 state->newly_attached = 0;
2359 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002360
2361 /* wl_surface.damage */
2362 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002363 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002364 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002365 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002366 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002367
2368 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002369 pixman_region32_init(&opaque);
2370 pixman_region32_intersect_rect(&opaque, &state->opaque,
2371 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002372
2373 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2374 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002375 wl_list_for_each(view, &surface->views, surface_link)
2376 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002377 }
2378
2379 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002380
2381 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002382 pixman_region32_intersect_rect(&surface->input, &state->input,
2383 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002384
Pekka Paalanenbc106382012-10-10 12:49:31 +03002385 /* wl_surface.frame */
2386 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002387 &state->frame_callback_list);
2388 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002389
2390 /* XXX:
2391 * What should happen with a feedback request, if there
2392 * is no wl_buffer attached for this commit?
2393 */
2394
2395 /* presentation.feedback */
2396 wl_list_insert_list(&surface->feedback_list,
2397 &state->feedback_list);
2398 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002399}
2400
2401static void
2402weston_surface_commit(struct weston_surface *surface)
2403{
2404 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002405
Pekka Paalanene67858b2013-04-25 13:57:42 +03002406 weston_surface_commit_subsurface_order(surface);
2407
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002408 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002409}
2410
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002411static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002412weston_subsurface_commit(struct weston_subsurface *sub);
2413
2414static void
2415weston_subsurface_parent_commit(struct weston_subsurface *sub,
2416 int parent_is_synchronized);
2417
2418static void
2419surface_commit(struct wl_client *client, struct wl_resource *resource)
2420{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002421 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002422 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2423
2424 if (sub) {
2425 weston_subsurface_commit(sub);
2426 return;
2427 }
2428
2429 weston_surface_commit(surface);
2430
2431 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2432 if (sub->surface != surface)
2433 weston_subsurface_parent_commit(sub, 0);
2434 }
2435}
2436
2437static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002438surface_set_buffer_transform(struct wl_client *client,
2439 struct wl_resource *resource, int transform)
2440{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002441 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002442
Jonny Lamba55f1392014-05-30 12:07:15 +02002443 /* if wl_output.transform grows more members this will need to be updated. */
2444 if (transform < 0 ||
2445 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2446 wl_resource_post_error(resource,
2447 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2448 "buffer transform must be a valid transform "
2449 "('%d' specified)", transform);
2450 return;
2451 }
2452
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002453 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002454 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002455}
2456
Alexander Larsson4ea95522013-05-22 14:41:37 +02002457static void
2458surface_set_buffer_scale(struct wl_client *client,
2459 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002460 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002461{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002462 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002463
Jonny Lamba55f1392014-05-30 12:07:15 +02002464 if (scale < 1) {
2465 wl_resource_post_error(resource,
2466 WL_SURFACE_ERROR_INVALID_SCALE,
2467 "buffer scale must be at least one "
2468 "('%d' specified)", scale);
2469 return;
2470 }
2471
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002472 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002473 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002474}
2475
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002476static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002477 surface_destroy,
2478 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002479 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002480 surface_frame,
2481 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002482 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002483 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002484 surface_set_buffer_transform,
2485 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002486};
2487
2488static void
2489compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002490 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002491{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002492 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002493 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002494
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002495 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002496 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002497 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002498 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002499 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002500
Jason Ekstranda85118c2013-06-27 20:17:02 -05002501 surface->resource =
2502 wl_resource_create(client, &wl_surface_interface,
2503 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002504 if (surface->resource == NULL) {
2505 weston_surface_destroy(surface);
2506 wl_resource_post_no_memory(resource);
2507 return;
2508 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002509 wl_resource_set_implementation(surface->resource, &surface_interface,
2510 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002511
2512 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002513}
2514
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002515static void
2516destroy_region(struct wl_resource *resource)
2517{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002518 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002519
2520 pixman_region32_fini(&region->region);
2521 free(region);
2522}
2523
2524static void
2525region_destroy(struct wl_client *client, struct wl_resource *resource)
2526{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002527 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002528}
2529
2530static void
2531region_add(struct wl_client *client, struct wl_resource *resource,
2532 int32_t x, int32_t y, int32_t width, int32_t height)
2533{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002534 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002535
2536 pixman_region32_union_rect(&region->region, &region->region,
2537 x, y, width, height);
2538}
2539
2540static void
2541region_subtract(struct wl_client *client, struct wl_resource *resource,
2542 int32_t x, int32_t y, int32_t width, int32_t height)
2543{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002544 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002545 pixman_region32_t rect;
2546
2547 pixman_region32_init_rect(&rect, x, y, width, height);
2548 pixman_region32_subtract(&region->region, &region->region, &rect);
2549 pixman_region32_fini(&rect);
2550}
2551
2552static const struct wl_region_interface region_interface = {
2553 region_destroy,
2554 region_add,
2555 region_subtract
2556};
2557
2558static void
2559compositor_create_region(struct wl_client *client,
2560 struct wl_resource *resource, uint32_t id)
2561{
2562 struct weston_region *region;
2563
2564 region = malloc(sizeof *region);
2565 if (region == NULL) {
2566 wl_resource_post_no_memory(resource);
2567 return;
2568 }
2569
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002570 pixman_region32_init(&region->region);
2571
Jason Ekstranda85118c2013-06-27 20:17:02 -05002572 region->resource =
2573 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002574 if (region->resource == NULL) {
2575 free(region);
2576 wl_resource_post_no_memory(resource);
2577 return;
2578 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002579 wl_resource_set_implementation(region->resource, &region_interface,
2580 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002581}
2582
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002583static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002584 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002585 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002586};
2587
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002588static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002589weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2590{
2591 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592
Jason Ekstrand7b982072014-05-20 14:33:03 -05002593 weston_surface_commit_state(surface, &sub->cached);
2594 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002595
2596 weston_surface_commit_subsurface_order(surface);
2597
2598 weston_surface_schedule_repaint(surface);
2599
Jason Ekstrand7b982072014-05-20 14:33:03 -05002600 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002601}
2602
2603static void
2604weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2605{
2606 struct weston_surface *surface = sub->surface;
2607
2608 /*
2609 * If this commit would cause the surface to move by the
2610 * attach(dx, dy) parameters, the old damage region must be
2611 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002612 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613 */
2614 pixman_region32_translate(&sub->cached.damage,
2615 -surface->pending.sx, -surface->pending.sy);
2616 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2617 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002618 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002619
2620 if (surface->pending.newly_attached) {
2621 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002622 weston_surface_state_set_buffer(&sub->cached,
2623 surface->pending.buffer);
2624 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002625 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002626 weston_presentation_feedback_discard_list(
2627 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002628 }
2629 sub->cached.sx += surface->pending.sx;
2630 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002631
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002632 sub->cached.buffer_viewport.changed |=
2633 surface->pending.buffer_viewport.changed;
2634 sub->cached.buffer_viewport.buffer =
2635 surface->pending.buffer_viewport.buffer;
2636 sub->cached.buffer_viewport.surface =
2637 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002638
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002639 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002640
2641 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2642
2643 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2644
2645 wl_list_insert_list(&sub->cached.frame_callback_list,
2646 &surface->pending.frame_callback_list);
2647 wl_list_init(&surface->pending.frame_callback_list);
2648
Pekka Paalanen133e4392014-09-23 22:08:46 -04002649 wl_list_insert_list(&sub->cached.feedback_list,
2650 &surface->pending.feedback_list);
2651 wl_list_init(&surface->pending.feedback_list);
2652
Jason Ekstrand7b982072014-05-20 14:33:03 -05002653 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002654}
2655
Derek Foreman280e7dd2014-10-03 13:13:42 -05002656static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002657weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2658{
2659 while (sub) {
2660 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002661 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662
2663 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002664 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665
2666 sub = weston_surface_to_subsurface(sub->parent);
2667 }
2668
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002669 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002670}
2671
2672static void
2673weston_subsurface_commit(struct weston_subsurface *sub)
2674{
2675 struct weston_surface *surface = sub->surface;
2676 struct weston_subsurface *tmp;
2677
2678 /* Recursive check for effectively synchronized. */
2679 if (weston_subsurface_is_synchronized(sub)) {
2680 weston_subsurface_commit_to_cache(sub);
2681 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002682 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002683 /* flush accumulated state from cache */
2684 weston_subsurface_commit_to_cache(sub);
2685 weston_subsurface_commit_from_cache(sub);
2686 } else {
2687 weston_surface_commit(surface);
2688 }
2689
2690 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2691 if (tmp->surface != surface)
2692 weston_subsurface_parent_commit(tmp, 0);
2693 }
2694 }
2695}
2696
2697static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002698weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002699{
2700 struct weston_surface *surface = sub->surface;
2701 struct weston_subsurface *tmp;
2702
Pekka Paalanene67858b2013-04-25 13:57:42 +03002703 /* From now on, commit_from_cache the whole sub-tree, regardless of
2704 * the synchronized mode of each child. This sub-surface or some
2705 * of its ancestors were synchronized, so we are synchronized
2706 * all the way down.
2707 */
2708
Jason Ekstrand7b982072014-05-20 14:33:03 -05002709 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002710 weston_subsurface_commit_from_cache(sub);
2711
2712 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2713 if (tmp->surface != surface)
2714 weston_subsurface_parent_commit(tmp, 1);
2715 }
2716}
2717
2718static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002719weston_subsurface_parent_commit(struct weston_subsurface *sub,
2720 int parent_is_synchronized)
2721{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002723 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002724 wl_list_for_each(view, &sub->surface->views, surface_link)
2725 weston_view_set_position(view,
2726 sub->position.x,
2727 sub->position.y);
2728
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002729 sub->position.set = 0;
2730 }
2731
2732 if (parent_is_synchronized || sub->synchronized)
2733 weston_subsurface_synchronized_commit(sub);
2734}
2735
2736static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002737subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002738{
2739 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002740 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002741
Jason Ekstranda7af7042013-10-12 22:38:11 -05002742 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002743 weston_view_set_position(view,
2744 view->geometry.x + dx,
2745 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002746
2747 /* No need to check parent mappedness, because if parent is not
2748 * mapped, parent is not in a visible layer, so this sub-surface
2749 * will not be drawn either.
2750 */
2751 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002752 struct weston_output *output;
2753
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002754 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002755 * because that would call it also for the parent surface,
2756 * which might not be mapped yet. That would lead to
2757 * inconsistent state, where the window could never be
2758 * mapped.
2759 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002760 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002761 * weston_surface_is_mapped() return true, so that when the
2762 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002763 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764 */
2765 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002766 output = container_of(compositor->output_list.next,
2767 struct weston_output, link);
2768
2769 surface->output = output;
2770 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771 }
2772}
2773
2774static struct weston_subsurface *
2775weston_surface_to_subsurface(struct weston_surface *surface)
2776{
2777 if (surface->configure == subsurface_configure)
2778 return surface->configure_private;
2779
2780 return NULL;
2781}
2782
Pekka Paalanen01388e22013-04-25 13:57:44 +03002783WL_EXPORT struct weston_surface *
2784weston_surface_get_main_surface(struct weston_surface *surface)
2785{
2786 struct weston_subsurface *sub;
2787
2788 while (surface && (sub = weston_surface_to_subsurface(surface)))
2789 surface = sub->parent;
2790
2791 return surface;
2792}
2793
Pekka Paalanen50b67472014-10-01 15:02:41 +03002794WL_EXPORT int
2795weston_surface_set_role(struct weston_surface *surface,
2796 const char *role_name,
2797 struct wl_resource *error_resource,
2798 uint32_t error_code)
2799{
2800 assert(role_name);
2801
2802 if (surface->role_name == NULL ||
2803 surface->role_name == role_name ||
2804 strcmp(surface->role_name, role_name) == 0) {
2805 surface->role_name = role_name;
2806
2807 return 0;
2808 }
2809
2810 wl_resource_post_error(error_resource, error_code,
2811 "Cannot assign role %s to wl_surface@%d,"
2812 " already has role %s\n",
2813 role_name,
2814 wl_resource_get_id(surface->resource),
2815 surface->role_name);
2816 return -1;
2817}
2818
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819static void
2820subsurface_set_position(struct wl_client *client,
2821 struct wl_resource *resource, int32_t x, int32_t y)
2822{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002823 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824
2825 if (!sub)
2826 return;
2827
2828 sub->position.x = x;
2829 sub->position.y = y;
2830 sub->position.set = 1;
2831}
2832
2833static struct weston_subsurface *
2834subsurface_from_surface(struct weston_surface *surface)
2835{
2836 struct weston_subsurface *sub;
2837
2838 sub = weston_surface_to_subsurface(surface);
2839 if (sub)
2840 return sub;
2841
2842 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2843 if (sub->surface == surface)
2844 return sub;
2845
2846 return NULL;
2847}
2848
2849static struct weston_subsurface *
2850subsurface_sibling_check(struct weston_subsurface *sub,
2851 struct weston_surface *surface,
2852 const char *request)
2853{
2854 struct weston_subsurface *sibling;
2855
2856 sibling = subsurface_from_surface(surface);
2857
2858 if (!sibling) {
2859 wl_resource_post_error(sub->resource,
2860 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2861 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002862 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002863 return NULL;
2864 }
2865
2866 if (sibling->parent != sub->parent) {
2867 wl_resource_post_error(sub->resource,
2868 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2869 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002870 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002871 return NULL;
2872 }
2873
2874 return sibling;
2875}
2876
2877static void
2878subsurface_place_above(struct wl_client *client,
2879 struct wl_resource *resource,
2880 struct wl_resource *sibling_resource)
2881{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002882 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002883 struct weston_surface *surface =
2884 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885 struct weston_subsurface *sibling;
2886
2887 if (!sub)
2888 return;
2889
2890 sibling = subsurface_sibling_check(sub, surface, "place_above");
2891 if (!sibling)
2892 return;
2893
2894 wl_list_remove(&sub->parent_link_pending);
2895 wl_list_insert(sibling->parent_link_pending.prev,
2896 &sub->parent_link_pending);
2897}
2898
2899static void
2900subsurface_place_below(struct wl_client *client,
2901 struct wl_resource *resource,
2902 struct wl_resource *sibling_resource)
2903{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002904 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002905 struct weston_surface *surface =
2906 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002907 struct weston_subsurface *sibling;
2908
2909 if (!sub)
2910 return;
2911
2912 sibling = subsurface_sibling_check(sub, surface, "place_below");
2913 if (!sibling)
2914 return;
2915
2916 wl_list_remove(&sub->parent_link_pending);
2917 wl_list_insert(&sibling->parent_link_pending,
2918 &sub->parent_link_pending);
2919}
2920
2921static void
2922subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2923{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002924 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002925
2926 if (sub)
2927 sub->synchronized = 1;
2928}
2929
2930static void
2931subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2932{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002933 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002934
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002935 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002936 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002937
2938 /* If sub became effectively desynchronized, flush. */
2939 if (!weston_subsurface_is_synchronized(sub))
2940 weston_subsurface_synchronized_commit(sub);
2941 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002942}
2943
2944static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002945weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2946{
2947 wl_list_remove(&sub->parent_link);
2948 wl_list_remove(&sub->parent_link_pending);
2949 wl_list_remove(&sub->parent_destroy_listener.link);
2950 sub->parent = NULL;
2951}
2952
2953static void
2954weston_subsurface_destroy(struct weston_subsurface *sub);
2955
2956static void
2957subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2958{
2959 struct weston_subsurface *sub =
2960 container_of(listener, struct weston_subsurface,
2961 surface_destroy_listener);
2962 assert(data == &sub->surface->resource);
2963
2964 /* The protocol object (wl_resource) is left inert. */
2965 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002966 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002967
2968 weston_subsurface_destroy(sub);
2969}
2970
2971static void
2972subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2973{
2974 struct weston_subsurface *sub =
2975 container_of(listener, struct weston_subsurface,
2976 parent_destroy_listener);
2977 assert(data == &sub->parent->resource);
2978 assert(sub->surface != sub->parent);
2979
2980 if (weston_surface_is_mapped(sub->surface))
2981 weston_surface_unmap(sub->surface);
2982
2983 weston_subsurface_unlink_parent(sub);
2984}
2985
2986static void
2987subsurface_resource_destroy(struct wl_resource *resource)
2988{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002989 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002990
2991 if (sub)
2992 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002993}
2994
2995static void
2996subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2997{
2998 wl_resource_destroy(resource);
2999}
3000
3001static void
3002weston_subsurface_link_parent(struct weston_subsurface *sub,
3003 struct weston_surface *parent)
3004{
3005 sub->parent = parent;
3006 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003007 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003008 &sub->parent_destroy_listener);
3009
3010 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3011 wl_list_insert(&parent->subsurface_list_pending,
3012 &sub->parent_link_pending);
3013}
3014
3015static void
3016weston_subsurface_link_surface(struct weston_subsurface *sub,
3017 struct weston_surface *surface)
3018{
3019 sub->surface = surface;
3020 sub->surface_destroy_listener.notify =
3021 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003022 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003023 &sub->surface_destroy_listener);
3024}
3025
3026static void
3027weston_subsurface_destroy(struct weston_subsurface *sub)
3028{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003029 struct weston_view *view, *next;
3030
Pekka Paalanene67858b2013-04-25 13:57:42 +03003031 assert(sub->surface);
3032
3033 if (sub->resource) {
3034 assert(weston_surface_to_subsurface(sub->surface) == sub);
3035 assert(sub->parent_destroy_listener.notify ==
3036 subsurface_handle_parent_destroy);
3037
George Kiagiadakised04d382014-06-13 18:10:26 +02003038 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3039 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003040 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003041 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003042
Pekka Paalanene67858b2013-04-25 13:57:42 +03003043 if (sub->parent)
3044 weston_subsurface_unlink_parent(sub);
3045
Jason Ekstrand7b982072014-05-20 14:33:03 -05003046 weston_surface_state_fini(&sub->cached);
3047 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003048
3049 sub->surface->configure = NULL;
3050 sub->surface->configure_private = NULL;
3051 } else {
3052 /* the dummy weston_subsurface for the parent itself */
3053 assert(sub->parent_destroy_listener.notify == NULL);
3054 wl_list_remove(&sub->parent_link);
3055 wl_list_remove(&sub->parent_link_pending);
3056 }
3057
3058 wl_list_remove(&sub->surface_destroy_listener.link);
3059 free(sub);
3060}
3061
3062static const struct wl_subsurface_interface subsurface_implementation = {
3063 subsurface_destroy,
3064 subsurface_set_position,
3065 subsurface_place_above,
3066 subsurface_place_below,
3067 subsurface_set_sync,
3068 subsurface_set_desync
3069};
3070
3071static struct weston_subsurface *
3072weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3073 struct weston_surface *parent)
3074{
3075 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003076 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077
3078 sub = calloc(1, sizeof *sub);
3079 if (!sub)
3080 return NULL;
3081
Jason Ekstranda7af7042013-10-12 22:38:11 -05003082 wl_list_init(&sub->unused_views);
3083
Jason Ekstranda85118c2013-06-27 20:17:02 -05003084 sub->resource =
3085 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003086 if (!sub->resource) {
3087 free(sub);
3088 return NULL;
3089 }
3090
Jason Ekstranda85118c2013-06-27 20:17:02 -05003091 wl_resource_set_implementation(sub->resource,
3092 &subsurface_implementation,
3093 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003094 weston_subsurface_link_surface(sub, surface);
3095 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003096 weston_surface_state_init(&sub->cached);
3097 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003098 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003099
3100 return sub;
3101}
3102
3103/* Create a dummy subsurface for having the parent itself in its
3104 * sub-surface lists. Makes stacking order manipulation easy.
3105 */
3106static struct weston_subsurface *
3107weston_subsurface_create_for_parent(struct weston_surface *parent)
3108{
3109 struct weston_subsurface *sub;
3110
3111 sub = calloc(1, sizeof *sub);
3112 if (!sub)
3113 return NULL;
3114
3115 weston_subsurface_link_surface(sub, parent);
3116 sub->parent = parent;
3117 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3118 wl_list_insert(&parent->subsurface_list_pending,
3119 &sub->parent_link_pending);
3120
3121 return sub;
3122}
3123
3124static void
3125subcompositor_get_subsurface(struct wl_client *client,
3126 struct wl_resource *resource,
3127 uint32_t id,
3128 struct wl_resource *surface_resource,
3129 struct wl_resource *parent_resource)
3130{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003131 struct weston_surface *surface =
3132 wl_resource_get_user_data(surface_resource);
3133 struct weston_surface *parent =
3134 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003135 struct weston_subsurface *sub;
3136 static const char where[] = "get_subsurface: wl_subsurface@";
3137
3138 if (surface == parent) {
3139 wl_resource_post_error(resource,
3140 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3141 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003142 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003143 return;
3144 }
3145
3146 if (weston_surface_to_subsurface(surface)) {
3147 wl_resource_post_error(resource,
3148 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3149 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003150 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003151 return;
3152 }
3153
Pekka Paalanen50b67472014-10-01 15:02:41 +03003154 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3155 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003156 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003158 if (weston_surface_get_main_surface(parent) == surface) {
3159 wl_resource_post_error(resource,
3160 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3161 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003162 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003163 return;
3164 }
3165
Pekka Paalanene67858b2013-04-25 13:57:42 +03003166 /* make sure the parent is in its own list */
3167 if (wl_list_empty(&parent->subsurface_list)) {
3168 if (!weston_subsurface_create_for_parent(parent)) {
3169 wl_resource_post_no_memory(resource);
3170 return;
3171 }
3172 }
3173
3174 sub = weston_subsurface_create(id, surface, parent);
3175 if (!sub) {
3176 wl_resource_post_no_memory(resource);
3177 return;
3178 }
3179
3180 surface->configure = subsurface_configure;
3181 surface->configure_private = sub;
3182}
3183
3184static void
3185subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3186{
3187 wl_resource_destroy(resource);
3188}
3189
3190static const struct wl_subcompositor_interface subcompositor_interface = {
3191 subcompositor_destroy,
3192 subcompositor_get_subsurface
3193};
3194
3195static void
3196bind_subcompositor(struct wl_client *client,
3197 void *data, uint32_t version, uint32_t id)
3198{
3199 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003200 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003201
Jason Ekstranda85118c2013-06-27 20:17:02 -05003202 resource =
3203 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003204 if (resource == NULL) {
3205 wl_client_post_no_memory(client);
3206 return;
3207 }
3208 wl_resource_set_implementation(resource, &subcompositor_interface,
3209 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003210}
3211
3212static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003213weston_compositor_dpms(struct weston_compositor *compositor,
3214 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003215{
3216 struct weston_output *output;
3217
3218 wl_list_for_each(output, &compositor->output_list, link)
3219 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003220 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003221}
3222
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003223WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003224weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003225{
Neil Roberts8b62e202013-09-30 13:14:47 +01003226 uint32_t old_state = compositor->state;
3227
3228 /* The state needs to be changed before emitting the wake
3229 * signal because that may try to schedule a repaint which
3230 * will not work if the compositor is still sleeping */
3231 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3232
3233 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003234 case WESTON_COMPOSITOR_SLEEPING:
3235 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3236 /* fall through */
3237 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003238 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003239 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003240 /* fall through */
3241 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003242 wl_event_source_timer_update(compositor->idle_source,
3243 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003244 }
3245}
3246
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003247WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003248weston_compositor_offscreen(struct weston_compositor *compositor)
3249{
3250 switch (compositor->state) {
3251 case WESTON_COMPOSITOR_OFFSCREEN:
3252 return;
3253 case WESTON_COMPOSITOR_SLEEPING:
3254 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3255 /* fall through */
3256 default:
3257 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3258 wl_event_source_timer_update(compositor->idle_source, 0);
3259 }
3260}
3261
3262WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003263weston_compositor_sleep(struct weston_compositor *compositor)
3264{
3265 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3266 return;
3267
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003268 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003269 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3270 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3271}
3272
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003273static int
3274idle_handler(void *data)
3275{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003276 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003277
3278 if (compositor->idle_inhibit)
3279 return 1;
3280
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003281 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003282 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003283
3284 return 1;
3285}
3286
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003287WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003288weston_plane_init(struct weston_plane *plane,
3289 struct weston_compositor *ec,
3290 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003291{
3292 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003293 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003294 plane->x = x;
3295 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003296 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003297
3298 /* Init the link so that the call to wl_list_remove() when releasing
3299 * the plane without ever stacking doesn't lead to a crash */
3300 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003301}
3302
3303WL_EXPORT void
3304weston_plane_release(struct weston_plane *plane)
3305{
Xiong Zhang97116532013-10-23 13:58:31 +08003306 struct weston_view *view;
3307
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003308 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003309 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003310
Xiong Zhang97116532013-10-23 13:58:31 +08003311 wl_list_for_each(view, &plane->compositor->view_list, link) {
3312 if (view->plane == plane)
3313 view->plane = NULL;
3314 }
3315
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003316 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003317}
3318
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003319WL_EXPORT void
3320weston_compositor_stack_plane(struct weston_compositor *ec,
3321 struct weston_plane *plane,
3322 struct weston_plane *above)
3323{
3324 if (above)
3325 wl_list_insert(above->link.prev, &plane->link);
3326 else
3327 wl_list_insert(&ec->plane_list, &plane->link);
3328}
3329
Casey Dahlin9074db52012-04-19 22:50:09 -04003330static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003331{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003332 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003333}
3334
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003335static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003336bind_output(struct wl_client *client,
3337 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003338{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003339 struct weston_output *output = data;
3340 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003341 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003342
Jason Ekstranda85118c2013-06-27 20:17:02 -05003343 resource = wl_resource_create(client, &wl_output_interface,
3344 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003345 if (resource == NULL) {
3346 wl_client_post_no_memory(client);
3347 return;
3348 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003349
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003350 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003351 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003352
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003353 wl_output_send_geometry(resource,
3354 output->x,
3355 output->y,
3356 output->mm_width,
3357 output->mm_height,
3358 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003359 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003360 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003361 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003362 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003363 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003364
3365 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003366 wl_output_send_mode(resource,
3367 mode->flags,
3368 mode->width,
3369 mode->height,
3370 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003371 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003372
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003373 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003374 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003375}
3376
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003377/* Move other outputs when one is removed so the space remains contiguos. */
3378static void
3379weston_compositor_remove_output(struct weston_compositor *compositor,
3380 struct weston_output *remove_output)
3381{
3382 struct weston_output *output;
3383 int offset = 0;
3384
3385 wl_list_for_each(output, &compositor->output_list, link) {
3386 if (output == remove_output) {
3387 offset = output->width;
3388 continue;
3389 }
3390
3391 if (offset > 0) {
3392 weston_output_move(output,
3393 output->x - offset, output->y);
3394 output->dirty = 1;
3395 }
3396 }
3397}
3398
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003399WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003400weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003401{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003402 struct wl_resource *resource;
3403
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003404 output->destroying = 1;
3405
Pekka Paalanen133e4392014-09-23 22:08:46 -04003406 weston_presentation_feedback_discard_list(&output->feedback_list);
3407
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003408 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003409 wl_list_remove(&output->link);
3410
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003411 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003412 wl_signal_emit(&output->destroy_signal, output);
3413
Richard Hughesafe690c2013-05-02 10:10:04 +01003414 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003415 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003416 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003417 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003418
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003419 wl_resource_for_each(resource, &output->resource_list) {
3420 wl_resource_set_destructor(resource, NULL);
3421 }
3422
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003423 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003424}
3425
Scott Moreau1bad5db2012-08-18 01:04:05 -06003426static void
3427weston_output_compute_transform(struct weston_output *output)
3428{
3429 struct weston_matrix transform;
3430 int flip;
3431
3432 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003433 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003434
3435 switch(output->transform) {
3436 case WL_OUTPUT_TRANSFORM_FLIPPED:
3437 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3438 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3439 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003440 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003441 flip = -1;
3442 break;
3443 default:
3444 flip = 1;
3445 break;
3446 }
3447
3448 switch(output->transform) {
3449 case WL_OUTPUT_TRANSFORM_NORMAL:
3450 case WL_OUTPUT_TRANSFORM_FLIPPED:
3451 transform.d[0] = flip;
3452 transform.d[1] = 0;
3453 transform.d[4] = 0;
3454 transform.d[5] = 1;
3455 break;
3456 case WL_OUTPUT_TRANSFORM_90:
3457 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3458 transform.d[0] = 0;
3459 transform.d[1] = -flip;
3460 transform.d[4] = 1;
3461 transform.d[5] = 0;
3462 break;
3463 case WL_OUTPUT_TRANSFORM_180:
3464 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3465 transform.d[0] = -flip;
3466 transform.d[1] = 0;
3467 transform.d[4] = 0;
3468 transform.d[5] = -1;
3469 break;
3470 case WL_OUTPUT_TRANSFORM_270:
3471 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3472 transform.d[0] = 0;
3473 transform.d[1] = flip;
3474 transform.d[4] = -1;
3475 transform.d[5] = 0;
3476 break;
3477 default:
3478 break;
3479 }
3480
3481 weston_matrix_multiply(&output->matrix, &transform);
3482}
3483
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003484WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003485weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003486{
Scott Moreau850ca422012-05-21 15:21:25 -06003487 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003488
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003489 weston_matrix_init(&output->matrix);
3490 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003491 -(output->x + output->width / 2.0),
3492 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003493
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003494 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003495 2.0 / output->width,
3496 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003497
Scott Moreauccbf29d2012-02-22 14:21:41 -07003498 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003499 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003500 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003501 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3502 output->zoom.trans_y, 0);
3503 weston_matrix_scale(&output->matrix, magnification,
3504 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003505 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003506
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003507 weston_output_compute_transform(output);
3508
Scott Moreauccbf29d2012-02-22 14:21:41 -07003509 output->dirty = 0;
3510}
3511
Scott Moreau1bad5db2012-08-18 01:04:05 -06003512static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003513weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003514{
3515 output->transform = transform;
3516
3517 switch (transform) {
3518 case WL_OUTPUT_TRANSFORM_90:
3519 case WL_OUTPUT_TRANSFORM_270:
3520 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3521 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3522 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003523 output->width = output->current_mode->height;
3524 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003525 break;
3526 case WL_OUTPUT_TRANSFORM_NORMAL:
3527 case WL_OUTPUT_TRANSFORM_180:
3528 case WL_OUTPUT_TRANSFORM_FLIPPED:
3529 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003530 output->width = output->current_mode->width;
3531 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003532 break;
3533 default:
3534 break;
3535 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003536
Hardening57388e42013-09-18 23:56:36 +02003537 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003538 output->width /= scale;
3539 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003540}
3541
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003542static void
3543weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003544{
3545 output->x = x;
3546 output->y = y;
3547
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003548 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003549 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003550 output->width,
3551 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003552}
3553
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003554WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003555weston_output_move(struct weston_output *output, int x, int y)
3556{
3557 pixman_region32_t old_region;
3558 struct wl_resource *resource;
3559
3560 output->move_x = x - output->x;
3561 output->move_y = y - output->y;
3562
3563 if (output->move_x == 0 && output->move_y == 0)
3564 return;
3565
3566 pixman_region32_init(&old_region);
3567 pixman_region32_copy(&old_region, &output->region);
3568
3569 weston_output_init_geometry(output, x, y);
3570
3571 output->dirty = 1;
3572
3573 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003574 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003575
3576 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003577 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003578 wl_output_send_geometry(resource,
3579 output->x,
3580 output->y,
3581 output->mm_width,
3582 output->mm_height,
3583 output->subpixel,
3584 output->make,
3585 output->model,
3586 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003587
3588 if (wl_resource_get_version(resource) >= 2)
3589 wl_output_send_done(resource);
3590 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003591}
3592
3593WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003594weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003595 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003596 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003597{
3598 output->compositor = c;
3599 output->x = x;
3600 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003601 output->mm_width = mm_width;
3602 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003603 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003604 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003605
Alexander Larsson0b135062013-05-28 16:23:36 +02003606 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003607 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003608
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003609 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003610 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003611
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003612 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003613 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003614 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003615 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003616 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003617
Casey Dahlin58ba1372012-04-19 22:50:08 -04003618 output->id = ffs(~output->compositor->output_id_pool) - 1;
3619 output->compositor->output_id_pool |= 1 << output->id;
3620
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003621 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003622 wl_global_create(c->wl_display, &wl_output_interface, 2,
3623 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003624 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003625}
3626
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003627WL_EXPORT void
3628weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003629 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003630 wl_fixed_t *x, wl_fixed_t *y)
3631{
3632 wl_fixed_t tx, ty;
3633 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003634 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003635
Hardeningff39efa2013-09-18 23:56:35 +02003636 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3637 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003638
3639 switch(output->transform) {
3640 case WL_OUTPUT_TRANSFORM_NORMAL:
3641 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003642 tx = device_x;
3643 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003644 break;
3645 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003646 tx = device_y;
3647 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003648 break;
3649 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003650 tx = width - device_x;
3651 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003652 break;
3653 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003654 tx = width - device_y;
3655 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003656 break;
3657 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003658 tx = width - device_x;
3659 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003660 break;
3661 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003662 tx = width - device_y;
3663 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003664 break;
3665 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003666 tx = device_x;
3667 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003668 break;
3669 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003670 tx = device_y;
3671 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003672 break;
3673 }
3674
Neil Robertseb5a2002014-05-01 16:13:55 +01003675 tx /= output->current_scale;
3676 ty /= output->current_scale;
3677
3678 if (output->zoom.active) {
3679 zoom_scale = output->zoom.spring_z.current;
3680 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3681 output->width / 2.0f *
3682 (zoom_scale + output->zoom.trans_x));
3683 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3684 output->height / 2.0f *
3685 (zoom_scale + output->zoom.trans_y));
3686 tx = wl_fixed_from_double(zx);
3687 ty = wl_fixed_from_double(zy);
3688 }
3689
3690 *x = tx + wl_fixed_from_int(output->x);
3691 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003692}
3693
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003694static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003695destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003696{
Jonny Lamb74130762013-11-26 18:19:46 +01003697 struct weston_surface *surface =
3698 wl_resource_get_user_data(resource);
3699
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003700 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003701 surface->pending.buffer_viewport.buffer.src_width =
3702 wl_fixed_from_int(-1);
3703 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003704 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003705}
3706
3707static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003708viewport_destroy(struct wl_client *client,
3709 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003710{
3711 wl_resource_destroy(resource);
3712}
3713
3714static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003715viewport_set(struct wl_client *client,
3716 struct wl_resource *resource,
3717 wl_fixed_t src_x,
3718 wl_fixed_t src_y,
3719 wl_fixed_t src_width,
3720 wl_fixed_t src_height,
3721 int32_t dst_width,
3722 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003723{
Jonny Lamb74130762013-11-26 18:19:46 +01003724 struct weston_surface *surface =
3725 wl_resource_get_user_data(resource);
3726
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003727 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003728
Jonny Lamb8ae35902013-11-26 18:19:45 +01003729 if (wl_fixed_to_double(src_width) < 0 ||
3730 wl_fixed_to_double(src_height) < 0) {
3731 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003732 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003733 "source dimensions must be non-negative (%fx%f)",
3734 wl_fixed_to_double(src_width),
3735 wl_fixed_to_double(src_height));
3736 return;
3737 }
3738
3739 if (dst_width <= 0 || dst_height <= 0) {
3740 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003741 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003742 "destination dimensions must be positive (%dx%d)",
3743 dst_width, dst_height);
3744 return;
3745 }
3746
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003747 surface->pending.buffer_viewport.buffer.src_x = src_x;
3748 surface->pending.buffer_viewport.buffer.src_y = src_y;
3749 surface->pending.buffer_viewport.buffer.src_width = src_width;
3750 surface->pending.buffer_viewport.buffer.src_height = src_height;
3751 surface->pending.buffer_viewport.surface.width = dst_width;
3752 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003753 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003754}
3755
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003756static void
3757viewport_set_source(struct wl_client *client,
3758 struct wl_resource *resource,
3759 wl_fixed_t src_x,
3760 wl_fixed_t src_y,
3761 wl_fixed_t src_width,
3762 wl_fixed_t src_height)
3763{
3764 struct weston_surface *surface =
3765 wl_resource_get_user_data(resource);
3766
3767 assert(surface->viewport_resource != NULL);
3768
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003769 if (src_width == wl_fixed_from_int(-1) &&
3770 src_height == wl_fixed_from_int(-1)) {
3771 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003772 surface->pending.buffer_viewport.buffer.src_width =
3773 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003774 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003775 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003776 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003777
3778 if (src_width <= 0 || src_height <= 0) {
3779 wl_resource_post_error(resource,
3780 WL_VIEWPORT_ERROR_BAD_VALUE,
3781 "source size must be positive (%fx%f)",
3782 wl_fixed_to_double(src_width),
3783 wl_fixed_to_double(src_height));
3784 return;
3785 }
3786
3787 surface->pending.buffer_viewport.buffer.src_x = src_x;
3788 surface->pending.buffer_viewport.buffer.src_y = src_y;
3789 surface->pending.buffer_viewport.buffer.src_width = src_width;
3790 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003791 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003792}
3793
3794static void
3795viewport_set_destination(struct wl_client *client,
3796 struct wl_resource *resource,
3797 int32_t dst_width,
3798 int32_t dst_height)
3799{
3800 struct weston_surface *surface =
3801 wl_resource_get_user_data(resource);
3802
3803 assert(surface->viewport_resource != NULL);
3804
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003805 if (dst_width == -1 && dst_height == -1) {
3806 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003807 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003808 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003809 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003810 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003811
3812 if (dst_width <= 0 || dst_height <= 0) {
3813 wl_resource_post_error(resource,
3814 WL_VIEWPORT_ERROR_BAD_VALUE,
3815 "destination size must be positive (%dx%d)",
3816 dst_width, dst_height);
3817 return;
3818 }
3819
3820 surface->pending.buffer_viewport.surface.width = dst_width;
3821 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003822 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003823}
3824
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003825static const struct wl_viewport_interface viewport_interface = {
3826 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003827 viewport_set,
3828 viewport_set_source,
3829 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003830};
3831
3832static void
3833scaler_destroy(struct wl_client *client,
3834 struct wl_resource *resource)
3835{
3836 wl_resource_destroy(resource);
3837}
3838
3839static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003840scaler_get_viewport(struct wl_client *client,
3841 struct wl_resource *scaler,
3842 uint32_t id,
3843 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003844{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003845 int version = wl_resource_get_version(scaler);
3846 struct weston_surface *surface =
3847 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003848 struct wl_resource *resource;
3849
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003850 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003851 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003852 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3853 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003854 return;
3855 }
3856
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003857 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003858 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003859 if (resource == NULL) {
3860 wl_client_post_no_memory(client);
3861 return;
3862 }
3863
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003864 wl_resource_set_implementation(resource, &viewport_interface,
3865 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003866
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003867 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003868}
3869
3870static const struct wl_scaler_interface scaler_interface = {
3871 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003872 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003873};
3874
3875static void
3876bind_scaler(struct wl_client *client,
3877 void *data, uint32_t version, uint32_t id)
3878{
3879 struct wl_resource *resource;
3880
3881 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003882 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003883 if (resource == NULL) {
3884 wl_client_post_no_memory(client);
3885 return;
3886 }
3887
3888 wl_resource_set_implementation(resource, &scaler_interface,
3889 NULL, NULL);
3890}
3891
3892static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003893destroy_presentation_feedback(struct wl_resource *feedback_resource)
3894{
3895 struct weston_presentation_feedback *feedback;
3896
3897 feedback = wl_resource_get_user_data(feedback_resource);
3898
3899 wl_list_remove(&feedback->link);
3900 free(feedback);
3901}
3902
3903static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003904presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3905{
3906 wl_resource_destroy(resource);
3907}
3908
3909static void
3910presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003911 struct wl_resource *presentation_resource,
3912 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003913 uint32_t callback)
3914{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003915 struct weston_surface *surface;
3916 struct weston_presentation_feedback *feedback;
3917
3918 surface = wl_resource_get_user_data(surface_resource);
3919
3920 feedback = calloc(1, sizeof *feedback);
3921 if (!feedback)
3922 goto err_calloc;
3923
3924 feedback->resource = wl_resource_create(client,
3925 &presentation_feedback_interface,
3926 1, callback);
3927 if (!feedback->resource)
3928 goto err_create;
3929
3930 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3931 destroy_presentation_feedback);
3932
3933 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3934
3935 return;
3936
3937err_create:
3938 free(feedback);
3939
3940err_calloc:
3941 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003942}
3943
3944static const struct presentation_interface presentation_implementation = {
3945 presentation_destroy,
3946 presentation_feedback
3947};
3948
3949static void
3950bind_presentation(struct wl_client *client,
3951 void *data, uint32_t version, uint32_t id)
3952{
3953 struct weston_compositor *compositor = data;
3954 struct wl_resource *resource;
3955
3956 resource = wl_resource_create(client, &presentation_interface,
3957 MIN(version, 1), id);
3958 if (resource == NULL) {
3959 wl_client_post_no_memory(client);
3960 return;
3961 }
3962
3963 wl_resource_set_implementation(resource, &presentation_implementation,
3964 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003965 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003966}
3967
3968static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003969compositor_bind(struct wl_client *client,
3970 void *data, uint32_t version, uint32_t id)
3971{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003972 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003973 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003974
Jason Ekstranda85118c2013-06-27 20:17:02 -05003975 resource = wl_resource_create(client, &wl_compositor_interface,
3976 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003977 if (resource == NULL) {
3978 wl_client_post_no_memory(client);
3979 return;
3980 }
3981
3982 wl_resource_set_implementation(resource, &compositor_interface,
3983 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003984}
3985
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003986static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003987log_uname(void)
3988{
3989 struct utsname usys;
3990
3991 uname(&usys);
3992
3993 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3994 usys.version, usys.machine);
3995}
3996
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003997WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003998weston_environment_get_fd(const char *env)
3999{
4000 char *e, *end;
4001 int fd, flags;
4002
4003 e = getenv(env);
4004 if (!e)
4005 return -1;
4006 fd = strtol(e, &end, 0);
4007 if (*end != '\0')
4008 return -1;
4009
4010 flags = fcntl(fd, F_GETFD);
4011 if (flags == -1)
4012 return -1;
4013
4014 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4015 unsetenv(env);
4016
4017 return fd;
4018}
4019
4020WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004021weston_compositor_init(struct weston_compositor *ec,
4022 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004023 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004024 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004025{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004026 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004027 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004028 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004029
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004030 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004031 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004032 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004033 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004034 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004035 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004036 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004037 wl_signal_init(&ec->idle_signal);
4038 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004039 wl_signal_init(&ec->show_input_panel_signal);
4040 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004041 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004042 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004043 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004044 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004045 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004046 wl_signal_init(&ec->session_signal);
4047 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004048
Casey Dahlin58ba1372012-04-19 22:50:08 -04004049 ec->output_id_pool = 0;
4050
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004051 if (!wl_global_create(display, &wl_compositor_interface, 3,
4052 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004053 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004054
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004055 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4056 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004057 return -1;
4058
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004059 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004060 ec, bind_scaler))
4061 return -1;
4062
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004063 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4064 ec, bind_presentation))
4065 return -1;
4066
Jason Ekstranda7af7042013-10-12 22:38:11 -05004067 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004068 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004069 wl_list_init(&ec->layer_list);
4070 wl_list_init(&ec->seat_list);
4071 wl_list_init(&ec->output_list);
4072 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004073 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004074 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004075 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004076 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004077 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004078
Xiong Zhang97116532013-10-23 13:58:31 +08004079 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004080 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004081
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004082 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4083 weston_config_section_get_string(s, "keymap_rules",
4084 (char **) &xkb_names.rules, NULL);
4085 weston_config_section_get_string(s, "keymap_model",
4086 (char **) &xkb_names.model, NULL);
4087 weston_config_section_get_string(s, "keymap_layout",
4088 (char **) &xkb_names.layout, NULL);
4089 weston_config_section_get_string(s, "keymap_variant",
4090 (char **) &xkb_names.variant, NULL);
4091 weston_config_section_get_string(s, "keymap_options",
4092 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004093
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004094 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4095 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004096
Jonny Lamb66a41a02014-08-12 14:58:25 +02004097 weston_config_section_get_int(s, "repeat-rate",
4098 &ec->kb_repeat_rate, 40);
4099 weston_config_section_get_int(s, "repeat-delay",
4100 &ec->kb_repeat_delay, 400);
4101
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004102 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004103
4104 wl_data_device_manager_init(ec->wl_display);
4105
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004106 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004107
Daniel Stone725c2c32012-06-22 14:04:36 +01004108 loop = wl_display_get_event_loop(ec->wl_display);
4109 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4110 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4111
4112 ec->input_loop = wl_event_loop_create();
4113
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004114 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4115 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4116
4117 weston_compositor_schedule_repaint(ec);
4118
Daniel Stone725c2c32012-06-22 14:04:36 +01004119 return 0;
4120}
4121
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004122WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004123weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004124{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004125 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004126
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004127 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004128 if (ec->input_loop_source)
4129 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004130
Matt Roper361d2ad2011-08-29 13:52:23 -07004131 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004132 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004133 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004134
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004135 if (ec->renderer)
4136 ec->renderer->destroy(ec);
4137
Daniel Stone325fc2d2012-05-30 16:31:58 +01004138 weston_binding_list_destroy_all(&ec->key_binding_list);
4139 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004140 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004141 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004142 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004143
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004144 weston_plane_release(&ec->primary_plane);
4145
Jonas Ådahlc97af922012-03-28 22:36:09 +02004146 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004147
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004148 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004149}
4150
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004151WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004152weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4153 const struct weston_pointer_grab_interface *interface)
4154{
4155 struct weston_seat *seat;
4156
4157 ec->default_pointer_grab = interface;
4158 wl_list_for_each(seat, &ec->seat_list, link) {
4159 if (seat->pointer) {
4160 weston_pointer_set_default_grab(seat->pointer,
4161 interface);
4162 }
4163 }
4164}
4165
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004166WL_EXPORT int
4167weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4168 clockid_t clk_id)
4169{
4170 struct timespec ts;
4171
4172 if (clock_gettime(clk_id, &ts) < 0)
4173 return -1;
4174
4175 compositor->presentation_clock = clk_id;
4176
4177 return 0;
4178}
4179
4180/*
4181 * For choosing the software clock, when the display hardware or API
4182 * does not expose a compatible presentation timestamp.
4183 */
4184WL_EXPORT int
4185weston_compositor_set_presentation_clock_software(
4186 struct weston_compositor *compositor)
4187{
4188 /* In order of preference */
4189 static const clockid_t clocks[] = {
4190 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4191 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4192 CLOCK_MONOTONIC, /* no jumps, may crawl */
4193 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4194 CLOCK_REALTIME /* may jump and crawl */
4195 };
4196 unsigned i;
4197
4198 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4199 if (weston_compositor_set_presentation_clock(compositor,
4200 clocks[i]) == 0)
4201 return 0;
4202
4203 weston_log("Error: no suitable presentation clock available.\n");
4204
4205 return -1;
4206}
4207
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004208WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004209weston_version(int *major, int *minor, int *micro)
4210{
4211 *major = WESTON_VERSION_MAJOR;
4212 *minor = WESTON_VERSION_MINOR;
4213 *micro = WESTON_VERSION_MICRO;
4214}
4215
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004216static const char *
4217clock_name(clockid_t clk_id)
4218{
4219 static const char *names[] = {
4220 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4221 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4222 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4223 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4224 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4225 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4226 };
4227
4228 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4229 return "unknown";
4230
4231 return names[clk_id];
4232}
4233
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004234static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004235 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004236 const char *desc;
4237} capability_strings[] = {
4238 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004239 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004240};
4241
4242static void
4243weston_compositor_log_capabilities(struct weston_compositor *compositor)
4244{
4245 unsigned i;
4246 int yes;
4247
4248 weston_log("Compositor capabilities:\n");
4249 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4250 yes = compositor->capabilities & capability_strings[i].bit;
4251 weston_log_continue(STAMP_SPACE "%s %s\n",
4252 capability_strings[i].desc,
4253 yes ? "yes" : "no");
4254 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004255
4256 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4257 clock_name(compositor->presentation_clock),
4258 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004259}
4260
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004261static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004262{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004263 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004264
Martin Minarik6d118362012-06-07 18:01:59 +02004265 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004266 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004267
4268 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004269}
4270
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004271#ifdef HAVE_LIBUNWIND
4272
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004273static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004274print_backtrace(void)
4275{
4276 unw_cursor_t cursor;
4277 unw_context_t context;
4278 unw_word_t off;
4279 unw_proc_info_t pip;
4280 int ret, i = 0;
4281 char procname[256];
4282 const char *filename;
4283 Dl_info dlinfo;
4284
4285 pip.unwind_info = NULL;
4286 ret = unw_getcontext(&context);
4287 if (ret) {
4288 weston_log("unw_getcontext: %d\n", ret);
4289 return;
4290 }
4291
4292 ret = unw_init_local(&cursor, &context);
4293 if (ret) {
4294 weston_log("unw_init_local: %d\n", ret);
4295 return;
4296 }
4297
4298 ret = unw_step(&cursor);
4299 while (ret > 0) {
4300 ret = unw_get_proc_info(&cursor, &pip);
4301 if (ret) {
4302 weston_log("unw_get_proc_info: %d\n", ret);
4303 break;
4304 }
4305
4306 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4307 if (ret && ret != -UNW_ENOMEM) {
4308 if (ret != -UNW_EUNSPEC)
4309 weston_log("unw_get_proc_name: %d\n", ret);
4310 procname[0] = '?';
4311 procname[1] = 0;
4312 }
4313
4314 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4315 *dlinfo.dli_fname)
4316 filename = dlinfo.dli_fname;
4317 else
4318 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004319
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004320 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4321 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4322
4323 ret = unw_step(&cursor);
4324 if (ret < 0)
4325 weston_log("unw_step: %d\n", ret);
4326 }
4327}
4328
4329#else
4330
4331static void
4332print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004333{
4334 void *buffer[32];
4335 int i, count;
4336 Dl_info info;
4337
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004338 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4339 for (i = 0; i < count; i++) {
4340 dladdr(buffer[i], &info);
4341 weston_log(" [%016lx] %s (%s)\n",
4342 (long) buffer[i],
4343 info.dli_sname ? info.dli_sname : "--",
4344 info.dli_fname);
4345 }
4346}
4347
4348#endif
4349
4350static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004351on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004352{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004353 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004354 * then call the backend restore function, which will switch
4355 * back to the vt we launched from or ungrab X etc and then
4356 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004357 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004358 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004359 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004360
Peter Maatmane5b42e42013-03-27 22:38:53 +01004361 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004362
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004363 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004364
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004365 segv_compositor->restore(segv_compositor);
4366
4367 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004368}
4369
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004370WL_EXPORT void *
4371weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004372{
4373 char path[PATH_MAX];
4374 void *module, *init;
4375
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004376 if (name == NULL)
4377 return NULL;
4378
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004379 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004380 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004381 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004382 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004383
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004384 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4385 if (module) {
4386 weston_log("Module '%s' already loaded\n", path);
4387 dlclose(module);
4388 return NULL;
4389 }
4390
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004391 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004392 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004393 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004394 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004395 return NULL;
4396 }
4397
4398 init = dlsym(module, entrypoint);
4399 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004400 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004401 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004402 return NULL;
4403 }
4404
4405 return init;
4406}
4407
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004408static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004409load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004410 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004411{
4412 const char *p, *end;
4413 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004414 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004415 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004416
4417 if (modules == NULL)
4418 return 0;
4419
4420 p = modules;
4421 while (*p) {
4422 end = strchrnul(p, ',');
4423 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004424 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004425 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004426 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004427 p = end;
4428 while (*p == ',')
4429 p++;
4430
4431 }
4432
4433 return 0;
4434}
4435
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004436static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004437 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4438
4439static const char xdg_wrong_message[] =
4440 "fatal: environment variable XDG_RUNTIME_DIR\n"
4441 "is set to \"%s\", which is not a directory.\n";
4442
4443static const char xdg_wrong_mode_message[] =
4444 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004445 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4446 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004447
4448static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004449 "Refer to your distribution on how to get it, or\n"
4450 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4451 "on how to implement it.\n";
4452
Martin Minarik37032f82012-06-18 20:15:18 +02004453static void
4454verify_xdg_runtime_dir(void)
4455{
4456 char *dir = getenv("XDG_RUNTIME_DIR");
4457 struct stat s;
4458
4459 if (!dir) {
4460 weston_log(xdg_error_message);
4461 weston_log_continue(xdg_detail_message);
4462 exit(EXIT_FAILURE);
4463 }
4464
4465 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4466 weston_log(xdg_wrong_message, dir);
4467 weston_log_continue(xdg_detail_message);
4468 exit(EXIT_FAILURE);
4469 }
4470
4471 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4472 weston_log(xdg_wrong_mode_message,
4473 dir, s.st_mode & 0777, s.st_uid);
4474 weston_log_continue(xdg_detail_message);
4475 }
4476}
4477
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004478static int
4479usage(int error_code)
4480{
4481 fprintf(stderr,
4482 "Usage: weston [OPTIONS]\n\n"
4483 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4484 "Weston supports multiple backends, and depending on which backend is in use\n"
4485 "different options will be accepted.\n\n"
4486
4487
4488 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004489 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004490 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004491 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4492 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004493 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004494 " -S, --socket=NAME\tName of socket to listen on\n"
4495 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004496 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004497 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004498 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004499 " -h, --help\t\tThis help message\n\n");
4500
4501 fprintf(stderr,
4502 "Options for drm-backend.so:\n\n"
4503 " --connector=ID\tBring up only this connector\n"
4504 " --seat=SEAT\t\tThe seat that weston should run on\n"
4505 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004506 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004507 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4508
4509 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004510 "Options for fbdev-backend.so:\n\n"
4511 " --tty=TTY\t\tThe tty to use\n"
4512 " --device=DEVICE\tThe framebuffer device to use\n\n");
4513
4514 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004515 "Options for x11-backend.so:\n\n"
4516 " --width=WIDTH\t\tWidth of X window\n"
4517 " --height=HEIGHT\tHeight of X window\n"
4518 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004519 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004520 " --output-count=COUNT\tCreate multiple outputs\n"
4521 " --no-input\t\tDont create input devices\n\n");
4522
4523 fprintf(stderr,
4524 "Options for wayland-backend.so:\n\n"
4525 " --width=WIDTH\t\tWidth of Wayland surface\n"
4526 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004527 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004528 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004529 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004530 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004531 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004532 " --display=DISPLAY\tWayland display to connect to\n\n");
4533
Pekka Paalanene31e0532013-05-22 18:03:07 +03004534#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4535 fprintf(stderr,
4536 "Options for rpi-backend.so:\n\n"
4537 " --tty=TTY\t\tThe tty to use\n"
4538 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4539 " --transform=TR\tThe output transformation, TR is one of:\n"
4540 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004541 " --opaque-regions\tEnable support for opaque regions, can be "
4542 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004543 "\n");
4544#endif
4545
Hardeningc077a842013-07-08 00:51:35 +02004546#if defined(BUILD_RDP_COMPOSITOR)
4547 fprintf(stderr,
4548 "Options for rdp-backend.so:\n\n"
4549 " --width=WIDTH\t\tWidth of desktop\n"
4550 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004551 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4552 " --address=ADDR\tThe address to bind\n"
4553 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004554 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004555 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4556 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4557 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4558 "\n");
4559#endif
4560
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004561 exit(error_code);
4562}
4563
Peter Maatmane5b42e42013-03-27 22:38:53 +01004564static void
4565catch_signals(void)
4566{
4567 struct sigaction action;
4568
4569 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4570 action.sa_sigaction = on_caught_signal;
4571 sigemptyset(&action.sa_mask);
4572 sigaction(SIGSEGV, &action, NULL);
4573 sigaction(SIGABRT, &action, NULL);
4574}
4575
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004576static void
4577handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4578{
4579 struct wl_client *client = data;
4580
4581 weston_log("Primary client died. Closing...\n");
4582
4583 wl_display_terminate(wl_client_get_display(client));
4584}
4585
Ryo Munakatad8deff62014-09-06 07:32:05 +09004586static char *
4587weston_choose_default_backend(void)
4588{
4589 char *backend = NULL;
4590
4591 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4592 backend = strdup("wayland-backend.so");
4593 else if (getenv("DISPLAY"))
4594 backend = strdup("x11-backend.so");
4595 else
4596 backend = strdup(WESTON_NATIVE_BACKEND);
4597
4598 return backend;
4599}
4600
4601static int
4602weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4603{
4604 if (socket_name) {
4605 if (wl_display_add_socket(display, socket_name)) {
4606 weston_log("fatal: failed to add socket: %m\n");
4607 return -1;
4608 }
4609 } else {
4610 socket_name = wl_display_add_socket_auto(display);
4611 if (!socket_name) {
4612 weston_log("fatal: failed to add socket: %m\n");
4613 return -1;
4614 }
4615 }
4616
4617 setenv("WAYLAND_DISPLAY", socket_name, 1);
4618
4619 return 0;
4620}
4621
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004622int main(int argc, char *argv[])
4623{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004624 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004625 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004626 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004627 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004628 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004629 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004630 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004631 int *argc, char *argv[],
4632 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004633 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004634 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004635 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004636 char *modules = NULL;
4637 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004638 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004639 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004640 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004641 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004642 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004643 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004644 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004645 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004646 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004647 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004648 struct wl_client *primary_client;
4649 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004650 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004651
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004652 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004653 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4654 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004655 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4656 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004657 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004658 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004659 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004660 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004661 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004662 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004663
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004664 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004665
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004666 if (help)
4667 usage(EXIT_SUCCESS);
4668
Scott Moreau12245142012-08-29 15:15:58 -06004669 if (version) {
4670 printf(PACKAGE_STRING "\n");
4671 return EXIT_SUCCESS;
4672 }
4673
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004674 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004675
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004676 weston_log("%s\n"
4677 STAMP_SPACE "%s\n"
4678 STAMP_SPACE "Bug reports to: %s\n"
4679 STAMP_SPACE "Build: %s\n",
4680 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004681 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004682 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004683
Martin Minarik37032f82012-06-18 20:15:18 +02004684 verify_xdg_runtime_dir();
4685
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004686 display = wl_display_create();
4687
Tiago Vignatti2116b892011-08-08 05:52:59 -07004688 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004689 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4690 display);
4691 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4692 display);
4693 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4694 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004695
4696 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004697 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4698 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004699
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304700 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4701 ret = EXIT_FAILURE;
4702 goto out_signals;
4703 }
4704
Pekka Paalanen588bee12014-05-07 16:26:25 +03004705 if (noconfig == 0)
4706 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004707 if (config != NULL) {
4708 weston_log("Using config file '%s'\n",
4709 weston_config_get_full_path(config));
4710 } else {
4711 weston_log("Starting with no config file.\n");
4712 }
4713 section = weston_config_get_section(config, "core", NULL, NULL);
4714
Ryo Munakata03faed22014-09-06 07:32:51 +09004715 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004716 weston_config_section_get_string(section, "backend", &backend,
4717 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004718 if (!backend)
4719 backend = weston_choose_default_backend();
4720 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004721
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004722 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304723 if (!backend_init) {
4724 ret = EXIT_FAILURE;
4725 goto out_signals;
4726 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004727
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004728 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004729 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004730 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304731 ret = EXIT_FAILURE;
4732 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004733 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004734
Peter Maatmane5b42e42013-03-27 22:38:53 +01004735 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004736 segv_compositor = ec;
4737
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004738 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004739 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004740
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004741 for (i = 1; i < argc; i++)
4742 weston_log("fatal: unhandled option: %s\n", argv[i]);
4743 if (argc > 1) {
4744 ret = EXIT_FAILURE;
4745 goto out;
4746 }
4747
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004748 weston_compositor_log_capabilities(ec);
4749
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004750 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4751 if (server_socket) {
4752 weston_log("Running with single client\n");
4753 fd = strtol(server_socket, &end, 0);
4754 if (*end != '\0')
4755 fd = -1;
4756 } else {
4757 fd = -1;
4758 }
4759
4760 if (fd != -1) {
4761 primary_client = wl_client_create(display, fd);
4762 if (!primary_client) {
4763 weston_log("fatal: failed to add client: %m\n");
4764 ret = EXIT_FAILURE;
4765 goto out;
4766 }
4767 primary_client_destroyed.notify =
4768 handle_primary_client_destroyed;
4769 wl_client_add_destroy_listener(primary_client,
4770 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004771 } else if (weston_create_listening_socket(display, socket_name)) {
4772 ret = EXIT_FAILURE;
4773 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004774 }
4775
Ryo Munakata03faed22014-09-06 07:32:51 +09004776 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004777 weston_config_section_get_string(section, "shell", &shell,
4778 "desktop-shell.so");
4779
Ryo Munakata03faed22014-09-06 07:32:51 +09004780 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004781 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004782
4783 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004784 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004785 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004786
4787 if (load_modules(ec, option_modules, &argc, argv) < 0)
4788 goto out;
4789
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004790 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4791 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4792 if (numlock_on) {
4793 wl_list_for_each(seat, &ec->seat_list, link) {
4794 if (seat->keyboard)
4795 weston_keyboard_set_locks(seat->keyboard,
4796 WESTON_NUM_LOCK,
4797 WESTON_NUM_LOCK);
4798 }
4799 }
4800
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004801 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004802
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004803 wl_display_run(display);
4804
Ryo Munakata03faed22014-09-06 07:32:51 +09004805out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004806 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004807 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004808
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004809 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004810
Daniel Stone855028f2012-05-01 20:37:10 +01004811 weston_compositor_xkb_destroy(ec);
4812
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004813 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304814
4815out_signals:
4816 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4817 if (signals[i])
4818 wl_event_source_remove(signals[i]);
4819
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004820 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004821
Martin Minarik19e6f262012-06-07 13:08:46 +02004822 weston_log_file_close();
4823
Ryo Munakata03faed22014-09-06 07:32:51 +09004824 free(backend);
4825 free(shell);
4826 free(socket_name);
4827 free(option_modules);
4828 free(log);
4829 free(modules);
4830
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004831 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004832}