blob: b8b7c0f1e7b59fff9c2d4fe0045f39e37c2945cb [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 Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 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>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030056#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040057#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050058#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050059
Kristian Høgsberg27da5382011-06-21 17:32:25 -040060static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040061static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062
63static int
64sigchld_handler(int signal_number, void *data)
65{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050066 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067 int status;
68 pid_t pid;
69
Bill Spitzak027b9622012-03-17 15:22:03 -070070 pid = waitpid(-1, &status, WNOHANG);
71 if (!pid)
72 return 1;
73
Kristian Høgsberg27da5382011-06-21 17:32:25 -040074 wl_list_for_each(p, &child_process_list, link) {
75 if (p->pid == pid)
76 break;
77 }
78
79 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020080 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040081 return 1;
82 }
83
84 wl_list_remove(&p->link);
85 p->cleanup(p, status);
86
87 return 1;
88}
89
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090static void
Alexander Larsson0b135062013-05-28 16:23:36 +020091weston_output_transform_scale_init(struct weston_output *output,
92 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093
Rob Bradford27b17932013-06-26 18:08:46 +010094static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050095weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010096
Alex Wu2dda6042012-04-17 17:20:47 +080097WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020098weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
99 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200102 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200103 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200104 int ret, notify_mode_changed, notify_scale_changed;
105 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106
Alex Wu2dda6042012-04-17 17:20:47 +0800107 if (!output->switch_mode)
108 return -1;
109
Hardening57388e42013-09-18 23:56:36 +0200110 temporary_mode = (output->original_mode != 0);
111 temporary_scale = (output->current_scale != output->original_scale);
112 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113
Hardening57388e42013-09-18 23:56:36 +0200114 notify_mode_changed = 0;
115 notify_scale_changed = 0;
116 switch(op) {
117 case WESTON_MODE_SWITCH_SET_NATIVE:
118 output->native_mode = mode;
119 if (!temporary_mode) {
120 notify_mode_changed = 1;
121 ret = output->switch_mode(output, mode);
122 if (ret < 0)
123 return ret;
124 }
125
126 output->native_scale = scale;
127 if(!temporary_scale)
128 notify_scale_changed = 1;
129 break;
130 case WESTON_MODE_SWITCH_SET_TEMPORARY:
131 if (!temporary_mode)
132 output->original_mode = output->native_mode;
133 if (!temporary_scale)
134 output->original_scale = output->native_scale;
135
136 ret = output->switch_mode(output, mode);
137 if (ret < 0)
138 return ret;
139
Hardening57388e42013-09-18 23:56:36 +0200140 output->current_scale = scale;
141 break;
142 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
143 if (!temporary_mode) {
144 weston_log("already in the native mode\n");
145 return -1;
146 }
147
148 notify_mode_changed = (output->original_mode != output->native_mode);
149
150 ret = output->switch_mode(output, mode);
151 if (ret < 0)
152 return ret;
153
154 if (output->original_scale != output->native_scale) {
155 notify_scale_changed = 1;
156 scale = output->native_scale;
157 output->original_scale = scale;
158 }
159 output->original_mode = 0;
160
161 output->current_scale = output->native_scale;
162 break;
163 default:
164 weston_log("unknown weston_switch_mode_op %d\n", op);
165 break;
166 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200167
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200168 pixman_region32_init(&old_output_region);
169 pixman_region32_copy(&old_output_region, &output->region);
170
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200171 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200172 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200173
174 pixman_region32_init(&output->previous_damage);
175 pixman_region32_init_rect(&output->region, output->x, output->y,
176 output->width, output->height);
177
178 weston_output_update_matrix(output);
179
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200180 /* If a pointer falls outside the outputs new geometry, move it to its
181 * lower-right corner */
182 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400183 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200184 int32_t x, y;
185
186 if (!pointer)
187 continue;
188
189 x = wl_fixed_to_int(pointer->x);
190 y = wl_fixed_to_int(pointer->y);
191
192 if (!pixman_region32_contains_point(&old_output_region,
193 x, y, NULL) ||
194 pixman_region32_contains_point(&output->region,
195 x, y, NULL))
196 continue;
197
198 if (x >= output->x + output->width)
199 x = output->x + output->width - 1;
200 if (y >= output->y + output->height)
201 y = output->y + output->height - 1;
202
203 pointer->x = wl_fixed_from_int(x);
204 pointer->y = wl_fixed_from_int(y);
205 }
206
207 pixman_region32_fini(&old_output_region);
208
Hardening57388e42013-09-18 23:56:36 +0200209 /* notify clients of the changes */
210 if (notify_mode_changed || notify_scale_changed) {
211 wl_resource_for_each(resource, &output->resource_list) {
212 if(notify_mode_changed) {
213 wl_output_send_mode(resource,
214 mode->flags | WL_OUTPUT_MODE_CURRENT,
215 mode->width,
216 mode->height,
217 mode->refresh);
218 }
219
220 if (notify_scale_changed)
221 wl_output_send_scale(resource, scale);
222
223 if (wl_resource_get_version(resource) >= 2)
224 wl_output_send_done(resource);
225 }
226 }
227
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200228 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800229}
230
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400231WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500232weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400233{
234 wl_list_insert(&child_process_list, &process->link);
235}
236
Benjamin Franzke06286262011-05-06 19:12:33 +0200237static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200238child_client_exec(int sockfd, const char *path)
239{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500240 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200241 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200242 sigset_t allsigs;
243
244 /* do not give our signal mask to the new process */
245 sigfillset(&allsigs);
246 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200247
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100248 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
249 if (seteuid(getuid()) == -1) {
250 weston_log("compositor: failed seteuid\n");
251 return;
252 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500253
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500254 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
255 * non-CLOEXEC fd to pass through exec. */
256 clientfd = dup(sockfd);
257 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200258 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500259 return;
260 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200261
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500262 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200263 setenv("WAYLAND_SOCKET", s, 1);
264
265 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200266 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200267 path);
268}
269
270WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500271weston_client_launch(struct weston_compositor *compositor,
272 struct weston_process *proc,
273 const char *path,
274 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200275{
276 int sv[2];
277 pid_t pid;
278 struct wl_client *client;
279
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300280 weston_log("launching '%s'\n", path);
281
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300282 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200283 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200284 "socketpair failed while launching '%s': %m\n",
285 path);
286 return NULL;
287 }
288
289 pid = fork();
290 if (pid == -1) {
291 close(sv[0]);
292 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200293 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200294 "fork failed while launching '%s': %m\n", path);
295 return NULL;
296 }
297
298 if (pid == 0) {
299 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700300 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200301 }
302
303 close(sv[1]);
304
305 client = wl_client_create(compositor->wl_display, sv[0]);
306 if (!client) {
307 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200308 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200309 "wl_client_create failed while launching '%s'.\n",
310 path);
311 return NULL;
312 }
313
314 proc->pid = pid;
315 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200317
318 return client;
319}
320
321static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300322surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
323{
324 struct weston_surface *surface =
325 container_of(listener, struct weston_surface,
326 pending.buffer_destroy_listener);
327
328 surface->pending.buffer = NULL;
329}
330
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200331static void
332empty_region(pixman_region32_t *region)
333{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300334 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200335 pixman_region32_init(region);
336}
337
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300338static void
339region_init_infinite(pixman_region32_t *region)
340{
341 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
342 UINT32_MAX, UINT32_MAX);
343}
344
Pekka Paalanene67858b2013-04-25 13:57:42 +0300345static struct weston_subsurface *
346weston_surface_to_subsurface(struct weston_surface *surface);
347
Jason Ekstranda7af7042013-10-12 22:38:11 -0500348WL_EXPORT struct weston_view *
349weston_view_create(struct weston_surface *surface)
350{
351 struct weston_view *view;
352
353 view = calloc(1, sizeof *view);
354 if (view == NULL)
355 return NULL;
356
357 view->surface = surface;
358
Jason Ekstranda7af7042013-10-12 22:38:11 -0500359 /* Assign to surface */
360 wl_list_insert(&surface->views, &view->surface_link);
361
362 wl_signal_init(&view->destroy_signal);
363 wl_list_init(&view->link);
364 wl_list_init(&view->layer_link);
365
Xiong Zhang97116532013-10-23 13:58:31 +0800366 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500367
368 pixman_region32_init(&view->clip);
369
370 view->alpha = 1.0;
371 pixman_region32_init(&view->transform.opaque);
372
373 wl_list_init(&view->geometry.transformation_list);
374 wl_list_insert(&view->geometry.transformation_list,
375 &view->transform.position.link);
376 weston_matrix_init(&view->transform.position.matrix);
377 wl_list_init(&view->geometry.child_list);
378 pixman_region32_init(&view->transform.boundingbox);
379 view->transform.dirty = 1;
380
381 view->output = NULL;
382
383 return view;
384}
385
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500386WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500387weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500388{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500389 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400390
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200391 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400392 if (surface == NULL)
393 return NULL;
394
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500395 wl_signal_init(&surface->destroy_signal);
396
397 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500398
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500399 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200400 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400401
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100402 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
403 surface->buffer_viewport.scale = 1;
404 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400405 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100406 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200407
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400408 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500409 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300410 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400411
Jason Ekstranda7af7042013-10-12 22:38:11 -0500412 wl_list_init(&surface->views);
413
414 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500415
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300416 surface->pending.buffer_destroy_listener.notify =
417 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300418 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300419 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300420 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300421 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300422
Pekka Paalanene67858b2013-04-25 13:57:42 +0300423 wl_list_init(&surface->subsurface_list);
424 wl_list_init(&surface->subsurface_list_pending);
425
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400426 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500427}
428
Alex Wu8811bf92012-02-28 18:07:54 +0800429WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500430weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200431 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500432{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100433 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500434}
435
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400436WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500437weston_view_to_global_float(struct weston_view *view,
438 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200439{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500440 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200441 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
442
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200444
445 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200446 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700447 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200448 v.f[3]);
449 *x = 0;
450 *y = 0;
451 return;
452 }
453
454 *x = v.f[0] / v.f[3];
455 *y = v.f[1] / v.f[3];
456 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500457 *x = sx + view->geometry.x;
458 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200459 }
460}
461
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500462WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200463weston_transformed_coord(int width, int height,
464 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200465 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200466 float sx, float sy, float *bx, float *by)
467{
468 switch (transform) {
469 case WL_OUTPUT_TRANSFORM_NORMAL:
470 default:
471 *bx = sx;
472 *by = sy;
473 break;
474 case WL_OUTPUT_TRANSFORM_FLIPPED:
475 *bx = width - sx;
476 *by = sy;
477 break;
478 case WL_OUTPUT_TRANSFORM_90:
479 *bx = height - sy;
480 *by = sx;
481 break;
482 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
483 *bx = height - sy;
484 *by = width - sx;
485 break;
486 case WL_OUTPUT_TRANSFORM_180:
487 *bx = width - sx;
488 *by = height - sy;
489 break;
490 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
491 *bx = sx;
492 *by = height - sy;
493 break;
494 case WL_OUTPUT_TRANSFORM_270:
495 *bx = sy;
496 *by = width - sx;
497 break;
498 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
499 *bx = sy;
500 *by = sx;
501 break;
502 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200503
504 *bx *= scale;
505 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200506}
507
508WL_EXPORT pixman_box32_t
509weston_transformed_rect(int width, int height,
510 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200511 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200512 pixman_box32_t rect)
513{
514 float x1, x2, y1, y2;
515
516 pixman_box32_t ret;
517
Alexander Larsson4ea95522013-05-22 14:41:37 +0200518 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200519 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200520 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200521 rect.x2, rect.y2, &x2, &y2);
522
523 if (x1 <= x2) {
524 ret.x1 = x1;
525 ret.x2 = x2;
526 } else {
527 ret.x1 = x2;
528 ret.x2 = x1;
529 }
530
531 if (y1 <= y2) {
532 ret.y1 = y1;
533 ret.y2 = y2;
534 } else {
535 ret.y1 = y2;
536 ret.y2 = y1;
537 }
538
539 return ret;
540}
541
542WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500543weston_transformed_region(int width, int height,
544 enum wl_output_transform transform,
545 int32_t scale,
546 pixman_region32_t *src, pixman_region32_t *dest)
547{
548 pixman_box32_t *src_rects, *dest_rects;
549 int nrects, i;
550
551 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
552 if (src != dest)
553 pixman_region32_copy(dest, src);
554 return;
555 }
556
557 src_rects = pixman_region32_rectangles(src, &nrects);
558 dest_rects = malloc(nrects * sizeof(*dest_rects));
559 if (!dest_rects)
560 return;
561
562 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
563 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
564 } else {
565 for (i = 0; i < nrects; i++) {
566 switch (transform) {
567 default:
568 case WL_OUTPUT_TRANSFORM_NORMAL:
569 dest_rects[i].x1 = src_rects[i].x1;
570 dest_rects[i].y1 = src_rects[i].y1;
571 dest_rects[i].x2 = src_rects[i].x2;
572 dest_rects[i].y2 = src_rects[i].y2;
573 break;
574 case WL_OUTPUT_TRANSFORM_90:
575 dest_rects[i].x1 = height - src_rects[i].y2;
576 dest_rects[i].y1 = src_rects[i].x1;
577 dest_rects[i].x2 = height - src_rects[i].y1;
578 dest_rects[i].y2 = src_rects[i].x2;
579 break;
580 case WL_OUTPUT_TRANSFORM_180:
581 dest_rects[i].x1 = width - src_rects[i].x2;
582 dest_rects[i].y1 = height - src_rects[i].y2;
583 dest_rects[i].x2 = width - src_rects[i].x1;
584 dest_rects[i].y2 = height - src_rects[i].y1;
585 break;
586 case WL_OUTPUT_TRANSFORM_270:
587 dest_rects[i].x1 = src_rects[i].y1;
588 dest_rects[i].y1 = width - src_rects[i].x2;
589 dest_rects[i].x2 = src_rects[i].y2;
590 dest_rects[i].y2 = width - src_rects[i].x1;
591 break;
592 case WL_OUTPUT_TRANSFORM_FLIPPED:
593 dest_rects[i].x1 = width - src_rects[i].x2;
594 dest_rects[i].y1 = src_rects[i].y1;
595 dest_rects[i].x2 = width - src_rects[i].x1;
596 dest_rects[i].y2 = src_rects[i].y2;
597 break;
598 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
599 dest_rects[i].x1 = height - src_rects[i].y2;
600 dest_rects[i].y1 = width - src_rects[i].x2;
601 dest_rects[i].x2 = height - src_rects[i].y1;
602 dest_rects[i].y2 = width - src_rects[i].x1;
603 break;
604 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
605 dest_rects[i].x1 = src_rects[i].x1;
606 dest_rects[i].y1 = height - src_rects[i].y2;
607 dest_rects[i].x2 = src_rects[i].x2;
608 dest_rects[i].y2 = height - src_rects[i].y1;
609 break;
610 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
611 dest_rects[i].x1 = src_rects[i].y1;
612 dest_rects[i].y1 = src_rects[i].x1;
613 dest_rects[i].x2 = src_rects[i].y2;
614 dest_rects[i].y2 = src_rects[i].x2;
615 break;
616 }
617 }
618 }
619
620 if (scale != 1) {
621 for (i = 0; i < nrects; i++) {
622 dest_rects[i].x1 *= scale;
623 dest_rects[i].x2 *= scale;
624 dest_rects[i].y1 *= scale;
625 dest_rects[i].y2 *= scale;
626 }
627 }
628
629 pixman_region32_clear(dest);
630 pixman_region32_init_rects(dest, dest_rects, nrects);
631 free(dest_rects);
632}
633
634WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200635weston_surface_to_buffer_float(struct weston_surface *surface,
636 float sx, float sy, float *bx, float *by)
637{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500638 weston_transformed_coord(surface->width,
639 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100640 surface->buffer_viewport.transform,
641 surface->buffer_viewport.scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200642 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200643}
644
Alexander Larsson4ea95522013-05-22 14:41:37 +0200645WL_EXPORT void
646weston_surface_to_buffer(struct weston_surface *surface,
647 int sx, int sy, int *bx, int *by)
648{
649 float bxf, byf;
650
Jason Ekstranda7af7042013-10-12 22:38:11 -0500651 weston_transformed_coord(surface->width,
652 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100653 surface->buffer_viewport.transform,
654 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200655 sx, sy, &bxf, &byf);
656 *bx = floorf(bxf);
657 *by = floorf(byf);
658}
659
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200660WL_EXPORT pixman_box32_t
661weston_surface_to_buffer_rect(struct weston_surface *surface,
662 pixman_box32_t rect)
663{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500664 return weston_transformed_rect(surface->width,
665 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100666 surface->buffer_viewport.transform,
667 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200668 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200669}
670
671WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500672weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400673 struct weston_plane *plane)
674{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400676 return;
677
Jason Ekstranda7af7042013-10-12 22:38:11 -0500678 weston_view_damage_below(view);
679 view->plane = plane;
680 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400681}
682
683WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500684weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200685{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500686 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200687
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500688 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500689 pixman_region32_subtract(&damage, &view->transform.boundingbox,
690 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800691 if (view->plane)
692 pixman_region32_union(&view->plane->damage,
693 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500694 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800695 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200696}
697
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400698static void
699weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
700{
701 uint32_t different = es->output_mask ^ mask;
702 uint32_t entered = mask & different;
703 uint32_t left = es->output_mask & different;
704 struct weston_output *output;
705 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500706 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400707
708 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500709 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400710 return;
711 if (different == 0)
712 return;
713
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500714 client = wl_resource_get_client(es->resource);
715
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400716 wl_list_for_each(output, &es->compositor->output_list, link) {
717 if (1 << output->id & different)
718 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500719 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400720 client);
721 if (resource == NULL)
722 continue;
723 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500724 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400725 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500726 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400727 }
728}
729
730static void
731weston_surface_assign_output(struct weston_surface *es)
732{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733 struct weston_output *new_output;
734 struct weston_view *view;
735 pixman_region32_t region;
736 uint32_t max, area, mask;
737 pixman_box32_t *e;
738
739 new_output = NULL;
740 max = 0;
741 mask = 0;
742 pixman_region32_init(&region);
743 wl_list_for_each(view, &es->views, surface_link) {
744 if (!view->output)
745 continue;
746
747 pixman_region32_intersect(&region, &view->transform.boundingbox,
748 &view->output->region);
749
750 e = pixman_region32_extents(&region);
751 area = (e->x2 - e->x1) * (e->y2 - e->y1);
752
753 mask |= view->output_mask;
754
755 if (area >= max) {
756 new_output = view->output;
757 max = area;
758 }
759 }
760 pixman_region32_fini(&region);
761
762 es->output = new_output;
763 weston_surface_update_output_mask(es, mask);
764}
765
766static void
767weston_view_assign_output(struct weston_view *ev)
768{
769 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400770 struct weston_output *output, *new_output;
771 pixman_region32_t region;
772 uint32_t max, area, mask;
773 pixman_box32_t *e;
774
775 new_output = NULL;
776 max = 0;
777 mask = 0;
778 pixman_region32_init(&region);
779 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500780 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400781 &output->region);
782
783 e = pixman_region32_extents(&region);
784 area = (e->x2 - e->x1) * (e->y2 - e->y1);
785
786 if (area > 0)
787 mask |= 1 << output->id;
788
789 if (area >= max) {
790 new_output = output;
791 max = area;
792 }
793 }
794 pixman_region32_fini(&region);
795
Jason Ekstranda7af7042013-10-12 22:38:11 -0500796 ev->output = new_output;
797 ev->output_mask = mask;
798
799 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400800}
801
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200802static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
804 int32_t width, int32_t height,
805 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200806{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200807 float min_x = HUGE_VALF, min_y = HUGE_VALF;
808 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200809 int32_t s[4][2] = {
810 { sx, sy },
811 { sx, sy + height },
812 { sx + width, sy },
813 { sx + width, sy + height }
814 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200815 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200816 int i;
817
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300818 if (width == 0 || height == 0) {
819 /* avoid rounding empty bbox to 1x1 */
820 pixman_region32_init(bbox);
821 return;
822 }
823
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200824 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200825 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500826 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200827 if (x < min_x)
828 min_x = x;
829 if (x > max_x)
830 max_x = x;
831 if (y < min_y)
832 min_y = y;
833 if (y > max_y)
834 max_y = y;
835 }
836
Pekka Paalanen219b9822012-02-08 15:38:37 +0200837 int_x = floorf(min_x);
838 int_y = floorf(min_y);
839 pixman_region32_init_rect(bbox, int_x, int_y,
840 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200841}
842
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200843static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500844weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200845{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200847
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200848 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500849 view->geometry.x = roundf(view->geometry.x);
850 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200851
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500852 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500853 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
854 view->transform.position.matrix.d[12] = view->geometry.x;
855 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500856
Jason Ekstranda7af7042013-10-12 22:38:11 -0500857 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500858
Jason Ekstranda7af7042013-10-12 22:38:11 -0500859 view->transform.inverse = view->transform.position.matrix;
860 view->transform.inverse.d[12] = -view->geometry.x;
861 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500862
Jason Ekstranda7af7042013-10-12 22:38:11 -0500863 pixman_region32_init_rect(&view->transform.boundingbox,
864 view->geometry.x,
865 view->geometry.y,
866 view->geometry.width,
867 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500868
Jason Ekstranda7af7042013-10-12 22:38:11 -0500869 if (view->alpha == 1.0) {
870 pixman_region32_copy(&view->transform.opaque,
871 &view->surface->opaque);
872 pixman_region32_translate(&view->transform.opaque,
873 view->geometry.x,
874 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500875 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200876}
877
878static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200880{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881 struct weston_view *parent = view->geometry.parent;
882 struct weston_matrix *matrix = &view->transform.matrix;
883 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200884 struct weston_transform *tform;
885
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200887
888 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
890 view->transform.position.matrix.d[12] = view->geometry.x;
891 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200892
893 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200895 weston_matrix_multiply(matrix, &tform->matrix);
896
Pekka Paalanen483243f2013-03-08 14:56:50 +0200897 if (parent)
898 weston_matrix_multiply(matrix, &parent->transform.matrix);
899
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200900 if (weston_matrix_invert(inverse, matrix) < 0) {
901 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 weston_log("error: weston_view %p"
903 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200904 return -1;
905 }
906
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 view_compute_bbox(view, 0, 0, view->geometry.width,
908 view->geometry.height,
909 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500910
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200911 return 0;
912}
913
914WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500915weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200916{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200918
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200920 return;
921
Pekka Paalanen483243f2013-03-08 14:56:50 +0200922 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200924
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200926
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200928
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 pixman_region32_fini(&view->transform.boundingbox);
930 pixman_region32_fini(&view->transform.opaque);
931 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500932
Pekka Paalanencd403622012-01-25 13:37:39 +0200933 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 if (view->geometry.transformation_list.next ==
935 &view->transform.position.link &&
936 view->geometry.transformation_list.prev ==
937 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200938 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200940 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 if (weston_view_update_transform_enable(view) < 0)
942 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200943 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200944
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200946
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 wl_signal_emit(&view->surface->compositor->transform_signal,
950 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200951}
952
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200953WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500954weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200955{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200957
958 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 * The invariant: if view->geometry.dirty, then all views
960 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200961 * Corollary: if not parent->geometry.dirty, then all ancestors
962 * are not dirty.
963 */
964
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200966 return;
967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200971 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200973}
974
975WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976weston_view_to_global_fixed(struct weston_view *view,
977 wl_fixed_t vx, wl_fixed_t vy,
978 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100979{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200980 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 weston_view_to_global_float(view,
983 wl_fixed_to_double(vx),
984 wl_fixed_to_double(vy),
985 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100986 *x = wl_fixed_from_double(xf);
987 *y = wl_fixed_from_double(yf);
988}
989
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400990WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991weston_view_from_global_float(struct weston_view *view,
992 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200993{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200995 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
996
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200998
999 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001000 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001002 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 *vx = 0;
1004 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001005 return;
1006 }
1007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 *vx = v.f[0] / v.f[3];
1009 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001010 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 *vx = x - view->geometry.x;
1012 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001013 }
1014}
1015
1016WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017weston_view_from_global_fixed(struct weston_view *view,
1018 wl_fixed_t x, wl_fixed_t y,
1019 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001020{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001022
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 weston_view_from_global_float(view,
1024 wl_fixed_to_double(x),
1025 wl_fixed_to_double(y),
1026 &vxf, &vyf);
1027 *vx = wl_fixed_from_double(vxf);
1028 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001029}
1030
1031WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032weston_view_from_global(struct weston_view *view,
1033 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001034{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1038 *vx = floorf(vxf);
1039 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001040}
1041
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001042WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001043weston_surface_schedule_repaint(struct weston_surface *surface)
1044{
1045 struct weston_output *output;
1046
1047 wl_list_for_each(output, &surface->compositor->output_list, link)
1048 if (surface->output_mask & (1 << output->id))
1049 weston_output_schedule_repaint(output);
1050}
1051
1052WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053weston_view_schedule_repaint(struct weston_view *view)
1054{
1055 struct weston_output *output;
1056
1057 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1058 if (view->output_mask & (1 << output->id))
1059 weston_output_schedule_repaint(output);
1060}
1061
1062WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001063weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001064{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001065 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 0, 0, surface->width,
1067 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001068
Kristian Høgsberg98238702012-08-03 16:29:12 -04001069 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001070}
1071
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001072WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073weston_view_configure(struct weston_view *view,
1074 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001075{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076 view->geometry.x = x;
1077 view->geometry.y = y;
1078 view->geometry.width = width;
1079 view->geometry.height = height;
1080 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001081}
1082
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001083WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001085{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 view->geometry.x = x;
1087 view->geometry.y = y;
1088 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001089}
1090
Pekka Paalanen483243f2013-03-08 14:56:50 +02001091static void
1092transform_parent_handle_parent_destroy(struct wl_listener *listener,
1093 void *data)
1094{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 struct weston_view *view =
1096 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001097 geometry.parent_destroy_listener);
1098
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001100}
1101
1102WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103weston_view_set_transform_parent(struct weston_view *view,
1104 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001105{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 if (view->geometry.parent) {
1107 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1108 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001109 }
1110
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001112
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001114 transform_parent_handle_parent_destroy;
1115 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001116 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001118 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001120 }
1121
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 weston_view_geometry_dirty(view);
1123}
1124
1125WL_EXPORT int
1126weston_view_is_mapped(struct weston_view *view)
1127{
1128 if (view->output)
1129 return 1;
1130 else
1131 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001132}
1133
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001134WL_EXPORT int
1135weston_surface_is_mapped(struct weston_surface *surface)
1136{
1137 if (surface->output)
1138 return 1;
1139 else
1140 return 0;
1141}
1142
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001143static void
1144weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001145{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001146 int32_t width, height;
1147
1148 if (!surface->buffer_ref.buffer) {
1149 surface->width = 0;
1150 surface->height = 0;
1151 return;
1152 }
1153
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001154 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001155 case WL_OUTPUT_TRANSFORM_90:
1156 case WL_OUTPUT_TRANSFORM_270:
1157 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1158 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001159 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001160 height = surface->buffer_ref.buffer->width;
1161 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001162 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001163 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001164 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001165 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001166 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001167
1168 surface->width = width / surface->buffer_viewport.scale;
1169 surface->height = height / surface->buffer_viewport.scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001170}
1171
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001172WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001173weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001174{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001175 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001176
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001177 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001178
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001179 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001180}
1181
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182WL_EXPORT struct weston_view *
1183weston_compositor_pick_view(struct weston_compositor *compositor,
1184 wl_fixed_t x, wl_fixed_t y,
1185 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001186{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001188
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 wl_list_for_each(view, &compositor->view_list, link) {
1190 weston_view_from_global_fixed(view, x, y, vx, vy);
1191 if (pixman_region32_contains_point(&view->surface->input,
1192 wl_fixed_to_int(*vx),
1193 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001194 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001196 }
1197
1198 return NULL;
1199}
1200
1201static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001202weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001203{
Daniel Stone37816df2012-05-16 18:45:18 +01001204 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001205
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001206 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001207 return;
1208
Daniel Stone37816df2012-05-16 18:45:18 +01001209 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001210 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001211}
1212
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001213WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001214weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001215{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001216 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 if (!weston_view_is_mapped(view))
1219 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_view_damage_below(view);
1222 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001223 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 wl_list_remove(&view->layer_link);
1225 wl_list_init(&view->layer_link);
1226 wl_list_remove(&view->link);
1227 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 view->output_mask = 0;
1229 weston_surface_assign_output(view->surface);
1230
1231 if (weston_surface_is_mapped(view->surface))
1232 return;
1233
1234 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1235 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001236 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001238 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001239 NULL,
1240 wl_fixed_from_int(0),
1241 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001243 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001244 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247WL_EXPORT void
1248weston_surface_unmap(struct weston_surface *surface)
1249{
1250 struct weston_view *view;
1251
1252 wl_list_for_each(view, &surface->views, surface_link)
1253 weston_view_unmap(view);
1254 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001255}
1256
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001257struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001258 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001259 struct wl_list link;
1260};
1261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262WL_EXPORT void
1263weston_view_destroy(struct weston_view *view)
1264{
1265 wl_signal_emit(&view->destroy_signal, view);
1266
1267 assert(wl_list_empty(&view->geometry.child_list));
1268
1269 if (weston_view_is_mapped(view)) {
1270 weston_view_unmap(view);
1271 weston_compositor_build_view_list(view->surface->compositor);
1272 }
1273
1274 wl_list_remove(&view->link);
1275 wl_list_remove(&view->layer_link);
1276
1277 pixman_region32_fini(&view->clip);
1278 pixman_region32_fini(&view->transform.boundingbox);
1279
1280 weston_view_set_transform_parent(view, NULL);
1281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 wl_list_remove(&view->surface_link);
1283
1284 free(view);
1285}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001286
1287WL_EXPORT void
1288weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001289{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001290 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001292
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001293 if (--surface->ref_count > 0)
1294 return;
1295
1296 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1297
Pekka Paalanene67858b2013-04-25 13:57:42 +03001298 assert(wl_list_empty(&surface->subsurface_list_pending));
1299 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001300
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1302 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001303
Pekka Paalanenbc106382012-10-10 12:49:31 +03001304 wl_list_for_each_safe(cb, next,
1305 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001306 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001307
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001308 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001309 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001310 pixman_region32_fini(&surface->pending.damage);
1311
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001312 if (surface->pending.buffer)
1313 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1314
Pekka Paalanende685b82012-12-04 15:58:12 +02001315 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001316
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001317 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001318 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001319 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001320
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001321 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001322 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001323
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001324 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001325}
1326
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001327static void
1328destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001329{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001330 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001331
Giulio Camuffo0d379742013-11-15 22:06:15 +01001332 /* Set the resource to NULL, since we don't want to leave a
1333 * dangling pointer if the surface was refcounted and survives
1334 * the weston_surface_destroy() call. */
1335 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001336 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001337}
1338
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001339static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001340weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1341{
1342 struct weston_buffer *buffer =
1343 container_of(listener, struct weston_buffer, destroy_listener);
1344
1345 wl_signal_emit(&buffer->destroy_signal, buffer);
1346 free(buffer);
1347}
1348
1349struct weston_buffer *
1350weston_buffer_from_resource(struct wl_resource *resource)
1351{
1352 struct weston_buffer *buffer;
1353 struct wl_listener *listener;
1354
1355 listener = wl_resource_get_destroy_listener(resource,
1356 weston_buffer_destroy_handler);
1357
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001358 if (listener)
1359 return container_of(listener, struct weston_buffer,
1360 destroy_listener);
1361
1362 buffer = zalloc(sizeof *buffer);
1363 if (buffer == NULL)
1364 return NULL;
1365
1366 buffer->resource = resource;
1367 wl_signal_init(&buffer->destroy_signal);
1368 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001369 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001370 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001371
1372 return buffer;
1373}
1374
1375static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001376weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1377 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001378{
Pekka Paalanende685b82012-12-04 15:58:12 +02001379 struct weston_buffer_reference *ref =
1380 container_of(listener, struct weston_buffer_reference,
1381 destroy_listener);
1382
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001383 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001384 ref->buffer = NULL;
1385}
1386
1387WL_EXPORT void
1388weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001389 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001390{
1391 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001392 ref->buffer->busy_count--;
1393 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001394 assert(wl_resource_get_client(ref->buffer->resource));
1395 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001396 WL_BUFFER_RELEASE);
1397 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001398 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001399 }
1400
Pekka Paalanende685b82012-12-04 15:58:12 +02001401 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001402 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001403 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001404 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001405 }
1406
Pekka Paalanende685b82012-12-04 15:58:12 +02001407 ref->buffer = buffer;
1408 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1409}
1410
1411static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001412weston_surface_attach(struct weston_surface *surface,
1413 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001414{
1415 weston_buffer_reference(&surface->buffer_ref, buffer);
1416
Pekka Paalanena6421c42012-12-04 15:58:10 +02001417 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001418 if (weston_surface_is_mapped(surface))
1419 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001420 }
1421
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001422 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001423}
1424
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001425WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001427{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 wl_list_remove(&view->layer_link);
1429 wl_list_insert(below, &view->layer_link);
1430 weston_view_damage_below(view);
1431 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001432}
1433
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001434WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001436{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001437 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001438
1439 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001440 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001441}
1442
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001443WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001445{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001446 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001447
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001448 pixman_region32_union(&compositor->primary_plane.damage,
1449 &compositor->primary_plane.damage,
1450 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001451 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001452}
1453
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001454static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001456{
Pekka Paalanende685b82012-12-04 15:58:12 +02001457 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001458 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001459 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001460
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 empty_region(&surface->damage);
1462}
1463
1464static void
1465view_accumulate_damage(struct weston_view *view,
1466 pixman_region32_t *opaque)
1467{
1468 pixman_region32_t damage;
1469
1470 pixman_region32_init(&damage);
1471 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001472 pixman_box32_t *extents;
1473
Jason Ekstranda7af7042013-10-12 22:38:11 -05001474 extents = pixman_region32_extents(&view->surface->damage);
1475 view_compute_bbox(view, extents->x1, extents->y1,
1476 extents->x2 - extents->x1,
1477 extents->y2 - extents->y1,
1478 &damage);
1479 pixman_region32_translate(&damage,
1480 -view->plane->x,
1481 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001482 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 pixman_region32_copy(&damage, &view->surface->damage);
1484 pixman_region32_translate(&damage,
1485 view->geometry.x - view->plane->x,
1486 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001487 }
1488
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 pixman_region32_subtract(&damage, &damage, opaque);
1490 pixman_region32_union(&view->plane->damage,
1491 &view->plane->damage, &damage);
1492 pixman_region32_fini(&damage);
1493 pixman_region32_copy(&view->clip, opaque);
1494 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001495}
1496
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001497static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001498compositor_accumulate_damage(struct weston_compositor *ec)
1499{
1500 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001501 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001502 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001503
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001504 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001505
1506 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001507 pixman_region32_copy(&plane->clip, &clip);
1508
1509 pixman_region32_init(&opaque);
1510
Jason Ekstranda7af7042013-10-12 22:38:11 -05001511 wl_list_for_each(ev, &ec->view_list, link) {
1512 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001513 continue;
1514
Jason Ekstranda7af7042013-10-12 22:38:11 -05001515 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001516 }
1517
1518 pixman_region32_union(&clip, &clip, &opaque);
1519 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001520 }
1521
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001522 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001523
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 wl_list_for_each(ev, &ec->view_list, link)
1525 ev->surface->touched = 0;
1526
1527 wl_list_for_each(ev, &ec->view_list, link) {
1528 if (ev->surface->touched)
1529 continue;
1530 ev->surface->touched = 1;
1531
1532 surface_flush_damage(ev->surface);
1533
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001534 /* Both the renderer and the backend have seen the buffer
1535 * by now. If renderer needs the buffer, it has its own
1536 * reference set. If the backend wants to keep the buffer
1537 * around for migrating the surface into a non-primary plane
1538 * later, keep_buffer is true. Otherwise, drop the core
1539 * reference now, and allow early buffer release. This enables
1540 * clients to use single-buffering.
1541 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 if (!ev->surface->keep_buffer)
1543 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001544 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001545}
1546
1547static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001549{
1550 struct weston_subsurface *sub;
1551
Pekka Paalanene67858b2013-04-25 13:57:42 +03001552 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001554 continue;
1555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1557 wl_list_init(&sub->surface->views);
1558
1559 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001560 }
1561}
1562
1563static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001565{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 struct weston_subsurface *sub;
1567 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001568
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1570 if (sub->surface == surface)
1571 continue;
1572
1573 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1574 weston_view_destroy(view);
1575
1576 surface_free_unused_subsurface_views(sub->surface);
1577 }
1578}
1579
1580static void
1581view_list_add_subsurface_view(struct weston_compositor *compositor,
1582 struct weston_subsurface *sub,
1583 struct weston_view *parent)
1584{
1585 struct weston_subsurface *child;
1586 struct weston_view *view = NULL, *iv;
1587
1588 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1589 if (iv->geometry.parent == parent) {
1590 view = iv;
1591 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001592 }
1593 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001594
1595 if (view) {
1596 /* Put it back in the surface's list of views */
1597 wl_list_remove(&view->surface_link);
1598 wl_list_insert(&sub->surface->views, &view->surface_link);
1599 } else {
1600 view = weston_view_create(sub->surface);
1601 weston_view_configure(view,
1602 sub->position.x,
1603 sub->position.y,
1604 sub->surface->width,
1605 sub->surface->height);
1606 weston_view_set_transform_parent(view, parent);
1607 }
1608
1609 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610
Pekka Paalanenb188e912013-11-19 14:03:35 +02001611 if (wl_list_empty(&sub->surface->subsurface_list)) {
1612 wl_list_insert(compositor->view_list.prev, &view->link);
1613 return;
1614 }
1615
1616 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1617 if (child->surface == sub->surface)
1618 wl_list_insert(compositor->view_list.prev, &view->link);
1619 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001621 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001622}
1623
1624static void
1625view_list_add(struct weston_compositor *compositor,
1626 struct weston_view *view)
1627{
1628 struct weston_subsurface *sub;
1629
1630 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631
Pekka Paalanenb188e912013-11-19 14:03:35 +02001632 if (wl_list_empty(&view->surface->subsurface_list)) {
1633 wl_list_insert(compositor->view_list.prev, &view->link);
1634 return;
1635 }
1636
1637 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1638 if (sub->surface == view->surface)
1639 wl_list_insert(compositor->view_list.prev, &view->link);
1640 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001641 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001642 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643}
1644
1645static void
1646weston_compositor_build_view_list(struct weston_compositor *compositor)
1647{
1648 struct weston_view *view;
1649 struct weston_layer *layer;
1650
1651 wl_list_for_each(layer, &compositor->layer_list, link)
1652 wl_list_for_each(view, &layer->view_list, layer_link)
1653 surface_stash_subsurface_views(view->surface);
1654
1655 wl_list_init(&compositor->view_list);
1656 wl_list_for_each(layer, &compositor->layer_list, link) {
1657 wl_list_for_each(view, &layer->view_list, layer_link) {
1658 view_list_add(compositor, view);
1659 }
1660 }
1661
1662 wl_list_for_each(layer, &compositor->layer_list, link)
1663 wl_list_for_each(view, &layer->view_list, layer_link)
1664 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001665}
1666
David Herrmann1edf44c2013-10-22 17:11:26 +02001667static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001668weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001669{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001670 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001672 struct weston_animation *animation, *next;
1673 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001674 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001675 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001676 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001677
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001678 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001680
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001681 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001682 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001683 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684 wl_list_for_each(ev, &ec->view_list, link)
1685 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001686
Pekka Paalanene67858b2013-04-25 13:57:42 +03001687 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 wl_list_for_each(ev, &ec->view_list, link) {
1689 /* Note: This operation is safe to do multiple times on the
1690 * same surface.
1691 */
1692 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001693 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694 &ev->surface->frame_callback_list);
1695 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001696 }
1697 }
1698
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001699 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001700
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001701 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001702 pixman_region32_intersect(&output_damage,
1703 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001704 pixman_region32_subtract(&output_damage,
1705 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001706
Scott Moreauccbf29d2012-02-22 14:21:41 -07001707 if (output->dirty)
1708 weston_output_update_matrix(output);
1709
David Herrmann1edf44c2013-10-22 17:11:26 +02001710 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001711
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001712 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001713
Kristian Høgsbergef044142011-06-21 15:02:12 -04001714 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001715
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001716 weston_compositor_repick(ec);
1717 wl_event_loop_dispatch(ec->input_loop, 0);
1718
Jonas Ådahldb773762012-06-13 00:01:21 +02001719 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001720 wl_callback_send_done(cb->resource, msecs);
1721 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001722 }
1723
Scott Moreaud64cf212012-06-08 19:40:54 -06001724 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001725 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001726 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001727 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001728
1729 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001730}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001731
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001732static int
1733weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001734{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001735 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001736
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001737 wl_event_loop_dispatch(compositor->input_loop, 0);
1738
1739 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001740}
1741
1742WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001743weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001744{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001745 struct weston_compositor *compositor = output->compositor;
1746 struct wl_event_loop *loop =
1747 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001748 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001749
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001750 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001751
1752 if (output->repaint_needed &&
1753 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1754 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001755 r = weston_output_repaint(output, msecs);
1756 if (!r)
1757 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001758 }
1759
1760 output->repaint_scheduled = 0;
1761 if (compositor->input_loop_source)
1762 return;
1763
1764 fd = wl_event_loop_get_fd(compositor->input_loop);
1765 compositor->input_loop_source =
1766 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1767 weston_compositor_read_input, compositor);
1768}
1769
1770static void
1771idle_repaint(void *data)
1772{
1773 struct weston_output *output = data;
1774
Jonas Ådahle5a12252013-04-05 23:07:11 +02001775 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001776}
1777
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001778WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001779weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1780{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001782 if (below != NULL)
1783 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001784}
1785
1786WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001787weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001788{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001789 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001790 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001791
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001792 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1793 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001794 return;
1795
Kristian Høgsbergef044142011-06-21 15:02:12 -04001796 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001797 output->repaint_needed = 1;
1798 if (output->repaint_scheduled)
1799 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001800
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001801 wl_event_loop_add_idle(loop, idle_repaint, output);
1802 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001803
1804 if (compositor->input_loop_source) {
1805 wl_event_source_remove(compositor->input_loop_source);
1806 compositor->input_loop_source = NULL;
1807 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001808}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001809
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001810WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001811weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1812{
1813 struct weston_output *output;
1814
1815 wl_list_for_each(output, &compositor->output_list, link)
1816 weston_output_schedule_repaint(output);
1817}
1818
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001819static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001820surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001821{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001822 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001823}
1824
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001825static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001826surface_attach(struct wl_client *client,
1827 struct wl_resource *resource,
1828 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1829{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001830 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001831 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001832
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001833 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001834 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001835 if (buffer == NULL) {
1836 wl_client_post_no_memory(client);
1837 return;
1838 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001839 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001840
Pekka Paalanende685b82012-12-04 15:58:12 +02001841 /* Attach, attach, without commit in between does not send
1842 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001843 if (surface->pending.buffer)
1844 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001845
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001846 surface->pending.sx = sx;
1847 surface->pending.sy = sy;
1848 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001849 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001850 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001851 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001852 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001853 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001854}
1855
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001856static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001857surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001858 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001859 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001860{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001861 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001862
Pekka Paalanen8e159182012-10-10 12:49:25 +03001863 pixman_region32_union_rect(&surface->pending.damage,
1864 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001865 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001866}
1867
Kristian Høgsberg33418202011-08-16 23:01:28 -04001868static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001869destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001870{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001871 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001872
1873 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001874 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001875}
1876
1877static void
1878surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001879 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001880{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001881 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001882 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001883
1884 cb = malloc(sizeof *cb);
1885 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001886 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001887 return;
1888 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001889
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001890 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1891 callback);
1892 if (cb->resource == NULL) {
1893 free(cb);
1894 wl_resource_post_no_memory(resource);
1895 return;
1896 }
1897
Jason Ekstranda85118c2013-06-27 20:17:02 -05001898 wl_resource_set_implementation(cb->resource, NULL, cb,
1899 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001900
Pekka Paalanenbc106382012-10-10 12:49:31 +03001901 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001902}
1903
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001904static void
1905surface_set_opaque_region(struct wl_client *client,
1906 struct wl_resource *resource,
1907 struct wl_resource *region_resource)
1908{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001909 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001910 struct weston_region *region;
1911
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001912 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001913 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001914 pixman_region32_copy(&surface->pending.opaque,
1915 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001916 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001917 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001918 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001919}
1920
1921static void
1922surface_set_input_region(struct wl_client *client,
1923 struct wl_resource *resource,
1924 struct wl_resource *region_resource)
1925{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001926 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001927 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001928
1929 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001930 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001931 pixman_region32_copy(&surface->pending.input,
1932 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001933 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001934 pixman_region32_fini(&surface->pending.input);
1935 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001936 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001937}
1938
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001939static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001940weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001941{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001942 struct weston_subsurface *sub;
1943
1944 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1945 parent_link_pending) {
1946 wl_list_remove(&sub->parent_link);
1947 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1948 }
1949}
1950
1951static void
1952weston_surface_commit(struct weston_surface *surface)
1953{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001954 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001955 pixman_region32_t opaque;
1956
Alexander Larsson4ea95522013-05-22 14:41:37 +02001957 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02001958 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001959 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001960
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001961 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001962 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001963 weston_surface_attach(surface, surface->pending.buffer);
1964
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001965 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001966
1967 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001968 surface->configure(surface,
1969 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001971
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001972 if (surface->pending.buffer)
1973 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1974 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001975 surface->pending.sx = 0;
1976 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001977 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001978
1979 /* wl_surface.damage */
1980 pixman_region32_union(&surface->damage, &surface->damage,
1981 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001982 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1983 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 surface->width,
1985 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001986 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001987
1988 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001989 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 surface->width,
1991 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001992 pixman_region32_intersect(&opaque,
1993 &opaque, &surface->pending.opaque);
1994
1995 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1996 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 wl_list_for_each(view, &surface->views, surface_link)
1998 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001999 }
2000
2001 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002002
2003 /* wl_surface.set_input_region */
2004 pixman_region32_fini(&surface->input);
2005 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 surface->width,
2007 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002008 pixman_region32_intersect(&surface->input,
2009 &surface->input, &surface->pending.input);
2010
Pekka Paalanenbc106382012-10-10 12:49:31 +03002011 /* wl_surface.frame */
2012 wl_list_insert_list(&surface->frame_callback_list,
2013 &surface->pending.frame_callback_list);
2014 wl_list_init(&surface->pending.frame_callback_list);
2015
Pekka Paalanene67858b2013-04-25 13:57:42 +03002016 weston_surface_commit_subsurface_order(surface);
2017
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002018 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002019}
2020
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002021static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002022weston_subsurface_commit(struct weston_subsurface *sub);
2023
2024static void
2025weston_subsurface_parent_commit(struct weston_subsurface *sub,
2026 int parent_is_synchronized);
2027
2028static void
2029surface_commit(struct wl_client *client, struct wl_resource *resource)
2030{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002031 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002032 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2033
2034 if (sub) {
2035 weston_subsurface_commit(sub);
2036 return;
2037 }
2038
2039 weston_surface_commit(surface);
2040
2041 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2042 if (sub->surface != surface)
2043 weston_subsurface_parent_commit(sub, 0);
2044 }
2045}
2046
2047static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002048surface_set_buffer_transform(struct wl_client *client,
2049 struct wl_resource *resource, int transform)
2050{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002051 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002052
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002053 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002054}
2055
Alexander Larsson4ea95522013-05-22 14:41:37 +02002056static void
2057surface_set_buffer_scale(struct wl_client *client,
2058 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002059 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002060{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002061 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002062
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002063 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002064}
2065
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002066static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002067 surface_destroy,
2068 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002069 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002070 surface_frame,
2071 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002072 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002073 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002074 surface_set_buffer_transform,
2075 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002076};
2077
2078static void
2079compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002080 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002081{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002082 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002084
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002085 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002086 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002087 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002088 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002089 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002090
Jason Ekstranda85118c2013-06-27 20:17:02 -05002091 surface->resource =
2092 wl_resource_create(client, &wl_surface_interface,
2093 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002094 if (surface->resource == NULL) {
2095 weston_surface_destroy(surface);
2096 wl_resource_post_no_memory(resource);
2097 return;
2098 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002099 wl_resource_set_implementation(surface->resource, &surface_interface,
2100 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002101}
2102
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002103static void
2104destroy_region(struct wl_resource *resource)
2105{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002106 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002107
2108 pixman_region32_fini(&region->region);
2109 free(region);
2110}
2111
2112static void
2113region_destroy(struct wl_client *client, struct wl_resource *resource)
2114{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002115 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002116}
2117
2118static void
2119region_add(struct wl_client *client, struct wl_resource *resource,
2120 int32_t x, int32_t y, int32_t width, int32_t height)
2121{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002122 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002123
2124 pixman_region32_union_rect(&region->region, &region->region,
2125 x, y, width, height);
2126}
2127
2128static void
2129region_subtract(struct wl_client *client, struct wl_resource *resource,
2130 int32_t x, int32_t y, int32_t width, int32_t height)
2131{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002132 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002133 pixman_region32_t rect;
2134
2135 pixman_region32_init_rect(&rect, x, y, width, height);
2136 pixman_region32_subtract(&region->region, &region->region, &rect);
2137 pixman_region32_fini(&rect);
2138}
2139
2140static const struct wl_region_interface region_interface = {
2141 region_destroy,
2142 region_add,
2143 region_subtract
2144};
2145
2146static void
2147compositor_create_region(struct wl_client *client,
2148 struct wl_resource *resource, uint32_t id)
2149{
2150 struct weston_region *region;
2151
2152 region = malloc(sizeof *region);
2153 if (region == NULL) {
2154 wl_resource_post_no_memory(resource);
2155 return;
2156 }
2157
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002158 pixman_region32_init(&region->region);
2159
Jason Ekstranda85118c2013-06-27 20:17:02 -05002160 region->resource =
2161 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002162 if (region->resource == NULL) {
2163 free(region);
2164 wl_resource_post_no_memory(resource);
2165 return;
2166 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002167 wl_resource_set_implementation(region->resource, &region_interface,
2168 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002169}
2170
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002171static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002172 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002173 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002174};
2175
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002176static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002177weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2178{
2179 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002181 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182
Alexander Larsson4ea95522013-05-22 14:41:37 +02002183 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002184 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002185 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002186
Pekka Paalanene67858b2013-04-25 13:57:42 +03002187 /* wl_surface.attach */
2188 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2189 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2190 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2191
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002192 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002193
2194 if (surface->configure && sub->cached.newly_attached)
2195 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002197 sub->cached.sx = 0;
2198 sub->cached.sy = 0;
2199 sub->cached.newly_attached = 0;
2200
2201 /* wl_surface.damage */
2202 pixman_region32_union(&surface->damage, &surface->damage,
2203 &sub->cached.damage);
2204 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2205 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002206 surface->width,
2207 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002208 empty_region(&sub->cached.damage);
2209
2210 /* wl_surface.set_opaque_region */
2211 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002212 surface->width,
2213 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002214 pixman_region32_intersect(&opaque,
2215 &opaque, &sub->cached.opaque);
2216
2217 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2218 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219 wl_list_for_each(view, &surface->views, surface_link)
2220 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002221 }
2222
2223 pixman_region32_fini(&opaque);
2224
2225 /* wl_surface.set_input_region */
2226 pixman_region32_fini(&surface->input);
2227 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002228 surface->width,
2229 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002230 pixman_region32_intersect(&surface->input,
2231 &surface->input, &sub->cached.input);
2232
2233 /* wl_surface.frame */
2234 wl_list_insert_list(&surface->frame_callback_list,
2235 &sub->cached.frame_callback_list);
2236 wl_list_init(&sub->cached.frame_callback_list);
2237
2238 weston_surface_commit_subsurface_order(surface);
2239
2240 weston_surface_schedule_repaint(surface);
2241
2242 sub->cached.has_data = 0;
2243}
2244
2245static void
2246weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2247{
2248 struct weston_surface *surface = sub->surface;
2249
2250 /*
2251 * If this commit would cause the surface to move by the
2252 * attach(dx, dy) parameters, the old damage region must be
2253 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002254 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002255 */
2256 pixman_region32_translate(&sub->cached.damage,
2257 -surface->pending.sx, -surface->pending.sy);
2258 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2259 &surface->pending.damage);
2260 empty_region(&surface->pending.damage);
2261
2262 if (surface->pending.newly_attached) {
2263 sub->cached.newly_attached = 1;
2264 weston_buffer_reference(&sub->cached.buffer_ref,
2265 surface->pending.buffer);
2266 }
2267 sub->cached.sx += surface->pending.sx;
2268 sub->cached.sy += surface->pending.sy;
2269 surface->pending.sx = 0;
2270 surface->pending.sy = 0;
2271 surface->pending.newly_attached = 0;
2272
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002273 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002274
2275 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2276
2277 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2278
2279 wl_list_insert_list(&sub->cached.frame_callback_list,
2280 &surface->pending.frame_callback_list);
2281 wl_list_init(&surface->pending.frame_callback_list);
2282
2283 sub->cached.has_data = 1;
2284}
2285
2286static int
2287weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2288{
2289 while (sub) {
2290 if (sub->synchronized)
2291 return 1;
2292
2293 if (!sub->parent)
2294 return 0;
2295
2296 sub = weston_surface_to_subsurface(sub->parent);
2297 }
2298
2299 return 0;
2300}
2301
2302static void
2303weston_subsurface_commit(struct weston_subsurface *sub)
2304{
2305 struct weston_surface *surface = sub->surface;
2306 struct weston_subsurface *tmp;
2307
2308 /* Recursive check for effectively synchronized. */
2309 if (weston_subsurface_is_synchronized(sub)) {
2310 weston_subsurface_commit_to_cache(sub);
2311 } else {
2312 if (sub->cached.has_data) {
2313 /* flush accumulated state from cache */
2314 weston_subsurface_commit_to_cache(sub);
2315 weston_subsurface_commit_from_cache(sub);
2316 } else {
2317 weston_surface_commit(surface);
2318 }
2319
2320 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2321 if (tmp->surface != surface)
2322 weston_subsurface_parent_commit(tmp, 0);
2323 }
2324 }
2325}
2326
2327static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002328weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329{
2330 struct weston_surface *surface = sub->surface;
2331 struct weston_subsurface *tmp;
2332
Pekka Paalanene67858b2013-04-25 13:57:42 +03002333 /* From now on, commit_from_cache the whole sub-tree, regardless of
2334 * the synchronized mode of each child. This sub-surface or some
2335 * of its ancestors were synchronized, so we are synchronized
2336 * all the way down.
2337 */
2338
2339 if (sub->cached.has_data)
2340 weston_subsurface_commit_from_cache(sub);
2341
2342 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2343 if (tmp->surface != surface)
2344 weston_subsurface_parent_commit(tmp, 1);
2345 }
2346}
2347
2348static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002349weston_subsurface_parent_commit(struct weston_subsurface *sub,
2350 int parent_is_synchronized)
2351{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002352 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002353 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002354 wl_list_for_each(view, &sub->surface->views, surface_link)
2355 weston_view_set_position(view,
2356 sub->position.x,
2357 sub->position.y);
2358
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002359 sub->position.set = 0;
2360 }
2361
2362 if (parent_is_synchronized || sub->synchronized)
2363 weston_subsurface_synchronized_commit(sub);
2364}
2365
2366static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002367subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2368 int32_t width, int32_t height)
2369{
2370 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002371 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002372
Jason Ekstranda7af7042013-10-12 22:38:11 -05002373 wl_list_for_each(view, &surface->views, surface_link)
2374 weston_view_configure(view,
2375 view->geometry.x + dx,
2376 view->geometry.y + dy,
2377 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002378
2379 /* No need to check parent mappedness, because if parent is not
2380 * mapped, parent is not in a visible layer, so this sub-surface
2381 * will not be drawn either.
2382 */
2383 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002384 /* Cannot call weston_surface_update_transform(),
2385 * because that would call it also for the parent surface,
2386 * which might not be mapped yet. That would lead to
2387 * inconsistent state, where the window could never be
2388 * mapped.
2389 *
2390 * Instead just assing any output, to make
2391 * weston_surface_is_mapped() return true, so that when the
2392 * parent surface does get mapped, this one will get
2393 * included, too. See surface_list_add().
2394 */
2395 assert(!wl_list_empty(&compositor->output_list));
2396 surface->output = container_of(compositor->output_list.next,
2397 struct weston_output, link);
2398 }
2399}
2400
2401static struct weston_subsurface *
2402weston_surface_to_subsurface(struct weston_surface *surface)
2403{
2404 if (surface->configure == subsurface_configure)
2405 return surface->configure_private;
2406
2407 return NULL;
2408}
2409
Pekka Paalanen01388e22013-04-25 13:57:44 +03002410WL_EXPORT struct weston_surface *
2411weston_surface_get_main_surface(struct weston_surface *surface)
2412{
2413 struct weston_subsurface *sub;
2414
2415 while (surface && (sub = weston_surface_to_subsurface(surface)))
2416 surface = sub->parent;
2417
2418 return surface;
2419}
2420
Pekka Paalanene67858b2013-04-25 13:57:42 +03002421static void
2422subsurface_set_position(struct wl_client *client,
2423 struct wl_resource *resource, int32_t x, int32_t y)
2424{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002425 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002426
2427 if (!sub)
2428 return;
2429
2430 sub->position.x = x;
2431 sub->position.y = y;
2432 sub->position.set = 1;
2433}
2434
2435static struct weston_subsurface *
2436subsurface_from_surface(struct weston_surface *surface)
2437{
2438 struct weston_subsurface *sub;
2439
2440 sub = weston_surface_to_subsurface(surface);
2441 if (sub)
2442 return sub;
2443
2444 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2445 if (sub->surface == surface)
2446 return sub;
2447
2448 return NULL;
2449}
2450
2451static struct weston_subsurface *
2452subsurface_sibling_check(struct weston_subsurface *sub,
2453 struct weston_surface *surface,
2454 const char *request)
2455{
2456 struct weston_subsurface *sibling;
2457
2458 sibling = subsurface_from_surface(surface);
2459
2460 if (!sibling) {
2461 wl_resource_post_error(sub->resource,
2462 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2463 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002464 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002465 return NULL;
2466 }
2467
2468 if (sibling->parent != sub->parent) {
2469 wl_resource_post_error(sub->resource,
2470 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2471 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002472 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473 return NULL;
2474 }
2475
2476 return sibling;
2477}
2478
2479static void
2480subsurface_place_above(struct wl_client *client,
2481 struct wl_resource *resource,
2482 struct wl_resource *sibling_resource)
2483{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002484 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002485 struct weston_surface *surface =
2486 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002487 struct weston_subsurface *sibling;
2488
2489 if (!sub)
2490 return;
2491
2492 sibling = subsurface_sibling_check(sub, surface, "place_above");
2493 if (!sibling)
2494 return;
2495
2496 wl_list_remove(&sub->parent_link_pending);
2497 wl_list_insert(sibling->parent_link_pending.prev,
2498 &sub->parent_link_pending);
2499}
2500
2501static void
2502subsurface_place_below(struct wl_client *client,
2503 struct wl_resource *resource,
2504 struct wl_resource *sibling_resource)
2505{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002506 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002507 struct weston_surface *surface =
2508 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002509 struct weston_subsurface *sibling;
2510
2511 if (!sub)
2512 return;
2513
2514 sibling = subsurface_sibling_check(sub, surface, "place_below");
2515 if (!sibling)
2516 return;
2517
2518 wl_list_remove(&sub->parent_link_pending);
2519 wl_list_insert(&sibling->parent_link_pending,
2520 &sub->parent_link_pending);
2521}
2522
2523static void
2524subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2525{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002526 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002527
2528 if (sub)
2529 sub->synchronized = 1;
2530}
2531
2532static void
2533subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2534{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002535 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002536
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002537 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002538 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002539
2540 /* If sub became effectively desynchronized, flush. */
2541 if (!weston_subsurface_is_synchronized(sub))
2542 weston_subsurface_synchronized_commit(sub);
2543 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002544}
2545
2546static void
2547weston_subsurface_cache_init(struct weston_subsurface *sub)
2548{
2549 pixman_region32_init(&sub->cached.damage);
2550 pixman_region32_init(&sub->cached.opaque);
2551 pixman_region32_init(&sub->cached.input);
2552 wl_list_init(&sub->cached.frame_callback_list);
2553 sub->cached.buffer_ref.buffer = NULL;
2554}
2555
2556static void
2557weston_subsurface_cache_fini(struct weston_subsurface *sub)
2558{
2559 struct weston_frame_callback *cb, *tmp;
2560
2561 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002562 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002563
2564 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2565 pixman_region32_fini(&sub->cached.damage);
2566 pixman_region32_fini(&sub->cached.opaque);
2567 pixman_region32_fini(&sub->cached.input);
2568}
2569
2570static void
2571weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2572{
2573 wl_list_remove(&sub->parent_link);
2574 wl_list_remove(&sub->parent_link_pending);
2575 wl_list_remove(&sub->parent_destroy_listener.link);
2576 sub->parent = NULL;
2577}
2578
2579static void
2580weston_subsurface_destroy(struct weston_subsurface *sub);
2581
2582static void
2583subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2584{
2585 struct weston_subsurface *sub =
2586 container_of(listener, struct weston_subsurface,
2587 surface_destroy_listener);
2588 assert(data == &sub->surface->resource);
2589
2590 /* The protocol object (wl_resource) is left inert. */
2591 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002592 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002593
2594 weston_subsurface_destroy(sub);
2595}
2596
2597static void
2598subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2599{
2600 struct weston_subsurface *sub =
2601 container_of(listener, struct weston_subsurface,
2602 parent_destroy_listener);
2603 assert(data == &sub->parent->resource);
2604 assert(sub->surface != sub->parent);
2605
2606 if (weston_surface_is_mapped(sub->surface))
2607 weston_surface_unmap(sub->surface);
2608
2609 weston_subsurface_unlink_parent(sub);
2610}
2611
2612static void
2613subsurface_resource_destroy(struct wl_resource *resource)
2614{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002615 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002616
2617 if (sub)
2618 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002619}
2620
2621static void
2622subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2623{
2624 wl_resource_destroy(resource);
2625}
2626
2627static void
2628weston_subsurface_link_parent(struct weston_subsurface *sub,
2629 struct weston_surface *parent)
2630{
2631 sub->parent = parent;
2632 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002633 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634 &sub->parent_destroy_listener);
2635
2636 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2637 wl_list_insert(&parent->subsurface_list_pending,
2638 &sub->parent_link_pending);
2639}
2640
2641static void
2642weston_subsurface_link_surface(struct weston_subsurface *sub,
2643 struct weston_surface *surface)
2644{
2645 sub->surface = surface;
2646 sub->surface_destroy_listener.notify =
2647 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002648 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002649 &sub->surface_destroy_listener);
2650}
2651
2652static void
2653weston_subsurface_destroy(struct weston_subsurface *sub)
2654{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002655 struct weston_view *view, *next;
2656
Pekka Paalanene67858b2013-04-25 13:57:42 +03002657 assert(sub->surface);
2658
2659 if (sub->resource) {
2660 assert(weston_surface_to_subsurface(sub->surface) == sub);
2661 assert(sub->parent_destroy_listener.notify ==
2662 subsurface_handle_parent_destroy);
2663
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2665 weston_view_destroy(view);
2666
Pekka Paalanene67858b2013-04-25 13:57:42 +03002667 if (sub->parent)
2668 weston_subsurface_unlink_parent(sub);
2669
2670 weston_subsurface_cache_fini(sub);
2671
2672 sub->surface->configure = NULL;
2673 sub->surface->configure_private = NULL;
2674 } else {
2675 /* the dummy weston_subsurface for the parent itself */
2676 assert(sub->parent_destroy_listener.notify == NULL);
2677 wl_list_remove(&sub->parent_link);
2678 wl_list_remove(&sub->parent_link_pending);
2679 }
2680
2681 wl_list_remove(&sub->surface_destroy_listener.link);
2682 free(sub);
2683}
2684
2685static const struct wl_subsurface_interface subsurface_implementation = {
2686 subsurface_destroy,
2687 subsurface_set_position,
2688 subsurface_place_above,
2689 subsurface_place_below,
2690 subsurface_set_sync,
2691 subsurface_set_desync
2692};
2693
2694static struct weston_subsurface *
2695weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2696 struct weston_surface *parent)
2697{
2698 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002699 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002700
2701 sub = calloc(1, sizeof *sub);
2702 if (!sub)
2703 return NULL;
2704
Jason Ekstranda7af7042013-10-12 22:38:11 -05002705 wl_list_init(&sub->unused_views);
2706
Jason Ekstranda85118c2013-06-27 20:17:02 -05002707 sub->resource =
2708 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709 if (!sub->resource) {
2710 free(sub);
2711 return NULL;
2712 }
2713
Jason Ekstranda85118c2013-06-27 20:17:02 -05002714 wl_resource_set_implementation(sub->resource,
2715 &subsurface_implementation,
2716 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002717 weston_subsurface_link_surface(sub, surface);
2718 weston_subsurface_link_parent(sub, parent);
2719 weston_subsurface_cache_init(sub);
2720 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002721
2722 return sub;
2723}
2724
2725/* Create a dummy subsurface for having the parent itself in its
2726 * sub-surface lists. Makes stacking order manipulation easy.
2727 */
2728static struct weston_subsurface *
2729weston_subsurface_create_for_parent(struct weston_surface *parent)
2730{
2731 struct weston_subsurface *sub;
2732
2733 sub = calloc(1, sizeof *sub);
2734 if (!sub)
2735 return NULL;
2736
2737 weston_subsurface_link_surface(sub, parent);
2738 sub->parent = parent;
2739 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2740 wl_list_insert(&parent->subsurface_list_pending,
2741 &sub->parent_link_pending);
2742
2743 return sub;
2744}
2745
2746static void
2747subcompositor_get_subsurface(struct wl_client *client,
2748 struct wl_resource *resource,
2749 uint32_t id,
2750 struct wl_resource *surface_resource,
2751 struct wl_resource *parent_resource)
2752{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002753 struct weston_surface *surface =
2754 wl_resource_get_user_data(surface_resource);
2755 struct weston_surface *parent =
2756 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002757 struct weston_subsurface *sub;
2758 static const char where[] = "get_subsurface: wl_subsurface@";
2759
2760 if (surface == parent) {
2761 wl_resource_post_error(resource,
2762 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2763 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002764 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002765 return;
2766 }
2767
2768 if (weston_surface_to_subsurface(surface)) {
2769 wl_resource_post_error(resource,
2770 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2771 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002772 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002773 return;
2774 }
2775
2776 if (surface->configure) {
2777 wl_resource_post_error(resource,
2778 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2779 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002780 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002781 return;
2782 }
2783
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002784 if (weston_surface_get_main_surface(parent) == surface) {
2785 wl_resource_post_error(resource,
2786 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2787 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002788 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002789 return;
2790 }
2791
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792 /* make sure the parent is in its own list */
2793 if (wl_list_empty(&parent->subsurface_list)) {
2794 if (!weston_subsurface_create_for_parent(parent)) {
2795 wl_resource_post_no_memory(resource);
2796 return;
2797 }
2798 }
2799
2800 sub = weston_subsurface_create(id, surface, parent);
2801 if (!sub) {
2802 wl_resource_post_no_memory(resource);
2803 return;
2804 }
2805
2806 surface->configure = subsurface_configure;
2807 surface->configure_private = sub;
2808}
2809
2810static void
2811subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2812{
2813 wl_resource_destroy(resource);
2814}
2815
2816static const struct wl_subcompositor_interface subcompositor_interface = {
2817 subcompositor_destroy,
2818 subcompositor_get_subsurface
2819};
2820
2821static void
2822bind_subcompositor(struct wl_client *client,
2823 void *data, uint32_t version, uint32_t id)
2824{
2825 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002826 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002827
Jason Ekstranda85118c2013-06-27 20:17:02 -05002828 resource =
2829 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002830 if (resource == NULL) {
2831 wl_client_post_no_memory(client);
2832 return;
2833 }
2834 wl_resource_set_implementation(resource, &subcompositor_interface,
2835 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836}
2837
2838static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002839weston_compositor_dpms(struct weston_compositor *compositor,
2840 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002841{
2842 struct weston_output *output;
2843
2844 wl_list_for_each(output, &compositor->output_list, link)
2845 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002846 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002847}
2848
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002849WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002850weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002851{
Neil Roberts8b62e202013-09-30 13:14:47 +01002852 uint32_t old_state = compositor->state;
2853
2854 /* The state needs to be changed before emitting the wake
2855 * signal because that may try to schedule a repaint which
2856 * will not work if the compositor is still sleeping */
2857 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2858
2859 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002860 case WESTON_COMPOSITOR_SLEEPING:
2861 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2862 /* fall through */
2863 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002864 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002865 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002866 /* fall through */
2867 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002868 wl_event_source_timer_update(compositor->idle_source,
2869 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002870 }
2871}
2872
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002873WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002874weston_compositor_offscreen(struct weston_compositor *compositor)
2875{
2876 switch (compositor->state) {
2877 case WESTON_COMPOSITOR_OFFSCREEN:
2878 return;
2879 case WESTON_COMPOSITOR_SLEEPING:
2880 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2881 /* fall through */
2882 default:
2883 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2884 wl_event_source_timer_update(compositor->idle_source, 0);
2885 }
2886}
2887
2888WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002889weston_compositor_sleep(struct weston_compositor *compositor)
2890{
2891 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2892 return;
2893
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002894 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002895 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2896 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2897}
2898
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002899static int
2900idle_handler(void *data)
2901{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002902 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002903
2904 if (compositor->idle_inhibit)
2905 return 1;
2906
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002907 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002908 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002909
2910 return 1;
2911}
2912
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002913WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002914weston_plane_init(struct weston_plane *plane,
2915 struct weston_compositor *ec,
2916 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002917{
2918 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002919 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002920 plane->x = x;
2921 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002922 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002923
2924 /* Init the link so that the call to wl_list_remove() when releasing
2925 * the plane without ever stacking doesn't lead to a crash */
2926 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002927}
2928
2929WL_EXPORT void
2930weston_plane_release(struct weston_plane *plane)
2931{
Xiong Zhang97116532013-10-23 13:58:31 +08002932 struct weston_view *view;
2933
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002934 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002935 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002936
Xiong Zhang97116532013-10-23 13:58:31 +08002937 wl_list_for_each(view, &plane->compositor->view_list, link) {
2938 if (view->plane == plane)
2939 view->plane = NULL;
2940 }
2941
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002942 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002943}
2944
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002945WL_EXPORT void
2946weston_compositor_stack_plane(struct weston_compositor *ec,
2947 struct weston_plane *plane,
2948 struct weston_plane *above)
2949{
2950 if (above)
2951 wl_list_insert(above->link.prev, &plane->link);
2952 else
2953 wl_list_insert(&ec->plane_list, &plane->link);
2954}
2955
Casey Dahlin9074db52012-04-19 22:50:09 -04002956static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002957{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002958 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002959}
2960
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002961static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002962bind_output(struct wl_client *client,
2963 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002964{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002965 struct weston_output *output = data;
2966 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002967 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002968
Jason Ekstranda85118c2013-06-27 20:17:02 -05002969 resource = wl_resource_create(client, &wl_output_interface,
2970 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002971 if (resource == NULL) {
2972 wl_client_post_no_memory(client);
2973 return;
2974 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002975
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002976 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002977 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002978
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002979 wl_output_send_geometry(resource,
2980 output->x,
2981 output->y,
2982 output->mm_width,
2983 output->mm_height,
2984 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002985 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002986 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002987 if (version >= 2)
2988 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002989 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002990
2991 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002992 wl_output_send_mode(resource,
2993 mode->flags,
2994 mode->width,
2995 mode->height,
2996 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002997 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002998
2999 if (version >= 2)
3000 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003001}
3002
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003003WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003004weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003005{
Richard Hughes64ddde12013-05-01 21:52:10 +01003006 wl_signal_emit(&output->destroy_signal, output);
3007
Richard Hughesafe690c2013-05-02 10:10:04 +01003008 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003009 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003010 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003011 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003012
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003013 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003014}
3015
Scott Moreau1bad5db2012-08-18 01:04:05 -06003016static void
3017weston_output_compute_transform(struct weston_output *output)
3018{
3019 struct weston_matrix transform;
3020 int flip;
3021
3022 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003023 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003024
3025 switch(output->transform) {
3026 case WL_OUTPUT_TRANSFORM_FLIPPED:
3027 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3028 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3029 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003030 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003031 flip = -1;
3032 break;
3033 default:
3034 flip = 1;
3035 break;
3036 }
3037
3038 switch(output->transform) {
3039 case WL_OUTPUT_TRANSFORM_NORMAL:
3040 case WL_OUTPUT_TRANSFORM_FLIPPED:
3041 transform.d[0] = flip;
3042 transform.d[1] = 0;
3043 transform.d[4] = 0;
3044 transform.d[5] = 1;
3045 break;
3046 case WL_OUTPUT_TRANSFORM_90:
3047 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3048 transform.d[0] = 0;
3049 transform.d[1] = -flip;
3050 transform.d[4] = 1;
3051 transform.d[5] = 0;
3052 break;
3053 case WL_OUTPUT_TRANSFORM_180:
3054 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3055 transform.d[0] = -flip;
3056 transform.d[1] = 0;
3057 transform.d[4] = 0;
3058 transform.d[5] = -1;
3059 break;
3060 case WL_OUTPUT_TRANSFORM_270:
3061 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3062 transform.d[0] = 0;
3063 transform.d[1] = flip;
3064 transform.d[4] = -1;
3065 transform.d[5] = 0;
3066 break;
3067 default:
3068 break;
3069 }
3070
3071 weston_matrix_multiply(&output->matrix, &transform);
3072}
3073
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003074WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003075weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003076{
Scott Moreau850ca422012-05-21 15:21:25 -06003077 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003078 struct weston_matrix camera;
3079 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003080
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003081 weston_matrix_init(&output->matrix);
3082 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003083 -(output->x + output->width / 2.0),
3084 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003085
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003086 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003087 2.0 / output->width,
3088 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003089
3090 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003091
Scott Moreauccbf29d2012-02-22 14:21:41 -07003092 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003093 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003094 weston_matrix_init(&camera);
3095 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003096 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003097 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003098 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003099 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003100 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003101 weston_matrix_multiply(&output->matrix, &modelview);
3102 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003103
Scott Moreauccbf29d2012-02-22 14:21:41 -07003104 output->dirty = 0;
3105}
3106
Scott Moreau1bad5db2012-08-18 01:04:05 -06003107static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003108weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003109{
3110 output->transform = transform;
3111
3112 switch (transform) {
3113 case WL_OUTPUT_TRANSFORM_90:
3114 case WL_OUTPUT_TRANSFORM_270:
3115 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3116 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3117 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003118 output->width = output->current_mode->height;
3119 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003120 break;
3121 case WL_OUTPUT_TRANSFORM_NORMAL:
3122 case WL_OUTPUT_TRANSFORM_180:
3123 case WL_OUTPUT_TRANSFORM_FLIPPED:
3124 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003125 output->width = output->current_mode->width;
3126 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003127 break;
3128 default:
3129 break;
3130 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003131
Hardening57388e42013-09-18 23:56:36 +02003132 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003133 output->width /= scale;
3134 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003135}
3136
Scott Moreauccbf29d2012-02-22 14:21:41 -07003137WL_EXPORT void
3138weston_output_move(struct weston_output *output, int x, int y)
3139{
3140 output->x = x;
3141 output->y = y;
3142
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003143 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003144 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003145 output->width,
3146 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003147}
3148
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003149WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003150weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003151 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003152 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003153{
3154 output->compositor = c;
3155 output->x = x;
3156 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003157 output->mm_width = mm_width;
3158 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003159 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003160 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003161
Alexander Larsson0b135062013-05-28 16:23:36 +02003162 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003163 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003164
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003165 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003166 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003167
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003168 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003169 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003170 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003171 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003172
Casey Dahlin58ba1372012-04-19 22:50:08 -04003173 output->id = ffs(~output->compositor->output_id_pool) - 1;
3174 output->compositor->output_id_pool |= 1 << output->id;
3175
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003176 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003177 wl_global_create(c->wl_display, &wl_output_interface, 2,
3178 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003179 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003180}
3181
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003182WL_EXPORT void
3183weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003184 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003185 wl_fixed_t *x, wl_fixed_t *y)
3186{
3187 wl_fixed_t tx, ty;
3188 wl_fixed_t width, height;
3189
Hardeningff39efa2013-09-18 23:56:35 +02003190 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3191 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003192
3193 switch(output->transform) {
3194 case WL_OUTPUT_TRANSFORM_NORMAL:
3195 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003196 tx = device_x;
3197 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003198 break;
3199 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003200 tx = device_y;
3201 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003202 break;
3203 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003204 tx = width - device_x;
3205 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003206 break;
3207 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003208 tx = width - device_y;
3209 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003210 break;
3211 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003212 tx = width - device_x;
3213 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003214 break;
3215 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003216 tx = width - device_y;
3217 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003218 break;
3219 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003220 tx = device_x;
3221 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003222 break;
3223 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003224 tx = device_y;
3225 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003226 break;
3227 }
3228
Hardeningff39efa2013-09-18 23:56:35 +02003229 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3230 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003231}
3232
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003233static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003234compositor_bind(struct wl_client *client,
3235 void *data, uint32_t version, uint32_t id)
3236{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003237 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003238 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003239
Jason Ekstranda85118c2013-06-27 20:17:02 -05003240 resource = wl_resource_create(client, &wl_compositor_interface,
3241 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003242 if (resource == NULL) {
3243 wl_client_post_no_memory(client);
3244 return;
3245 }
3246
3247 wl_resource_set_implementation(resource, &compositor_interface,
3248 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003249}
3250
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003251static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003252log_uname(void)
3253{
3254 struct utsname usys;
3255
3256 uname(&usys);
3257
3258 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3259 usys.version, usys.machine);
3260}
3261
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003262WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003263weston_environment_get_fd(const char *env)
3264{
3265 char *e, *end;
3266 int fd, flags;
3267
3268 e = getenv(env);
3269 if (!e)
3270 return -1;
3271 fd = strtol(e, &end, 0);
3272 if (*end != '\0')
3273 return -1;
3274
3275 flags = fcntl(fd, F_GETFD);
3276 if (flags == -1)
3277 return -1;
3278
3279 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3280 unsetenv(env);
3281
3282 return fd;
3283}
3284
3285WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003286weston_compositor_init(struct weston_compositor *ec,
3287 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003288 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003289 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003290{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003291 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003292 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003293 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003294
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003295 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003296 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003297 wl_signal_init(&ec->destroy_signal);
3298 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003299 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003300 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003301 wl_signal_init(&ec->idle_signal);
3302 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003303 wl_signal_init(&ec->show_input_panel_signal);
3304 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003305 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003306 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003307 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003308 wl_signal_init(&ec->session_signal);
3309 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003310
Casey Dahlin58ba1372012-04-19 22:50:08 -04003311 ec->output_id_pool = 0;
3312
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003313 if (!wl_global_create(display, &wl_compositor_interface, 3,
3314 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003315 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003316
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003317 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3318 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003319 return -1;
3320
Jason Ekstranda7af7042013-10-12 22:38:11 -05003321 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003322 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003323 wl_list_init(&ec->layer_list);
3324 wl_list_init(&ec->seat_list);
3325 wl_list_init(&ec->output_list);
3326 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003327 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003328 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003329 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003330 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003331 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003332
Xiong Zhang97116532013-10-23 13:58:31 +08003333 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003334 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003335
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003336 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3337 weston_config_section_get_string(s, "keymap_rules",
3338 (char **) &xkb_names.rules, NULL);
3339 weston_config_section_get_string(s, "keymap_model",
3340 (char **) &xkb_names.model, NULL);
3341 weston_config_section_get_string(s, "keymap_layout",
3342 (char **) &xkb_names.layout, NULL);
3343 weston_config_section_get_string(s, "keymap_variant",
3344 (char **) &xkb_names.variant, NULL);
3345 weston_config_section_get_string(s, "keymap_options",
3346 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003347
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003348 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3349 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003350
3351 ec->ping_handler = NULL;
3352
3353 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003354 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003355
3356 wl_data_device_manager_init(ec->wl_display);
3357
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003358 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003359
Daniel Stone725c2c32012-06-22 14:04:36 +01003360 loop = wl_display_get_event_loop(ec->wl_display);
3361 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3362 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3363
3364 ec->input_loop = wl_event_loop_create();
3365
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003366 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3367 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3368
3369 weston_compositor_schedule_repaint(ec);
3370
Daniel Stone725c2c32012-06-22 14:04:36 +01003371 return 0;
3372}
3373
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003374WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003375weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003376{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003377 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003378
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003379 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003380 if (ec->input_loop_source)
3381 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003382
Matt Roper361d2ad2011-08-29 13:52:23 -07003383 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003384 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003385 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003386
Daniel Stone325fc2d2012-05-30 16:31:58 +01003387 weston_binding_list_destroy_all(&ec->key_binding_list);
3388 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003389 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003390 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003391 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003392
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003393 weston_plane_release(&ec->primary_plane);
3394
Jonas Ådahlc97af922012-03-28 22:36:09 +02003395 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003396
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003397 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003398}
3399
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003400WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003401weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3402 const struct weston_pointer_grab_interface *interface)
3403{
3404 struct weston_seat *seat;
3405
3406 ec->default_pointer_grab = interface;
3407 wl_list_for_each(seat, &ec->seat_list, link) {
3408 if (seat->pointer) {
3409 weston_pointer_set_default_grab(seat->pointer,
3410 interface);
3411 }
3412 }
3413}
3414
3415WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003416weston_version(int *major, int *minor, int *micro)
3417{
3418 *major = WESTON_VERSION_MAJOR;
3419 *minor = WESTON_VERSION_MINOR;
3420 *micro = WESTON_VERSION_MICRO;
3421}
3422
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003423static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003424 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003425 const char *desc;
3426} capability_strings[] = {
3427 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003428 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003429};
3430
3431static void
3432weston_compositor_log_capabilities(struct weston_compositor *compositor)
3433{
3434 unsigned i;
3435 int yes;
3436
3437 weston_log("Compositor capabilities:\n");
3438 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3439 yes = compositor->capabilities & capability_strings[i].bit;
3440 weston_log_continue(STAMP_SPACE "%s %s\n",
3441 capability_strings[i].desc,
3442 yes ? "yes" : "no");
3443 }
3444}
3445
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003446static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003447{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003448 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003449
Martin Minarik6d118362012-06-07 18:01:59 +02003450 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003451 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003452
3453 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003454}
3455
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003456#ifdef HAVE_LIBUNWIND
3457
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003458static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003459print_backtrace(void)
3460{
3461 unw_cursor_t cursor;
3462 unw_context_t context;
3463 unw_word_t off;
3464 unw_proc_info_t pip;
3465 int ret, i = 0;
3466 char procname[256];
3467 const char *filename;
3468 Dl_info dlinfo;
3469
3470 pip.unwind_info = NULL;
3471 ret = unw_getcontext(&context);
3472 if (ret) {
3473 weston_log("unw_getcontext: %d\n", ret);
3474 return;
3475 }
3476
3477 ret = unw_init_local(&cursor, &context);
3478 if (ret) {
3479 weston_log("unw_init_local: %d\n", ret);
3480 return;
3481 }
3482
3483 ret = unw_step(&cursor);
3484 while (ret > 0) {
3485 ret = unw_get_proc_info(&cursor, &pip);
3486 if (ret) {
3487 weston_log("unw_get_proc_info: %d\n", ret);
3488 break;
3489 }
3490
3491 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3492 if (ret && ret != -UNW_ENOMEM) {
3493 if (ret != -UNW_EUNSPEC)
3494 weston_log("unw_get_proc_name: %d\n", ret);
3495 procname[0] = '?';
3496 procname[1] = 0;
3497 }
3498
3499 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3500 *dlinfo.dli_fname)
3501 filename = dlinfo.dli_fname;
3502 else
3503 filename = "?";
3504
3505 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3506 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3507
3508 ret = unw_step(&cursor);
3509 if (ret < 0)
3510 weston_log("unw_step: %d\n", ret);
3511 }
3512}
3513
3514#else
3515
3516static void
3517print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003518{
3519 void *buffer[32];
3520 int i, count;
3521 Dl_info info;
3522
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003523 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3524 for (i = 0; i < count; i++) {
3525 dladdr(buffer[i], &info);
3526 weston_log(" [%016lx] %s (%s)\n",
3527 (long) buffer[i],
3528 info.dli_sname ? info.dli_sname : "--",
3529 info.dli_fname);
3530 }
3531}
3532
3533#endif
3534
3535static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003536on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003537{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003538 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003539 * then call the backend restore function, which will switch
3540 * back to the vt we launched from or ungrab X etc and then
3541 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003542 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003543 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003544 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003545
Peter Maatmane5b42e42013-03-27 22:38:53 +01003546 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003547
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003548 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003549
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003550 segv_compositor->restore(segv_compositor);
3551
3552 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003553}
3554
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003555WL_EXPORT void *
3556weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003557{
3558 char path[PATH_MAX];
3559 void *module, *init;
3560
3561 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003562 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003563 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003564 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003565
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003566 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3567 if (module) {
3568 weston_log("Module '%s' already loaded\n", path);
3569 dlclose(module);
3570 return NULL;
3571 }
3572
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003573 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003574 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003575 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003576 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003577 return NULL;
3578 }
3579
3580 init = dlsym(module, entrypoint);
3581 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003582 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003583 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003584 return NULL;
3585 }
3586
3587 return init;
3588}
3589
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003590static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003591load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003592 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003593{
3594 const char *p, *end;
3595 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003596 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003597 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003598
3599 if (modules == NULL)
3600 return 0;
3601
3602 p = modules;
3603 while (*p) {
3604 end = strchrnul(p, ',');
3605 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003606 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003607 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003608 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003609 p = end;
3610 while (*p == ',')
3611 p++;
3612
3613 }
3614
3615 return 0;
3616}
3617
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003618static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003619 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3620
3621static const char xdg_wrong_message[] =
3622 "fatal: environment variable XDG_RUNTIME_DIR\n"
3623 "is set to \"%s\", which is not a directory.\n";
3624
3625static const char xdg_wrong_mode_message[] =
3626 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3627 "correctly. Unix access mode must be 0700 but is %o,\n"
3628 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003629 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003630
3631static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003632 "Refer to your distribution on how to get it, or\n"
3633 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3634 "on how to implement it.\n";
3635
Martin Minarik37032f82012-06-18 20:15:18 +02003636static void
3637verify_xdg_runtime_dir(void)
3638{
3639 char *dir = getenv("XDG_RUNTIME_DIR");
3640 struct stat s;
3641
3642 if (!dir) {
3643 weston_log(xdg_error_message);
3644 weston_log_continue(xdg_detail_message);
3645 exit(EXIT_FAILURE);
3646 }
3647
3648 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3649 weston_log(xdg_wrong_message, dir);
3650 weston_log_continue(xdg_detail_message);
3651 exit(EXIT_FAILURE);
3652 }
3653
3654 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3655 weston_log(xdg_wrong_mode_message,
3656 dir, s.st_mode & 0777, s.st_uid);
3657 weston_log_continue(xdg_detail_message);
3658 }
3659}
3660
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003661static int
3662usage(int error_code)
3663{
3664 fprintf(stderr,
3665 "Usage: weston [OPTIONS]\n\n"
3666 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3667 "Weston supports multiple backends, and depending on which backend is in use\n"
3668 "different options will be accepted.\n\n"
3669
3670
3671 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003672 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003673 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003674 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3675 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003676 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003677 " -S, --socket=NAME\tName of socket to listen on\n"
3678 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003679 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003680 " --log==FILE\t\tLog to the given file\n"
3681 " -h, --help\t\tThis help message\n\n");
3682
3683 fprintf(stderr,
3684 "Options for drm-backend.so:\n\n"
3685 " --connector=ID\tBring up only this connector\n"
3686 " --seat=SEAT\t\tThe seat that weston should run on\n"
3687 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003688 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003689 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3690
3691 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003692 "Options for fbdev-backend.so:\n\n"
3693 " --tty=TTY\t\tThe tty to use\n"
3694 " --device=DEVICE\tThe framebuffer device to use\n\n");
3695
3696 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003697 "Options for x11-backend.so:\n\n"
3698 " --width=WIDTH\t\tWidth of X window\n"
3699 " --height=HEIGHT\tHeight of X window\n"
3700 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003701 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003702 " --output-count=COUNT\tCreate multiple outputs\n"
3703 " --no-input\t\tDont create input devices\n\n");
3704
3705 fprintf(stderr,
3706 "Options for wayland-backend.so:\n\n"
3707 " --width=WIDTH\t\tWidth of Wayland surface\n"
3708 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003709 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003710 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003711 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003712 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003713 " --display=DISPLAY\tWayland display to connect to\n\n");
3714
Pekka Paalanene31e0532013-05-22 18:03:07 +03003715#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3716 fprintf(stderr,
3717 "Options for rpi-backend.so:\n\n"
3718 " --tty=TTY\t\tThe tty to use\n"
3719 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3720 " --transform=TR\tThe output transformation, TR is one of:\n"
3721 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3722 "\n");
3723#endif
3724
Hardeningc077a842013-07-08 00:51:35 +02003725#if defined(BUILD_RDP_COMPOSITOR)
3726 fprintf(stderr,
3727 "Options for rdp-backend.so:\n\n"
3728 " --width=WIDTH\t\tWidth of desktop\n"
3729 " --height=HEIGHT\tHeight of desktop\n"
3730 " --extra-modes=MODES\t\n"
3731 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3732 " --address=ADDR\tThe address to bind\n"
3733 " --port=PORT\tThe port to listen on\n"
3734 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3735 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3736 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3737 "\n");
3738#endif
3739
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003740 exit(error_code);
3741}
3742
Peter Maatmane5b42e42013-03-27 22:38:53 +01003743static void
3744catch_signals(void)
3745{
3746 struct sigaction action;
3747
3748 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3749 action.sa_sigaction = on_caught_signal;
3750 sigemptyset(&action.sa_mask);
3751 sigaction(SIGSEGV, &action, NULL);
3752 sigaction(SIGABRT, &action, NULL);
3753}
3754
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003755int main(int argc, char *argv[])
3756{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003757 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003758 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003759 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003760 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003761 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003762 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003763 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003764 int *argc, char *argv[],
3765 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003766 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003767 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003768 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003769 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003770 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003771 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003772 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003773 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003774 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003775 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003776 struct weston_config *config;
3777 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003778
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003779 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003780 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003781 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003782 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3783 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003784 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003785 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003786 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003787 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003788 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003789
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003790 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003791
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003792 if (help)
3793 usage(EXIT_SUCCESS);
3794
Scott Moreau12245142012-08-29 15:15:58 -06003795 if (version) {
3796 printf(PACKAGE_STRING "\n");
3797 return EXIT_SUCCESS;
3798 }
3799
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003800 weston_log_file_open(log);
3801
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003802 weston_log("%s\n"
3803 STAMP_SPACE "%s\n"
3804 STAMP_SPACE "Bug reports to: %s\n"
3805 STAMP_SPACE "Build: %s\n",
3806 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003807 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003808 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003809
Martin Minarik37032f82012-06-18 20:15:18 +02003810 verify_xdg_runtime_dir();
3811
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003812 display = wl_display_create();
3813
Tiago Vignatti2116b892011-08-08 05:52:59 -07003814 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003815 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3816 display);
3817 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3818 display);
3819 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3820 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003821
3822 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003823 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3824 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003825
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003826 config = weston_config_parse("weston.ini");
3827 if (config != NULL) {
3828 weston_log("Using config file '%s'\n",
3829 weston_config_get_full_path(config));
3830 } else {
3831 weston_log("Starting with no config file.\n");
3832 }
3833 section = weston_config_get_section(config, "core", NULL, NULL);
3834
3835 weston_config_section_get_string(section, "backend", &backend, NULL);
3836 if (option_backend) {
3837 backend = option_backend;
3838 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003839 if (!backend) {
3840 if (getenv("WAYLAND_DISPLAY"))
3841 backend = "wayland-backend.so";
3842 else if (getenv("DISPLAY"))
3843 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003844 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003845 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003846 }
3847
Jason Ekstranda985da42013-08-22 17:28:03 -05003848 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003849
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003850 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003851 if (!backend_init)
3852 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003853
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003854 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003855 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003856 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003857 exit(EXIT_FAILURE);
3858 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003859
Peter Maatmane5b42e42013-03-27 22:38:53 +01003860 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003861 segv_compositor = ec;
3862
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003863 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003864 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003865
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003866 setenv("WAYLAND_DISPLAY", socket_name, 1);
3867
Jason Ekstranda985da42013-08-22 17:28:03 -05003868 if (!shell)
3869 weston_config_section_get_string(section, "shell", &shell,
3870 "desktop-shell.so");
3871 if (load_modules(ec, shell, &argc, argv) < 0)
3872 goto out;
3873
Ossama Othmana50e6e42013-05-14 09:48:26 -07003874 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003875 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003876 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003877 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003878
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003879 for (i = 1; i < argc; i++)
3880 weston_log("fatal: unhandled option: %s\n", argv[i]);
3881 if (argc > 1) {
3882 ret = EXIT_FAILURE;
3883 goto out;
3884 }
3885
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003886 weston_compositor_log_capabilities(ec);
3887
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003888 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003889 weston_log("fatal: failed to add socket: %m\n");
3890 ret = EXIT_FAILURE;
3891 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003892 }
3893
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003894 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003895
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003896 wl_display_run(display);
3897
3898 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003899 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003900 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003901
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003902 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003903
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003904 for (i = ARRAY_LENGTH(signals); i;)
3905 wl_event_source_remove(signals[--i]);
3906
Daniel Stone855028f2012-05-01 20:37:10 +01003907 weston_compositor_xkb_destroy(ec);
3908
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003909 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003910 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003911
Martin Minarik19e6f262012-06-07 13:08:46 +02003912 weston_log_file_close();
3913
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003914 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003915}