blob: 5d54227b48e33ee663a5dd00e1d5a85f638b1877 [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
Alex Wu2dda6042012-04-17 17:20:47 +0800101WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +0200102weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
103 int32_t scale, enum weston_mode_switch_op op)
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;
Hardening57388e42013-09-18 23:56:36 +0200108 int ret, notify_mode_changed, notify_scale_changed;
109 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200110
Alex Wu2dda6042012-04-17 17:20:47 +0800111 if (!output->switch_mode)
112 return -1;
113
Hardening57388e42013-09-18 23:56:36 +0200114 temporary_mode = (output->original_mode != 0);
115 temporary_scale = (output->current_scale != output->original_scale);
116 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
Hardening57388e42013-09-18 23:56:36 +0200118 notify_mode_changed = 0;
119 notify_scale_changed = 0;
120 switch(op) {
121 case WESTON_MODE_SWITCH_SET_NATIVE:
122 output->native_mode = mode;
123 if (!temporary_mode) {
124 notify_mode_changed = 1;
125 ret = output->switch_mode(output, mode);
126 if (ret < 0)
127 return ret;
128 }
129
130 output->native_scale = scale;
131 if(!temporary_scale)
132 notify_scale_changed = 1;
133 break;
134 case WESTON_MODE_SWITCH_SET_TEMPORARY:
135 if (!temporary_mode)
136 output->original_mode = output->native_mode;
137 if (!temporary_scale)
138 output->original_scale = output->native_scale;
139
140 ret = output->switch_mode(output, mode);
141 if (ret < 0)
142 return ret;
143
Hardening57388e42013-09-18 23:56:36 +0200144 output->current_scale = scale;
145 break;
146 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
147 if (!temporary_mode) {
148 weston_log("already in the native mode\n");
149 return -1;
150 }
151
152 notify_mode_changed = (output->original_mode != output->native_mode);
153
154 ret = output->switch_mode(output, mode);
155 if (ret < 0)
156 return ret;
157
158 if (output->original_scale != output->native_scale) {
159 notify_scale_changed = 1;
160 scale = output->native_scale;
161 output->original_scale = scale;
162 }
163 output->original_mode = 0;
164
165 output->current_scale = output->native_scale;
166 break;
167 default:
168 weston_log("unknown weston_switch_mode_op %d\n", op);
169 break;
170 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200171
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200172 pixman_region32_init(&old_output_region);
173 pixman_region32_copy(&old_output_region, &output->region);
174
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200175 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200176 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200177
178 pixman_region32_init(&output->previous_damage);
179 pixman_region32_init_rect(&output->region, output->x, output->y,
180 output->width, output->height);
181
182 weston_output_update_matrix(output);
183
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200184 /* If a pointer falls outside the outputs new geometry, move it to its
185 * lower-right corner */
186 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400187 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200188 int32_t x, y;
189
190 if (!pointer)
191 continue;
192
193 x = wl_fixed_to_int(pointer->x);
194 y = wl_fixed_to_int(pointer->y);
195
196 if (!pixman_region32_contains_point(&old_output_region,
197 x, y, NULL) ||
198 pixman_region32_contains_point(&output->region,
199 x, y, NULL))
200 continue;
201
202 if (x >= output->x + output->width)
203 x = output->x + output->width - 1;
204 if (y >= output->y + output->height)
205 y = output->y + output->height - 1;
206
207 pointer->x = wl_fixed_from_int(x);
208 pointer->y = wl_fixed_from_int(y);
209 }
210
211 pixman_region32_fini(&old_output_region);
212
Hardening57388e42013-09-18 23:56:36 +0200213 /* notify clients of the changes */
214 if (notify_mode_changed || notify_scale_changed) {
215 wl_resource_for_each(resource, &output->resource_list) {
216 if(notify_mode_changed) {
217 wl_output_send_mode(resource,
218 mode->flags | WL_OUTPUT_MODE_CURRENT,
219 mode->width,
220 mode->height,
221 mode->refresh);
222 }
223
224 if (notify_scale_changed)
225 wl_output_send_scale(resource, scale);
226
227 if (wl_resource_get_version(resource) >= 2)
228 wl_output_send_done(resource);
229 }
230 }
231
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200232 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800233}
234
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400235WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500236weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400237{
238 wl_list_insert(&child_process_list, &process->link);
239}
240
Benjamin Franzke06286262011-05-06 19:12:33 +0200241static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242child_client_exec(int sockfd, const char *path)
243{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500244 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200245 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200246 sigset_t allsigs;
247
248 /* do not give our signal mask to the new process */
249 sigfillset(&allsigs);
250 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200251
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100252 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
253 if (seteuid(getuid()) == -1) {
254 weston_log("compositor: failed seteuid\n");
255 return;
256 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500257
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500258 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
259 * non-CLOEXEC fd to pass through exec. */
260 clientfd = dup(sockfd);
261 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200262 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 return;
264 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200265
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500266 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200267 setenv("WAYLAND_SOCKET", s, 1);
268
269 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200270 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200271 path);
272}
273
274WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500275weston_client_launch(struct weston_compositor *compositor,
276 struct weston_process *proc,
277 const char *path,
278 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200279{
280 int sv[2];
281 pid_t pid;
282 struct wl_client *client;
283
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300284 weston_log("launching '%s'\n", path);
285
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300286 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200287 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200288 "socketpair failed while launching '%s': %m\n",
289 path);
290 return NULL;
291 }
292
293 pid = fork();
294 if (pid == -1) {
295 close(sv[0]);
296 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200298 "fork failed while launching '%s': %m\n", path);
299 return NULL;
300 }
301
302 if (pid == 0) {
303 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700304 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200305 }
306
307 close(sv[1]);
308
309 client = wl_client_create(compositor->wl_display, sv[0]);
310 if (!client) {
311 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200312 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200313 "wl_client_create failed while launching '%s'.\n",
314 path);
315 return NULL;
316 }
317
318 proc->pid = pid;
319 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200321
322 return client;
323}
324
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300325struct process_info {
326 struct weston_process proc;
327 char *path;
328};
329
330static void
331process_handle_sigchld(struct weston_process *process, int status)
332{
333 struct process_info *pinfo =
334 container_of(process, struct process_info, proc);
335
336 /*
337 * There are no guarantees whether this runs before or after
338 * the wl_client destructor.
339 */
340
341 if (WIFEXITED(status)) {
342 weston_log("%s exited with status %d\n", pinfo->path,
343 WEXITSTATUS(status));
344 } else if (WIFSIGNALED(status)) {
345 weston_log("%s died on signal %d\n", pinfo->path,
346 WTERMSIG(status));
347 } else {
348 weston_log("%s disappeared\n", pinfo->path);
349 }
350
351 free(pinfo->path);
352 free(pinfo);
353}
354
355WL_EXPORT struct wl_client *
356weston_client_start(struct weston_compositor *compositor, const char *path)
357{
358 struct process_info *pinfo;
359 struct wl_client *client;
360
361 pinfo = zalloc(sizeof *pinfo);
362 if (!pinfo)
363 return NULL;
364
365 pinfo->path = strdup(path);
366 if (!pinfo->path)
367 goto out_free;
368
369 client = weston_client_launch(compositor, &pinfo->proc, path,
370 process_handle_sigchld);
371 if (!client)
372 goto out_str;
373
374 return client;
375
376out_str:
377 free(pinfo->path);
378
379out_free:
380 free(pinfo);
381
382 return NULL;
383}
384
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200385static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300386region_init_infinite(pixman_region32_t *region)
387{
388 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
389 UINT32_MAX, UINT32_MAX);
390}
391
Pekka Paalanene67858b2013-04-25 13:57:42 +0300392static struct weston_subsurface *
393weston_surface_to_subsurface(struct weston_surface *surface);
394
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395WL_EXPORT struct weston_view *
396weston_view_create(struct weston_surface *surface)
397{
398 struct weston_view *view;
399
400 view = calloc(1, sizeof *view);
401 if (view == NULL)
402 return NULL;
403
404 view->surface = surface;
405
Jason Ekstranda7af7042013-10-12 22:38:11 -0500406 /* Assign to surface */
407 wl_list_insert(&surface->views, &view->surface_link);
408
409 wl_signal_init(&view->destroy_signal);
410 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300411 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500412
Xiong Zhang97116532013-10-23 13:58:31 +0800413 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300414 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300415 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416
417 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300418 pixman_region32_init(&view->transform.masked_boundingbox);
419 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420
421 view->alpha = 1.0;
422 pixman_region32_init(&view->transform.opaque);
423
424 wl_list_init(&view->geometry.transformation_list);
425 wl_list_insert(&view->geometry.transformation_list,
426 &view->transform.position.link);
427 weston_matrix_init(&view->transform.position.matrix);
428 wl_list_init(&view->geometry.child_list);
429 pixman_region32_init(&view->transform.boundingbox);
430 view->transform.dirty = 1;
431
432 view->output = NULL;
433
434 return view;
435}
436
Jason Ekstrand108865d2014-06-26 10:04:49 -0700437struct weston_frame_callback {
438 struct wl_resource *resource;
439 struct wl_list link;
440};
441
Pekka Paalanen133e4392014-09-23 22:08:46 -0400442struct weston_presentation_feedback {
443 struct wl_resource *resource;
444
445 /* XXX: could use just wl_resource_get_link() instead */
446 struct wl_list link;
447};
448
449static void
450weston_presentation_feedback_discard(
451 struct weston_presentation_feedback *feedback)
452{
453 presentation_feedback_send_discarded(feedback->resource);
454 wl_resource_destroy(feedback->resource);
455}
456
457static void
458weston_presentation_feedback_discard_list(struct wl_list *list)
459{
460 struct weston_presentation_feedback *feedback, *tmp;
461
462 wl_list_for_each_safe(feedback, tmp, list, link)
463 weston_presentation_feedback_discard(feedback);
464}
465
466static void
467weston_presentation_feedback_present(
468 struct weston_presentation_feedback *feedback,
469 struct weston_output *output,
470 uint32_t refresh_nsec,
471 const struct timespec *ts,
472 uint64_t seq)
473{
474 struct wl_client *client = wl_resource_get_client(feedback->resource);
475 struct wl_resource *o;
476 uint64_t secs;
477 uint32_t flags = 0;
478
479 wl_resource_for_each(o, &output->resource_list) {
480 if (wl_resource_get_client(o) != client)
481 continue;
482
483 presentation_feedback_send_sync_output(feedback->resource, o);
484 }
485
486 secs = ts->tv_sec;
487 presentation_feedback_send_presented(feedback->resource,
488 secs >> 32, secs & 0xffffffff,
489 ts->tv_nsec,
490 refresh_nsec,
491 seq >> 32, seq & 0xffffffff,
492 flags);
493 wl_resource_destroy(feedback->resource);
494}
495
496static void
497weston_presentation_feedback_present_list(struct wl_list *list,
498 struct weston_output *output,
499 uint32_t refresh_nsec,
500 const struct timespec *ts,
501 uint64_t seq)
502{
503 struct weston_presentation_feedback *feedback, *tmp;
504
505 wl_list_for_each_safe(feedback, tmp, list, link)
506 weston_presentation_feedback_present(feedback, output,
507 refresh_nsec, ts, seq);
508}
509
Jason Ekstrand7b982072014-05-20 14:33:03 -0500510static void
511surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
512{
513 struct weston_surface_state *state =
514 container_of(listener, struct weston_surface_state,
515 buffer_destroy_listener);
516
517 state->buffer = NULL;
518}
519
520static void
521weston_surface_state_init(struct weston_surface_state *state)
522{
523 state->newly_attached = 0;
524 state->buffer = NULL;
525 state->buffer_destroy_listener.notify =
526 surface_state_handle_buffer_destroy;
527 state->sx = 0;
528 state->sy = 0;
529
530 pixman_region32_init(&state->damage);
531 pixman_region32_init(&state->opaque);
532 region_init_infinite(&state->input);
533
534 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400535 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500536
537 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
538 state->buffer_viewport.buffer.scale = 1;
539 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
540 state->buffer_viewport.surface.width = -1;
541 state->buffer_viewport.changed = 0;
542}
543
544static void
545weston_surface_state_fini(struct weston_surface_state *state)
546{
547 struct weston_frame_callback *cb, *next;
548
549 wl_list_for_each_safe(cb, next,
550 &state->frame_callback_list, link)
551 wl_resource_destroy(cb->resource);
552
Pekka Paalanen133e4392014-09-23 22:08:46 -0400553 weston_presentation_feedback_discard_list(&state->feedback_list);
554
Jason Ekstrand7b982072014-05-20 14:33:03 -0500555 pixman_region32_fini(&state->input);
556 pixman_region32_fini(&state->opaque);
557 pixman_region32_fini(&state->damage);
558
559 if (state->buffer)
560 wl_list_remove(&state->buffer_destroy_listener.link);
561 state->buffer = NULL;
562}
563
564static void
565weston_surface_state_set_buffer(struct weston_surface_state *state,
566 struct weston_buffer *buffer)
567{
568 if (state->buffer == buffer)
569 return;
570
571 if (state->buffer)
572 wl_list_remove(&state->buffer_destroy_listener.link);
573 state->buffer = buffer;
574 if (state->buffer)
575 wl_signal_add(&state->buffer->destroy_signal,
576 &state->buffer_destroy_listener);
577}
578
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500579WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500580weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500581{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500582 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400583
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200584 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400585 if (surface == NULL)
586 return NULL;
587
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500588 wl_signal_init(&surface->destroy_signal);
589
590 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500591
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500592 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200593 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400594
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200595 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
596 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200597 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
598 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500599
600 weston_surface_state_init(&surface->pending);
601
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400602 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200603
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200604 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100605
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400606 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500607 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300608 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400609
Jason Ekstranda7af7042013-10-12 22:38:11 -0500610 wl_list_init(&surface->views);
611
612 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400613 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500614
Pekka Paalanene67858b2013-04-25 13:57:42 +0300615 wl_list_init(&surface->subsurface_list);
616 wl_list_init(&surface->subsurface_list_pending);
617
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400618 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500619}
620
Alex Wu8811bf92012-02-28 18:07:54 +0800621WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500622weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200623 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500624{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100625 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500626}
627
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400628WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500629weston_view_to_global_float(struct weston_view *view,
630 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200631{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500632 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200633 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
634
Jason Ekstranda7af7042013-10-12 22:38:11 -0500635 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200636
637 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200638 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700639 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200640 v.f[3]);
641 *x = 0;
642 *y = 0;
643 return;
644 }
645
646 *x = v.f[0] / v.f[3];
647 *y = v.f[1] / v.f[3];
648 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500649 *x = sx + view->geometry.x;
650 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200651 }
652}
653
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500654WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200655weston_transformed_coord(int width, int height,
656 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200657 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200658 float sx, float sy, float *bx, float *by)
659{
660 switch (transform) {
661 case WL_OUTPUT_TRANSFORM_NORMAL:
662 default:
663 *bx = sx;
664 *by = sy;
665 break;
666 case WL_OUTPUT_TRANSFORM_FLIPPED:
667 *bx = width - sx;
668 *by = sy;
669 break;
670 case WL_OUTPUT_TRANSFORM_90:
671 *bx = height - sy;
672 *by = sx;
673 break;
674 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
675 *bx = height - sy;
676 *by = width - sx;
677 break;
678 case WL_OUTPUT_TRANSFORM_180:
679 *bx = width - sx;
680 *by = height - sy;
681 break;
682 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
683 *bx = sx;
684 *by = height - sy;
685 break;
686 case WL_OUTPUT_TRANSFORM_270:
687 *bx = sy;
688 *by = width - sx;
689 break;
690 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
691 *bx = sy;
692 *by = sx;
693 break;
694 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200695
696 *bx *= scale;
697 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200698}
699
700WL_EXPORT pixman_box32_t
701weston_transformed_rect(int width, int height,
702 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200703 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200704 pixman_box32_t rect)
705{
706 float x1, x2, y1, y2;
707
708 pixman_box32_t ret;
709
Alexander Larsson4ea95522013-05-22 14:41:37 +0200710 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200711 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200712 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200713 rect.x2, rect.y2, &x2, &y2);
714
715 if (x1 <= x2) {
716 ret.x1 = x1;
717 ret.x2 = x2;
718 } else {
719 ret.x1 = x2;
720 ret.x2 = x1;
721 }
722
723 if (y1 <= y2) {
724 ret.y1 = y1;
725 ret.y2 = y2;
726 } else {
727 ret.y1 = y2;
728 ret.y2 = y1;
729 }
730
731 return ret;
732}
733
734WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500735weston_transformed_region(int width, int height,
736 enum wl_output_transform transform,
737 int32_t scale,
738 pixman_region32_t *src, pixman_region32_t *dest)
739{
740 pixman_box32_t *src_rects, *dest_rects;
741 int nrects, i;
742
743 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
744 if (src != dest)
745 pixman_region32_copy(dest, src);
746 return;
747 }
748
749 src_rects = pixman_region32_rectangles(src, &nrects);
750 dest_rects = malloc(nrects * sizeof(*dest_rects));
751 if (!dest_rects)
752 return;
753
754 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
755 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
756 } else {
757 for (i = 0; i < nrects; i++) {
758 switch (transform) {
759 default:
760 case WL_OUTPUT_TRANSFORM_NORMAL:
761 dest_rects[i].x1 = src_rects[i].x1;
762 dest_rects[i].y1 = src_rects[i].y1;
763 dest_rects[i].x2 = src_rects[i].x2;
764 dest_rects[i].y2 = src_rects[i].y2;
765 break;
766 case WL_OUTPUT_TRANSFORM_90:
767 dest_rects[i].x1 = height - src_rects[i].y2;
768 dest_rects[i].y1 = src_rects[i].x1;
769 dest_rects[i].x2 = height - src_rects[i].y1;
770 dest_rects[i].y2 = src_rects[i].x2;
771 break;
772 case WL_OUTPUT_TRANSFORM_180:
773 dest_rects[i].x1 = width - src_rects[i].x2;
774 dest_rects[i].y1 = height - src_rects[i].y2;
775 dest_rects[i].x2 = width - src_rects[i].x1;
776 dest_rects[i].y2 = height - src_rects[i].y1;
777 break;
778 case WL_OUTPUT_TRANSFORM_270:
779 dest_rects[i].x1 = src_rects[i].y1;
780 dest_rects[i].y1 = width - src_rects[i].x2;
781 dest_rects[i].x2 = src_rects[i].y2;
782 dest_rects[i].y2 = width - src_rects[i].x1;
783 break;
784 case WL_OUTPUT_TRANSFORM_FLIPPED:
785 dest_rects[i].x1 = width - src_rects[i].x2;
786 dest_rects[i].y1 = src_rects[i].y1;
787 dest_rects[i].x2 = width - src_rects[i].x1;
788 dest_rects[i].y2 = src_rects[i].y2;
789 break;
790 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
791 dest_rects[i].x1 = height - src_rects[i].y2;
792 dest_rects[i].y1 = width - src_rects[i].x2;
793 dest_rects[i].x2 = height - src_rects[i].y1;
794 dest_rects[i].y2 = width - src_rects[i].x1;
795 break;
796 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
797 dest_rects[i].x1 = src_rects[i].x1;
798 dest_rects[i].y1 = height - src_rects[i].y2;
799 dest_rects[i].x2 = src_rects[i].x2;
800 dest_rects[i].y2 = height - src_rects[i].y1;
801 break;
802 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
803 dest_rects[i].x1 = src_rects[i].y1;
804 dest_rects[i].y1 = src_rects[i].x1;
805 dest_rects[i].x2 = src_rects[i].y2;
806 dest_rects[i].y2 = src_rects[i].x2;
807 break;
808 }
809 }
810 }
811
812 if (scale != 1) {
813 for (i = 0; i < nrects; i++) {
814 dest_rects[i].x1 *= scale;
815 dest_rects[i].x2 *= scale;
816 dest_rects[i].y1 *= scale;
817 dest_rects[i].y2 *= scale;
818 }
819 }
820
821 pixman_region32_clear(dest);
822 pixman_region32_init_rects(dest, dest_rects, nrects);
823 free(dest_rects);
824}
825
Jonny Lamb74130762013-11-26 18:19:46 +0100826static void
827scaler_surface_to_buffer(struct weston_surface *surface,
828 float sx, float sy, float *bx, float *by)
829{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200830 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200831 double src_width, src_height;
832 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200833
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200834 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
835 if (vp->surface.width == -1) {
836 *bx = sx;
837 *by = sy;
838 return;
839 }
Jonny Lamb74130762013-11-26 18:19:46 +0100840
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200841 src_x = 0.0;
842 src_y = 0.0;
843 src_width = surface->width_from_buffer;
844 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100845 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200846 src_x = wl_fixed_to_double(vp->buffer.src_x);
847 src_y = wl_fixed_to_double(vp->buffer.src_y);
848 src_width = wl_fixed_to_double(vp->buffer.src_width);
849 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100850 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200851
852 *bx = sx * src_width / surface->width + src_x;
853 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100854}
855
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500856WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200857weston_surface_to_buffer_float(struct weston_surface *surface,
858 float sx, float sy, float *bx, float *by)
859{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
861
Jonny Lamb74130762013-11-26 18:19:46 +0100862 /* first transform coordinates if the scaler is set */
863 scaler_surface_to_buffer(surface, sx, sy, bx, by);
864
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500865 weston_transformed_coord(surface->width_from_buffer,
866 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200867 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100868 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200869}
870
Alexander Larsson4ea95522013-05-22 14:41:37 +0200871WL_EXPORT void
872weston_surface_to_buffer(struct weston_surface *surface,
873 int sx, int sy, int *bx, int *by)
874{
875 float bxf, byf;
876
Jonny Lamb74130762013-11-26 18:19:46 +0100877 weston_surface_to_buffer_float(surface,
878 sx, sy, &bxf, &byf);
879
Alexander Larsson4ea95522013-05-22 14:41:37 +0200880 *bx = floorf(bxf);
881 *by = floorf(byf);
882}
883
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200884WL_EXPORT pixman_box32_t
885weston_surface_to_buffer_rect(struct weston_surface *surface,
886 pixman_box32_t rect)
887{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200888 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100889 float xf, yf;
890
891 /* first transform box coordinates if the scaler is set */
892 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
893 rect.x1 = floorf(xf);
894 rect.y1 = floorf(yf);
895
896 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
897 rect.x2 = floorf(xf);
898 rect.y2 = floorf(yf);
899
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500900 return weston_transformed_rect(surface->width_from_buffer,
901 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200902 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200903 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200904}
905
906WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400908 struct weston_plane *plane)
909{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400911 return;
912
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 weston_view_damage_below(view);
914 view->plane = plane;
915 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400916}
917
918WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200920{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500921 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200922
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500923 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300924 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800926 if (view->plane)
927 pixman_region32_union(&view->plane->damage,
928 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500929 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800930 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200931}
932
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400933static void
934weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
935{
936 uint32_t different = es->output_mask ^ mask;
937 uint32_t entered = mask & different;
938 uint32_t left = es->output_mask & different;
939 struct weston_output *output;
940 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500941 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942
943 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500944 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400945 return;
946 if (different == 0)
947 return;
948
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500949 client = wl_resource_get_client(es->resource);
950
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400951 wl_list_for_each(output, &es->compositor->output_list, link) {
952 if (1 << output->id & different)
953 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500954 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400955 client);
956 if (resource == NULL)
957 continue;
958 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500959 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400960 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500961 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400962 }
963}
964
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200965
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400966static void
967weston_surface_assign_output(struct weston_surface *es)
968{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 struct weston_output *new_output;
970 struct weston_view *view;
971 pixman_region32_t region;
972 uint32_t max, area, mask;
973 pixman_box32_t *e;
974
975 new_output = NULL;
976 max = 0;
977 mask = 0;
978 pixman_region32_init(&region);
979 wl_list_for_each(view, &es->views, surface_link) {
980 if (!view->output)
981 continue;
982
983 pixman_region32_intersect(&region, &view->transform.boundingbox,
984 &view->output->region);
985
986 e = pixman_region32_extents(&region);
987 area = (e->x2 - e->x1) * (e->y2 - e->y1);
988
989 mask |= view->output_mask;
990
991 if (area >= max) {
992 new_output = view->output;
993 max = area;
994 }
995 }
996 pixman_region32_fini(&region);
997
998 es->output = new_output;
999 weston_surface_update_output_mask(es, mask);
1000}
1001
1002static void
1003weston_view_assign_output(struct weston_view *ev)
1004{
1005 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001006 struct weston_output *output, *new_output;
1007 pixman_region32_t region;
1008 uint32_t max, area, mask;
1009 pixman_box32_t *e;
1010
1011 new_output = NULL;
1012 max = 0;
1013 mask = 0;
1014 pixman_region32_init(&region);
1015 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001017 &output->region);
1018
1019 e = pixman_region32_extents(&region);
1020 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1021
1022 if (area > 0)
1023 mask |= 1 << output->id;
1024
1025 if (area >= max) {
1026 new_output = output;
1027 max = area;
1028 }
1029 }
1030 pixman_region32_fini(&region);
1031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 ev->output = new_output;
1033 ev->output_mask = mask;
1034
1035 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036}
1037
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001038static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1040 int32_t width, int32_t height,
1041 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001042{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001043 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1044 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001045 int32_t s[4][2] = {
1046 { sx, sy },
1047 { sx, sy + height },
1048 { sx + width, sy },
1049 { sx + width, sy + height }
1050 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001051 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001052 int i;
1053
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001054 if (width == 0 || height == 0) {
1055 /* avoid rounding empty bbox to 1x1 */
1056 pixman_region32_init(bbox);
1057 return;
1058 }
1059
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001060 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001061 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001063 if (x < min_x)
1064 min_x = x;
1065 if (x > max_x)
1066 max_x = x;
1067 if (y < min_y)
1068 min_y = y;
1069 if (y > max_y)
1070 max_y = y;
1071 }
1072
Pekka Paalanen219b9822012-02-08 15:38:37 +02001073 int_x = floorf(min_x);
1074 int_y = floorf(min_y);
1075 pixman_region32_init_rect(bbox, int_x, int_y,
1076 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001077}
1078
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001079static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001081{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001083
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001084 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 view->geometry.x = roundf(view->geometry.x);
1086 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001087
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001088 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1090 view->transform.position.matrix.d[12] = view->geometry.x;
1091 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001092
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001094
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 view->transform.inverse = view->transform.position.matrix;
1096 view->transform.inverse.d[12] = -view->geometry.x;
1097 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001098
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 pixman_region32_init_rect(&view->transform.boundingbox,
1100 view->geometry.x,
1101 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001102 view->surface->width,
1103 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001104
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 if (view->alpha == 1.0) {
1106 pixman_region32_copy(&view->transform.opaque,
1107 &view->surface->opaque);
1108 pixman_region32_translate(&view->transform.opaque,
1109 view->geometry.x,
1110 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001111 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001112}
1113
1114static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001116{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 struct weston_view *parent = view->geometry.parent;
1118 struct weston_matrix *matrix = &view->transform.matrix;
1119 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001120 struct weston_transform *tform;
1121
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001123
1124 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1126 view->transform.position.matrix.d[12] = view->geometry.x;
1127 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001128
1129 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001131 weston_matrix_multiply(matrix, &tform->matrix);
1132
Pekka Paalanen483243f2013-03-08 14:56:50 +02001133 if (parent)
1134 weston_matrix_multiply(matrix, &parent->transform.matrix);
1135
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001136 if (weston_matrix_invert(inverse, matrix) < 0) {
1137 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 weston_log("error: weston_view %p"
1139 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001140 return -1;
1141 }
1142
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001143 view_compute_bbox(view, 0, 0,
1144 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001146
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001147 return 0;
1148}
1149
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001150static struct weston_layer *
1151get_view_layer(struct weston_view *view)
1152{
1153 if (view->parent_view)
1154 return get_view_layer(view->parent_view);
1155 return view->layer_link.layer;
1156}
1157
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001160{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001162 struct weston_layer *layer;
1163 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001166 return;
1167
Pekka Paalanen483243f2013-03-08 14:56:50 +02001168 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001169 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001170
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001172
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001174
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 pixman_region32_fini(&view->transform.boundingbox);
1176 pixman_region32_fini(&view->transform.opaque);
1177 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001178
Pekka Paalanencd403622012-01-25 13:37:39 +02001179 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 if (view->geometry.transformation_list.next ==
1181 &view->transform.position.link &&
1182 view->geometry.transformation_list.prev ==
1183 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001184 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001186 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 if (weston_view_update_transform_enable(view) < 0)
1188 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001189 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001190
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001191 layer = get_view_layer(view);
1192 if (layer) {
1193 pixman_region32_init_with_extents(&mask, &layer->mask);
1194 pixman_region32_intersect(&view->transform.masked_boundingbox,
1195 &view->transform.boundingbox, &mask);
1196 pixman_region32_intersect(&view->transform.masked_opaque,
1197 &view->transform.opaque, &mask);
1198 pixman_region32_fini(&mask);
1199 }
1200
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 wl_signal_emit(&view->surface->compositor->transform_signal,
1206 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001207}
1208
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001209WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001211{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001213
1214 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 * The invariant: if view->geometry.dirty, then all views
1216 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001217 * Corollary: if not parent->geometry.dirty, then all ancestors
1218 * are not dirty.
1219 */
1220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222 return;
1223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001229}
1230
1231WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232weston_view_to_global_fixed(struct weston_view *view,
1233 wl_fixed_t vx, wl_fixed_t vy,
1234 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001235{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001236 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001237
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 weston_view_to_global_float(view,
1239 wl_fixed_to_double(vx),
1240 wl_fixed_to_double(vy),
1241 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001242 *x = wl_fixed_from_double(xf);
1243 *y = wl_fixed_from_double(yf);
1244}
1245
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001246WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247weston_view_from_global_float(struct weston_view *view,
1248 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001249{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001251 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1252
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001254
1255 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001256 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001258 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 *vx = 0;
1260 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001261 return;
1262 }
1263
Jason Ekstranda7af7042013-10-12 22:38:11 -05001264 *vx = v.f[0] / v.f[3];
1265 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001266 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001267 *vx = x - view->geometry.x;
1268 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001269 }
1270}
1271
1272WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273weston_view_from_global_fixed(struct weston_view *view,
1274 wl_fixed_t x, wl_fixed_t y,
1275 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001276{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001278
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 weston_view_from_global_float(view,
1280 wl_fixed_to_double(x),
1281 wl_fixed_to_double(y),
1282 &vxf, &vyf);
1283 *vx = wl_fixed_from_double(vxf);
1284 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001285}
1286
1287WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288weston_view_from_global(struct weston_view *view,
1289 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001290{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1294 *vx = floorf(vxf);
1295 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001296}
1297
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001298WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001299weston_surface_schedule_repaint(struct weston_surface *surface)
1300{
1301 struct weston_output *output;
1302
1303 wl_list_for_each(output, &surface->compositor->output_list, link)
1304 if (surface->output_mask & (1 << output->id))
1305 weston_output_schedule_repaint(output);
1306}
1307
1308WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309weston_view_schedule_repaint(struct weston_view *view)
1310{
1311 struct weston_output *output;
1312
1313 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1314 if (view->output_mask & (1 << output->id))
1315 weston_output_schedule_repaint(output);
1316}
1317
1318WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001319weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001320{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001321 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 0, 0, surface->width,
1323 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001324
Kristian Høgsberg98238702012-08-03 16:29:12 -04001325 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001326}
1327
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001328WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001329weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001330{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001331 if (view->geometry.x == x && view->geometry.y == y)
1332 return;
1333
Jason Ekstranda7af7042013-10-12 22:38:11 -05001334 view->geometry.x = x;
1335 view->geometry.y = y;
1336 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001337}
1338
Pekka Paalanen483243f2013-03-08 14:56:50 +02001339static void
1340transform_parent_handle_parent_destroy(struct wl_listener *listener,
1341 void *data)
1342{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 struct weston_view *view =
1344 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001345 geometry.parent_destroy_listener);
1346
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001348}
1349
1350WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351weston_view_set_transform_parent(struct weston_view *view,
1352 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001353{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001354 if (view->geometry.parent) {
1355 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1356 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357 }
1358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001362 transform_parent_handle_parent_destroy;
1363 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001364 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001366 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001368 }
1369
Jason Ekstranda7af7042013-10-12 22:38:11 -05001370 weston_view_geometry_dirty(view);
1371}
1372
1373WL_EXPORT int
1374weston_view_is_mapped(struct weston_view *view)
1375{
1376 if (view->output)
1377 return 1;
1378 else
1379 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001380}
1381
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001382WL_EXPORT int
1383weston_surface_is_mapped(struct weston_surface *surface)
1384{
1385 if (surface->output)
1386 return 1;
1387 else
1388 return 0;
1389}
1390
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001391static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001392surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001393{
1394 struct weston_view *view;
1395
1396 if (surface->width == width && surface->height == height)
1397 return;
1398
1399 surface->width = width;
1400 surface->height = height;
1401
1402 wl_list_for_each(view, &surface->views, surface_link)
1403 weston_view_geometry_dirty(view);
1404}
1405
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001406WL_EXPORT void
1407weston_surface_set_size(struct weston_surface *surface,
1408 int32_t width, int32_t height)
1409{
1410 assert(!surface->resource);
1411 surface_set_size(surface, width, height);
1412}
1413
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001414static int
1415fixed_round_up_to_int(wl_fixed_t f)
1416{
1417 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1418}
1419
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001420static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001421weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001422{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001423 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001424 int32_t width, height;
1425
1426 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001427 surface->width_from_buffer = 0;
1428 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001429 return;
1430 }
1431
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001432 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001433 case WL_OUTPUT_TRANSFORM_90:
1434 case WL_OUTPUT_TRANSFORM_270:
1435 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1436 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001437 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1438 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001439 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001440 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001441 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1442 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001443 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001444 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001445
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001446 surface->width_from_buffer = width;
1447 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001448}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001449
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001450static void
1451weston_surface_update_size(struct weston_surface *surface)
1452{
1453 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1454 int32_t width, height;
1455
1456 width = surface->width_from_buffer;
1457 height = surface->height_from_buffer;
1458
1459 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001460 surface_set_size(surface,
1461 vp->surface.width, vp->surface.height);
1462 return;
1463 }
1464
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001465 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001466 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1467 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1468
1469 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001470 return;
1471 }
1472
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001473 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001474}
1475
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001476WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001477weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001478{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001479 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001480
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001481 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001482
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001483 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001484}
1485
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486WL_EXPORT struct weston_view *
1487weston_compositor_pick_view(struct weston_compositor *compositor,
1488 wl_fixed_t x, wl_fixed_t y,
1489 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001490{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001491 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001492 int ix = wl_fixed_to_int(x);
1493 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001494
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 wl_list_for_each(view, &compositor->view_list, link) {
1496 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001497 if (pixman_region32_contains_point(
1498 &view->transform.masked_boundingbox,
1499 ix, iy, NULL) &&
1500 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 wl_fixed_to_int(*vx),
1502 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001503 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001505 }
1506
1507 return NULL;
1508}
1509
1510static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001511weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001512{
Daniel Stone37816df2012-05-16 18:45:18 +01001513 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001514
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001515 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001516 return;
1517
Daniel Stone37816df2012-05-16 18:45:18 +01001518 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001519 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001520}
1521
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001522WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001523weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001524{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001525 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001526
Jason Ekstranda7af7042013-10-12 22:38:11 -05001527 if (!weston_view_is_mapped(view))
1528 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 weston_view_damage_below(view);
1531 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001532 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001533 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 wl_list_remove(&view->link);
1535 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001536 view->output_mask = 0;
1537 weston_surface_assign_output(view->surface);
1538
1539 if (weston_surface_is_mapped(view->surface))
1540 return;
1541
1542 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1543 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001544 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001546 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001547 NULL,
1548 wl_fixed_from_int(0),
1549 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001551 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001552 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555WL_EXPORT void
1556weston_surface_unmap(struct weston_surface *surface)
1557{
1558 struct weston_view *view;
1559
1560 wl_list_for_each(view, &surface->views, surface_link)
1561 weston_view_unmap(view);
1562 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001563}
1564
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001565static void
1566weston_surface_reset_pending_buffer(struct weston_surface *surface)
1567{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001568 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001569 surface->pending.sx = 0;
1570 surface->pending.sy = 0;
1571 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001572 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001573}
1574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575WL_EXPORT void
1576weston_view_destroy(struct weston_view *view)
1577{
1578 wl_signal_emit(&view->destroy_signal, view);
1579
1580 assert(wl_list_empty(&view->geometry.child_list));
1581
1582 if (weston_view_is_mapped(view)) {
1583 weston_view_unmap(view);
1584 weston_compositor_build_view_list(view->surface->compositor);
1585 }
1586
1587 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001588 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589
1590 pixman_region32_fini(&view->clip);
1591 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001592 pixman_region32_fini(&view->transform.masked_boundingbox);
1593 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594
1595 weston_view_set_transform_parent(view, NULL);
1596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 wl_list_remove(&view->surface_link);
1598
1599 free(view);
1600}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001601
1602WL_EXPORT void
1603weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001604{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001605 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001607
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001608 if (--surface->ref_count > 0)
1609 return;
1610
1611 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1612
Pekka Paalanene67858b2013-04-25 13:57:42 +03001613 assert(wl_list_empty(&surface->subsurface_list_pending));
1614 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001615
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1617 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001618
Jason Ekstrand7b982072014-05-20 14:33:03 -05001619 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001620
Pekka Paalanende685b82012-12-04 15:58:12 +02001621 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001622
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001623 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001624 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001625 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001626
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001627 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001628 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001629
Pekka Paalanen133e4392014-09-23 22:08:46 -04001630 weston_presentation_feedback_discard_list(&surface->feedback_list);
1631
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001632 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001633}
1634
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001635static void
1636destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001637{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001638 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001639
Giulio Camuffo0d379742013-11-15 22:06:15 +01001640 /* Set the resource to NULL, since we don't want to leave a
1641 * dangling pointer if the surface was refcounted and survives
1642 * the weston_surface_destroy() call. */
1643 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001644 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001645}
1646
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001647static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001648weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1649{
1650 struct weston_buffer *buffer =
1651 container_of(listener, struct weston_buffer, destroy_listener);
1652
1653 wl_signal_emit(&buffer->destroy_signal, buffer);
1654 free(buffer);
1655}
1656
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001657WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001658weston_buffer_from_resource(struct wl_resource *resource)
1659{
1660 struct weston_buffer *buffer;
1661 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001662
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001663 listener = wl_resource_get_destroy_listener(resource,
1664 weston_buffer_destroy_handler);
1665
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001666 if (listener)
1667 return container_of(listener, struct weston_buffer,
1668 destroy_listener);
1669
1670 buffer = zalloc(sizeof *buffer);
1671 if (buffer == NULL)
1672 return NULL;
1673
1674 buffer->resource = resource;
1675 wl_signal_init(&buffer->destroy_signal);
1676 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001677 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001678 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001679
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001680 return buffer;
1681}
1682
1683static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001684weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1685 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001686{
Pekka Paalanende685b82012-12-04 15:58:12 +02001687 struct weston_buffer_reference *ref =
1688 container_of(listener, struct weston_buffer_reference,
1689 destroy_listener);
1690
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001691 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001692 ref->buffer = NULL;
1693}
1694
1695WL_EXPORT void
1696weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001697 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001698{
1699 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001700 ref->buffer->busy_count--;
1701 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001702 assert(wl_resource_get_client(ref->buffer->resource));
1703 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001704 WL_BUFFER_RELEASE);
1705 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001706 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001707 }
1708
Pekka Paalanende685b82012-12-04 15:58:12 +02001709 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001710 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001711 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001712 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001713 }
1714
Pekka Paalanende685b82012-12-04 15:58:12 +02001715 ref->buffer = buffer;
1716 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1717}
1718
1719static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001720weston_surface_attach(struct weston_surface *surface,
1721 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001722{
1723 weston_buffer_reference(&surface->buffer_ref, buffer);
1724
Pekka Paalanena6421c42012-12-04 15:58:10 +02001725 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001726 if (weston_surface_is_mapped(surface))
1727 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001728 }
1729
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001730 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001731
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001732 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001733 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001734}
1735
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001736WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001737weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001738{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001739 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001740
1741 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001742 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001743}
1744
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001745WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001746weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001747{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001748 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001749
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001750 pixman_region32_union(&compositor->primary_plane.damage,
1751 &compositor->primary_plane.damage,
1752 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001753 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001754}
1755
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001756static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001758{
Pekka Paalanende685b82012-12-04 15:58:12 +02001759 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001760 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001761 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001762
Jason Ekstrandef540082014-06-26 10:37:36 -07001763 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001764}
1765
1766static void
1767view_accumulate_damage(struct weston_view *view,
1768 pixman_region32_t *opaque)
1769{
1770 pixman_region32_t damage;
1771
1772 pixman_region32_init(&damage);
1773 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001774 pixman_box32_t *extents;
1775
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 extents = pixman_region32_extents(&view->surface->damage);
1777 view_compute_bbox(view, extents->x1, extents->y1,
1778 extents->x2 - extents->x1,
1779 extents->y2 - extents->y1,
1780 &damage);
1781 pixman_region32_translate(&damage,
1782 -view->plane->x,
1783 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001784 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 pixman_region32_copy(&damage, &view->surface->damage);
1786 pixman_region32_translate(&damage,
1787 view->geometry.x - view->plane->x,
1788 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001789 }
1790
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 pixman_region32_subtract(&damage, &damage, opaque);
1792 pixman_region32_union(&view->plane->damage,
1793 &view->plane->damage, &damage);
1794 pixman_region32_fini(&damage);
1795 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001796 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001797}
1798
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001799static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001800compositor_accumulate_damage(struct weston_compositor *ec)
1801{
1802 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001804 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001805
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001806 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001807
1808 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001809 pixman_region32_copy(&plane->clip, &clip);
1810
1811 pixman_region32_init(&opaque);
1812
Jason Ekstranda7af7042013-10-12 22:38:11 -05001813 wl_list_for_each(ev, &ec->view_list, link) {
1814 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001815 continue;
1816
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001818 }
1819
1820 pixman_region32_union(&clip, &clip, &opaque);
1821 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001822 }
1823
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001824 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001825
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826 wl_list_for_each(ev, &ec->view_list, link)
1827 ev->surface->touched = 0;
1828
1829 wl_list_for_each(ev, &ec->view_list, link) {
1830 if (ev->surface->touched)
1831 continue;
1832 ev->surface->touched = 1;
1833
1834 surface_flush_damage(ev->surface);
1835
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001836 /* Both the renderer and the backend have seen the buffer
1837 * by now. If renderer needs the buffer, it has its own
1838 * reference set. If the backend wants to keep the buffer
1839 * around for migrating the surface into a non-primary plane
1840 * later, keep_buffer is true. Otherwise, drop the core
1841 * reference now, and allow early buffer release. This enables
1842 * clients to use single-buffering.
1843 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844 if (!ev->surface->keep_buffer)
1845 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001846 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001847}
1848
1849static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001851{
1852 struct weston_subsurface *sub;
1853
Pekka Paalanene67858b2013-04-25 13:57:42 +03001854 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001855 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001856 continue;
1857
Jason Ekstranda7af7042013-10-12 22:38:11 -05001858 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1859 wl_list_init(&sub->surface->views);
1860
1861 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001862 }
1863}
1864
1865static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001866surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001867{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 struct weston_subsurface *sub;
1869 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001870
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1872 if (sub->surface == surface)
1873 continue;
1874
George Kiagiadakised04d382014-06-13 18:10:26 +02001875 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1876 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001877 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001878 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001879
1880 surface_free_unused_subsurface_views(sub->surface);
1881 }
1882}
1883
1884static void
1885view_list_add_subsurface_view(struct weston_compositor *compositor,
1886 struct weston_subsurface *sub,
1887 struct weston_view *parent)
1888{
1889 struct weston_subsurface *child;
1890 struct weston_view *view = NULL, *iv;
1891
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001892 if (!weston_surface_is_mapped(sub->surface))
1893 return;
1894
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1896 if (iv->geometry.parent == parent) {
1897 view = iv;
1898 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001899 }
1900 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901
1902 if (view) {
1903 /* Put it back in the surface's list of views */
1904 wl_list_remove(&view->surface_link);
1905 wl_list_insert(&sub->surface->views, &view->surface_link);
1906 } else {
1907 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001908 weston_view_set_position(view,
1909 sub->position.x,
1910 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001911 weston_view_set_transform_parent(view, parent);
1912 }
1913
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001914 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001916
Pekka Paalanenb188e912013-11-19 14:03:35 +02001917 if (wl_list_empty(&sub->surface->subsurface_list)) {
1918 wl_list_insert(compositor->view_list.prev, &view->link);
1919 return;
1920 }
1921
1922 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1923 if (child->surface == sub->surface)
1924 wl_list_insert(compositor->view_list.prev, &view->link);
1925 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001926 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001927 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928}
1929
1930static void
1931view_list_add(struct weston_compositor *compositor,
1932 struct weston_view *view)
1933{
1934 struct weston_subsurface *sub;
1935
1936 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001937
Pekka Paalanenb188e912013-11-19 14:03:35 +02001938 if (wl_list_empty(&view->surface->subsurface_list)) {
1939 wl_list_insert(compositor->view_list.prev, &view->link);
1940 return;
1941 }
1942
1943 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1944 if (sub->surface == view->surface)
1945 wl_list_insert(compositor->view_list.prev, &view->link);
1946 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001948 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001949}
1950
1951static void
1952weston_compositor_build_view_list(struct weston_compositor *compositor)
1953{
1954 struct weston_view *view;
1955 struct weston_layer *layer;
1956
1957 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001958 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 surface_stash_subsurface_views(view->surface);
1960
1961 wl_list_init(&compositor->view_list);
1962 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001963 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001964 view_list_add(compositor, view);
1965 }
1966 }
1967
1968 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001969 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001971}
1972
David Herrmann1edf44c2013-10-22 17:11:26 +02001973static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001974weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001975{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001976 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001978 struct weston_animation *animation, *next;
1979 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001980 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001981 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001982 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001983
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001984 if (output->destroying)
1985 return 0;
1986
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001987 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001989
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001990 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001991 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001992 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001993 wl_list_for_each(ev, &ec->view_list, link)
1994 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001995
Pekka Paalanene67858b2013-04-25 13:57:42 +03001996 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 wl_list_for_each(ev, &ec->view_list, link) {
1998 /* Note: This operation is safe to do multiple times on the
1999 * same surface.
2000 */
2001 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002002 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002003 &ev->surface->frame_callback_list);
2004 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002005
2006 wl_list_insert_list(&output->feedback_list,
2007 &ev->surface->feedback_list);
2008 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002009 }
2010 }
2011
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002012 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002013
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002014 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002015 pixman_region32_intersect(&output_damage,
2016 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002017 pixman_region32_subtract(&output_damage,
2018 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002019
Scott Moreauccbf29d2012-02-22 14:21:41 -07002020 if (output->dirty)
2021 weston_output_update_matrix(output);
2022
David Herrmann1edf44c2013-10-22 17:11:26 +02002023 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002024
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002025 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002026
Kristian Høgsbergef044142011-06-21 15:02:12 -04002027 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002028
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002029 weston_compositor_repick(ec);
2030 wl_event_loop_dispatch(ec->input_loop, 0);
2031
Jonas Ådahldb773762012-06-13 00:01:21 +02002032 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002033 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002034 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002035 }
2036
Scott Moreaud64cf212012-06-08 19:40:54 -06002037 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002038 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002039 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002040 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002041
2042 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002043}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002044
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002045static int
2046weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002047{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002048 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002049
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002050 wl_event_loop_dispatch(compositor->input_loop, 0);
2051
2052 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002053}
2054
2055WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002056weston_output_finish_frame(struct weston_output *output,
2057 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002058{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002059 struct weston_compositor *compositor = output->compositor;
2060 struct wl_event_loop *loop =
2061 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002062 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002063 uint32_t refresh_nsec;
2064
2065 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2066 weston_presentation_feedback_present_list(&output->feedback_list,
2067 output, refresh_nsec, stamp,
2068 0);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002069
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002070 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002071
2072 if (output->repaint_needed &&
2073 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2074 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002075 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002076 if (!r)
2077 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002078 }
2079
2080 output->repaint_scheduled = 0;
2081 if (compositor->input_loop_source)
2082 return;
2083
2084 fd = wl_event_loop_get_fd(compositor->input_loop);
2085 compositor->input_loop_source =
2086 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2087 weston_compositor_read_input, compositor);
2088}
2089
2090static void
2091idle_repaint(void *data)
2092{
2093 struct weston_output *output = data;
2094
Jonas Ådahle5a12252013-04-05 23:07:11 +02002095 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002096}
2097
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002098WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002099weston_layer_entry_insert(struct weston_layer_entry *list,
2100 struct weston_layer_entry *entry)
2101{
2102 wl_list_insert(&list->link, &entry->link);
2103 entry->layer = list->layer;
2104}
2105
2106WL_EXPORT void
2107weston_layer_entry_remove(struct weston_layer_entry *entry)
2108{
2109 wl_list_remove(&entry->link);
2110 wl_list_init(&entry->link);
2111 entry->layer = NULL;
2112}
2113
2114WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002115weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2116{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002117 wl_list_init(&layer->view_list.link);
2118 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002119 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002120 if (below != NULL)
2121 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002122}
2123
2124WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002125weston_layer_set_mask(struct weston_layer *layer,
2126 int x, int y, int width, int height)
2127{
2128 struct weston_view *view;
2129
2130 layer->mask.x1 = x;
2131 layer->mask.x2 = x + width;
2132 layer->mask.y1 = y;
2133 layer->mask.y2 = y + height;
2134
2135 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2136 weston_view_geometry_dirty(view);
2137 }
2138}
2139
2140WL_EXPORT void
2141weston_layer_set_mask_infinite(struct weston_layer *layer)
2142{
2143 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2144 UINT32_MAX, UINT32_MAX);
2145}
2146
2147WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002148weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002149{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002150 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002151 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002152
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002153 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2154 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002155 return;
2156
Kristian Høgsbergef044142011-06-21 15:02:12 -04002157 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002158 output->repaint_needed = 1;
2159 if (output->repaint_scheduled)
2160 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002161
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002162 wl_event_loop_add_idle(loop, idle_repaint, output);
2163 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002164
2165 if (compositor->input_loop_source) {
2166 wl_event_source_remove(compositor->input_loop_source);
2167 compositor->input_loop_source = NULL;
2168 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002169}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002170
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002171WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002172weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2173{
2174 struct weston_output *output;
2175
2176 wl_list_for_each(output, &compositor->output_list, link)
2177 weston_output_schedule_repaint(output);
2178}
2179
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002180static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002181surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002182{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002183 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002184}
2185
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002186static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002187surface_attach(struct wl_client *client,
2188 struct wl_resource *resource,
2189 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2190{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002191 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002192 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002193
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002194 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002195 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002196 if (buffer == NULL) {
2197 wl_client_post_no_memory(client);
2198 return;
2199 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002200 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002201
Pekka Paalanende685b82012-12-04 15:58:12 +02002202 /* Attach, attach, without commit in between does not send
2203 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002204 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002205
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002206 surface->pending.sx = sx;
2207 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002208 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002209}
2210
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002211static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002212surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002213 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002214 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002215{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002216 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002217
Pekka Paalanen8e159182012-10-10 12:49:25 +03002218 pixman_region32_union_rect(&surface->pending.damage,
2219 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002220 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002221}
2222
Kristian Høgsberg33418202011-08-16 23:01:28 -04002223static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002224destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002225{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002226 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002227
2228 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002229 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002230}
2231
2232static void
2233surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002234 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002235{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002236 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002237 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002238
2239 cb = malloc(sizeof *cb);
2240 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002241 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002242 return;
2243 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002244
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002245 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2246 callback);
2247 if (cb->resource == NULL) {
2248 free(cb);
2249 wl_resource_post_no_memory(resource);
2250 return;
2251 }
2252
Jason Ekstranda85118c2013-06-27 20:17:02 -05002253 wl_resource_set_implementation(cb->resource, NULL, cb,
2254 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002255
Pekka Paalanenbc106382012-10-10 12:49:31 +03002256 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002257}
2258
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002259static void
2260surface_set_opaque_region(struct wl_client *client,
2261 struct wl_resource *resource,
2262 struct wl_resource *region_resource)
2263{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002264 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002265 struct weston_region *region;
2266
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002267 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002268 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002269 pixman_region32_copy(&surface->pending.opaque,
2270 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002271 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002272 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002273 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002274}
2275
2276static void
2277surface_set_input_region(struct wl_client *client,
2278 struct wl_resource *resource,
2279 struct wl_resource *region_resource)
2280{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002281 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002282 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002283
2284 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002285 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002286 pixman_region32_copy(&surface->pending.input,
2287 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002288 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002289 pixman_region32_fini(&surface->pending.input);
2290 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002291 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002292}
2293
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002294static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002295weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002296{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002297 struct weston_subsurface *sub;
2298
2299 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2300 parent_link_pending) {
2301 wl_list_remove(&sub->parent_link);
2302 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2303 }
2304}
2305
2306static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002307weston_surface_commit_state(struct weston_surface *surface,
2308 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002309{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002311 pixman_region32_t opaque;
2312
Alexander Larsson4ea95522013-05-22 14:41:37 +02002313 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002314 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002315 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002316 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002317
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002318 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002319 if (state->newly_attached)
2320 weston_surface_attach(surface, state->buffer);
2321 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002322
Jason Ekstrand7b982072014-05-20 14:33:03 -05002323 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002324 weston_surface_update_size(surface);
2325 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002326 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002327 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002328
Jason Ekstrand7b982072014-05-20 14:33:03 -05002329 state->sx = 0;
2330 state->sy = 0;
2331 state->newly_attached = 0;
2332 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002333
2334 /* wl_surface.damage */
2335 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002336 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002337 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002338 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002339 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002340
2341 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002342 pixman_region32_init(&opaque);
2343 pixman_region32_intersect_rect(&opaque, &state->opaque,
2344 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002345
2346 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2347 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 wl_list_for_each(view, &surface->views, surface_link)
2349 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002350 }
2351
2352 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002353
2354 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002355 pixman_region32_intersect_rect(&surface->input, &state->input,
2356 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002357
Pekka Paalanenbc106382012-10-10 12:49:31 +03002358 /* wl_surface.frame */
2359 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002360 &state->frame_callback_list);
2361 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002362
2363 /* XXX:
2364 * What should happen with a feedback request, if there
2365 * is no wl_buffer attached for this commit?
2366 */
2367
2368 /* presentation.feedback */
2369 wl_list_insert_list(&surface->feedback_list,
2370 &state->feedback_list);
2371 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002372}
2373
2374static void
2375weston_surface_commit(struct weston_surface *surface)
2376{
2377 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002378
Pekka Paalanene67858b2013-04-25 13:57:42 +03002379 weston_surface_commit_subsurface_order(surface);
2380
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002381 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002382}
2383
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002384static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002385weston_subsurface_commit(struct weston_subsurface *sub);
2386
2387static void
2388weston_subsurface_parent_commit(struct weston_subsurface *sub,
2389 int parent_is_synchronized);
2390
2391static void
2392surface_commit(struct wl_client *client, struct wl_resource *resource)
2393{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002394 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002395 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2396
2397 if (sub) {
2398 weston_subsurface_commit(sub);
2399 return;
2400 }
2401
2402 weston_surface_commit(surface);
2403
2404 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2405 if (sub->surface != surface)
2406 weston_subsurface_parent_commit(sub, 0);
2407 }
2408}
2409
2410static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002411surface_set_buffer_transform(struct wl_client *client,
2412 struct wl_resource *resource, int transform)
2413{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002414 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002415
Jonny Lamba55f1392014-05-30 12:07:15 +02002416 /* if wl_output.transform grows more members this will need to be updated. */
2417 if (transform < 0 ||
2418 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2419 wl_resource_post_error(resource,
2420 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2421 "buffer transform must be a valid transform "
2422 "('%d' specified)", transform);
2423 return;
2424 }
2425
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002426 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002427 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002428}
2429
Alexander Larsson4ea95522013-05-22 14:41:37 +02002430static void
2431surface_set_buffer_scale(struct wl_client *client,
2432 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002433 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002434{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002435 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002436
Jonny Lamba55f1392014-05-30 12:07:15 +02002437 if (scale < 1) {
2438 wl_resource_post_error(resource,
2439 WL_SURFACE_ERROR_INVALID_SCALE,
2440 "buffer scale must be at least one "
2441 "('%d' specified)", scale);
2442 return;
2443 }
2444
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002445 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002446 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002447}
2448
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002449static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002450 surface_destroy,
2451 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002452 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002453 surface_frame,
2454 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002455 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002456 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002457 surface_set_buffer_transform,
2458 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002459};
2460
2461static void
2462compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002463 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002464{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002465 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002466 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002467
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002468 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002469 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002470 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002471 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002472 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002473
Jason Ekstranda85118c2013-06-27 20:17:02 -05002474 surface->resource =
2475 wl_resource_create(client, &wl_surface_interface,
2476 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002477 if (surface->resource == NULL) {
2478 weston_surface_destroy(surface);
2479 wl_resource_post_no_memory(resource);
2480 return;
2481 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002482 wl_resource_set_implementation(surface->resource, &surface_interface,
2483 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002484
2485 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002486}
2487
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002488static void
2489destroy_region(struct wl_resource *resource)
2490{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002491 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002492
2493 pixman_region32_fini(&region->region);
2494 free(region);
2495}
2496
2497static void
2498region_destroy(struct wl_client *client, struct wl_resource *resource)
2499{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002500 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002501}
2502
2503static void
2504region_add(struct wl_client *client, struct wl_resource *resource,
2505 int32_t x, int32_t y, int32_t width, int32_t height)
2506{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002507 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002508
2509 pixman_region32_union_rect(&region->region, &region->region,
2510 x, y, width, height);
2511}
2512
2513static void
2514region_subtract(struct wl_client *client, struct wl_resource *resource,
2515 int32_t x, int32_t y, int32_t width, int32_t height)
2516{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002517 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002518 pixman_region32_t rect;
2519
2520 pixman_region32_init_rect(&rect, x, y, width, height);
2521 pixman_region32_subtract(&region->region, &region->region, &rect);
2522 pixman_region32_fini(&rect);
2523}
2524
2525static const struct wl_region_interface region_interface = {
2526 region_destroy,
2527 region_add,
2528 region_subtract
2529};
2530
2531static void
2532compositor_create_region(struct wl_client *client,
2533 struct wl_resource *resource, uint32_t id)
2534{
2535 struct weston_region *region;
2536
2537 region = malloc(sizeof *region);
2538 if (region == NULL) {
2539 wl_resource_post_no_memory(resource);
2540 return;
2541 }
2542
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002543 pixman_region32_init(&region->region);
2544
Jason Ekstranda85118c2013-06-27 20:17:02 -05002545 region->resource =
2546 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002547 if (region->resource == NULL) {
2548 free(region);
2549 wl_resource_post_no_memory(resource);
2550 return;
2551 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002552 wl_resource_set_implementation(region->resource, &region_interface,
2553 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002554}
2555
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002556static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002557 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002558 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002559};
2560
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002561static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002562weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2563{
2564 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002565
Jason Ekstrand7b982072014-05-20 14:33:03 -05002566 weston_surface_commit_state(surface, &sub->cached);
2567 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002568
2569 weston_surface_commit_subsurface_order(surface);
2570
2571 weston_surface_schedule_repaint(surface);
2572
Jason Ekstrand7b982072014-05-20 14:33:03 -05002573 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002574}
2575
2576static void
2577weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2578{
2579 struct weston_surface *surface = sub->surface;
2580
2581 /*
2582 * If this commit would cause the surface to move by the
2583 * attach(dx, dy) parameters, the old damage region must be
2584 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002585 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002586 */
2587 pixman_region32_translate(&sub->cached.damage,
2588 -surface->pending.sx, -surface->pending.sy);
2589 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2590 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002591 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592
2593 if (surface->pending.newly_attached) {
2594 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002595 weston_surface_state_set_buffer(&sub->cached,
2596 surface->pending.buffer);
2597 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002598 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002599 weston_presentation_feedback_discard_list(
2600 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002601 }
2602 sub->cached.sx += surface->pending.sx;
2603 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002604
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002605 sub->cached.buffer_viewport.changed |=
2606 surface->pending.buffer_viewport.changed;
2607 sub->cached.buffer_viewport.buffer =
2608 surface->pending.buffer_viewport.buffer;
2609 sub->cached.buffer_viewport.surface =
2610 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002611
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002612 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613
2614 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2615
2616 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2617
2618 wl_list_insert_list(&sub->cached.frame_callback_list,
2619 &surface->pending.frame_callback_list);
2620 wl_list_init(&surface->pending.frame_callback_list);
2621
Pekka Paalanen133e4392014-09-23 22:08:46 -04002622 wl_list_insert_list(&sub->cached.feedback_list,
2623 &surface->pending.feedback_list);
2624 wl_list_init(&surface->pending.feedback_list);
2625
Jason Ekstrand7b982072014-05-20 14:33:03 -05002626 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002627}
2628
2629static int
2630weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2631{
2632 while (sub) {
2633 if (sub->synchronized)
2634 return 1;
2635
2636 if (!sub->parent)
2637 return 0;
2638
2639 sub = weston_surface_to_subsurface(sub->parent);
2640 }
2641
2642 return 0;
2643}
2644
2645static void
2646weston_subsurface_commit(struct weston_subsurface *sub)
2647{
2648 struct weston_surface *surface = sub->surface;
2649 struct weston_subsurface *tmp;
2650
2651 /* Recursive check for effectively synchronized. */
2652 if (weston_subsurface_is_synchronized(sub)) {
2653 weston_subsurface_commit_to_cache(sub);
2654 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002655 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656 /* flush accumulated state from cache */
2657 weston_subsurface_commit_to_cache(sub);
2658 weston_subsurface_commit_from_cache(sub);
2659 } else {
2660 weston_surface_commit(surface);
2661 }
2662
2663 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2664 if (tmp->surface != surface)
2665 weston_subsurface_parent_commit(tmp, 0);
2666 }
2667 }
2668}
2669
2670static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002671weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002672{
2673 struct weston_surface *surface = sub->surface;
2674 struct weston_subsurface *tmp;
2675
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676 /* From now on, commit_from_cache the whole sub-tree, regardless of
2677 * the synchronized mode of each child. This sub-surface or some
2678 * of its ancestors were synchronized, so we are synchronized
2679 * all the way down.
2680 */
2681
Jason Ekstrand7b982072014-05-20 14:33:03 -05002682 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002683 weston_subsurface_commit_from_cache(sub);
2684
2685 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2686 if (tmp->surface != surface)
2687 weston_subsurface_parent_commit(tmp, 1);
2688 }
2689}
2690
2691static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002692weston_subsurface_parent_commit(struct weston_subsurface *sub,
2693 int parent_is_synchronized)
2694{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002695 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002696 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002697 wl_list_for_each(view, &sub->surface->views, surface_link)
2698 weston_view_set_position(view,
2699 sub->position.x,
2700 sub->position.y);
2701
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002702 sub->position.set = 0;
2703 }
2704
2705 if (parent_is_synchronized || sub->synchronized)
2706 weston_subsurface_synchronized_commit(sub);
2707}
2708
2709static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002710subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002711{
2712 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002714
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002716 weston_view_set_position(view,
2717 view->geometry.x + dx,
2718 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002719
2720 /* No need to check parent mappedness, because if parent is not
2721 * mapped, parent is not in a visible layer, so this sub-surface
2722 * will not be drawn either.
2723 */
2724 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002725 struct weston_output *output;
2726
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002727 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002728 * because that would call it also for the parent surface,
2729 * which might not be mapped yet. That would lead to
2730 * inconsistent state, where the window could never be
2731 * mapped.
2732 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002733 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002734 * weston_surface_is_mapped() return true, so that when the
2735 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002736 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002737 */
2738 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002739 output = container_of(compositor->output_list.next,
2740 struct weston_output, link);
2741
2742 surface->output = output;
2743 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002744 }
2745}
2746
2747static struct weston_subsurface *
2748weston_surface_to_subsurface(struct weston_surface *surface)
2749{
2750 if (surface->configure == subsurface_configure)
2751 return surface->configure_private;
2752
2753 return NULL;
2754}
2755
Pekka Paalanen01388e22013-04-25 13:57:44 +03002756WL_EXPORT struct weston_surface *
2757weston_surface_get_main_surface(struct weston_surface *surface)
2758{
2759 struct weston_subsurface *sub;
2760
2761 while (surface && (sub = weston_surface_to_subsurface(surface)))
2762 surface = sub->parent;
2763
2764 return surface;
2765}
2766
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767static void
2768subsurface_set_position(struct wl_client *client,
2769 struct wl_resource *resource, int32_t x, int32_t y)
2770{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002771 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002772
2773 if (!sub)
2774 return;
2775
2776 sub->position.x = x;
2777 sub->position.y = y;
2778 sub->position.set = 1;
2779}
2780
2781static struct weston_subsurface *
2782subsurface_from_surface(struct weston_surface *surface)
2783{
2784 struct weston_subsurface *sub;
2785
2786 sub = weston_surface_to_subsurface(surface);
2787 if (sub)
2788 return sub;
2789
2790 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2791 if (sub->surface == surface)
2792 return sub;
2793
2794 return NULL;
2795}
2796
2797static struct weston_subsurface *
2798subsurface_sibling_check(struct weston_subsurface *sub,
2799 struct weston_surface *surface,
2800 const char *request)
2801{
2802 struct weston_subsurface *sibling;
2803
2804 sibling = subsurface_from_surface(surface);
2805
2806 if (!sibling) {
2807 wl_resource_post_error(sub->resource,
2808 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2809 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002810 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002811 return NULL;
2812 }
2813
2814 if (sibling->parent != sub->parent) {
2815 wl_resource_post_error(sub->resource,
2816 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2817 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002818 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819 return NULL;
2820 }
2821
2822 return sibling;
2823}
2824
2825static void
2826subsurface_place_above(struct wl_client *client,
2827 struct wl_resource *resource,
2828 struct wl_resource *sibling_resource)
2829{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002830 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002831 struct weston_surface *surface =
2832 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833 struct weston_subsurface *sibling;
2834
2835 if (!sub)
2836 return;
2837
2838 sibling = subsurface_sibling_check(sub, surface, "place_above");
2839 if (!sibling)
2840 return;
2841
2842 wl_list_remove(&sub->parent_link_pending);
2843 wl_list_insert(sibling->parent_link_pending.prev,
2844 &sub->parent_link_pending);
2845}
2846
2847static void
2848subsurface_place_below(struct wl_client *client,
2849 struct wl_resource *resource,
2850 struct wl_resource *sibling_resource)
2851{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002852 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002853 struct weston_surface *surface =
2854 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002855 struct weston_subsurface *sibling;
2856
2857 if (!sub)
2858 return;
2859
2860 sibling = subsurface_sibling_check(sub, surface, "place_below");
2861 if (!sibling)
2862 return;
2863
2864 wl_list_remove(&sub->parent_link_pending);
2865 wl_list_insert(&sibling->parent_link_pending,
2866 &sub->parent_link_pending);
2867}
2868
2869static void
2870subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2871{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002872 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002873
2874 if (sub)
2875 sub->synchronized = 1;
2876}
2877
2878static void
2879subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2880{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002881 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002882
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002883 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002884 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002885
2886 /* If sub became effectively desynchronized, flush. */
2887 if (!weston_subsurface_is_synchronized(sub))
2888 weston_subsurface_synchronized_commit(sub);
2889 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890}
2891
2892static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002893weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2894{
2895 wl_list_remove(&sub->parent_link);
2896 wl_list_remove(&sub->parent_link_pending);
2897 wl_list_remove(&sub->parent_destroy_listener.link);
2898 sub->parent = NULL;
2899}
2900
2901static void
2902weston_subsurface_destroy(struct weston_subsurface *sub);
2903
2904static void
2905subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2906{
2907 struct weston_subsurface *sub =
2908 container_of(listener, struct weston_subsurface,
2909 surface_destroy_listener);
2910 assert(data == &sub->surface->resource);
2911
2912 /* The protocol object (wl_resource) is left inert. */
2913 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002914 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002915
2916 weston_subsurface_destroy(sub);
2917}
2918
2919static void
2920subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2921{
2922 struct weston_subsurface *sub =
2923 container_of(listener, struct weston_subsurface,
2924 parent_destroy_listener);
2925 assert(data == &sub->parent->resource);
2926 assert(sub->surface != sub->parent);
2927
2928 if (weston_surface_is_mapped(sub->surface))
2929 weston_surface_unmap(sub->surface);
2930
2931 weston_subsurface_unlink_parent(sub);
2932}
2933
2934static void
2935subsurface_resource_destroy(struct wl_resource *resource)
2936{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002937 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002938
2939 if (sub)
2940 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941}
2942
2943static void
2944subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2945{
2946 wl_resource_destroy(resource);
2947}
2948
2949static void
2950weston_subsurface_link_parent(struct weston_subsurface *sub,
2951 struct weston_surface *parent)
2952{
2953 sub->parent = parent;
2954 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002955 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002956 &sub->parent_destroy_listener);
2957
2958 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2959 wl_list_insert(&parent->subsurface_list_pending,
2960 &sub->parent_link_pending);
2961}
2962
2963static void
2964weston_subsurface_link_surface(struct weston_subsurface *sub,
2965 struct weston_surface *surface)
2966{
2967 sub->surface = surface;
2968 sub->surface_destroy_listener.notify =
2969 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002970 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002971 &sub->surface_destroy_listener);
2972}
2973
2974static void
2975weston_subsurface_destroy(struct weston_subsurface *sub)
2976{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002977 struct weston_view *view, *next;
2978
Pekka Paalanene67858b2013-04-25 13:57:42 +03002979 assert(sub->surface);
2980
2981 if (sub->resource) {
2982 assert(weston_surface_to_subsurface(sub->surface) == sub);
2983 assert(sub->parent_destroy_listener.notify ==
2984 subsurface_handle_parent_destroy);
2985
George Kiagiadakised04d382014-06-13 18:10:26 +02002986 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2987 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002988 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002989 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002990
Pekka Paalanene67858b2013-04-25 13:57:42 +03002991 if (sub->parent)
2992 weston_subsurface_unlink_parent(sub);
2993
Jason Ekstrand7b982072014-05-20 14:33:03 -05002994 weston_surface_state_fini(&sub->cached);
2995 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002996
2997 sub->surface->configure = NULL;
2998 sub->surface->configure_private = NULL;
2999 } else {
3000 /* the dummy weston_subsurface for the parent itself */
3001 assert(sub->parent_destroy_listener.notify == NULL);
3002 wl_list_remove(&sub->parent_link);
3003 wl_list_remove(&sub->parent_link_pending);
3004 }
3005
3006 wl_list_remove(&sub->surface_destroy_listener.link);
3007 free(sub);
3008}
3009
3010static const struct wl_subsurface_interface subsurface_implementation = {
3011 subsurface_destroy,
3012 subsurface_set_position,
3013 subsurface_place_above,
3014 subsurface_place_below,
3015 subsurface_set_sync,
3016 subsurface_set_desync
3017};
3018
3019static struct weston_subsurface *
3020weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3021 struct weston_surface *parent)
3022{
3023 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003024 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003025
3026 sub = calloc(1, sizeof *sub);
3027 if (!sub)
3028 return NULL;
3029
Jason Ekstranda7af7042013-10-12 22:38:11 -05003030 wl_list_init(&sub->unused_views);
3031
Jason Ekstranda85118c2013-06-27 20:17:02 -05003032 sub->resource =
3033 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003034 if (!sub->resource) {
3035 free(sub);
3036 return NULL;
3037 }
3038
Jason Ekstranda85118c2013-06-27 20:17:02 -05003039 wl_resource_set_implementation(sub->resource,
3040 &subsurface_implementation,
3041 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003042 weston_subsurface_link_surface(sub, surface);
3043 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003044 weston_surface_state_init(&sub->cached);
3045 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003046 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003047
3048 return sub;
3049}
3050
3051/* Create a dummy subsurface for having the parent itself in its
3052 * sub-surface lists. Makes stacking order manipulation easy.
3053 */
3054static struct weston_subsurface *
3055weston_subsurface_create_for_parent(struct weston_surface *parent)
3056{
3057 struct weston_subsurface *sub;
3058
3059 sub = calloc(1, sizeof *sub);
3060 if (!sub)
3061 return NULL;
3062
3063 weston_subsurface_link_surface(sub, parent);
3064 sub->parent = parent;
3065 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3066 wl_list_insert(&parent->subsurface_list_pending,
3067 &sub->parent_link_pending);
3068
3069 return sub;
3070}
3071
3072static void
3073subcompositor_get_subsurface(struct wl_client *client,
3074 struct wl_resource *resource,
3075 uint32_t id,
3076 struct wl_resource *surface_resource,
3077 struct wl_resource *parent_resource)
3078{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003079 struct weston_surface *surface =
3080 wl_resource_get_user_data(surface_resource);
3081 struct weston_surface *parent =
3082 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003083 struct weston_subsurface *sub;
3084 static const char where[] = "get_subsurface: wl_subsurface@";
3085
3086 if (surface == parent) {
3087 wl_resource_post_error(resource,
3088 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3089 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003090 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003091 return;
3092 }
3093
3094 if (weston_surface_to_subsurface(surface)) {
3095 wl_resource_post_error(resource,
3096 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3097 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003098 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003099 return;
3100 }
3101
3102 if (surface->configure) {
3103 wl_resource_post_error(resource,
3104 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3105 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003106 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003107 return;
3108 }
3109
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003110 if (weston_surface_get_main_surface(parent) == surface) {
3111 wl_resource_post_error(resource,
3112 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3113 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003114 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003115 return;
3116 }
3117
Pekka Paalanene67858b2013-04-25 13:57:42 +03003118 /* make sure the parent is in its own list */
3119 if (wl_list_empty(&parent->subsurface_list)) {
3120 if (!weston_subsurface_create_for_parent(parent)) {
3121 wl_resource_post_no_memory(resource);
3122 return;
3123 }
3124 }
3125
3126 sub = weston_subsurface_create(id, surface, parent);
3127 if (!sub) {
3128 wl_resource_post_no_memory(resource);
3129 return;
3130 }
3131
3132 surface->configure = subsurface_configure;
3133 surface->configure_private = sub;
3134}
3135
3136static void
3137subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3138{
3139 wl_resource_destroy(resource);
3140}
3141
3142static const struct wl_subcompositor_interface subcompositor_interface = {
3143 subcompositor_destroy,
3144 subcompositor_get_subsurface
3145};
3146
3147static void
3148bind_subcompositor(struct wl_client *client,
3149 void *data, uint32_t version, uint32_t id)
3150{
3151 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003152 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003153
Jason Ekstranda85118c2013-06-27 20:17:02 -05003154 resource =
3155 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003156 if (resource == NULL) {
3157 wl_client_post_no_memory(client);
3158 return;
3159 }
3160 wl_resource_set_implementation(resource, &subcompositor_interface,
3161 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003162}
3163
3164static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003165weston_compositor_dpms(struct weston_compositor *compositor,
3166 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003167{
3168 struct weston_output *output;
3169
3170 wl_list_for_each(output, &compositor->output_list, link)
3171 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003172 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003173}
3174
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003175WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003176weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003177{
Neil Roberts8b62e202013-09-30 13:14:47 +01003178 uint32_t old_state = compositor->state;
3179
3180 /* The state needs to be changed before emitting the wake
3181 * signal because that may try to schedule a repaint which
3182 * will not work if the compositor is still sleeping */
3183 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3184
3185 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003186 case WESTON_COMPOSITOR_SLEEPING:
3187 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3188 /* fall through */
3189 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003190 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003191 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003192 /* fall through */
3193 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003194 wl_event_source_timer_update(compositor->idle_source,
3195 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003196 }
3197}
3198
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003199WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003200weston_compositor_offscreen(struct weston_compositor *compositor)
3201{
3202 switch (compositor->state) {
3203 case WESTON_COMPOSITOR_OFFSCREEN:
3204 return;
3205 case WESTON_COMPOSITOR_SLEEPING:
3206 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3207 /* fall through */
3208 default:
3209 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3210 wl_event_source_timer_update(compositor->idle_source, 0);
3211 }
3212}
3213
3214WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003215weston_compositor_sleep(struct weston_compositor *compositor)
3216{
3217 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3218 return;
3219
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003220 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003221 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3222 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3223}
3224
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003225static int
3226idle_handler(void *data)
3227{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003228 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003229
3230 if (compositor->idle_inhibit)
3231 return 1;
3232
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003233 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003234 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003235
3236 return 1;
3237}
3238
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003239WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003240weston_plane_init(struct weston_plane *plane,
3241 struct weston_compositor *ec,
3242 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003243{
3244 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003245 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003246 plane->x = x;
3247 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003248 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003249
3250 /* Init the link so that the call to wl_list_remove() when releasing
3251 * the plane without ever stacking doesn't lead to a crash */
3252 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003253}
3254
3255WL_EXPORT void
3256weston_plane_release(struct weston_plane *plane)
3257{
Xiong Zhang97116532013-10-23 13:58:31 +08003258 struct weston_view *view;
3259
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003260 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003261 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003262
Xiong Zhang97116532013-10-23 13:58:31 +08003263 wl_list_for_each(view, &plane->compositor->view_list, link) {
3264 if (view->plane == plane)
3265 view->plane = NULL;
3266 }
3267
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003268 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003269}
3270
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003271WL_EXPORT void
3272weston_compositor_stack_plane(struct weston_compositor *ec,
3273 struct weston_plane *plane,
3274 struct weston_plane *above)
3275{
3276 if (above)
3277 wl_list_insert(above->link.prev, &plane->link);
3278 else
3279 wl_list_insert(&ec->plane_list, &plane->link);
3280}
3281
Casey Dahlin9074db52012-04-19 22:50:09 -04003282static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003283{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003284 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003285}
3286
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003287static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003288bind_output(struct wl_client *client,
3289 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003290{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003291 struct weston_output *output = data;
3292 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003293 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003294
Jason Ekstranda85118c2013-06-27 20:17:02 -05003295 resource = wl_resource_create(client, &wl_output_interface,
3296 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003297 if (resource == NULL) {
3298 wl_client_post_no_memory(client);
3299 return;
3300 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003301
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003302 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003303 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003304
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003305 wl_output_send_geometry(resource,
3306 output->x,
3307 output->y,
3308 output->mm_width,
3309 output->mm_height,
3310 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003311 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003312 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003313 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003314 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003315 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003316
3317 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003318 wl_output_send_mode(resource,
3319 mode->flags,
3320 mode->width,
3321 mode->height,
3322 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003323 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003324
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003325 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003326 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003327}
3328
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003329/* Move other outputs when one is removed so the space remains contiguos. */
3330static void
3331weston_compositor_remove_output(struct weston_compositor *compositor,
3332 struct weston_output *remove_output)
3333{
3334 struct weston_output *output;
3335 int offset = 0;
3336
3337 wl_list_for_each(output, &compositor->output_list, link) {
3338 if (output == remove_output) {
3339 offset = output->width;
3340 continue;
3341 }
3342
3343 if (offset > 0) {
3344 weston_output_move(output,
3345 output->x - offset, output->y);
3346 output->dirty = 1;
3347 }
3348 }
3349}
3350
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003351WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003352weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003353{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003354 struct wl_resource *resource;
3355
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003356 output->destroying = 1;
3357
Pekka Paalanen133e4392014-09-23 22:08:46 -04003358 weston_presentation_feedback_discard_list(&output->feedback_list);
3359
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003360 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003361 wl_list_remove(&output->link);
3362
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003363 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003364 wl_signal_emit(&output->destroy_signal, output);
3365
Richard Hughesafe690c2013-05-02 10:10:04 +01003366 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003367 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003368 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003369 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003370
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003371 wl_resource_for_each(resource, &output->resource_list) {
3372 wl_resource_set_destructor(resource, NULL);
3373 }
3374
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003375 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003376}
3377
Scott Moreau1bad5db2012-08-18 01:04:05 -06003378static void
3379weston_output_compute_transform(struct weston_output *output)
3380{
3381 struct weston_matrix transform;
3382 int flip;
3383
3384 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003385 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003386
3387 switch(output->transform) {
3388 case WL_OUTPUT_TRANSFORM_FLIPPED:
3389 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3390 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3391 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003392 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003393 flip = -1;
3394 break;
3395 default:
3396 flip = 1;
3397 break;
3398 }
3399
3400 switch(output->transform) {
3401 case WL_OUTPUT_TRANSFORM_NORMAL:
3402 case WL_OUTPUT_TRANSFORM_FLIPPED:
3403 transform.d[0] = flip;
3404 transform.d[1] = 0;
3405 transform.d[4] = 0;
3406 transform.d[5] = 1;
3407 break;
3408 case WL_OUTPUT_TRANSFORM_90:
3409 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3410 transform.d[0] = 0;
3411 transform.d[1] = -flip;
3412 transform.d[4] = 1;
3413 transform.d[5] = 0;
3414 break;
3415 case WL_OUTPUT_TRANSFORM_180:
3416 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3417 transform.d[0] = -flip;
3418 transform.d[1] = 0;
3419 transform.d[4] = 0;
3420 transform.d[5] = -1;
3421 break;
3422 case WL_OUTPUT_TRANSFORM_270:
3423 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3424 transform.d[0] = 0;
3425 transform.d[1] = flip;
3426 transform.d[4] = -1;
3427 transform.d[5] = 0;
3428 break;
3429 default:
3430 break;
3431 }
3432
3433 weston_matrix_multiply(&output->matrix, &transform);
3434}
3435
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003436WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003437weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003438{
Scott Moreau850ca422012-05-21 15:21:25 -06003439 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003440
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003441 weston_matrix_init(&output->matrix);
3442 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003443 -(output->x + output->width / 2.0),
3444 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003445
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003446 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003447 2.0 / output->width,
3448 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003449
Scott Moreauccbf29d2012-02-22 14:21:41 -07003450 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003451 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003452 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003453 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3454 output->zoom.trans_y, 0);
3455 weston_matrix_scale(&output->matrix, magnification,
3456 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003457 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003458
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003459 weston_output_compute_transform(output);
3460
Scott Moreauccbf29d2012-02-22 14:21:41 -07003461 output->dirty = 0;
3462}
3463
Scott Moreau1bad5db2012-08-18 01:04:05 -06003464static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003465weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003466{
3467 output->transform = transform;
3468
3469 switch (transform) {
3470 case WL_OUTPUT_TRANSFORM_90:
3471 case WL_OUTPUT_TRANSFORM_270:
3472 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3473 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3474 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003475 output->width = output->current_mode->height;
3476 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003477 break;
3478 case WL_OUTPUT_TRANSFORM_NORMAL:
3479 case WL_OUTPUT_TRANSFORM_180:
3480 case WL_OUTPUT_TRANSFORM_FLIPPED:
3481 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003482 output->width = output->current_mode->width;
3483 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003484 break;
3485 default:
3486 break;
3487 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003488
Hardening57388e42013-09-18 23:56:36 +02003489 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003490 output->width /= scale;
3491 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003492}
3493
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003494static void
3495weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003496{
3497 output->x = x;
3498 output->y = y;
3499
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003500 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003501 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003502 output->width,
3503 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003504}
3505
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003506WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003507weston_output_move(struct weston_output *output, int x, int y)
3508{
3509 pixman_region32_t old_region;
3510 struct wl_resource *resource;
3511
3512 output->move_x = x - output->x;
3513 output->move_y = y - output->y;
3514
3515 if (output->move_x == 0 && output->move_y == 0)
3516 return;
3517
3518 pixman_region32_init(&old_region);
3519 pixman_region32_copy(&old_region, &output->region);
3520
3521 weston_output_init_geometry(output, x, y);
3522
3523 output->dirty = 1;
3524
3525 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003526 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003527
3528 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003529 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003530 wl_output_send_geometry(resource,
3531 output->x,
3532 output->y,
3533 output->mm_width,
3534 output->mm_height,
3535 output->subpixel,
3536 output->make,
3537 output->model,
3538 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003539
3540 if (wl_resource_get_version(resource) >= 2)
3541 wl_output_send_done(resource);
3542 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003543}
3544
3545WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003546weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003547 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003548 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003549{
3550 output->compositor = c;
3551 output->x = x;
3552 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003553 output->mm_width = mm_width;
3554 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003555 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003556 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003557
Alexander Larsson0b135062013-05-28 16:23:36 +02003558 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003559 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003560
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003561 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003562 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003563
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003564 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003565 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003566 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003567 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003568 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003569
Casey Dahlin58ba1372012-04-19 22:50:08 -04003570 output->id = ffs(~output->compositor->output_id_pool) - 1;
3571 output->compositor->output_id_pool |= 1 << output->id;
3572
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003573 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003574 wl_global_create(c->wl_display, &wl_output_interface, 2,
3575 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003576 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003577}
3578
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003579WL_EXPORT void
3580weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003581 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003582 wl_fixed_t *x, wl_fixed_t *y)
3583{
3584 wl_fixed_t tx, ty;
3585 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003586 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003587
Hardeningff39efa2013-09-18 23:56:35 +02003588 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3589 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003590
3591 switch(output->transform) {
3592 case WL_OUTPUT_TRANSFORM_NORMAL:
3593 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003594 tx = device_x;
3595 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003596 break;
3597 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003598 tx = device_y;
3599 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003600 break;
3601 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003602 tx = width - device_x;
3603 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003604 break;
3605 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003606 tx = width - device_y;
3607 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003608 break;
3609 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003610 tx = width - device_x;
3611 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003612 break;
3613 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003614 tx = width - device_y;
3615 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003616 break;
3617 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003618 tx = device_x;
3619 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003620 break;
3621 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003622 tx = device_y;
3623 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003624 break;
3625 }
3626
Neil Robertseb5a2002014-05-01 16:13:55 +01003627 tx /= output->current_scale;
3628 ty /= output->current_scale;
3629
3630 if (output->zoom.active) {
3631 zoom_scale = output->zoom.spring_z.current;
3632 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3633 output->width / 2.0f *
3634 (zoom_scale + output->zoom.trans_x));
3635 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3636 output->height / 2.0f *
3637 (zoom_scale + output->zoom.trans_y));
3638 tx = wl_fixed_from_double(zx);
3639 ty = wl_fixed_from_double(zy);
3640 }
3641
3642 *x = tx + wl_fixed_from_int(output->x);
3643 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003644}
3645
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003646static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003647destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003648{
Jonny Lamb74130762013-11-26 18:19:46 +01003649 struct weston_surface *surface =
3650 wl_resource_get_user_data(resource);
3651
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003652 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003653 surface->pending.buffer_viewport.buffer.src_width =
3654 wl_fixed_from_int(-1);
3655 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003656 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003657}
3658
3659static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003660viewport_destroy(struct wl_client *client,
3661 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003662{
3663 wl_resource_destroy(resource);
3664}
3665
3666static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003667viewport_set(struct wl_client *client,
3668 struct wl_resource *resource,
3669 wl_fixed_t src_x,
3670 wl_fixed_t src_y,
3671 wl_fixed_t src_width,
3672 wl_fixed_t src_height,
3673 int32_t dst_width,
3674 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003675{
Jonny Lamb74130762013-11-26 18:19:46 +01003676 struct weston_surface *surface =
3677 wl_resource_get_user_data(resource);
3678
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003679 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003680
Jonny Lamb8ae35902013-11-26 18:19:45 +01003681 if (wl_fixed_to_double(src_width) < 0 ||
3682 wl_fixed_to_double(src_height) < 0) {
3683 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003684 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003685 "source dimensions must be non-negative (%fx%f)",
3686 wl_fixed_to_double(src_width),
3687 wl_fixed_to_double(src_height));
3688 return;
3689 }
3690
3691 if (dst_width <= 0 || dst_height <= 0) {
3692 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003693 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003694 "destination dimensions must be positive (%dx%d)",
3695 dst_width, dst_height);
3696 return;
3697 }
3698
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003699 surface->pending.buffer_viewport.buffer.src_x = src_x;
3700 surface->pending.buffer_viewport.buffer.src_y = src_y;
3701 surface->pending.buffer_viewport.buffer.src_width = src_width;
3702 surface->pending.buffer_viewport.buffer.src_height = src_height;
3703 surface->pending.buffer_viewport.surface.width = dst_width;
3704 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003705 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003706}
3707
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003708static void
3709viewport_set_source(struct wl_client *client,
3710 struct wl_resource *resource,
3711 wl_fixed_t src_x,
3712 wl_fixed_t src_y,
3713 wl_fixed_t src_width,
3714 wl_fixed_t src_height)
3715{
3716 struct weston_surface *surface =
3717 wl_resource_get_user_data(resource);
3718
3719 assert(surface->viewport_resource != NULL);
3720
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003721 if (src_width == wl_fixed_from_int(-1) &&
3722 src_height == wl_fixed_from_int(-1)) {
3723 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003724 surface->pending.buffer_viewport.buffer.src_width =
3725 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003726 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003727 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003728 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003729
3730 if (src_width <= 0 || src_height <= 0) {
3731 wl_resource_post_error(resource,
3732 WL_VIEWPORT_ERROR_BAD_VALUE,
3733 "source size must be positive (%fx%f)",
3734 wl_fixed_to_double(src_width),
3735 wl_fixed_to_double(src_height));
3736 return;
3737 }
3738
3739 surface->pending.buffer_viewport.buffer.src_x = src_x;
3740 surface->pending.buffer_viewport.buffer.src_y = src_y;
3741 surface->pending.buffer_viewport.buffer.src_width = src_width;
3742 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003743 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003744}
3745
3746static void
3747viewport_set_destination(struct wl_client *client,
3748 struct wl_resource *resource,
3749 int32_t dst_width,
3750 int32_t dst_height)
3751{
3752 struct weston_surface *surface =
3753 wl_resource_get_user_data(resource);
3754
3755 assert(surface->viewport_resource != NULL);
3756
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003757 if (dst_width == -1 && dst_height == -1) {
3758 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003759 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003760 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003761 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003762 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003763
3764 if (dst_width <= 0 || dst_height <= 0) {
3765 wl_resource_post_error(resource,
3766 WL_VIEWPORT_ERROR_BAD_VALUE,
3767 "destination size must be positive (%dx%d)",
3768 dst_width, dst_height);
3769 return;
3770 }
3771
3772 surface->pending.buffer_viewport.surface.width = dst_width;
3773 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003774 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003775}
3776
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003777static const struct wl_viewport_interface viewport_interface = {
3778 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003779 viewport_set,
3780 viewport_set_source,
3781 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003782};
3783
3784static void
3785scaler_destroy(struct wl_client *client,
3786 struct wl_resource *resource)
3787{
3788 wl_resource_destroy(resource);
3789}
3790
3791static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003792scaler_get_viewport(struct wl_client *client,
3793 struct wl_resource *scaler,
3794 uint32_t id,
3795 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003796{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003797 int version = wl_resource_get_version(scaler);
3798 struct weston_surface *surface =
3799 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003800 struct wl_resource *resource;
3801
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003802 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003803 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003804 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3805 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003806 return;
3807 }
3808
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003809 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003810 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003811 if (resource == NULL) {
3812 wl_client_post_no_memory(client);
3813 return;
3814 }
3815
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003816 wl_resource_set_implementation(resource, &viewport_interface,
3817 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003818
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003819 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003820}
3821
3822static const struct wl_scaler_interface scaler_interface = {
3823 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003824 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003825};
3826
3827static void
3828bind_scaler(struct wl_client *client,
3829 void *data, uint32_t version, uint32_t id)
3830{
3831 struct wl_resource *resource;
3832
3833 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003834 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003835 if (resource == NULL) {
3836 wl_client_post_no_memory(client);
3837 return;
3838 }
3839
3840 wl_resource_set_implementation(resource, &scaler_interface,
3841 NULL, NULL);
3842}
3843
3844static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003845destroy_presentation_feedback(struct wl_resource *feedback_resource)
3846{
3847 struct weston_presentation_feedback *feedback;
3848
3849 feedback = wl_resource_get_user_data(feedback_resource);
3850
3851 wl_list_remove(&feedback->link);
3852 free(feedback);
3853}
3854
3855static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003856presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3857{
3858 wl_resource_destroy(resource);
3859}
3860
3861static void
3862presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003863 struct wl_resource *presentation_resource,
3864 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003865 uint32_t callback)
3866{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003867 struct weston_surface *surface;
3868 struct weston_presentation_feedback *feedback;
3869
3870 surface = wl_resource_get_user_data(surface_resource);
3871
3872 feedback = calloc(1, sizeof *feedback);
3873 if (!feedback)
3874 goto err_calloc;
3875
3876 feedback->resource = wl_resource_create(client,
3877 &presentation_feedback_interface,
3878 1, callback);
3879 if (!feedback->resource)
3880 goto err_create;
3881
3882 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3883 destroy_presentation_feedback);
3884
3885 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3886
3887 return;
3888
3889err_create:
3890 free(feedback);
3891
3892err_calloc:
3893 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003894}
3895
3896static const struct presentation_interface presentation_implementation = {
3897 presentation_destroy,
3898 presentation_feedback
3899};
3900
3901static void
3902bind_presentation(struct wl_client *client,
3903 void *data, uint32_t version, uint32_t id)
3904{
3905 struct weston_compositor *compositor = data;
3906 struct wl_resource *resource;
3907
3908 resource = wl_resource_create(client, &presentation_interface,
3909 MIN(version, 1), id);
3910 if (resource == NULL) {
3911 wl_client_post_no_memory(client);
3912 return;
3913 }
3914
3915 wl_resource_set_implementation(resource, &presentation_implementation,
3916 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003917 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003918}
3919
3920static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003921compositor_bind(struct wl_client *client,
3922 void *data, uint32_t version, uint32_t id)
3923{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003924 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003925 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003926
Jason Ekstranda85118c2013-06-27 20:17:02 -05003927 resource = wl_resource_create(client, &wl_compositor_interface,
3928 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003929 if (resource == NULL) {
3930 wl_client_post_no_memory(client);
3931 return;
3932 }
3933
3934 wl_resource_set_implementation(resource, &compositor_interface,
3935 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003936}
3937
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003938static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003939log_uname(void)
3940{
3941 struct utsname usys;
3942
3943 uname(&usys);
3944
3945 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3946 usys.version, usys.machine);
3947}
3948
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003949WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003950weston_environment_get_fd(const char *env)
3951{
3952 char *e, *end;
3953 int fd, flags;
3954
3955 e = getenv(env);
3956 if (!e)
3957 return -1;
3958 fd = strtol(e, &end, 0);
3959 if (*end != '\0')
3960 return -1;
3961
3962 flags = fcntl(fd, F_GETFD);
3963 if (flags == -1)
3964 return -1;
3965
3966 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3967 unsetenv(env);
3968
3969 return fd;
3970}
3971
3972WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003973weston_compositor_init(struct weston_compositor *ec,
3974 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003975 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003976 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003977{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003978 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003979 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003980 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003981
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003982 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003983 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003984 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003985 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003986 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003987 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003988 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003989 wl_signal_init(&ec->idle_signal);
3990 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003991 wl_signal_init(&ec->show_input_panel_signal);
3992 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003993 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003994 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003995 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003996 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003997 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003998 wl_signal_init(&ec->session_signal);
3999 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004000
Casey Dahlin58ba1372012-04-19 22:50:08 -04004001 ec->output_id_pool = 0;
4002
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004003 if (!wl_global_create(display, &wl_compositor_interface, 3,
4004 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004005 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004006
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004007 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4008 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004009 return -1;
4010
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004011 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004012 ec, bind_scaler))
4013 return -1;
4014
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004015 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4016 ec, bind_presentation))
4017 return -1;
4018
Jason Ekstranda7af7042013-10-12 22:38:11 -05004019 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004020 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004021 wl_list_init(&ec->layer_list);
4022 wl_list_init(&ec->seat_list);
4023 wl_list_init(&ec->output_list);
4024 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004025 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004026 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004027 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004028 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004029 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004030
Xiong Zhang97116532013-10-23 13:58:31 +08004031 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004032 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004033
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004034 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4035 weston_config_section_get_string(s, "keymap_rules",
4036 (char **) &xkb_names.rules, NULL);
4037 weston_config_section_get_string(s, "keymap_model",
4038 (char **) &xkb_names.model, NULL);
4039 weston_config_section_get_string(s, "keymap_layout",
4040 (char **) &xkb_names.layout, NULL);
4041 weston_config_section_get_string(s, "keymap_variant",
4042 (char **) &xkb_names.variant, NULL);
4043 weston_config_section_get_string(s, "keymap_options",
4044 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004045
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004046 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4047 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004048
Jonny Lamb66a41a02014-08-12 14:58:25 +02004049 weston_config_section_get_int(s, "repeat-rate",
4050 &ec->kb_repeat_rate, 40);
4051 weston_config_section_get_int(s, "repeat-delay",
4052 &ec->kb_repeat_delay, 400);
4053
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004054 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004055
4056 wl_data_device_manager_init(ec->wl_display);
4057
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004058 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004059
Daniel Stone725c2c32012-06-22 14:04:36 +01004060 loop = wl_display_get_event_loop(ec->wl_display);
4061 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4062 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4063
4064 ec->input_loop = wl_event_loop_create();
4065
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004066 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4067 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4068
4069 weston_compositor_schedule_repaint(ec);
4070
Daniel Stone725c2c32012-06-22 14:04:36 +01004071 return 0;
4072}
4073
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004074WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004075weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004076{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004077 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004078
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004079 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004080 if (ec->input_loop_source)
4081 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004082
Matt Roper361d2ad2011-08-29 13:52:23 -07004083 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004084 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004085 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004086
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004087 if (ec->renderer)
4088 ec->renderer->destroy(ec);
4089
Daniel Stone325fc2d2012-05-30 16:31:58 +01004090 weston_binding_list_destroy_all(&ec->key_binding_list);
4091 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004092 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004093 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004094 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004095
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004096 weston_plane_release(&ec->primary_plane);
4097
Jonas Ådahlc97af922012-03-28 22:36:09 +02004098 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004099
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004100 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004101}
4102
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004103WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004104weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4105 const struct weston_pointer_grab_interface *interface)
4106{
4107 struct weston_seat *seat;
4108
4109 ec->default_pointer_grab = interface;
4110 wl_list_for_each(seat, &ec->seat_list, link) {
4111 if (seat->pointer) {
4112 weston_pointer_set_default_grab(seat->pointer,
4113 interface);
4114 }
4115 }
4116}
4117
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004118WL_EXPORT int
4119weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4120 clockid_t clk_id)
4121{
4122 struct timespec ts;
4123
4124 if (clock_gettime(clk_id, &ts) < 0)
4125 return -1;
4126
4127 compositor->presentation_clock = clk_id;
4128
4129 return 0;
4130}
4131
4132/*
4133 * For choosing the software clock, when the display hardware or API
4134 * does not expose a compatible presentation timestamp.
4135 */
4136WL_EXPORT int
4137weston_compositor_set_presentation_clock_software(
4138 struct weston_compositor *compositor)
4139{
4140 /* In order of preference */
4141 static const clockid_t clocks[] = {
4142 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4143 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4144 CLOCK_MONOTONIC, /* no jumps, may crawl */
4145 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4146 CLOCK_REALTIME /* may jump and crawl */
4147 };
4148 unsigned i;
4149
4150 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4151 if (weston_compositor_set_presentation_clock(compositor,
4152 clocks[i]) == 0)
4153 return 0;
4154
4155 weston_log("Error: no suitable presentation clock available.\n");
4156
4157 return -1;
4158}
4159
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004160WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004161weston_version(int *major, int *minor, int *micro)
4162{
4163 *major = WESTON_VERSION_MAJOR;
4164 *minor = WESTON_VERSION_MINOR;
4165 *micro = WESTON_VERSION_MICRO;
4166}
4167
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004168static const char *
4169clock_name(clockid_t clk_id)
4170{
4171 static const char *names[] = {
4172 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4173 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4174 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4175 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4176 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4177 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4178 };
4179
4180 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4181 return "unknown";
4182
4183 return names[clk_id];
4184}
4185
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004186static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004187 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004188 const char *desc;
4189} capability_strings[] = {
4190 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004191 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004192};
4193
4194static void
4195weston_compositor_log_capabilities(struct weston_compositor *compositor)
4196{
4197 unsigned i;
4198 int yes;
4199
4200 weston_log("Compositor capabilities:\n");
4201 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4202 yes = compositor->capabilities & capability_strings[i].bit;
4203 weston_log_continue(STAMP_SPACE "%s %s\n",
4204 capability_strings[i].desc,
4205 yes ? "yes" : "no");
4206 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004207
4208 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4209 clock_name(compositor->presentation_clock),
4210 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004211}
4212
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004213static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004214{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004215 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004216
Martin Minarik6d118362012-06-07 18:01:59 +02004217 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004218 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004219
4220 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004221}
4222
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004223#ifdef HAVE_LIBUNWIND
4224
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004225static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004226print_backtrace(void)
4227{
4228 unw_cursor_t cursor;
4229 unw_context_t context;
4230 unw_word_t off;
4231 unw_proc_info_t pip;
4232 int ret, i = 0;
4233 char procname[256];
4234 const char *filename;
4235 Dl_info dlinfo;
4236
4237 pip.unwind_info = NULL;
4238 ret = unw_getcontext(&context);
4239 if (ret) {
4240 weston_log("unw_getcontext: %d\n", ret);
4241 return;
4242 }
4243
4244 ret = unw_init_local(&cursor, &context);
4245 if (ret) {
4246 weston_log("unw_init_local: %d\n", ret);
4247 return;
4248 }
4249
4250 ret = unw_step(&cursor);
4251 while (ret > 0) {
4252 ret = unw_get_proc_info(&cursor, &pip);
4253 if (ret) {
4254 weston_log("unw_get_proc_info: %d\n", ret);
4255 break;
4256 }
4257
4258 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4259 if (ret && ret != -UNW_ENOMEM) {
4260 if (ret != -UNW_EUNSPEC)
4261 weston_log("unw_get_proc_name: %d\n", ret);
4262 procname[0] = '?';
4263 procname[1] = 0;
4264 }
4265
4266 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4267 *dlinfo.dli_fname)
4268 filename = dlinfo.dli_fname;
4269 else
4270 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004271
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004272 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4273 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4274
4275 ret = unw_step(&cursor);
4276 if (ret < 0)
4277 weston_log("unw_step: %d\n", ret);
4278 }
4279}
4280
4281#else
4282
4283static void
4284print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004285{
4286 void *buffer[32];
4287 int i, count;
4288 Dl_info info;
4289
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004290 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4291 for (i = 0; i < count; i++) {
4292 dladdr(buffer[i], &info);
4293 weston_log(" [%016lx] %s (%s)\n",
4294 (long) buffer[i],
4295 info.dli_sname ? info.dli_sname : "--",
4296 info.dli_fname);
4297 }
4298}
4299
4300#endif
4301
4302static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004303on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004304{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004305 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004306 * then call the backend restore function, which will switch
4307 * back to the vt we launched from or ungrab X etc and then
4308 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004309 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004310 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004311 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004312
Peter Maatmane5b42e42013-03-27 22:38:53 +01004313 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004314
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004315 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004316
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004317 segv_compositor->restore(segv_compositor);
4318
4319 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004320}
4321
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004322WL_EXPORT void *
4323weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004324{
4325 char path[PATH_MAX];
4326 void *module, *init;
4327
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004328 if (name == NULL)
4329 return NULL;
4330
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004331 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004332 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004333 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004334 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004335
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004336 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4337 if (module) {
4338 weston_log("Module '%s' already loaded\n", path);
4339 dlclose(module);
4340 return NULL;
4341 }
4342
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004343 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004344 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004345 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004346 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004347 return NULL;
4348 }
4349
4350 init = dlsym(module, entrypoint);
4351 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004352 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004353 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004354 return NULL;
4355 }
4356
4357 return init;
4358}
4359
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004360static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004361load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004362 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004363{
4364 const char *p, *end;
4365 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004366 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004367 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004368
4369 if (modules == NULL)
4370 return 0;
4371
4372 p = modules;
4373 while (*p) {
4374 end = strchrnul(p, ',');
4375 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004376 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004377 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004378 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004379 p = end;
4380 while (*p == ',')
4381 p++;
4382
4383 }
4384
4385 return 0;
4386}
4387
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004388static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004389 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4390
4391static const char xdg_wrong_message[] =
4392 "fatal: environment variable XDG_RUNTIME_DIR\n"
4393 "is set to \"%s\", which is not a directory.\n";
4394
4395static const char xdg_wrong_mode_message[] =
4396 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004397 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4398 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004399
4400static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004401 "Refer to your distribution on how to get it, or\n"
4402 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4403 "on how to implement it.\n";
4404
Martin Minarik37032f82012-06-18 20:15:18 +02004405static void
4406verify_xdg_runtime_dir(void)
4407{
4408 char *dir = getenv("XDG_RUNTIME_DIR");
4409 struct stat s;
4410
4411 if (!dir) {
4412 weston_log(xdg_error_message);
4413 weston_log_continue(xdg_detail_message);
4414 exit(EXIT_FAILURE);
4415 }
4416
4417 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4418 weston_log(xdg_wrong_message, dir);
4419 weston_log_continue(xdg_detail_message);
4420 exit(EXIT_FAILURE);
4421 }
4422
4423 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4424 weston_log(xdg_wrong_mode_message,
4425 dir, s.st_mode & 0777, s.st_uid);
4426 weston_log_continue(xdg_detail_message);
4427 }
4428}
4429
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004430static int
4431usage(int error_code)
4432{
4433 fprintf(stderr,
4434 "Usage: weston [OPTIONS]\n\n"
4435 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4436 "Weston supports multiple backends, and depending on which backend is in use\n"
4437 "different options will be accepted.\n\n"
4438
4439
4440 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004441 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004442 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004443 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4444 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004445 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004446 " -S, --socket=NAME\tName of socket to listen on\n"
4447 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004448 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004449 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004450 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004451 " -h, --help\t\tThis help message\n\n");
4452
4453 fprintf(stderr,
4454 "Options for drm-backend.so:\n\n"
4455 " --connector=ID\tBring up only this connector\n"
4456 " --seat=SEAT\t\tThe seat that weston should run on\n"
4457 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004458 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004459 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4460
4461 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004462 "Options for fbdev-backend.so:\n\n"
4463 " --tty=TTY\t\tThe tty to use\n"
4464 " --device=DEVICE\tThe framebuffer device to use\n\n");
4465
4466 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004467 "Options for x11-backend.so:\n\n"
4468 " --width=WIDTH\t\tWidth of X window\n"
4469 " --height=HEIGHT\tHeight of X window\n"
4470 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004471 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004472 " --output-count=COUNT\tCreate multiple outputs\n"
4473 " --no-input\t\tDont create input devices\n\n");
4474
4475 fprintf(stderr,
4476 "Options for wayland-backend.so:\n\n"
4477 " --width=WIDTH\t\tWidth of Wayland surface\n"
4478 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004479 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004480 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004481 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004482 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004483 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004484 " --display=DISPLAY\tWayland display to connect to\n\n");
4485
Pekka Paalanene31e0532013-05-22 18:03:07 +03004486#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4487 fprintf(stderr,
4488 "Options for rpi-backend.so:\n\n"
4489 " --tty=TTY\t\tThe tty to use\n"
4490 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4491 " --transform=TR\tThe output transformation, TR is one of:\n"
4492 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004493 " --opaque-regions\tEnable support for opaque regions, can be "
4494 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004495 "\n");
4496#endif
4497
Hardeningc077a842013-07-08 00:51:35 +02004498#if defined(BUILD_RDP_COMPOSITOR)
4499 fprintf(stderr,
4500 "Options for rdp-backend.so:\n\n"
4501 " --width=WIDTH\t\tWidth of desktop\n"
4502 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004503 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4504 " --address=ADDR\tThe address to bind\n"
4505 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004506 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004507 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4508 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4509 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4510 "\n");
4511#endif
4512
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004513 exit(error_code);
4514}
4515
Peter Maatmane5b42e42013-03-27 22:38:53 +01004516static void
4517catch_signals(void)
4518{
4519 struct sigaction action;
4520
4521 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4522 action.sa_sigaction = on_caught_signal;
4523 sigemptyset(&action.sa_mask);
4524 sigaction(SIGSEGV, &action, NULL);
4525 sigaction(SIGABRT, &action, NULL);
4526}
4527
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004528static void
4529handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4530{
4531 struct wl_client *client = data;
4532
4533 weston_log("Primary client died. Closing...\n");
4534
4535 wl_display_terminate(wl_client_get_display(client));
4536}
4537
Ryo Munakatad8deff62014-09-06 07:32:05 +09004538static char *
4539weston_choose_default_backend(void)
4540{
4541 char *backend = NULL;
4542
4543 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4544 backend = strdup("wayland-backend.so");
4545 else if (getenv("DISPLAY"))
4546 backend = strdup("x11-backend.so");
4547 else
4548 backend = strdup(WESTON_NATIVE_BACKEND);
4549
4550 return backend;
4551}
4552
4553static int
4554weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4555{
4556 if (socket_name) {
4557 if (wl_display_add_socket(display, socket_name)) {
4558 weston_log("fatal: failed to add socket: %m\n");
4559 return -1;
4560 }
4561 } else {
4562 socket_name = wl_display_add_socket_auto(display);
4563 if (!socket_name) {
4564 weston_log("fatal: failed to add socket: %m\n");
4565 return -1;
4566 }
4567 }
4568
4569 setenv("WAYLAND_DISPLAY", socket_name, 1);
4570
4571 return 0;
4572}
4573
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004574int main(int argc, char *argv[])
4575{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004576 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004577 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004578 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004579 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004580 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004581 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004582 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004583 int *argc, char *argv[],
4584 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004585 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004586 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004587 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004588 char *modules = NULL;
4589 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004590 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004591 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004592 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004593 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004594 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004595 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004596 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004597 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004598 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004599 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004600 struct wl_client *primary_client;
4601 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004602 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004603
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004604 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004605 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4606 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004607 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4608 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004609 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004610 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004611 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004612 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004613 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004614 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004615
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004616 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004617
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004618 if (help)
4619 usage(EXIT_SUCCESS);
4620
Scott Moreau12245142012-08-29 15:15:58 -06004621 if (version) {
4622 printf(PACKAGE_STRING "\n");
4623 return EXIT_SUCCESS;
4624 }
4625
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004626 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004627
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004628 weston_log("%s\n"
4629 STAMP_SPACE "%s\n"
4630 STAMP_SPACE "Bug reports to: %s\n"
4631 STAMP_SPACE "Build: %s\n",
4632 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004633 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004634 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004635
Martin Minarik37032f82012-06-18 20:15:18 +02004636 verify_xdg_runtime_dir();
4637
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004638 display = wl_display_create();
4639
Tiago Vignatti2116b892011-08-08 05:52:59 -07004640 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004641 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4642 display);
4643 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4644 display);
4645 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4646 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004647
4648 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004649 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4650 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004651
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304652 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4653 ret = EXIT_FAILURE;
4654 goto out_signals;
4655 }
4656
Pekka Paalanen588bee12014-05-07 16:26:25 +03004657 if (noconfig == 0)
4658 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004659 if (config != NULL) {
4660 weston_log("Using config file '%s'\n",
4661 weston_config_get_full_path(config));
4662 } else {
4663 weston_log("Starting with no config file.\n");
4664 }
4665 section = weston_config_get_section(config, "core", NULL, NULL);
4666
Ryo Munakata03faed22014-09-06 07:32:51 +09004667 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004668 weston_config_section_get_string(section, "backend", &backend,
4669 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004670 if (!backend)
4671 backend = weston_choose_default_backend();
4672 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004673
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004674 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304675 if (!backend_init) {
4676 ret = EXIT_FAILURE;
4677 goto out_signals;
4678 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004679
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004680 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004681 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004682 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304683 ret = EXIT_FAILURE;
4684 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004685 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004686
Peter Maatmane5b42e42013-03-27 22:38:53 +01004687 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004688 segv_compositor = ec;
4689
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004690 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004691 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004692
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004693 for (i = 1; i < argc; i++)
4694 weston_log("fatal: unhandled option: %s\n", argv[i]);
4695 if (argc > 1) {
4696 ret = EXIT_FAILURE;
4697 goto out;
4698 }
4699
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004700 weston_compositor_log_capabilities(ec);
4701
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004702 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4703 if (server_socket) {
4704 weston_log("Running with single client\n");
4705 fd = strtol(server_socket, &end, 0);
4706 if (*end != '\0')
4707 fd = -1;
4708 } else {
4709 fd = -1;
4710 }
4711
4712 if (fd != -1) {
4713 primary_client = wl_client_create(display, fd);
4714 if (!primary_client) {
4715 weston_log("fatal: failed to add client: %m\n");
4716 ret = EXIT_FAILURE;
4717 goto out;
4718 }
4719 primary_client_destroyed.notify =
4720 handle_primary_client_destroyed;
4721 wl_client_add_destroy_listener(primary_client,
4722 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004723 } else if (weston_create_listening_socket(display, socket_name)) {
4724 ret = EXIT_FAILURE;
4725 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004726 }
4727
Ryo Munakata03faed22014-09-06 07:32:51 +09004728 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004729 weston_config_section_get_string(section, "shell", &shell,
4730 "desktop-shell.so");
4731
Ryo Munakata03faed22014-09-06 07:32:51 +09004732 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004733 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004734
4735 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004736 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004737 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004738
4739 if (load_modules(ec, option_modules, &argc, argv) < 0)
4740 goto out;
4741
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004742 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4743 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4744 if (numlock_on) {
4745 wl_list_for_each(seat, &ec->seat_list, link) {
4746 if (seat->keyboard)
4747 weston_keyboard_set_locks(seat->keyboard,
4748 WESTON_NUM_LOCK,
4749 WESTON_NUM_LOCK);
4750 }
4751 }
4752
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004753 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004754
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004755 wl_display_run(display);
4756
Ryo Munakata03faed22014-09-06 07:32:51 +09004757out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004758 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004759 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004760
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004761 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004762
Daniel Stone855028f2012-05-01 20:37:10 +01004763 weston_compositor_xkb_destroy(ec);
4764
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004765 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304766
4767out_signals:
4768 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4769 if (signals[i])
4770 wl_event_source_remove(signals[i]);
4771
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004772 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004773
Martin Minarik19e6f262012-06-07 13:08:46 +02004774 weston_log_file_close();
4775
Ryo Munakata03faed22014-09-06 07:32:51 +09004776 free(backend);
4777 free(shell);
4778 free(socket_name);
4779 free(option_modules);
4780 free(log);
4781 free(modules);
4782
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004783 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004784}