blob: fd2decb37df7e95b011b8ea1af27fbf10c80da7a [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"
Jonny Lamb8ae35902013-11-26 18:19:45 +010056#include "scaler-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
Hardening57388e42013-09-18 23:56:36 +0200141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349WL_EXPORT struct weston_view *
350weston_view_create(struct weston_surface *surface)
351{
352 struct weston_view *view;
353
354 view = calloc(1, sizeof *view);
355 if (view == NULL)
356 return NULL;
357
358 view->surface = surface;
359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 /* Assign to surface */
361 wl_list_insert(&surface->views, &view->surface_link);
362
363 wl_signal_init(&view->destroy_signal);
364 wl_list_init(&view->link);
365 wl_list_init(&view->layer_link);
366
Xiong Zhang97116532013-10-23 13:58:31 +0800367 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368
369 pixman_region32_init(&view->clip);
370
371 view->alpha = 1.0;
372 pixman_region32_init(&view->transform.opaque);
373
374 wl_list_init(&view->geometry.transformation_list);
375 wl_list_insert(&view->geometry.transformation_list,
376 &view->transform.position.link);
377 weston_matrix_init(&view->transform.position.matrix);
378 wl_list_init(&view->geometry.child_list);
379 pixman_region32_init(&view->transform.boundingbox);
380 view->transform.dirty = 1;
381
382 view->output = NULL;
383
384 return view;
385}
386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500387WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500388weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400391
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200392 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400393 if (surface == NULL)
394 return NULL;
395
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500396 wl_signal_init(&surface->destroy_signal);
397
398 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500399
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500400 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200401 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400402
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200403 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
404 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200405 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
406 surface->buffer_viewport.surface.width = -1;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100407 surface->pending.buffer_viewport = surface->buffer_viewport;
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
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200411 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100412
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400413 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500414 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300415 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400416
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417 wl_list_init(&surface->views);
418
419 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500420
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300421 surface->pending.buffer_destroy_listener.notify =
422 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300423 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300424 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300425 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300426 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300427
Pekka Paalanene67858b2013-04-25 13:57:42 +0300428 wl_list_init(&surface->subsurface_list);
429 wl_list_init(&surface->subsurface_list_pending);
430
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400431 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500432}
433
Alex Wu8811bf92012-02-28 18:07:54 +0800434WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500435weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200436 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500437{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100438 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500439}
440
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400441WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442weston_view_to_global_float(struct weston_view *view,
443 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200444{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500445 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200446 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
447
Jason Ekstranda7af7042013-10-12 22:38:11 -0500448 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200449
450 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200451 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700452 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200453 v.f[3]);
454 *x = 0;
455 *y = 0;
456 return;
457 }
458
459 *x = v.f[0] / v.f[3];
460 *y = v.f[1] / v.f[3];
461 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500462 *x = sx + view->geometry.x;
463 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200464 }
465}
466
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500467WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200468weston_transformed_coord(int width, int height,
469 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200470 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200471 float sx, float sy, float *bx, float *by)
472{
473 switch (transform) {
474 case WL_OUTPUT_TRANSFORM_NORMAL:
475 default:
476 *bx = sx;
477 *by = sy;
478 break;
479 case WL_OUTPUT_TRANSFORM_FLIPPED:
480 *bx = width - sx;
481 *by = sy;
482 break;
483 case WL_OUTPUT_TRANSFORM_90:
484 *bx = height - sy;
485 *by = sx;
486 break;
487 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
488 *bx = height - sy;
489 *by = width - sx;
490 break;
491 case WL_OUTPUT_TRANSFORM_180:
492 *bx = width - sx;
493 *by = height - sy;
494 break;
495 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
496 *bx = sx;
497 *by = height - sy;
498 break;
499 case WL_OUTPUT_TRANSFORM_270:
500 *bx = sy;
501 *by = width - sx;
502 break;
503 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
504 *bx = sy;
505 *by = sx;
506 break;
507 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200508
509 *bx *= scale;
510 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200511}
512
513WL_EXPORT pixman_box32_t
514weston_transformed_rect(int width, int height,
515 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200516 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200517 pixman_box32_t rect)
518{
519 float x1, x2, y1, y2;
520
521 pixman_box32_t ret;
522
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.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200525 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200526 rect.x2, rect.y2, &x2, &y2);
527
528 if (x1 <= x2) {
529 ret.x1 = x1;
530 ret.x2 = x2;
531 } else {
532 ret.x1 = x2;
533 ret.x2 = x1;
534 }
535
536 if (y1 <= y2) {
537 ret.y1 = y1;
538 ret.y2 = y2;
539 } else {
540 ret.y1 = y2;
541 ret.y2 = y1;
542 }
543
544 return ret;
545}
546
547WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500548weston_transformed_region(int width, int height,
549 enum wl_output_transform transform,
550 int32_t scale,
551 pixman_region32_t *src, pixman_region32_t *dest)
552{
553 pixman_box32_t *src_rects, *dest_rects;
554 int nrects, i;
555
556 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
557 if (src != dest)
558 pixman_region32_copy(dest, src);
559 return;
560 }
561
562 src_rects = pixman_region32_rectangles(src, &nrects);
563 dest_rects = malloc(nrects * sizeof(*dest_rects));
564 if (!dest_rects)
565 return;
566
567 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
568 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
569 } else {
570 for (i = 0; i < nrects; i++) {
571 switch (transform) {
572 default:
573 case WL_OUTPUT_TRANSFORM_NORMAL:
574 dest_rects[i].x1 = src_rects[i].x1;
575 dest_rects[i].y1 = src_rects[i].y1;
576 dest_rects[i].x2 = src_rects[i].x2;
577 dest_rects[i].y2 = src_rects[i].y2;
578 break;
579 case WL_OUTPUT_TRANSFORM_90:
580 dest_rects[i].x1 = height - src_rects[i].y2;
581 dest_rects[i].y1 = src_rects[i].x1;
582 dest_rects[i].x2 = height - src_rects[i].y1;
583 dest_rects[i].y2 = src_rects[i].x2;
584 break;
585 case WL_OUTPUT_TRANSFORM_180:
586 dest_rects[i].x1 = width - src_rects[i].x2;
587 dest_rects[i].y1 = height - src_rects[i].y2;
588 dest_rects[i].x2 = width - src_rects[i].x1;
589 dest_rects[i].y2 = height - src_rects[i].y1;
590 break;
591 case WL_OUTPUT_TRANSFORM_270:
592 dest_rects[i].x1 = src_rects[i].y1;
593 dest_rects[i].y1 = width - src_rects[i].x2;
594 dest_rects[i].x2 = src_rects[i].y2;
595 dest_rects[i].y2 = width - src_rects[i].x1;
596 break;
597 case WL_OUTPUT_TRANSFORM_FLIPPED:
598 dest_rects[i].x1 = width - src_rects[i].x2;
599 dest_rects[i].y1 = src_rects[i].y1;
600 dest_rects[i].x2 = width - src_rects[i].x1;
601 dest_rects[i].y2 = src_rects[i].y2;
602 break;
603 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
604 dest_rects[i].x1 = height - src_rects[i].y2;
605 dest_rects[i].y1 = width - src_rects[i].x2;
606 dest_rects[i].x2 = height - src_rects[i].y1;
607 dest_rects[i].y2 = width - src_rects[i].x1;
608 break;
609 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
610 dest_rects[i].x1 = src_rects[i].x1;
611 dest_rects[i].y1 = height - src_rects[i].y2;
612 dest_rects[i].x2 = src_rects[i].x2;
613 dest_rects[i].y2 = height - src_rects[i].y1;
614 break;
615 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
616 dest_rects[i].x1 = src_rects[i].y1;
617 dest_rects[i].y1 = src_rects[i].x1;
618 dest_rects[i].x2 = src_rects[i].y2;
619 dest_rects[i].y2 = src_rects[i].x2;
620 break;
621 }
622 }
623 }
624
625 if (scale != 1) {
626 for (i = 0; i < nrects; i++) {
627 dest_rects[i].x1 *= scale;
628 dest_rects[i].x2 *= scale;
629 dest_rects[i].y1 *= scale;
630 dest_rects[i].y2 *= scale;
631 }
632 }
633
634 pixman_region32_clear(dest);
635 pixman_region32_init_rects(dest, dest_rects, nrects);
636 free(dest_rects);
637}
638
Jonny Lamb74130762013-11-26 18:19:46 +0100639static void
640scaler_surface_to_buffer(struct weston_surface *surface,
641 float sx, float sy, float *bx, float *by)
642{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200643 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200644 double src_width, src_height;
645 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200646
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200647 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
648 if (vp->surface.width == -1) {
649 *bx = sx;
650 *by = sy;
651 return;
652 }
Jonny Lamb74130762013-11-26 18:19:46 +0100653
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200654 src_x = 0.0;
655 src_y = 0.0;
656 src_width = surface->width_from_buffer;
657 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100658 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200659 src_x = wl_fixed_to_double(vp->buffer.src_x);
660 src_y = wl_fixed_to_double(vp->buffer.src_y);
661 src_width = wl_fixed_to_double(vp->buffer.src_width);
662 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100663 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200664
665 *bx = sx * src_width / surface->width + src_x;
666 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100667}
668
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500669WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200670weston_surface_to_buffer_float(struct weston_surface *surface,
671 float sx, float sy, float *bx, float *by)
672{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200673 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
674
Jonny Lamb74130762013-11-26 18:19:46 +0100675 /* first transform coordinates if the scaler is set */
676 scaler_surface_to_buffer(surface, sx, sy, bx, by);
677
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200678 weston_transformed_coord(surface->width, surface->height,
679 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100680 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200681}
682
Alexander Larsson4ea95522013-05-22 14:41:37 +0200683WL_EXPORT void
684weston_surface_to_buffer(struct weston_surface *surface,
685 int sx, int sy, int *bx, int *by)
686{
687 float bxf, byf;
688
Jonny Lamb74130762013-11-26 18:19:46 +0100689 weston_surface_to_buffer_float(surface,
690 sx, sy, &bxf, &byf);
691
Alexander Larsson4ea95522013-05-22 14:41:37 +0200692 *bx = floorf(bxf);
693 *by = floorf(byf);
694}
695
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200696WL_EXPORT pixman_box32_t
697weston_surface_to_buffer_rect(struct weston_surface *surface,
698 pixman_box32_t rect)
699{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200700 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100701 float xf, yf;
702
703 /* first transform box coordinates if the scaler is set */
704 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
705 rect.x1 = floorf(xf);
706 rect.y1 = floorf(yf);
707
708 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
709 rect.x2 = floorf(xf);
710 rect.y2 = floorf(yf);
711
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200712 return weston_transformed_rect(surface->width, surface->height,
713 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200714 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200715}
716
717WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500718weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400719 struct weston_plane *plane)
720{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400722 return;
723
Jason Ekstranda7af7042013-10-12 22:38:11 -0500724 weston_view_damage_below(view);
725 view->plane = plane;
726 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400727}
728
729WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500730weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200731{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500732 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200733
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500734 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500735 pixman_region32_subtract(&damage, &view->transform.boundingbox,
736 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800737 if (view->plane)
738 pixman_region32_union(&view->plane->damage,
739 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500740 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800741 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200742}
743
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400744static void
745weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
746{
747 uint32_t different = es->output_mask ^ mask;
748 uint32_t entered = mask & different;
749 uint32_t left = es->output_mask & different;
750 struct weston_output *output;
751 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500752 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400753
754 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500755 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400756 return;
757 if (different == 0)
758 return;
759
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500760 client = wl_resource_get_client(es->resource);
761
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400762 wl_list_for_each(output, &es->compositor->output_list, link) {
763 if (1 << output->id & different)
764 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500765 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400766 client);
767 if (resource == NULL)
768 continue;
769 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500770 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400771 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500772 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400773 }
774}
775
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200776
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400777static void
778weston_surface_assign_output(struct weston_surface *es)
779{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500780 struct weston_output *new_output;
781 struct weston_view *view;
782 pixman_region32_t region;
783 uint32_t max, area, mask;
784 pixman_box32_t *e;
785
786 new_output = NULL;
787 max = 0;
788 mask = 0;
789 pixman_region32_init(&region);
790 wl_list_for_each(view, &es->views, surface_link) {
791 if (!view->output)
792 continue;
793
794 pixman_region32_intersect(&region, &view->transform.boundingbox,
795 &view->output->region);
796
797 e = pixman_region32_extents(&region);
798 area = (e->x2 - e->x1) * (e->y2 - e->y1);
799
800 mask |= view->output_mask;
801
802 if (area >= max) {
803 new_output = view->output;
804 max = area;
805 }
806 }
807 pixman_region32_fini(&region);
808
809 es->output = new_output;
810 weston_surface_update_output_mask(es, mask);
811}
812
813static void
814weston_view_assign_output(struct weston_view *ev)
815{
816 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400817 struct weston_output *output, *new_output;
818 pixman_region32_t region;
819 uint32_t max, area, mask;
820 pixman_box32_t *e;
821
822 new_output = NULL;
823 max = 0;
824 mask = 0;
825 pixman_region32_init(&region);
826 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400828 &output->region);
829
830 e = pixman_region32_extents(&region);
831 area = (e->x2 - e->x1) * (e->y2 - e->y1);
832
833 if (area > 0)
834 mask |= 1 << output->id;
835
836 if (area >= max) {
837 new_output = output;
838 max = area;
839 }
840 }
841 pixman_region32_fini(&region);
842
Jason Ekstranda7af7042013-10-12 22:38:11 -0500843 ev->output = new_output;
844 ev->output_mask = mask;
845
846 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400847}
848
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200849static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
851 int32_t width, int32_t height,
852 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200853{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200854 float min_x = HUGE_VALF, min_y = HUGE_VALF;
855 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200856 int32_t s[4][2] = {
857 { sx, sy },
858 { sx, sy + height },
859 { sx + width, sy },
860 { sx + width, sy + height }
861 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200862 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200863 int i;
864
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300865 if (width == 0 || height == 0) {
866 /* avoid rounding empty bbox to 1x1 */
867 pixman_region32_init(bbox);
868 return;
869 }
870
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200871 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200872 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500873 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200874 if (x < min_x)
875 min_x = x;
876 if (x > max_x)
877 max_x = x;
878 if (y < min_y)
879 min_y = y;
880 if (y > max_y)
881 max_y = y;
882 }
883
Pekka Paalanen219b9822012-02-08 15:38:37 +0200884 int_x = floorf(min_x);
885 int_y = floorf(min_y);
886 pixman_region32_init_rect(bbox, int_x, int_y,
887 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200888}
889
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200890static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200892{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200894
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200895 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 view->geometry.x = roundf(view->geometry.x);
897 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200898
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500899 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500900 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
901 view->transform.position.matrix.d[12] = view->geometry.x;
902 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500903
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500905
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 view->transform.inverse = view->transform.position.matrix;
907 view->transform.inverse.d[12] = -view->geometry.x;
908 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500909
Jason Ekstranda7af7042013-10-12 22:38:11 -0500910 pixman_region32_init_rect(&view->transform.boundingbox,
911 view->geometry.x,
912 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600913 view->surface->width,
914 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500915
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 if (view->alpha == 1.0) {
917 pixman_region32_copy(&view->transform.opaque,
918 &view->surface->opaque);
919 pixman_region32_translate(&view->transform.opaque,
920 view->geometry.x,
921 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500922 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200923}
924
925static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200927{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 struct weston_view *parent = view->geometry.parent;
929 struct weston_matrix *matrix = &view->transform.matrix;
930 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200931 struct weston_transform *tform;
932
Jason Ekstranda7af7042013-10-12 22:38:11 -0500933 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200934
935 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
937 view->transform.position.matrix.d[12] = view->geometry.x;
938 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200939
940 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200942 weston_matrix_multiply(matrix, &tform->matrix);
943
Pekka Paalanen483243f2013-03-08 14:56:50 +0200944 if (parent)
945 weston_matrix_multiply(matrix, &parent->transform.matrix);
946
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200947 if (weston_matrix_invert(inverse, matrix) < 0) {
948 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 weston_log("error: weston_view %p"
950 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200951 return -1;
952 }
953
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600954 view_compute_bbox(view, 0, 0,
955 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500957
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200958 return 0;
959}
960
961WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200963{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200965
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200967 return;
968
Pekka Paalanen483243f2013-03-08 14:56:50 +0200969 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200973
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200975
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 pixman_region32_fini(&view->transform.boundingbox);
977 pixman_region32_fini(&view->transform.opaque);
978 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500979
Pekka Paalanencd403622012-01-25 13:37:39 +0200980 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 if (view->geometry.transformation_list.next ==
982 &view->transform.position.link &&
983 view->geometry.transformation_list.prev ==
984 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200985 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200987 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 if (weston_view_update_transform_enable(view) < 0)
989 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200990 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200991
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200993
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300995
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 wl_signal_emit(&view->surface->compositor->transform_signal,
997 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200998}
999
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001000WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001004
1005 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 * The invariant: if view->geometry.dirty, then all views
1007 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001008 * Corollary: if not parent->geometry.dirty, then all ancestors
1009 * are not dirty.
1010 */
1011
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001013 return;
1014
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001016
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001018 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001020}
1021
1022WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023weston_view_to_global_fixed(struct weston_view *view,
1024 wl_fixed_t vx, wl_fixed_t vy,
1025 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001026{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001027 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001028
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029 weston_view_to_global_float(view,
1030 wl_fixed_to_double(vx),
1031 wl_fixed_to_double(vy),
1032 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001033 *x = wl_fixed_from_double(xf);
1034 *y = wl_fixed_from_double(yf);
1035}
1036
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001037WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038weston_view_from_global_float(struct weston_view *view,
1039 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001040{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001042 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1043
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001045
1046 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001047 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001049 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 *vx = 0;
1051 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001052 return;
1053 }
1054
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 *vx = v.f[0] / v.f[3];
1056 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001057 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 *vx = x - view->geometry.x;
1059 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001060 }
1061}
1062
1063WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064weston_view_from_global_fixed(struct weston_view *view,
1065 wl_fixed_t x, wl_fixed_t y,
1066 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001067{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001069
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 weston_view_from_global_float(view,
1071 wl_fixed_to_double(x),
1072 wl_fixed_to_double(y),
1073 &vxf, &vyf);
1074 *vx = wl_fixed_from_double(vxf);
1075 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001076}
1077
1078WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001079weston_view_from_global(struct weston_view *view,
1080 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001081{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001083
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1085 *vx = floorf(vxf);
1086 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001087}
1088
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001089WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001090weston_surface_schedule_repaint(struct weston_surface *surface)
1091{
1092 struct weston_output *output;
1093
1094 wl_list_for_each(output, &surface->compositor->output_list, link)
1095 if (surface->output_mask & (1 << output->id))
1096 weston_output_schedule_repaint(output);
1097}
1098
1099WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100weston_view_schedule_repaint(struct weston_view *view)
1101{
1102 struct weston_output *output;
1103
1104 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1105 if (view->output_mask & (1 << output->id))
1106 weston_output_schedule_repaint(output);
1107}
1108
1109WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001110weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001111{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001112 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 0, 0, surface->width,
1114 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001115
Kristian Høgsberg98238702012-08-03 16:29:12 -04001116 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001117}
1118
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001119WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001121{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001122 if (view->geometry.x == x && view->geometry.y == y)
1123 return;
1124
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->geometry.x = x;
1126 view->geometry.y = y;
1127 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001128}
1129
Pekka Paalanen483243f2013-03-08 14:56:50 +02001130static void
1131transform_parent_handle_parent_destroy(struct wl_listener *listener,
1132 void *data)
1133{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 struct weston_view *view =
1135 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001136 geometry.parent_destroy_listener);
1137
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001139}
1140
1141WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142weston_view_set_transform_parent(struct weston_view *view,
1143 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001144{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 if (view->geometry.parent) {
1146 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1147 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001148 }
1149
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001153 transform_parent_handle_parent_destroy;
1154 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001155 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001157 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001159 }
1160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 weston_view_geometry_dirty(view);
1162}
1163
1164WL_EXPORT int
1165weston_view_is_mapped(struct weston_view *view)
1166{
1167 if (view->output)
1168 return 1;
1169 else
1170 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001171}
1172
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001173WL_EXPORT int
1174weston_surface_is_mapped(struct weston_surface *surface)
1175{
1176 if (surface->output)
1177 return 1;
1178 else
1179 return 0;
1180}
1181
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001182static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001183surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001184{
1185 struct weston_view *view;
1186
1187 if (surface->width == width && surface->height == height)
1188 return;
1189
1190 surface->width = width;
1191 surface->height = height;
1192
1193 wl_list_for_each(view, &surface->views, surface_link)
1194 weston_view_geometry_dirty(view);
1195}
1196
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001197WL_EXPORT void
1198weston_surface_set_size(struct weston_surface *surface,
1199 int32_t width, int32_t height)
1200{
1201 assert(!surface->resource);
1202 surface_set_size(surface, width, height);
1203}
1204
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001205static int
1206fixed_round_up_to_int(wl_fixed_t f)
1207{
1208 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1209}
1210
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001211static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001212weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001213{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001214 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001215 int32_t width, height;
1216
1217 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001218 surface_set_size(surface, 0, 0);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001219 surface->width_from_buffer = 0;
1220 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001221 return;
1222 }
1223
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001224 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001225 case WL_OUTPUT_TRANSFORM_90:
1226 case WL_OUTPUT_TRANSFORM_270:
1227 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1228 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001229 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1230 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001231 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001232 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001233 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1234 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001235 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001236 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001237
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001238 surface->width_from_buffer = width;
1239 surface->height_from_buffer = height;
1240
1241 if (vp->surface.width != -1) {
1242 surface_set_size(surface,
1243 vp->surface.width, vp->surface.height);
1244 return;
1245 }
1246
1247 if (vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001248 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1249 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1250
1251 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001252 return;
1253 }
1254
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001255 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001256}
1257
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001258WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001259weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001260{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001261 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001262
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001263 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001264
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001265 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001266}
1267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268WL_EXPORT struct weston_view *
1269weston_compositor_pick_view(struct weston_compositor *compositor,
1270 wl_fixed_t x, wl_fixed_t y,
1271 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001272{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 wl_list_for_each(view, &compositor->view_list, link) {
1276 weston_view_from_global_fixed(view, x, y, vx, vy);
1277 if (pixman_region32_contains_point(&view->surface->input,
1278 wl_fixed_to_int(*vx),
1279 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001280 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001282 }
1283
1284 return NULL;
1285}
1286
1287static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001288weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001289{
Daniel Stone37816df2012-05-16 18:45:18 +01001290 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001291
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001292 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001293 return;
1294
Daniel Stone37816df2012-05-16 18:45:18 +01001295 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001296 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001297}
1298
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001299WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001301{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001302 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001303
Jason Ekstranda7af7042013-10-12 22:38:11 -05001304 if (!weston_view_is_mapped(view))
1305 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 weston_view_damage_below(view);
1308 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001309 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310 wl_list_remove(&view->layer_link);
1311 wl_list_init(&view->layer_link);
1312 wl_list_remove(&view->link);
1313 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001314 view->output_mask = 0;
1315 weston_surface_assign_output(view->surface);
1316
1317 if (weston_surface_is_mapped(view->surface))
1318 return;
1319
1320 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1321 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001322 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001324 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001325 NULL,
1326 wl_fixed_from_int(0),
1327 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001329 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001330 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333WL_EXPORT void
1334weston_surface_unmap(struct weston_surface *surface)
1335{
1336 struct weston_view *view;
1337
1338 wl_list_for_each(view, &surface->views, surface_link)
1339 weston_view_unmap(view);
1340 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001341}
1342
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001343static void
1344weston_surface_reset_pending_buffer(struct weston_surface *surface)
1345{
1346 if (surface->pending.buffer)
1347 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1348 surface->pending.buffer = NULL;
1349 surface->pending.sx = 0;
1350 surface->pending.sy = 0;
1351 surface->pending.newly_attached = 0;
1352}
1353
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001354struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001355 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001356 struct wl_list link;
1357};
1358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359WL_EXPORT void
1360weston_view_destroy(struct weston_view *view)
1361{
1362 wl_signal_emit(&view->destroy_signal, view);
1363
1364 assert(wl_list_empty(&view->geometry.child_list));
1365
1366 if (weston_view_is_mapped(view)) {
1367 weston_view_unmap(view);
1368 weston_compositor_build_view_list(view->surface->compositor);
1369 }
1370
1371 wl_list_remove(&view->link);
1372 wl_list_remove(&view->layer_link);
1373
1374 pixman_region32_fini(&view->clip);
1375 pixman_region32_fini(&view->transform.boundingbox);
1376
1377 weston_view_set_transform_parent(view, NULL);
1378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 wl_list_remove(&view->surface_link);
1380
1381 free(view);
1382}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001383
1384WL_EXPORT void
1385weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001386{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001387 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001389
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001390 if (--surface->ref_count > 0)
1391 return;
1392
1393 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1394
Pekka Paalanene67858b2013-04-25 13:57:42 +03001395 assert(wl_list_empty(&surface->subsurface_list_pending));
1396 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001397
Jason Ekstranda7af7042013-10-12 22:38:11 -05001398 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1399 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001400
Pekka Paalanenbc106382012-10-10 12:49:31 +03001401 wl_list_for_each_safe(cb, next,
1402 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001403 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001404
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001405 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001406 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001407 pixman_region32_fini(&surface->pending.damage);
1408
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001409 if (surface->pending.buffer)
1410 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1411
Pekka Paalanende685b82012-12-04 15:58:12 +02001412 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001413
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001414 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001415 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001416 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001417
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001418 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001419 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001420
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001421 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001422}
1423
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001424static void
1425destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001426{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001427 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001428
Giulio Camuffo0d379742013-11-15 22:06:15 +01001429 /* Set the resource to NULL, since we don't want to leave a
1430 * dangling pointer if the surface was refcounted and survives
1431 * the weston_surface_destroy() call. */
1432 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001433 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001434}
1435
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001436static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001437weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1438{
1439 struct weston_buffer *buffer =
1440 container_of(listener, struct weston_buffer, destroy_listener);
1441
1442 wl_signal_emit(&buffer->destroy_signal, buffer);
1443 free(buffer);
1444}
1445
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001446WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001447weston_buffer_from_resource(struct wl_resource *resource)
1448{
1449 struct weston_buffer *buffer;
1450 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001451
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001452 listener = wl_resource_get_destroy_listener(resource,
1453 weston_buffer_destroy_handler);
1454
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001455 if (listener)
1456 return container_of(listener, struct weston_buffer,
1457 destroy_listener);
1458
1459 buffer = zalloc(sizeof *buffer);
1460 if (buffer == NULL)
1461 return NULL;
1462
1463 buffer->resource = resource;
1464 wl_signal_init(&buffer->destroy_signal);
1465 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001466 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001467 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001468
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001469 return buffer;
1470}
1471
1472static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001473weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1474 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001475{
Pekka Paalanende685b82012-12-04 15:58:12 +02001476 struct weston_buffer_reference *ref =
1477 container_of(listener, struct weston_buffer_reference,
1478 destroy_listener);
1479
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001480 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001481 ref->buffer = NULL;
1482}
1483
1484WL_EXPORT void
1485weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001486 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001487{
1488 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001489 ref->buffer->busy_count--;
1490 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001491 assert(wl_resource_get_client(ref->buffer->resource));
1492 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001493 WL_BUFFER_RELEASE);
1494 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001495 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001496 }
1497
Pekka Paalanende685b82012-12-04 15:58:12 +02001498 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001499 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001500 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001501 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001502 }
1503
Pekka Paalanende685b82012-12-04 15:58:12 +02001504 ref->buffer = buffer;
1505 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1506}
1507
1508static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001509weston_surface_attach(struct weston_surface *surface,
1510 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001511{
1512 weston_buffer_reference(&surface->buffer_ref, buffer);
1513
Pekka Paalanena6421c42012-12-04 15:58:10 +02001514 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001515 if (weston_surface_is_mapped(surface))
1516 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001517 }
1518
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001519 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001520
1521 weston_surface_set_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001522}
1523
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001524WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001525weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001526{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001527 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001528
1529 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001530 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001531}
1532
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001533WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001534weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001535{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001537
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001538 pixman_region32_union(&compositor->primary_plane.damage,
1539 &compositor->primary_plane.damage,
1540 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001541 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001542}
1543
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001544static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001546{
Pekka Paalanende685b82012-12-04 15:58:12 +02001547 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001548 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001549 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001550
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551 empty_region(&surface->damage);
1552}
1553
1554static void
1555view_accumulate_damage(struct weston_view *view,
1556 pixman_region32_t *opaque)
1557{
1558 pixman_region32_t damage;
1559
1560 pixman_region32_init(&damage);
1561 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001562 pixman_box32_t *extents;
1563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 extents = pixman_region32_extents(&view->surface->damage);
1565 view_compute_bbox(view, extents->x1, extents->y1,
1566 extents->x2 - extents->x1,
1567 extents->y2 - extents->y1,
1568 &damage);
1569 pixman_region32_translate(&damage,
1570 -view->plane->x,
1571 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001572 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 pixman_region32_copy(&damage, &view->surface->damage);
1574 pixman_region32_translate(&damage,
1575 view->geometry.x - view->plane->x,
1576 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001577 }
1578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 pixman_region32_subtract(&damage, &damage, opaque);
1580 pixman_region32_union(&view->plane->damage,
1581 &view->plane->damage, &damage);
1582 pixman_region32_fini(&damage);
1583 pixman_region32_copy(&view->clip, opaque);
1584 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001585}
1586
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001587static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001588compositor_accumulate_damage(struct weston_compositor *ec)
1589{
1590 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001592 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001593
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001594 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001595
1596 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001597 pixman_region32_copy(&plane->clip, &clip);
1598
1599 pixman_region32_init(&opaque);
1600
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601 wl_list_for_each(ev, &ec->view_list, link) {
1602 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001603 continue;
1604
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001606 }
1607
1608 pixman_region32_union(&clip, &clip, &opaque);
1609 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001610 }
1611
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001612 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001613
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 wl_list_for_each(ev, &ec->view_list, link)
1615 ev->surface->touched = 0;
1616
1617 wl_list_for_each(ev, &ec->view_list, link) {
1618 if (ev->surface->touched)
1619 continue;
1620 ev->surface->touched = 1;
1621
1622 surface_flush_damage(ev->surface);
1623
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001624 /* Both the renderer and the backend have seen the buffer
1625 * by now. If renderer needs the buffer, it has its own
1626 * reference set. If the backend wants to keep the buffer
1627 * around for migrating the surface into a non-primary plane
1628 * later, keep_buffer is true. Otherwise, drop the core
1629 * reference now, and allow early buffer release. This enables
1630 * clients to use single-buffering.
1631 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 if (!ev->surface->keep_buffer)
1633 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001634 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001635}
1636
1637static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001639{
1640 struct weston_subsurface *sub;
1641
Pekka Paalanene67858b2013-04-25 13:57:42 +03001642 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001643 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001644 continue;
1645
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1647 wl_list_init(&sub->surface->views);
1648
1649 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001650 }
1651}
1652
1653static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001654surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001655{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 struct weston_subsurface *sub;
1657 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001658
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1660 if (sub->surface == surface)
1661 continue;
1662
1663 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1664 weston_view_destroy(view);
1665
1666 surface_free_unused_subsurface_views(sub->surface);
1667 }
1668}
1669
1670static void
1671view_list_add_subsurface_view(struct weston_compositor *compositor,
1672 struct weston_subsurface *sub,
1673 struct weston_view *parent)
1674{
1675 struct weston_subsurface *child;
1676 struct weston_view *view = NULL, *iv;
1677
1678 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1679 if (iv->geometry.parent == parent) {
1680 view = iv;
1681 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001682 }
1683 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684
1685 if (view) {
1686 /* Put it back in the surface's list of views */
1687 wl_list_remove(&view->surface_link);
1688 wl_list_insert(&sub->surface->views, &view->surface_link);
1689 } else {
1690 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001691 weston_view_set_position(view,
1692 sub->position.x,
1693 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694 weston_view_set_transform_parent(view, parent);
1695 }
1696
1697 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698
Pekka Paalanenb188e912013-11-19 14:03:35 +02001699 if (wl_list_empty(&sub->surface->subsurface_list)) {
1700 wl_list_insert(compositor->view_list.prev, &view->link);
1701 return;
1702 }
1703
1704 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1705 if (child->surface == sub->surface)
1706 wl_list_insert(compositor->view_list.prev, &view->link);
1707 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001709 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710}
1711
1712static void
1713view_list_add(struct weston_compositor *compositor,
1714 struct weston_view *view)
1715{
1716 struct weston_subsurface *sub;
1717
1718 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001719
Pekka Paalanenb188e912013-11-19 14:03:35 +02001720 if (wl_list_empty(&view->surface->subsurface_list)) {
1721 wl_list_insert(compositor->view_list.prev, &view->link);
1722 return;
1723 }
1724
1725 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1726 if (sub->surface == view->surface)
1727 wl_list_insert(compositor->view_list.prev, &view->link);
1728 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001729 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001730 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731}
1732
1733static void
1734weston_compositor_build_view_list(struct weston_compositor *compositor)
1735{
1736 struct weston_view *view;
1737 struct weston_layer *layer;
1738
1739 wl_list_for_each(layer, &compositor->layer_list, link)
1740 wl_list_for_each(view, &layer->view_list, layer_link)
1741 surface_stash_subsurface_views(view->surface);
1742
1743 wl_list_init(&compositor->view_list);
1744 wl_list_for_each(layer, &compositor->layer_list, link) {
1745 wl_list_for_each(view, &layer->view_list, layer_link) {
1746 view_list_add(compositor, view);
1747 }
1748 }
1749
1750 wl_list_for_each(layer, &compositor->layer_list, link)
1751 wl_list_for_each(view, &layer->view_list, layer_link)
1752 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001753}
1754
David Herrmann1edf44c2013-10-22 17:11:26 +02001755static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001756weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001757{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001758 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001760 struct weston_animation *animation, *next;
1761 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001762 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001763 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001764 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001765
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001766 if (output->destroying)
1767 return 0;
1768
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001769 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001771
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001772 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001773 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001774 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 wl_list_for_each(ev, &ec->view_list, link)
1776 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001777
Pekka Paalanene67858b2013-04-25 13:57:42 +03001778 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 wl_list_for_each(ev, &ec->view_list, link) {
1780 /* Note: This operation is safe to do multiple times on the
1781 * same surface.
1782 */
1783 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001784 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 &ev->surface->frame_callback_list);
1786 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001787 }
1788 }
1789
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001790 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001791
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001792 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001793 pixman_region32_intersect(&output_damage,
1794 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001795 pixman_region32_subtract(&output_damage,
1796 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001797
Scott Moreauccbf29d2012-02-22 14:21:41 -07001798 if (output->dirty)
1799 weston_output_update_matrix(output);
1800
David Herrmann1edf44c2013-10-22 17:11:26 +02001801 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001802
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001803 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001804
Kristian Høgsbergef044142011-06-21 15:02:12 -04001805 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001806
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001807 weston_compositor_repick(ec);
1808 wl_event_loop_dispatch(ec->input_loop, 0);
1809
Jonas Ådahldb773762012-06-13 00:01:21 +02001810 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001811 wl_callback_send_done(cb->resource, msecs);
1812 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001813 }
1814
Scott Moreaud64cf212012-06-08 19:40:54 -06001815 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001816 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001817 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001818 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001819
1820 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001821}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001822
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001823static int
1824weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001825{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001826 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001827
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001828 wl_event_loop_dispatch(compositor->input_loop, 0);
1829
1830 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001831}
1832
1833WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001834weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001835{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001836 struct weston_compositor *compositor = output->compositor;
1837 struct wl_event_loop *loop =
1838 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001839 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001840
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001841 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001842
1843 if (output->repaint_needed &&
1844 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1845 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001846 r = weston_output_repaint(output, msecs);
1847 if (!r)
1848 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001849 }
1850
1851 output->repaint_scheduled = 0;
1852 if (compositor->input_loop_source)
1853 return;
1854
1855 fd = wl_event_loop_get_fd(compositor->input_loop);
1856 compositor->input_loop_source =
1857 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1858 weston_compositor_read_input, compositor);
1859}
1860
1861static void
1862idle_repaint(void *data)
1863{
1864 struct weston_output *output = data;
1865
Jonas Ådahle5a12252013-04-05 23:07:11 +02001866 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001867}
1868
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001869WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001870weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1871{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001873 if (below != NULL)
1874 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001875}
1876
1877WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001878weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001879{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001880 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001881 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001882
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001883 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1884 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001885 return;
1886
Kristian Høgsbergef044142011-06-21 15:02:12 -04001887 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001888 output->repaint_needed = 1;
1889 if (output->repaint_scheduled)
1890 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001891
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001892 wl_event_loop_add_idle(loop, idle_repaint, output);
1893 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001894
1895 if (compositor->input_loop_source) {
1896 wl_event_source_remove(compositor->input_loop_source);
1897 compositor->input_loop_source = NULL;
1898 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001899}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001900
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001901WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001902weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1903{
1904 struct weston_output *output;
1905
1906 wl_list_for_each(output, &compositor->output_list, link)
1907 weston_output_schedule_repaint(output);
1908}
1909
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001910static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001911surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001912{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001913 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001914}
1915
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001916static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001917surface_attach(struct wl_client *client,
1918 struct wl_resource *resource,
1919 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1920{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001921 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001922 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001923
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001924 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001925 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001926 if (buffer == NULL) {
1927 wl_client_post_no_memory(client);
1928 return;
1929 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001930 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001931
Pekka Paalanende685b82012-12-04 15:58:12 +02001932 /* Attach, attach, without commit in between does not send
1933 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001934 if (surface->pending.buffer)
1935 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001936
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001937 surface->pending.sx = sx;
1938 surface->pending.sy = sy;
1939 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001940 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001941 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001942 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001943 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001944 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001945}
1946
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001947static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001948surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001949 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001950 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001951{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001952 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001953
Pekka Paalanen8e159182012-10-10 12:49:25 +03001954 pixman_region32_union_rect(&surface->pending.damage,
1955 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001956 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001957}
1958
Kristian Høgsberg33418202011-08-16 23:01:28 -04001959static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001960destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001961{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001962 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001963
1964 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001965 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001966}
1967
1968static void
1969surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001970 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001971{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001972 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001973 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001974
1975 cb = malloc(sizeof *cb);
1976 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001977 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001978 return;
1979 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001980
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001981 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1982 callback);
1983 if (cb->resource == NULL) {
1984 free(cb);
1985 wl_resource_post_no_memory(resource);
1986 return;
1987 }
1988
Jason Ekstranda85118c2013-06-27 20:17:02 -05001989 wl_resource_set_implementation(cb->resource, NULL, cb,
1990 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001991
Pekka Paalanenbc106382012-10-10 12:49:31 +03001992 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001993}
1994
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001995static void
1996surface_set_opaque_region(struct wl_client *client,
1997 struct wl_resource *resource,
1998 struct wl_resource *region_resource)
1999{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002000 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002001 struct weston_region *region;
2002
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002003 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002004 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002005 pixman_region32_copy(&surface->pending.opaque,
2006 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002007 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002008 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002009 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002010}
2011
2012static void
2013surface_set_input_region(struct wl_client *client,
2014 struct wl_resource *resource,
2015 struct wl_resource *region_resource)
2016{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002017 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002018 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002019
2020 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002021 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002022 pixman_region32_copy(&surface->pending.input,
2023 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002024 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002025 pixman_region32_fini(&surface->pending.input);
2026 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002027 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002028}
2029
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002030static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002031weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002032{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002033 struct weston_subsurface *sub;
2034
2035 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2036 parent_link_pending) {
2037 wl_list_remove(&sub->parent_link);
2038 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2039 }
2040}
2041
2042static void
2043weston_surface_commit(struct weston_surface *surface)
2044{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002045 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002046 pixman_region32_t opaque;
2047
Pekka Paalanen260ba382014-03-14 14:38:12 +02002048 /* XXX: wl_viewport.set without an attach should call configure */
2049
Alexander Larsson4ea95522013-05-22 14:41:37 +02002050 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002051 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002052 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002053 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002054
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002055 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002056 if (surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002057 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002058
2059 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002060 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002061 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002062
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002063 weston_surface_reset_pending_buffer(surface);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002064
2065 /* wl_surface.damage */
2066 pixman_region32_union(&surface->damage, &surface->damage,
2067 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002068 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2069 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 surface->width,
2071 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002072 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002073
2074 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002075 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 surface->width,
2077 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002078 pixman_region32_intersect(&opaque,
2079 &opaque, &surface->pending.opaque);
2080
2081 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2082 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_for_each(view, &surface->views, surface_link)
2084 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002085 }
2086
2087 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002088
2089 /* wl_surface.set_input_region */
2090 pixman_region32_fini(&surface->input);
2091 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 surface->width,
2093 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002094 pixman_region32_intersect(&surface->input,
2095 &surface->input, &surface->pending.input);
2096
Pekka Paalanenbc106382012-10-10 12:49:31 +03002097 /* wl_surface.frame */
2098 wl_list_insert_list(&surface->frame_callback_list,
2099 &surface->pending.frame_callback_list);
2100 wl_list_init(&surface->pending.frame_callback_list);
2101
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102 weston_surface_commit_subsurface_order(surface);
2103
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002104 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002105}
2106
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002107static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108weston_subsurface_commit(struct weston_subsurface *sub);
2109
2110static void
2111weston_subsurface_parent_commit(struct weston_subsurface *sub,
2112 int parent_is_synchronized);
2113
2114static void
2115surface_commit(struct wl_client *client, struct wl_resource *resource)
2116{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002117 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002118 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2119
2120 if (sub) {
2121 weston_subsurface_commit(sub);
2122 return;
2123 }
2124
2125 weston_surface_commit(surface);
2126
2127 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2128 if (sub->surface != surface)
2129 weston_subsurface_parent_commit(sub, 0);
2130 }
2131}
2132
2133static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002134surface_set_buffer_transform(struct wl_client *client,
2135 struct wl_resource *resource, int transform)
2136{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002137 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002138
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002139 surface->pending.buffer_viewport.buffer.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002140}
2141
Alexander Larsson4ea95522013-05-22 14:41:37 +02002142static void
2143surface_set_buffer_scale(struct wl_client *client,
2144 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002145 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002146{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002147 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002148
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002149 surface->pending.buffer_viewport.buffer.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002150}
2151
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002152static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002153 surface_destroy,
2154 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002155 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002156 surface_frame,
2157 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002158 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002159 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002160 surface_set_buffer_transform,
2161 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002162};
2163
2164static void
2165compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002166 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002167{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002168 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002169 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002170
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002171 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002172 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002173 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002174 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002175 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002176
Jason Ekstranda85118c2013-06-27 20:17:02 -05002177 surface->resource =
2178 wl_resource_create(client, &wl_surface_interface,
2179 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002180 if (surface->resource == NULL) {
2181 weston_surface_destroy(surface);
2182 wl_resource_post_no_memory(resource);
2183 return;
2184 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002185 wl_resource_set_implementation(surface->resource, &surface_interface,
2186 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002187
2188 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002189}
2190
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002191static void
2192destroy_region(struct wl_resource *resource)
2193{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002194 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002195
2196 pixman_region32_fini(&region->region);
2197 free(region);
2198}
2199
2200static void
2201region_destroy(struct wl_client *client, struct wl_resource *resource)
2202{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002203 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002204}
2205
2206static void
2207region_add(struct wl_client *client, struct wl_resource *resource,
2208 int32_t x, int32_t y, int32_t width, int32_t height)
2209{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002210 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002211
2212 pixman_region32_union_rect(&region->region, &region->region,
2213 x, y, width, height);
2214}
2215
2216static void
2217region_subtract(struct wl_client *client, struct wl_resource *resource,
2218 int32_t x, int32_t y, int32_t width, int32_t height)
2219{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002220 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002221 pixman_region32_t rect;
2222
2223 pixman_region32_init_rect(&rect, x, y, width, height);
2224 pixman_region32_subtract(&region->region, &region->region, &rect);
2225 pixman_region32_fini(&rect);
2226}
2227
2228static const struct wl_region_interface region_interface = {
2229 region_destroy,
2230 region_add,
2231 region_subtract
2232};
2233
2234static void
2235compositor_create_region(struct wl_client *client,
2236 struct wl_resource *resource, uint32_t id)
2237{
2238 struct weston_region *region;
2239
2240 region = malloc(sizeof *region);
2241 if (region == NULL) {
2242 wl_resource_post_no_memory(resource);
2243 return;
2244 }
2245
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002246 pixman_region32_init(&region->region);
2247
Jason Ekstranda85118c2013-06-27 20:17:02 -05002248 region->resource =
2249 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002250 if (region->resource == NULL) {
2251 free(region);
2252 wl_resource_post_no_memory(resource);
2253 return;
2254 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002255 wl_resource_set_implementation(region->resource, &region_interface,
2256 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002257}
2258
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002259static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002260 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002261 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002262};
2263
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002264static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002265weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2266{
2267 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002268 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002269 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270
Alexander Larsson4ea95522013-05-22 14:41:37 +02002271 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002272 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002273 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002274 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002275
Pekka Paalanene67858b2013-04-25 13:57:42 +03002276 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002277 if (sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002278 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2279 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2280
Pekka Paalanene67858b2013-04-25 13:57:42 +03002281 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002282 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002283 sub->cached.sx = 0;
2284 sub->cached.sy = 0;
2285 sub->cached.newly_attached = 0;
2286
2287 /* wl_surface.damage */
2288 pixman_region32_union(&surface->damage, &surface->damage,
2289 &sub->cached.damage);
2290 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2291 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002292 surface->width,
2293 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002294 empty_region(&sub->cached.damage);
2295
2296 /* wl_surface.set_opaque_region */
2297 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002298 surface->width,
2299 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002300 pixman_region32_intersect(&opaque,
2301 &opaque, &sub->cached.opaque);
2302
2303 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2304 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 wl_list_for_each(view, &surface->views, surface_link)
2306 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002307 }
2308
2309 pixman_region32_fini(&opaque);
2310
2311 /* wl_surface.set_input_region */
2312 pixman_region32_fini(&surface->input);
2313 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314 surface->width,
2315 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 pixman_region32_intersect(&surface->input,
2317 &surface->input, &sub->cached.input);
2318
2319 /* wl_surface.frame */
2320 wl_list_insert_list(&surface->frame_callback_list,
2321 &sub->cached.frame_callback_list);
2322 wl_list_init(&sub->cached.frame_callback_list);
2323
2324 weston_surface_commit_subsurface_order(surface);
2325
2326 weston_surface_schedule_repaint(surface);
2327
2328 sub->cached.has_data = 0;
2329}
2330
2331static void
2332weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2333{
2334 struct weston_surface *surface = sub->surface;
2335
2336 /*
2337 * If this commit would cause the surface to move by the
2338 * attach(dx, dy) parameters, the old damage region must be
2339 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002340 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002341 */
2342 pixman_region32_translate(&sub->cached.damage,
2343 -surface->pending.sx, -surface->pending.sy);
2344 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2345 &surface->pending.damage);
2346 empty_region(&surface->pending.damage);
2347
2348 if (surface->pending.newly_attached) {
2349 sub->cached.newly_attached = 1;
2350 weston_buffer_reference(&sub->cached.buffer_ref,
2351 surface->pending.buffer);
2352 }
2353 sub->cached.sx += surface->pending.sx;
2354 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002355
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002356 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002357
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002358 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002359
2360 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2361
2362 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2363
2364 wl_list_insert_list(&sub->cached.frame_callback_list,
2365 &surface->pending.frame_callback_list);
2366 wl_list_init(&surface->pending.frame_callback_list);
2367
2368 sub->cached.has_data = 1;
2369}
2370
2371static int
2372weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2373{
2374 while (sub) {
2375 if (sub->synchronized)
2376 return 1;
2377
2378 if (!sub->parent)
2379 return 0;
2380
2381 sub = weston_surface_to_subsurface(sub->parent);
2382 }
2383
2384 return 0;
2385}
2386
2387static void
2388weston_subsurface_commit(struct weston_subsurface *sub)
2389{
2390 struct weston_surface *surface = sub->surface;
2391 struct weston_subsurface *tmp;
2392
2393 /* Recursive check for effectively synchronized. */
2394 if (weston_subsurface_is_synchronized(sub)) {
2395 weston_subsurface_commit_to_cache(sub);
2396 } else {
2397 if (sub->cached.has_data) {
2398 /* flush accumulated state from cache */
2399 weston_subsurface_commit_to_cache(sub);
2400 weston_subsurface_commit_from_cache(sub);
2401 } else {
2402 weston_surface_commit(surface);
2403 }
2404
2405 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2406 if (tmp->surface != surface)
2407 weston_subsurface_parent_commit(tmp, 0);
2408 }
2409 }
2410}
2411
2412static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002413weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002414{
2415 struct weston_surface *surface = sub->surface;
2416 struct weston_subsurface *tmp;
2417
Pekka Paalanene67858b2013-04-25 13:57:42 +03002418 /* From now on, commit_from_cache the whole sub-tree, regardless of
2419 * the synchronized mode of each child. This sub-surface or some
2420 * of its ancestors were synchronized, so we are synchronized
2421 * all the way down.
2422 */
2423
2424 if (sub->cached.has_data)
2425 weston_subsurface_commit_from_cache(sub);
2426
2427 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2428 if (tmp->surface != surface)
2429 weston_subsurface_parent_commit(tmp, 1);
2430 }
2431}
2432
2433static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002434weston_subsurface_parent_commit(struct weston_subsurface *sub,
2435 int parent_is_synchronized)
2436{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002437 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002438 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 wl_list_for_each(view, &sub->surface->views, surface_link)
2440 weston_view_set_position(view,
2441 sub->position.x,
2442 sub->position.y);
2443
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002444 sub->position.set = 0;
2445 }
2446
2447 if (parent_is_synchronized || sub->synchronized)
2448 weston_subsurface_synchronized_commit(sub);
2449}
2450
2451static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002452subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453{
2454 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002456
Jason Ekstranda7af7042013-10-12 22:38:11 -05002457 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002458 weston_view_set_position(view,
2459 view->geometry.x + dx,
2460 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002461
2462 /* No need to check parent mappedness, because if parent is not
2463 * mapped, parent is not in a visible layer, so this sub-surface
2464 * will not be drawn either.
2465 */
2466 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002467 /* Cannot call weston_surface_update_transform(),
2468 * because that would call it also for the parent surface,
2469 * which might not be mapped yet. That would lead to
2470 * inconsistent state, where the window could never be
2471 * mapped.
2472 *
2473 * Instead just assing any output, to make
2474 * weston_surface_is_mapped() return true, so that when the
2475 * parent surface does get mapped, this one will get
2476 * included, too. See surface_list_add().
2477 */
2478 assert(!wl_list_empty(&compositor->output_list));
2479 surface->output = container_of(compositor->output_list.next,
2480 struct weston_output, link);
2481 }
2482}
2483
2484static struct weston_subsurface *
2485weston_surface_to_subsurface(struct weston_surface *surface)
2486{
2487 if (surface->configure == subsurface_configure)
2488 return surface->configure_private;
2489
2490 return NULL;
2491}
2492
Pekka Paalanen01388e22013-04-25 13:57:44 +03002493WL_EXPORT struct weston_surface *
2494weston_surface_get_main_surface(struct weston_surface *surface)
2495{
2496 struct weston_subsurface *sub;
2497
2498 while (surface && (sub = weston_surface_to_subsurface(surface)))
2499 surface = sub->parent;
2500
2501 return surface;
2502}
2503
Pekka Paalanene67858b2013-04-25 13:57:42 +03002504static void
2505subsurface_set_position(struct wl_client *client,
2506 struct wl_resource *resource, int32_t x, int32_t y)
2507{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002508 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002509
2510 if (!sub)
2511 return;
2512
2513 sub->position.x = x;
2514 sub->position.y = y;
2515 sub->position.set = 1;
2516}
2517
2518static struct weston_subsurface *
2519subsurface_from_surface(struct weston_surface *surface)
2520{
2521 struct weston_subsurface *sub;
2522
2523 sub = weston_surface_to_subsurface(surface);
2524 if (sub)
2525 return sub;
2526
2527 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2528 if (sub->surface == surface)
2529 return sub;
2530
2531 return NULL;
2532}
2533
2534static struct weston_subsurface *
2535subsurface_sibling_check(struct weston_subsurface *sub,
2536 struct weston_surface *surface,
2537 const char *request)
2538{
2539 struct weston_subsurface *sibling;
2540
2541 sibling = subsurface_from_surface(surface);
2542
2543 if (!sibling) {
2544 wl_resource_post_error(sub->resource,
2545 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2546 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002547 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002548 return NULL;
2549 }
2550
2551 if (sibling->parent != sub->parent) {
2552 wl_resource_post_error(sub->resource,
2553 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2554 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002555 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002556 return NULL;
2557 }
2558
2559 return sibling;
2560}
2561
2562static void
2563subsurface_place_above(struct wl_client *client,
2564 struct wl_resource *resource,
2565 struct wl_resource *sibling_resource)
2566{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002567 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002568 struct weston_surface *surface =
2569 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570 struct weston_subsurface *sibling;
2571
2572 if (!sub)
2573 return;
2574
2575 sibling = subsurface_sibling_check(sub, surface, "place_above");
2576 if (!sibling)
2577 return;
2578
2579 wl_list_remove(&sub->parent_link_pending);
2580 wl_list_insert(sibling->parent_link_pending.prev,
2581 &sub->parent_link_pending);
2582}
2583
2584static void
2585subsurface_place_below(struct wl_client *client,
2586 struct wl_resource *resource,
2587 struct wl_resource *sibling_resource)
2588{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002589 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002590 struct weston_surface *surface =
2591 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592 struct weston_subsurface *sibling;
2593
2594 if (!sub)
2595 return;
2596
2597 sibling = subsurface_sibling_check(sub, surface, "place_below");
2598 if (!sibling)
2599 return;
2600
2601 wl_list_remove(&sub->parent_link_pending);
2602 wl_list_insert(&sibling->parent_link_pending,
2603 &sub->parent_link_pending);
2604}
2605
2606static void
2607subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2608{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002609 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610
2611 if (sub)
2612 sub->synchronized = 1;
2613}
2614
2615static void
2616subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2617{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002618 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002619
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002620 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002621 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002622
2623 /* If sub became effectively desynchronized, flush. */
2624 if (!weston_subsurface_is_synchronized(sub))
2625 weston_subsurface_synchronized_commit(sub);
2626 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002627}
2628
2629static void
2630weston_subsurface_cache_init(struct weston_subsurface *sub)
2631{
2632 pixman_region32_init(&sub->cached.damage);
2633 pixman_region32_init(&sub->cached.opaque);
2634 pixman_region32_init(&sub->cached.input);
2635 wl_list_init(&sub->cached.frame_callback_list);
2636 sub->cached.buffer_ref.buffer = NULL;
2637}
2638
2639static void
2640weston_subsurface_cache_fini(struct weston_subsurface *sub)
2641{
2642 struct weston_frame_callback *cb, *tmp;
2643
2644 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002645 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002646
2647 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2648 pixman_region32_fini(&sub->cached.damage);
2649 pixman_region32_fini(&sub->cached.opaque);
2650 pixman_region32_fini(&sub->cached.input);
2651}
2652
2653static void
2654weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2655{
2656 wl_list_remove(&sub->parent_link);
2657 wl_list_remove(&sub->parent_link_pending);
2658 wl_list_remove(&sub->parent_destroy_listener.link);
2659 sub->parent = NULL;
2660}
2661
2662static void
2663weston_subsurface_destroy(struct weston_subsurface *sub);
2664
2665static void
2666subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2667{
2668 struct weston_subsurface *sub =
2669 container_of(listener, struct weston_subsurface,
2670 surface_destroy_listener);
2671 assert(data == &sub->surface->resource);
2672
2673 /* The protocol object (wl_resource) is left inert. */
2674 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002675 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676
2677 weston_subsurface_destroy(sub);
2678}
2679
2680static void
2681subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2682{
2683 struct weston_subsurface *sub =
2684 container_of(listener, struct weston_subsurface,
2685 parent_destroy_listener);
2686 assert(data == &sub->parent->resource);
2687 assert(sub->surface != sub->parent);
2688
2689 if (weston_surface_is_mapped(sub->surface))
2690 weston_surface_unmap(sub->surface);
2691
2692 weston_subsurface_unlink_parent(sub);
2693}
2694
2695static void
2696subsurface_resource_destroy(struct wl_resource *resource)
2697{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002698 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002699
2700 if (sub)
2701 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002702}
2703
2704static void
2705subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2706{
2707 wl_resource_destroy(resource);
2708}
2709
2710static void
2711weston_subsurface_link_parent(struct weston_subsurface *sub,
2712 struct weston_surface *parent)
2713{
2714 sub->parent = parent;
2715 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002716 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002717 &sub->parent_destroy_listener);
2718
2719 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2720 wl_list_insert(&parent->subsurface_list_pending,
2721 &sub->parent_link_pending);
2722}
2723
2724static void
2725weston_subsurface_link_surface(struct weston_subsurface *sub,
2726 struct weston_surface *surface)
2727{
2728 sub->surface = surface;
2729 sub->surface_destroy_listener.notify =
2730 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002731 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732 &sub->surface_destroy_listener);
2733}
2734
2735static void
2736weston_subsurface_destroy(struct weston_subsurface *sub)
2737{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 struct weston_view *view, *next;
2739
Pekka Paalanene67858b2013-04-25 13:57:42 +03002740 assert(sub->surface);
2741
2742 if (sub->resource) {
2743 assert(weston_surface_to_subsurface(sub->surface) == sub);
2744 assert(sub->parent_destroy_listener.notify ==
2745 subsurface_handle_parent_destroy);
2746
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2748 weston_view_destroy(view);
2749
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750 if (sub->parent)
2751 weston_subsurface_unlink_parent(sub);
2752
2753 weston_subsurface_cache_fini(sub);
2754
2755 sub->surface->configure = NULL;
2756 sub->surface->configure_private = NULL;
2757 } else {
2758 /* the dummy weston_subsurface for the parent itself */
2759 assert(sub->parent_destroy_listener.notify == NULL);
2760 wl_list_remove(&sub->parent_link);
2761 wl_list_remove(&sub->parent_link_pending);
2762 }
2763
2764 wl_list_remove(&sub->surface_destroy_listener.link);
2765 free(sub);
2766}
2767
2768static const struct wl_subsurface_interface subsurface_implementation = {
2769 subsurface_destroy,
2770 subsurface_set_position,
2771 subsurface_place_above,
2772 subsurface_place_below,
2773 subsurface_set_sync,
2774 subsurface_set_desync
2775};
2776
2777static struct weston_subsurface *
2778weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2779 struct weston_surface *parent)
2780{
2781 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002782 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002783
2784 sub = calloc(1, sizeof *sub);
2785 if (!sub)
2786 return NULL;
2787
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788 wl_list_init(&sub->unused_views);
2789
Jason Ekstranda85118c2013-06-27 20:17:02 -05002790 sub->resource =
2791 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792 if (!sub->resource) {
2793 free(sub);
2794 return NULL;
2795 }
2796
Jason Ekstranda85118c2013-06-27 20:17:02 -05002797 wl_resource_set_implementation(sub->resource,
2798 &subsurface_implementation,
2799 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002800 weston_subsurface_link_surface(sub, surface);
2801 weston_subsurface_link_parent(sub, parent);
2802 weston_subsurface_cache_init(sub);
2803 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002804
2805 return sub;
2806}
2807
2808/* Create a dummy subsurface for having the parent itself in its
2809 * sub-surface lists. Makes stacking order manipulation easy.
2810 */
2811static struct weston_subsurface *
2812weston_subsurface_create_for_parent(struct weston_surface *parent)
2813{
2814 struct weston_subsurface *sub;
2815
2816 sub = calloc(1, sizeof *sub);
2817 if (!sub)
2818 return NULL;
2819
2820 weston_subsurface_link_surface(sub, parent);
2821 sub->parent = parent;
2822 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2823 wl_list_insert(&parent->subsurface_list_pending,
2824 &sub->parent_link_pending);
2825
2826 return sub;
2827}
2828
2829static void
2830subcompositor_get_subsurface(struct wl_client *client,
2831 struct wl_resource *resource,
2832 uint32_t id,
2833 struct wl_resource *surface_resource,
2834 struct wl_resource *parent_resource)
2835{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002836 struct weston_surface *surface =
2837 wl_resource_get_user_data(surface_resource);
2838 struct weston_surface *parent =
2839 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002840 struct weston_subsurface *sub;
2841 static const char where[] = "get_subsurface: wl_subsurface@";
2842
2843 if (surface == parent) {
2844 wl_resource_post_error(resource,
2845 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2846 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002847 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002848 return;
2849 }
2850
2851 if (weston_surface_to_subsurface(surface)) {
2852 wl_resource_post_error(resource,
2853 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2854 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002855 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002856 return;
2857 }
2858
2859 if (surface->configure) {
2860 wl_resource_post_error(resource,
2861 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2862 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002863 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002864 return;
2865 }
2866
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002867 if (weston_surface_get_main_surface(parent) == surface) {
2868 wl_resource_post_error(resource,
2869 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2870 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002871 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002872 return;
2873 }
2874
Pekka Paalanene67858b2013-04-25 13:57:42 +03002875 /* make sure the parent is in its own list */
2876 if (wl_list_empty(&parent->subsurface_list)) {
2877 if (!weston_subsurface_create_for_parent(parent)) {
2878 wl_resource_post_no_memory(resource);
2879 return;
2880 }
2881 }
2882
2883 sub = weston_subsurface_create(id, surface, parent);
2884 if (!sub) {
2885 wl_resource_post_no_memory(resource);
2886 return;
2887 }
2888
2889 surface->configure = subsurface_configure;
2890 surface->configure_private = sub;
2891}
2892
2893static void
2894subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2895{
2896 wl_resource_destroy(resource);
2897}
2898
2899static const struct wl_subcompositor_interface subcompositor_interface = {
2900 subcompositor_destroy,
2901 subcompositor_get_subsurface
2902};
2903
2904static void
2905bind_subcompositor(struct wl_client *client,
2906 void *data, uint32_t version, uint32_t id)
2907{
2908 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002909 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002910
Jason Ekstranda85118c2013-06-27 20:17:02 -05002911 resource =
2912 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002913 if (resource == NULL) {
2914 wl_client_post_no_memory(client);
2915 return;
2916 }
2917 wl_resource_set_implementation(resource, &subcompositor_interface,
2918 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002919}
2920
2921static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002922weston_compositor_dpms(struct weston_compositor *compositor,
2923 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002924{
2925 struct weston_output *output;
2926
2927 wl_list_for_each(output, &compositor->output_list, link)
2928 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002929 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002930}
2931
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002932WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002933weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002934{
Neil Roberts8b62e202013-09-30 13:14:47 +01002935 uint32_t old_state = compositor->state;
2936
2937 /* The state needs to be changed before emitting the wake
2938 * signal because that may try to schedule a repaint which
2939 * will not work if the compositor is still sleeping */
2940 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2941
2942 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002943 case WESTON_COMPOSITOR_SLEEPING:
2944 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2945 /* fall through */
2946 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002947 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002948 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002949 /* fall through */
2950 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002951 wl_event_source_timer_update(compositor->idle_source,
2952 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002953 }
2954}
2955
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002956WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002957weston_compositor_offscreen(struct weston_compositor *compositor)
2958{
2959 switch (compositor->state) {
2960 case WESTON_COMPOSITOR_OFFSCREEN:
2961 return;
2962 case WESTON_COMPOSITOR_SLEEPING:
2963 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2964 /* fall through */
2965 default:
2966 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2967 wl_event_source_timer_update(compositor->idle_source, 0);
2968 }
2969}
2970
2971WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002972weston_compositor_sleep(struct weston_compositor *compositor)
2973{
2974 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2975 return;
2976
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002977 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002978 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2979 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2980}
2981
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002982static int
2983idle_handler(void *data)
2984{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002985 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002986
2987 if (compositor->idle_inhibit)
2988 return 1;
2989
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002990 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002991 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002992
2993 return 1;
2994}
2995
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002996WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002997weston_plane_init(struct weston_plane *plane,
2998 struct weston_compositor *ec,
2999 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003000{
3001 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003002 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003003 plane->x = x;
3004 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003005 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003006
3007 /* Init the link so that the call to wl_list_remove() when releasing
3008 * the plane without ever stacking doesn't lead to a crash */
3009 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003010}
3011
3012WL_EXPORT void
3013weston_plane_release(struct weston_plane *plane)
3014{
Xiong Zhang97116532013-10-23 13:58:31 +08003015 struct weston_view *view;
3016
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003017 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003018 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003019
Xiong Zhang97116532013-10-23 13:58:31 +08003020 wl_list_for_each(view, &plane->compositor->view_list, link) {
3021 if (view->plane == plane)
3022 view->plane = NULL;
3023 }
3024
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003025 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003026}
3027
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003028WL_EXPORT void
3029weston_compositor_stack_plane(struct weston_compositor *ec,
3030 struct weston_plane *plane,
3031 struct weston_plane *above)
3032{
3033 if (above)
3034 wl_list_insert(above->link.prev, &plane->link);
3035 else
3036 wl_list_insert(&ec->plane_list, &plane->link);
3037}
3038
Casey Dahlin9074db52012-04-19 22:50:09 -04003039static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003040{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003041 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003042}
3043
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003044static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003045bind_output(struct wl_client *client,
3046 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003047{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003048 struct weston_output *output = data;
3049 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003050 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003051
Jason Ekstranda85118c2013-06-27 20:17:02 -05003052 resource = wl_resource_create(client, &wl_output_interface,
3053 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003054 if (resource == NULL) {
3055 wl_client_post_no_memory(client);
3056 return;
3057 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003058
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003059 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003060 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003061
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003062 wl_output_send_geometry(resource,
3063 output->x,
3064 output->y,
3065 output->mm_width,
3066 output->mm_height,
3067 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003068 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003069 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003070 if (version >= 2)
3071 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003072 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003073
3074 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003075 wl_output_send_mode(resource,
3076 mode->flags,
3077 mode->width,
3078 mode->height,
3079 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003080 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003081
3082 if (version >= 2)
3083 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003084}
3085
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003086/* Move other outputs when one is removed so the space remains contiguos. */
3087static void
3088weston_compositor_remove_output(struct weston_compositor *compositor,
3089 struct weston_output *remove_output)
3090{
3091 struct weston_output *output;
3092 int offset = 0;
3093
3094 wl_list_for_each(output, &compositor->output_list, link) {
3095 if (output == remove_output) {
3096 offset = output->width;
3097 continue;
3098 }
3099
3100 if (offset > 0) {
3101 weston_output_move(output,
3102 output->x - offset, output->y);
3103 output->dirty = 1;
3104 }
3105 }
3106}
3107
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003108WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003109weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003110{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003111 output->destroying = 1;
3112
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003113 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003114 wl_list_remove(&output->link);
3115
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003116 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003117 wl_signal_emit(&output->destroy_signal, output);
3118
Richard Hughesafe690c2013-05-02 10:10:04 +01003119 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003120 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003121 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003122 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003123
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003124 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003125}
3126
Scott Moreau1bad5db2012-08-18 01:04:05 -06003127static void
3128weston_output_compute_transform(struct weston_output *output)
3129{
3130 struct weston_matrix transform;
3131 int flip;
3132
3133 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003134 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003135
3136 switch(output->transform) {
3137 case WL_OUTPUT_TRANSFORM_FLIPPED:
3138 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3139 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3140 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003141 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003142 flip = -1;
3143 break;
3144 default:
3145 flip = 1;
3146 break;
3147 }
3148
3149 switch(output->transform) {
3150 case WL_OUTPUT_TRANSFORM_NORMAL:
3151 case WL_OUTPUT_TRANSFORM_FLIPPED:
3152 transform.d[0] = flip;
3153 transform.d[1] = 0;
3154 transform.d[4] = 0;
3155 transform.d[5] = 1;
3156 break;
3157 case WL_OUTPUT_TRANSFORM_90:
3158 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3159 transform.d[0] = 0;
3160 transform.d[1] = -flip;
3161 transform.d[4] = 1;
3162 transform.d[5] = 0;
3163 break;
3164 case WL_OUTPUT_TRANSFORM_180:
3165 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3166 transform.d[0] = -flip;
3167 transform.d[1] = 0;
3168 transform.d[4] = 0;
3169 transform.d[5] = -1;
3170 break;
3171 case WL_OUTPUT_TRANSFORM_270:
3172 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3173 transform.d[0] = 0;
3174 transform.d[1] = flip;
3175 transform.d[4] = -1;
3176 transform.d[5] = 0;
3177 break;
3178 default:
3179 break;
3180 }
3181
3182 weston_matrix_multiply(&output->matrix, &transform);
3183}
3184
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003185WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003186weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003187{
Scott Moreau850ca422012-05-21 15:21:25 -06003188 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003189 struct weston_matrix camera;
3190 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003191
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003192 weston_matrix_init(&output->matrix);
3193 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003194 -(output->x + output->width / 2.0),
3195 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003196
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003197 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003198 2.0 / output->width,
3199 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003200
3201 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003202
Scott Moreauccbf29d2012-02-22 14:21:41 -07003203 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003204 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003205 weston_matrix_init(&camera);
3206 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003207 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003208 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003209 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003210 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003211 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003212 weston_matrix_multiply(&output->matrix, &modelview);
3213 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003214
Scott Moreauccbf29d2012-02-22 14:21:41 -07003215 output->dirty = 0;
3216}
3217
Scott Moreau1bad5db2012-08-18 01:04:05 -06003218static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003219weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003220{
3221 output->transform = transform;
3222
3223 switch (transform) {
3224 case WL_OUTPUT_TRANSFORM_90:
3225 case WL_OUTPUT_TRANSFORM_270:
3226 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3227 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3228 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003229 output->width = output->current_mode->height;
3230 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003231 break;
3232 case WL_OUTPUT_TRANSFORM_NORMAL:
3233 case WL_OUTPUT_TRANSFORM_180:
3234 case WL_OUTPUT_TRANSFORM_FLIPPED:
3235 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003236 output->width = output->current_mode->width;
3237 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003238 break;
3239 default:
3240 break;
3241 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003242
Hardening57388e42013-09-18 23:56:36 +02003243 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003244 output->width /= scale;
3245 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003246}
3247
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003248static void
3249weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003250{
3251 output->x = x;
3252 output->y = y;
3253
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003254 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003255 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003256 output->width,
3257 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003258}
3259
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003260WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003261weston_output_move(struct weston_output *output, int x, int y)
3262{
3263 pixman_region32_t old_region;
3264 struct wl_resource *resource;
3265
3266 output->move_x = x - output->x;
3267 output->move_y = y - output->y;
3268
3269 if (output->move_x == 0 && output->move_y == 0)
3270 return;
3271
3272 pixman_region32_init(&old_region);
3273 pixman_region32_copy(&old_region, &output->region);
3274
3275 weston_output_init_geometry(output, x, y);
3276
3277 output->dirty = 1;
3278
3279 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003280 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003281
3282 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003283 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003284 wl_output_send_geometry(resource,
3285 output->x,
3286 output->y,
3287 output->mm_width,
3288 output->mm_height,
3289 output->subpixel,
3290 output->make,
3291 output->model,
3292 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003293
3294 if (wl_resource_get_version(resource) >= 2)
3295 wl_output_send_done(resource);
3296 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003297}
3298
3299WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003300weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003301 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003302 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003303{
3304 output->compositor = c;
3305 output->x = x;
3306 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003307 output->mm_width = mm_width;
3308 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003309 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003310 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003311
Alexander Larsson0b135062013-05-28 16:23:36 +02003312 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003313 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003314
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003315 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003316 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003317
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003318 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003319 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003320 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003321 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003322
Casey Dahlin58ba1372012-04-19 22:50:08 -04003323 output->id = ffs(~output->compositor->output_id_pool) - 1;
3324 output->compositor->output_id_pool |= 1 << output->id;
3325
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003326 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003327 wl_global_create(c->wl_display, &wl_output_interface, 2,
3328 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003329 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003330}
3331
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003332WL_EXPORT void
3333weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003334 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003335 wl_fixed_t *x, wl_fixed_t *y)
3336{
3337 wl_fixed_t tx, ty;
3338 wl_fixed_t width, height;
3339
Hardeningff39efa2013-09-18 23:56:35 +02003340 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3341 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003342
3343 switch(output->transform) {
3344 case WL_OUTPUT_TRANSFORM_NORMAL:
3345 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003346 tx = device_x;
3347 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003348 break;
3349 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003350 tx = device_y;
3351 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003352 break;
3353 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003354 tx = width - device_x;
3355 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003356 break;
3357 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003358 tx = width - device_y;
3359 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003360 break;
3361 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003362 tx = width - device_x;
3363 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003364 break;
3365 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003366 tx = width - device_y;
3367 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003368 break;
3369 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003370 tx = device_x;
3371 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003372 break;
3373 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003374 tx = device_y;
3375 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003376 break;
3377 }
3378
Hardeningff39efa2013-09-18 23:56:35 +02003379 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3380 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003381}
3382
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003383static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003384destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003385{
Jonny Lamb74130762013-11-26 18:19:46 +01003386 struct weston_surface *surface =
3387 wl_resource_get_user_data(resource);
3388
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003389 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003390 surface->pending.buffer_viewport.buffer.src_width =
3391 wl_fixed_from_int(-1);
3392 surface->pending.buffer_viewport.surface.width = -1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003393}
3394
3395static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003396viewport_destroy(struct wl_client *client,
3397 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003398{
3399 wl_resource_destroy(resource);
3400}
3401
3402static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003403viewport_set(struct wl_client *client,
3404 struct wl_resource *resource,
3405 wl_fixed_t src_x,
3406 wl_fixed_t src_y,
3407 wl_fixed_t src_width,
3408 wl_fixed_t src_height,
3409 int32_t dst_width,
3410 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003411{
Jonny Lamb74130762013-11-26 18:19:46 +01003412 struct weston_surface *surface =
3413 wl_resource_get_user_data(resource);
3414
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003415 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003416
Jonny Lamb8ae35902013-11-26 18:19:45 +01003417 if (wl_fixed_to_double(src_width) < 0 ||
3418 wl_fixed_to_double(src_height) < 0) {
3419 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003420 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003421 "source dimensions must be non-negative (%fx%f)",
3422 wl_fixed_to_double(src_width),
3423 wl_fixed_to_double(src_height));
3424 return;
3425 }
3426
3427 if (dst_width <= 0 || dst_height <= 0) {
3428 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003429 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003430 "destination dimensions must be positive (%dx%d)",
3431 dst_width, dst_height);
3432 return;
3433 }
3434
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003435 surface->pending.buffer_viewport.buffer.src_x = src_x;
3436 surface->pending.buffer_viewport.buffer.src_y = src_y;
3437 surface->pending.buffer_viewport.buffer.src_width = src_width;
3438 surface->pending.buffer_viewport.buffer.src_height = src_height;
3439 surface->pending.buffer_viewport.surface.width = dst_width;
3440 surface->pending.buffer_viewport.surface.height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003441}
3442
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003443static void
3444viewport_set_source(struct wl_client *client,
3445 struct wl_resource *resource,
3446 wl_fixed_t src_x,
3447 wl_fixed_t src_y,
3448 wl_fixed_t src_width,
3449 wl_fixed_t src_height)
3450{
3451 struct weston_surface *surface =
3452 wl_resource_get_user_data(resource);
3453
3454 assert(surface->viewport_resource != NULL);
3455
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003456 if (src_width == wl_fixed_from_int(-1) &&
3457 src_height == wl_fixed_from_int(-1)) {
3458 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003459 surface->pending.buffer_viewport.buffer.src_width =
3460 wl_fixed_from_int(-1);
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003461 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003462 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003463
3464 if (src_width <= 0 || src_height <= 0) {
3465 wl_resource_post_error(resource,
3466 WL_VIEWPORT_ERROR_BAD_VALUE,
3467 "source size must be positive (%fx%f)",
3468 wl_fixed_to_double(src_width),
3469 wl_fixed_to_double(src_height));
3470 return;
3471 }
3472
3473 surface->pending.buffer_viewport.buffer.src_x = src_x;
3474 surface->pending.buffer_viewport.buffer.src_y = src_y;
3475 surface->pending.buffer_viewport.buffer.src_width = src_width;
3476 surface->pending.buffer_viewport.buffer.src_height = src_height;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003477}
3478
3479static void
3480viewport_set_destination(struct wl_client *client,
3481 struct wl_resource *resource,
3482 int32_t dst_width,
3483 int32_t dst_height)
3484{
3485 struct weston_surface *surface =
3486 wl_resource_get_user_data(resource);
3487
3488 assert(surface->viewport_resource != NULL);
3489
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003490 if (dst_width == -1 && dst_height == -1) {
3491 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003492 surface->pending.buffer_viewport.surface.width = -1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003493 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003494 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003495
3496 if (dst_width <= 0 || dst_height <= 0) {
3497 wl_resource_post_error(resource,
3498 WL_VIEWPORT_ERROR_BAD_VALUE,
3499 "destination size must be positive (%dx%d)",
3500 dst_width, dst_height);
3501 return;
3502 }
3503
3504 surface->pending.buffer_viewport.surface.width = dst_width;
3505 surface->pending.buffer_viewport.surface.height = dst_height;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003506}
3507
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003508static const struct wl_viewport_interface viewport_interface = {
3509 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003510 viewport_set,
3511 viewport_set_source,
3512 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003513};
3514
3515static void
3516scaler_destroy(struct wl_client *client,
3517 struct wl_resource *resource)
3518{
3519 wl_resource_destroy(resource);
3520}
3521
3522static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003523scaler_get_viewport(struct wl_client *client,
3524 struct wl_resource *scaler,
3525 uint32_t id,
3526 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003527{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003528 int version = wl_resource_get_version(scaler);
3529 struct weston_surface *surface =
3530 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003531 struct wl_resource *resource;
3532
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003533 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003534 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003535 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3536 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003537 return;
3538 }
3539
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003540 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003541 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003542 if (resource == NULL) {
3543 wl_client_post_no_memory(client);
3544 return;
3545 }
3546
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003547 wl_resource_set_implementation(resource, &viewport_interface,
3548 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003549
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003550 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003551}
3552
3553static const struct wl_scaler_interface scaler_interface = {
3554 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003555 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003556};
3557
3558static void
3559bind_scaler(struct wl_client *client,
3560 void *data, uint32_t version, uint32_t id)
3561{
3562 struct wl_resource *resource;
3563
3564 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003565 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003566 if (resource == NULL) {
3567 wl_client_post_no_memory(client);
3568 return;
3569 }
3570
3571 wl_resource_set_implementation(resource, &scaler_interface,
3572 NULL, NULL);
3573}
3574
3575static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003576compositor_bind(struct wl_client *client,
3577 void *data, uint32_t version, uint32_t id)
3578{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003579 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003580 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003581
Jason Ekstranda85118c2013-06-27 20:17:02 -05003582 resource = wl_resource_create(client, &wl_compositor_interface,
3583 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003584 if (resource == NULL) {
3585 wl_client_post_no_memory(client);
3586 return;
3587 }
3588
3589 wl_resource_set_implementation(resource, &compositor_interface,
3590 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003591}
3592
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003593static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003594log_uname(void)
3595{
3596 struct utsname usys;
3597
3598 uname(&usys);
3599
3600 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3601 usys.version, usys.machine);
3602}
3603
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003604WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003605weston_environment_get_fd(const char *env)
3606{
3607 char *e, *end;
3608 int fd, flags;
3609
3610 e = getenv(env);
3611 if (!e)
3612 return -1;
3613 fd = strtol(e, &end, 0);
3614 if (*end != '\0')
3615 return -1;
3616
3617 flags = fcntl(fd, F_GETFD);
3618 if (flags == -1)
3619 return -1;
3620
3621 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3622 unsetenv(env);
3623
3624 return fd;
3625}
3626
3627WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003628weston_compositor_init(struct weston_compositor *ec,
3629 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003630 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003631 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003632{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003633 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003634 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003635 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003636
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003637 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003638 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003639 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003640 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003641 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003642 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003643 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003644 wl_signal_init(&ec->idle_signal);
3645 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003646 wl_signal_init(&ec->show_input_panel_signal);
3647 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003648 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003649 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003650 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003651 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003652 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003653 wl_signal_init(&ec->session_signal);
3654 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003655
Casey Dahlin58ba1372012-04-19 22:50:08 -04003656 ec->output_id_pool = 0;
3657
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003658 if (!wl_global_create(display, &wl_compositor_interface, 3,
3659 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003660 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003661
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003662 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3663 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003664 return -1;
3665
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003666 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003667 ec, bind_scaler))
3668 return -1;
3669
Jason Ekstranda7af7042013-10-12 22:38:11 -05003670 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003671 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003672 wl_list_init(&ec->layer_list);
3673 wl_list_init(&ec->seat_list);
3674 wl_list_init(&ec->output_list);
3675 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003676 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003677 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003678 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003679 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003680 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003681
Xiong Zhang97116532013-10-23 13:58:31 +08003682 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003683 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003684
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003685 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3686 weston_config_section_get_string(s, "keymap_rules",
3687 (char **) &xkb_names.rules, NULL);
3688 weston_config_section_get_string(s, "keymap_model",
3689 (char **) &xkb_names.model, NULL);
3690 weston_config_section_get_string(s, "keymap_layout",
3691 (char **) &xkb_names.layout, NULL);
3692 weston_config_section_get_string(s, "keymap_variant",
3693 (char **) &xkb_names.variant, NULL);
3694 weston_config_section_get_string(s, "keymap_options",
3695 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003696
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003697 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3698 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003699
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003700 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003701
3702 wl_data_device_manager_init(ec->wl_display);
3703
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003704 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003705
Daniel Stone725c2c32012-06-22 14:04:36 +01003706 loop = wl_display_get_event_loop(ec->wl_display);
3707 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3708 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3709
3710 ec->input_loop = wl_event_loop_create();
3711
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003712 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3713 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3714
3715 weston_compositor_schedule_repaint(ec);
3716
Daniel Stone725c2c32012-06-22 14:04:36 +01003717 return 0;
3718}
3719
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003720WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003721weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003722{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003723 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003724
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003725 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003726 if (ec->input_loop_source)
3727 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003728
Matt Roper361d2ad2011-08-29 13:52:23 -07003729 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003730 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003731 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003732
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003733 if (ec->renderer)
3734 ec->renderer->destroy(ec);
3735
Daniel Stone325fc2d2012-05-30 16:31:58 +01003736 weston_binding_list_destroy_all(&ec->key_binding_list);
3737 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003738 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003739 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003740 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003741
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003742 weston_plane_release(&ec->primary_plane);
3743
Jonas Ådahlc97af922012-03-28 22:36:09 +02003744 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003745
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003746 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003747}
3748
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003749WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003750weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3751 const struct weston_pointer_grab_interface *interface)
3752{
3753 struct weston_seat *seat;
3754
3755 ec->default_pointer_grab = interface;
3756 wl_list_for_each(seat, &ec->seat_list, link) {
3757 if (seat->pointer) {
3758 weston_pointer_set_default_grab(seat->pointer,
3759 interface);
3760 }
3761 }
3762}
3763
3764WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003765weston_version(int *major, int *minor, int *micro)
3766{
3767 *major = WESTON_VERSION_MAJOR;
3768 *minor = WESTON_VERSION_MINOR;
3769 *micro = WESTON_VERSION_MICRO;
3770}
3771
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003772static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003773 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003774 const char *desc;
3775} capability_strings[] = {
3776 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003777 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003778};
3779
3780static void
3781weston_compositor_log_capabilities(struct weston_compositor *compositor)
3782{
3783 unsigned i;
3784 int yes;
3785
3786 weston_log("Compositor capabilities:\n");
3787 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3788 yes = compositor->capabilities & capability_strings[i].bit;
3789 weston_log_continue(STAMP_SPACE "%s %s\n",
3790 capability_strings[i].desc,
3791 yes ? "yes" : "no");
3792 }
3793}
3794
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003795static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003796{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003797 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003798
Martin Minarik6d118362012-06-07 18:01:59 +02003799 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003800 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003801
3802 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003803}
3804
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003805#ifdef HAVE_LIBUNWIND
3806
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003807static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003808print_backtrace(void)
3809{
3810 unw_cursor_t cursor;
3811 unw_context_t context;
3812 unw_word_t off;
3813 unw_proc_info_t pip;
3814 int ret, i = 0;
3815 char procname[256];
3816 const char *filename;
3817 Dl_info dlinfo;
3818
3819 pip.unwind_info = NULL;
3820 ret = unw_getcontext(&context);
3821 if (ret) {
3822 weston_log("unw_getcontext: %d\n", ret);
3823 return;
3824 }
3825
3826 ret = unw_init_local(&cursor, &context);
3827 if (ret) {
3828 weston_log("unw_init_local: %d\n", ret);
3829 return;
3830 }
3831
3832 ret = unw_step(&cursor);
3833 while (ret > 0) {
3834 ret = unw_get_proc_info(&cursor, &pip);
3835 if (ret) {
3836 weston_log("unw_get_proc_info: %d\n", ret);
3837 break;
3838 }
3839
3840 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3841 if (ret && ret != -UNW_ENOMEM) {
3842 if (ret != -UNW_EUNSPEC)
3843 weston_log("unw_get_proc_name: %d\n", ret);
3844 procname[0] = '?';
3845 procname[1] = 0;
3846 }
3847
3848 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3849 *dlinfo.dli_fname)
3850 filename = dlinfo.dli_fname;
3851 else
3852 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003853
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003854 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3855 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3856
3857 ret = unw_step(&cursor);
3858 if (ret < 0)
3859 weston_log("unw_step: %d\n", ret);
3860 }
3861}
3862
3863#else
3864
3865static void
3866print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003867{
3868 void *buffer[32];
3869 int i, count;
3870 Dl_info info;
3871
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003872 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3873 for (i = 0; i < count; i++) {
3874 dladdr(buffer[i], &info);
3875 weston_log(" [%016lx] %s (%s)\n",
3876 (long) buffer[i],
3877 info.dli_sname ? info.dli_sname : "--",
3878 info.dli_fname);
3879 }
3880}
3881
3882#endif
3883
3884static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003885on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003886{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003887 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003888 * then call the backend restore function, which will switch
3889 * back to the vt we launched from or ungrab X etc and then
3890 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003891 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003892 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003893 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003894
Peter Maatmane5b42e42013-03-27 22:38:53 +01003895 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003896
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003897 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003898
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003899 segv_compositor->restore(segv_compositor);
3900
3901 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003902}
3903
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003904WL_EXPORT void *
3905weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003906{
3907 char path[PATH_MAX];
3908 void *module, *init;
3909
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003910 if (name == NULL)
3911 return NULL;
3912
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003913 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003914 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003915 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003916 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003917
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003918 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3919 if (module) {
3920 weston_log("Module '%s' already loaded\n", path);
3921 dlclose(module);
3922 return NULL;
3923 }
3924
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003925 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003926 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003927 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003928 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003929 return NULL;
3930 }
3931
3932 init = dlsym(module, entrypoint);
3933 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003934 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003935 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003936 return NULL;
3937 }
3938
3939 return init;
3940}
3941
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003942static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003943load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003944 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003945{
3946 const char *p, *end;
3947 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003948 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003949 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003950
3951 if (modules == NULL)
3952 return 0;
3953
3954 p = modules;
3955 while (*p) {
3956 end = strchrnul(p, ',');
3957 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003958 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003959 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003960 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003961 p = end;
3962 while (*p == ',')
3963 p++;
3964
3965 }
3966
3967 return 0;
3968}
3969
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003970static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003971 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3972
3973static const char xdg_wrong_message[] =
3974 "fatal: environment variable XDG_RUNTIME_DIR\n"
3975 "is set to \"%s\", which is not a directory.\n";
3976
3977static const char xdg_wrong_mode_message[] =
3978 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01003979 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
3980 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003981
3982static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003983 "Refer to your distribution on how to get it, or\n"
3984 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3985 "on how to implement it.\n";
3986
Martin Minarik37032f82012-06-18 20:15:18 +02003987static void
3988verify_xdg_runtime_dir(void)
3989{
3990 char *dir = getenv("XDG_RUNTIME_DIR");
3991 struct stat s;
3992
3993 if (!dir) {
3994 weston_log(xdg_error_message);
3995 weston_log_continue(xdg_detail_message);
3996 exit(EXIT_FAILURE);
3997 }
3998
3999 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4000 weston_log(xdg_wrong_message, dir);
4001 weston_log_continue(xdg_detail_message);
4002 exit(EXIT_FAILURE);
4003 }
4004
4005 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4006 weston_log(xdg_wrong_mode_message,
4007 dir, s.st_mode & 0777, s.st_uid);
4008 weston_log_continue(xdg_detail_message);
4009 }
4010}
4011
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004012static int
4013usage(int error_code)
4014{
4015 fprintf(stderr,
4016 "Usage: weston [OPTIONS]\n\n"
4017 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4018 "Weston supports multiple backends, and depending on which backend is in use\n"
4019 "different options will be accepted.\n\n"
4020
4021
4022 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004023 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004024 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004025 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4026 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004027 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004028 " -S, --socket=NAME\tName of socket to listen on\n"
4029 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004030 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004031 " --log==FILE\t\tLog to the given file\n"
4032 " -h, --help\t\tThis help message\n\n");
4033
4034 fprintf(stderr,
4035 "Options for drm-backend.so:\n\n"
4036 " --connector=ID\tBring up only this connector\n"
4037 " --seat=SEAT\t\tThe seat that weston should run on\n"
4038 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004039 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004040 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4041
4042 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004043 "Options for fbdev-backend.so:\n\n"
4044 " --tty=TTY\t\tThe tty to use\n"
4045 " --device=DEVICE\tThe framebuffer device to use\n\n");
4046
4047 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004048 "Options for x11-backend.so:\n\n"
4049 " --width=WIDTH\t\tWidth of X window\n"
4050 " --height=HEIGHT\tHeight of X window\n"
4051 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004052 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004053 " --output-count=COUNT\tCreate multiple outputs\n"
4054 " --no-input\t\tDont create input devices\n\n");
4055
4056 fprintf(stderr,
4057 "Options for wayland-backend.so:\n\n"
4058 " --width=WIDTH\t\tWidth of Wayland surface\n"
4059 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004060 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004061 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004062 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004063 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004064 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004065 " --display=DISPLAY\tWayland display to connect to\n\n");
4066
Pekka Paalanene31e0532013-05-22 18:03:07 +03004067#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4068 fprintf(stderr,
4069 "Options for rpi-backend.so:\n\n"
4070 " --tty=TTY\t\tThe tty to use\n"
4071 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4072 " --transform=TR\tThe output transformation, TR is one of:\n"
4073 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004074 " --opaque-regions\tEnable support for opaque regions, can be "
4075 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004076 "\n");
4077#endif
4078
Hardeningc077a842013-07-08 00:51:35 +02004079#if defined(BUILD_RDP_COMPOSITOR)
4080 fprintf(stderr,
4081 "Options for rdp-backend.so:\n\n"
4082 " --width=WIDTH\t\tWidth of desktop\n"
4083 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004084 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4085 " --address=ADDR\tThe address to bind\n"
4086 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004087 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004088 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4089 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4090 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4091 "\n");
4092#endif
4093
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004094 exit(error_code);
4095}
4096
Peter Maatmane5b42e42013-03-27 22:38:53 +01004097static void
4098catch_signals(void)
4099{
4100 struct sigaction action;
4101
4102 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4103 action.sa_sigaction = on_caught_signal;
4104 sigemptyset(&action.sa_mask);
4105 sigaction(SIGSEGV, &action, NULL);
4106 sigaction(SIGABRT, &action, NULL);
4107}
4108
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004109static void
4110handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4111{
4112 struct wl_client *client = data;
4113
4114 weston_log("Primary client died. Closing...\n");
4115
4116 wl_display_terminate(wl_client_get_display(client));
4117}
4118
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004119int main(int argc, char *argv[])
4120{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004121 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004122 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004123 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004124 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004125 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004126 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004127 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004128 int *argc, char *argv[],
4129 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004130 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004131 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004132 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004133 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004134 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004135 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004136 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004137 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004138 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004139 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004140 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004141 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004142 struct weston_config *config;
4143 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004144 struct wl_client *primary_client;
4145 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004146
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004147 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004148 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004149 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004150 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4151 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004152 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004153 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004154 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004155 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004156 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004157
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004158 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004159
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004160 if (help)
4161 usage(EXIT_SUCCESS);
4162
Scott Moreau12245142012-08-29 15:15:58 -06004163 if (version) {
4164 printf(PACKAGE_STRING "\n");
4165 return EXIT_SUCCESS;
4166 }
4167
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004168 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004169
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004170 weston_log("%s\n"
4171 STAMP_SPACE "%s\n"
4172 STAMP_SPACE "Bug reports to: %s\n"
4173 STAMP_SPACE "Build: %s\n",
4174 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004175 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004176 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004177
Martin Minarik37032f82012-06-18 20:15:18 +02004178 verify_xdg_runtime_dir();
4179
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004180 display = wl_display_create();
4181
Tiago Vignatti2116b892011-08-08 05:52:59 -07004182 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004183 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4184 display);
4185 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4186 display);
4187 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4188 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004189
4190 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004191 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4192 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004193
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004194 config = weston_config_parse("weston.ini");
4195 if (config != NULL) {
4196 weston_log("Using config file '%s'\n",
4197 weston_config_get_full_path(config));
4198 } else {
4199 weston_log("Starting with no config file.\n");
4200 }
4201 section = weston_config_get_section(config, "core", NULL, NULL);
4202
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004203 if (option_backend)
4204 backend = strdup(option_backend);
4205 else
4206 weston_config_section_get_string(section, "backend", &backend,
4207 NULL);
4208
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004209 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004210 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004211 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004212 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004213 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004214 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004215 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004216 }
4217
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004218 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004219 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004220 if (!backend_init)
4221 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004222
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004223 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004224 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004225 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004226 exit(EXIT_FAILURE);
4227 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004228
Peter Maatmane5b42e42013-03-27 22:38:53 +01004229 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004230 segv_compositor = ec;
4231
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004232 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004233 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004234
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004235 setenv("WAYLAND_DISPLAY", socket_name, 1);
4236
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004237 if (option_shell)
4238 shell = strdup(option_shell);
4239 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004240 weston_config_section_get_string(section, "shell", &shell,
4241 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004242
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004243 if (load_modules(ec, shell, &argc, argv) < 0) {
4244 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004245 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004246 }
4247 free(shell);
4248
4249 weston_config_section_get_string(section, "modules", &modules, "");
4250 if (load_modules(ec, modules, &argc, argv) < 0) {
4251 free(modules);
4252 goto out;
4253 }
4254 free(modules);
4255
Ossama Othmana50e6e42013-05-14 09:48:26 -07004256 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004257 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004258
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004259 for (i = 1; i < argc; i++)
4260 weston_log("fatal: unhandled option: %s\n", argv[i]);
4261 if (argc > 1) {
4262 ret = EXIT_FAILURE;
4263 goto out;
4264 }
4265
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004266 weston_compositor_log_capabilities(ec);
4267
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004268 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4269 if (server_socket) {
4270 weston_log("Running with single client\n");
4271 fd = strtol(server_socket, &end, 0);
4272 if (*end != '\0')
4273 fd = -1;
4274 } else {
4275 fd = -1;
4276 }
4277
4278 if (fd != -1) {
4279 primary_client = wl_client_create(display, fd);
4280 if (!primary_client) {
4281 weston_log("fatal: failed to add client: %m\n");
4282 ret = EXIT_FAILURE;
4283 goto out;
4284 }
4285 primary_client_destroyed.notify =
4286 handle_primary_client_destroyed;
4287 wl_client_add_destroy_listener(primary_client,
4288 &primary_client_destroyed);
4289 } else {
4290 if (wl_display_add_socket(display, socket_name)) {
4291 weston_log("fatal: failed to add socket: %m\n");
4292 ret = EXIT_FAILURE;
4293 goto out;
4294 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004295 }
4296
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004297 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004298
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004299 wl_display_run(display);
4300
4301 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004302 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004303 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004304
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004305 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004306
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004307 for (i = ARRAY_LENGTH(signals); i;)
4308 wl_event_source_remove(signals[--i]);
4309
Daniel Stone855028f2012-05-01 20:37:10 +01004310 weston_compositor_xkb_destroy(ec);
4311
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004312 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004313 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004314
Martin Minarik19e6f262012-06-07 13:08:46 +02004315 weston_log_file_close();
4316
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004317 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004318}