blob: 2fbfdbf80e7f0cc518658c55995a2df3e1866af0 [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
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500678 weston_transformed_coord(surface->width_from_buffer,
679 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200680 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100681 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200682}
683
Alexander Larsson4ea95522013-05-22 14:41:37 +0200684WL_EXPORT void
685weston_surface_to_buffer(struct weston_surface *surface,
686 int sx, int sy, int *bx, int *by)
687{
688 float bxf, byf;
689
Jonny Lamb74130762013-11-26 18:19:46 +0100690 weston_surface_to_buffer_float(surface,
691 sx, sy, &bxf, &byf);
692
Alexander Larsson4ea95522013-05-22 14:41:37 +0200693 *bx = floorf(bxf);
694 *by = floorf(byf);
695}
696
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200697WL_EXPORT pixman_box32_t
698weston_surface_to_buffer_rect(struct weston_surface *surface,
699 pixman_box32_t rect)
700{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200701 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100702 float xf, yf;
703
704 /* first transform box coordinates if the scaler is set */
705 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
706 rect.x1 = floorf(xf);
707 rect.y1 = floorf(yf);
708
709 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
710 rect.x2 = floorf(xf);
711 rect.y2 = floorf(yf);
712
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500713 return weston_transformed_rect(surface->width_from_buffer,
714 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200715 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200716 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200717}
718
719WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500720weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400721 struct weston_plane *plane)
722{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500723 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400724 return;
725
Jason Ekstranda7af7042013-10-12 22:38:11 -0500726 weston_view_damage_below(view);
727 view->plane = plane;
728 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400729}
730
731WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500732weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200733{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500734 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200735
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500736 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500737 pixman_region32_subtract(&damage, &view->transform.boundingbox,
738 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800739 if (view->plane)
740 pixman_region32_union(&view->plane->damage,
741 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500742 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800743 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200744}
745
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400746static void
747weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
748{
749 uint32_t different = es->output_mask ^ mask;
750 uint32_t entered = mask & different;
751 uint32_t left = es->output_mask & different;
752 struct weston_output *output;
753 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500754 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400755
756 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500757 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400758 return;
759 if (different == 0)
760 return;
761
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500762 client = wl_resource_get_client(es->resource);
763
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400764 wl_list_for_each(output, &es->compositor->output_list, link) {
765 if (1 << output->id & different)
766 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500767 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400768 client);
769 if (resource == NULL)
770 continue;
771 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500772 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400773 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500774 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400775 }
776}
777
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200778
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400779static void
780weston_surface_assign_output(struct weston_surface *es)
781{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500782 struct weston_output *new_output;
783 struct weston_view *view;
784 pixman_region32_t region;
785 uint32_t max, area, mask;
786 pixman_box32_t *e;
787
788 new_output = NULL;
789 max = 0;
790 mask = 0;
791 pixman_region32_init(&region);
792 wl_list_for_each(view, &es->views, surface_link) {
793 if (!view->output)
794 continue;
795
796 pixman_region32_intersect(&region, &view->transform.boundingbox,
797 &view->output->region);
798
799 e = pixman_region32_extents(&region);
800 area = (e->x2 - e->x1) * (e->y2 - e->y1);
801
802 mask |= view->output_mask;
803
804 if (area >= max) {
805 new_output = view->output;
806 max = area;
807 }
808 }
809 pixman_region32_fini(&region);
810
811 es->output = new_output;
812 weston_surface_update_output_mask(es, mask);
813}
814
815static void
816weston_view_assign_output(struct weston_view *ev)
817{
818 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400819 struct weston_output *output, *new_output;
820 pixman_region32_t region;
821 uint32_t max, area, mask;
822 pixman_box32_t *e;
823
824 new_output = NULL;
825 max = 0;
826 mask = 0;
827 pixman_region32_init(&region);
828 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500829 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400830 &output->region);
831
832 e = pixman_region32_extents(&region);
833 area = (e->x2 - e->x1) * (e->y2 - e->y1);
834
835 if (area > 0)
836 mask |= 1 << output->id;
837
838 if (area >= max) {
839 new_output = output;
840 max = area;
841 }
842 }
843 pixman_region32_fini(&region);
844
Jason Ekstranda7af7042013-10-12 22:38:11 -0500845 ev->output = new_output;
846 ev->output_mask = mask;
847
848 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400849}
850
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200851static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500852view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
853 int32_t width, int32_t height,
854 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200855{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200856 float min_x = HUGE_VALF, min_y = HUGE_VALF;
857 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200858 int32_t s[4][2] = {
859 { sx, sy },
860 { sx, sy + height },
861 { sx + width, sy },
862 { sx + width, sy + height }
863 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200864 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200865 int i;
866
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300867 if (width == 0 || height == 0) {
868 /* avoid rounding empty bbox to 1x1 */
869 pixman_region32_init(bbox);
870 return;
871 }
872
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200873 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200874 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500875 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200876 if (x < min_x)
877 min_x = x;
878 if (x > max_x)
879 max_x = x;
880 if (y < min_y)
881 min_y = y;
882 if (y > max_y)
883 max_y = y;
884 }
885
Pekka Paalanen219b9822012-02-08 15:38:37 +0200886 int_x = floorf(min_x);
887 int_y = floorf(min_y);
888 pixman_region32_init_rect(bbox, int_x, int_y,
889 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200890}
891
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200892static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200894{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200896
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200897 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898 view->geometry.x = roundf(view->geometry.x);
899 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200900
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500901 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
903 view->transform.position.matrix.d[12] = view->geometry.x;
904 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500905
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500907
Jason Ekstranda7af7042013-10-12 22:38:11 -0500908 view->transform.inverse = view->transform.position.matrix;
909 view->transform.inverse.d[12] = -view->geometry.x;
910 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500911
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912 pixman_region32_init_rect(&view->transform.boundingbox,
913 view->geometry.x,
914 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600915 view->surface->width,
916 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500917
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 if (view->alpha == 1.0) {
919 pixman_region32_copy(&view->transform.opaque,
920 &view->surface->opaque);
921 pixman_region32_translate(&view->transform.opaque,
922 view->geometry.x,
923 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500924 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200925}
926
927static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200929{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930 struct weston_view *parent = view->geometry.parent;
931 struct weston_matrix *matrix = &view->transform.matrix;
932 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200933 struct weston_transform *tform;
934
Jason Ekstranda7af7042013-10-12 22:38:11 -0500935 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200936
937 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500938 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
939 view->transform.position.matrix.d[12] = view->geometry.x;
940 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200941
942 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200944 weston_matrix_multiply(matrix, &tform->matrix);
945
Pekka Paalanen483243f2013-03-08 14:56:50 +0200946 if (parent)
947 weston_matrix_multiply(matrix, &parent->transform.matrix);
948
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200949 if (weston_matrix_invert(inverse, matrix) < 0) {
950 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 weston_log("error: weston_view %p"
952 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200953 return -1;
954 }
955
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600956 view_compute_bbox(view, 0, 0,
957 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500959
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200960 return 0;
961}
962
963WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200965{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200969 return;
970
Pekka Paalanen483243f2013-03-08 14:56:50 +0200971 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200973
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200975
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200977
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 pixman_region32_fini(&view->transform.boundingbox);
979 pixman_region32_fini(&view->transform.opaque);
980 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500981
Pekka Paalanencd403622012-01-25 13:37:39 +0200982 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 if (view->geometry.transformation_list.next ==
984 &view->transform.position.link &&
985 view->geometry.transformation_list.prev ==
986 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200987 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200989 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 if (weston_view_update_transform_enable(view) < 0)
991 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200992 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200993
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200995
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300997
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 wl_signal_emit(&view->surface->compositor->transform_signal,
999 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001000}
1001
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001002WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001004{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001006
1007 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 * The invariant: if view->geometry.dirty, then all views
1009 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001010 * Corollary: if not parent->geometry.dirty, then all ancestors
1011 * are not dirty.
1012 */
1013
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001015 return;
1016
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001018
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001020 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001022}
1023
1024WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025weston_view_to_global_fixed(struct weston_view *view,
1026 wl_fixed_t vx, wl_fixed_t vy,
1027 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001028{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001029 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001030
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 weston_view_to_global_float(view,
1032 wl_fixed_to_double(vx),
1033 wl_fixed_to_double(vy),
1034 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001035 *x = wl_fixed_from_double(xf);
1036 *y = wl_fixed_from_double(yf);
1037}
1038
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001039WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040weston_view_from_global_float(struct weston_view *view,
1041 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001042{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001044 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1045
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001047
1048 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001049 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001051 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 *vx = 0;
1053 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001054 return;
1055 }
1056
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 *vx = v.f[0] / v.f[3];
1058 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001059 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 *vx = x - view->geometry.x;
1061 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001062 }
1063}
1064
1065WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066weston_view_from_global_fixed(struct weston_view *view,
1067 wl_fixed_t x, wl_fixed_t y,
1068 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001069{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001071
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 weston_view_from_global_float(view,
1073 wl_fixed_to_double(x),
1074 wl_fixed_to_double(y),
1075 &vxf, &vyf);
1076 *vx = wl_fixed_from_double(vxf);
1077 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001078}
1079
1080WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001081weston_view_from_global(struct weston_view *view,
1082 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001083{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001085
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1087 *vx = floorf(vxf);
1088 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001089}
1090
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001091WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001092weston_surface_schedule_repaint(struct weston_surface *surface)
1093{
1094 struct weston_output *output;
1095
1096 wl_list_for_each(output, &surface->compositor->output_list, link)
1097 if (surface->output_mask & (1 << output->id))
1098 weston_output_schedule_repaint(output);
1099}
1100
1101WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102weston_view_schedule_repaint(struct weston_view *view)
1103{
1104 struct weston_output *output;
1105
1106 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1107 if (view->output_mask & (1 << output->id))
1108 weston_output_schedule_repaint(output);
1109}
1110
1111WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001112weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001113{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001114 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 0, 0, surface->width,
1116 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001117
Kristian Høgsberg98238702012-08-03 16:29:12 -04001118 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001119}
1120
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001121WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001123{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001124 if (view->geometry.x == x && view->geometry.y == y)
1125 return;
1126
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 view->geometry.x = x;
1128 view->geometry.y = y;
1129 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001130}
1131
Pekka Paalanen483243f2013-03-08 14:56:50 +02001132static void
1133transform_parent_handle_parent_destroy(struct wl_listener *listener,
1134 void *data)
1135{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 struct weston_view *view =
1137 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001138 geometry.parent_destroy_listener);
1139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001141}
1142
1143WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144weston_view_set_transform_parent(struct weston_view *view,
1145 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001146{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 if (view->geometry.parent) {
1148 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1149 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001150 }
1151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001155 transform_parent_handle_parent_destroy;
1156 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001157 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001159 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001161 }
1162
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 weston_view_geometry_dirty(view);
1164}
1165
1166WL_EXPORT int
1167weston_view_is_mapped(struct weston_view *view)
1168{
1169 if (view->output)
1170 return 1;
1171 else
1172 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001173}
1174
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001175WL_EXPORT int
1176weston_surface_is_mapped(struct weston_surface *surface)
1177{
1178 if (surface->output)
1179 return 1;
1180 else
1181 return 0;
1182}
1183
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001184static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001185surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001186{
1187 struct weston_view *view;
1188
1189 if (surface->width == width && surface->height == height)
1190 return;
1191
1192 surface->width = width;
1193 surface->height = height;
1194
1195 wl_list_for_each(view, &surface->views, surface_link)
1196 weston_view_geometry_dirty(view);
1197}
1198
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001199WL_EXPORT void
1200weston_surface_set_size(struct weston_surface *surface,
1201 int32_t width, int32_t height)
1202{
1203 assert(!surface->resource);
1204 surface_set_size(surface, width, height);
1205}
1206
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001207static int
1208fixed_round_up_to_int(wl_fixed_t f)
1209{
1210 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1211}
1212
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001213static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001214weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001215{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001216 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001217 int32_t width, height;
1218
1219 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001220 surface_set_size(surface, 0, 0);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001221 surface->width_from_buffer = 0;
1222 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001223 return;
1224 }
1225
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001226 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001227 case WL_OUTPUT_TRANSFORM_90:
1228 case WL_OUTPUT_TRANSFORM_270:
1229 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1230 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001231 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1232 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001233 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001234 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001235 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1236 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001237 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001238 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001239
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001240 surface->width_from_buffer = width;
1241 surface->height_from_buffer = height;
1242
1243 if (vp->surface.width != -1) {
1244 surface_set_size(surface,
1245 vp->surface.width, vp->surface.height);
1246 return;
1247 }
1248
1249 if (vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001250 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1251 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1252
1253 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001254 return;
1255 }
1256
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001257 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001258}
1259
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001260WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001261weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001262{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001263 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001264
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001265 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001266
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001267 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001268}
1269
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270WL_EXPORT struct weston_view *
1271weston_compositor_pick_view(struct weston_compositor *compositor,
1272 wl_fixed_t x, wl_fixed_t y,
1273 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001274{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001276
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 wl_list_for_each(view, &compositor->view_list, link) {
1278 weston_view_from_global_fixed(view, x, y, vx, vy);
1279 if (pixman_region32_contains_point(&view->surface->input,
1280 wl_fixed_to_int(*vx),
1281 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001282 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001284 }
1285
1286 return NULL;
1287}
1288
1289static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001290weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001291{
Daniel Stone37816df2012-05-16 18:45:18 +01001292 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001293
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001294 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001295 return;
1296
Daniel Stone37816df2012-05-16 18:45:18 +01001297 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001298 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001299}
1300
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001301WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001303{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001304 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001305
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306 if (!weston_view_is_mapped(view))
1307 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 weston_view_damage_below(view);
1310 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001311 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 wl_list_remove(&view->layer_link);
1313 wl_list_init(&view->layer_link);
1314 wl_list_remove(&view->link);
1315 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 view->output_mask = 0;
1317 weston_surface_assign_output(view->surface);
1318
1319 if (weston_surface_is_mapped(view->surface))
1320 return;
1321
1322 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1323 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001324 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001326 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001327 NULL,
1328 wl_fixed_from_int(0),
1329 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001330 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001331 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001332 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001334
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335WL_EXPORT void
1336weston_surface_unmap(struct weston_surface *surface)
1337{
1338 struct weston_view *view;
1339
1340 wl_list_for_each(view, &surface->views, surface_link)
1341 weston_view_unmap(view);
1342 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001343}
1344
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001345static void
1346weston_surface_reset_pending_buffer(struct weston_surface *surface)
1347{
1348 if (surface->pending.buffer)
1349 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1350 surface->pending.buffer = NULL;
1351 surface->pending.sx = 0;
1352 surface->pending.sy = 0;
1353 surface->pending.newly_attached = 0;
1354}
1355
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001356struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001357 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001358 struct wl_list link;
1359};
1360
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361WL_EXPORT void
1362weston_view_destroy(struct weston_view *view)
1363{
1364 wl_signal_emit(&view->destroy_signal, view);
1365
1366 assert(wl_list_empty(&view->geometry.child_list));
1367
1368 if (weston_view_is_mapped(view)) {
1369 weston_view_unmap(view);
1370 weston_compositor_build_view_list(view->surface->compositor);
1371 }
1372
1373 wl_list_remove(&view->link);
1374 wl_list_remove(&view->layer_link);
1375
1376 pixman_region32_fini(&view->clip);
1377 pixman_region32_fini(&view->transform.boundingbox);
1378
1379 weston_view_set_transform_parent(view, NULL);
1380
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381 wl_list_remove(&view->surface_link);
1382
1383 free(view);
1384}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001385
1386WL_EXPORT void
1387weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001388{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001389 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001391
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001392 if (--surface->ref_count > 0)
1393 return;
1394
1395 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1396
Pekka Paalanene67858b2013-04-25 13:57:42 +03001397 assert(wl_list_empty(&surface->subsurface_list_pending));
1398 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001399
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1401 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001402
Pekka Paalanenbc106382012-10-10 12:49:31 +03001403 wl_list_for_each_safe(cb, next,
1404 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001405 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001406
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001407 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001408 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001409 pixman_region32_fini(&surface->pending.damage);
1410
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001411 if (surface->pending.buffer)
1412 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1413
Pekka Paalanende685b82012-12-04 15:58:12 +02001414 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001415
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001416 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001417 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001418 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001419
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001420 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001421 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001422
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001423 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001424}
1425
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001426static void
1427destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001428{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001429 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001430
Giulio Camuffo0d379742013-11-15 22:06:15 +01001431 /* Set the resource to NULL, since we don't want to leave a
1432 * dangling pointer if the surface was refcounted and survives
1433 * the weston_surface_destroy() call. */
1434 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001435 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001436}
1437
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001438static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001439weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1440{
1441 struct weston_buffer *buffer =
1442 container_of(listener, struct weston_buffer, destroy_listener);
1443
1444 wl_signal_emit(&buffer->destroy_signal, buffer);
1445 free(buffer);
1446}
1447
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001448WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001449weston_buffer_from_resource(struct wl_resource *resource)
1450{
1451 struct weston_buffer *buffer;
1452 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001453
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001454 listener = wl_resource_get_destroy_listener(resource,
1455 weston_buffer_destroy_handler);
1456
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001457 if (listener)
1458 return container_of(listener, struct weston_buffer,
1459 destroy_listener);
1460
1461 buffer = zalloc(sizeof *buffer);
1462 if (buffer == NULL)
1463 return NULL;
1464
1465 buffer->resource = resource;
1466 wl_signal_init(&buffer->destroy_signal);
1467 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001468 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001469 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001470
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001471 return buffer;
1472}
1473
1474static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001475weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1476 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001477{
Pekka Paalanende685b82012-12-04 15:58:12 +02001478 struct weston_buffer_reference *ref =
1479 container_of(listener, struct weston_buffer_reference,
1480 destroy_listener);
1481
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001482 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001483 ref->buffer = NULL;
1484}
1485
1486WL_EXPORT void
1487weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001488 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001489{
1490 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001491 ref->buffer->busy_count--;
1492 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001493 assert(wl_resource_get_client(ref->buffer->resource));
1494 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001495 WL_BUFFER_RELEASE);
1496 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001497 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001498 }
1499
Pekka Paalanende685b82012-12-04 15:58:12 +02001500 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001501 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001502 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001503 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001504 }
1505
Pekka Paalanende685b82012-12-04 15:58:12 +02001506 ref->buffer = buffer;
1507 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1508}
1509
1510static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001511weston_surface_attach(struct weston_surface *surface,
1512 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001513{
1514 weston_buffer_reference(&surface->buffer_ref, buffer);
1515
Pekka Paalanena6421c42012-12-04 15:58:10 +02001516 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001517 if (weston_surface_is_mapped(surface))
1518 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001519 }
1520
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001521 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001522
1523 weston_surface_set_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001524}
1525
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001526WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001527weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001528{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001530
1531 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001532 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001533}
1534
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001535WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001537{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001539
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001540 pixman_region32_union(&compositor->primary_plane.damage,
1541 &compositor->primary_plane.damage,
1542 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001543 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001544}
1545
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001546static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001548{
Pekka Paalanende685b82012-12-04 15:58:12 +02001549 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001550 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001551 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001552
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 empty_region(&surface->damage);
1554}
1555
1556static void
1557view_accumulate_damage(struct weston_view *view,
1558 pixman_region32_t *opaque)
1559{
1560 pixman_region32_t damage;
1561
1562 pixman_region32_init(&damage);
1563 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001564 pixman_box32_t *extents;
1565
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 extents = pixman_region32_extents(&view->surface->damage);
1567 view_compute_bbox(view, extents->x1, extents->y1,
1568 extents->x2 - extents->x1,
1569 extents->y2 - extents->y1,
1570 &damage);
1571 pixman_region32_translate(&damage,
1572 -view->plane->x,
1573 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001574 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 pixman_region32_copy(&damage, &view->surface->damage);
1576 pixman_region32_translate(&damage,
1577 view->geometry.x - view->plane->x,
1578 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001579 }
1580
Jason Ekstranda7af7042013-10-12 22:38:11 -05001581 pixman_region32_subtract(&damage, &damage, opaque);
1582 pixman_region32_union(&view->plane->damage,
1583 &view->plane->damage, &damage);
1584 pixman_region32_fini(&damage);
1585 pixman_region32_copy(&view->clip, opaque);
1586 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001587}
1588
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001589static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001590compositor_accumulate_damage(struct weston_compositor *ec)
1591{
1592 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001594 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001595
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001596 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001597
1598 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001599 pixman_region32_copy(&plane->clip, &clip);
1600
1601 pixman_region32_init(&opaque);
1602
Jason Ekstranda7af7042013-10-12 22:38:11 -05001603 wl_list_for_each(ev, &ec->view_list, link) {
1604 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001605 continue;
1606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001608 }
1609
1610 pixman_region32_union(&clip, &clip, &opaque);
1611 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001612 }
1613
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001614 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001615
Jason Ekstranda7af7042013-10-12 22:38:11 -05001616 wl_list_for_each(ev, &ec->view_list, link)
1617 ev->surface->touched = 0;
1618
1619 wl_list_for_each(ev, &ec->view_list, link) {
1620 if (ev->surface->touched)
1621 continue;
1622 ev->surface->touched = 1;
1623
1624 surface_flush_damage(ev->surface);
1625
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001626 /* Both the renderer and the backend have seen the buffer
1627 * by now. If renderer needs the buffer, it has its own
1628 * reference set. If the backend wants to keep the buffer
1629 * around for migrating the surface into a non-primary plane
1630 * later, keep_buffer is true. Otherwise, drop the core
1631 * reference now, and allow early buffer release. This enables
1632 * clients to use single-buffering.
1633 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 if (!ev->surface->keep_buffer)
1635 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001636 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001637}
1638
1639static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001640surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001641{
1642 struct weston_subsurface *sub;
1643
Pekka Paalanene67858b2013-04-25 13:57:42 +03001644 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001646 continue;
1647
Jason Ekstranda7af7042013-10-12 22:38:11 -05001648 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1649 wl_list_init(&sub->surface->views);
1650
1651 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001652 }
1653}
1654
1655static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001657{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658 struct weston_subsurface *sub;
1659 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001660
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1662 if (sub->surface == surface)
1663 continue;
1664
George Kiagiadakised04d382014-06-13 18:10:26 +02001665 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1666 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001667 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001668 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669
1670 surface_free_unused_subsurface_views(sub->surface);
1671 }
1672}
1673
1674static void
1675view_list_add_subsurface_view(struct weston_compositor *compositor,
1676 struct weston_subsurface *sub,
1677 struct weston_view *parent)
1678{
1679 struct weston_subsurface *child;
1680 struct weston_view *view = NULL, *iv;
1681
1682 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1683 if (iv->geometry.parent == parent) {
1684 view = iv;
1685 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001686 }
1687 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688
1689 if (view) {
1690 /* Put it back in the surface's list of views */
1691 wl_list_remove(&view->surface_link);
1692 wl_list_insert(&sub->surface->views, &view->surface_link);
1693 } else {
1694 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001695 weston_view_set_position(view,
1696 sub->position.x,
1697 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 weston_view_set_transform_parent(view, parent);
1699 }
1700
1701 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702
Pekka Paalanenb188e912013-11-19 14:03:35 +02001703 if (wl_list_empty(&sub->surface->subsurface_list)) {
1704 wl_list_insert(compositor->view_list.prev, &view->link);
1705 return;
1706 }
1707
1708 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1709 if (child->surface == sub->surface)
1710 wl_list_insert(compositor->view_list.prev, &view->link);
1711 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001713 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714}
1715
1716static void
1717view_list_add(struct weston_compositor *compositor,
1718 struct weston_view *view)
1719{
1720 struct weston_subsurface *sub;
1721
1722 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723
Pekka Paalanenb188e912013-11-19 14:03:35 +02001724 if (wl_list_empty(&view->surface->subsurface_list)) {
1725 wl_list_insert(compositor->view_list.prev, &view->link);
1726 return;
1727 }
1728
1729 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1730 if (sub->surface == view->surface)
1731 wl_list_insert(compositor->view_list.prev, &view->link);
1732 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001734 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735}
1736
1737static void
1738weston_compositor_build_view_list(struct weston_compositor *compositor)
1739{
1740 struct weston_view *view;
1741 struct weston_layer *layer;
1742
1743 wl_list_for_each(layer, &compositor->layer_list, link)
1744 wl_list_for_each(view, &layer->view_list, layer_link)
1745 surface_stash_subsurface_views(view->surface);
1746
1747 wl_list_init(&compositor->view_list);
1748 wl_list_for_each(layer, &compositor->layer_list, link) {
1749 wl_list_for_each(view, &layer->view_list, layer_link) {
1750 view_list_add(compositor, view);
1751 }
1752 }
1753
1754 wl_list_for_each(layer, &compositor->layer_list, link)
1755 wl_list_for_each(view, &layer->view_list, layer_link)
1756 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001757}
1758
David Herrmann1edf44c2013-10-22 17:11:26 +02001759static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001760weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001761{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001762 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001764 struct weston_animation *animation, *next;
1765 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001766 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001767 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001768 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001769
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001770 if (output->destroying)
1771 return 0;
1772
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001773 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001775
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001776 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001777 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001778 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 wl_list_for_each(ev, &ec->view_list, link)
1780 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001781
Pekka Paalanene67858b2013-04-25 13:57:42 +03001782 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 wl_list_for_each(ev, &ec->view_list, link) {
1784 /* Note: This operation is safe to do multiple times on the
1785 * same surface.
1786 */
1787 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001788 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001789 &ev->surface->frame_callback_list);
1790 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001791 }
1792 }
1793
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001794 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001795
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001796 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001797 pixman_region32_intersect(&output_damage,
1798 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001799 pixman_region32_subtract(&output_damage,
1800 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001801
Scott Moreauccbf29d2012-02-22 14:21:41 -07001802 if (output->dirty)
1803 weston_output_update_matrix(output);
1804
David Herrmann1edf44c2013-10-22 17:11:26 +02001805 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001806
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001807 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001808
Kristian Høgsbergef044142011-06-21 15:02:12 -04001809 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001810
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001811 weston_compositor_repick(ec);
1812 wl_event_loop_dispatch(ec->input_loop, 0);
1813
Jonas Ådahldb773762012-06-13 00:01:21 +02001814 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001815 wl_callback_send_done(cb->resource, msecs);
1816 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001817 }
1818
Scott Moreaud64cf212012-06-08 19:40:54 -06001819 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001820 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001821 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001822 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001823
1824 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001825}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001826
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001827static int
1828weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001829{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001830 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001831
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001832 wl_event_loop_dispatch(compositor->input_loop, 0);
1833
1834 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001835}
1836
1837WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001838weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001839{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001840 struct weston_compositor *compositor = output->compositor;
1841 struct wl_event_loop *loop =
1842 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001843 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001844
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001845 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001846
1847 if (output->repaint_needed &&
1848 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1849 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001850 r = weston_output_repaint(output, msecs);
1851 if (!r)
1852 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001853 }
1854
1855 output->repaint_scheduled = 0;
1856 if (compositor->input_loop_source)
1857 return;
1858
1859 fd = wl_event_loop_get_fd(compositor->input_loop);
1860 compositor->input_loop_source =
1861 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1862 weston_compositor_read_input, compositor);
1863}
1864
1865static void
1866idle_repaint(void *data)
1867{
1868 struct weston_output *output = data;
1869
Jonas Ådahle5a12252013-04-05 23:07:11 +02001870 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001871}
1872
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001873WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001874weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1875{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001877 if (below != NULL)
1878 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001879}
1880
1881WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001882weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001883{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001884 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001885 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001886
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001887 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1888 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001889 return;
1890
Kristian Høgsbergef044142011-06-21 15:02:12 -04001891 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001892 output->repaint_needed = 1;
1893 if (output->repaint_scheduled)
1894 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001895
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001896 wl_event_loop_add_idle(loop, idle_repaint, output);
1897 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001898
1899 if (compositor->input_loop_source) {
1900 wl_event_source_remove(compositor->input_loop_source);
1901 compositor->input_loop_source = NULL;
1902 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001903}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001904
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001905WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001906weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1907{
1908 struct weston_output *output;
1909
1910 wl_list_for_each(output, &compositor->output_list, link)
1911 weston_output_schedule_repaint(output);
1912}
1913
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001914static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001915surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001916{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001917 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001918}
1919
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001920static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001921surface_attach(struct wl_client *client,
1922 struct wl_resource *resource,
1923 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1924{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001925 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001926 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001927
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001928 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001929 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001930 if (buffer == NULL) {
1931 wl_client_post_no_memory(client);
1932 return;
1933 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001934 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001935
Pekka Paalanende685b82012-12-04 15:58:12 +02001936 /* Attach, attach, without commit in between does not send
1937 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001938 if (surface->pending.buffer)
1939 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001940
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001941 surface->pending.sx = sx;
1942 surface->pending.sy = sy;
1943 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001944 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001945 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001946 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001947 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001948 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001949}
1950
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001951static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001952surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001953 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001954 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001955{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001956 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001957
Pekka Paalanen8e159182012-10-10 12:49:25 +03001958 pixman_region32_union_rect(&surface->pending.damage,
1959 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001960 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001961}
1962
Kristian Høgsberg33418202011-08-16 23:01:28 -04001963static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001964destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001965{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001966 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001967
1968 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001969 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001970}
1971
1972static void
1973surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001974 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001975{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001976 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001977 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001978
1979 cb = malloc(sizeof *cb);
1980 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001981 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001982 return;
1983 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001984
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001985 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1986 callback);
1987 if (cb->resource == NULL) {
1988 free(cb);
1989 wl_resource_post_no_memory(resource);
1990 return;
1991 }
1992
Jason Ekstranda85118c2013-06-27 20:17:02 -05001993 wl_resource_set_implementation(cb->resource, NULL, cb,
1994 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001995
Pekka Paalanenbc106382012-10-10 12:49:31 +03001996 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001997}
1998
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001999static void
2000surface_set_opaque_region(struct wl_client *client,
2001 struct wl_resource *resource,
2002 struct wl_resource *region_resource)
2003{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002004 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002005 struct weston_region *region;
2006
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002007 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002008 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002009 pixman_region32_copy(&surface->pending.opaque,
2010 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002011 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002012 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002013 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002014}
2015
2016static void
2017surface_set_input_region(struct wl_client *client,
2018 struct wl_resource *resource,
2019 struct wl_resource *region_resource)
2020{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002021 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002022 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002023
2024 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002025 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002026 pixman_region32_copy(&surface->pending.input,
2027 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002028 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002029 pixman_region32_fini(&surface->pending.input);
2030 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002031 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002032}
2033
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002034static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002035weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002036{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002037 struct weston_subsurface *sub;
2038
2039 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2040 parent_link_pending) {
2041 wl_list_remove(&sub->parent_link);
2042 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2043 }
2044}
2045
2046static void
2047weston_surface_commit(struct weston_surface *surface)
2048{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002049 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002050 pixman_region32_t opaque;
2051
Pekka Paalanen260ba382014-03-14 14:38:12 +02002052 /* XXX: wl_viewport.set without an attach should call configure */
2053
Alexander Larsson4ea95522013-05-22 14:41:37 +02002054 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002055 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002056 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002057 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002058
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002059 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002060 if (surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002061 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002062
2063 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002064 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002065 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002066
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002067 weston_surface_reset_pending_buffer(surface);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002068
2069 /* wl_surface.damage */
2070 pixman_region32_union(&surface->damage, &surface->damage,
2071 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002072 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2073 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002074 surface->width,
2075 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002076 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002077
2078 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002079 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 surface->width,
2081 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002082 pixman_region32_intersect(&opaque,
2083 &opaque, &surface->pending.opaque);
2084
2085 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2086 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 wl_list_for_each(view, &surface->views, surface_link)
2088 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002089 }
2090
2091 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002092
2093 /* wl_surface.set_input_region */
2094 pixman_region32_fini(&surface->input);
2095 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002096 surface->width,
2097 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002098 pixman_region32_intersect(&surface->input,
2099 &surface->input, &surface->pending.input);
2100
Pekka Paalanenbc106382012-10-10 12:49:31 +03002101 /* wl_surface.frame */
2102 wl_list_insert_list(&surface->frame_callback_list,
2103 &surface->pending.frame_callback_list);
2104 wl_list_init(&surface->pending.frame_callback_list);
2105
Pekka Paalanene67858b2013-04-25 13:57:42 +03002106 weston_surface_commit_subsurface_order(surface);
2107
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002108 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002109}
2110
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002111static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002112weston_subsurface_commit(struct weston_subsurface *sub);
2113
2114static void
2115weston_subsurface_parent_commit(struct weston_subsurface *sub,
2116 int parent_is_synchronized);
2117
2118static void
2119surface_commit(struct wl_client *client, struct wl_resource *resource)
2120{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002121 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002122 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2123
2124 if (sub) {
2125 weston_subsurface_commit(sub);
2126 return;
2127 }
2128
2129 weston_surface_commit(surface);
2130
2131 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2132 if (sub->surface != surface)
2133 weston_subsurface_parent_commit(sub, 0);
2134 }
2135}
2136
2137static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002138surface_set_buffer_transform(struct wl_client *client,
2139 struct wl_resource *resource, int transform)
2140{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002141 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002142
Jonny Lamba55f1392014-05-30 12:07:15 +02002143 /* if wl_output.transform grows more members this will need to be updated. */
2144 if (transform < 0 ||
2145 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2146 wl_resource_post_error(resource,
2147 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2148 "buffer transform must be a valid transform "
2149 "('%d' specified)", transform);
2150 return;
2151 }
2152
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002153 surface->pending.buffer_viewport.buffer.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002154}
2155
Alexander Larsson4ea95522013-05-22 14:41:37 +02002156static void
2157surface_set_buffer_scale(struct wl_client *client,
2158 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002159 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002160{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002161 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002162
Jonny Lamba55f1392014-05-30 12:07:15 +02002163 if (scale < 1) {
2164 wl_resource_post_error(resource,
2165 WL_SURFACE_ERROR_INVALID_SCALE,
2166 "buffer scale must be at least one "
2167 "('%d' specified)", scale);
2168 return;
2169 }
2170
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002171 surface->pending.buffer_viewport.buffer.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002172}
2173
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002174static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002175 surface_destroy,
2176 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002177 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002178 surface_frame,
2179 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002180 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002181 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002182 surface_set_buffer_transform,
2183 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002184};
2185
2186static void
2187compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002188 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002189{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002190 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002191 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002192
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002193 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002194 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002195 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002196 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002197 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002198
Jason Ekstranda85118c2013-06-27 20:17:02 -05002199 surface->resource =
2200 wl_resource_create(client, &wl_surface_interface,
2201 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002202 if (surface->resource == NULL) {
2203 weston_surface_destroy(surface);
2204 wl_resource_post_no_memory(resource);
2205 return;
2206 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002207 wl_resource_set_implementation(surface->resource, &surface_interface,
2208 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002209
2210 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002211}
2212
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002213static void
2214destroy_region(struct wl_resource *resource)
2215{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002216 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002217
2218 pixman_region32_fini(&region->region);
2219 free(region);
2220}
2221
2222static void
2223region_destroy(struct wl_client *client, struct wl_resource *resource)
2224{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002225 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002226}
2227
2228static void
2229region_add(struct wl_client *client, struct wl_resource *resource,
2230 int32_t x, int32_t y, int32_t width, int32_t height)
2231{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002232 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002233
2234 pixman_region32_union_rect(&region->region, &region->region,
2235 x, y, width, height);
2236}
2237
2238static void
2239region_subtract(struct wl_client *client, struct wl_resource *resource,
2240 int32_t x, int32_t y, int32_t width, int32_t height)
2241{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002242 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002243 pixman_region32_t rect;
2244
2245 pixman_region32_init_rect(&rect, x, y, width, height);
2246 pixman_region32_subtract(&region->region, &region->region, &rect);
2247 pixman_region32_fini(&rect);
2248}
2249
2250static const struct wl_region_interface region_interface = {
2251 region_destroy,
2252 region_add,
2253 region_subtract
2254};
2255
2256static void
2257compositor_create_region(struct wl_client *client,
2258 struct wl_resource *resource, uint32_t id)
2259{
2260 struct weston_region *region;
2261
2262 region = malloc(sizeof *region);
2263 if (region == NULL) {
2264 wl_resource_post_no_memory(resource);
2265 return;
2266 }
2267
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002268 pixman_region32_init(&region->region);
2269
Jason Ekstranda85118c2013-06-27 20:17:02 -05002270 region->resource =
2271 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002272 if (region->resource == NULL) {
2273 free(region);
2274 wl_resource_post_no_memory(resource);
2275 return;
2276 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002277 wl_resource_set_implementation(region->resource, &region_interface,
2278 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002279}
2280
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002281static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002282 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002283 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002284};
2285
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002286static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002287weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2288{
2289 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002290 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002291 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002292
Alexander Larsson4ea95522013-05-22 14:41:37 +02002293 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002294 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002295 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002296 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002297
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002299 if (sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002300 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2301 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2302
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002304 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002305 sub->cached.sx = 0;
2306 sub->cached.sy = 0;
2307 sub->cached.newly_attached = 0;
2308
2309 /* wl_surface.damage */
2310 pixman_region32_union(&surface->damage, &surface->damage,
2311 &sub->cached.damage);
2312 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2313 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314 surface->width,
2315 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 empty_region(&sub->cached.damage);
2317
2318 /* wl_surface.set_opaque_region */
2319 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002320 surface->width,
2321 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322 pixman_region32_intersect(&opaque,
2323 &opaque, &sub->cached.opaque);
2324
2325 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2326 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002327 wl_list_for_each(view, &surface->views, surface_link)
2328 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329 }
2330
2331 pixman_region32_fini(&opaque);
2332
2333 /* wl_surface.set_input_region */
2334 pixman_region32_fini(&surface->input);
2335 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002336 surface->width,
2337 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002338 pixman_region32_intersect(&surface->input,
2339 &surface->input, &sub->cached.input);
2340
2341 /* wl_surface.frame */
2342 wl_list_insert_list(&surface->frame_callback_list,
2343 &sub->cached.frame_callback_list);
2344 wl_list_init(&sub->cached.frame_callback_list);
2345
2346 weston_surface_commit_subsurface_order(surface);
2347
2348 weston_surface_schedule_repaint(surface);
2349
2350 sub->cached.has_data = 0;
2351}
2352
2353static void
2354weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2355{
2356 struct weston_surface *surface = sub->surface;
2357
2358 /*
2359 * If this commit would cause the surface to move by the
2360 * attach(dx, dy) parameters, the old damage region must be
2361 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002362 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002363 */
2364 pixman_region32_translate(&sub->cached.damage,
2365 -surface->pending.sx, -surface->pending.sy);
2366 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2367 &surface->pending.damage);
2368 empty_region(&surface->pending.damage);
2369
2370 if (surface->pending.newly_attached) {
2371 sub->cached.newly_attached = 1;
2372 weston_buffer_reference(&sub->cached.buffer_ref,
2373 surface->pending.buffer);
2374 }
2375 sub->cached.sx += surface->pending.sx;
2376 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002377
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002378 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002379
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002380 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002381
2382 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2383
2384 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2385
2386 wl_list_insert_list(&sub->cached.frame_callback_list,
2387 &surface->pending.frame_callback_list);
2388 wl_list_init(&surface->pending.frame_callback_list);
2389
2390 sub->cached.has_data = 1;
2391}
2392
2393static int
2394weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2395{
2396 while (sub) {
2397 if (sub->synchronized)
2398 return 1;
2399
2400 if (!sub->parent)
2401 return 0;
2402
2403 sub = weston_surface_to_subsurface(sub->parent);
2404 }
2405
2406 return 0;
2407}
2408
2409static void
2410weston_subsurface_commit(struct weston_subsurface *sub)
2411{
2412 struct weston_surface *surface = sub->surface;
2413 struct weston_subsurface *tmp;
2414
2415 /* Recursive check for effectively synchronized. */
2416 if (weston_subsurface_is_synchronized(sub)) {
2417 weston_subsurface_commit_to_cache(sub);
2418 } else {
2419 if (sub->cached.has_data) {
2420 /* flush accumulated state from cache */
2421 weston_subsurface_commit_to_cache(sub);
2422 weston_subsurface_commit_from_cache(sub);
2423 } else {
2424 weston_surface_commit(surface);
2425 }
2426
2427 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2428 if (tmp->surface != surface)
2429 weston_subsurface_parent_commit(tmp, 0);
2430 }
2431 }
2432}
2433
2434static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002435weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002436{
2437 struct weston_surface *surface = sub->surface;
2438 struct weston_subsurface *tmp;
2439
Pekka Paalanene67858b2013-04-25 13:57:42 +03002440 /* From now on, commit_from_cache the whole sub-tree, regardless of
2441 * the synchronized mode of each child. This sub-surface or some
2442 * of its ancestors were synchronized, so we are synchronized
2443 * all the way down.
2444 */
2445
2446 if (sub->cached.has_data)
2447 weston_subsurface_commit_from_cache(sub);
2448
2449 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2450 if (tmp->surface != surface)
2451 weston_subsurface_parent_commit(tmp, 1);
2452 }
2453}
2454
2455static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002456weston_subsurface_parent_commit(struct weston_subsurface *sub,
2457 int parent_is_synchronized)
2458{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002459 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002460 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002461 wl_list_for_each(view, &sub->surface->views, surface_link)
2462 weston_view_set_position(view,
2463 sub->position.x,
2464 sub->position.y);
2465
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002466 sub->position.set = 0;
2467 }
2468
2469 if (parent_is_synchronized || sub->synchronized)
2470 weston_subsurface_synchronized_commit(sub);
2471}
2472
2473static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002474subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002475{
2476 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002477 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002478
Jason Ekstranda7af7042013-10-12 22:38:11 -05002479 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002480 weston_view_set_position(view,
2481 view->geometry.x + dx,
2482 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002483
2484 /* No need to check parent mappedness, because if parent is not
2485 * mapped, parent is not in a visible layer, so this sub-surface
2486 * will not be drawn either.
2487 */
2488 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002489 /* Cannot call weston_surface_update_transform(),
2490 * because that would call it also for the parent surface,
2491 * which might not be mapped yet. That would lead to
2492 * inconsistent state, where the window could never be
2493 * mapped.
2494 *
2495 * Instead just assing any output, to make
2496 * weston_surface_is_mapped() return true, so that when the
2497 * parent surface does get mapped, this one will get
2498 * included, too. See surface_list_add().
2499 */
2500 assert(!wl_list_empty(&compositor->output_list));
2501 surface->output = container_of(compositor->output_list.next,
2502 struct weston_output, link);
2503 }
2504}
2505
2506static struct weston_subsurface *
2507weston_surface_to_subsurface(struct weston_surface *surface)
2508{
2509 if (surface->configure == subsurface_configure)
2510 return surface->configure_private;
2511
2512 return NULL;
2513}
2514
Pekka Paalanen01388e22013-04-25 13:57:44 +03002515WL_EXPORT struct weston_surface *
2516weston_surface_get_main_surface(struct weston_surface *surface)
2517{
2518 struct weston_subsurface *sub;
2519
2520 while (surface && (sub = weston_surface_to_subsurface(surface)))
2521 surface = sub->parent;
2522
2523 return surface;
2524}
2525
Pekka Paalanene67858b2013-04-25 13:57:42 +03002526static void
2527subsurface_set_position(struct wl_client *client,
2528 struct wl_resource *resource, int32_t x, int32_t y)
2529{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002530 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002531
2532 if (!sub)
2533 return;
2534
2535 sub->position.x = x;
2536 sub->position.y = y;
2537 sub->position.set = 1;
2538}
2539
2540static struct weston_subsurface *
2541subsurface_from_surface(struct weston_surface *surface)
2542{
2543 struct weston_subsurface *sub;
2544
2545 sub = weston_surface_to_subsurface(surface);
2546 if (sub)
2547 return sub;
2548
2549 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2550 if (sub->surface == surface)
2551 return sub;
2552
2553 return NULL;
2554}
2555
2556static struct weston_subsurface *
2557subsurface_sibling_check(struct weston_subsurface *sub,
2558 struct weston_surface *surface,
2559 const char *request)
2560{
2561 struct weston_subsurface *sibling;
2562
2563 sibling = subsurface_from_surface(surface);
2564
2565 if (!sibling) {
2566 wl_resource_post_error(sub->resource,
2567 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2568 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002569 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570 return NULL;
2571 }
2572
2573 if (sibling->parent != sub->parent) {
2574 wl_resource_post_error(sub->resource,
2575 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2576 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002577 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002578 return NULL;
2579 }
2580
2581 return sibling;
2582}
2583
2584static void
2585subsurface_place_above(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_above");
2598 if (!sibling)
2599 return;
2600
2601 wl_list_remove(&sub->parent_link_pending);
2602 wl_list_insert(sibling->parent_link_pending.prev,
2603 &sub->parent_link_pending);
2604}
2605
2606static void
2607subsurface_place_below(struct wl_client *client,
2608 struct wl_resource *resource,
2609 struct wl_resource *sibling_resource)
2610{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002611 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002612 struct weston_surface *surface =
2613 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002614 struct weston_subsurface *sibling;
2615
2616 if (!sub)
2617 return;
2618
2619 sibling = subsurface_sibling_check(sub, surface, "place_below");
2620 if (!sibling)
2621 return;
2622
2623 wl_list_remove(&sub->parent_link_pending);
2624 wl_list_insert(&sibling->parent_link_pending,
2625 &sub->parent_link_pending);
2626}
2627
2628static void
2629subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2630{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002631 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632
2633 if (sub)
2634 sub->synchronized = 1;
2635}
2636
2637static void
2638subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2639{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002640 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002642 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002644
2645 /* If sub became effectively desynchronized, flush. */
2646 if (!weston_subsurface_is_synchronized(sub))
2647 weston_subsurface_synchronized_commit(sub);
2648 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002649}
2650
2651static void
2652weston_subsurface_cache_init(struct weston_subsurface *sub)
2653{
2654 pixman_region32_init(&sub->cached.damage);
2655 pixman_region32_init(&sub->cached.opaque);
2656 pixman_region32_init(&sub->cached.input);
2657 wl_list_init(&sub->cached.frame_callback_list);
2658 sub->cached.buffer_ref.buffer = NULL;
2659}
2660
2661static void
2662weston_subsurface_cache_fini(struct weston_subsurface *sub)
2663{
2664 struct weston_frame_callback *cb, *tmp;
2665
2666 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002667 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002668
2669 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2670 pixman_region32_fini(&sub->cached.damage);
2671 pixman_region32_fini(&sub->cached.opaque);
2672 pixman_region32_fini(&sub->cached.input);
2673}
2674
2675static void
2676weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2677{
2678 wl_list_remove(&sub->parent_link);
2679 wl_list_remove(&sub->parent_link_pending);
2680 wl_list_remove(&sub->parent_destroy_listener.link);
2681 sub->parent = NULL;
2682}
2683
2684static void
2685weston_subsurface_destroy(struct weston_subsurface *sub);
2686
2687static void
2688subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2689{
2690 struct weston_subsurface *sub =
2691 container_of(listener, struct weston_subsurface,
2692 surface_destroy_listener);
2693 assert(data == &sub->surface->resource);
2694
2695 /* The protocol object (wl_resource) is left inert. */
2696 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002697 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002698
2699 weston_subsurface_destroy(sub);
2700}
2701
2702static void
2703subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2704{
2705 struct weston_subsurface *sub =
2706 container_of(listener, struct weston_subsurface,
2707 parent_destroy_listener);
2708 assert(data == &sub->parent->resource);
2709 assert(sub->surface != sub->parent);
2710
2711 if (weston_surface_is_mapped(sub->surface))
2712 weston_surface_unmap(sub->surface);
2713
2714 weston_subsurface_unlink_parent(sub);
2715}
2716
2717static void
2718subsurface_resource_destroy(struct wl_resource *resource)
2719{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002720 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002721
2722 if (sub)
2723 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002724}
2725
2726static void
2727subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2728{
2729 wl_resource_destroy(resource);
2730}
2731
2732static void
2733weston_subsurface_link_parent(struct weston_subsurface *sub,
2734 struct weston_surface *parent)
2735{
2736 sub->parent = parent;
2737 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002738 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002739 &sub->parent_destroy_listener);
2740
2741 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2742 wl_list_insert(&parent->subsurface_list_pending,
2743 &sub->parent_link_pending);
2744}
2745
2746static void
2747weston_subsurface_link_surface(struct weston_subsurface *sub,
2748 struct weston_surface *surface)
2749{
2750 sub->surface = surface;
2751 sub->surface_destroy_listener.notify =
2752 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002753 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002754 &sub->surface_destroy_listener);
2755}
2756
2757static void
2758weston_subsurface_destroy(struct weston_subsurface *sub)
2759{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002760 struct weston_view *view, *next;
2761
Pekka Paalanene67858b2013-04-25 13:57:42 +03002762 assert(sub->surface);
2763
2764 if (sub->resource) {
2765 assert(weston_surface_to_subsurface(sub->surface) == sub);
2766 assert(sub->parent_destroy_listener.notify ==
2767 subsurface_handle_parent_destroy);
2768
George Kiagiadakised04d382014-06-13 18:10:26 +02002769 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2770 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002771 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002772 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002773
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774 if (sub->parent)
2775 weston_subsurface_unlink_parent(sub);
2776
2777 weston_subsurface_cache_fini(sub);
2778
2779 sub->surface->configure = NULL;
2780 sub->surface->configure_private = NULL;
2781 } else {
2782 /* the dummy weston_subsurface for the parent itself */
2783 assert(sub->parent_destroy_listener.notify == NULL);
2784 wl_list_remove(&sub->parent_link);
2785 wl_list_remove(&sub->parent_link_pending);
2786 }
2787
2788 wl_list_remove(&sub->surface_destroy_listener.link);
2789 free(sub);
2790}
2791
2792static const struct wl_subsurface_interface subsurface_implementation = {
2793 subsurface_destroy,
2794 subsurface_set_position,
2795 subsurface_place_above,
2796 subsurface_place_below,
2797 subsurface_set_sync,
2798 subsurface_set_desync
2799};
2800
2801static struct weston_subsurface *
2802weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2803 struct weston_surface *parent)
2804{
2805 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002806 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002807
2808 sub = calloc(1, sizeof *sub);
2809 if (!sub)
2810 return NULL;
2811
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812 wl_list_init(&sub->unused_views);
2813
Jason Ekstranda85118c2013-06-27 20:17:02 -05002814 sub->resource =
2815 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002816 if (!sub->resource) {
2817 free(sub);
2818 return NULL;
2819 }
2820
Jason Ekstranda85118c2013-06-27 20:17:02 -05002821 wl_resource_set_implementation(sub->resource,
2822 &subsurface_implementation,
2823 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824 weston_subsurface_link_surface(sub, surface);
2825 weston_subsurface_link_parent(sub, parent);
2826 weston_subsurface_cache_init(sub);
2827 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828
2829 return sub;
2830}
2831
2832/* Create a dummy subsurface for having the parent itself in its
2833 * sub-surface lists. Makes stacking order manipulation easy.
2834 */
2835static struct weston_subsurface *
2836weston_subsurface_create_for_parent(struct weston_surface *parent)
2837{
2838 struct weston_subsurface *sub;
2839
2840 sub = calloc(1, sizeof *sub);
2841 if (!sub)
2842 return NULL;
2843
2844 weston_subsurface_link_surface(sub, parent);
2845 sub->parent = parent;
2846 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2847 wl_list_insert(&parent->subsurface_list_pending,
2848 &sub->parent_link_pending);
2849
2850 return sub;
2851}
2852
2853static void
2854subcompositor_get_subsurface(struct wl_client *client,
2855 struct wl_resource *resource,
2856 uint32_t id,
2857 struct wl_resource *surface_resource,
2858 struct wl_resource *parent_resource)
2859{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002860 struct weston_surface *surface =
2861 wl_resource_get_user_data(surface_resource);
2862 struct weston_surface *parent =
2863 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002864 struct weston_subsurface *sub;
2865 static const char where[] = "get_subsurface: wl_subsurface@";
2866
2867 if (surface == parent) {
2868 wl_resource_post_error(resource,
2869 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2870 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002871 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002872 return;
2873 }
2874
2875 if (weston_surface_to_subsurface(surface)) {
2876 wl_resource_post_error(resource,
2877 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2878 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002879 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002880 return;
2881 }
2882
2883 if (surface->configure) {
2884 wl_resource_post_error(resource,
2885 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2886 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002887 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002888 return;
2889 }
2890
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002891 if (weston_surface_get_main_surface(parent) == surface) {
2892 wl_resource_post_error(resource,
2893 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2894 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002895 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002896 return;
2897 }
2898
Pekka Paalanene67858b2013-04-25 13:57:42 +03002899 /* make sure the parent is in its own list */
2900 if (wl_list_empty(&parent->subsurface_list)) {
2901 if (!weston_subsurface_create_for_parent(parent)) {
2902 wl_resource_post_no_memory(resource);
2903 return;
2904 }
2905 }
2906
2907 sub = weston_subsurface_create(id, surface, parent);
2908 if (!sub) {
2909 wl_resource_post_no_memory(resource);
2910 return;
2911 }
2912
2913 surface->configure = subsurface_configure;
2914 surface->configure_private = sub;
2915}
2916
2917static void
2918subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2919{
2920 wl_resource_destroy(resource);
2921}
2922
2923static const struct wl_subcompositor_interface subcompositor_interface = {
2924 subcompositor_destroy,
2925 subcompositor_get_subsurface
2926};
2927
2928static void
2929bind_subcompositor(struct wl_client *client,
2930 void *data, uint32_t version, uint32_t id)
2931{
2932 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002933 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002934
Jason Ekstranda85118c2013-06-27 20:17:02 -05002935 resource =
2936 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002937 if (resource == NULL) {
2938 wl_client_post_no_memory(client);
2939 return;
2940 }
2941 wl_resource_set_implementation(resource, &subcompositor_interface,
2942 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002943}
2944
2945static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002946weston_compositor_dpms(struct weston_compositor *compositor,
2947 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002948{
2949 struct weston_output *output;
2950
2951 wl_list_for_each(output, &compositor->output_list, link)
2952 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002953 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002954}
2955
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002956WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002957weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002958{
Neil Roberts8b62e202013-09-30 13:14:47 +01002959 uint32_t old_state = compositor->state;
2960
2961 /* The state needs to be changed before emitting the wake
2962 * signal because that may try to schedule a repaint which
2963 * will not work if the compositor is still sleeping */
2964 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2965
2966 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002967 case WESTON_COMPOSITOR_SLEEPING:
2968 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2969 /* fall through */
2970 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002971 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002972 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002973 /* fall through */
2974 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002975 wl_event_source_timer_update(compositor->idle_source,
2976 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002977 }
2978}
2979
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002980WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002981weston_compositor_offscreen(struct weston_compositor *compositor)
2982{
2983 switch (compositor->state) {
2984 case WESTON_COMPOSITOR_OFFSCREEN:
2985 return;
2986 case WESTON_COMPOSITOR_SLEEPING:
2987 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2988 /* fall through */
2989 default:
2990 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2991 wl_event_source_timer_update(compositor->idle_source, 0);
2992 }
2993}
2994
2995WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002996weston_compositor_sleep(struct weston_compositor *compositor)
2997{
2998 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2999 return;
3000
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003001 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003002 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3003 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3004}
3005
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003006static int
3007idle_handler(void *data)
3008{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003009 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003010
3011 if (compositor->idle_inhibit)
3012 return 1;
3013
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003014 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003015 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003016
3017 return 1;
3018}
3019
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003020WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003021weston_plane_init(struct weston_plane *plane,
3022 struct weston_compositor *ec,
3023 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003024{
3025 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003026 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003027 plane->x = x;
3028 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003029 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003030
3031 /* Init the link so that the call to wl_list_remove() when releasing
3032 * the plane without ever stacking doesn't lead to a crash */
3033 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003034}
3035
3036WL_EXPORT void
3037weston_plane_release(struct weston_plane *plane)
3038{
Xiong Zhang97116532013-10-23 13:58:31 +08003039 struct weston_view *view;
3040
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003041 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003042 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003043
Xiong Zhang97116532013-10-23 13:58:31 +08003044 wl_list_for_each(view, &plane->compositor->view_list, link) {
3045 if (view->plane == plane)
3046 view->plane = NULL;
3047 }
3048
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003049 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003050}
3051
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003052WL_EXPORT void
3053weston_compositor_stack_plane(struct weston_compositor *ec,
3054 struct weston_plane *plane,
3055 struct weston_plane *above)
3056{
3057 if (above)
3058 wl_list_insert(above->link.prev, &plane->link);
3059 else
3060 wl_list_insert(&ec->plane_list, &plane->link);
3061}
3062
Casey Dahlin9074db52012-04-19 22:50:09 -04003063static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003064{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003065 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003066}
3067
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003068static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003069bind_output(struct wl_client *client,
3070 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003071{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003072 struct weston_output *output = data;
3073 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003074 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003075
Jason Ekstranda85118c2013-06-27 20:17:02 -05003076 resource = wl_resource_create(client, &wl_output_interface,
3077 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003078 if (resource == NULL) {
3079 wl_client_post_no_memory(client);
3080 return;
3081 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003082
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003083 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003084 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003085
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003086 wl_output_send_geometry(resource,
3087 output->x,
3088 output->y,
3089 output->mm_width,
3090 output->mm_height,
3091 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003092 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003093 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003094 if (version >= 2)
3095 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003096 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003097
3098 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003099 wl_output_send_mode(resource,
3100 mode->flags,
3101 mode->width,
3102 mode->height,
3103 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003104 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003105
3106 if (version >= 2)
3107 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003108}
3109
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003110/* Move other outputs when one is removed so the space remains contiguos. */
3111static void
3112weston_compositor_remove_output(struct weston_compositor *compositor,
3113 struct weston_output *remove_output)
3114{
3115 struct weston_output *output;
3116 int offset = 0;
3117
3118 wl_list_for_each(output, &compositor->output_list, link) {
3119 if (output == remove_output) {
3120 offset = output->width;
3121 continue;
3122 }
3123
3124 if (offset > 0) {
3125 weston_output_move(output,
3126 output->x - offset, output->y);
3127 output->dirty = 1;
3128 }
3129 }
3130}
3131
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003132WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003133weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003134{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003135 output->destroying = 1;
3136
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003137 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003138 wl_list_remove(&output->link);
3139
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003140 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003141 wl_signal_emit(&output->destroy_signal, output);
3142
Richard Hughesafe690c2013-05-02 10:10:04 +01003143 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003144 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003145 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003146 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003147
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003148 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003149}
3150
Scott Moreau1bad5db2012-08-18 01:04:05 -06003151static void
3152weston_output_compute_transform(struct weston_output *output)
3153{
3154 struct weston_matrix transform;
3155 int flip;
3156
3157 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003158 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003159
3160 switch(output->transform) {
3161 case WL_OUTPUT_TRANSFORM_FLIPPED:
3162 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3163 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3164 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003165 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003166 flip = -1;
3167 break;
3168 default:
3169 flip = 1;
3170 break;
3171 }
3172
3173 switch(output->transform) {
3174 case WL_OUTPUT_TRANSFORM_NORMAL:
3175 case WL_OUTPUT_TRANSFORM_FLIPPED:
3176 transform.d[0] = flip;
3177 transform.d[1] = 0;
3178 transform.d[4] = 0;
3179 transform.d[5] = 1;
3180 break;
3181 case WL_OUTPUT_TRANSFORM_90:
3182 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3183 transform.d[0] = 0;
3184 transform.d[1] = -flip;
3185 transform.d[4] = 1;
3186 transform.d[5] = 0;
3187 break;
3188 case WL_OUTPUT_TRANSFORM_180:
3189 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3190 transform.d[0] = -flip;
3191 transform.d[1] = 0;
3192 transform.d[4] = 0;
3193 transform.d[5] = -1;
3194 break;
3195 case WL_OUTPUT_TRANSFORM_270:
3196 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3197 transform.d[0] = 0;
3198 transform.d[1] = flip;
3199 transform.d[4] = -1;
3200 transform.d[5] = 0;
3201 break;
3202 default:
3203 break;
3204 }
3205
3206 weston_matrix_multiply(&output->matrix, &transform);
3207}
3208
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003209WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003210weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003211{
Scott Moreau850ca422012-05-21 15:21:25 -06003212 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003213
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003214 weston_matrix_init(&output->matrix);
3215 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003216 -(output->x + output->width / 2.0),
3217 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003218
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003219 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003220 2.0 / output->width,
3221 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003222
Scott Moreauccbf29d2012-02-22 14:21:41 -07003223 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003224 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003225 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003226 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3227 output->zoom.trans_y, 0);
3228 weston_matrix_scale(&output->matrix, magnification,
3229 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003230 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003231
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003232 weston_output_compute_transform(output);
3233
Scott Moreauccbf29d2012-02-22 14:21:41 -07003234 output->dirty = 0;
3235}
3236
Scott Moreau1bad5db2012-08-18 01:04:05 -06003237static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003238weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003239{
3240 output->transform = transform;
3241
3242 switch (transform) {
3243 case WL_OUTPUT_TRANSFORM_90:
3244 case WL_OUTPUT_TRANSFORM_270:
3245 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3246 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3247 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003248 output->width = output->current_mode->height;
3249 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003250 break;
3251 case WL_OUTPUT_TRANSFORM_NORMAL:
3252 case WL_OUTPUT_TRANSFORM_180:
3253 case WL_OUTPUT_TRANSFORM_FLIPPED:
3254 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003255 output->width = output->current_mode->width;
3256 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003257 break;
3258 default:
3259 break;
3260 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003261
Hardening57388e42013-09-18 23:56:36 +02003262 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003263 output->width /= scale;
3264 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003265}
3266
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003267static void
3268weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003269{
3270 output->x = x;
3271 output->y = y;
3272
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003273 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003274 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003275 output->width,
3276 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003277}
3278
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003279WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003280weston_output_move(struct weston_output *output, int x, int y)
3281{
3282 pixman_region32_t old_region;
3283 struct wl_resource *resource;
3284
3285 output->move_x = x - output->x;
3286 output->move_y = y - output->y;
3287
3288 if (output->move_x == 0 && output->move_y == 0)
3289 return;
3290
3291 pixman_region32_init(&old_region);
3292 pixman_region32_copy(&old_region, &output->region);
3293
3294 weston_output_init_geometry(output, x, y);
3295
3296 output->dirty = 1;
3297
3298 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003299 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003300
3301 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003302 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003303 wl_output_send_geometry(resource,
3304 output->x,
3305 output->y,
3306 output->mm_width,
3307 output->mm_height,
3308 output->subpixel,
3309 output->make,
3310 output->model,
3311 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003312
3313 if (wl_resource_get_version(resource) >= 2)
3314 wl_output_send_done(resource);
3315 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003316}
3317
3318WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003319weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003320 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003321 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003322{
3323 output->compositor = c;
3324 output->x = x;
3325 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003326 output->mm_width = mm_width;
3327 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003328 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003329 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003330
Alexander Larsson0b135062013-05-28 16:23:36 +02003331 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003332 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003333
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003334 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003335 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003336
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003337 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003338 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003339 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003340 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003341
Casey Dahlin58ba1372012-04-19 22:50:08 -04003342 output->id = ffs(~output->compositor->output_id_pool) - 1;
3343 output->compositor->output_id_pool |= 1 << output->id;
3344
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003345 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003346 wl_global_create(c->wl_display, &wl_output_interface, 2,
3347 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003348 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003349}
3350
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003351WL_EXPORT void
3352weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003353 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003354 wl_fixed_t *x, wl_fixed_t *y)
3355{
3356 wl_fixed_t tx, ty;
3357 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003358 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003359
Hardeningff39efa2013-09-18 23:56:35 +02003360 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3361 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003362
3363 switch(output->transform) {
3364 case WL_OUTPUT_TRANSFORM_NORMAL:
3365 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003366 tx = device_x;
3367 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003368 break;
3369 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003370 tx = device_y;
3371 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003372 break;
3373 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003374 tx = width - device_x;
3375 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003376 break;
3377 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003378 tx = width - device_y;
3379 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003380 break;
3381 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003382 tx = width - device_x;
3383 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003384 break;
3385 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003386 tx = width - device_y;
3387 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003388 break;
3389 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003390 tx = device_x;
3391 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003392 break;
3393 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003394 tx = device_y;
3395 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003396 break;
3397 }
3398
Neil Robertseb5a2002014-05-01 16:13:55 +01003399 tx /= output->current_scale;
3400 ty /= output->current_scale;
3401
3402 if (output->zoom.active) {
3403 zoom_scale = output->zoom.spring_z.current;
3404 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3405 output->width / 2.0f *
3406 (zoom_scale + output->zoom.trans_x));
3407 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3408 output->height / 2.0f *
3409 (zoom_scale + output->zoom.trans_y));
3410 tx = wl_fixed_from_double(zx);
3411 ty = wl_fixed_from_double(zy);
3412 }
3413
3414 *x = tx + wl_fixed_from_int(output->x);
3415 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003416}
3417
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003418static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003419destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003420{
Jonny Lamb74130762013-11-26 18:19:46 +01003421 struct weston_surface *surface =
3422 wl_resource_get_user_data(resource);
3423
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003424 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003425 surface->pending.buffer_viewport.buffer.src_width =
3426 wl_fixed_from_int(-1);
3427 surface->pending.buffer_viewport.surface.width = -1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003428}
3429
3430static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003431viewport_destroy(struct wl_client *client,
3432 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003433{
3434 wl_resource_destroy(resource);
3435}
3436
3437static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003438viewport_set(struct wl_client *client,
3439 struct wl_resource *resource,
3440 wl_fixed_t src_x,
3441 wl_fixed_t src_y,
3442 wl_fixed_t src_width,
3443 wl_fixed_t src_height,
3444 int32_t dst_width,
3445 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003446{
Jonny Lamb74130762013-11-26 18:19:46 +01003447 struct weston_surface *surface =
3448 wl_resource_get_user_data(resource);
3449
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003450 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003451
Jonny Lamb8ae35902013-11-26 18:19:45 +01003452 if (wl_fixed_to_double(src_width) < 0 ||
3453 wl_fixed_to_double(src_height) < 0) {
3454 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003455 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003456 "source dimensions must be non-negative (%fx%f)",
3457 wl_fixed_to_double(src_width),
3458 wl_fixed_to_double(src_height));
3459 return;
3460 }
3461
3462 if (dst_width <= 0 || dst_height <= 0) {
3463 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003464 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003465 "destination dimensions must be positive (%dx%d)",
3466 dst_width, dst_height);
3467 return;
3468 }
3469
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003470 surface->pending.buffer_viewport.buffer.src_x = src_x;
3471 surface->pending.buffer_viewport.buffer.src_y = src_y;
3472 surface->pending.buffer_viewport.buffer.src_width = src_width;
3473 surface->pending.buffer_viewport.buffer.src_height = src_height;
3474 surface->pending.buffer_viewport.surface.width = dst_width;
3475 surface->pending.buffer_viewport.surface.height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003476}
3477
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003478static void
3479viewport_set_source(struct wl_client *client,
3480 struct wl_resource *resource,
3481 wl_fixed_t src_x,
3482 wl_fixed_t src_y,
3483 wl_fixed_t src_width,
3484 wl_fixed_t src_height)
3485{
3486 struct weston_surface *surface =
3487 wl_resource_get_user_data(resource);
3488
3489 assert(surface->viewport_resource != NULL);
3490
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003491 if (src_width == wl_fixed_from_int(-1) &&
3492 src_height == wl_fixed_from_int(-1)) {
3493 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003494 surface->pending.buffer_viewport.buffer.src_width =
3495 wl_fixed_from_int(-1);
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003496 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003497 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003498
3499 if (src_width <= 0 || src_height <= 0) {
3500 wl_resource_post_error(resource,
3501 WL_VIEWPORT_ERROR_BAD_VALUE,
3502 "source size must be positive (%fx%f)",
3503 wl_fixed_to_double(src_width),
3504 wl_fixed_to_double(src_height));
3505 return;
3506 }
3507
3508 surface->pending.buffer_viewport.buffer.src_x = src_x;
3509 surface->pending.buffer_viewport.buffer.src_y = src_y;
3510 surface->pending.buffer_viewport.buffer.src_width = src_width;
3511 surface->pending.buffer_viewport.buffer.src_height = src_height;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003512}
3513
3514static void
3515viewport_set_destination(struct wl_client *client,
3516 struct wl_resource *resource,
3517 int32_t dst_width,
3518 int32_t dst_height)
3519{
3520 struct weston_surface *surface =
3521 wl_resource_get_user_data(resource);
3522
3523 assert(surface->viewport_resource != NULL);
3524
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003525 if (dst_width == -1 && dst_height == -1) {
3526 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003527 surface->pending.buffer_viewport.surface.width = -1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003528 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003529 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003530
3531 if (dst_width <= 0 || dst_height <= 0) {
3532 wl_resource_post_error(resource,
3533 WL_VIEWPORT_ERROR_BAD_VALUE,
3534 "destination size must be positive (%dx%d)",
3535 dst_width, dst_height);
3536 return;
3537 }
3538
3539 surface->pending.buffer_viewport.surface.width = dst_width;
3540 surface->pending.buffer_viewport.surface.height = dst_height;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003541}
3542
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003543static const struct wl_viewport_interface viewport_interface = {
3544 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003545 viewport_set,
3546 viewport_set_source,
3547 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003548};
3549
3550static void
3551scaler_destroy(struct wl_client *client,
3552 struct wl_resource *resource)
3553{
3554 wl_resource_destroy(resource);
3555}
3556
3557static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003558scaler_get_viewport(struct wl_client *client,
3559 struct wl_resource *scaler,
3560 uint32_t id,
3561 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003562{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003563 int version = wl_resource_get_version(scaler);
3564 struct weston_surface *surface =
3565 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003566 struct wl_resource *resource;
3567
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003568 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003569 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003570 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3571 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003572 return;
3573 }
3574
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003575 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003576 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003577 if (resource == NULL) {
3578 wl_client_post_no_memory(client);
3579 return;
3580 }
3581
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003582 wl_resource_set_implementation(resource, &viewport_interface,
3583 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003584
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003585 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003586}
3587
3588static const struct wl_scaler_interface scaler_interface = {
3589 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003590 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003591};
3592
3593static void
3594bind_scaler(struct wl_client *client,
3595 void *data, uint32_t version, uint32_t id)
3596{
3597 struct wl_resource *resource;
3598
3599 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003600 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003601 if (resource == NULL) {
3602 wl_client_post_no_memory(client);
3603 return;
3604 }
3605
3606 wl_resource_set_implementation(resource, &scaler_interface,
3607 NULL, NULL);
3608}
3609
3610static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003611compositor_bind(struct wl_client *client,
3612 void *data, uint32_t version, uint32_t id)
3613{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003614 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003615 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003616
Jason Ekstranda85118c2013-06-27 20:17:02 -05003617 resource = wl_resource_create(client, &wl_compositor_interface,
3618 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003619 if (resource == NULL) {
3620 wl_client_post_no_memory(client);
3621 return;
3622 }
3623
3624 wl_resource_set_implementation(resource, &compositor_interface,
3625 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003626}
3627
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003628static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003629log_uname(void)
3630{
3631 struct utsname usys;
3632
3633 uname(&usys);
3634
3635 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3636 usys.version, usys.machine);
3637}
3638
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003639WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003640weston_environment_get_fd(const char *env)
3641{
3642 char *e, *end;
3643 int fd, flags;
3644
3645 e = getenv(env);
3646 if (!e)
3647 return -1;
3648 fd = strtol(e, &end, 0);
3649 if (*end != '\0')
3650 return -1;
3651
3652 flags = fcntl(fd, F_GETFD);
3653 if (flags == -1)
3654 return -1;
3655
3656 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3657 unsetenv(env);
3658
3659 return fd;
3660}
3661
3662WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003663weston_compositor_init(struct weston_compositor *ec,
3664 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003665 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003666 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003667{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003668 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003669 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003670 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003671
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003672 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003673 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003674 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003675 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003676 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003677 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003678 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003679 wl_signal_init(&ec->idle_signal);
3680 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003681 wl_signal_init(&ec->show_input_panel_signal);
3682 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003683 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003684 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003685 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003686 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003687 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003688 wl_signal_init(&ec->session_signal);
3689 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003690
Casey Dahlin58ba1372012-04-19 22:50:08 -04003691 ec->output_id_pool = 0;
3692
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003693 if (!wl_global_create(display, &wl_compositor_interface, 3,
3694 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003695 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003696
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003697 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3698 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003699 return -1;
3700
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003701 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003702 ec, bind_scaler))
3703 return -1;
3704
Jason Ekstranda7af7042013-10-12 22:38:11 -05003705 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003706 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003707 wl_list_init(&ec->layer_list);
3708 wl_list_init(&ec->seat_list);
3709 wl_list_init(&ec->output_list);
3710 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003711 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003712 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003713 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003714 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003715 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003716
Xiong Zhang97116532013-10-23 13:58:31 +08003717 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003718 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003719
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003720 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3721 weston_config_section_get_string(s, "keymap_rules",
3722 (char **) &xkb_names.rules, NULL);
3723 weston_config_section_get_string(s, "keymap_model",
3724 (char **) &xkb_names.model, NULL);
3725 weston_config_section_get_string(s, "keymap_layout",
3726 (char **) &xkb_names.layout, NULL);
3727 weston_config_section_get_string(s, "keymap_variant",
3728 (char **) &xkb_names.variant, NULL);
3729 weston_config_section_get_string(s, "keymap_options",
3730 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003731
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003732 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3733 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003734
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003735 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003736
3737 wl_data_device_manager_init(ec->wl_display);
3738
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003739 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003740
Daniel Stone725c2c32012-06-22 14:04:36 +01003741 loop = wl_display_get_event_loop(ec->wl_display);
3742 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3743 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3744
3745 ec->input_loop = wl_event_loop_create();
3746
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003747 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3748 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3749
3750 weston_compositor_schedule_repaint(ec);
3751
Daniel Stone725c2c32012-06-22 14:04:36 +01003752 return 0;
3753}
3754
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003755WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003756weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003757{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003758 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003759
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003760 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003761 if (ec->input_loop_source)
3762 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003763
Matt Roper361d2ad2011-08-29 13:52:23 -07003764 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003765 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003766 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003767
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003768 if (ec->renderer)
3769 ec->renderer->destroy(ec);
3770
Daniel Stone325fc2d2012-05-30 16:31:58 +01003771 weston_binding_list_destroy_all(&ec->key_binding_list);
3772 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003773 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003774 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003775 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003776
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003777 weston_plane_release(&ec->primary_plane);
3778
Jonas Ådahlc97af922012-03-28 22:36:09 +02003779 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003780
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003781 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003782}
3783
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003784WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003785weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3786 const struct weston_pointer_grab_interface *interface)
3787{
3788 struct weston_seat *seat;
3789
3790 ec->default_pointer_grab = interface;
3791 wl_list_for_each(seat, &ec->seat_list, link) {
3792 if (seat->pointer) {
3793 weston_pointer_set_default_grab(seat->pointer,
3794 interface);
3795 }
3796 }
3797}
3798
3799WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003800weston_version(int *major, int *minor, int *micro)
3801{
3802 *major = WESTON_VERSION_MAJOR;
3803 *minor = WESTON_VERSION_MINOR;
3804 *micro = WESTON_VERSION_MICRO;
3805}
3806
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003807static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003808 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003809 const char *desc;
3810} capability_strings[] = {
3811 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003812 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003813};
3814
3815static void
3816weston_compositor_log_capabilities(struct weston_compositor *compositor)
3817{
3818 unsigned i;
3819 int yes;
3820
3821 weston_log("Compositor capabilities:\n");
3822 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3823 yes = compositor->capabilities & capability_strings[i].bit;
3824 weston_log_continue(STAMP_SPACE "%s %s\n",
3825 capability_strings[i].desc,
3826 yes ? "yes" : "no");
3827 }
3828}
3829
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003830static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003831{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003832 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003833
Martin Minarik6d118362012-06-07 18:01:59 +02003834 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003835 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003836
3837 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003838}
3839
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003840#ifdef HAVE_LIBUNWIND
3841
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003842static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003843print_backtrace(void)
3844{
3845 unw_cursor_t cursor;
3846 unw_context_t context;
3847 unw_word_t off;
3848 unw_proc_info_t pip;
3849 int ret, i = 0;
3850 char procname[256];
3851 const char *filename;
3852 Dl_info dlinfo;
3853
3854 pip.unwind_info = NULL;
3855 ret = unw_getcontext(&context);
3856 if (ret) {
3857 weston_log("unw_getcontext: %d\n", ret);
3858 return;
3859 }
3860
3861 ret = unw_init_local(&cursor, &context);
3862 if (ret) {
3863 weston_log("unw_init_local: %d\n", ret);
3864 return;
3865 }
3866
3867 ret = unw_step(&cursor);
3868 while (ret > 0) {
3869 ret = unw_get_proc_info(&cursor, &pip);
3870 if (ret) {
3871 weston_log("unw_get_proc_info: %d\n", ret);
3872 break;
3873 }
3874
3875 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3876 if (ret && ret != -UNW_ENOMEM) {
3877 if (ret != -UNW_EUNSPEC)
3878 weston_log("unw_get_proc_name: %d\n", ret);
3879 procname[0] = '?';
3880 procname[1] = 0;
3881 }
3882
3883 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3884 *dlinfo.dli_fname)
3885 filename = dlinfo.dli_fname;
3886 else
3887 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003888
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003889 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3890 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3891
3892 ret = unw_step(&cursor);
3893 if (ret < 0)
3894 weston_log("unw_step: %d\n", ret);
3895 }
3896}
3897
3898#else
3899
3900static void
3901print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003902{
3903 void *buffer[32];
3904 int i, count;
3905 Dl_info info;
3906
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003907 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3908 for (i = 0; i < count; i++) {
3909 dladdr(buffer[i], &info);
3910 weston_log(" [%016lx] %s (%s)\n",
3911 (long) buffer[i],
3912 info.dli_sname ? info.dli_sname : "--",
3913 info.dli_fname);
3914 }
3915}
3916
3917#endif
3918
3919static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003920on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003921{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003922 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003923 * then call the backend restore function, which will switch
3924 * back to the vt we launched from or ungrab X etc and then
3925 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003926 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003927 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003928 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003929
Peter Maatmane5b42e42013-03-27 22:38:53 +01003930 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003931
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003932 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003933
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003934 segv_compositor->restore(segv_compositor);
3935
3936 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003937}
3938
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003939WL_EXPORT void *
3940weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003941{
3942 char path[PATH_MAX];
3943 void *module, *init;
3944
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003945 if (name == NULL)
3946 return NULL;
3947
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003948 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003949 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003950 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003951 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003952
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003953 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3954 if (module) {
3955 weston_log("Module '%s' already loaded\n", path);
3956 dlclose(module);
3957 return NULL;
3958 }
3959
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003960 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003961 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003962 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003963 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003964 return NULL;
3965 }
3966
3967 init = dlsym(module, entrypoint);
3968 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003969 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003970 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003971 return NULL;
3972 }
3973
3974 return init;
3975}
3976
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003977static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003978load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003979 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003980{
3981 const char *p, *end;
3982 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003983 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003984 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003985
3986 if (modules == NULL)
3987 return 0;
3988
3989 p = modules;
3990 while (*p) {
3991 end = strchrnul(p, ',');
3992 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003993 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003994 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003995 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003996 p = end;
3997 while (*p == ',')
3998 p++;
3999
4000 }
4001
4002 return 0;
4003}
4004
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004005static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004006 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4007
4008static const char xdg_wrong_message[] =
4009 "fatal: environment variable XDG_RUNTIME_DIR\n"
4010 "is set to \"%s\", which is not a directory.\n";
4011
4012static const char xdg_wrong_mode_message[] =
4013 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004014 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4015 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004016
4017static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004018 "Refer to your distribution on how to get it, or\n"
4019 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4020 "on how to implement it.\n";
4021
Martin Minarik37032f82012-06-18 20:15:18 +02004022static void
4023verify_xdg_runtime_dir(void)
4024{
4025 char *dir = getenv("XDG_RUNTIME_DIR");
4026 struct stat s;
4027
4028 if (!dir) {
4029 weston_log(xdg_error_message);
4030 weston_log_continue(xdg_detail_message);
4031 exit(EXIT_FAILURE);
4032 }
4033
4034 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4035 weston_log(xdg_wrong_message, dir);
4036 weston_log_continue(xdg_detail_message);
4037 exit(EXIT_FAILURE);
4038 }
4039
4040 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4041 weston_log(xdg_wrong_mode_message,
4042 dir, s.st_mode & 0777, s.st_uid);
4043 weston_log_continue(xdg_detail_message);
4044 }
4045}
4046
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004047static int
4048usage(int error_code)
4049{
4050 fprintf(stderr,
4051 "Usage: weston [OPTIONS]\n\n"
4052 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4053 "Weston supports multiple backends, and depending on which backend is in use\n"
4054 "different options will be accepted.\n\n"
4055
4056
4057 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004058 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004059 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004060 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4061 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004062 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004063 " -S, --socket=NAME\tName of socket to listen on\n"
4064 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004065 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004066 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004067 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004068 " -h, --help\t\tThis help message\n\n");
4069
4070 fprintf(stderr,
4071 "Options for drm-backend.so:\n\n"
4072 " --connector=ID\tBring up only this connector\n"
4073 " --seat=SEAT\t\tThe seat that weston should run on\n"
4074 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004075 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004076 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4077
4078 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004079 "Options for fbdev-backend.so:\n\n"
4080 " --tty=TTY\t\tThe tty to use\n"
4081 " --device=DEVICE\tThe framebuffer device to use\n\n");
4082
4083 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004084 "Options for x11-backend.so:\n\n"
4085 " --width=WIDTH\t\tWidth of X window\n"
4086 " --height=HEIGHT\tHeight of X window\n"
4087 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004088 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004089 " --output-count=COUNT\tCreate multiple outputs\n"
4090 " --no-input\t\tDont create input devices\n\n");
4091
4092 fprintf(stderr,
4093 "Options for wayland-backend.so:\n\n"
4094 " --width=WIDTH\t\tWidth of Wayland surface\n"
4095 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004096 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004097 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004098 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004099 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004100 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004101 " --display=DISPLAY\tWayland display to connect to\n\n");
4102
Pekka Paalanene31e0532013-05-22 18:03:07 +03004103#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4104 fprintf(stderr,
4105 "Options for rpi-backend.so:\n\n"
4106 " --tty=TTY\t\tThe tty to use\n"
4107 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4108 " --transform=TR\tThe output transformation, TR is one of:\n"
4109 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004110 " --opaque-regions\tEnable support for opaque regions, can be "
4111 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004112 "\n");
4113#endif
4114
Hardeningc077a842013-07-08 00:51:35 +02004115#if defined(BUILD_RDP_COMPOSITOR)
4116 fprintf(stderr,
4117 "Options for rdp-backend.so:\n\n"
4118 " --width=WIDTH\t\tWidth of desktop\n"
4119 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004120 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4121 " --address=ADDR\tThe address to bind\n"
4122 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004123 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004124 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4125 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4126 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4127 "\n");
4128#endif
4129
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004130 exit(error_code);
4131}
4132
Peter Maatmane5b42e42013-03-27 22:38:53 +01004133static void
4134catch_signals(void)
4135{
4136 struct sigaction action;
4137
4138 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4139 action.sa_sigaction = on_caught_signal;
4140 sigemptyset(&action.sa_mask);
4141 sigaction(SIGSEGV, &action, NULL);
4142 sigaction(SIGABRT, &action, NULL);
4143}
4144
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004145static void
4146handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4147{
4148 struct wl_client *client = data;
4149
4150 weston_log("Primary client died. Closing...\n");
4151
4152 wl_display_terminate(wl_client_get_display(client));
4153}
4154
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004155int main(int argc, char *argv[])
4156{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004157 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004158 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004159 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004160 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004161 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004162 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004163 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004164 int *argc, char *argv[],
4165 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004166 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004167 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004168 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004169 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004170 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004171 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004172 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004173 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004174 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004175 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004176 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004177 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004178 int32_t noconfig = 0;
4179 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004180 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004181 struct wl_client *primary_client;
4182 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004183
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004184 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004185 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004186 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004187 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4188 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004189 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004190 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004191 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004192 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004193 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004194 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004195
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004196 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004197
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004198 if (help)
4199 usage(EXIT_SUCCESS);
4200
Scott Moreau12245142012-08-29 15:15:58 -06004201 if (version) {
4202 printf(PACKAGE_STRING "\n");
4203 return EXIT_SUCCESS;
4204 }
4205
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004206 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004207
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004208 weston_log("%s\n"
4209 STAMP_SPACE "%s\n"
4210 STAMP_SPACE "Bug reports to: %s\n"
4211 STAMP_SPACE "Build: %s\n",
4212 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004213 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004214 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004215
Martin Minarik37032f82012-06-18 20:15:18 +02004216 verify_xdg_runtime_dir();
4217
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004218 display = wl_display_create();
4219
Tiago Vignatti2116b892011-08-08 05:52:59 -07004220 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004221 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4222 display);
4223 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4224 display);
4225 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4226 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004227
4228 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004229 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4230 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004231
Pekka Paalanen588bee12014-05-07 16:26:25 +03004232 if (noconfig == 0)
4233 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004234 if (config != NULL) {
4235 weston_log("Using config file '%s'\n",
4236 weston_config_get_full_path(config));
4237 } else {
4238 weston_log("Starting with no config file.\n");
4239 }
4240 section = weston_config_get_section(config, "core", NULL, NULL);
4241
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004242 if (option_backend)
4243 backend = strdup(option_backend);
4244 else
4245 weston_config_section_get_string(section, "backend", &backend,
4246 NULL);
4247
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004248 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004249 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004250 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004251 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004252 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004253 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004254 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004255 }
4256
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004257 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004258 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004259 if (!backend_init)
4260 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004261
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004262 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004263 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004264 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004265 exit(EXIT_FAILURE);
4266 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004267
Peter Maatmane5b42e42013-03-27 22:38:53 +01004268 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004269 segv_compositor = ec;
4270
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004271 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004272 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004273
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004274 setenv("WAYLAND_DISPLAY", socket_name, 1);
4275
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004276 if (option_shell)
4277 shell = strdup(option_shell);
4278 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004279 weston_config_section_get_string(section, "shell", &shell,
4280 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004281
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004282 if (load_modules(ec, shell, &argc, argv) < 0) {
4283 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004284 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004285 }
4286 free(shell);
4287
4288 weston_config_section_get_string(section, "modules", &modules, "");
4289 if (load_modules(ec, modules, &argc, argv) < 0) {
4290 free(modules);
4291 goto out;
4292 }
4293 free(modules);
4294
Ossama Othmana50e6e42013-05-14 09:48:26 -07004295 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004296 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004297
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004298 for (i = 1; i < argc; i++)
4299 weston_log("fatal: unhandled option: %s\n", argv[i]);
4300 if (argc > 1) {
4301 ret = EXIT_FAILURE;
4302 goto out;
4303 }
4304
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004305 weston_compositor_log_capabilities(ec);
4306
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004307 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4308 if (server_socket) {
4309 weston_log("Running with single client\n");
4310 fd = strtol(server_socket, &end, 0);
4311 if (*end != '\0')
4312 fd = -1;
4313 } else {
4314 fd = -1;
4315 }
4316
4317 if (fd != -1) {
4318 primary_client = wl_client_create(display, fd);
4319 if (!primary_client) {
4320 weston_log("fatal: failed to add client: %m\n");
4321 ret = EXIT_FAILURE;
4322 goto out;
4323 }
4324 primary_client_destroyed.notify =
4325 handle_primary_client_destroyed;
4326 wl_client_add_destroy_listener(primary_client,
4327 &primary_client_destroyed);
4328 } else {
4329 if (wl_display_add_socket(display, socket_name)) {
4330 weston_log("fatal: failed to add socket: %m\n");
4331 ret = EXIT_FAILURE;
4332 goto out;
4333 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004334 }
4335
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004336 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004337
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004338 wl_display_run(display);
4339
4340 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004341 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004342 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004343
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004344 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004345
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004346 for (i = ARRAY_LENGTH(signals); i;)
4347 wl_event_source_remove(signals[--i]);
4348
Daniel Stone855028f2012-05-01 20:37:10 +01004349 weston_compositor_xkb_destroy(ec);
4350
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004351 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004352 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004353
Martin Minarik19e6f262012-06-07 13:08:46 +02004354 weston_log_file_close();
4355
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004356 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004357}