blob: c6cf682373b0829aca7a5149a978b87a9481018a [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);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800698 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200699}
700
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400701static void
702weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
703{
704 uint32_t different = es->output_mask ^ mask;
705 uint32_t entered = mask & different;
706 uint32_t left = es->output_mask & different;
707 struct weston_output *output;
708 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500709 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400710
711 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500712 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400713 return;
714 if (different == 0)
715 return;
716
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500717 client = wl_resource_get_client(es->resource);
718
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400719 wl_list_for_each(output, &es->compositor->output_list, link) {
720 if (1 << output->id & different)
721 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500722 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400723 client);
724 if (resource == NULL)
725 continue;
726 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500727 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400728 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500729 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400730 }
731}
732
733static void
734weston_surface_assign_output(struct weston_surface *es)
735{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500736 struct weston_output *new_output;
737 struct weston_view *view;
738 pixman_region32_t region;
739 uint32_t max, area, mask;
740 pixman_box32_t *e;
741
742 new_output = NULL;
743 max = 0;
744 mask = 0;
745 pixman_region32_init(&region);
746 wl_list_for_each(view, &es->views, surface_link) {
747 if (!view->output)
748 continue;
749
750 pixman_region32_intersect(&region, &view->transform.boundingbox,
751 &view->output->region);
752
753 e = pixman_region32_extents(&region);
754 area = (e->x2 - e->x1) * (e->y2 - e->y1);
755
756 mask |= view->output_mask;
757
758 if (area >= max) {
759 new_output = view->output;
760 max = area;
761 }
762 }
763 pixman_region32_fini(&region);
764
765 es->output = new_output;
766 weston_surface_update_output_mask(es, mask);
767}
768
769static void
770weston_view_assign_output(struct weston_view *ev)
771{
772 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400773 struct weston_output *output, *new_output;
774 pixman_region32_t region;
775 uint32_t max, area, mask;
776 pixman_box32_t *e;
777
778 new_output = NULL;
779 max = 0;
780 mask = 0;
781 pixman_region32_init(&region);
782 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400784 &output->region);
785
786 e = pixman_region32_extents(&region);
787 area = (e->x2 - e->x1) * (e->y2 - e->y1);
788
789 if (area > 0)
790 mask |= 1 << output->id;
791
792 if (area >= max) {
793 new_output = output;
794 max = area;
795 }
796 }
797 pixman_region32_fini(&region);
798
Jason Ekstranda7af7042013-10-12 22:38:11 -0500799 ev->output = new_output;
800 ev->output_mask = mask;
801
802 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400803}
804
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200805static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
807 int32_t width, int32_t height,
808 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200809{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200810 float min_x = HUGE_VALF, min_y = HUGE_VALF;
811 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200812 int32_t s[4][2] = {
813 { sx, sy },
814 { sx, sy + height },
815 { sx + width, sy },
816 { sx + width, sy + height }
817 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200818 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200819 int i;
820
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300821 if (width == 0 || height == 0) {
822 /* avoid rounding empty bbox to 1x1 */
823 pixman_region32_init(bbox);
824 return;
825 }
826
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200827 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200828 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500829 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200830 if (x < min_x)
831 min_x = x;
832 if (x > max_x)
833 max_x = x;
834 if (y < min_y)
835 min_y = y;
836 if (y > max_y)
837 max_y = y;
838 }
839
Pekka Paalanen219b9822012-02-08 15:38:37 +0200840 int_x = floorf(min_x);
841 int_y = floorf(min_y);
842 pixman_region32_init_rect(bbox, int_x, int_y,
843 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200844}
845
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200846static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500847weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200848{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500849 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200850
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200851 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500852 view->geometry.x = roundf(view->geometry.x);
853 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200854
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500855 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
857 view->transform.position.matrix.d[12] = view->geometry.x;
858 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500859
Jason Ekstranda7af7042013-10-12 22:38:11 -0500860 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500861
Jason Ekstranda7af7042013-10-12 22:38:11 -0500862 view->transform.inverse = view->transform.position.matrix;
863 view->transform.inverse.d[12] = -view->geometry.x;
864 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500865
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 pixman_region32_init_rect(&view->transform.boundingbox,
867 view->geometry.x,
868 view->geometry.y,
869 view->geometry.width,
870 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500871
Jason Ekstranda7af7042013-10-12 22:38:11 -0500872 if (view->alpha == 1.0) {
873 pixman_region32_copy(&view->transform.opaque,
874 &view->surface->opaque);
875 pixman_region32_translate(&view->transform.opaque,
876 view->geometry.x,
877 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500878 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200879}
880
881static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200883{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500884 struct weston_view *parent = view->geometry.parent;
885 struct weston_matrix *matrix = &view->transform.matrix;
886 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200887 struct weston_transform *tform;
888
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200890
891 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
893 view->transform.position.matrix.d[12] = view->geometry.x;
894 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200895
896 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200898 weston_matrix_multiply(matrix, &tform->matrix);
899
Pekka Paalanen483243f2013-03-08 14:56:50 +0200900 if (parent)
901 weston_matrix_multiply(matrix, &parent->transform.matrix);
902
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200903 if (weston_matrix_invert(inverse, matrix) < 0) {
904 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500905 weston_log("error: weston_view %p"
906 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200907 return -1;
908 }
909
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 view_compute_bbox(view, 0, 0, view->geometry.width,
911 view->geometry.height,
912 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500913
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200914 return 0;
915}
916
917WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200919{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500920 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200921
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200923 return;
924
Pekka Paalanen483243f2013-03-08 14:56:50 +0200925 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200927
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200929
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200931
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 pixman_region32_fini(&view->transform.boundingbox);
933 pixman_region32_fini(&view->transform.opaque);
934 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500935
Pekka Paalanencd403622012-01-25 13:37:39 +0200936 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 if (view->geometry.transformation_list.next ==
938 &view->transform.position.link &&
939 view->geometry.transformation_list.prev ==
940 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200941 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200943 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 if (weston_view_update_transform_enable(view) < 0)
945 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200946 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200947
Jason Ekstranda7af7042013-10-12 22:38:11 -0500948 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200949
Jason Ekstranda7af7042013-10-12 22:38:11 -0500950 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300951
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 wl_signal_emit(&view->surface->compositor->transform_signal,
953 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200954}
955
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200956WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200958{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200960
961 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 * The invariant: if view->geometry.dirty, then all views
963 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200964 * Corollary: if not parent->geometry.dirty, then all ancestors
965 * are not dirty.
966 */
967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200969 return;
970
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200972
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200974 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200976}
977
978WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979weston_view_to_global_fixed(struct weston_view *view,
980 wl_fixed_t vx, wl_fixed_t vy,
981 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100982{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200983 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100984
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 weston_view_to_global_float(view,
986 wl_fixed_to_double(vx),
987 wl_fixed_to_double(vy),
988 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100989 *x = wl_fixed_from_double(xf);
990 *y = wl_fixed_from_double(yf);
991}
992
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400993WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994weston_view_from_global_float(struct weston_view *view,
995 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200996{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200998 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
999
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001001
1002 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001003 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001005 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 *vx = 0;
1007 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001008 return;
1009 }
1010
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 *vx = v.f[0] / v.f[3];
1012 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001013 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 *vx = x - view->geometry.x;
1015 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001016 }
1017}
1018
1019WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020weston_view_from_global_fixed(struct weston_view *view,
1021 wl_fixed_t x, wl_fixed_t y,
1022 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001025
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 weston_view_from_global_float(view,
1027 wl_fixed_to_double(x),
1028 wl_fixed_to_double(y),
1029 &vxf, &vyf);
1030 *vx = wl_fixed_from_double(vxf);
1031 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001032}
1033
1034WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035weston_view_from_global(struct weston_view *view,
1036 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001039
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1041 *vx = floorf(vxf);
1042 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001043}
1044
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001045WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001046weston_surface_schedule_repaint(struct weston_surface *surface)
1047{
1048 struct weston_output *output;
1049
1050 wl_list_for_each(output, &surface->compositor->output_list, link)
1051 if (surface->output_mask & (1 << output->id))
1052 weston_output_schedule_repaint(output);
1053}
1054
1055WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056weston_view_schedule_repaint(struct weston_view *view)
1057{
1058 struct weston_output *output;
1059
1060 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1061 if (view->output_mask & (1 << output->id))
1062 weston_output_schedule_repaint(output);
1063}
1064
1065WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001066weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001067{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001068 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069 0, 0, surface->width,
1070 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001071
Kristian Høgsberg98238702012-08-03 16:29:12 -04001072 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001073}
1074
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001075WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001076weston_view_configure(struct weston_view *view,
1077 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001078{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079 view->geometry.x = x;
1080 view->geometry.y = y;
1081 view->geometry.width = width;
1082 view->geometry.height = height;
1083 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001084}
1085
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001086WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 view->geometry.x = x;
1090 view->geometry.y = y;
1091 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001092}
1093
Pekka Paalanen483243f2013-03-08 14:56:50 +02001094static void
1095transform_parent_handle_parent_destroy(struct wl_listener *listener,
1096 void *data)
1097{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 struct weston_view *view =
1099 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001100 geometry.parent_destroy_listener);
1101
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001103}
1104
1105WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106weston_view_set_transform_parent(struct weston_view *view,
1107 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001108{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 if (view->geometry.parent) {
1110 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1111 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001112 }
1113
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001115
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001117 transform_parent_handle_parent_destroy;
1118 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001119 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001121 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001123 }
1124
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 weston_view_geometry_dirty(view);
1126}
1127
1128WL_EXPORT int
1129weston_view_is_mapped(struct weston_view *view)
1130{
1131 if (view->output)
1132 return 1;
1133 else
1134 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001135}
1136
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001137WL_EXPORT int
1138weston_surface_is_mapped(struct weston_surface *surface)
1139{
1140 if (surface->output)
1141 return 1;
1142 else
1143 return 0;
1144}
1145
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001146WL_EXPORT int32_t
1147weston_surface_buffer_width(struct weston_surface *surface)
1148{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001149 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001150 switch (surface->buffer_transform) {
1151 case WL_OUTPUT_TRANSFORM_90:
1152 case WL_OUTPUT_TRANSFORM_270:
1153 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1154 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001155 width = surface->buffer_ref.buffer->height;
1156 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001157 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001158 width = surface->buffer_ref.buffer->width;
1159 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001160 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001161 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001162}
1163
1164WL_EXPORT int32_t
1165weston_surface_buffer_height(struct weston_surface *surface)
1166{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001167 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001168 switch (surface->buffer_transform) {
1169 case WL_OUTPUT_TRANSFORM_90:
1170 case WL_OUTPUT_TRANSFORM_270:
1171 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1172 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001173 height = surface->buffer_ref.buffer->width;
1174 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001175 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001176 height = surface->buffer_ref.buffer->height;
1177 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001178 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001179 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001180}
1181
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001182WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001183weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001184{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001185 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001186
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001187 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001188
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001189 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001190}
1191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192WL_EXPORT struct weston_view *
1193weston_compositor_pick_view(struct weston_compositor *compositor,
1194 wl_fixed_t x, wl_fixed_t y,
1195 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001196{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 wl_list_for_each(view, &compositor->view_list, link) {
1200 weston_view_from_global_fixed(view, x, y, vx, vy);
1201 if (pixman_region32_contains_point(&view->surface->input,
1202 wl_fixed_to_int(*vx),
1203 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001204 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001206 }
1207
1208 return NULL;
1209}
1210
1211static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001212weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001213{
Daniel Stone37816df2012-05-16 18:45:18 +01001214 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001215
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001216 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001217 return;
1218
Daniel Stone37816df2012-05-16 18:45:18 +01001219 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001220 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001221}
1222
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001223WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001225{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001226 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001227
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228 if (!weston_view_is_mapped(view))
1229 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 weston_view_damage_below(view);
1232 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001233 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234 wl_list_remove(&view->layer_link);
1235 wl_list_init(&view->layer_link);
1236 wl_list_remove(&view->link);
1237 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 view->output_mask = 0;
1239 weston_surface_assign_output(view->surface);
1240
1241 if (weston_surface_is_mapped(view->surface))
1242 return;
1243
1244 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1245 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001246 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001248 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001249 NULL,
1250 wl_fixed_from_int(0),
1251 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001253 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001254 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001256
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257WL_EXPORT void
1258weston_surface_unmap(struct weston_surface *surface)
1259{
1260 struct weston_view *view;
1261
1262 wl_list_for_each(view, &surface->views, surface_link)
1263 weston_view_unmap(view);
1264 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001265}
1266
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001267struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001268 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001269 struct wl_list link;
1270};
1271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272WL_EXPORT void
1273weston_view_destroy(struct weston_view *view)
1274{
1275 wl_signal_emit(&view->destroy_signal, view);
1276
1277 assert(wl_list_empty(&view->geometry.child_list));
1278
1279 if (weston_view_is_mapped(view)) {
1280 weston_view_unmap(view);
1281 weston_compositor_build_view_list(view->surface->compositor);
1282 }
1283
1284 wl_list_remove(&view->link);
1285 wl_list_remove(&view->layer_link);
1286
1287 pixman_region32_fini(&view->clip);
1288 pixman_region32_fini(&view->transform.boundingbox);
1289
1290 weston_view_set_transform_parent(view, NULL);
1291
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 wl_list_remove(&view->surface_link);
1293
1294 free(view);
1295}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001296
1297WL_EXPORT void
1298weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001299{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001300 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001301 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001302
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001303 if (--surface->ref_count > 0)
1304 return;
1305
1306 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1307
Pekka Paalanene67858b2013-04-25 13:57:42 +03001308 assert(wl_list_empty(&surface->subsurface_list_pending));
1309 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1312 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001313
Pekka Paalanenbc106382012-10-10 12:49:31 +03001314 wl_list_for_each_safe(cb, next,
1315 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001316 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001317
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001318 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001319 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001320 pixman_region32_fini(&surface->pending.damage);
1321
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001322 if (surface->pending.buffer)
1323 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1324
Pekka Paalanende685b82012-12-04 15:58:12 +02001325 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001326
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001327 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001328 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001329 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001330
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001331 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001332 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001333
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001334 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001335}
1336
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001337static void
1338destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001339{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001340 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001341
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001342 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001343}
1344
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001345static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001346weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1347{
1348 struct weston_buffer *buffer =
1349 container_of(listener, struct weston_buffer, destroy_listener);
1350
1351 wl_signal_emit(&buffer->destroy_signal, buffer);
1352 free(buffer);
1353}
1354
1355struct weston_buffer *
1356weston_buffer_from_resource(struct wl_resource *resource)
1357{
1358 struct weston_buffer *buffer;
1359 struct wl_listener *listener;
1360
1361 listener = wl_resource_get_destroy_listener(resource,
1362 weston_buffer_destroy_handler);
1363
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001364 if (listener)
1365 return container_of(listener, struct weston_buffer,
1366 destroy_listener);
1367
1368 buffer = zalloc(sizeof *buffer);
1369 if (buffer == NULL)
1370 return NULL;
1371
1372 buffer->resource = resource;
1373 wl_signal_init(&buffer->destroy_signal);
1374 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001375 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001376 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001377
1378 return buffer;
1379}
1380
1381static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001382weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1383 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001384{
Pekka Paalanende685b82012-12-04 15:58:12 +02001385 struct weston_buffer_reference *ref =
1386 container_of(listener, struct weston_buffer_reference,
1387 destroy_listener);
1388
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001389 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001390 ref->buffer = NULL;
1391}
1392
1393WL_EXPORT void
1394weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001395 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001396{
1397 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001398 ref->buffer->busy_count--;
1399 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001400 assert(wl_resource_get_client(ref->buffer->resource));
1401 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001402 WL_BUFFER_RELEASE);
1403 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001404 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001405 }
1406
Pekka Paalanende685b82012-12-04 15:58:12 +02001407 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001408 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001409 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001410 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001411 }
1412
Pekka Paalanende685b82012-12-04 15:58:12 +02001413 ref->buffer = buffer;
1414 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1415}
1416
1417static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001418weston_surface_attach(struct weston_surface *surface,
1419 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001420{
1421 weston_buffer_reference(&surface->buffer_ref, buffer);
1422
Pekka Paalanena6421c42012-12-04 15:58:10 +02001423 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001424 if (weston_surface_is_mapped(surface))
1425 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001426 }
1427
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001428 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001429}
1430
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001431WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001434 wl_list_remove(&view->layer_link);
1435 wl_list_insert(below, &view->layer_link);
1436 weston_view_damage_below(view);
1437 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001438}
1439
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001440WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001441weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001442{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001443 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001444
1445 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001446 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001447}
1448
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001449WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001450weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001451{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001452 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001453
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001454 pixman_region32_union(&compositor->primary_plane.damage,
1455 &compositor->primary_plane.damage,
1456 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001457 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001458}
1459
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001460static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001462{
Pekka Paalanende685b82012-12-04 15:58:12 +02001463 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001464 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001465 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001466
Jason Ekstranda7af7042013-10-12 22:38:11 -05001467 empty_region(&surface->damage);
1468}
1469
1470static void
1471view_accumulate_damage(struct weston_view *view,
1472 pixman_region32_t *opaque)
1473{
1474 pixman_region32_t damage;
1475
1476 pixman_region32_init(&damage);
1477 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001478 pixman_box32_t *extents;
1479
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480 extents = pixman_region32_extents(&view->surface->damage);
1481 view_compute_bbox(view, extents->x1, extents->y1,
1482 extents->x2 - extents->x1,
1483 extents->y2 - extents->y1,
1484 &damage);
1485 pixman_region32_translate(&damage,
1486 -view->plane->x,
1487 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001488 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001489 pixman_region32_copy(&damage, &view->surface->damage);
1490 pixman_region32_translate(&damage,
1491 view->geometry.x - view->plane->x,
1492 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001493 }
1494
Jason Ekstranda7af7042013-10-12 22:38:11 -05001495 pixman_region32_subtract(&damage, &damage, opaque);
1496 pixman_region32_union(&view->plane->damage,
1497 &view->plane->damage, &damage);
1498 pixman_region32_fini(&damage);
1499 pixman_region32_copy(&view->clip, opaque);
1500 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001501}
1502
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001503static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001504compositor_accumulate_damage(struct weston_compositor *ec)
1505{
1506 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001507 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001508 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001509
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001510 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001511
1512 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001513 pixman_region32_copy(&plane->clip, &clip);
1514
1515 pixman_region32_init(&opaque);
1516
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517 wl_list_for_each(ev, &ec->view_list, link) {
1518 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001519 continue;
1520
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001522 }
1523
1524 pixman_region32_union(&clip, &clip, &opaque);
1525 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001526 }
1527
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001528 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001529
Jason Ekstranda7af7042013-10-12 22:38:11 -05001530 wl_list_for_each(ev, &ec->view_list, link)
1531 ev->surface->touched = 0;
1532
1533 wl_list_for_each(ev, &ec->view_list, link) {
1534 if (ev->surface->touched)
1535 continue;
1536 ev->surface->touched = 1;
1537
1538 surface_flush_damage(ev->surface);
1539
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001540 /* Both the renderer and the backend have seen the buffer
1541 * by now. If renderer needs the buffer, it has its own
1542 * reference set. If the backend wants to keep the buffer
1543 * around for migrating the surface into a non-primary plane
1544 * later, keep_buffer is true. Otherwise, drop the core
1545 * reference now, and allow early buffer release. This enables
1546 * clients to use single-buffering.
1547 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 if (!ev->surface->keep_buffer)
1549 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001550 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001551}
1552
1553static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001555{
1556 struct weston_subsurface *sub;
1557
Pekka Paalanene67858b2013-04-25 13:57:42 +03001558 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001559 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001560 continue;
1561
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1563 wl_list_init(&sub->surface->views);
1564
1565 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001566 }
1567}
1568
1569static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001571{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 struct weston_subsurface *sub;
1573 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1576 if (sub->surface == surface)
1577 continue;
1578
1579 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1580 weston_view_destroy(view);
1581
1582 surface_free_unused_subsurface_views(sub->surface);
1583 }
1584}
1585
1586static void
1587view_list_add_subsurface_view(struct weston_compositor *compositor,
1588 struct weston_subsurface *sub,
1589 struct weston_view *parent)
1590{
1591 struct weston_subsurface *child;
1592 struct weston_view *view = NULL, *iv;
1593
1594 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1595 if (iv->geometry.parent == parent) {
1596 view = iv;
1597 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001598 }
1599 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600
1601 if (view) {
1602 /* Put it back in the surface's list of views */
1603 wl_list_remove(&view->surface_link);
1604 wl_list_insert(&sub->surface->views, &view->surface_link);
1605 } else {
1606 view = weston_view_create(sub->surface);
1607 weston_view_configure(view,
1608 sub->position.x,
1609 sub->position.y,
1610 sub->surface->width,
1611 sub->surface->height);
1612 weston_view_set_transform_parent(view, parent);
1613 }
1614
1615 weston_view_update_transform(view);
1616 wl_list_insert(compositor->view_list.next, &view->link);
1617
1618 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
1619 if (child->surface != sub->surface)
1620 view_list_add_subsurface_view(compositor, child, view);
1621}
1622
1623static void
1624view_list_add(struct weston_compositor *compositor,
1625 struct weston_view *view)
1626{
1627 struct weston_subsurface *sub;
1628
1629 weston_view_update_transform(view);
1630 wl_list_insert(compositor->view_list.prev, &view->link);
1631
1632 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link)
1633 if (sub->surface != view->surface)
1634 view_list_add_subsurface_view(compositor, sub, view);
1635}
1636
1637static void
1638weston_compositor_build_view_list(struct weston_compositor *compositor)
1639{
1640 struct weston_view *view;
1641 struct weston_layer *layer;
1642
1643 wl_list_for_each(layer, &compositor->layer_list, link)
1644 wl_list_for_each(view, &layer->view_list, layer_link)
1645 surface_stash_subsurface_views(view->surface);
1646
1647 wl_list_init(&compositor->view_list);
1648 wl_list_for_each(layer, &compositor->layer_list, link) {
1649 wl_list_for_each(view, &layer->view_list, layer_link) {
1650 view_list_add(compositor, view);
1651 }
1652 }
1653
1654 wl_list_for_each(layer, &compositor->layer_list, link)
1655 wl_list_for_each(view, &layer->view_list, layer_link)
1656 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001657}
1658
David Herrmann1edf44c2013-10-22 17:11:26 +02001659static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001660weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001661{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001662 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001663 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001664 struct weston_animation *animation, *next;
1665 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001666 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001667 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001668 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001669
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001670 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001672
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001673 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001674 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001675 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 wl_list_for_each(ev, &ec->view_list, link)
1677 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001678
Pekka Paalanene67858b2013-04-25 13:57:42 +03001679 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680 wl_list_for_each(ev, &ec->view_list, link) {
1681 /* Note: This operation is safe to do multiple times on the
1682 * same surface.
1683 */
1684 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001685 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686 &ev->surface->frame_callback_list);
1687 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001688 }
1689 }
1690
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001691 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001692
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001693 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001694 pixman_region32_intersect(&output_damage,
1695 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001696 pixman_region32_subtract(&output_damage,
1697 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001698
Scott Moreauccbf29d2012-02-22 14:21:41 -07001699 if (output->dirty)
1700 weston_output_update_matrix(output);
1701
David Herrmann1edf44c2013-10-22 17:11:26 +02001702 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001703
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001704 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001705
Kristian Høgsbergef044142011-06-21 15:02:12 -04001706 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001707
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001708 weston_compositor_repick(ec);
1709 wl_event_loop_dispatch(ec->input_loop, 0);
1710
Jonas Ådahldb773762012-06-13 00:01:21 +02001711 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001712 wl_callback_send_done(cb->resource, msecs);
1713 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001714 }
1715
Scott Moreaud64cf212012-06-08 19:40:54 -06001716 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001717 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001718 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001719 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001720
1721 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001722}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001723
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001724static int
1725weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001726{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001727 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001728
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001729 wl_event_loop_dispatch(compositor->input_loop, 0);
1730
1731 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001732}
1733
1734WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001735weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001736{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001737 struct weston_compositor *compositor = output->compositor;
1738 struct wl_event_loop *loop =
1739 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001740 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001741
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001742 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001743
1744 if (output->repaint_needed &&
1745 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1746 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001747 r = weston_output_repaint(output, msecs);
1748 if (!r)
1749 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001750 }
1751
1752 output->repaint_scheduled = 0;
1753 if (compositor->input_loop_source)
1754 return;
1755
1756 fd = wl_event_loop_get_fd(compositor->input_loop);
1757 compositor->input_loop_source =
1758 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1759 weston_compositor_read_input, compositor);
1760}
1761
1762static void
1763idle_repaint(void *data)
1764{
1765 struct weston_output *output = data;
1766
Jonas Ådahle5a12252013-04-05 23:07:11 +02001767 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001768}
1769
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001770WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001771weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1772{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001773 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001774 if (below != NULL)
1775 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001776}
1777
1778WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001779weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001780{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001781 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001782 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001783
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001784 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1785 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001786 return;
1787
Kristian Høgsbergef044142011-06-21 15:02:12 -04001788 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001789 output->repaint_needed = 1;
1790 if (output->repaint_scheduled)
1791 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001792
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001793 wl_event_loop_add_idle(loop, idle_repaint, output);
1794 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001795
1796 if (compositor->input_loop_source) {
1797 wl_event_source_remove(compositor->input_loop_source);
1798 compositor->input_loop_source = NULL;
1799 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001800}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001801
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001802WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001803weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1804{
1805 struct weston_output *output;
1806
1807 wl_list_for_each(output, &compositor->output_list, link)
1808 weston_output_schedule_repaint(output);
1809}
1810
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001811static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001812surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001813{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001814 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001815}
1816
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001817static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001818surface_attach(struct wl_client *client,
1819 struct wl_resource *resource,
1820 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1821{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001822 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001823 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001824
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001825 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001826 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001827 if (buffer == NULL) {
1828 wl_client_post_no_memory(client);
1829 return;
1830 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001831 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001832
Pekka Paalanende685b82012-12-04 15:58:12 +02001833 /* Attach, attach, without commit in between does not send
1834 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001835 if (surface->pending.buffer)
1836 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001837
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001838 surface->pending.sx = sx;
1839 surface->pending.sy = sy;
1840 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001841 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001842 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001843 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001844 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001845 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001846}
1847
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001848static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001849surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001850 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001851 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001852{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001853 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001854
Pekka Paalanen8e159182012-10-10 12:49:25 +03001855 pixman_region32_union_rect(&surface->pending.damage,
1856 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001857 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001858}
1859
Kristian Høgsberg33418202011-08-16 23:01:28 -04001860static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001861destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001862{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001863 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001864
1865 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001866 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001867}
1868
1869static void
1870surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001871 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001872{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001873 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001874 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001875
1876 cb = malloc(sizeof *cb);
1877 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001878 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001879 return;
1880 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001881
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001882 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1883 callback);
1884 if (cb->resource == NULL) {
1885 free(cb);
1886 wl_resource_post_no_memory(resource);
1887 return;
1888 }
1889
Jason Ekstranda85118c2013-06-27 20:17:02 -05001890 wl_resource_set_implementation(cb->resource, NULL, cb,
1891 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001892
Pekka Paalanenbc106382012-10-10 12:49:31 +03001893 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001894}
1895
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001896static void
1897surface_set_opaque_region(struct wl_client *client,
1898 struct wl_resource *resource,
1899 struct wl_resource *region_resource)
1900{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001901 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001902 struct weston_region *region;
1903
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001904 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001905 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001906 pixman_region32_copy(&surface->pending.opaque,
1907 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001908 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001909 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001910 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001911}
1912
1913static void
1914surface_set_input_region(struct wl_client *client,
1915 struct wl_resource *resource,
1916 struct wl_resource *region_resource)
1917{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001918 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001919 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001920
1921 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001922 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001923 pixman_region32_copy(&surface->pending.input,
1924 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001925 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001926 pixman_region32_fini(&surface->pending.input);
1927 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001928 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001929}
1930
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001931static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001932weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001933{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001934 struct weston_subsurface *sub;
1935
1936 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1937 parent_link_pending) {
1938 wl_list_remove(&sub->parent_link);
1939 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1940 }
1941}
1942
1943static void
1944weston_surface_commit(struct weston_surface *surface)
1945{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001947 pixman_region32_t opaque;
1948
Alexander Larsson4ea95522013-05-22 14:41:37 +02001949 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001950 surface->buffer_transform = surface->pending.buffer_transform;
1951
Alexander Larsson4ea95522013-05-22 14:41:37 +02001952 /* wl_surface.set_buffer_scale */
1953 surface->buffer_scale = surface->pending.buffer_scale;
1954
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001955 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001956 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001957 weston_surface_attach(surface, surface->pending.buffer);
1958
Jason Ekstranda7af7042013-10-12 22:38:11 -05001959 surface->width = 0;
1960 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001961 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001962 /* This already includes the buffer scale */
1963 surface->width = weston_surface_buffer_width(surface);
1964 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001965 }
1966
1967 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001968 surface->configure(surface,
1969 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001970 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001971
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001972 if (surface->pending.buffer)
1973 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1974 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001975 surface->pending.sx = 0;
1976 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001977 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001978
1979 /* wl_surface.damage */
1980 pixman_region32_union(&surface->damage, &surface->damage,
1981 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001982 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1983 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001984 surface->width,
1985 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001986 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001987
1988 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001989 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001990 surface->width,
1991 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001992 pixman_region32_intersect(&opaque,
1993 &opaque, &surface->pending.opaque);
1994
1995 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1996 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001997 wl_list_for_each(view, &surface->views, surface_link)
1998 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001999 }
2000
2001 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002002
2003 /* wl_surface.set_input_region */
2004 pixman_region32_fini(&surface->input);
2005 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 surface->width,
2007 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002008 pixman_region32_intersect(&surface->input,
2009 &surface->input, &surface->pending.input);
2010
Pekka Paalanenbc106382012-10-10 12:49:31 +03002011 /* wl_surface.frame */
2012 wl_list_insert_list(&surface->frame_callback_list,
2013 &surface->pending.frame_callback_list);
2014 wl_list_init(&surface->pending.frame_callback_list);
2015
Pekka Paalanene67858b2013-04-25 13:57:42 +03002016 weston_surface_commit_subsurface_order(surface);
2017
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002018 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002019}
2020
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002021static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002022weston_subsurface_commit(struct weston_subsurface *sub);
2023
2024static void
2025weston_subsurface_parent_commit(struct weston_subsurface *sub,
2026 int parent_is_synchronized);
2027
2028static void
2029surface_commit(struct wl_client *client, struct wl_resource *resource)
2030{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002031 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002032 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2033
2034 if (sub) {
2035 weston_subsurface_commit(sub);
2036 return;
2037 }
2038
2039 weston_surface_commit(surface);
2040
2041 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2042 if (sub->surface != surface)
2043 weston_subsurface_parent_commit(sub, 0);
2044 }
2045}
2046
2047static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002048surface_set_buffer_transform(struct wl_client *client,
2049 struct wl_resource *resource, int transform)
2050{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002051 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002052
2053 surface->pending.buffer_transform = transform;
2054}
2055
Alexander Larsson4ea95522013-05-22 14:41:37 +02002056static void
2057surface_set_buffer_scale(struct wl_client *client,
2058 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002059 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002060{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002061 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002062
2063 surface->pending.buffer_scale = scale;
2064}
2065
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002066static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002067 surface_destroy,
2068 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002069 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002070 surface_frame,
2071 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002072 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002073 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002074 surface_set_buffer_transform,
2075 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002076};
2077
2078static void
2079compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002080 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002081{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002082 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002083 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002084
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002085 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002086 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002087 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002088 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002089 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002090
Jason Ekstranda85118c2013-06-27 20:17:02 -05002091 surface->resource =
2092 wl_resource_create(client, &wl_surface_interface,
2093 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002094 if (surface->resource == NULL) {
2095 weston_surface_destroy(surface);
2096 wl_resource_post_no_memory(resource);
2097 return;
2098 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002099 wl_resource_set_implementation(surface->resource, &surface_interface,
2100 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002101}
2102
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002103static void
2104destroy_region(struct wl_resource *resource)
2105{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002106 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002107
2108 pixman_region32_fini(&region->region);
2109 free(region);
2110}
2111
2112static void
2113region_destroy(struct wl_client *client, struct wl_resource *resource)
2114{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002115 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002116}
2117
2118static void
2119region_add(struct wl_client *client, struct wl_resource *resource,
2120 int32_t x, int32_t y, int32_t width, int32_t height)
2121{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002122 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002123
2124 pixman_region32_union_rect(&region->region, &region->region,
2125 x, y, width, height);
2126}
2127
2128static void
2129region_subtract(struct wl_client *client, struct wl_resource *resource,
2130 int32_t x, int32_t y, int32_t width, int32_t height)
2131{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002132 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002133 pixman_region32_t rect;
2134
2135 pixman_region32_init_rect(&rect, x, y, width, height);
2136 pixman_region32_subtract(&region->region, &region->region, &rect);
2137 pixman_region32_fini(&rect);
2138}
2139
2140static const struct wl_region_interface region_interface = {
2141 region_destroy,
2142 region_add,
2143 region_subtract
2144};
2145
2146static void
2147compositor_create_region(struct wl_client *client,
2148 struct wl_resource *resource, uint32_t id)
2149{
2150 struct weston_region *region;
2151
2152 region = malloc(sizeof *region);
2153 if (region == NULL) {
2154 wl_resource_post_no_memory(resource);
2155 return;
2156 }
2157
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002158 pixman_region32_init(&region->region);
2159
Jason Ekstranda85118c2013-06-27 20:17:02 -05002160 region->resource =
2161 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002162 if (region->resource == NULL) {
2163 free(region);
2164 wl_resource_post_no_memory(resource);
2165 return;
2166 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002167 wl_resource_set_implementation(region->resource, &region_interface,
2168 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002169}
2170
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002171static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002172 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002173 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002174};
2175
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002176static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002177weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2178{
2179 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002180 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002181 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182
Alexander Larsson4ea95522013-05-22 14:41:37 +02002183 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002184 surface->buffer_transform = sub->cached.buffer_transform;
2185
Alexander Larsson4ea95522013-05-22 14:41:37 +02002186 /* wl_surface.set_buffer_scale */
2187 surface->buffer_scale = sub->cached.buffer_scale;
2188
Pekka Paalanene67858b2013-04-25 13:57:42 +03002189 /* wl_surface.attach */
2190 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2191 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2192 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2193
Jason Ekstranda7af7042013-10-12 22:38:11 -05002194 surface->width = 0;
2195 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002196 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002197 surface->width = weston_surface_buffer_width(surface);
2198 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002199 }
2200
2201 if (surface->configure && sub->cached.newly_attached)
2202 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002203 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002204 sub->cached.sx = 0;
2205 sub->cached.sy = 0;
2206 sub->cached.newly_attached = 0;
2207
2208 /* wl_surface.damage */
2209 pixman_region32_union(&surface->damage, &surface->damage,
2210 &sub->cached.damage);
2211 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2212 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002213 surface->width,
2214 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002215 empty_region(&sub->cached.damage);
2216
2217 /* wl_surface.set_opaque_region */
2218 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002219 surface->width,
2220 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002221 pixman_region32_intersect(&opaque,
2222 &opaque, &sub->cached.opaque);
2223
2224 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2225 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002226 wl_list_for_each(view, &surface->views, surface_link)
2227 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002228 }
2229
2230 pixman_region32_fini(&opaque);
2231
2232 /* wl_surface.set_input_region */
2233 pixman_region32_fini(&surface->input);
2234 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002235 surface->width,
2236 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237 pixman_region32_intersect(&surface->input,
2238 &surface->input, &sub->cached.input);
2239
2240 /* wl_surface.frame */
2241 wl_list_insert_list(&surface->frame_callback_list,
2242 &sub->cached.frame_callback_list);
2243 wl_list_init(&sub->cached.frame_callback_list);
2244
2245 weston_surface_commit_subsurface_order(surface);
2246
2247 weston_surface_schedule_repaint(surface);
2248
2249 sub->cached.has_data = 0;
2250}
2251
2252static void
2253weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2254{
2255 struct weston_surface *surface = sub->surface;
2256
2257 /*
2258 * If this commit would cause the surface to move by the
2259 * attach(dx, dy) parameters, the old damage region must be
2260 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002261 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002262 */
2263 pixman_region32_translate(&sub->cached.damage,
2264 -surface->pending.sx, -surface->pending.sy);
2265 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2266 &surface->pending.damage);
2267 empty_region(&surface->pending.damage);
2268
2269 if (surface->pending.newly_attached) {
2270 sub->cached.newly_attached = 1;
2271 weston_buffer_reference(&sub->cached.buffer_ref,
2272 surface->pending.buffer);
2273 }
2274 sub->cached.sx += surface->pending.sx;
2275 sub->cached.sy += surface->pending.sy;
2276 surface->pending.sx = 0;
2277 surface->pending.sy = 0;
2278 surface->pending.newly_attached = 0;
2279
2280 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002281 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002282
2283 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2284
2285 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2286
2287 wl_list_insert_list(&sub->cached.frame_callback_list,
2288 &surface->pending.frame_callback_list);
2289 wl_list_init(&surface->pending.frame_callback_list);
2290
2291 sub->cached.has_data = 1;
2292}
2293
2294static int
2295weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2296{
2297 while (sub) {
2298 if (sub->synchronized)
2299 return 1;
2300
2301 if (!sub->parent)
2302 return 0;
2303
2304 sub = weston_surface_to_subsurface(sub->parent);
2305 }
2306
2307 return 0;
2308}
2309
2310static void
2311weston_subsurface_commit(struct weston_subsurface *sub)
2312{
2313 struct weston_surface *surface = sub->surface;
2314 struct weston_subsurface *tmp;
2315
2316 /* Recursive check for effectively synchronized. */
2317 if (weston_subsurface_is_synchronized(sub)) {
2318 weston_subsurface_commit_to_cache(sub);
2319 } else {
2320 if (sub->cached.has_data) {
2321 /* flush accumulated state from cache */
2322 weston_subsurface_commit_to_cache(sub);
2323 weston_subsurface_commit_from_cache(sub);
2324 } else {
2325 weston_surface_commit(surface);
2326 }
2327
2328 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2329 if (tmp->surface != surface)
2330 weston_subsurface_parent_commit(tmp, 0);
2331 }
2332 }
2333}
2334
2335static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002336weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002337{
2338 struct weston_surface *surface = sub->surface;
2339 struct weston_subsurface *tmp;
2340
Pekka Paalanene67858b2013-04-25 13:57:42 +03002341 /* From now on, commit_from_cache the whole sub-tree, regardless of
2342 * the synchronized mode of each child. This sub-surface or some
2343 * of its ancestors were synchronized, so we are synchronized
2344 * all the way down.
2345 */
2346
2347 if (sub->cached.has_data)
2348 weston_subsurface_commit_from_cache(sub);
2349
2350 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2351 if (tmp->surface != surface)
2352 weston_subsurface_parent_commit(tmp, 1);
2353 }
2354}
2355
2356static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002357weston_subsurface_parent_commit(struct weston_subsurface *sub,
2358 int parent_is_synchronized)
2359{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002360 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002361 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362 wl_list_for_each(view, &sub->surface->views, surface_link)
2363 weston_view_set_position(view,
2364 sub->position.x,
2365 sub->position.y);
2366
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002367 sub->position.set = 0;
2368 }
2369
2370 if (parent_is_synchronized || sub->synchronized)
2371 weston_subsurface_synchronized_commit(sub);
2372}
2373
2374static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002375subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2376 int32_t width, int32_t height)
2377{
2378 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002379 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002380
Jason Ekstranda7af7042013-10-12 22:38:11 -05002381 wl_list_for_each(view, &surface->views, surface_link)
2382 weston_view_configure(view,
2383 view->geometry.x + dx,
2384 view->geometry.y + dy,
2385 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002386
2387 /* No need to check parent mappedness, because if parent is not
2388 * mapped, parent is not in a visible layer, so this sub-surface
2389 * will not be drawn either.
2390 */
2391 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002392 /* Cannot call weston_surface_update_transform(),
2393 * because that would call it also for the parent surface,
2394 * which might not be mapped yet. That would lead to
2395 * inconsistent state, where the window could never be
2396 * mapped.
2397 *
2398 * Instead just assing any output, to make
2399 * weston_surface_is_mapped() return true, so that when the
2400 * parent surface does get mapped, this one will get
2401 * included, too. See surface_list_add().
2402 */
2403 assert(!wl_list_empty(&compositor->output_list));
2404 surface->output = container_of(compositor->output_list.next,
2405 struct weston_output, link);
2406 }
2407}
2408
2409static struct weston_subsurface *
2410weston_surface_to_subsurface(struct weston_surface *surface)
2411{
2412 if (surface->configure == subsurface_configure)
2413 return surface->configure_private;
2414
2415 return NULL;
2416}
2417
Pekka Paalanen01388e22013-04-25 13:57:44 +03002418WL_EXPORT struct weston_surface *
2419weston_surface_get_main_surface(struct weston_surface *surface)
2420{
2421 struct weston_subsurface *sub;
2422
2423 while (surface && (sub = weston_surface_to_subsurface(surface)))
2424 surface = sub->parent;
2425
2426 return surface;
2427}
2428
Pekka Paalanene67858b2013-04-25 13:57:42 +03002429static void
2430subsurface_set_position(struct wl_client *client,
2431 struct wl_resource *resource, int32_t x, int32_t y)
2432{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002433 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002434
2435 if (!sub)
2436 return;
2437
2438 sub->position.x = x;
2439 sub->position.y = y;
2440 sub->position.set = 1;
2441}
2442
2443static struct weston_subsurface *
2444subsurface_from_surface(struct weston_surface *surface)
2445{
2446 struct weston_subsurface *sub;
2447
2448 sub = weston_surface_to_subsurface(surface);
2449 if (sub)
2450 return sub;
2451
2452 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2453 if (sub->surface == surface)
2454 return sub;
2455
2456 return NULL;
2457}
2458
2459static struct weston_subsurface *
2460subsurface_sibling_check(struct weston_subsurface *sub,
2461 struct weston_surface *surface,
2462 const char *request)
2463{
2464 struct weston_subsurface *sibling;
2465
2466 sibling = subsurface_from_surface(surface);
2467
2468 if (!sibling) {
2469 wl_resource_post_error(sub->resource,
2470 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2471 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002472 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473 return NULL;
2474 }
2475
2476 if (sibling->parent != sub->parent) {
2477 wl_resource_post_error(sub->resource,
2478 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2479 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002480 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002481 return NULL;
2482 }
2483
2484 return sibling;
2485}
2486
2487static void
2488subsurface_place_above(struct wl_client *client,
2489 struct wl_resource *resource,
2490 struct wl_resource *sibling_resource)
2491{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002492 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002493 struct weston_surface *surface =
2494 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002495 struct weston_subsurface *sibling;
2496
2497 if (!sub)
2498 return;
2499
2500 sibling = subsurface_sibling_check(sub, surface, "place_above");
2501 if (!sibling)
2502 return;
2503
2504 wl_list_remove(&sub->parent_link_pending);
2505 wl_list_insert(sibling->parent_link_pending.prev,
2506 &sub->parent_link_pending);
2507}
2508
2509static void
2510subsurface_place_below(struct wl_client *client,
2511 struct wl_resource *resource,
2512 struct wl_resource *sibling_resource)
2513{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002514 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002515 struct weston_surface *surface =
2516 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002517 struct weston_subsurface *sibling;
2518
2519 if (!sub)
2520 return;
2521
2522 sibling = subsurface_sibling_check(sub, surface, "place_below");
2523 if (!sibling)
2524 return;
2525
2526 wl_list_remove(&sub->parent_link_pending);
2527 wl_list_insert(&sibling->parent_link_pending,
2528 &sub->parent_link_pending);
2529}
2530
2531static void
2532subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2533{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002534 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002535
2536 if (sub)
2537 sub->synchronized = 1;
2538}
2539
2540static void
2541subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2542{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002543 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002544
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002545 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002546 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002547
2548 /* If sub became effectively desynchronized, flush. */
2549 if (!weston_subsurface_is_synchronized(sub))
2550 weston_subsurface_synchronized_commit(sub);
2551 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002552}
2553
2554static void
2555weston_subsurface_cache_init(struct weston_subsurface *sub)
2556{
2557 pixman_region32_init(&sub->cached.damage);
2558 pixman_region32_init(&sub->cached.opaque);
2559 pixman_region32_init(&sub->cached.input);
2560 wl_list_init(&sub->cached.frame_callback_list);
2561 sub->cached.buffer_ref.buffer = NULL;
2562}
2563
2564static void
2565weston_subsurface_cache_fini(struct weston_subsurface *sub)
2566{
2567 struct weston_frame_callback *cb, *tmp;
2568
2569 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002570 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002571
2572 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2573 pixman_region32_fini(&sub->cached.damage);
2574 pixman_region32_fini(&sub->cached.opaque);
2575 pixman_region32_fini(&sub->cached.input);
2576}
2577
2578static void
2579weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2580{
2581 wl_list_remove(&sub->parent_link);
2582 wl_list_remove(&sub->parent_link_pending);
2583 wl_list_remove(&sub->parent_destroy_listener.link);
2584 sub->parent = NULL;
2585}
2586
2587static void
2588weston_subsurface_destroy(struct weston_subsurface *sub);
2589
2590static void
2591subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2592{
2593 struct weston_subsurface *sub =
2594 container_of(listener, struct weston_subsurface,
2595 surface_destroy_listener);
2596 assert(data == &sub->surface->resource);
2597
2598 /* The protocol object (wl_resource) is left inert. */
2599 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002600 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002601
2602 weston_subsurface_destroy(sub);
2603}
2604
2605static void
2606subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2607{
2608 struct weston_subsurface *sub =
2609 container_of(listener, struct weston_subsurface,
2610 parent_destroy_listener);
2611 assert(data == &sub->parent->resource);
2612 assert(sub->surface != sub->parent);
2613
2614 if (weston_surface_is_mapped(sub->surface))
2615 weston_surface_unmap(sub->surface);
2616
2617 weston_subsurface_unlink_parent(sub);
2618}
2619
2620static void
2621subsurface_resource_destroy(struct wl_resource *resource)
2622{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002623 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002624
2625 if (sub)
2626 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002627}
2628
2629static void
2630subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2631{
2632 wl_resource_destroy(resource);
2633}
2634
2635static void
2636weston_subsurface_link_parent(struct weston_subsurface *sub,
2637 struct weston_surface *parent)
2638{
2639 sub->parent = parent;
2640 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002641 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002642 &sub->parent_destroy_listener);
2643
2644 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2645 wl_list_insert(&parent->subsurface_list_pending,
2646 &sub->parent_link_pending);
2647}
2648
2649static void
2650weston_subsurface_link_surface(struct weston_subsurface *sub,
2651 struct weston_surface *surface)
2652{
2653 sub->surface = surface;
2654 sub->surface_destroy_listener.notify =
2655 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002656 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002657 &sub->surface_destroy_listener);
2658}
2659
2660static void
2661weston_subsurface_destroy(struct weston_subsurface *sub)
2662{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002663 struct weston_view *view, *next;
2664
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665 assert(sub->surface);
2666
2667 if (sub->resource) {
2668 assert(weston_surface_to_subsurface(sub->surface) == sub);
2669 assert(sub->parent_destroy_listener.notify ==
2670 subsurface_handle_parent_destroy);
2671
Jason Ekstranda7af7042013-10-12 22:38:11 -05002672 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2673 weston_view_destroy(view);
2674
Pekka Paalanene67858b2013-04-25 13:57:42 +03002675 if (sub->parent)
2676 weston_subsurface_unlink_parent(sub);
2677
2678 weston_subsurface_cache_fini(sub);
2679
2680 sub->surface->configure = NULL;
2681 sub->surface->configure_private = NULL;
2682 } else {
2683 /* the dummy weston_subsurface for the parent itself */
2684 assert(sub->parent_destroy_listener.notify == NULL);
2685 wl_list_remove(&sub->parent_link);
2686 wl_list_remove(&sub->parent_link_pending);
2687 }
2688
2689 wl_list_remove(&sub->surface_destroy_listener.link);
2690 free(sub);
2691}
2692
2693static const struct wl_subsurface_interface subsurface_implementation = {
2694 subsurface_destroy,
2695 subsurface_set_position,
2696 subsurface_place_above,
2697 subsurface_place_below,
2698 subsurface_set_sync,
2699 subsurface_set_desync
2700};
2701
2702static struct weston_subsurface *
2703weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2704 struct weston_surface *parent)
2705{
2706 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002707 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002708
2709 sub = calloc(1, sizeof *sub);
2710 if (!sub)
2711 return NULL;
2712
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 wl_list_init(&sub->unused_views);
2714
Jason Ekstranda85118c2013-06-27 20:17:02 -05002715 sub->resource =
2716 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002717 if (!sub->resource) {
2718 free(sub);
2719 return NULL;
2720 }
2721
Jason Ekstranda85118c2013-06-27 20:17:02 -05002722 wl_resource_set_implementation(sub->resource,
2723 &subsurface_implementation,
2724 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725 weston_subsurface_link_surface(sub, surface);
2726 weston_subsurface_link_parent(sub, parent);
2727 weston_subsurface_cache_init(sub);
2728 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729
2730 return sub;
2731}
2732
2733/* Create a dummy subsurface for having the parent itself in its
2734 * sub-surface lists. Makes stacking order manipulation easy.
2735 */
2736static struct weston_subsurface *
2737weston_subsurface_create_for_parent(struct weston_surface *parent)
2738{
2739 struct weston_subsurface *sub;
2740
2741 sub = calloc(1, sizeof *sub);
2742 if (!sub)
2743 return NULL;
2744
2745 weston_subsurface_link_surface(sub, parent);
2746 sub->parent = parent;
2747 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2748 wl_list_insert(&parent->subsurface_list_pending,
2749 &sub->parent_link_pending);
2750
2751 return sub;
2752}
2753
2754static void
2755subcompositor_get_subsurface(struct wl_client *client,
2756 struct wl_resource *resource,
2757 uint32_t id,
2758 struct wl_resource *surface_resource,
2759 struct wl_resource *parent_resource)
2760{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002761 struct weston_surface *surface =
2762 wl_resource_get_user_data(surface_resource);
2763 struct weston_surface *parent =
2764 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002765 struct weston_subsurface *sub;
2766 static const char where[] = "get_subsurface: wl_subsurface@";
2767
2768 if (surface == parent) {
2769 wl_resource_post_error(resource,
2770 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2771 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002772 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002773 return;
2774 }
2775
2776 if (weston_surface_to_subsurface(surface)) {
2777 wl_resource_post_error(resource,
2778 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2779 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002780 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002781 return;
2782 }
2783
2784 if (surface->configure) {
2785 wl_resource_post_error(resource,
2786 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2787 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002788 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002789 return;
2790 }
2791
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002792 if (weston_surface_get_main_surface(parent) == surface) {
2793 wl_resource_post_error(resource,
2794 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2795 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002796 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002797 return;
2798 }
2799
Pekka Paalanene67858b2013-04-25 13:57:42 +03002800 /* make sure the parent is in its own list */
2801 if (wl_list_empty(&parent->subsurface_list)) {
2802 if (!weston_subsurface_create_for_parent(parent)) {
2803 wl_resource_post_no_memory(resource);
2804 return;
2805 }
2806 }
2807
2808 sub = weston_subsurface_create(id, surface, parent);
2809 if (!sub) {
2810 wl_resource_post_no_memory(resource);
2811 return;
2812 }
2813
2814 surface->configure = subsurface_configure;
2815 surface->configure_private = sub;
2816}
2817
2818static void
2819subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2820{
2821 wl_resource_destroy(resource);
2822}
2823
2824static const struct wl_subcompositor_interface subcompositor_interface = {
2825 subcompositor_destroy,
2826 subcompositor_get_subsurface
2827};
2828
2829static void
2830bind_subcompositor(struct wl_client *client,
2831 void *data, uint32_t version, uint32_t id)
2832{
2833 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002834 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002835
Jason Ekstranda85118c2013-06-27 20:17:02 -05002836 resource =
2837 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002838 if (resource == NULL) {
2839 wl_client_post_no_memory(client);
2840 return;
2841 }
2842 wl_resource_set_implementation(resource, &subcompositor_interface,
2843 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002844}
2845
2846static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002847weston_compositor_dpms(struct weston_compositor *compositor,
2848 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002849{
2850 struct weston_output *output;
2851
2852 wl_list_for_each(output, &compositor->output_list, link)
2853 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002854 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002855}
2856
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002857WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002858weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002859{
Neil Roberts8b62e202013-09-30 13:14:47 +01002860 uint32_t old_state = compositor->state;
2861
2862 /* The state needs to be changed before emitting the wake
2863 * signal because that may try to schedule a repaint which
2864 * will not work if the compositor is still sleeping */
2865 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2866
2867 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002868 case WESTON_COMPOSITOR_SLEEPING:
2869 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2870 /* fall through */
2871 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002872 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002873 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002874 /* fall through */
2875 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002876 wl_event_source_timer_update(compositor->idle_source,
2877 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002878 }
2879}
2880
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002881WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002882weston_compositor_offscreen(struct weston_compositor *compositor)
2883{
2884 switch (compositor->state) {
2885 case WESTON_COMPOSITOR_OFFSCREEN:
2886 return;
2887 case WESTON_COMPOSITOR_SLEEPING:
2888 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2889 /* fall through */
2890 default:
2891 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2892 wl_event_source_timer_update(compositor->idle_source, 0);
2893 }
2894}
2895
2896WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002897weston_compositor_sleep(struct weston_compositor *compositor)
2898{
2899 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2900 return;
2901
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002902 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002903 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2904 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2905}
2906
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002907static int
2908idle_handler(void *data)
2909{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002910 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002911
2912 if (compositor->idle_inhibit)
2913 return 1;
2914
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002915 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002916 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002917
2918 return 1;
2919}
2920
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002921WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002922weston_plane_init(struct weston_plane *plane,
2923 struct weston_compositor *ec,
2924 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002925{
2926 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002927 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002928 plane->x = x;
2929 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002930 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002931
2932 /* Init the link so that the call to wl_list_remove() when releasing
2933 * the plane without ever stacking doesn't lead to a crash */
2934 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002935}
2936
2937WL_EXPORT void
2938weston_plane_release(struct weston_plane *plane)
2939{
Xiong Zhang97116532013-10-23 13:58:31 +08002940 struct weston_view *view;
2941
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002942 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002943 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002944
Xiong Zhang97116532013-10-23 13:58:31 +08002945 wl_list_for_each(view, &plane->compositor->view_list, link) {
2946 if (view->plane == plane)
2947 view->plane = NULL;
2948 }
2949
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002950 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002951}
2952
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002953WL_EXPORT void
2954weston_compositor_stack_plane(struct weston_compositor *ec,
2955 struct weston_plane *plane,
2956 struct weston_plane *above)
2957{
2958 if (above)
2959 wl_list_insert(above->link.prev, &plane->link);
2960 else
2961 wl_list_insert(&ec->plane_list, &plane->link);
2962}
2963
Casey Dahlin9074db52012-04-19 22:50:09 -04002964static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002965{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002966 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002967}
2968
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002969static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002970bind_output(struct wl_client *client,
2971 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002972{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002973 struct weston_output *output = data;
2974 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002975 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002976
Jason Ekstranda85118c2013-06-27 20:17:02 -05002977 resource = wl_resource_create(client, &wl_output_interface,
2978 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002979 if (resource == NULL) {
2980 wl_client_post_no_memory(client);
2981 return;
2982 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002983
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002984 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002985 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002986
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002987 wl_output_send_geometry(resource,
2988 output->x,
2989 output->y,
2990 output->mm_width,
2991 output->mm_height,
2992 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002993 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002994 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002995 if (version >= 2)
2996 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002997 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002998
2999 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003000 wl_output_send_mode(resource,
3001 mode->flags,
3002 mode->width,
3003 mode->height,
3004 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003005 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003006
3007 if (version >= 2)
3008 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003009}
3010
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003011WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003012weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003013{
Richard Hughes64ddde12013-05-01 21:52:10 +01003014 wl_signal_emit(&output->destroy_signal, output);
3015
Richard Hughesafe690c2013-05-02 10:10:04 +01003016 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003017 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003018 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003019 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003020
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003021 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003022}
3023
Scott Moreau1bad5db2012-08-18 01:04:05 -06003024static void
3025weston_output_compute_transform(struct weston_output *output)
3026{
3027 struct weston_matrix transform;
3028 int flip;
3029
3030 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003031 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003032
3033 switch(output->transform) {
3034 case WL_OUTPUT_TRANSFORM_FLIPPED:
3035 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3036 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3037 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003038 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003039 flip = -1;
3040 break;
3041 default:
3042 flip = 1;
3043 break;
3044 }
3045
3046 switch(output->transform) {
3047 case WL_OUTPUT_TRANSFORM_NORMAL:
3048 case WL_OUTPUT_TRANSFORM_FLIPPED:
3049 transform.d[0] = flip;
3050 transform.d[1] = 0;
3051 transform.d[4] = 0;
3052 transform.d[5] = 1;
3053 break;
3054 case WL_OUTPUT_TRANSFORM_90:
3055 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3056 transform.d[0] = 0;
3057 transform.d[1] = -flip;
3058 transform.d[4] = 1;
3059 transform.d[5] = 0;
3060 break;
3061 case WL_OUTPUT_TRANSFORM_180:
3062 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3063 transform.d[0] = -flip;
3064 transform.d[1] = 0;
3065 transform.d[4] = 0;
3066 transform.d[5] = -1;
3067 break;
3068 case WL_OUTPUT_TRANSFORM_270:
3069 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3070 transform.d[0] = 0;
3071 transform.d[1] = flip;
3072 transform.d[4] = -1;
3073 transform.d[5] = 0;
3074 break;
3075 default:
3076 break;
3077 }
3078
3079 weston_matrix_multiply(&output->matrix, &transform);
3080}
3081
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003082WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003083weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003084{
Scott Moreau850ca422012-05-21 15:21:25 -06003085 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003086 struct weston_matrix camera;
3087 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003088
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003089 weston_matrix_init(&output->matrix);
3090 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003091 -(output->x + output->width / 2.0),
3092 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003093
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003094 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003095 2.0 / output->width,
3096 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003097
3098 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003099
Scott Moreauccbf29d2012-02-22 14:21:41 -07003100 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003101 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003102 weston_matrix_init(&camera);
3103 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003104 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003105 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003106 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003107 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003108 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003109 weston_matrix_multiply(&output->matrix, &modelview);
3110 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003111
Scott Moreauccbf29d2012-02-22 14:21:41 -07003112 output->dirty = 0;
3113}
3114
Scott Moreau1bad5db2012-08-18 01:04:05 -06003115static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003116weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003117{
3118 output->transform = transform;
3119
3120 switch (transform) {
3121 case WL_OUTPUT_TRANSFORM_90:
3122 case WL_OUTPUT_TRANSFORM_270:
3123 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3124 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3125 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003126 output->width = output->current_mode->height;
3127 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003128 break;
3129 case WL_OUTPUT_TRANSFORM_NORMAL:
3130 case WL_OUTPUT_TRANSFORM_180:
3131 case WL_OUTPUT_TRANSFORM_FLIPPED:
3132 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003133 output->width = output->current_mode->width;
3134 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003135 break;
3136 default:
3137 break;
3138 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003139
Hardening57388e42013-09-18 23:56:36 +02003140 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003141 output->width /= scale;
3142 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003143}
3144
Scott Moreauccbf29d2012-02-22 14:21:41 -07003145WL_EXPORT void
3146weston_output_move(struct weston_output *output, int x, int y)
3147{
3148 output->x = x;
3149 output->y = y;
3150
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003151 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003152 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003153 output->width,
3154 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003155}
3156
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003157WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003158weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003159 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003160 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003161{
3162 output->compositor = c;
3163 output->x = x;
3164 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003165 output->mm_width = mm_width;
3166 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003167 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003168 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003169
Alexander Larsson0b135062013-05-28 16:23:36 +02003170 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003171 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003172
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003173 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003174 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003175
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003176 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003177 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003178 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003179 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003180
Casey Dahlin58ba1372012-04-19 22:50:08 -04003181 output->id = ffs(~output->compositor->output_id_pool) - 1;
3182 output->compositor->output_id_pool |= 1 << output->id;
3183
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003184 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003185 wl_global_create(c->wl_display, &wl_output_interface, 2,
3186 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003187 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003188}
3189
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003190WL_EXPORT void
3191weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003192 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003193 wl_fixed_t *x, wl_fixed_t *y)
3194{
3195 wl_fixed_t tx, ty;
3196 wl_fixed_t width, height;
3197
Hardeningff39efa2013-09-18 23:56:35 +02003198 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3199 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003200
3201 switch(output->transform) {
3202 case WL_OUTPUT_TRANSFORM_NORMAL:
3203 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003204 tx = device_x;
3205 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003206 break;
3207 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003208 tx = device_y;
3209 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003210 break;
3211 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003212 tx = width - device_x;
3213 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003214 break;
3215 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003216 tx = width - device_y;
3217 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003218 break;
3219 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003220 tx = width - device_x;
3221 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003222 break;
3223 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003224 tx = width - device_y;
3225 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003226 break;
3227 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003228 tx = device_x;
3229 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003230 break;
3231 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003232 tx = device_y;
3233 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003234 break;
3235 }
3236
Hardeningff39efa2013-09-18 23:56:35 +02003237 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3238 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003239}
3240
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003241static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003242compositor_bind(struct wl_client *client,
3243 void *data, uint32_t version, uint32_t id)
3244{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003245 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003246 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003247
Jason Ekstranda85118c2013-06-27 20:17:02 -05003248 resource = wl_resource_create(client, &wl_compositor_interface,
3249 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003250 if (resource == NULL) {
3251 wl_client_post_no_memory(client);
3252 return;
3253 }
3254
3255 wl_resource_set_implementation(resource, &compositor_interface,
3256 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003257}
3258
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003259static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003260log_uname(void)
3261{
3262 struct utsname usys;
3263
3264 uname(&usys);
3265
3266 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3267 usys.version, usys.machine);
3268}
3269
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003270WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003271weston_environment_get_fd(const char *env)
3272{
3273 char *e, *end;
3274 int fd, flags;
3275
3276 e = getenv(env);
3277 if (!e)
3278 return -1;
3279 fd = strtol(e, &end, 0);
3280 if (*end != '\0')
3281 return -1;
3282
3283 flags = fcntl(fd, F_GETFD);
3284 if (flags == -1)
3285 return -1;
3286
3287 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3288 unsetenv(env);
3289
3290 return fd;
3291}
3292
3293WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003294weston_compositor_init(struct weston_compositor *ec,
3295 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003296 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003297 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003298{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003299 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003300 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003301 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003302
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003303 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003304 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003305 wl_signal_init(&ec->destroy_signal);
3306 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003307 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003308 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003309 wl_signal_init(&ec->idle_signal);
3310 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003311 wl_signal_init(&ec->show_input_panel_signal);
3312 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003313 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003314 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003315 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003316 wl_signal_init(&ec->session_signal);
3317 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003318
Casey Dahlin58ba1372012-04-19 22:50:08 -04003319 ec->output_id_pool = 0;
3320
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003321 if (!wl_global_create(display, &wl_compositor_interface, 3,
3322 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003323 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003324
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003325 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3326 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003327 return -1;
3328
Jason Ekstranda7af7042013-10-12 22:38:11 -05003329 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003330 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003331 wl_list_init(&ec->layer_list);
3332 wl_list_init(&ec->seat_list);
3333 wl_list_init(&ec->output_list);
3334 wl_list_init(&ec->key_binding_list);
3335 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003336 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003337 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003338 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003339
Xiong Zhang97116532013-10-23 13:58:31 +08003340 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003341 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003342
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003343 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3344 weston_config_section_get_string(s, "keymap_rules",
3345 (char **) &xkb_names.rules, NULL);
3346 weston_config_section_get_string(s, "keymap_model",
3347 (char **) &xkb_names.model, NULL);
3348 weston_config_section_get_string(s, "keymap_layout",
3349 (char **) &xkb_names.layout, NULL);
3350 weston_config_section_get_string(s, "keymap_variant",
3351 (char **) &xkb_names.variant, NULL);
3352 weston_config_section_get_string(s, "keymap_options",
3353 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003354
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003355 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3356 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003357
3358 ec->ping_handler = NULL;
3359
3360 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003361 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003362
3363 wl_data_device_manager_init(ec->wl_display);
3364
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003365 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003366
Daniel Stone725c2c32012-06-22 14:04:36 +01003367 loop = wl_display_get_event_loop(ec->wl_display);
3368 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3369 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3370
3371 ec->input_loop = wl_event_loop_create();
3372
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003373 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3374 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3375
3376 weston_compositor_schedule_repaint(ec);
3377
Daniel Stone725c2c32012-06-22 14:04:36 +01003378 return 0;
3379}
3380
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003381WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003382weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003383{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003384 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003385
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003386 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003387 if (ec->input_loop_source)
3388 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003389
Matt Roper361d2ad2011-08-29 13:52:23 -07003390 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003391 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003392 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003393
Daniel Stone325fc2d2012-05-30 16:31:58 +01003394 weston_binding_list_destroy_all(&ec->key_binding_list);
3395 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003396 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003397 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003398 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003399
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003400 weston_plane_release(&ec->primary_plane);
3401
Jonas Ådahlc97af922012-03-28 22:36:09 +02003402 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003403
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003404 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003405}
3406
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003407WL_EXPORT void
3408weston_version(int *major, int *minor, int *micro)
3409{
3410 *major = WESTON_VERSION_MAJOR;
3411 *minor = WESTON_VERSION_MINOR;
3412 *micro = WESTON_VERSION_MICRO;
3413}
3414
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003415static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003416 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003417 const char *desc;
3418} capability_strings[] = {
3419 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003420 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003421};
3422
3423static void
3424weston_compositor_log_capabilities(struct weston_compositor *compositor)
3425{
3426 unsigned i;
3427 int yes;
3428
3429 weston_log("Compositor capabilities:\n");
3430 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3431 yes = compositor->capabilities & capability_strings[i].bit;
3432 weston_log_continue(STAMP_SPACE "%s %s\n",
3433 capability_strings[i].desc,
3434 yes ? "yes" : "no");
3435 }
3436}
3437
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003438static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003439{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003440 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003441
Martin Minarik6d118362012-06-07 18:01:59 +02003442 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003443 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003444
3445 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003446}
3447
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003448#ifdef HAVE_LIBUNWIND
3449
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003450static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003451print_backtrace(void)
3452{
3453 unw_cursor_t cursor;
3454 unw_context_t context;
3455 unw_word_t off;
3456 unw_proc_info_t pip;
3457 int ret, i = 0;
3458 char procname[256];
3459 const char *filename;
3460 Dl_info dlinfo;
3461
3462 pip.unwind_info = NULL;
3463 ret = unw_getcontext(&context);
3464 if (ret) {
3465 weston_log("unw_getcontext: %d\n", ret);
3466 return;
3467 }
3468
3469 ret = unw_init_local(&cursor, &context);
3470 if (ret) {
3471 weston_log("unw_init_local: %d\n", ret);
3472 return;
3473 }
3474
3475 ret = unw_step(&cursor);
3476 while (ret > 0) {
3477 ret = unw_get_proc_info(&cursor, &pip);
3478 if (ret) {
3479 weston_log("unw_get_proc_info: %d\n", ret);
3480 break;
3481 }
3482
3483 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3484 if (ret && ret != -UNW_ENOMEM) {
3485 if (ret != -UNW_EUNSPEC)
3486 weston_log("unw_get_proc_name: %d\n", ret);
3487 procname[0] = '?';
3488 procname[1] = 0;
3489 }
3490
3491 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3492 *dlinfo.dli_fname)
3493 filename = dlinfo.dli_fname;
3494 else
3495 filename = "?";
3496
3497 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3498 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3499
3500 ret = unw_step(&cursor);
3501 if (ret < 0)
3502 weston_log("unw_step: %d\n", ret);
3503 }
3504}
3505
3506#else
3507
3508static void
3509print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003510{
3511 void *buffer[32];
3512 int i, count;
3513 Dl_info info;
3514
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003515 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3516 for (i = 0; i < count; i++) {
3517 dladdr(buffer[i], &info);
3518 weston_log(" [%016lx] %s (%s)\n",
3519 (long) buffer[i],
3520 info.dli_sname ? info.dli_sname : "--",
3521 info.dli_fname);
3522 }
3523}
3524
3525#endif
3526
3527static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003528on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003529{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003530 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003531 * then call the backend restore function, which will switch
3532 * back to the vt we launched from or ungrab X etc and then
3533 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003534 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003535 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003536 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003537
Peter Maatmane5b42e42013-03-27 22:38:53 +01003538 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003539
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003540 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003541
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003542 segv_compositor->restore(segv_compositor);
3543
3544 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003545}
3546
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003547WL_EXPORT void *
3548weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003549{
3550 char path[PATH_MAX];
3551 void *module, *init;
3552
3553 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003554 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003555 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003556 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003557
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003558 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3559 if (module) {
3560 weston_log("Module '%s' already loaded\n", path);
3561 dlclose(module);
3562 return NULL;
3563 }
3564
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003565 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003566 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003567 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003568 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003569 return NULL;
3570 }
3571
3572 init = dlsym(module, entrypoint);
3573 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003574 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003575 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003576 return NULL;
3577 }
3578
3579 return init;
3580}
3581
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003582static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003583load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003584 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003585{
3586 const char *p, *end;
3587 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003588 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003589 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003590
3591 if (modules == NULL)
3592 return 0;
3593
3594 p = modules;
3595 while (*p) {
3596 end = strchrnul(p, ',');
3597 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003598 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003599 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003600 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003601 p = end;
3602 while (*p == ',')
3603 p++;
3604
3605 }
3606
3607 return 0;
3608}
3609
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003610static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003611 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3612
3613static const char xdg_wrong_message[] =
3614 "fatal: environment variable XDG_RUNTIME_DIR\n"
3615 "is set to \"%s\", which is not a directory.\n";
3616
3617static const char xdg_wrong_mode_message[] =
3618 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3619 "correctly. Unix access mode must be 0700 but is %o,\n"
3620 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003621 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003622
3623static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003624 "Refer to your distribution on how to get it, or\n"
3625 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3626 "on how to implement it.\n";
3627
Martin Minarik37032f82012-06-18 20:15:18 +02003628static void
3629verify_xdg_runtime_dir(void)
3630{
3631 char *dir = getenv("XDG_RUNTIME_DIR");
3632 struct stat s;
3633
3634 if (!dir) {
3635 weston_log(xdg_error_message);
3636 weston_log_continue(xdg_detail_message);
3637 exit(EXIT_FAILURE);
3638 }
3639
3640 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3641 weston_log(xdg_wrong_message, dir);
3642 weston_log_continue(xdg_detail_message);
3643 exit(EXIT_FAILURE);
3644 }
3645
3646 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3647 weston_log(xdg_wrong_mode_message,
3648 dir, s.st_mode & 0777, s.st_uid);
3649 weston_log_continue(xdg_detail_message);
3650 }
3651}
3652
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003653static int
3654usage(int error_code)
3655{
3656 fprintf(stderr,
3657 "Usage: weston [OPTIONS]\n\n"
3658 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3659 "Weston supports multiple backends, and depending on which backend is in use\n"
3660 "different options will be accepted.\n\n"
3661
3662
3663 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003664 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003665 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003666 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3667 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003668 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003669 " -S, --socket=NAME\tName of socket to listen on\n"
3670 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003671 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003672 " --log==FILE\t\tLog to the given file\n"
3673 " -h, --help\t\tThis help message\n\n");
3674
3675 fprintf(stderr,
3676 "Options for drm-backend.so:\n\n"
3677 " --connector=ID\tBring up only this connector\n"
3678 " --seat=SEAT\t\tThe seat that weston should run on\n"
3679 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003680 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003681 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3682
3683 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003684 "Options for fbdev-backend.so:\n\n"
3685 " --tty=TTY\t\tThe tty to use\n"
3686 " --device=DEVICE\tThe framebuffer device to use\n\n");
3687
3688 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003689 "Options for x11-backend.so:\n\n"
3690 " --width=WIDTH\t\tWidth of X window\n"
3691 " --height=HEIGHT\tHeight of X window\n"
3692 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003693 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003694 " --output-count=COUNT\tCreate multiple outputs\n"
3695 " --no-input\t\tDont create input devices\n\n");
3696
3697 fprintf(stderr,
3698 "Options for wayland-backend.so:\n\n"
3699 " --width=WIDTH\t\tWidth of Wayland surface\n"
3700 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003701 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003702 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003703 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003704 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003705 " --display=DISPLAY\tWayland display to connect to\n\n");
3706
Pekka Paalanene31e0532013-05-22 18:03:07 +03003707#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3708 fprintf(stderr,
3709 "Options for rpi-backend.so:\n\n"
3710 " --tty=TTY\t\tThe tty to use\n"
3711 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3712 " --transform=TR\tThe output transformation, TR is one of:\n"
3713 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3714 "\n");
3715#endif
3716
Hardeningc077a842013-07-08 00:51:35 +02003717#if defined(BUILD_RDP_COMPOSITOR)
3718 fprintf(stderr,
3719 "Options for rdp-backend.so:\n\n"
3720 " --width=WIDTH\t\tWidth of desktop\n"
3721 " --height=HEIGHT\tHeight of desktop\n"
3722 " --extra-modes=MODES\t\n"
3723 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3724 " --address=ADDR\tThe address to bind\n"
3725 " --port=PORT\tThe port to listen on\n"
3726 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3727 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3728 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3729 "\n");
3730#endif
3731
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003732 exit(error_code);
3733}
3734
Peter Maatmane5b42e42013-03-27 22:38:53 +01003735static void
3736catch_signals(void)
3737{
3738 struct sigaction action;
3739
3740 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3741 action.sa_sigaction = on_caught_signal;
3742 sigemptyset(&action.sa_mask);
3743 sigaction(SIGSEGV, &action, NULL);
3744 sigaction(SIGABRT, &action, NULL);
3745}
3746
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003747int main(int argc, char *argv[])
3748{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003749 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003750 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003751 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003752 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003753 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003754 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003755 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003756 int *argc, char *argv[],
3757 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003758 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003759 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003760 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003761 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003762 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003763 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003764 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003765 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003766 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003767 struct weston_config *config;
3768 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003769
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003770 const struct weston_option core_options[] = {
3771 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003772 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003773 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3774 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003775 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003776 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003777 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003778 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003779 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003780
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003781 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003782
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003783 if (help)
3784 usage(EXIT_SUCCESS);
3785
Scott Moreau12245142012-08-29 15:15:58 -06003786 if (version) {
3787 printf(PACKAGE_STRING "\n");
3788 return EXIT_SUCCESS;
3789 }
3790
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003791 weston_log_file_open(log);
3792
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003793 weston_log("%s\n"
3794 STAMP_SPACE "%s\n"
3795 STAMP_SPACE "Bug reports to: %s\n"
3796 STAMP_SPACE "Build: %s\n",
3797 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003798 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003799 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003800
Martin Minarik37032f82012-06-18 20:15:18 +02003801 verify_xdg_runtime_dir();
3802
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003803 display = wl_display_create();
3804
Tiago Vignatti2116b892011-08-08 05:52:59 -07003805 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003806 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3807 display);
3808 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3809 display);
3810 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3811 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003812
3813 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003814 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3815 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003816
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003817 if (!backend) {
3818 if (getenv("WAYLAND_DISPLAY"))
3819 backend = "wayland-backend.so";
3820 else if (getenv("DISPLAY"))
3821 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003822 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003823 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003824 }
3825
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003826 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003827 if (config != NULL) {
3828 weston_log("Using config file '%s'\n",
3829 weston_config_get_full_path(config));
3830 } else {
3831 weston_log("Starting with no config file.\n");
3832 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003833 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003834 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003835
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003836 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003837 if (!backend_init)
3838 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003839
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003840 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003841 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003842 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003843 exit(EXIT_FAILURE);
3844 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003845
Peter Maatmane5b42e42013-03-27 22:38:53 +01003846 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003847 segv_compositor = ec;
3848
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003849 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003850
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003851 setenv("WAYLAND_DISPLAY", socket_name, 1);
3852
Jason Ekstranda985da42013-08-22 17:28:03 -05003853 if (!shell)
3854 weston_config_section_get_string(section, "shell", &shell,
3855 "desktop-shell.so");
3856 if (load_modules(ec, shell, &argc, argv) < 0)
3857 goto out;
3858
Ossama Othmana50e6e42013-05-14 09:48:26 -07003859 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003860 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003861 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003862 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003863
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003864 for (i = 1; i < argc; i++)
3865 weston_log("fatal: unhandled option: %s\n", argv[i]);
3866 if (argc > 1) {
3867 ret = EXIT_FAILURE;
3868 goto out;
3869 }
3870
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003871 weston_compositor_log_capabilities(ec);
3872
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003873 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003874 weston_log("fatal: failed to add socket: %m\n");
3875 ret = EXIT_FAILURE;
3876 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003877 }
3878
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003879 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003880
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003881 wl_display_run(display);
3882
3883 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003884 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003885 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003886
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003887 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003888
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003889 for (i = ARRAY_LENGTH(signals); i;)
3890 wl_event_source_remove(signals[--i]);
3891
Daniel Stone855028f2012-05-01 20:37:10 +01003892 weston_compositor_xkb_destroy(ec);
3893
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003894 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003895 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003896
Martin Minarik19e6f262012-06-07 13:08:46 +02003897 weston_log_file_close();
3898
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003899 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003900}