blob: cfcb273f0b7a97eb03fa5979aa4afde23da968b3 [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
140 output->current_mode = mode;
141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349WL_EXPORT struct weston_view *
350weston_view_create(struct weston_surface *surface)
351{
352 struct weston_view *view;
353
354 view = calloc(1, sizeof *view);
355 if (view == NULL)
356 return NULL;
357
358 view->surface = surface;
359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 /* Assign to surface */
361 wl_list_insert(&surface->views, &view->surface_link);
362
363 wl_signal_init(&view->destroy_signal);
364 wl_list_init(&view->link);
365 wl_list_init(&view->layer_link);
366
Xiong Zhang97116532013-10-23 13:58:31 +0800367 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368
369 pixman_region32_init(&view->clip);
370
371 view->alpha = 1.0;
372 pixman_region32_init(&view->transform.opaque);
373
374 wl_list_init(&view->geometry.transformation_list);
375 wl_list_insert(&view->geometry.transformation_list,
376 &view->transform.position.link);
377 weston_matrix_init(&view->transform.position.matrix);
378 wl_list_init(&view->geometry.child_list);
379 pixman_region32_init(&view->transform.boundingbox);
380 view->transform.dirty = 1;
381
382 view->output = NULL;
383
384 return view;
385}
386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500387WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500388weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400391
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200392 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400393 if (surface == NULL)
394 return NULL;
395
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500396 wl_signal_init(&surface->destroy_signal);
397
398 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500399
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500400 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200401 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400402
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200403 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200404 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200405 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200406 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400407 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100408 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200409
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400410 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500411 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300412 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400413
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414 wl_list_init(&surface->views);
415
416 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500417
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300418 surface->pending.buffer_destroy_listener.notify =
419 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300420 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300421 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300422 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300423 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300424
Pekka Paalanene67858b2013-04-25 13:57:42 +0300425 wl_list_init(&surface->subsurface_list);
426 wl_list_init(&surface->subsurface_list_pending);
427
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400428 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500429}
430
Alex Wu8811bf92012-02-28 18:07:54 +0800431WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500432weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200433 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500434{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100435 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500436}
437
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400438WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500439weston_view_to_global_float(struct weston_view *view,
440 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200441{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200443 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
444
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200446
447 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200448 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700449 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200450 v.f[3]);
451 *x = 0;
452 *y = 0;
453 return;
454 }
455
456 *x = v.f[0] / v.f[3];
457 *y = v.f[1] / v.f[3];
458 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459 *x = sx + view->geometry.x;
460 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200461 }
462}
463
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500464WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200465weston_transformed_coord(int width, int height,
466 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200467 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200468 float sx, float sy, float *bx, float *by)
469{
470 switch (transform) {
471 case WL_OUTPUT_TRANSFORM_NORMAL:
472 default:
473 *bx = sx;
474 *by = sy;
475 break;
476 case WL_OUTPUT_TRANSFORM_FLIPPED:
477 *bx = width - sx;
478 *by = sy;
479 break;
480 case WL_OUTPUT_TRANSFORM_90:
481 *bx = height - sy;
482 *by = sx;
483 break;
484 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
485 *bx = height - sy;
486 *by = width - sx;
487 break;
488 case WL_OUTPUT_TRANSFORM_180:
489 *bx = width - sx;
490 *by = height - sy;
491 break;
492 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
493 *bx = sx;
494 *by = height - sy;
495 break;
496 case WL_OUTPUT_TRANSFORM_270:
497 *bx = sy;
498 *by = width - sx;
499 break;
500 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
501 *bx = sy;
502 *by = sx;
503 break;
504 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200505
506 *bx *= scale;
507 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200508}
509
510WL_EXPORT pixman_box32_t
511weston_transformed_rect(int width, int height,
512 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200513 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200514 pixman_box32_t rect)
515{
516 float x1, x2, y1, y2;
517
518 pixman_box32_t ret;
519
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200522 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200523 rect.x2, rect.y2, &x2, &y2);
524
525 if (x1 <= x2) {
526 ret.x1 = x1;
527 ret.x2 = x2;
528 } else {
529 ret.x1 = x2;
530 ret.x2 = x1;
531 }
532
533 if (y1 <= y2) {
534 ret.y1 = y1;
535 ret.y2 = y2;
536 } else {
537 ret.y1 = y2;
538 ret.y2 = y1;
539 }
540
541 return ret;
542}
543
544WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500545weston_transformed_region(int width, int height,
546 enum wl_output_transform transform,
547 int32_t scale,
548 pixman_region32_t *src, pixman_region32_t *dest)
549{
550 pixman_box32_t *src_rects, *dest_rects;
551 int nrects, i;
552
553 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
554 if (src != dest)
555 pixman_region32_copy(dest, src);
556 return;
557 }
558
559 src_rects = pixman_region32_rectangles(src, &nrects);
560 dest_rects = malloc(nrects * sizeof(*dest_rects));
561 if (!dest_rects)
562 return;
563
564 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
565 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
566 } else {
567 for (i = 0; i < nrects; i++) {
568 switch (transform) {
569 default:
570 case WL_OUTPUT_TRANSFORM_NORMAL:
571 dest_rects[i].x1 = src_rects[i].x1;
572 dest_rects[i].y1 = src_rects[i].y1;
573 dest_rects[i].x2 = src_rects[i].x2;
574 dest_rects[i].y2 = src_rects[i].y2;
575 break;
576 case WL_OUTPUT_TRANSFORM_90:
577 dest_rects[i].x1 = height - src_rects[i].y2;
578 dest_rects[i].y1 = src_rects[i].x1;
579 dest_rects[i].x2 = height - src_rects[i].y1;
580 dest_rects[i].y2 = src_rects[i].x2;
581 break;
582 case WL_OUTPUT_TRANSFORM_180:
583 dest_rects[i].x1 = width - src_rects[i].x2;
584 dest_rects[i].y1 = height - src_rects[i].y2;
585 dest_rects[i].x2 = width - src_rects[i].x1;
586 dest_rects[i].y2 = height - src_rects[i].y1;
587 break;
588 case WL_OUTPUT_TRANSFORM_270:
589 dest_rects[i].x1 = src_rects[i].y1;
590 dest_rects[i].y1 = width - src_rects[i].x2;
591 dest_rects[i].x2 = src_rects[i].y2;
592 dest_rects[i].y2 = width - src_rects[i].x1;
593 break;
594 case WL_OUTPUT_TRANSFORM_FLIPPED:
595 dest_rects[i].x1 = width - src_rects[i].x2;
596 dest_rects[i].y1 = src_rects[i].y1;
597 dest_rects[i].x2 = width - src_rects[i].x1;
598 dest_rects[i].y2 = src_rects[i].y2;
599 break;
600 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
601 dest_rects[i].x1 = height - src_rects[i].y2;
602 dest_rects[i].y1 = width - src_rects[i].x2;
603 dest_rects[i].x2 = height - src_rects[i].y1;
604 dest_rects[i].y2 = width - src_rects[i].x1;
605 break;
606 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
607 dest_rects[i].x1 = src_rects[i].x1;
608 dest_rects[i].y1 = height - src_rects[i].y2;
609 dest_rects[i].x2 = src_rects[i].x2;
610 dest_rects[i].y2 = height - src_rects[i].y1;
611 break;
612 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
613 dest_rects[i].x1 = src_rects[i].y1;
614 dest_rects[i].y1 = src_rects[i].x1;
615 dest_rects[i].x2 = src_rects[i].y2;
616 dest_rects[i].y2 = src_rects[i].x2;
617 break;
618 }
619 }
620 }
621
622 if (scale != 1) {
623 for (i = 0; i < nrects; i++) {
624 dest_rects[i].x1 *= scale;
625 dest_rects[i].x2 *= scale;
626 dest_rects[i].y1 *= scale;
627 dest_rects[i].y2 *= scale;
628 }
629 }
630
631 pixman_region32_clear(dest);
632 pixman_region32_init_rects(dest, dest_rects, nrects);
633 free(dest_rects);
634}
635
636WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200637weston_surface_to_buffer_float(struct weston_surface *surface,
638 float sx, float sy, float *bx, float *by)
639{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500640 weston_transformed_coord(surface->width,
641 surface->height,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200642 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200643 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200644 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200645}
646
Alexander Larsson4ea95522013-05-22 14:41:37 +0200647WL_EXPORT void
648weston_surface_to_buffer(struct weston_surface *surface,
649 int sx, int sy, int *bx, int *by)
650{
651 float bxf, byf;
652
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 weston_transformed_coord(surface->width,
654 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200655 surface->buffer_transform,
656 surface->buffer_scale,
657 sx, sy, &bxf, &byf);
658 *bx = floorf(bxf);
659 *by = floorf(byf);
660}
661
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200662WL_EXPORT pixman_box32_t
663weston_surface_to_buffer_rect(struct weston_surface *surface,
664 pixman_box32_t rect)
665{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500666 return weston_transformed_rect(surface->width,
667 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200668 surface->buffer_transform,
669 surface->buffer_scale,
670 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200671}
672
673WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500674weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400675 struct weston_plane *plane)
676{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500677 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400678 return;
679
Jason Ekstranda7af7042013-10-12 22:38:11 -0500680 weston_view_damage_below(view);
681 view->plane = plane;
682 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400683}
684
685WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200687{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500688 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200689
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500690 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500691 pixman_region32_subtract(&damage, &view->transform.boundingbox,
692 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800693 if (view->plane)
694 pixman_region32_union(&view->plane->damage,
695 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500696 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800697 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200698}
699
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400700static void
701weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
702{
703 uint32_t different = es->output_mask ^ mask;
704 uint32_t entered = mask & different;
705 uint32_t left = es->output_mask & different;
706 struct weston_output *output;
707 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500708 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400709
710 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500711 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400712 return;
713 if (different == 0)
714 return;
715
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500716 client = wl_resource_get_client(es->resource);
717
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400718 wl_list_for_each(output, &es->compositor->output_list, link) {
719 if (1 << output->id & different)
720 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500721 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400722 client);
723 if (resource == NULL)
724 continue;
725 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500726 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400727 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500728 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400729 }
730}
731
732static void
733weston_surface_assign_output(struct weston_surface *es)
734{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 struct weston_output *new_output;
736 struct weston_view *view;
737 pixman_region32_t region;
738 uint32_t max, area, mask;
739 pixman_box32_t *e;
740
741 new_output = NULL;
742 max = 0;
743 mask = 0;
744 pixman_region32_init(&region);
745 wl_list_for_each(view, &es->views, surface_link) {
746 if (!view->output)
747 continue;
748
749 pixman_region32_intersect(&region, &view->transform.boundingbox,
750 &view->output->region);
751
752 e = pixman_region32_extents(&region);
753 area = (e->x2 - e->x1) * (e->y2 - e->y1);
754
755 mask |= view->output_mask;
756
757 if (area >= max) {
758 new_output = view->output;
759 max = area;
760 }
761 }
762 pixman_region32_fini(&region);
763
764 es->output = new_output;
765 weston_surface_update_output_mask(es, mask);
766}
767
768static void
769weston_view_assign_output(struct weston_view *ev)
770{
771 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400772 struct weston_output *output, *new_output;
773 pixman_region32_t region;
774 uint32_t max, area, mask;
775 pixman_box32_t *e;
776
777 new_output = NULL;
778 max = 0;
779 mask = 0;
780 pixman_region32_init(&region);
781 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500782 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400783 &output->region);
784
785 e = pixman_region32_extents(&region);
786 area = (e->x2 - e->x1) * (e->y2 - e->y1);
787
788 if (area > 0)
789 mask |= 1 << output->id;
790
791 if (area >= max) {
792 new_output = output;
793 max = area;
794 }
795 }
796 pixman_region32_fini(&region);
797
Jason Ekstranda7af7042013-10-12 22:38:11 -0500798 ev->output = new_output;
799 ev->output_mask = mask;
800
801 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400802}
803
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200804static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500805view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
806 int32_t width, int32_t height,
807 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200808{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200809 float min_x = HUGE_VALF, min_y = HUGE_VALF;
810 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200811 int32_t s[4][2] = {
812 { sx, sy },
813 { sx, sy + height },
814 { sx + width, sy },
815 { sx + width, sy + height }
816 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200817 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200818 int i;
819
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300820 if (width == 0 || height == 0) {
821 /* avoid rounding empty bbox to 1x1 */
822 pixman_region32_init(bbox);
823 return;
824 }
825
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200826 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200827 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500828 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200829 if (x < min_x)
830 min_x = x;
831 if (x > max_x)
832 max_x = x;
833 if (y < min_y)
834 min_y = y;
835 if (y > max_y)
836 max_y = y;
837 }
838
Pekka Paalanen219b9822012-02-08 15:38:37 +0200839 int_x = floorf(min_x);
840 int_y = floorf(min_y);
841 pixman_region32_init_rect(bbox, int_x, int_y,
842 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200843}
844
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200845static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200847{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500848 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200849
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200850 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 view->geometry.x = roundf(view->geometry.x);
852 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200853
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500854 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500855 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
856 view->transform.position.matrix.d[12] = view->geometry.x;
857 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500858
Jason Ekstranda7af7042013-10-12 22:38:11 -0500859 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500860
Jason Ekstranda7af7042013-10-12 22:38:11 -0500861 view->transform.inverse = view->transform.position.matrix;
862 view->transform.inverse.d[12] = -view->geometry.x;
863 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500864
Jason Ekstranda7af7042013-10-12 22:38:11 -0500865 pixman_region32_init_rect(&view->transform.boundingbox,
866 view->geometry.x,
867 view->geometry.y,
868 view->geometry.width,
869 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500870
Jason Ekstranda7af7042013-10-12 22:38:11 -0500871 if (view->alpha == 1.0) {
872 pixman_region32_copy(&view->transform.opaque,
873 &view->surface->opaque);
874 pixman_region32_translate(&view->transform.opaque,
875 view->geometry.x,
876 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500877 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200878}
879
880static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 struct weston_view *parent = view->geometry.parent;
884 struct weston_matrix *matrix = &view->transform.matrix;
885 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200886 struct weston_transform *tform;
887
Jason Ekstranda7af7042013-10-12 22:38:11 -0500888 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200889
890 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
892 view->transform.position.matrix.d[12] = view->geometry.x;
893 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200894
895 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200897 weston_matrix_multiply(matrix, &tform->matrix);
898
Pekka Paalanen483243f2013-03-08 14:56:50 +0200899 if (parent)
900 weston_matrix_multiply(matrix, &parent->transform.matrix);
901
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200902 if (weston_matrix_invert(inverse, matrix) < 0) {
903 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 weston_log("error: weston_view %p"
905 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200906 return -1;
907 }
908
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 view_compute_bbox(view, 0, 0, view->geometry.width,
910 view->geometry.height,
911 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500912
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200913 return 0;
914}
915
916WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200918{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200920
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200922 return;
923
Pekka Paalanen483243f2013-03-08 14:56:50 +0200924 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200926
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200928
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200930
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 pixman_region32_fini(&view->transform.boundingbox);
932 pixman_region32_fini(&view->transform.opaque);
933 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500934
Pekka Paalanencd403622012-01-25 13:37:39 +0200935 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 if (view->geometry.transformation_list.next ==
937 &view->transform.position.link &&
938 view->geometry.transformation_list.prev ==
939 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200940 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200942 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 if (weston_view_update_transform_enable(view) < 0)
944 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200945 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200946
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300950
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 wl_signal_emit(&view->surface->compositor->transform_signal,
952 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200953}
954
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200955WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200957{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200959
960 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 * The invariant: if view->geometry.dirty, then all views
962 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200963 * Corollary: if not parent->geometry.dirty, then all ancestors
964 * are not dirty.
965 */
966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200968 return;
969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200973 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200975}
976
977WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978weston_view_to_global_fixed(struct weston_view *view,
979 wl_fixed_t vx, wl_fixed_t vy,
980 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100981{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200982 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100983
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 weston_view_to_global_float(view,
985 wl_fixed_to_double(vx),
986 wl_fixed_to_double(vy),
987 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100988 *x = wl_fixed_from_double(xf);
989 *y = wl_fixed_from_double(yf);
990}
991
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400992WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993weston_view_from_global_float(struct weston_view *view,
994 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200995{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200997 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
998
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001000
1001 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001002 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001004 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 *vx = 0;
1006 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001007 return;
1008 }
1009
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 *vx = v.f[0] / v.f[3];
1011 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001012 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 *vx = x - view->geometry.x;
1014 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001015 }
1016}
1017
1018WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019weston_view_from_global_fixed(struct weston_view *view,
1020 wl_fixed_t x, wl_fixed_t y,
1021 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001022{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001024
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 weston_view_from_global_float(view,
1026 wl_fixed_to_double(x),
1027 wl_fixed_to_double(y),
1028 &vxf, &vyf);
1029 *vx = wl_fixed_from_double(vxf);
1030 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001031}
1032
1033WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034weston_view_from_global(struct weston_view *view,
1035 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001038
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1040 *vx = floorf(vxf);
1041 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001042}
1043
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001044WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001045weston_surface_schedule_repaint(struct weston_surface *surface)
1046{
1047 struct weston_output *output;
1048
1049 wl_list_for_each(output, &surface->compositor->output_list, link)
1050 if (surface->output_mask & (1 << output->id))
1051 weston_output_schedule_repaint(output);
1052}
1053
1054WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055weston_view_schedule_repaint(struct weston_view *view)
1056{
1057 struct weston_output *output;
1058
1059 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1060 if (view->output_mask & (1 << output->id))
1061 weston_output_schedule_repaint(output);
1062}
1063
1064WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001065weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001066{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001067 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 0, 0, surface->width,
1069 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001070
Kristian Høgsberg98238702012-08-03 16:29:12 -04001071 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001072}
1073
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001074WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075weston_view_configure(struct weston_view *view,
1076 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001077{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 view->geometry.x = x;
1079 view->geometry.y = y;
1080 view->geometry.width = width;
1081 view->geometry.height = height;
1082 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001083}
1084
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001085WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001087{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001088 view->geometry.x = x;
1089 view->geometry.y = y;
1090 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001091}
1092
Pekka Paalanen483243f2013-03-08 14:56:50 +02001093static void
1094transform_parent_handle_parent_destroy(struct wl_listener *listener,
1095 void *data)
1096{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097 struct weston_view *view =
1098 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001099 geometry.parent_destroy_listener);
1100
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001102}
1103
1104WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105weston_view_set_transform_parent(struct weston_view *view,
1106 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001107{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 if (view->geometry.parent) {
1109 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1110 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001111 }
1112
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001116 transform_parent_handle_parent_destroy;
1117 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001118 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001120 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001122 }
1123
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 weston_view_geometry_dirty(view);
1125}
1126
1127WL_EXPORT int
1128weston_view_is_mapped(struct weston_view *view)
1129{
1130 if (view->output)
1131 return 1;
1132 else
1133 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001134}
1135
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001136WL_EXPORT int
1137weston_surface_is_mapped(struct weston_surface *surface)
1138{
1139 if (surface->output)
1140 return 1;
1141 else
1142 return 0;
1143}
1144
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001145WL_EXPORT int32_t
1146weston_surface_buffer_width(struct weston_surface *surface)
1147{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001148 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001149 switch (surface->buffer_transform) {
1150 case WL_OUTPUT_TRANSFORM_90:
1151 case WL_OUTPUT_TRANSFORM_270:
1152 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1153 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001154 width = surface->buffer_ref.buffer->height;
1155 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001156 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001157 width = surface->buffer_ref.buffer->width;
1158 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001159 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001160 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001161}
1162
1163WL_EXPORT int32_t
1164weston_surface_buffer_height(struct weston_surface *surface)
1165{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001166 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001167 switch (surface->buffer_transform) {
1168 case WL_OUTPUT_TRANSFORM_90:
1169 case WL_OUTPUT_TRANSFORM_270:
1170 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1171 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001172 height = surface->buffer_ref.buffer->width;
1173 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001174 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001175 height = surface->buffer_ref.buffer->height;
1176 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001177 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001178 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001179}
1180
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001181WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001182weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001183{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001184 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001185
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001186 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001187
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001188 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001189}
1190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191WL_EXPORT struct weston_view *
1192weston_compositor_pick_view(struct weston_compositor *compositor,
1193 wl_fixed_t x, wl_fixed_t y,
1194 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001197
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 wl_list_for_each(view, &compositor->view_list, link) {
1199 weston_view_from_global_fixed(view, x, y, vx, vy);
1200 if (pixman_region32_contains_point(&view->surface->input,
1201 wl_fixed_to_int(*vx),
1202 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001203 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001205 }
1206
1207 return NULL;
1208}
1209
1210static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001212{
Daniel Stone37816df2012-05-16 18:45:18 +01001213 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001214
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001215 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001216 return;
1217
Daniel Stone37816df2012-05-16 18:45:18 +01001218 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001219 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001220}
1221
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001222WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001224{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001225 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 if (!weston_view_is_mapped(view))
1228 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001229
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 weston_view_damage_below(view);
1231 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001232 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 wl_list_remove(&view->layer_link);
1234 wl_list_init(&view->layer_link);
1235 wl_list_remove(&view->link);
1236 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237 view->output_mask = 0;
1238 weston_surface_assign_output(view->surface);
1239
1240 if (weston_surface_is_mapped(view->surface))
1241 return;
1242
1243 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1244 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001245 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001247 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001248 NULL,
1249 wl_fixed_from_int(0),
1250 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001252 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001253 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256WL_EXPORT void
1257weston_surface_unmap(struct weston_surface *surface)
1258{
1259 struct weston_view *view;
1260
1261 wl_list_for_each(view, &surface->views, surface_link)
1262 weston_view_unmap(view);
1263 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001264}
1265
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001266struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001267 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001268 struct wl_list link;
1269};
1270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271WL_EXPORT void
1272weston_view_destroy(struct weston_view *view)
1273{
1274 wl_signal_emit(&view->destroy_signal, view);
1275
1276 assert(wl_list_empty(&view->geometry.child_list));
1277
1278 if (weston_view_is_mapped(view)) {
1279 weston_view_unmap(view);
1280 weston_compositor_build_view_list(view->surface->compositor);
1281 }
1282
1283 wl_list_remove(&view->link);
1284 wl_list_remove(&view->layer_link);
1285
1286 pixman_region32_fini(&view->clip);
1287 pixman_region32_fini(&view->transform.boundingbox);
1288
1289 weston_view_set_transform_parent(view, NULL);
1290
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 wl_list_remove(&view->surface_link);
1292
1293 free(view);
1294}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001295
1296WL_EXPORT void
1297weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001298{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001299 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001301
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001302 if (--surface->ref_count > 0)
1303 return;
1304
1305 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1306
Pekka Paalanene67858b2013-04-25 13:57:42 +03001307 assert(wl_list_empty(&surface->subsurface_list_pending));
1308 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1311 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001312
Pekka Paalanenbc106382012-10-10 12:49:31 +03001313 wl_list_for_each_safe(cb, next,
1314 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001315 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001316
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001317 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001318 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001319 pixman_region32_fini(&surface->pending.damage);
1320
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001321 if (surface->pending.buffer)
1322 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1323
Pekka Paalanende685b82012-12-04 15:58:12 +02001324 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001325
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001326 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001327 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001328 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001329
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001330 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001331 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001332
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001333 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001334}
1335
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001336static void
1337destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001338{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001339 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001340
Giulio Camuffo0d379742013-11-15 22:06:15 +01001341 /* Set the resource to NULL, since we don't want to leave a
1342 * dangling pointer if the surface was refcounted and survives
1343 * the weston_surface_destroy() call. */
1344 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001345 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001346}
1347
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001348static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001349weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1350{
1351 struct weston_buffer *buffer =
1352 container_of(listener, struct weston_buffer, destroy_listener);
1353
1354 wl_signal_emit(&buffer->destroy_signal, buffer);
1355 free(buffer);
1356}
1357
1358struct weston_buffer *
1359weston_buffer_from_resource(struct wl_resource *resource)
1360{
1361 struct weston_buffer *buffer;
1362 struct wl_listener *listener;
1363
1364 listener = wl_resource_get_destroy_listener(resource,
1365 weston_buffer_destroy_handler);
1366
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001367 if (listener)
1368 return container_of(listener, struct weston_buffer,
1369 destroy_listener);
1370
1371 buffer = zalloc(sizeof *buffer);
1372 if (buffer == NULL)
1373 return NULL;
1374
1375 buffer->resource = resource;
1376 wl_signal_init(&buffer->destroy_signal);
1377 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001378 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001379 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001380
1381 return buffer;
1382}
1383
1384static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001385weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1386 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001387{
Pekka Paalanende685b82012-12-04 15:58:12 +02001388 struct weston_buffer_reference *ref =
1389 container_of(listener, struct weston_buffer_reference,
1390 destroy_listener);
1391
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001392 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001393 ref->buffer = NULL;
1394}
1395
1396WL_EXPORT void
1397weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001398 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001399{
1400 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001401 ref->buffer->busy_count--;
1402 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001403 assert(wl_resource_get_client(ref->buffer->resource));
1404 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001405 WL_BUFFER_RELEASE);
1406 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001407 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001408 }
1409
Pekka Paalanende685b82012-12-04 15:58:12 +02001410 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001411 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001412 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001413 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001414 }
1415
Pekka Paalanende685b82012-12-04 15:58:12 +02001416 ref->buffer = buffer;
1417 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1418}
1419
1420static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001421weston_surface_attach(struct weston_surface *surface,
1422 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001423{
1424 weston_buffer_reference(&surface->buffer_ref, buffer);
1425
Pekka Paalanena6421c42012-12-04 15:58:10 +02001426 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001427 if (weston_surface_is_mapped(surface))
1428 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001429 }
1430
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001431 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001432}
1433
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001434WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001436{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001437 wl_list_remove(&view->layer_link);
1438 wl_list_insert(below, &view->layer_link);
1439 weston_view_damage_below(view);
1440 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001441}
1442
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001443WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001445{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001446 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001447
1448 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001450}
1451
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001452WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001453weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001455 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001456
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001457 pixman_region32_union(&compositor->primary_plane.damage,
1458 &compositor->primary_plane.damage,
1459 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001460 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001461}
1462
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001463static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001465{
Pekka Paalanende685b82012-12-04 15:58:12 +02001466 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001467 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001468 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001469
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 empty_region(&surface->damage);
1471}
1472
1473static void
1474view_accumulate_damage(struct weston_view *view,
1475 pixman_region32_t *opaque)
1476{
1477 pixman_region32_t damage;
1478
1479 pixman_region32_init(&damage);
1480 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001481 pixman_box32_t *extents;
1482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 extents = pixman_region32_extents(&view->surface->damage);
1484 view_compute_bbox(view, extents->x1, extents->y1,
1485 extents->x2 - extents->x1,
1486 extents->y2 - extents->y1,
1487 &damage);
1488 pixman_region32_translate(&damage,
1489 -view->plane->x,
1490 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001491 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 pixman_region32_copy(&damage, &view->surface->damage);
1493 pixman_region32_translate(&damage,
1494 view->geometry.x - view->plane->x,
1495 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001496 }
1497
Jason Ekstranda7af7042013-10-12 22:38:11 -05001498 pixman_region32_subtract(&damage, &damage, opaque);
1499 pixman_region32_union(&view->plane->damage,
1500 &view->plane->damage, &damage);
1501 pixman_region32_fini(&damage);
1502 pixman_region32_copy(&view->clip, opaque);
1503 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001504}
1505
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001506static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001507compositor_accumulate_damage(struct weston_compositor *ec)
1508{
1509 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001510 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001511 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001512
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001513 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001514
1515 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001516 pixman_region32_copy(&plane->clip, &clip);
1517
1518 pixman_region32_init(&opaque);
1519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 wl_list_for_each(ev, &ec->view_list, link) {
1521 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001522 continue;
1523
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001525 }
1526
1527 pixman_region32_union(&clip, &clip, &opaque);
1528 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001529 }
1530
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001531 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001532
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 wl_list_for_each(ev, &ec->view_list, link)
1534 ev->surface->touched = 0;
1535
1536 wl_list_for_each(ev, &ec->view_list, link) {
1537 if (ev->surface->touched)
1538 continue;
1539 ev->surface->touched = 1;
1540
1541 surface_flush_damage(ev->surface);
1542
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001543 /* Both the renderer and the backend have seen the buffer
1544 * by now. If renderer needs the buffer, it has its own
1545 * reference set. If the backend wants to keep the buffer
1546 * around for migrating the surface into a non-primary plane
1547 * later, keep_buffer is true. Otherwise, drop the core
1548 * reference now, and allow early buffer release. This enables
1549 * clients to use single-buffering.
1550 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 if (!ev->surface->keep_buffer)
1552 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001553 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001554}
1555
1556static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001558{
1559 struct weston_subsurface *sub;
1560
Pekka Paalanene67858b2013-04-25 13:57:42 +03001561 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001563 continue;
1564
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1566 wl_list_init(&sub->surface->views);
1567
1568 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001569 }
1570}
1571
1572static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001574{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 struct weston_subsurface *sub;
1576 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001577
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1579 if (sub->surface == surface)
1580 continue;
1581
1582 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1583 weston_view_destroy(view);
1584
1585 surface_free_unused_subsurface_views(sub->surface);
1586 }
1587}
1588
1589static void
1590view_list_add_subsurface_view(struct weston_compositor *compositor,
1591 struct weston_subsurface *sub,
1592 struct weston_view *parent)
1593{
1594 struct weston_subsurface *child;
1595 struct weston_view *view = NULL, *iv;
1596
1597 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1598 if (iv->geometry.parent == parent) {
1599 view = iv;
1600 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001601 }
1602 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603
1604 if (view) {
1605 /* Put it back in the surface's list of views */
1606 wl_list_remove(&view->surface_link);
1607 wl_list_insert(&sub->surface->views, &view->surface_link);
1608 } else {
1609 view = weston_view_create(sub->surface);
1610 weston_view_configure(view,
1611 sub->position.x,
1612 sub->position.y,
1613 sub->surface->width,
1614 sub->surface->height);
1615 weston_view_set_transform_parent(view, parent);
1616 }
1617
1618 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619
Pekka Paalanenb188e912013-11-19 14:03:35 +02001620 if (wl_list_empty(&sub->surface->subsurface_list)) {
1621 wl_list_insert(compositor->view_list.prev, &view->link);
1622 return;
1623 }
1624
1625 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1626 if (child->surface == sub->surface)
1627 wl_list_insert(compositor->view_list.prev, &view->link);
1628 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001629 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001630 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631}
1632
1633static void
1634view_list_add(struct weston_compositor *compositor,
1635 struct weston_view *view)
1636{
1637 struct weston_subsurface *sub;
1638
1639 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640
Pekka Paalanenb188e912013-11-19 14:03:35 +02001641 if (wl_list_empty(&view->surface->subsurface_list)) {
1642 wl_list_insert(compositor->view_list.prev, &view->link);
1643 return;
1644 }
1645
1646 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1647 if (sub->surface == view->surface)
1648 wl_list_insert(compositor->view_list.prev, &view->link);
1649 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001651 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652}
1653
1654static void
1655weston_compositor_build_view_list(struct weston_compositor *compositor)
1656{
1657 struct weston_view *view;
1658 struct weston_layer *layer;
1659
1660 wl_list_for_each(layer, &compositor->layer_list, link)
1661 wl_list_for_each(view, &layer->view_list, layer_link)
1662 surface_stash_subsurface_views(view->surface);
1663
1664 wl_list_init(&compositor->view_list);
1665 wl_list_for_each(layer, &compositor->layer_list, link) {
1666 wl_list_for_each(view, &layer->view_list, layer_link) {
1667 view_list_add(compositor, view);
1668 }
1669 }
1670
1671 wl_list_for_each(layer, &compositor->layer_list, link)
1672 wl_list_for_each(view, &layer->view_list, layer_link)
1673 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001674}
1675
David Herrmann1edf44c2013-10-22 17:11:26 +02001676static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001677weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001678{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001679 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001681 struct weston_animation *animation, *next;
1682 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001683 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001684 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001685 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001686
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001687 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001689
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001690 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001691 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001692 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 wl_list_for_each(ev, &ec->view_list, link)
1694 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001695
Pekka Paalanene67858b2013-04-25 13:57:42 +03001696 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 wl_list_for_each(ev, &ec->view_list, link) {
1698 /* Note: This operation is safe to do multiple times on the
1699 * same surface.
1700 */
1701 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001702 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 &ev->surface->frame_callback_list);
1704 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001705 }
1706 }
1707
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001708 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001709
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001710 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001711 pixman_region32_intersect(&output_damage,
1712 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001713 pixman_region32_subtract(&output_damage,
1714 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001715
Scott Moreauccbf29d2012-02-22 14:21:41 -07001716 if (output->dirty)
1717 weston_output_update_matrix(output);
1718
David Herrmann1edf44c2013-10-22 17:11:26 +02001719 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001720
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001721 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001722
Kristian Høgsbergef044142011-06-21 15:02:12 -04001723 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001724
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001725 weston_compositor_repick(ec);
1726 wl_event_loop_dispatch(ec->input_loop, 0);
1727
Jonas Ådahldb773762012-06-13 00:01:21 +02001728 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001729 wl_callback_send_done(cb->resource, msecs);
1730 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001731 }
1732
Scott Moreaud64cf212012-06-08 19:40:54 -06001733 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001734 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001735 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001736 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001737
1738 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001739}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001740
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001741static int
1742weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001743{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001744 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001745
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001746 wl_event_loop_dispatch(compositor->input_loop, 0);
1747
1748 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001749}
1750
1751WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001752weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001753{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001754 struct weston_compositor *compositor = output->compositor;
1755 struct wl_event_loop *loop =
1756 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001757 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001758
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001759 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001760
1761 if (output->repaint_needed &&
1762 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1763 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001764 r = weston_output_repaint(output, msecs);
1765 if (!r)
1766 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001767 }
1768
1769 output->repaint_scheduled = 0;
1770 if (compositor->input_loop_source)
1771 return;
1772
1773 fd = wl_event_loop_get_fd(compositor->input_loop);
1774 compositor->input_loop_source =
1775 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1776 weston_compositor_read_input, compositor);
1777}
1778
1779static void
1780idle_repaint(void *data)
1781{
1782 struct weston_output *output = data;
1783
Jonas Ådahle5a12252013-04-05 23:07:11 +02001784 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001785}
1786
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001787WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001788weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1789{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001791 if (below != NULL)
1792 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001793}
1794
1795WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001796weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001797{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001798 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001799 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001800
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001801 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1802 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001803 return;
1804
Kristian Høgsbergef044142011-06-21 15:02:12 -04001805 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001806 output->repaint_needed = 1;
1807 if (output->repaint_scheduled)
1808 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001809
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001810 wl_event_loop_add_idle(loop, idle_repaint, output);
1811 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001812
1813 if (compositor->input_loop_source) {
1814 wl_event_source_remove(compositor->input_loop_source);
1815 compositor->input_loop_source = NULL;
1816 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001817}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001818
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001819WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001820weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1821{
1822 struct weston_output *output;
1823
1824 wl_list_for_each(output, &compositor->output_list, link)
1825 weston_output_schedule_repaint(output);
1826}
1827
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001828static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001829surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001830{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001831 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001832}
1833
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001834static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001835surface_attach(struct wl_client *client,
1836 struct wl_resource *resource,
1837 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1838{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001839 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001840 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001841
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001842 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001843 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001844 if (buffer == NULL) {
1845 wl_client_post_no_memory(client);
1846 return;
1847 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001848 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001849
Pekka Paalanende685b82012-12-04 15:58:12 +02001850 /* Attach, attach, without commit in between does not send
1851 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001852 if (surface->pending.buffer)
1853 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001854
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001855 surface->pending.sx = sx;
1856 surface->pending.sy = sy;
1857 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001858 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001859 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001860 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001861 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001862 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001863}
1864
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001865static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001866surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001867 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001868 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001869{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001870 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001871
Pekka Paalanen8e159182012-10-10 12:49:25 +03001872 pixman_region32_union_rect(&surface->pending.damage,
1873 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001874 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001875}
1876
Kristian Høgsberg33418202011-08-16 23:01:28 -04001877static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001878destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001879{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001880 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001881
1882 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001883 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001884}
1885
1886static void
1887surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001888 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001889{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001890 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001891 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001892
1893 cb = malloc(sizeof *cb);
1894 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001895 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001896 return;
1897 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001898
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001899 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1900 callback);
1901 if (cb->resource == NULL) {
1902 free(cb);
1903 wl_resource_post_no_memory(resource);
1904 return;
1905 }
1906
Jason Ekstranda85118c2013-06-27 20:17:02 -05001907 wl_resource_set_implementation(cb->resource, NULL, cb,
1908 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001909
Pekka Paalanenbc106382012-10-10 12:49:31 +03001910 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001911}
1912
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001913static void
1914surface_set_opaque_region(struct wl_client *client,
1915 struct wl_resource *resource,
1916 struct wl_resource *region_resource)
1917{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001918 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001919 struct weston_region *region;
1920
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001921 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001922 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001923 pixman_region32_copy(&surface->pending.opaque,
1924 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001925 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001926 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001927 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001928}
1929
1930static void
1931surface_set_input_region(struct wl_client *client,
1932 struct wl_resource *resource,
1933 struct wl_resource *region_resource)
1934{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001935 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001936 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001937
1938 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001939 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001940 pixman_region32_copy(&surface->pending.input,
1941 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001942 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001943 pixman_region32_fini(&surface->pending.input);
1944 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001945 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001946}
1947
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001948static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001949weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001950{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001951 struct weston_subsurface *sub;
1952
1953 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1954 parent_link_pending) {
1955 wl_list_remove(&sub->parent_link);
1956 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1957 }
1958}
1959
1960static void
1961weston_surface_commit(struct weston_surface *surface)
1962{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001963 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001964 pixman_region32_t opaque;
1965
Alexander Larsson4ea95522013-05-22 14:41:37 +02001966 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001967 surface->buffer_transform = surface->pending.buffer_transform;
1968
Alexander Larsson4ea95522013-05-22 14:41:37 +02001969 /* wl_surface.set_buffer_scale */
1970 surface->buffer_scale = surface->pending.buffer_scale;
1971
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001972 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001973 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001974 weston_surface_attach(surface, surface->pending.buffer);
1975
Jason Ekstranda7af7042013-10-12 22:38:11 -05001976 surface->width = 0;
1977 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001978 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979 /* This already includes the buffer scale */
1980 surface->width = weston_surface_buffer_width(surface);
1981 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001982 }
1983
1984 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001985 surface->configure(surface,
1986 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001987 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001988
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001989 if (surface->pending.buffer)
1990 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1991 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001992 surface->pending.sx = 0;
1993 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001994 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001995
1996 /* wl_surface.damage */
1997 pixman_region32_union(&surface->damage, &surface->damage,
1998 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001999 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2000 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002001 surface->width,
2002 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002003 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002004
2005 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002006 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 surface->width,
2008 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002009 pixman_region32_intersect(&opaque,
2010 &opaque, &surface->pending.opaque);
2011
2012 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2013 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002014 wl_list_for_each(view, &surface->views, surface_link)
2015 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002016 }
2017
2018 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002019
2020 /* wl_surface.set_input_region */
2021 pixman_region32_fini(&surface->input);
2022 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 surface->width,
2024 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002025 pixman_region32_intersect(&surface->input,
2026 &surface->input, &surface->pending.input);
2027
Pekka Paalanenbc106382012-10-10 12:49:31 +03002028 /* wl_surface.frame */
2029 wl_list_insert_list(&surface->frame_callback_list,
2030 &surface->pending.frame_callback_list);
2031 wl_list_init(&surface->pending.frame_callback_list);
2032
Pekka Paalanene67858b2013-04-25 13:57:42 +03002033 weston_surface_commit_subsurface_order(surface);
2034
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002035 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002036}
2037
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002038static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002039weston_subsurface_commit(struct weston_subsurface *sub);
2040
2041static void
2042weston_subsurface_parent_commit(struct weston_subsurface *sub,
2043 int parent_is_synchronized);
2044
2045static void
2046surface_commit(struct wl_client *client, struct wl_resource *resource)
2047{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002048 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002049 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2050
2051 if (sub) {
2052 weston_subsurface_commit(sub);
2053 return;
2054 }
2055
2056 weston_surface_commit(surface);
2057
2058 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2059 if (sub->surface != surface)
2060 weston_subsurface_parent_commit(sub, 0);
2061 }
2062}
2063
2064static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002065surface_set_buffer_transform(struct wl_client *client,
2066 struct wl_resource *resource, int transform)
2067{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002068 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002069
2070 surface->pending.buffer_transform = transform;
2071}
2072
Alexander Larsson4ea95522013-05-22 14:41:37 +02002073static void
2074surface_set_buffer_scale(struct wl_client *client,
2075 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002076 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002077{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002078 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002079
2080 surface->pending.buffer_scale = scale;
2081}
2082
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002083static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002084 surface_destroy,
2085 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002086 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002087 surface_frame,
2088 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002089 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002090 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002091 surface_set_buffer_transform,
2092 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002093};
2094
2095static void
2096compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002097 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002098{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002099 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002100 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002101
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002102 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002103 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002104 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002105 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002106 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002107
Jason Ekstranda85118c2013-06-27 20:17:02 -05002108 surface->resource =
2109 wl_resource_create(client, &wl_surface_interface,
2110 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002111 if (surface->resource == NULL) {
2112 weston_surface_destroy(surface);
2113 wl_resource_post_no_memory(resource);
2114 return;
2115 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002116 wl_resource_set_implementation(surface->resource, &surface_interface,
2117 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002118}
2119
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002120static void
2121destroy_region(struct wl_resource *resource)
2122{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002123 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002124
2125 pixman_region32_fini(&region->region);
2126 free(region);
2127}
2128
2129static void
2130region_destroy(struct wl_client *client, struct wl_resource *resource)
2131{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002132 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002133}
2134
2135static void
2136region_add(struct wl_client *client, struct wl_resource *resource,
2137 int32_t x, int32_t y, int32_t width, int32_t height)
2138{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002139 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002140
2141 pixman_region32_union_rect(&region->region, &region->region,
2142 x, y, width, height);
2143}
2144
2145static void
2146region_subtract(struct wl_client *client, struct wl_resource *resource,
2147 int32_t x, int32_t y, int32_t width, int32_t height)
2148{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002149 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002150 pixman_region32_t rect;
2151
2152 pixman_region32_init_rect(&rect, x, y, width, height);
2153 pixman_region32_subtract(&region->region, &region->region, &rect);
2154 pixman_region32_fini(&rect);
2155}
2156
2157static const struct wl_region_interface region_interface = {
2158 region_destroy,
2159 region_add,
2160 region_subtract
2161};
2162
2163static void
2164compositor_create_region(struct wl_client *client,
2165 struct wl_resource *resource, uint32_t id)
2166{
2167 struct weston_region *region;
2168
2169 region = malloc(sizeof *region);
2170 if (region == NULL) {
2171 wl_resource_post_no_memory(resource);
2172 return;
2173 }
2174
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002175 pixman_region32_init(&region->region);
2176
Jason Ekstranda85118c2013-06-27 20:17:02 -05002177 region->resource =
2178 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002179 if (region->resource == NULL) {
2180 free(region);
2181 wl_resource_post_no_memory(resource);
2182 return;
2183 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002184 wl_resource_set_implementation(region->resource, &region_interface,
2185 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002186}
2187
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002188static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002189 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002190 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002191};
2192
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002193static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002194weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2195{
2196 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002198 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002199
Alexander Larsson4ea95522013-05-22 14:41:37 +02002200 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002201 surface->buffer_transform = sub->cached.buffer_transform;
2202
Alexander Larsson4ea95522013-05-22 14:41:37 +02002203 /* wl_surface.set_buffer_scale */
2204 surface->buffer_scale = sub->cached.buffer_scale;
2205
Pekka Paalanene67858b2013-04-25 13:57:42 +03002206 /* wl_surface.attach */
2207 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2208 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2209 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2210
Jason Ekstranda7af7042013-10-12 22:38:11 -05002211 surface->width = 0;
2212 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002213 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 surface->width = weston_surface_buffer_width(surface);
2215 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002216 }
2217
2218 if (surface->configure && sub->cached.newly_attached)
2219 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002221 sub->cached.sx = 0;
2222 sub->cached.sy = 0;
2223 sub->cached.newly_attached = 0;
2224
2225 /* wl_surface.damage */
2226 pixman_region32_union(&surface->damage, &surface->damage,
2227 &sub->cached.damage);
2228 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2229 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 surface->width,
2231 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002232 empty_region(&sub->cached.damage);
2233
2234 /* wl_surface.set_opaque_region */
2235 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002236 surface->width,
2237 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002238 pixman_region32_intersect(&opaque,
2239 &opaque, &sub->cached.opaque);
2240
2241 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2242 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 wl_list_for_each(view, &surface->views, surface_link)
2244 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245 }
2246
2247 pixman_region32_fini(&opaque);
2248
2249 /* wl_surface.set_input_region */
2250 pixman_region32_fini(&surface->input);
2251 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002252 surface->width,
2253 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002254 pixman_region32_intersect(&surface->input,
2255 &surface->input, &sub->cached.input);
2256
2257 /* wl_surface.frame */
2258 wl_list_insert_list(&surface->frame_callback_list,
2259 &sub->cached.frame_callback_list);
2260 wl_list_init(&sub->cached.frame_callback_list);
2261
2262 weston_surface_commit_subsurface_order(surface);
2263
2264 weston_surface_schedule_repaint(surface);
2265
2266 sub->cached.has_data = 0;
2267}
2268
2269static void
2270weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2271{
2272 struct weston_surface *surface = sub->surface;
2273
2274 /*
2275 * If this commit would cause the surface to move by the
2276 * attach(dx, dy) parameters, the old damage region must be
2277 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002278 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279 */
2280 pixman_region32_translate(&sub->cached.damage,
2281 -surface->pending.sx, -surface->pending.sy);
2282 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2283 &surface->pending.damage);
2284 empty_region(&surface->pending.damage);
2285
2286 if (surface->pending.newly_attached) {
2287 sub->cached.newly_attached = 1;
2288 weston_buffer_reference(&sub->cached.buffer_ref,
2289 surface->pending.buffer);
2290 }
2291 sub->cached.sx += surface->pending.sx;
2292 sub->cached.sy += surface->pending.sy;
2293 surface->pending.sx = 0;
2294 surface->pending.sy = 0;
2295 surface->pending.newly_attached = 0;
2296
2297 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002298 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299
2300 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2301
2302 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2303
2304 wl_list_insert_list(&sub->cached.frame_callback_list,
2305 &surface->pending.frame_callback_list);
2306 wl_list_init(&surface->pending.frame_callback_list);
2307
2308 sub->cached.has_data = 1;
2309}
2310
2311static int
2312weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2313{
2314 while (sub) {
2315 if (sub->synchronized)
2316 return 1;
2317
2318 if (!sub->parent)
2319 return 0;
2320
2321 sub = weston_surface_to_subsurface(sub->parent);
2322 }
2323
2324 return 0;
2325}
2326
2327static void
2328weston_subsurface_commit(struct weston_subsurface *sub)
2329{
2330 struct weston_surface *surface = sub->surface;
2331 struct weston_subsurface *tmp;
2332
2333 /* Recursive check for effectively synchronized. */
2334 if (weston_subsurface_is_synchronized(sub)) {
2335 weston_subsurface_commit_to_cache(sub);
2336 } else {
2337 if (sub->cached.has_data) {
2338 /* flush accumulated state from cache */
2339 weston_subsurface_commit_to_cache(sub);
2340 weston_subsurface_commit_from_cache(sub);
2341 } else {
2342 weston_surface_commit(surface);
2343 }
2344
2345 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2346 if (tmp->surface != surface)
2347 weston_subsurface_parent_commit(tmp, 0);
2348 }
2349 }
2350}
2351
2352static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002353weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002354{
2355 struct weston_surface *surface = sub->surface;
2356 struct weston_subsurface *tmp;
2357
Pekka Paalanene67858b2013-04-25 13:57:42 +03002358 /* From now on, commit_from_cache the whole sub-tree, regardless of
2359 * the synchronized mode of each child. This sub-surface or some
2360 * of its ancestors were synchronized, so we are synchronized
2361 * all the way down.
2362 */
2363
2364 if (sub->cached.has_data)
2365 weston_subsurface_commit_from_cache(sub);
2366
2367 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2368 if (tmp->surface != surface)
2369 weston_subsurface_parent_commit(tmp, 1);
2370 }
2371}
2372
2373static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002374weston_subsurface_parent_commit(struct weston_subsurface *sub,
2375 int parent_is_synchronized)
2376{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002377 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002378 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002379 wl_list_for_each(view, &sub->surface->views, surface_link)
2380 weston_view_set_position(view,
2381 sub->position.x,
2382 sub->position.y);
2383
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002384 sub->position.set = 0;
2385 }
2386
2387 if (parent_is_synchronized || sub->synchronized)
2388 weston_subsurface_synchronized_commit(sub);
2389}
2390
2391static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002392subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2393 int32_t width, int32_t height)
2394{
2395 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002396 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002397
Jason Ekstranda7af7042013-10-12 22:38:11 -05002398 wl_list_for_each(view, &surface->views, surface_link)
2399 weston_view_configure(view,
2400 view->geometry.x + dx,
2401 view->geometry.y + dy,
2402 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002403
2404 /* No need to check parent mappedness, because if parent is not
2405 * mapped, parent is not in a visible layer, so this sub-surface
2406 * will not be drawn either.
2407 */
2408 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002409 /* Cannot call weston_surface_update_transform(),
2410 * because that would call it also for the parent surface,
2411 * which might not be mapped yet. That would lead to
2412 * inconsistent state, where the window could never be
2413 * mapped.
2414 *
2415 * Instead just assing any output, to make
2416 * weston_surface_is_mapped() return true, so that when the
2417 * parent surface does get mapped, this one will get
2418 * included, too. See surface_list_add().
2419 */
2420 assert(!wl_list_empty(&compositor->output_list));
2421 surface->output = container_of(compositor->output_list.next,
2422 struct weston_output, link);
2423 }
2424}
2425
2426static struct weston_subsurface *
2427weston_surface_to_subsurface(struct weston_surface *surface)
2428{
2429 if (surface->configure == subsurface_configure)
2430 return surface->configure_private;
2431
2432 return NULL;
2433}
2434
Pekka Paalanen01388e22013-04-25 13:57:44 +03002435WL_EXPORT struct weston_surface *
2436weston_surface_get_main_surface(struct weston_surface *surface)
2437{
2438 struct weston_subsurface *sub;
2439
2440 while (surface && (sub = weston_surface_to_subsurface(surface)))
2441 surface = sub->parent;
2442
2443 return surface;
2444}
2445
Pekka Paalanene67858b2013-04-25 13:57:42 +03002446static void
2447subsurface_set_position(struct wl_client *client,
2448 struct wl_resource *resource, int32_t x, int32_t y)
2449{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002450 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002451
2452 if (!sub)
2453 return;
2454
2455 sub->position.x = x;
2456 sub->position.y = y;
2457 sub->position.set = 1;
2458}
2459
2460static struct weston_subsurface *
2461subsurface_from_surface(struct weston_surface *surface)
2462{
2463 struct weston_subsurface *sub;
2464
2465 sub = weston_surface_to_subsurface(surface);
2466 if (sub)
2467 return sub;
2468
2469 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2470 if (sub->surface == surface)
2471 return sub;
2472
2473 return NULL;
2474}
2475
2476static struct weston_subsurface *
2477subsurface_sibling_check(struct weston_subsurface *sub,
2478 struct weston_surface *surface,
2479 const char *request)
2480{
2481 struct weston_subsurface *sibling;
2482
2483 sibling = subsurface_from_surface(surface);
2484
2485 if (!sibling) {
2486 wl_resource_post_error(sub->resource,
2487 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2488 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002489 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002490 return NULL;
2491 }
2492
2493 if (sibling->parent != sub->parent) {
2494 wl_resource_post_error(sub->resource,
2495 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2496 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002497 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002498 return NULL;
2499 }
2500
2501 return sibling;
2502}
2503
2504static void
2505subsurface_place_above(struct wl_client *client,
2506 struct wl_resource *resource,
2507 struct wl_resource *sibling_resource)
2508{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002509 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002510 struct weston_surface *surface =
2511 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512 struct weston_subsurface *sibling;
2513
2514 if (!sub)
2515 return;
2516
2517 sibling = subsurface_sibling_check(sub, surface, "place_above");
2518 if (!sibling)
2519 return;
2520
2521 wl_list_remove(&sub->parent_link_pending);
2522 wl_list_insert(sibling->parent_link_pending.prev,
2523 &sub->parent_link_pending);
2524}
2525
2526static void
2527subsurface_place_below(struct wl_client *client,
2528 struct wl_resource *resource,
2529 struct wl_resource *sibling_resource)
2530{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002531 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002532 struct weston_surface *surface =
2533 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002534 struct weston_subsurface *sibling;
2535
2536 if (!sub)
2537 return;
2538
2539 sibling = subsurface_sibling_check(sub, surface, "place_below");
2540 if (!sibling)
2541 return;
2542
2543 wl_list_remove(&sub->parent_link_pending);
2544 wl_list_insert(&sibling->parent_link_pending,
2545 &sub->parent_link_pending);
2546}
2547
2548static void
2549subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2550{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002551 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002552
2553 if (sub)
2554 sub->synchronized = 1;
2555}
2556
2557static void
2558subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2559{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002560 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002561
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002562 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002563 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002564
2565 /* If sub became effectively desynchronized, flush. */
2566 if (!weston_subsurface_is_synchronized(sub))
2567 weston_subsurface_synchronized_commit(sub);
2568 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569}
2570
2571static void
2572weston_subsurface_cache_init(struct weston_subsurface *sub)
2573{
2574 pixman_region32_init(&sub->cached.damage);
2575 pixman_region32_init(&sub->cached.opaque);
2576 pixman_region32_init(&sub->cached.input);
2577 wl_list_init(&sub->cached.frame_callback_list);
2578 sub->cached.buffer_ref.buffer = NULL;
2579}
2580
2581static void
2582weston_subsurface_cache_fini(struct weston_subsurface *sub)
2583{
2584 struct weston_frame_callback *cb, *tmp;
2585
2586 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002587 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002588
2589 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2590 pixman_region32_fini(&sub->cached.damage);
2591 pixman_region32_fini(&sub->cached.opaque);
2592 pixman_region32_fini(&sub->cached.input);
2593}
2594
2595static void
2596weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2597{
2598 wl_list_remove(&sub->parent_link);
2599 wl_list_remove(&sub->parent_link_pending);
2600 wl_list_remove(&sub->parent_destroy_listener.link);
2601 sub->parent = NULL;
2602}
2603
2604static void
2605weston_subsurface_destroy(struct weston_subsurface *sub);
2606
2607static void
2608subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2609{
2610 struct weston_subsurface *sub =
2611 container_of(listener, struct weston_subsurface,
2612 surface_destroy_listener);
2613 assert(data == &sub->surface->resource);
2614
2615 /* The protocol object (wl_resource) is left inert. */
2616 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002617 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618
2619 weston_subsurface_destroy(sub);
2620}
2621
2622static void
2623subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2624{
2625 struct weston_subsurface *sub =
2626 container_of(listener, struct weston_subsurface,
2627 parent_destroy_listener);
2628 assert(data == &sub->parent->resource);
2629 assert(sub->surface != sub->parent);
2630
2631 if (weston_surface_is_mapped(sub->surface))
2632 weston_surface_unmap(sub->surface);
2633
2634 weston_subsurface_unlink_parent(sub);
2635}
2636
2637static void
2638subsurface_resource_destroy(struct wl_resource *resource)
2639{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002640 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641
2642 if (sub)
2643 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002644}
2645
2646static void
2647subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2648{
2649 wl_resource_destroy(resource);
2650}
2651
2652static void
2653weston_subsurface_link_parent(struct weston_subsurface *sub,
2654 struct weston_surface *parent)
2655{
2656 sub->parent = parent;
2657 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002658 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002659 &sub->parent_destroy_listener);
2660
2661 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2662 wl_list_insert(&parent->subsurface_list_pending,
2663 &sub->parent_link_pending);
2664}
2665
2666static void
2667weston_subsurface_link_surface(struct weston_subsurface *sub,
2668 struct weston_surface *surface)
2669{
2670 sub->surface = surface;
2671 sub->surface_destroy_listener.notify =
2672 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002673 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674 &sub->surface_destroy_listener);
2675}
2676
2677static void
2678weston_subsurface_destroy(struct weston_subsurface *sub)
2679{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680 struct weston_view *view, *next;
2681
Pekka Paalanene67858b2013-04-25 13:57:42 +03002682 assert(sub->surface);
2683
2684 if (sub->resource) {
2685 assert(weston_surface_to_subsurface(sub->surface) == sub);
2686 assert(sub->parent_destroy_listener.notify ==
2687 subsurface_handle_parent_destroy);
2688
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2690 weston_view_destroy(view);
2691
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692 if (sub->parent)
2693 weston_subsurface_unlink_parent(sub);
2694
2695 weston_subsurface_cache_fini(sub);
2696
2697 sub->surface->configure = NULL;
2698 sub->surface->configure_private = NULL;
2699 } else {
2700 /* the dummy weston_subsurface for the parent itself */
2701 assert(sub->parent_destroy_listener.notify == NULL);
2702 wl_list_remove(&sub->parent_link);
2703 wl_list_remove(&sub->parent_link_pending);
2704 }
2705
2706 wl_list_remove(&sub->surface_destroy_listener.link);
2707 free(sub);
2708}
2709
2710static const struct wl_subsurface_interface subsurface_implementation = {
2711 subsurface_destroy,
2712 subsurface_set_position,
2713 subsurface_place_above,
2714 subsurface_place_below,
2715 subsurface_set_sync,
2716 subsurface_set_desync
2717};
2718
2719static struct weston_subsurface *
2720weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2721 struct weston_surface *parent)
2722{
2723 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002724 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725
2726 sub = calloc(1, sizeof *sub);
2727 if (!sub)
2728 return NULL;
2729
Jason Ekstranda7af7042013-10-12 22:38:11 -05002730 wl_list_init(&sub->unused_views);
2731
Jason Ekstranda85118c2013-06-27 20:17:02 -05002732 sub->resource =
2733 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002734 if (!sub->resource) {
2735 free(sub);
2736 return NULL;
2737 }
2738
Jason Ekstranda85118c2013-06-27 20:17:02 -05002739 wl_resource_set_implementation(sub->resource,
2740 &subsurface_implementation,
2741 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002742 weston_subsurface_link_surface(sub, surface);
2743 weston_subsurface_link_parent(sub, parent);
2744 weston_subsurface_cache_init(sub);
2745 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002746
2747 return sub;
2748}
2749
2750/* Create a dummy subsurface for having the parent itself in its
2751 * sub-surface lists. Makes stacking order manipulation easy.
2752 */
2753static struct weston_subsurface *
2754weston_subsurface_create_for_parent(struct weston_surface *parent)
2755{
2756 struct weston_subsurface *sub;
2757
2758 sub = calloc(1, sizeof *sub);
2759 if (!sub)
2760 return NULL;
2761
2762 weston_subsurface_link_surface(sub, parent);
2763 sub->parent = parent;
2764 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2765 wl_list_insert(&parent->subsurface_list_pending,
2766 &sub->parent_link_pending);
2767
2768 return sub;
2769}
2770
2771static void
2772subcompositor_get_subsurface(struct wl_client *client,
2773 struct wl_resource *resource,
2774 uint32_t id,
2775 struct wl_resource *surface_resource,
2776 struct wl_resource *parent_resource)
2777{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002778 struct weston_surface *surface =
2779 wl_resource_get_user_data(surface_resource);
2780 struct weston_surface *parent =
2781 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 struct weston_subsurface *sub;
2783 static const char where[] = "get_subsurface: wl_subsurface@";
2784
2785 if (surface == parent) {
2786 wl_resource_post_error(resource,
2787 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2788 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002789 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002790 return;
2791 }
2792
2793 if (weston_surface_to_subsurface(surface)) {
2794 wl_resource_post_error(resource,
2795 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2796 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002797 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002798 return;
2799 }
2800
2801 if (surface->configure) {
2802 wl_resource_post_error(resource,
2803 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2804 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002805 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002806 return;
2807 }
2808
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002809 if (weston_surface_get_main_surface(parent) == surface) {
2810 wl_resource_post_error(resource,
2811 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2812 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002813 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002814 return;
2815 }
2816
Pekka Paalanene67858b2013-04-25 13:57:42 +03002817 /* make sure the parent is in its own list */
2818 if (wl_list_empty(&parent->subsurface_list)) {
2819 if (!weston_subsurface_create_for_parent(parent)) {
2820 wl_resource_post_no_memory(resource);
2821 return;
2822 }
2823 }
2824
2825 sub = weston_subsurface_create(id, surface, parent);
2826 if (!sub) {
2827 wl_resource_post_no_memory(resource);
2828 return;
2829 }
2830
2831 surface->configure = subsurface_configure;
2832 surface->configure_private = sub;
2833}
2834
2835static void
2836subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2837{
2838 wl_resource_destroy(resource);
2839}
2840
2841static const struct wl_subcompositor_interface subcompositor_interface = {
2842 subcompositor_destroy,
2843 subcompositor_get_subsurface
2844};
2845
2846static void
2847bind_subcompositor(struct wl_client *client,
2848 void *data, uint32_t version, uint32_t id)
2849{
2850 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002851 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002852
Jason Ekstranda85118c2013-06-27 20:17:02 -05002853 resource =
2854 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002855 if (resource == NULL) {
2856 wl_client_post_no_memory(client);
2857 return;
2858 }
2859 wl_resource_set_implementation(resource, &subcompositor_interface,
2860 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861}
2862
2863static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002864weston_compositor_dpms(struct weston_compositor *compositor,
2865 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002866{
2867 struct weston_output *output;
2868
2869 wl_list_for_each(output, &compositor->output_list, link)
2870 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002871 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002872}
2873
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002874WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002875weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002876{
Neil Roberts8b62e202013-09-30 13:14:47 +01002877 uint32_t old_state = compositor->state;
2878
2879 /* The state needs to be changed before emitting the wake
2880 * signal because that may try to schedule a repaint which
2881 * will not work if the compositor is still sleeping */
2882 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2883
2884 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002885 case WESTON_COMPOSITOR_SLEEPING:
2886 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2887 /* fall through */
2888 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002889 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002890 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002891 /* fall through */
2892 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002893 wl_event_source_timer_update(compositor->idle_source,
2894 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002895 }
2896}
2897
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002898WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002899weston_compositor_offscreen(struct weston_compositor *compositor)
2900{
2901 switch (compositor->state) {
2902 case WESTON_COMPOSITOR_OFFSCREEN:
2903 return;
2904 case WESTON_COMPOSITOR_SLEEPING:
2905 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2906 /* fall through */
2907 default:
2908 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2909 wl_event_source_timer_update(compositor->idle_source, 0);
2910 }
2911}
2912
2913WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002914weston_compositor_sleep(struct weston_compositor *compositor)
2915{
2916 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2917 return;
2918
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002919 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002920 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2921 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2922}
2923
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002924static int
2925idle_handler(void *data)
2926{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002927 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002928
2929 if (compositor->idle_inhibit)
2930 return 1;
2931
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002932 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002933 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002934
2935 return 1;
2936}
2937
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002938WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002939weston_plane_init(struct weston_plane *plane,
2940 struct weston_compositor *ec,
2941 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002942{
2943 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002944 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002945 plane->x = x;
2946 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002947 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002948
2949 /* Init the link so that the call to wl_list_remove() when releasing
2950 * the plane without ever stacking doesn't lead to a crash */
2951 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002952}
2953
2954WL_EXPORT void
2955weston_plane_release(struct weston_plane *plane)
2956{
Xiong Zhang97116532013-10-23 13:58:31 +08002957 struct weston_view *view;
2958
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002959 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002960 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002961
Xiong Zhang97116532013-10-23 13:58:31 +08002962 wl_list_for_each(view, &plane->compositor->view_list, link) {
2963 if (view->plane == plane)
2964 view->plane = NULL;
2965 }
2966
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002967 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002968}
2969
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002970WL_EXPORT void
2971weston_compositor_stack_plane(struct weston_compositor *ec,
2972 struct weston_plane *plane,
2973 struct weston_plane *above)
2974{
2975 if (above)
2976 wl_list_insert(above->link.prev, &plane->link);
2977 else
2978 wl_list_insert(&ec->plane_list, &plane->link);
2979}
2980
Casey Dahlin9074db52012-04-19 22:50:09 -04002981static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002982{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002983 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002984}
2985
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002986static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002987bind_output(struct wl_client *client,
2988 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002989{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002990 struct weston_output *output = data;
2991 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002992 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002993
Jason Ekstranda85118c2013-06-27 20:17:02 -05002994 resource = wl_resource_create(client, &wl_output_interface,
2995 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002996 if (resource == NULL) {
2997 wl_client_post_no_memory(client);
2998 return;
2999 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003000
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003001 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003002 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003003
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003004 wl_output_send_geometry(resource,
3005 output->x,
3006 output->y,
3007 output->mm_width,
3008 output->mm_height,
3009 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003010 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003011 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003012 if (version >= 2)
3013 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003014 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003015
3016 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003017 wl_output_send_mode(resource,
3018 mode->flags,
3019 mode->width,
3020 mode->height,
3021 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003022 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003023
3024 if (version >= 2)
3025 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003026}
3027
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003028WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003029weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003030{
Richard Hughes64ddde12013-05-01 21:52:10 +01003031 wl_signal_emit(&output->destroy_signal, output);
3032
Richard Hughesafe690c2013-05-02 10:10:04 +01003033 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003034 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003035 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003036 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003037
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003038 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003039}
3040
Scott Moreau1bad5db2012-08-18 01:04:05 -06003041static void
3042weston_output_compute_transform(struct weston_output *output)
3043{
3044 struct weston_matrix transform;
3045 int flip;
3046
3047 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003048 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003049
3050 switch(output->transform) {
3051 case WL_OUTPUT_TRANSFORM_FLIPPED:
3052 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3053 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3054 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003055 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003056 flip = -1;
3057 break;
3058 default:
3059 flip = 1;
3060 break;
3061 }
3062
3063 switch(output->transform) {
3064 case WL_OUTPUT_TRANSFORM_NORMAL:
3065 case WL_OUTPUT_TRANSFORM_FLIPPED:
3066 transform.d[0] = flip;
3067 transform.d[1] = 0;
3068 transform.d[4] = 0;
3069 transform.d[5] = 1;
3070 break;
3071 case WL_OUTPUT_TRANSFORM_90:
3072 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3073 transform.d[0] = 0;
3074 transform.d[1] = -flip;
3075 transform.d[4] = 1;
3076 transform.d[5] = 0;
3077 break;
3078 case WL_OUTPUT_TRANSFORM_180:
3079 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3080 transform.d[0] = -flip;
3081 transform.d[1] = 0;
3082 transform.d[4] = 0;
3083 transform.d[5] = -1;
3084 break;
3085 case WL_OUTPUT_TRANSFORM_270:
3086 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3087 transform.d[0] = 0;
3088 transform.d[1] = flip;
3089 transform.d[4] = -1;
3090 transform.d[5] = 0;
3091 break;
3092 default:
3093 break;
3094 }
3095
3096 weston_matrix_multiply(&output->matrix, &transform);
3097}
3098
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003099WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003100weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003101{
Scott Moreau850ca422012-05-21 15:21:25 -06003102 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003103 struct weston_matrix camera;
3104 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003105
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003106 weston_matrix_init(&output->matrix);
3107 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003108 -(output->x + output->width / 2.0),
3109 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003110
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003111 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003112 2.0 / output->width,
3113 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003114
3115 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003116
Scott Moreauccbf29d2012-02-22 14:21:41 -07003117 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003118 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003119 weston_matrix_init(&camera);
3120 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003121 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003122 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003123 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003124 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003125 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003126 weston_matrix_multiply(&output->matrix, &modelview);
3127 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003128
Scott Moreauccbf29d2012-02-22 14:21:41 -07003129 output->dirty = 0;
3130}
3131
Scott Moreau1bad5db2012-08-18 01:04:05 -06003132static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003133weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003134{
3135 output->transform = transform;
3136
3137 switch (transform) {
3138 case WL_OUTPUT_TRANSFORM_90:
3139 case WL_OUTPUT_TRANSFORM_270:
3140 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3141 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3142 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003143 output->width = output->current_mode->height;
3144 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003145 break;
3146 case WL_OUTPUT_TRANSFORM_NORMAL:
3147 case WL_OUTPUT_TRANSFORM_180:
3148 case WL_OUTPUT_TRANSFORM_FLIPPED:
3149 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003150 output->width = output->current_mode->width;
3151 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003152 break;
3153 default:
3154 break;
3155 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003156
Hardening57388e42013-09-18 23:56:36 +02003157 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003158 output->width /= scale;
3159 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003160}
3161
Scott Moreauccbf29d2012-02-22 14:21:41 -07003162WL_EXPORT void
3163weston_output_move(struct weston_output *output, int x, int y)
3164{
3165 output->x = x;
3166 output->y = y;
3167
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003168 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003169 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003170 output->width,
3171 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003172}
3173
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003174WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003175weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003176 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003177 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003178{
3179 output->compositor = c;
3180 output->x = x;
3181 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003182 output->mm_width = mm_width;
3183 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003184 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003185 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003186
Alexander Larsson0b135062013-05-28 16:23:36 +02003187 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003188 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003189
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003190 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003191 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003192
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003193 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003194 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003195 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003196 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003197
Casey Dahlin58ba1372012-04-19 22:50:08 -04003198 output->id = ffs(~output->compositor->output_id_pool) - 1;
3199 output->compositor->output_id_pool |= 1 << output->id;
3200
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003201 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003202 wl_global_create(c->wl_display, &wl_output_interface, 2,
3203 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003204 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003205}
3206
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003207WL_EXPORT void
3208weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003209 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003210 wl_fixed_t *x, wl_fixed_t *y)
3211{
3212 wl_fixed_t tx, ty;
3213 wl_fixed_t width, height;
3214
Hardeningff39efa2013-09-18 23:56:35 +02003215 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3216 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003217
3218 switch(output->transform) {
3219 case WL_OUTPUT_TRANSFORM_NORMAL:
3220 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003221 tx = device_x;
3222 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003223 break;
3224 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003225 tx = device_y;
3226 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003227 break;
3228 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003229 tx = width - device_x;
3230 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003231 break;
3232 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003233 tx = width - device_y;
3234 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003235 break;
3236 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003237 tx = width - device_x;
3238 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003239 break;
3240 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003241 tx = width - device_y;
3242 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003243 break;
3244 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003245 tx = device_x;
3246 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003247 break;
3248 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003249 tx = device_y;
3250 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003251 break;
3252 }
3253
Hardeningff39efa2013-09-18 23:56:35 +02003254 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3255 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003256}
3257
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003258static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003259compositor_bind(struct wl_client *client,
3260 void *data, uint32_t version, uint32_t id)
3261{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003262 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003263 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003264
Jason Ekstranda85118c2013-06-27 20:17:02 -05003265 resource = wl_resource_create(client, &wl_compositor_interface,
3266 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003267 if (resource == NULL) {
3268 wl_client_post_no_memory(client);
3269 return;
3270 }
3271
3272 wl_resource_set_implementation(resource, &compositor_interface,
3273 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003274}
3275
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003276static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003277log_uname(void)
3278{
3279 struct utsname usys;
3280
3281 uname(&usys);
3282
3283 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3284 usys.version, usys.machine);
3285}
3286
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003287WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003288weston_environment_get_fd(const char *env)
3289{
3290 char *e, *end;
3291 int fd, flags;
3292
3293 e = getenv(env);
3294 if (!e)
3295 return -1;
3296 fd = strtol(e, &end, 0);
3297 if (*end != '\0')
3298 return -1;
3299
3300 flags = fcntl(fd, F_GETFD);
3301 if (flags == -1)
3302 return -1;
3303
3304 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3305 unsetenv(env);
3306
3307 return fd;
3308}
3309
3310WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003311weston_compositor_init(struct weston_compositor *ec,
3312 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003313 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003314 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003315{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003316 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003317 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003318 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003319
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003320 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003321 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003322 wl_signal_init(&ec->destroy_signal);
3323 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003324 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003325 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003326 wl_signal_init(&ec->idle_signal);
3327 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003328 wl_signal_init(&ec->show_input_panel_signal);
3329 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003330 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003331 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003332 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003333 wl_signal_init(&ec->session_signal);
3334 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003335
Casey Dahlin58ba1372012-04-19 22:50:08 -04003336 ec->output_id_pool = 0;
3337
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003338 if (!wl_global_create(display, &wl_compositor_interface, 3,
3339 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003340 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003341
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003342 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3343 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003344 return -1;
3345
Jason Ekstranda7af7042013-10-12 22:38:11 -05003346 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003347 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003348 wl_list_init(&ec->layer_list);
3349 wl_list_init(&ec->seat_list);
3350 wl_list_init(&ec->output_list);
3351 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003352 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003353 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003354 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003355 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003356 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003357
Xiong Zhang97116532013-10-23 13:58:31 +08003358 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003359 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003360
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003361 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3362 weston_config_section_get_string(s, "keymap_rules",
3363 (char **) &xkb_names.rules, NULL);
3364 weston_config_section_get_string(s, "keymap_model",
3365 (char **) &xkb_names.model, NULL);
3366 weston_config_section_get_string(s, "keymap_layout",
3367 (char **) &xkb_names.layout, NULL);
3368 weston_config_section_get_string(s, "keymap_variant",
3369 (char **) &xkb_names.variant, NULL);
3370 weston_config_section_get_string(s, "keymap_options",
3371 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003372
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003373 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3374 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003375
3376 ec->ping_handler = NULL;
3377
3378 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003379 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003380
3381 wl_data_device_manager_init(ec->wl_display);
3382
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003383 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003384
Daniel Stone725c2c32012-06-22 14:04:36 +01003385 loop = wl_display_get_event_loop(ec->wl_display);
3386 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3387 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3388
3389 ec->input_loop = wl_event_loop_create();
3390
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003391 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3392 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3393
3394 weston_compositor_schedule_repaint(ec);
3395
Daniel Stone725c2c32012-06-22 14:04:36 +01003396 return 0;
3397}
3398
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003399WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003400weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003401{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003402 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003403
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003404 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003405 if (ec->input_loop_source)
3406 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003407
Matt Roper361d2ad2011-08-29 13:52:23 -07003408 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003409 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003410 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003411
Daniel Stone325fc2d2012-05-30 16:31:58 +01003412 weston_binding_list_destroy_all(&ec->key_binding_list);
3413 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003414 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003415 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003416 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003417
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003418 weston_plane_release(&ec->primary_plane);
3419
Jonas Ådahlc97af922012-03-28 22:36:09 +02003420 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003421
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003422 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003423}
3424
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003425WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003426weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3427 const struct weston_pointer_grab_interface *interface)
3428{
3429 struct weston_seat *seat;
3430
3431 ec->default_pointer_grab = interface;
3432 wl_list_for_each(seat, &ec->seat_list, link) {
3433 if (seat->pointer) {
3434 weston_pointer_set_default_grab(seat->pointer,
3435 interface);
3436 }
3437 }
3438}
3439
3440WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003441weston_version(int *major, int *minor, int *micro)
3442{
3443 *major = WESTON_VERSION_MAJOR;
3444 *minor = WESTON_VERSION_MINOR;
3445 *micro = WESTON_VERSION_MICRO;
3446}
3447
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003448static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003449 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003450 const char *desc;
3451} capability_strings[] = {
3452 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003453 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003454};
3455
3456static void
3457weston_compositor_log_capabilities(struct weston_compositor *compositor)
3458{
3459 unsigned i;
3460 int yes;
3461
3462 weston_log("Compositor capabilities:\n");
3463 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3464 yes = compositor->capabilities & capability_strings[i].bit;
3465 weston_log_continue(STAMP_SPACE "%s %s\n",
3466 capability_strings[i].desc,
3467 yes ? "yes" : "no");
3468 }
3469}
3470
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003471static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003472{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003473 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003474
Martin Minarik6d118362012-06-07 18:01:59 +02003475 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003476 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003477
3478 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003479}
3480
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003481#ifdef HAVE_LIBUNWIND
3482
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003483static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003484print_backtrace(void)
3485{
3486 unw_cursor_t cursor;
3487 unw_context_t context;
3488 unw_word_t off;
3489 unw_proc_info_t pip;
3490 int ret, i = 0;
3491 char procname[256];
3492 const char *filename;
3493 Dl_info dlinfo;
3494
3495 pip.unwind_info = NULL;
3496 ret = unw_getcontext(&context);
3497 if (ret) {
3498 weston_log("unw_getcontext: %d\n", ret);
3499 return;
3500 }
3501
3502 ret = unw_init_local(&cursor, &context);
3503 if (ret) {
3504 weston_log("unw_init_local: %d\n", ret);
3505 return;
3506 }
3507
3508 ret = unw_step(&cursor);
3509 while (ret > 0) {
3510 ret = unw_get_proc_info(&cursor, &pip);
3511 if (ret) {
3512 weston_log("unw_get_proc_info: %d\n", ret);
3513 break;
3514 }
3515
3516 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3517 if (ret && ret != -UNW_ENOMEM) {
3518 if (ret != -UNW_EUNSPEC)
3519 weston_log("unw_get_proc_name: %d\n", ret);
3520 procname[0] = '?';
3521 procname[1] = 0;
3522 }
3523
3524 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3525 *dlinfo.dli_fname)
3526 filename = dlinfo.dli_fname;
3527 else
3528 filename = "?";
3529
3530 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3531 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3532
3533 ret = unw_step(&cursor);
3534 if (ret < 0)
3535 weston_log("unw_step: %d\n", ret);
3536 }
3537}
3538
3539#else
3540
3541static void
3542print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003543{
3544 void *buffer[32];
3545 int i, count;
3546 Dl_info info;
3547
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003548 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3549 for (i = 0; i < count; i++) {
3550 dladdr(buffer[i], &info);
3551 weston_log(" [%016lx] %s (%s)\n",
3552 (long) buffer[i],
3553 info.dli_sname ? info.dli_sname : "--",
3554 info.dli_fname);
3555 }
3556}
3557
3558#endif
3559
3560static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003561on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003562{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003563 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003564 * then call the backend restore function, which will switch
3565 * back to the vt we launched from or ungrab X etc and then
3566 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003567 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003568 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003569 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003570
Peter Maatmane5b42e42013-03-27 22:38:53 +01003571 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003572
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003573 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003574
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003575 segv_compositor->restore(segv_compositor);
3576
3577 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003578}
3579
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003580WL_EXPORT void *
3581weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003582{
3583 char path[PATH_MAX];
3584 void *module, *init;
3585
3586 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003587 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003588 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003589 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003590
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003591 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3592 if (module) {
3593 weston_log("Module '%s' already loaded\n", path);
3594 dlclose(module);
3595 return NULL;
3596 }
3597
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003598 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003599 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003600 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003601 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003602 return NULL;
3603 }
3604
3605 init = dlsym(module, entrypoint);
3606 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003607 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003608 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003609 return NULL;
3610 }
3611
3612 return init;
3613}
3614
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003615static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003616load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003617 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003618{
3619 const char *p, *end;
3620 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003621 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003622 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003623
3624 if (modules == NULL)
3625 return 0;
3626
3627 p = modules;
3628 while (*p) {
3629 end = strchrnul(p, ',');
3630 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003631 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003632 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003633 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003634 p = end;
3635 while (*p == ',')
3636 p++;
3637
3638 }
3639
3640 return 0;
3641}
3642
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003643static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003644 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3645
3646static const char xdg_wrong_message[] =
3647 "fatal: environment variable XDG_RUNTIME_DIR\n"
3648 "is set to \"%s\", which is not a directory.\n";
3649
3650static const char xdg_wrong_mode_message[] =
3651 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3652 "correctly. Unix access mode must be 0700 but is %o,\n"
3653 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003654 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003655
3656static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003657 "Refer to your distribution on how to get it, or\n"
3658 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3659 "on how to implement it.\n";
3660
Martin Minarik37032f82012-06-18 20:15:18 +02003661static void
3662verify_xdg_runtime_dir(void)
3663{
3664 char *dir = getenv("XDG_RUNTIME_DIR");
3665 struct stat s;
3666
3667 if (!dir) {
3668 weston_log(xdg_error_message);
3669 weston_log_continue(xdg_detail_message);
3670 exit(EXIT_FAILURE);
3671 }
3672
3673 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3674 weston_log(xdg_wrong_message, dir);
3675 weston_log_continue(xdg_detail_message);
3676 exit(EXIT_FAILURE);
3677 }
3678
3679 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3680 weston_log(xdg_wrong_mode_message,
3681 dir, s.st_mode & 0777, s.st_uid);
3682 weston_log_continue(xdg_detail_message);
3683 }
3684}
3685
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003686static int
3687usage(int error_code)
3688{
3689 fprintf(stderr,
3690 "Usage: weston [OPTIONS]\n\n"
3691 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3692 "Weston supports multiple backends, and depending on which backend is in use\n"
3693 "different options will be accepted.\n\n"
3694
3695
3696 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003697 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003698 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003699 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3700 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003701 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003702 " -S, --socket=NAME\tName of socket to listen on\n"
3703 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003704 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003705 " --log==FILE\t\tLog to the given file\n"
3706 " -h, --help\t\tThis help message\n\n");
3707
3708 fprintf(stderr,
3709 "Options for drm-backend.so:\n\n"
3710 " --connector=ID\tBring up only this connector\n"
3711 " --seat=SEAT\t\tThe seat that weston should run on\n"
3712 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003713 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003714 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3715
3716 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003717 "Options for fbdev-backend.so:\n\n"
3718 " --tty=TTY\t\tThe tty to use\n"
3719 " --device=DEVICE\tThe framebuffer device to use\n\n");
3720
3721 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003722 "Options for x11-backend.so:\n\n"
3723 " --width=WIDTH\t\tWidth of X window\n"
3724 " --height=HEIGHT\tHeight of X window\n"
3725 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003726 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003727 " --output-count=COUNT\tCreate multiple outputs\n"
3728 " --no-input\t\tDont create input devices\n\n");
3729
3730 fprintf(stderr,
3731 "Options for wayland-backend.so:\n\n"
3732 " --width=WIDTH\t\tWidth of Wayland surface\n"
3733 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003734 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003735 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003736 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003737 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003738 " --display=DISPLAY\tWayland display to connect to\n\n");
3739
Pekka Paalanene31e0532013-05-22 18:03:07 +03003740#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3741 fprintf(stderr,
3742 "Options for rpi-backend.so:\n\n"
3743 " --tty=TTY\t\tThe tty to use\n"
3744 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3745 " --transform=TR\tThe output transformation, TR is one of:\n"
3746 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3747 "\n");
3748#endif
3749
Hardeningc077a842013-07-08 00:51:35 +02003750#if defined(BUILD_RDP_COMPOSITOR)
3751 fprintf(stderr,
3752 "Options for rdp-backend.so:\n\n"
3753 " --width=WIDTH\t\tWidth of desktop\n"
3754 " --height=HEIGHT\tHeight of desktop\n"
3755 " --extra-modes=MODES\t\n"
3756 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3757 " --address=ADDR\tThe address to bind\n"
3758 " --port=PORT\tThe port to listen on\n"
3759 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3760 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3761 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3762 "\n");
3763#endif
3764
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003765 exit(error_code);
3766}
3767
Peter Maatmane5b42e42013-03-27 22:38:53 +01003768static void
3769catch_signals(void)
3770{
3771 struct sigaction action;
3772
3773 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3774 action.sa_sigaction = on_caught_signal;
3775 sigemptyset(&action.sa_mask);
3776 sigaction(SIGSEGV, &action, NULL);
3777 sigaction(SIGABRT, &action, NULL);
3778}
3779
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003780int main(int argc, char *argv[])
3781{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003782 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003783 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003784 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003785 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003786 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003787 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003788 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003789 int *argc, char *argv[],
3790 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003791 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003792 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003793 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003794 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003795 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003796 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003797 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003798 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003799 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003800 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003801 struct weston_config *config;
3802 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003803
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003804 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003805 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003806 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003807 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3808 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003809 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003810 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003811 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003812 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003813 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003814
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003815 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003816
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003817 if (help)
3818 usage(EXIT_SUCCESS);
3819
Scott Moreau12245142012-08-29 15:15:58 -06003820 if (version) {
3821 printf(PACKAGE_STRING "\n");
3822 return EXIT_SUCCESS;
3823 }
3824
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003825 weston_log_file_open(log);
3826
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003827 weston_log("%s\n"
3828 STAMP_SPACE "%s\n"
3829 STAMP_SPACE "Bug reports to: %s\n"
3830 STAMP_SPACE "Build: %s\n",
3831 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003832 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003833 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003834
Martin Minarik37032f82012-06-18 20:15:18 +02003835 verify_xdg_runtime_dir();
3836
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003837 display = wl_display_create();
3838
Tiago Vignatti2116b892011-08-08 05:52:59 -07003839 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003840 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3841 display);
3842 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3843 display);
3844 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3845 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003846
3847 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003848 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3849 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003850
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003851 config = weston_config_parse("weston.ini");
3852 if (config != NULL) {
3853 weston_log("Using config file '%s'\n",
3854 weston_config_get_full_path(config));
3855 } else {
3856 weston_log("Starting with no config file.\n");
3857 }
3858 section = weston_config_get_section(config, "core", NULL, NULL);
3859
3860 weston_config_section_get_string(section, "backend", &backend, NULL);
3861 if (option_backend) {
3862 backend = option_backend;
3863 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003864 if (!backend) {
3865 if (getenv("WAYLAND_DISPLAY"))
3866 backend = "wayland-backend.so";
3867 else if (getenv("DISPLAY"))
3868 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003869 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003870 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003871 }
3872
Jason Ekstranda985da42013-08-22 17:28:03 -05003873 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003874
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003875 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003876 if (!backend_init)
3877 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003878
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003879 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003880 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003881 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003882 exit(EXIT_FAILURE);
3883 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003884
Peter Maatmane5b42e42013-03-27 22:38:53 +01003885 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003886 segv_compositor = ec;
3887
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003888 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003889 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003890
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003891 setenv("WAYLAND_DISPLAY", socket_name, 1);
3892
Jason Ekstranda985da42013-08-22 17:28:03 -05003893 if (!shell)
3894 weston_config_section_get_string(section, "shell", &shell,
3895 "desktop-shell.so");
3896 if (load_modules(ec, shell, &argc, argv) < 0)
3897 goto out;
3898
Ossama Othmana50e6e42013-05-14 09:48:26 -07003899 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003900 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003901 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003902 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003903
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003904 for (i = 1; i < argc; i++)
3905 weston_log("fatal: unhandled option: %s\n", argv[i]);
3906 if (argc > 1) {
3907 ret = EXIT_FAILURE;
3908 goto out;
3909 }
3910
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003911 weston_compositor_log_capabilities(ec);
3912
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003913 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003914 weston_log("fatal: failed to add socket: %m\n");
3915 ret = EXIT_FAILURE;
3916 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003917 }
3918
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003919 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003920
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003921 wl_display_run(display);
3922
3923 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003924 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003925 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003926
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003927 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003928
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003929 for (i = ARRAY_LENGTH(signals); i;)
3930 wl_event_source_remove(signals[--i]);
3931
Daniel Stone855028f2012-05-01 20:37:10 +01003932 weston_compositor_xkb_destroy(ec);
3933
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003934 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003935 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003936
Martin Minarik19e6f262012-06-07 13:08:46 +02003937 weston_log_file_close();
3938
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003939 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003940}