blob: 134793c2ae1e86aaf24053f722714ba617d41564 [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"
U. Artie Eoffec08f332013-05-13 15:55:47 -070056#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
141 output->current_mode = mode;
142 output->current_scale = scale;
143 break;
144 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
145 if (!temporary_mode) {
146 weston_log("already in the native mode\n");
147 return -1;
148 }
149
150 notify_mode_changed = (output->original_mode != output->native_mode);
151
152 ret = output->switch_mode(output, mode);
153 if (ret < 0)
154 return ret;
155
156 if (output->original_scale != output->native_scale) {
157 notify_scale_changed = 1;
158 scale = output->native_scale;
159 output->original_scale = scale;
160 }
161 output->original_mode = 0;
162
163 output->current_scale = output->native_scale;
164 break;
165 default:
166 weston_log("unknown weston_switch_mode_op %d\n", op);
167 break;
168 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200169
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200170 pixman_region32_init(&old_output_region);
171 pixman_region32_copy(&old_output_region, &output->region);
172
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200173 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200174 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200175
176 pixman_region32_init(&output->previous_damage);
177 pixman_region32_init_rect(&output->region, output->x, output->y,
178 output->width, output->height);
179
180 weston_output_update_matrix(output);
181
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200182 /* If a pointer falls outside the outputs new geometry, move it to its
183 * lower-right corner */
184 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400185 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200186 int32_t x, y;
187
188 if (!pointer)
189 continue;
190
191 x = wl_fixed_to_int(pointer->x);
192 y = wl_fixed_to_int(pointer->y);
193
194 if (!pixman_region32_contains_point(&old_output_region,
195 x, y, NULL) ||
196 pixman_region32_contains_point(&output->region,
197 x, y, NULL))
198 continue;
199
200 if (x >= output->x + output->width)
201 x = output->x + output->width - 1;
202 if (y >= output->y + output->height)
203 y = output->y + output->height - 1;
204
205 pointer->x = wl_fixed_from_int(x);
206 pointer->y = wl_fixed_from_int(y);
207 }
208
209 pixman_region32_fini(&old_output_region);
210
Hardening57388e42013-09-18 23:56:36 +0200211 /* notify clients of the changes */
212 if (notify_mode_changed || notify_scale_changed) {
213 wl_resource_for_each(resource, &output->resource_list) {
214 if(notify_mode_changed) {
215 wl_output_send_mode(resource,
216 mode->flags | WL_OUTPUT_MODE_CURRENT,
217 mode->width,
218 mode->height,
219 mode->refresh);
220 }
221
222 if (notify_scale_changed)
223 wl_output_send_scale(resource, scale);
224
225 if (wl_resource_get_version(resource) >= 2)
226 wl_output_send_done(resource);
227 }
228 }
229
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200230 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800231}
232
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400233WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400235{
236 wl_list_insert(&child_process_list, &process->link);
237}
238
Benjamin Franzke06286262011-05-06 19:12:33 +0200239static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200240child_client_exec(int sockfd, const char *path)
241{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500242 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200243 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200244 sigset_t allsigs;
245
246 /* do not give our signal mask to the new process */
247 sigfillset(&allsigs);
248 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200249
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100250 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
251 if (seteuid(getuid()) == -1) {
252 weston_log("compositor: failed seteuid\n");
253 return;
254 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500255
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500256 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
257 * non-CLOEXEC fd to pass through exec. */
258 clientfd = dup(sockfd);
259 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500261 return;
262 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200263
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500264 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200265 setenv("WAYLAND_SOCKET", s, 1);
266
267 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200268 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200269 path);
270}
271
272WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273weston_client_launch(struct weston_compositor *compositor,
274 struct weston_process *proc,
275 const char *path,
276 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277{
278 int sv[2];
279 pid_t pid;
280 struct wl_client *client;
281
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300282 weston_log("launching '%s'\n", path);
283
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300284 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200286 "socketpair failed while launching '%s': %m\n",
287 path);
288 return NULL;
289 }
290
291 pid = fork();
292 if (pid == -1) {
293 close(sv[0]);
294 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296 "fork failed while launching '%s': %m\n", path);
297 return NULL;
298 }
299
300 if (pid == 0) {
301 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700302 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 }
304
305 close(sv[1]);
306
307 client = wl_client_create(compositor->wl_display, sv[0]);
308 if (!client) {
309 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200310 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311 "wl_client_create failed while launching '%s'.\n",
312 path);
313 return NULL;
314 }
315
316 proc->pid = pid;
317 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200319
320 return client;
321}
322
323static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300324surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
325{
326 struct weston_surface *surface =
327 container_of(listener, struct weston_surface,
328 pending.buffer_destroy_listener);
329
330 surface->pending.buffer = NULL;
331}
332
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200333static void
334empty_region(pixman_region32_t *region)
335{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300336 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200337 pixman_region32_init(region);
338}
339
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300340static void
341region_init_infinite(pixman_region32_t *region)
342{
343 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
344 UINT32_MAX, UINT32_MAX);
345}
346
Pekka Paalanene67858b2013-04-25 13:57:42 +0300347static struct weston_subsurface *
348weston_surface_to_subsurface(struct weston_surface *surface);
349
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350WL_EXPORT struct weston_view *
351weston_view_create(struct weston_surface *surface)
352{
353 struct weston_view *view;
354
355 view = calloc(1, sizeof *view);
356 if (view == NULL)
357 return NULL;
358
359 view->surface = surface;
360
Jason Ekstranda7af7042013-10-12 22:38:11 -0500361 /* Assign to surface */
362 wl_list_insert(&surface->views, &view->surface_link);
363
364 wl_signal_init(&view->destroy_signal);
365 wl_list_init(&view->link);
366 wl_list_init(&view->layer_link);
367
Xiong Zhang97116532013-10-23 13:58:31 +0800368 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500369
370 pixman_region32_init(&view->clip);
371
372 view->alpha = 1.0;
373 pixman_region32_init(&view->transform.opaque);
374
375 wl_list_init(&view->geometry.transformation_list);
376 wl_list_insert(&view->geometry.transformation_list,
377 &view->transform.position.link);
378 weston_matrix_init(&view->transform.position.matrix);
379 wl_list_init(&view->geometry.child_list);
380 pixman_region32_init(&view->transform.boundingbox);
381 view->transform.dirty = 1;
382
383 view->output = NULL;
384
385 return view;
386}
387
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500388WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500389weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500390{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500391 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400392
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200393 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400394 if (surface == NULL)
395 return NULL;
396
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500397 wl_signal_init(&surface->destroy_signal);
398
399 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500400
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500401 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200402 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400403
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200404 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200405 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200406 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200407 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400408 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100409 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200410
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400411 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500412 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300413 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400414
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415 wl_list_init(&surface->views);
416
417 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500418
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300419 surface->pending.buffer_destroy_listener.notify =
420 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300421 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300422 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300423 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300424 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300425
Pekka Paalanene67858b2013-04-25 13:57:42 +0300426 wl_list_init(&surface->subsurface_list);
427 wl_list_init(&surface->subsurface_list_pending);
428
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400429 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500430}
431
Alex Wu8811bf92012-02-28 18:07:54 +0800432WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500433weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200434 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500435{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100436 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500437}
438
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400439WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500440weston_view_to_global_float(struct weston_view *view,
441 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200442{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200444 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
445
Jason Ekstranda7af7042013-10-12 22:38:11 -0500446 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200447
448 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200449 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700450 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200451 v.f[3]);
452 *x = 0;
453 *y = 0;
454 return;
455 }
456
457 *x = v.f[0] / v.f[3];
458 *y = v.f[1] / v.f[3];
459 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500460 *x = sx + view->geometry.x;
461 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200462 }
463}
464
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500465WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200466weston_transformed_coord(int width, int height,
467 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200468 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200469 float sx, float sy, float *bx, float *by)
470{
471 switch (transform) {
472 case WL_OUTPUT_TRANSFORM_NORMAL:
473 default:
474 *bx = sx;
475 *by = sy;
476 break;
477 case WL_OUTPUT_TRANSFORM_FLIPPED:
478 *bx = width - sx;
479 *by = sy;
480 break;
481 case WL_OUTPUT_TRANSFORM_90:
482 *bx = height - sy;
483 *by = sx;
484 break;
485 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
486 *bx = height - sy;
487 *by = width - sx;
488 break;
489 case WL_OUTPUT_TRANSFORM_180:
490 *bx = width - sx;
491 *by = height - sy;
492 break;
493 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
494 *bx = sx;
495 *by = height - sy;
496 break;
497 case WL_OUTPUT_TRANSFORM_270:
498 *bx = sy;
499 *by = width - sx;
500 break;
501 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
502 *bx = sy;
503 *by = sx;
504 break;
505 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200506
507 *bx *= scale;
508 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200509}
510
511WL_EXPORT pixman_box32_t
512weston_transformed_rect(int width, int height,
513 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200514 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200515 pixman_box32_t rect)
516{
517 float x1, x2, y1, y2;
518
519 pixman_box32_t ret;
520
Alexander Larsson4ea95522013-05-22 14:41:37 +0200521 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200522 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200523 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200524 rect.x2, rect.y2, &x2, &y2);
525
526 if (x1 <= x2) {
527 ret.x1 = x1;
528 ret.x2 = x2;
529 } else {
530 ret.x1 = x2;
531 ret.x2 = x1;
532 }
533
534 if (y1 <= y2) {
535 ret.y1 = y1;
536 ret.y2 = y2;
537 } else {
538 ret.y1 = y2;
539 ret.y2 = y1;
540 }
541
542 return ret;
543}
544
545WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500546weston_transformed_region(int width, int height,
547 enum wl_output_transform transform,
548 int32_t scale,
549 pixman_region32_t *src, pixman_region32_t *dest)
550{
551 pixman_box32_t *src_rects, *dest_rects;
552 int nrects, i;
553
554 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
555 if (src != dest)
556 pixman_region32_copy(dest, src);
557 return;
558 }
559
560 src_rects = pixman_region32_rectangles(src, &nrects);
561 dest_rects = malloc(nrects * sizeof(*dest_rects));
562 if (!dest_rects)
563 return;
564
565 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
566 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
567 } else {
568 for (i = 0; i < nrects; i++) {
569 switch (transform) {
570 default:
571 case WL_OUTPUT_TRANSFORM_NORMAL:
572 dest_rects[i].x1 = src_rects[i].x1;
573 dest_rects[i].y1 = src_rects[i].y1;
574 dest_rects[i].x2 = src_rects[i].x2;
575 dest_rects[i].y2 = src_rects[i].y2;
576 break;
577 case WL_OUTPUT_TRANSFORM_90:
578 dest_rects[i].x1 = height - src_rects[i].y2;
579 dest_rects[i].y1 = src_rects[i].x1;
580 dest_rects[i].x2 = height - src_rects[i].y1;
581 dest_rects[i].y2 = src_rects[i].x2;
582 break;
583 case WL_OUTPUT_TRANSFORM_180:
584 dest_rects[i].x1 = width - src_rects[i].x2;
585 dest_rects[i].y1 = height - src_rects[i].y2;
586 dest_rects[i].x2 = width - src_rects[i].x1;
587 dest_rects[i].y2 = height - src_rects[i].y1;
588 break;
589 case WL_OUTPUT_TRANSFORM_270:
590 dest_rects[i].x1 = src_rects[i].y1;
591 dest_rects[i].y1 = width - src_rects[i].x2;
592 dest_rects[i].x2 = src_rects[i].y2;
593 dest_rects[i].y2 = width - src_rects[i].x1;
594 break;
595 case WL_OUTPUT_TRANSFORM_FLIPPED:
596 dest_rects[i].x1 = width - src_rects[i].x2;
597 dest_rects[i].y1 = src_rects[i].y1;
598 dest_rects[i].x2 = width - src_rects[i].x1;
599 dest_rects[i].y2 = src_rects[i].y2;
600 break;
601 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
602 dest_rects[i].x1 = height - src_rects[i].y2;
603 dest_rects[i].y1 = width - src_rects[i].x2;
604 dest_rects[i].x2 = height - src_rects[i].y1;
605 dest_rects[i].y2 = width - src_rects[i].x1;
606 break;
607 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
608 dest_rects[i].x1 = src_rects[i].x1;
609 dest_rects[i].y1 = height - src_rects[i].y2;
610 dest_rects[i].x2 = src_rects[i].x2;
611 dest_rects[i].y2 = height - src_rects[i].y1;
612 break;
613 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
614 dest_rects[i].x1 = src_rects[i].y1;
615 dest_rects[i].y1 = src_rects[i].x1;
616 dest_rects[i].x2 = src_rects[i].y2;
617 dest_rects[i].y2 = src_rects[i].x2;
618 break;
619 }
620 }
621 }
622
623 if (scale != 1) {
624 for (i = 0; i < nrects; i++) {
625 dest_rects[i].x1 *= scale;
626 dest_rects[i].x2 *= scale;
627 dest_rects[i].y1 *= scale;
628 dest_rects[i].y2 *= scale;
629 }
630 }
631
632 pixman_region32_clear(dest);
633 pixman_region32_init_rects(dest, dest_rects, nrects);
634 free(dest_rects);
635}
636
637WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200638weston_surface_to_buffer_float(struct weston_surface *surface,
639 float sx, float sy, float *bx, float *by)
640{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500641 weston_transformed_coord(surface->width,
642 surface->height,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200643 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200644 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200645 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200646}
647
Alexander Larsson4ea95522013-05-22 14:41:37 +0200648WL_EXPORT void
649weston_surface_to_buffer(struct weston_surface *surface,
650 int sx, int sy, int *bx, int *by)
651{
652 float bxf, byf;
653
Jason Ekstranda7af7042013-10-12 22:38:11 -0500654 weston_transformed_coord(surface->width,
655 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200656 surface->buffer_transform,
657 surface->buffer_scale,
658 sx, sy, &bxf, &byf);
659 *bx = floorf(bxf);
660 *by = floorf(byf);
661}
662
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200663WL_EXPORT pixman_box32_t
664weston_surface_to_buffer_rect(struct weston_surface *surface,
665 pixman_box32_t rect)
666{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500667 return weston_transformed_rect(surface->width,
668 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200669 surface->buffer_transform,
670 surface->buffer_scale,
671 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200672}
673
674WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500675weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400676 struct weston_plane *plane)
677{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500678 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400679 return;
680
Jason Ekstranda7af7042013-10-12 22:38:11 -0500681 weston_view_damage_below(view);
682 view->plane = plane;
683 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400684}
685
686WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500687weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200688{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500689 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200690
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500691 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500692 pixman_region32_subtract(&damage, &view->transform.boundingbox,
693 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800694 if (view->plane)
695 pixman_region32_union(&view->plane->damage,
696 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500697 pixman_region32_fini(&damage);
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);
1237 /* We need to do this before torching the output mask */
1238 weston_view_schedule_repaint(view);
1239 view->output_mask = 0;
1240 weston_surface_assign_output(view->surface);
1241
1242 if (weston_surface_is_mapped(view->surface))
1243 return;
1244
1245 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1246 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001247 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001248 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001249 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001250 NULL,
1251 wl_fixed_from_int(0),
1252 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001254 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001255 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001257
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258WL_EXPORT void
1259weston_surface_unmap(struct weston_surface *surface)
1260{
1261 struct weston_view *view;
1262
1263 wl_list_for_each(view, &surface->views, surface_link)
1264 weston_view_unmap(view);
1265 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001266}
1267
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001268struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001269 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001270 struct wl_list link;
1271};
1272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273WL_EXPORT void
1274weston_view_destroy(struct weston_view *view)
1275{
1276 wl_signal_emit(&view->destroy_signal, view);
1277
1278 assert(wl_list_empty(&view->geometry.child_list));
1279
1280 if (weston_view_is_mapped(view)) {
1281 weston_view_unmap(view);
1282 weston_compositor_build_view_list(view->surface->compositor);
1283 }
1284
1285 wl_list_remove(&view->link);
1286 wl_list_remove(&view->layer_link);
1287
1288 pixman_region32_fini(&view->clip);
1289 pixman_region32_fini(&view->transform.boundingbox);
1290
1291 weston_view_set_transform_parent(view, NULL);
1292
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 wl_list_remove(&view->surface_link);
1294
1295 free(view);
1296}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001297
1298WL_EXPORT void
1299weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001300{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001301 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001303
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001304 if (--surface->ref_count > 0)
1305 return;
1306
1307 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1308
Pekka Paalanene67858b2013-04-25 13:57:42 +03001309 assert(wl_list_empty(&surface->subsurface_list_pending));
1310 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1313 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001314
Pekka Paalanenbc106382012-10-10 12:49:31 +03001315 wl_list_for_each_safe(cb, next,
1316 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001317 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001318
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001319 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001320 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001321 pixman_region32_fini(&surface->pending.damage);
1322
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001323 if (surface->pending.buffer)
1324 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1325
Pekka Paalanende685b82012-12-04 15:58:12 +02001326 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001327
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001328 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001329 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001330 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001331
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001332 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001333 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001334
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001335 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001336}
1337
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001338static void
1339destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001340{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001341 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001342
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001343 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001344}
1345
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001346static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001347weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1348{
1349 struct weston_buffer *buffer =
1350 container_of(listener, struct weston_buffer, destroy_listener);
1351
1352 wl_signal_emit(&buffer->destroy_signal, buffer);
1353 free(buffer);
1354}
1355
1356struct weston_buffer *
1357weston_buffer_from_resource(struct wl_resource *resource)
1358{
1359 struct weston_buffer *buffer;
1360 struct wl_listener *listener;
1361
1362 listener = wl_resource_get_destroy_listener(resource,
1363 weston_buffer_destroy_handler);
1364
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001365 if (listener)
1366 return container_of(listener, struct weston_buffer,
1367 destroy_listener);
1368
1369 buffer = zalloc(sizeof *buffer);
1370 if (buffer == NULL)
1371 return NULL;
1372
1373 buffer->resource = resource;
1374 wl_signal_init(&buffer->destroy_signal);
1375 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001376 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001377 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001378
1379 return buffer;
1380}
1381
1382static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001383weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1384 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001385{
Pekka Paalanende685b82012-12-04 15:58:12 +02001386 struct weston_buffer_reference *ref =
1387 container_of(listener, struct weston_buffer_reference,
1388 destroy_listener);
1389
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001390 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001391 ref->buffer = NULL;
1392}
1393
1394WL_EXPORT void
1395weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001396 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001397{
1398 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001399 ref->buffer->busy_count--;
1400 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001401 assert(wl_resource_get_client(ref->buffer->resource));
1402 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001403 WL_BUFFER_RELEASE);
1404 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001405 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001406 }
1407
Pekka Paalanende685b82012-12-04 15:58:12 +02001408 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001409 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001410 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001411 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001412 }
1413
Pekka Paalanende685b82012-12-04 15:58:12 +02001414 ref->buffer = buffer;
1415 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1416}
1417
1418static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001419weston_surface_attach(struct weston_surface *surface,
1420 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001421{
1422 weston_buffer_reference(&surface->buffer_ref, buffer);
1423
Pekka Paalanena6421c42012-12-04 15:58:10 +02001424 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001425 if (weston_surface_is_mapped(surface))
1426 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001427 }
1428
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001429 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001430}
1431
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001432WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001433weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001434{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001435 wl_list_remove(&view->layer_link);
1436 wl_list_insert(below, &view->layer_link);
1437 weston_view_damage_below(view);
1438 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001439}
1440
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001441WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001442weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001443{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001444 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001445
1446 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001447 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001448}
1449
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001450WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001451weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001452{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001453 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001454
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001455 pixman_region32_union(&compositor->primary_plane.damage,
1456 &compositor->primary_plane.damage,
1457 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001458 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001459}
1460
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001461static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001463{
Pekka Paalanende685b82012-12-04 15:58:12 +02001464 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001465 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001466 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001467
Jason Ekstranda7af7042013-10-12 22:38:11 -05001468 empty_region(&surface->damage);
1469}
1470
1471static void
1472view_accumulate_damage(struct weston_view *view,
1473 pixman_region32_t *opaque)
1474{
1475 pixman_region32_t damage;
1476
1477 pixman_region32_init(&damage);
1478 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001479 pixman_box32_t *extents;
1480
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 extents = pixman_region32_extents(&view->surface->damage);
1482 view_compute_bbox(view, extents->x1, extents->y1,
1483 extents->x2 - extents->x1,
1484 extents->y2 - extents->y1,
1485 &damage);
1486 pixman_region32_translate(&damage,
1487 -view->plane->x,
1488 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001489 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001490 pixman_region32_copy(&damage, &view->surface->damage);
1491 pixman_region32_translate(&damage,
1492 view->geometry.x - view->plane->x,
1493 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001494 }
1495
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 pixman_region32_subtract(&damage, &damage, opaque);
1497 pixman_region32_union(&view->plane->damage,
1498 &view->plane->damage, &damage);
1499 pixman_region32_fini(&damage);
1500 pixman_region32_copy(&view->clip, opaque);
1501 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001502}
1503
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001504static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001505compositor_accumulate_damage(struct weston_compositor *ec)
1506{
1507 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001508 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001509 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001510
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001511 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001512
1513 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001514 pixman_region32_copy(&plane->clip, &clip);
1515
1516 pixman_region32_init(&opaque);
1517
Jason Ekstranda7af7042013-10-12 22:38:11 -05001518 wl_list_for_each(ev, &ec->view_list, link) {
1519 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001520 continue;
1521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001523 }
1524
1525 pixman_region32_union(&clip, &clip, &opaque);
1526 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001527 }
1528
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001529 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001530
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 wl_list_for_each(ev, &ec->view_list, link)
1532 ev->surface->touched = 0;
1533
1534 wl_list_for_each(ev, &ec->view_list, link) {
1535 if (ev->surface->touched)
1536 continue;
1537 ev->surface->touched = 1;
1538
1539 surface_flush_damage(ev->surface);
1540
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001541 /* Both the renderer and the backend have seen the buffer
1542 * by now. If renderer needs the buffer, it has its own
1543 * reference set. If the backend wants to keep the buffer
1544 * around for migrating the surface into a non-primary plane
1545 * later, keep_buffer is true. Otherwise, drop the core
1546 * reference now, and allow early buffer release. This enables
1547 * clients to use single-buffering.
1548 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001549 if (!ev->surface->keep_buffer)
1550 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001551 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001552}
1553
1554static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001556{
1557 struct weston_subsurface *sub;
1558
Pekka Paalanene67858b2013-04-25 13:57:42 +03001559 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001561 continue;
1562
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1564 wl_list_init(&sub->surface->views);
1565
1566 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001567 }
1568}
1569
1570static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001572{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 struct weston_subsurface *sub;
1574 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001575
Jason Ekstranda7af7042013-10-12 22:38:11 -05001576 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1577 if (sub->surface == surface)
1578 continue;
1579
1580 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1581 weston_view_destroy(view);
1582
1583 surface_free_unused_subsurface_views(sub->surface);
1584 }
1585}
1586
1587static void
1588view_list_add_subsurface_view(struct weston_compositor *compositor,
1589 struct weston_subsurface *sub,
1590 struct weston_view *parent)
1591{
1592 struct weston_subsurface *child;
1593 struct weston_view *view = NULL, *iv;
1594
1595 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1596 if (iv->geometry.parent == parent) {
1597 view = iv;
1598 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001599 }
1600 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601
1602 if (view) {
1603 /* Put it back in the surface's list of views */
1604 wl_list_remove(&view->surface_link);
1605 wl_list_insert(&sub->surface->views, &view->surface_link);
1606 } else {
1607 view = weston_view_create(sub->surface);
1608 weston_view_configure(view,
1609 sub->position.x,
1610 sub->position.y,
1611 sub->surface->width,
1612 sub->surface->height);
1613 weston_view_set_transform_parent(view, parent);
1614 }
1615
1616 weston_view_update_transform(view);
1617 wl_list_insert(compositor->view_list.next, &view->link);
1618
1619 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
1620 if (child->surface != sub->surface)
1621 view_list_add_subsurface_view(compositor, child, view);
1622}
1623
1624static void
1625view_list_add(struct weston_compositor *compositor,
1626 struct weston_view *view)
1627{
1628 struct weston_subsurface *sub;
1629
1630 weston_view_update_transform(view);
1631 wl_list_insert(compositor->view_list.prev, &view->link);
1632
1633 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link)
1634 if (sub->surface != view->surface)
1635 view_list_add_subsurface_view(compositor, sub, view);
1636}
1637
1638static void
1639weston_compositor_build_view_list(struct weston_compositor *compositor)
1640{
1641 struct weston_view *view;
1642 struct weston_layer *layer;
1643
1644 wl_list_for_each(layer, &compositor->layer_list, link)
1645 wl_list_for_each(view, &layer->view_list, layer_link)
1646 surface_stash_subsurface_views(view->surface);
1647
1648 wl_list_init(&compositor->view_list);
1649 wl_list_for_each(layer, &compositor->layer_list, link) {
1650 wl_list_for_each(view, &layer->view_list, layer_link) {
1651 view_list_add(compositor, view);
1652 }
1653 }
1654
1655 wl_list_for_each(layer, &compositor->layer_list, link)
1656 wl_list_for_each(view, &layer->view_list, layer_link)
1657 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001658}
1659
David Herrmann1edf44c2013-10-22 17:11:26 +02001660static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001661weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001662{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001663 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001665 struct weston_animation *animation, *next;
1666 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001667 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001668 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001669 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001670
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001671 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001673
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001674 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001675 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001676 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001677 wl_list_for_each(ev, &ec->view_list, link)
1678 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001679
Pekka Paalanene67858b2013-04-25 13:57:42 +03001680 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681 wl_list_for_each(ev, &ec->view_list, link) {
1682 /* Note: This operation is safe to do multiple times on the
1683 * same surface.
1684 */
1685 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001686 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 &ev->surface->frame_callback_list);
1688 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001689 }
1690 }
1691
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001692 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001693
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001694 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001695 pixman_region32_intersect(&output_damage,
1696 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001697 pixman_region32_subtract(&output_damage,
1698 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001699
Scott Moreauccbf29d2012-02-22 14:21:41 -07001700 if (output->dirty)
1701 weston_output_update_matrix(output);
1702
David Herrmann1edf44c2013-10-22 17:11:26 +02001703 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001704
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001705 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001706
Kristian Høgsbergef044142011-06-21 15:02:12 -04001707 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001708
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001709 weston_compositor_repick(ec);
1710 wl_event_loop_dispatch(ec->input_loop, 0);
1711
Jonas Ådahldb773762012-06-13 00:01:21 +02001712 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001713 wl_callback_send_done(cb->resource, msecs);
1714 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001715 }
1716
Scott Moreaud64cf212012-06-08 19:40:54 -06001717 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001718 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001719 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001720 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001721
1722 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001723}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001724
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001725static int
1726weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001727{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001728 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001729
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001730 wl_event_loop_dispatch(compositor->input_loop, 0);
1731
1732 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001733}
1734
1735WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001736weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001737{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001738 struct weston_compositor *compositor = output->compositor;
1739 struct wl_event_loop *loop =
1740 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001741 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001742
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001743 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001744
1745 if (output->repaint_needed &&
1746 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1747 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001748 r = weston_output_repaint(output, msecs);
1749 if (!r)
1750 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001751 }
1752
1753 output->repaint_scheduled = 0;
1754 if (compositor->input_loop_source)
1755 return;
1756
1757 fd = wl_event_loop_get_fd(compositor->input_loop);
1758 compositor->input_loop_source =
1759 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1760 weston_compositor_read_input, compositor);
1761}
1762
1763static void
1764idle_repaint(void *data)
1765{
1766 struct weston_output *output = data;
1767
Jonas Ådahle5a12252013-04-05 23:07:11 +02001768 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001769}
1770
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001771WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001772weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1773{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001775 if (below != NULL)
1776 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001777}
1778
1779WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001780weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001781{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001782 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001783 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001784
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001785 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1786 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001787 return;
1788
Kristian Høgsbergef044142011-06-21 15:02:12 -04001789 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001790 output->repaint_needed = 1;
1791 if (output->repaint_scheduled)
1792 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001793
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001794 wl_event_loop_add_idle(loop, idle_repaint, output);
1795 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001796
1797 if (compositor->input_loop_source) {
1798 wl_event_source_remove(compositor->input_loop_source);
1799 compositor->input_loop_source = NULL;
1800 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001801}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001802
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001803WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001804weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1805{
1806 struct weston_output *output;
1807
1808 wl_list_for_each(output, &compositor->output_list, link)
1809 weston_output_schedule_repaint(output);
1810}
1811
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001812static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001813surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001814{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001815 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001816}
1817
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001818static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001819surface_attach(struct wl_client *client,
1820 struct wl_resource *resource,
1821 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1822{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001823 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001824 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001825
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001826 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001827 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001828 if (buffer == NULL) {
1829 wl_client_post_no_memory(client);
1830 return;
1831 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001832 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001833
Pekka Paalanende685b82012-12-04 15:58:12 +02001834 /* Attach, attach, without commit in between does not send
1835 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001836 if (surface->pending.buffer)
1837 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001838
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001839 surface->pending.sx = sx;
1840 surface->pending.sy = sy;
1841 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001842 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001843 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001844 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001845 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001846 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001847}
1848
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001849static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001850surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001851 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001852 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001853{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001854 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001855
Pekka Paalanen8e159182012-10-10 12:49:25 +03001856 pixman_region32_union_rect(&surface->pending.damage,
1857 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001858 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001859}
1860
Kristian Høgsberg33418202011-08-16 23:01:28 -04001861static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001862destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001863{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001864 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001865
1866 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001867 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001868}
1869
1870static void
1871surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001872 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001873{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001874 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001875 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001876
1877 cb = malloc(sizeof *cb);
1878 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001879 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001880 return;
1881 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001882
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001883 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1884 callback);
1885 if (cb->resource == NULL) {
1886 free(cb);
1887 wl_resource_post_no_memory(resource);
1888 return;
1889 }
1890
Jason Ekstranda85118c2013-06-27 20:17:02 -05001891 wl_resource_set_implementation(cb->resource, NULL, cb,
1892 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001893
Pekka Paalanenbc106382012-10-10 12:49:31 +03001894 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001895}
1896
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001897static void
1898surface_set_opaque_region(struct wl_client *client,
1899 struct wl_resource *resource,
1900 struct wl_resource *region_resource)
1901{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001902 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001903 struct weston_region *region;
1904
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001905 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001906 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001907 pixman_region32_copy(&surface->pending.opaque,
1908 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001909 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001910 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001911 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001912}
1913
1914static void
1915surface_set_input_region(struct wl_client *client,
1916 struct wl_resource *resource,
1917 struct wl_resource *region_resource)
1918{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001919 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001920 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001921
1922 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001923 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001924 pixman_region32_copy(&surface->pending.input,
1925 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001926 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001927 pixman_region32_fini(&surface->pending.input);
1928 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001929 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001930}
1931
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001932static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001933weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001934{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001935 struct weston_subsurface *sub;
1936
1937 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1938 parent_link_pending) {
1939 wl_list_remove(&sub->parent_link);
1940 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1941 }
1942}
1943
1944static void
1945weston_surface_commit(struct weston_surface *surface)
1946{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001947 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001948 pixman_region32_t opaque;
1949
Alexander Larsson4ea95522013-05-22 14:41:37 +02001950 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001951 surface->buffer_transform = surface->pending.buffer_transform;
1952
Alexander Larsson4ea95522013-05-22 14:41:37 +02001953 /* wl_surface.set_buffer_scale */
1954 surface->buffer_scale = surface->pending.buffer_scale;
1955
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001956 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001957 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001958 weston_surface_attach(surface, surface->pending.buffer);
1959
Jason Ekstranda7af7042013-10-12 22:38:11 -05001960 surface->width = 0;
1961 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001962 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001963 /* This already includes the buffer scale */
1964 surface->width = weston_surface_buffer_width(surface);
1965 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001966 }
1967
1968 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001969 surface->configure(surface,
1970 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001971 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001972
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001973 if (surface->pending.buffer)
1974 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1975 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001976 surface->pending.sx = 0;
1977 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001978 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001979
1980 /* wl_surface.damage */
1981 pixman_region32_union(&surface->damage, &surface->damage,
1982 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001983 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1984 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001985 surface->width,
1986 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001987 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001988
1989 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001990 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001991 surface->width,
1992 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001993 pixman_region32_intersect(&opaque,
1994 &opaque, &surface->pending.opaque);
1995
1996 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1997 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001998 wl_list_for_each(view, &surface->views, surface_link)
1999 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002000 }
2001
2002 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002003
2004 /* wl_surface.set_input_region */
2005 pixman_region32_fini(&surface->input);
2006 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 surface->width,
2008 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002009 pixman_region32_intersect(&surface->input,
2010 &surface->input, &surface->pending.input);
2011
Pekka Paalanenbc106382012-10-10 12:49:31 +03002012 /* wl_surface.frame */
2013 wl_list_insert_list(&surface->frame_callback_list,
2014 &surface->pending.frame_callback_list);
2015 wl_list_init(&surface->pending.frame_callback_list);
2016
Pekka Paalanene67858b2013-04-25 13:57:42 +03002017 weston_surface_commit_subsurface_order(surface);
2018
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002019 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002020}
2021
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002022static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002023weston_subsurface_commit(struct weston_subsurface *sub);
2024
2025static void
2026weston_subsurface_parent_commit(struct weston_subsurface *sub,
2027 int parent_is_synchronized);
2028
2029static void
2030surface_commit(struct wl_client *client, struct wl_resource *resource)
2031{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002032 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002033 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2034
2035 if (sub) {
2036 weston_subsurface_commit(sub);
2037 return;
2038 }
2039
2040 weston_surface_commit(surface);
2041
2042 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2043 if (sub->surface != surface)
2044 weston_subsurface_parent_commit(sub, 0);
2045 }
2046}
2047
2048static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002049surface_set_buffer_transform(struct wl_client *client,
2050 struct wl_resource *resource, int transform)
2051{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002052 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002053
2054 surface->pending.buffer_transform = transform;
2055}
2056
Alexander Larsson4ea95522013-05-22 14:41:37 +02002057static void
2058surface_set_buffer_scale(struct wl_client *client,
2059 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002060 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002061{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002062 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002063
2064 surface->pending.buffer_scale = scale;
2065}
2066
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002067static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002068 surface_destroy,
2069 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002070 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002071 surface_frame,
2072 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002073 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002074 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002075 surface_set_buffer_transform,
2076 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002077};
2078
2079static void
2080compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002081 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002082{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002083 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002084 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002085
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002086 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002087 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002088 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002089 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002090 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002091
Jason Ekstranda85118c2013-06-27 20:17:02 -05002092 surface->resource =
2093 wl_resource_create(client, &wl_surface_interface,
2094 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002095 if (surface->resource == NULL) {
2096 weston_surface_destroy(surface);
2097 wl_resource_post_no_memory(resource);
2098 return;
2099 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002100 wl_resource_set_implementation(surface->resource, &surface_interface,
2101 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002102}
2103
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002104static void
2105destroy_region(struct wl_resource *resource)
2106{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002107 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002108
2109 pixman_region32_fini(&region->region);
2110 free(region);
2111}
2112
2113static void
2114region_destroy(struct wl_client *client, struct wl_resource *resource)
2115{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002116 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002117}
2118
2119static void
2120region_add(struct wl_client *client, struct wl_resource *resource,
2121 int32_t x, int32_t y, int32_t width, int32_t height)
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_union_rect(&region->region, &region->region,
2126 x, y, width, height);
2127}
2128
2129static void
2130region_subtract(struct wl_client *client, struct wl_resource *resource,
2131 int32_t x, int32_t y, int32_t width, int32_t height)
2132{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002133 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002134 pixman_region32_t rect;
2135
2136 pixman_region32_init_rect(&rect, x, y, width, height);
2137 pixman_region32_subtract(&region->region, &region->region, &rect);
2138 pixman_region32_fini(&rect);
2139}
2140
2141static const struct wl_region_interface region_interface = {
2142 region_destroy,
2143 region_add,
2144 region_subtract
2145};
2146
2147static void
2148compositor_create_region(struct wl_client *client,
2149 struct wl_resource *resource, uint32_t id)
2150{
2151 struct weston_region *region;
2152
2153 region = malloc(sizeof *region);
2154 if (region == NULL) {
2155 wl_resource_post_no_memory(resource);
2156 return;
2157 }
2158
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002159 pixman_region32_init(&region->region);
2160
Jason Ekstranda85118c2013-06-27 20:17:02 -05002161 region->resource =
2162 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002163 if (region->resource == NULL) {
2164 free(region);
2165 wl_resource_post_no_memory(resource);
2166 return;
2167 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002168 wl_resource_set_implementation(region->resource, &region_interface,
2169 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002170}
2171
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002172static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002173 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002174 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002175};
2176
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002177static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2179{
2180 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002181 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002183
Alexander Larsson4ea95522013-05-22 14:41:37 +02002184 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002185 surface->buffer_transform = sub->cached.buffer_transform;
2186
Alexander Larsson4ea95522013-05-22 14:41:37 +02002187 /* wl_surface.set_buffer_scale */
2188 surface->buffer_scale = sub->cached.buffer_scale;
2189
Pekka Paalanene67858b2013-04-25 13:57:42 +03002190 /* wl_surface.attach */
2191 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2192 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2193 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2194
Jason Ekstranda7af7042013-10-12 22:38:11 -05002195 surface->width = 0;
2196 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002197 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002198 surface->width = weston_surface_buffer_width(surface);
2199 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002200 }
2201
2202 if (surface->configure && sub->cached.newly_attached)
2203 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002204 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002205 sub->cached.sx = 0;
2206 sub->cached.sy = 0;
2207 sub->cached.newly_attached = 0;
2208
2209 /* wl_surface.damage */
2210 pixman_region32_union(&surface->damage, &surface->damage,
2211 &sub->cached.damage);
2212 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2213 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002214 surface->width,
2215 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002216 empty_region(&sub->cached.damage);
2217
2218 /* wl_surface.set_opaque_region */
2219 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002220 surface->width,
2221 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222 pixman_region32_intersect(&opaque,
2223 &opaque, &sub->cached.opaque);
2224
2225 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2226 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002227 wl_list_for_each(view, &surface->views, surface_link)
2228 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002229 }
2230
2231 pixman_region32_fini(&opaque);
2232
2233 /* wl_surface.set_input_region */
2234 pixman_region32_fini(&surface->input);
2235 pixman_region32_init_rect(&surface->input, 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(&surface->input,
2239 &surface->input, &sub->cached.input);
2240
2241 /* wl_surface.frame */
2242 wl_list_insert_list(&surface->frame_callback_list,
2243 &sub->cached.frame_callback_list);
2244 wl_list_init(&sub->cached.frame_callback_list);
2245
2246 weston_surface_commit_subsurface_order(surface);
2247
2248 weston_surface_schedule_repaint(surface);
2249
2250 sub->cached.has_data = 0;
2251}
2252
2253static void
2254weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2255{
2256 struct weston_surface *surface = sub->surface;
2257
2258 /*
2259 * If this commit would cause the surface to move by the
2260 * attach(dx, dy) parameters, the old damage region must be
2261 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002262 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002263 */
2264 pixman_region32_translate(&sub->cached.damage,
2265 -surface->pending.sx, -surface->pending.sy);
2266 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2267 &surface->pending.damage);
2268 empty_region(&surface->pending.damage);
2269
2270 if (surface->pending.newly_attached) {
2271 sub->cached.newly_attached = 1;
2272 weston_buffer_reference(&sub->cached.buffer_ref,
2273 surface->pending.buffer);
2274 }
2275 sub->cached.sx += surface->pending.sx;
2276 sub->cached.sy += surface->pending.sy;
2277 surface->pending.sx = 0;
2278 surface->pending.sy = 0;
2279 surface->pending.newly_attached = 0;
2280
2281 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002282 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002283
2284 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2285
2286 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2287
2288 wl_list_insert_list(&sub->cached.frame_callback_list,
2289 &surface->pending.frame_callback_list);
2290 wl_list_init(&surface->pending.frame_callback_list);
2291
2292 sub->cached.has_data = 1;
2293}
2294
2295static int
2296weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2297{
2298 while (sub) {
2299 if (sub->synchronized)
2300 return 1;
2301
2302 if (!sub->parent)
2303 return 0;
2304
2305 sub = weston_surface_to_subsurface(sub->parent);
2306 }
2307
2308 return 0;
2309}
2310
2311static void
2312weston_subsurface_commit(struct weston_subsurface *sub)
2313{
2314 struct weston_surface *surface = sub->surface;
2315 struct weston_subsurface *tmp;
2316
2317 /* Recursive check for effectively synchronized. */
2318 if (weston_subsurface_is_synchronized(sub)) {
2319 weston_subsurface_commit_to_cache(sub);
2320 } else {
2321 if (sub->cached.has_data) {
2322 /* flush accumulated state from cache */
2323 weston_subsurface_commit_to_cache(sub);
2324 weston_subsurface_commit_from_cache(sub);
2325 } else {
2326 weston_surface_commit(surface);
2327 }
2328
2329 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2330 if (tmp->surface != surface)
2331 weston_subsurface_parent_commit(tmp, 0);
2332 }
2333 }
2334}
2335
2336static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002337weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002338{
2339 struct weston_surface *surface = sub->surface;
2340 struct weston_subsurface *tmp;
2341
Pekka Paalanene67858b2013-04-25 13:57:42 +03002342 /* From now on, commit_from_cache the whole sub-tree, regardless of
2343 * the synchronized mode of each child. This sub-surface or some
2344 * of its ancestors were synchronized, so we are synchronized
2345 * all the way down.
2346 */
2347
2348 if (sub->cached.has_data)
2349 weston_subsurface_commit_from_cache(sub);
2350
2351 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2352 if (tmp->surface != surface)
2353 weston_subsurface_parent_commit(tmp, 1);
2354 }
2355}
2356
2357static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002358weston_subsurface_parent_commit(struct weston_subsurface *sub,
2359 int parent_is_synchronized)
2360{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002361 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002362 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002363 wl_list_for_each(view, &sub->surface->views, surface_link)
2364 weston_view_set_position(view,
2365 sub->position.x,
2366 sub->position.y);
2367
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002368 sub->position.set = 0;
2369 }
2370
2371 if (parent_is_synchronized || sub->synchronized)
2372 weston_subsurface_synchronized_commit(sub);
2373}
2374
2375static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002376subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2377 int32_t width, int32_t height)
2378{
2379 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002380 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002381
Jason Ekstranda7af7042013-10-12 22:38:11 -05002382 wl_list_for_each(view, &surface->views, surface_link)
2383 weston_view_configure(view,
2384 view->geometry.x + dx,
2385 view->geometry.y + dy,
2386 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002387
2388 /* No need to check parent mappedness, because if parent is not
2389 * mapped, parent is not in a visible layer, so this sub-surface
2390 * will not be drawn either.
2391 */
2392 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002393 /* Cannot call weston_surface_update_transform(),
2394 * because that would call it also for the parent surface,
2395 * which might not be mapped yet. That would lead to
2396 * inconsistent state, where the window could never be
2397 * mapped.
2398 *
2399 * Instead just assing any output, to make
2400 * weston_surface_is_mapped() return true, so that when the
2401 * parent surface does get mapped, this one will get
2402 * included, too. See surface_list_add().
2403 */
2404 assert(!wl_list_empty(&compositor->output_list));
2405 surface->output = container_of(compositor->output_list.next,
2406 struct weston_output, link);
2407 }
2408}
2409
2410static struct weston_subsurface *
2411weston_surface_to_subsurface(struct weston_surface *surface)
2412{
2413 if (surface->configure == subsurface_configure)
2414 return surface->configure_private;
2415
2416 return NULL;
2417}
2418
Pekka Paalanen01388e22013-04-25 13:57:44 +03002419WL_EXPORT struct weston_surface *
2420weston_surface_get_main_surface(struct weston_surface *surface)
2421{
2422 struct weston_subsurface *sub;
2423
2424 while (surface && (sub = weston_surface_to_subsurface(surface)))
2425 surface = sub->parent;
2426
2427 return surface;
2428}
2429
Pekka Paalanene67858b2013-04-25 13:57:42 +03002430static void
2431subsurface_set_position(struct wl_client *client,
2432 struct wl_resource *resource, int32_t x, int32_t y)
2433{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002434 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002435
2436 if (!sub)
2437 return;
2438
2439 sub->position.x = x;
2440 sub->position.y = y;
2441 sub->position.set = 1;
2442}
2443
2444static struct weston_subsurface *
2445subsurface_from_surface(struct weston_surface *surface)
2446{
2447 struct weston_subsurface *sub;
2448
2449 sub = weston_surface_to_subsurface(surface);
2450 if (sub)
2451 return sub;
2452
2453 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2454 if (sub->surface == surface)
2455 return sub;
2456
2457 return NULL;
2458}
2459
2460static struct weston_subsurface *
2461subsurface_sibling_check(struct weston_subsurface *sub,
2462 struct weston_surface *surface,
2463 const char *request)
2464{
2465 struct weston_subsurface *sibling;
2466
2467 sibling = subsurface_from_surface(surface);
2468
2469 if (!sibling) {
2470 wl_resource_post_error(sub->resource,
2471 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2472 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002473 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002474 return NULL;
2475 }
2476
2477 if (sibling->parent != sub->parent) {
2478 wl_resource_post_error(sub->resource,
2479 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2480 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002481 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002482 return NULL;
2483 }
2484
2485 return sibling;
2486}
2487
2488static void
2489subsurface_place_above(struct wl_client *client,
2490 struct wl_resource *resource,
2491 struct wl_resource *sibling_resource)
2492{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002493 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002494 struct weston_surface *surface =
2495 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002496 struct weston_subsurface *sibling;
2497
2498 if (!sub)
2499 return;
2500
2501 sibling = subsurface_sibling_check(sub, surface, "place_above");
2502 if (!sibling)
2503 return;
2504
2505 wl_list_remove(&sub->parent_link_pending);
2506 wl_list_insert(sibling->parent_link_pending.prev,
2507 &sub->parent_link_pending);
2508}
2509
2510static void
2511subsurface_place_below(struct wl_client *client,
2512 struct wl_resource *resource,
2513 struct wl_resource *sibling_resource)
2514{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002515 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002516 struct weston_surface *surface =
2517 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002518 struct weston_subsurface *sibling;
2519
2520 if (!sub)
2521 return;
2522
2523 sibling = subsurface_sibling_check(sub, surface, "place_below");
2524 if (!sibling)
2525 return;
2526
2527 wl_list_remove(&sub->parent_link_pending);
2528 wl_list_insert(&sibling->parent_link_pending,
2529 &sub->parent_link_pending);
2530}
2531
2532static void
2533subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2534{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002535 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002536
2537 if (sub)
2538 sub->synchronized = 1;
2539}
2540
2541static void
2542subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2543{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002544 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002545
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002546 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002547 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002548
2549 /* If sub became effectively desynchronized, flush. */
2550 if (!weston_subsurface_is_synchronized(sub))
2551 weston_subsurface_synchronized_commit(sub);
2552 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553}
2554
2555static void
2556weston_subsurface_cache_init(struct weston_subsurface *sub)
2557{
2558 pixman_region32_init(&sub->cached.damage);
2559 pixman_region32_init(&sub->cached.opaque);
2560 pixman_region32_init(&sub->cached.input);
2561 wl_list_init(&sub->cached.frame_callback_list);
2562 sub->cached.buffer_ref.buffer = NULL;
2563}
2564
2565static void
2566weston_subsurface_cache_fini(struct weston_subsurface *sub)
2567{
2568 struct weston_frame_callback *cb, *tmp;
2569
2570 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002571 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002572
2573 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2574 pixman_region32_fini(&sub->cached.damage);
2575 pixman_region32_fini(&sub->cached.opaque);
2576 pixman_region32_fini(&sub->cached.input);
2577}
2578
2579static void
2580weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2581{
2582 wl_list_remove(&sub->parent_link);
2583 wl_list_remove(&sub->parent_link_pending);
2584 wl_list_remove(&sub->parent_destroy_listener.link);
2585 sub->parent = NULL;
2586}
2587
2588static void
2589weston_subsurface_destroy(struct weston_subsurface *sub);
2590
2591static void
2592subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2593{
2594 struct weston_subsurface *sub =
2595 container_of(listener, struct weston_subsurface,
2596 surface_destroy_listener);
2597 assert(data == &sub->surface->resource);
2598
2599 /* The protocol object (wl_resource) is left inert. */
2600 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002601 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002602
2603 weston_subsurface_destroy(sub);
2604}
2605
2606static void
2607subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2608{
2609 struct weston_subsurface *sub =
2610 container_of(listener, struct weston_subsurface,
2611 parent_destroy_listener);
2612 assert(data == &sub->parent->resource);
2613 assert(sub->surface != sub->parent);
2614
2615 if (weston_surface_is_mapped(sub->surface))
2616 weston_surface_unmap(sub->surface);
2617
2618 weston_subsurface_unlink_parent(sub);
2619}
2620
2621static void
2622subsurface_resource_destroy(struct wl_resource *resource)
2623{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002624 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002625
2626 if (sub)
2627 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002628}
2629
2630static void
2631subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2632{
2633 wl_resource_destroy(resource);
2634}
2635
2636static void
2637weston_subsurface_link_parent(struct weston_subsurface *sub,
2638 struct weston_surface *parent)
2639{
2640 sub->parent = parent;
2641 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002642 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 &sub->parent_destroy_listener);
2644
2645 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2646 wl_list_insert(&parent->subsurface_list_pending,
2647 &sub->parent_link_pending);
2648}
2649
2650static void
2651weston_subsurface_link_surface(struct weston_subsurface *sub,
2652 struct weston_surface *surface)
2653{
2654 sub->surface = surface;
2655 sub->surface_destroy_listener.notify =
2656 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002657 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002658 &sub->surface_destroy_listener);
2659}
2660
2661static void
2662weston_subsurface_destroy(struct weston_subsurface *sub)
2663{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002664 struct weston_view *view, *next;
2665
Pekka Paalanene67858b2013-04-25 13:57:42 +03002666 assert(sub->surface);
2667
2668 if (sub->resource) {
2669 assert(weston_surface_to_subsurface(sub->surface) == sub);
2670 assert(sub->parent_destroy_listener.notify ==
2671 subsurface_handle_parent_destroy);
2672
Jason Ekstranda7af7042013-10-12 22:38:11 -05002673 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2674 weston_view_destroy(view);
2675
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676 if (sub->parent)
2677 weston_subsurface_unlink_parent(sub);
2678
2679 weston_subsurface_cache_fini(sub);
2680
2681 sub->surface->configure = NULL;
2682 sub->surface->configure_private = NULL;
2683 } else {
2684 /* the dummy weston_subsurface for the parent itself */
2685 assert(sub->parent_destroy_listener.notify == NULL);
2686 wl_list_remove(&sub->parent_link);
2687 wl_list_remove(&sub->parent_link_pending);
2688 }
2689
2690 wl_list_remove(&sub->surface_destroy_listener.link);
2691 free(sub);
2692}
2693
2694static const struct wl_subsurface_interface subsurface_implementation = {
2695 subsurface_destroy,
2696 subsurface_set_position,
2697 subsurface_place_above,
2698 subsurface_place_below,
2699 subsurface_set_sync,
2700 subsurface_set_desync
2701};
2702
2703static struct weston_subsurface *
2704weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2705 struct weston_surface *parent)
2706{
2707 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002708 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709
2710 sub = calloc(1, sizeof *sub);
2711 if (!sub)
2712 return NULL;
2713
Jason Ekstranda7af7042013-10-12 22:38:11 -05002714 wl_list_init(&sub->unused_views);
2715
Jason Ekstranda85118c2013-06-27 20:17:02 -05002716 sub->resource =
2717 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002718 if (!sub->resource) {
2719 free(sub);
2720 return NULL;
2721 }
2722
Jason Ekstranda85118c2013-06-27 20:17:02 -05002723 wl_resource_set_implementation(sub->resource,
2724 &subsurface_implementation,
2725 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002726 weston_subsurface_link_surface(sub, surface);
2727 weston_subsurface_link_parent(sub, parent);
2728 weston_subsurface_cache_init(sub);
2729 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002730
2731 return sub;
2732}
2733
2734/* Create a dummy subsurface for having the parent itself in its
2735 * sub-surface lists. Makes stacking order manipulation easy.
2736 */
2737static struct weston_subsurface *
2738weston_subsurface_create_for_parent(struct weston_surface *parent)
2739{
2740 struct weston_subsurface *sub;
2741
2742 sub = calloc(1, sizeof *sub);
2743 if (!sub)
2744 return NULL;
2745
2746 weston_subsurface_link_surface(sub, parent);
2747 sub->parent = parent;
2748 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2749 wl_list_insert(&parent->subsurface_list_pending,
2750 &sub->parent_link_pending);
2751
2752 return sub;
2753}
2754
2755static void
2756subcompositor_get_subsurface(struct wl_client *client,
2757 struct wl_resource *resource,
2758 uint32_t id,
2759 struct wl_resource *surface_resource,
2760 struct wl_resource *parent_resource)
2761{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002762 struct weston_surface *surface =
2763 wl_resource_get_user_data(surface_resource);
2764 struct weston_surface *parent =
2765 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002766 struct weston_subsurface *sub;
2767 static const char where[] = "get_subsurface: wl_subsurface@";
2768
2769 if (surface == parent) {
2770 wl_resource_post_error(resource,
2771 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2772 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002773 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774 return;
2775 }
2776
2777 if (weston_surface_to_subsurface(surface)) {
2778 wl_resource_post_error(resource,
2779 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2780 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002781 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 return;
2783 }
2784
2785 if (surface->configure) {
2786 wl_resource_post_error(resource,
2787 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2788 "%s%d: wl_surface@%d already has a role",
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
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002793 if (weston_surface_get_main_surface(parent) == surface) {
2794 wl_resource_post_error(resource,
2795 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2796 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002797 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002798 return;
2799 }
2800
Pekka Paalanene67858b2013-04-25 13:57:42 +03002801 /* make sure the parent is in its own list */
2802 if (wl_list_empty(&parent->subsurface_list)) {
2803 if (!weston_subsurface_create_for_parent(parent)) {
2804 wl_resource_post_no_memory(resource);
2805 return;
2806 }
2807 }
2808
2809 sub = weston_subsurface_create(id, surface, parent);
2810 if (!sub) {
2811 wl_resource_post_no_memory(resource);
2812 return;
2813 }
2814
2815 surface->configure = subsurface_configure;
2816 surface->configure_private = sub;
2817}
2818
2819static void
2820subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2821{
2822 wl_resource_destroy(resource);
2823}
2824
2825static const struct wl_subcompositor_interface subcompositor_interface = {
2826 subcompositor_destroy,
2827 subcompositor_get_subsurface
2828};
2829
2830static void
2831bind_subcompositor(struct wl_client *client,
2832 void *data, uint32_t version, uint32_t id)
2833{
2834 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002835 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836
Jason Ekstranda85118c2013-06-27 20:17:02 -05002837 resource =
2838 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002839 if (resource == NULL) {
2840 wl_client_post_no_memory(client);
2841 return;
2842 }
2843 wl_resource_set_implementation(resource, &subcompositor_interface,
2844 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002845}
2846
2847static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002848weston_compositor_dpms(struct weston_compositor *compositor,
2849 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002850{
2851 struct weston_output *output;
2852
2853 wl_list_for_each(output, &compositor->output_list, link)
2854 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002855 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002856}
2857
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002858WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002859weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002860{
Neil Roberts8b62e202013-09-30 13:14:47 +01002861 uint32_t old_state = compositor->state;
2862
2863 /* The state needs to be changed before emitting the wake
2864 * signal because that may try to schedule a repaint which
2865 * will not work if the compositor is still sleeping */
2866 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2867
2868 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002869 case WESTON_COMPOSITOR_SLEEPING:
2870 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2871 /* fall through */
2872 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002873 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002874 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002875 /* fall through */
2876 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002877 wl_event_source_timer_update(compositor->idle_source,
2878 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002879 }
2880}
2881
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002882WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002883weston_compositor_offscreen(struct weston_compositor *compositor)
2884{
2885 switch (compositor->state) {
2886 case WESTON_COMPOSITOR_OFFSCREEN:
2887 return;
2888 case WESTON_COMPOSITOR_SLEEPING:
2889 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2890 /* fall through */
2891 default:
2892 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2893 wl_event_source_timer_update(compositor->idle_source, 0);
2894 }
2895}
2896
2897WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002898weston_compositor_sleep(struct weston_compositor *compositor)
2899{
2900 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2901 return;
2902
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002903 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002904 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2905 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2906}
2907
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002908static int
2909idle_handler(void *data)
2910{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002911 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002912
2913 if (compositor->idle_inhibit)
2914 return 1;
2915
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002916 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002917 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002918
2919 return 1;
2920}
2921
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002922WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002923weston_plane_init(struct weston_plane *plane,
2924 struct weston_compositor *ec,
2925 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002926{
2927 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002928 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002929 plane->x = x;
2930 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002931 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002932
2933 /* Init the link so that the call to wl_list_remove() when releasing
2934 * the plane without ever stacking doesn't lead to a crash */
2935 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002936}
2937
2938WL_EXPORT void
2939weston_plane_release(struct weston_plane *plane)
2940{
Xiong Zhang97116532013-10-23 13:58:31 +08002941 struct weston_view *view;
2942
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002943 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002944 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002945
Xiong Zhang97116532013-10-23 13:58:31 +08002946 wl_list_for_each(view, &plane->compositor->view_list, link) {
2947 if (view->plane == plane)
2948 view->plane = NULL;
2949 }
2950
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002951 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002952}
2953
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002954WL_EXPORT void
2955weston_compositor_stack_plane(struct weston_compositor *ec,
2956 struct weston_plane *plane,
2957 struct weston_plane *above)
2958{
2959 if (above)
2960 wl_list_insert(above->link.prev, &plane->link);
2961 else
2962 wl_list_insert(&ec->plane_list, &plane->link);
2963}
2964
Casey Dahlin9074db52012-04-19 22:50:09 -04002965static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002966{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002967 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002968}
2969
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002970static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002971bind_output(struct wl_client *client,
2972 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002973{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002974 struct weston_output *output = data;
2975 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002976 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002977
Jason Ekstranda85118c2013-06-27 20:17:02 -05002978 resource = wl_resource_create(client, &wl_output_interface,
2979 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002980 if (resource == NULL) {
2981 wl_client_post_no_memory(client);
2982 return;
2983 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002984
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002985 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002986 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002987
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002988 wl_output_send_geometry(resource,
2989 output->x,
2990 output->y,
2991 output->mm_width,
2992 output->mm_height,
2993 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002994 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002995 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002996 if (version >= 2)
2997 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002998 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002999
3000 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003001 wl_output_send_mode(resource,
3002 mode->flags,
3003 mode->width,
3004 mode->height,
3005 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003006 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003007
3008 if (version >= 2)
3009 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003010}
3011
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003012WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003013weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003014{
Richard Hughes64ddde12013-05-01 21:52:10 +01003015 wl_signal_emit(&output->destroy_signal, output);
3016
Richard Hughesafe690c2013-05-02 10:10:04 +01003017 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003018 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003019 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003020 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003021
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003022 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003023}
3024
Scott Moreau1bad5db2012-08-18 01:04:05 -06003025static void
3026weston_output_compute_transform(struct weston_output *output)
3027{
3028 struct weston_matrix transform;
3029 int flip;
3030
3031 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003032 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003033
3034 switch(output->transform) {
3035 case WL_OUTPUT_TRANSFORM_FLIPPED:
3036 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3037 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3038 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003039 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003040 flip = -1;
3041 break;
3042 default:
3043 flip = 1;
3044 break;
3045 }
3046
3047 switch(output->transform) {
3048 case WL_OUTPUT_TRANSFORM_NORMAL:
3049 case WL_OUTPUT_TRANSFORM_FLIPPED:
3050 transform.d[0] = flip;
3051 transform.d[1] = 0;
3052 transform.d[4] = 0;
3053 transform.d[5] = 1;
3054 break;
3055 case WL_OUTPUT_TRANSFORM_90:
3056 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3057 transform.d[0] = 0;
3058 transform.d[1] = -flip;
3059 transform.d[4] = 1;
3060 transform.d[5] = 0;
3061 break;
3062 case WL_OUTPUT_TRANSFORM_180:
3063 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3064 transform.d[0] = -flip;
3065 transform.d[1] = 0;
3066 transform.d[4] = 0;
3067 transform.d[5] = -1;
3068 break;
3069 case WL_OUTPUT_TRANSFORM_270:
3070 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3071 transform.d[0] = 0;
3072 transform.d[1] = flip;
3073 transform.d[4] = -1;
3074 transform.d[5] = 0;
3075 break;
3076 default:
3077 break;
3078 }
3079
3080 weston_matrix_multiply(&output->matrix, &transform);
3081}
3082
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003083WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003084weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003085{
Scott Moreau850ca422012-05-21 15:21:25 -06003086 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003087 struct weston_matrix camera;
3088 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003089
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003090 weston_matrix_init(&output->matrix);
3091 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003092 -(output->x + output->width / 2.0),
3093 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003094
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003095 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003096 2.0 / output->width,
3097 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003098
3099 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003100
Scott Moreauccbf29d2012-02-22 14:21:41 -07003101 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003102 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003103 weston_matrix_init(&camera);
3104 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003105 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003106 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003107 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003108 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003109 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003110 weston_matrix_multiply(&output->matrix, &modelview);
3111 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003112
Scott Moreauccbf29d2012-02-22 14:21:41 -07003113 output->dirty = 0;
3114}
3115
Scott Moreau1bad5db2012-08-18 01:04:05 -06003116static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003117weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003118{
3119 output->transform = transform;
3120
3121 switch (transform) {
3122 case WL_OUTPUT_TRANSFORM_90:
3123 case WL_OUTPUT_TRANSFORM_270:
3124 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3125 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3126 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003127 output->width = output->current_mode->height;
3128 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003129 break;
3130 case WL_OUTPUT_TRANSFORM_NORMAL:
3131 case WL_OUTPUT_TRANSFORM_180:
3132 case WL_OUTPUT_TRANSFORM_FLIPPED:
3133 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003134 output->width = output->current_mode->width;
3135 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003136 break;
3137 default:
3138 break;
3139 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003140
Hardening57388e42013-09-18 23:56:36 +02003141 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003142 output->width /= scale;
3143 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003144}
3145
Scott Moreauccbf29d2012-02-22 14:21:41 -07003146WL_EXPORT void
3147weston_output_move(struct weston_output *output, int x, int y)
3148{
3149 output->x = x;
3150 output->y = y;
3151
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003152 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003153 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003154 output->width,
3155 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003156}
3157
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003158WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003159weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003160 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003161 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003162{
3163 output->compositor = c;
3164 output->x = x;
3165 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003166 output->mm_width = mm_width;
3167 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003168 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003169 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003170
Alexander Larsson0b135062013-05-28 16:23:36 +02003171 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003172 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003173
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003174 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003175 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003176
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003177 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003178 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003179 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003180 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003181
Casey Dahlin58ba1372012-04-19 22:50:08 -04003182 output->id = ffs(~output->compositor->output_id_pool) - 1;
3183 output->compositor->output_id_pool |= 1 << output->id;
3184
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003185 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003186 wl_global_create(c->wl_display, &wl_output_interface, 2,
3187 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003188 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003189}
3190
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003191WL_EXPORT void
3192weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003193 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003194 wl_fixed_t *x, wl_fixed_t *y)
3195{
3196 wl_fixed_t tx, ty;
3197 wl_fixed_t width, height;
3198
Hardeningff39efa2013-09-18 23:56:35 +02003199 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3200 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003201
3202 switch(output->transform) {
3203 case WL_OUTPUT_TRANSFORM_NORMAL:
3204 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003205 tx = device_x;
3206 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003207 break;
3208 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003209 tx = device_y;
3210 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003211 break;
3212 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003213 tx = width - device_x;
3214 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003215 break;
3216 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003217 tx = width - device_y;
3218 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003219 break;
3220 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003221 tx = width - device_x;
3222 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003223 break;
3224 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003225 tx = width - device_y;
3226 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003227 break;
3228 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003229 tx = device_x;
3230 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003231 break;
3232 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003233 tx = device_y;
3234 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003235 break;
3236 }
3237
Hardeningff39efa2013-09-18 23:56:35 +02003238 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3239 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003240}
3241
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003242static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003243compositor_bind(struct wl_client *client,
3244 void *data, uint32_t version, uint32_t id)
3245{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003246 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003247 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003248
Jason Ekstranda85118c2013-06-27 20:17:02 -05003249 resource = wl_resource_create(client, &wl_compositor_interface,
3250 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003251 if (resource == NULL) {
3252 wl_client_post_no_memory(client);
3253 return;
3254 }
3255
3256 wl_resource_set_implementation(resource, &compositor_interface,
3257 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003258}
3259
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003260static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003261log_uname(void)
3262{
3263 struct utsname usys;
3264
3265 uname(&usys);
3266
3267 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3268 usys.version, usys.machine);
3269}
3270
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003271WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003272weston_environment_get_fd(const char *env)
3273{
3274 char *e, *end;
3275 int fd, flags;
3276
3277 e = getenv(env);
3278 if (!e)
3279 return -1;
3280 fd = strtol(e, &end, 0);
3281 if (*end != '\0')
3282 return -1;
3283
3284 flags = fcntl(fd, F_GETFD);
3285 if (flags == -1)
3286 return -1;
3287
3288 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3289 unsetenv(env);
3290
3291 return fd;
3292}
3293
3294WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003295weston_compositor_init(struct weston_compositor *ec,
3296 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003297 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003298 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003299{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003300 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003301 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003302 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003303
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003304 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003305 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003306 wl_signal_init(&ec->destroy_signal);
3307 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003308 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003309 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003310 wl_signal_init(&ec->idle_signal);
3311 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003312 wl_signal_init(&ec->show_input_panel_signal);
3313 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003314 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003315 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003316 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003317 wl_signal_init(&ec->session_signal);
3318 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003319
Casey Dahlin58ba1372012-04-19 22:50:08 -04003320 ec->output_id_pool = 0;
3321
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003322 if (!wl_global_create(display, &wl_compositor_interface, 3,
3323 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003324 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003325
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003326 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3327 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003328 return -1;
3329
Jason Ekstranda7af7042013-10-12 22:38:11 -05003330 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003331 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003332 wl_list_init(&ec->layer_list);
3333 wl_list_init(&ec->seat_list);
3334 wl_list_init(&ec->output_list);
3335 wl_list_init(&ec->key_binding_list);
3336 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003337 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003338 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003339 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003340
Xiong Zhang97116532013-10-23 13:58:31 +08003341 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003342 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003343
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003344 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3345 weston_config_section_get_string(s, "keymap_rules",
3346 (char **) &xkb_names.rules, NULL);
3347 weston_config_section_get_string(s, "keymap_model",
3348 (char **) &xkb_names.model, NULL);
3349 weston_config_section_get_string(s, "keymap_layout",
3350 (char **) &xkb_names.layout, NULL);
3351 weston_config_section_get_string(s, "keymap_variant",
3352 (char **) &xkb_names.variant, NULL);
3353 weston_config_section_get_string(s, "keymap_options",
3354 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003355
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003356 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3357 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003358
3359 ec->ping_handler = NULL;
3360
3361 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003362 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003363
3364 wl_data_device_manager_init(ec->wl_display);
3365
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003366 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003367
Daniel Stone725c2c32012-06-22 14:04:36 +01003368 loop = wl_display_get_event_loop(ec->wl_display);
3369 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3370 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3371
3372 ec->input_loop = wl_event_loop_create();
3373
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003374 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3375 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3376
3377 weston_compositor_schedule_repaint(ec);
3378
Daniel Stone725c2c32012-06-22 14:04:36 +01003379 return 0;
3380}
3381
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003382WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003383weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003384{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003385 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003386
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003387 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003388 if (ec->input_loop_source)
3389 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003390
Matt Roper361d2ad2011-08-29 13:52:23 -07003391 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003392 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003393 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003394
Daniel Stone325fc2d2012-05-30 16:31:58 +01003395 weston_binding_list_destroy_all(&ec->key_binding_list);
3396 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003397 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003398 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003399 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003400
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003401 weston_plane_release(&ec->primary_plane);
3402
Jonas Ådahlc97af922012-03-28 22:36:09 +02003403 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003404
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003405 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003406}
3407
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003408WL_EXPORT void
3409weston_version(int *major, int *minor, int *micro)
3410{
3411 *major = WESTON_VERSION_MAJOR;
3412 *minor = WESTON_VERSION_MINOR;
3413 *micro = WESTON_VERSION_MICRO;
3414}
3415
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003416static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003417 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003418 const char *desc;
3419} capability_strings[] = {
3420 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003421 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003422};
3423
3424static void
3425weston_compositor_log_capabilities(struct weston_compositor *compositor)
3426{
3427 unsigned i;
3428 int yes;
3429
3430 weston_log("Compositor capabilities:\n");
3431 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3432 yes = compositor->capabilities & capability_strings[i].bit;
3433 weston_log_continue(STAMP_SPACE "%s %s\n",
3434 capability_strings[i].desc,
3435 yes ? "yes" : "no");
3436 }
3437}
3438
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003439static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003440{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003441 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003442
Martin Minarik6d118362012-06-07 18:01:59 +02003443 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003444 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003445
3446 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003447}
3448
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003449#ifdef HAVE_LIBUNWIND
3450
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003451static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003452print_backtrace(void)
3453{
3454 unw_cursor_t cursor;
3455 unw_context_t context;
3456 unw_word_t off;
3457 unw_proc_info_t pip;
3458 int ret, i = 0;
3459 char procname[256];
3460 const char *filename;
3461 Dl_info dlinfo;
3462
3463 pip.unwind_info = NULL;
3464 ret = unw_getcontext(&context);
3465 if (ret) {
3466 weston_log("unw_getcontext: %d\n", ret);
3467 return;
3468 }
3469
3470 ret = unw_init_local(&cursor, &context);
3471 if (ret) {
3472 weston_log("unw_init_local: %d\n", ret);
3473 return;
3474 }
3475
3476 ret = unw_step(&cursor);
3477 while (ret > 0) {
3478 ret = unw_get_proc_info(&cursor, &pip);
3479 if (ret) {
3480 weston_log("unw_get_proc_info: %d\n", ret);
3481 break;
3482 }
3483
3484 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3485 if (ret && ret != -UNW_ENOMEM) {
3486 if (ret != -UNW_EUNSPEC)
3487 weston_log("unw_get_proc_name: %d\n", ret);
3488 procname[0] = '?';
3489 procname[1] = 0;
3490 }
3491
3492 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3493 *dlinfo.dli_fname)
3494 filename = dlinfo.dli_fname;
3495 else
3496 filename = "?";
3497
3498 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3499 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3500
3501 ret = unw_step(&cursor);
3502 if (ret < 0)
3503 weston_log("unw_step: %d\n", ret);
3504 }
3505}
3506
3507#else
3508
3509static void
3510print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003511{
3512 void *buffer[32];
3513 int i, count;
3514 Dl_info info;
3515
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003516 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3517 for (i = 0; i < count; i++) {
3518 dladdr(buffer[i], &info);
3519 weston_log(" [%016lx] %s (%s)\n",
3520 (long) buffer[i],
3521 info.dli_sname ? info.dli_sname : "--",
3522 info.dli_fname);
3523 }
3524}
3525
3526#endif
3527
3528static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003529on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003530{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003531 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003532 * then call the backend restore function, which will switch
3533 * back to the vt we launched from or ungrab X etc and then
3534 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003535 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003536 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003537 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003538
Peter Maatmane5b42e42013-03-27 22:38:53 +01003539 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003540
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003541 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003542
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003543 segv_compositor->restore(segv_compositor);
3544
3545 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003546}
3547
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003548WL_EXPORT void *
3549weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003550{
3551 char path[PATH_MAX];
3552 void *module, *init;
3553
3554 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003555 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003556 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003557 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003558
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003559 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3560 if (module) {
3561 weston_log("Module '%s' already loaded\n", path);
3562 dlclose(module);
3563 return NULL;
3564 }
3565
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003566 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003567 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003568 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003569 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003570 return NULL;
3571 }
3572
3573 init = dlsym(module, entrypoint);
3574 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003575 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003576 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003577 return NULL;
3578 }
3579
3580 return init;
3581}
3582
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003583static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003584load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003585 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003586{
3587 const char *p, *end;
3588 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003589 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003590 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003591
3592 if (modules == NULL)
3593 return 0;
3594
3595 p = modules;
3596 while (*p) {
3597 end = strchrnul(p, ',');
3598 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003599 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003600 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003601 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003602 p = end;
3603 while (*p == ',')
3604 p++;
3605
3606 }
3607
3608 return 0;
3609}
3610
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003611static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003612 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3613
3614static const char xdg_wrong_message[] =
3615 "fatal: environment variable XDG_RUNTIME_DIR\n"
3616 "is set to \"%s\", which is not a directory.\n";
3617
3618static const char xdg_wrong_mode_message[] =
3619 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3620 "correctly. Unix access mode must be 0700 but is %o,\n"
3621 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003622 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003623
3624static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003625 "Refer to your distribution on how to get it, or\n"
3626 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3627 "on how to implement it.\n";
3628
Martin Minarik37032f82012-06-18 20:15:18 +02003629static void
3630verify_xdg_runtime_dir(void)
3631{
3632 char *dir = getenv("XDG_RUNTIME_DIR");
3633 struct stat s;
3634
3635 if (!dir) {
3636 weston_log(xdg_error_message);
3637 weston_log_continue(xdg_detail_message);
3638 exit(EXIT_FAILURE);
3639 }
3640
3641 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3642 weston_log(xdg_wrong_message, dir);
3643 weston_log_continue(xdg_detail_message);
3644 exit(EXIT_FAILURE);
3645 }
3646
3647 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3648 weston_log(xdg_wrong_mode_message,
3649 dir, s.st_mode & 0777, s.st_uid);
3650 weston_log_continue(xdg_detail_message);
3651 }
3652}
3653
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003654static int
3655usage(int error_code)
3656{
3657 fprintf(stderr,
3658 "Usage: weston [OPTIONS]\n\n"
3659 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3660 "Weston supports multiple backends, and depending on which backend is in use\n"
3661 "different options will be accepted.\n\n"
3662
3663
3664 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003665 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003666 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003667 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3668 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003669 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003670 " -S, --socket=NAME\tName of socket to listen on\n"
3671 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003672 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003673 " --log==FILE\t\tLog to the given file\n"
3674 " -h, --help\t\tThis help message\n\n");
3675
3676 fprintf(stderr,
3677 "Options for drm-backend.so:\n\n"
3678 " --connector=ID\tBring up only this connector\n"
3679 " --seat=SEAT\t\tThe seat that weston should run on\n"
3680 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003681 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003682 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3683
3684 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003685 "Options for fbdev-backend.so:\n\n"
3686 " --tty=TTY\t\tThe tty to use\n"
3687 " --device=DEVICE\tThe framebuffer device to use\n\n");
3688
3689 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003690 "Options for x11-backend.so:\n\n"
3691 " --width=WIDTH\t\tWidth of X window\n"
3692 " --height=HEIGHT\tHeight of X window\n"
3693 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003694 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003695 " --output-count=COUNT\tCreate multiple outputs\n"
3696 " --no-input\t\tDont create input devices\n\n");
3697
3698 fprintf(stderr,
3699 "Options for wayland-backend.so:\n\n"
3700 " --width=WIDTH\t\tWidth of Wayland surface\n"
3701 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003702 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003703 " --display=DISPLAY\tWayland display to connect to\n\n");
3704
Pekka Paalanene31e0532013-05-22 18:03:07 +03003705#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3706 fprintf(stderr,
3707 "Options for rpi-backend.so:\n\n"
3708 " --tty=TTY\t\tThe tty to use\n"
3709 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3710 " --transform=TR\tThe output transformation, TR is one of:\n"
3711 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3712 "\n");
3713#endif
3714
Hardeningc077a842013-07-08 00:51:35 +02003715#if defined(BUILD_RDP_COMPOSITOR)
3716 fprintf(stderr,
3717 "Options for rdp-backend.so:\n\n"
3718 " --width=WIDTH\t\tWidth of desktop\n"
3719 " --height=HEIGHT\tHeight of desktop\n"
3720 " --extra-modes=MODES\t\n"
3721 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3722 " --address=ADDR\tThe address to bind\n"
3723 " --port=PORT\tThe port to listen on\n"
3724 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3725 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3726 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3727 "\n");
3728#endif
3729
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003730 exit(error_code);
3731}
3732
Peter Maatmane5b42e42013-03-27 22:38:53 +01003733static void
3734catch_signals(void)
3735{
3736 struct sigaction action;
3737
3738 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3739 action.sa_sigaction = on_caught_signal;
3740 sigemptyset(&action.sa_mask);
3741 sigaction(SIGSEGV, &action, NULL);
3742 sigaction(SIGABRT, &action, NULL);
3743}
3744
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003745int main(int argc, char *argv[])
3746{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003747 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003748 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003749 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003750 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003751 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003752 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003753 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003754 int *argc, char *argv[],
3755 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003756 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003757 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003758 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003759 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003760 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003761 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003762 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003763 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003764 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003765 struct weston_config *config;
3766 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003767
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003768 const struct weston_option core_options[] = {
3769 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003770 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003771 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3772 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003773 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003774 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003775 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003776 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003777 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003778
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003779 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003780
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003781 if (help)
3782 usage(EXIT_SUCCESS);
3783
Scott Moreau12245142012-08-29 15:15:58 -06003784 if (version) {
3785 printf(PACKAGE_STRING "\n");
3786 return EXIT_SUCCESS;
3787 }
3788
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003789 weston_log_file_open(log);
3790
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003791 weston_log("%s\n"
3792 STAMP_SPACE "%s\n"
3793 STAMP_SPACE "Bug reports to: %s\n"
3794 STAMP_SPACE "Build: %s\n",
3795 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003796 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003797 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003798
Martin Minarik37032f82012-06-18 20:15:18 +02003799 verify_xdg_runtime_dir();
3800
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003801 display = wl_display_create();
3802
Tiago Vignatti2116b892011-08-08 05:52:59 -07003803 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003804 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3805 display);
3806 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3807 display);
3808 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3809 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003810
3811 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003812 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3813 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003814
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003815 if (!backend) {
3816 if (getenv("WAYLAND_DISPLAY"))
3817 backend = "wayland-backend.so";
3818 else if (getenv("DISPLAY"))
3819 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003820 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003821 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003822 }
3823
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003824 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003825 if (config != NULL) {
3826 weston_log("Using config file '%s'\n",
3827 weston_config_get_full_path(config));
3828 } else {
3829 weston_log("Starting with no config file.\n");
3830 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003831 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003832 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003833
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003834 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003835 if (!backend_init)
3836 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003837
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003838 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003839 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003840 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003841 exit(EXIT_FAILURE);
3842 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003843
Peter Maatmane5b42e42013-03-27 22:38:53 +01003844 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003845 segv_compositor = ec;
3846
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003847 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003848
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003849 setenv("WAYLAND_DISPLAY", socket_name, 1);
3850
Jason Ekstranda985da42013-08-22 17:28:03 -05003851 if (!shell)
3852 weston_config_section_get_string(section, "shell", &shell,
3853 "desktop-shell.so");
3854 if (load_modules(ec, shell, &argc, argv) < 0)
3855 goto out;
3856
Ossama Othmana50e6e42013-05-14 09:48:26 -07003857 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003858 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003859 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003860 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003861
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003862 for (i = 1; i < argc; i++)
3863 weston_log("fatal: unhandled option: %s\n", argv[i]);
3864 if (argc > 1) {
3865 ret = EXIT_FAILURE;
3866 goto out;
3867 }
3868
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003869 weston_compositor_log_capabilities(ec);
3870
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003871 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003872 weston_log("fatal: failed to add socket: %m\n");
3873 ret = EXIT_FAILURE;
3874 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003875 }
3876
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003877 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003878
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003879 wl_display_run(display);
3880
3881 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003882 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003883 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003884
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003885 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003886
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003887 for (i = ARRAY_LENGTH(signals); i;)
3888 wl_event_source_remove(signals[--i]);
3889
Daniel Stone855028f2012-05-01 20:37:10 +01003890 weston_compositor_xkb_destroy(ec);
3891
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003892 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003893 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003894
Martin Minarik19e6f262012-06-07 13:08:46 +02003895 weston_log_file_close();
3896
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003897 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003898}