blob: 82edf35600fb59366c8e69b77dd2ffca31ea2ed6 [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;
644
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200645 if (vp->buffer.src_width != wl_fixed_from_int(-1) &&
646 vp->surface.width != -1) {
Jonny Lamb74130762013-11-26 18:19:46 +0100647 double a, b;
648
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200649 a = sx / vp->surface.width;
650 b = a * wl_fixed_to_double(vp->buffer.src_width);
651 *bx = b + wl_fixed_to_double(vp->buffer.src_x);
Jonny Lamb74130762013-11-26 18:19:46 +0100652
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200653 a = sy / vp->surface.height;
654 b = a * wl_fixed_to_double(vp->buffer.src_height);
655 *by = b + wl_fixed_to_double(vp->buffer.src_y);
Jonny Lamb74130762013-11-26 18:19:46 +0100656 } else {
657 *bx = sx;
658 *by = sy;
659 }
660}
661
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500662WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200663weston_surface_to_buffer_float(struct weston_surface *surface,
664 float sx, float sy, float *bx, float *by)
665{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200666 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
667
Jonny Lamb74130762013-11-26 18:19:46 +0100668 /* first transform coordinates if the scaler is set */
669 scaler_surface_to_buffer(surface, sx, sy, bx, by);
670
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200671 weston_transformed_coord(surface->width, surface->height,
672 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100673 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200674}
675
Alexander Larsson4ea95522013-05-22 14:41:37 +0200676WL_EXPORT void
677weston_surface_to_buffer(struct weston_surface *surface,
678 int sx, int sy, int *bx, int *by)
679{
680 float bxf, byf;
681
Jonny Lamb74130762013-11-26 18:19:46 +0100682 weston_surface_to_buffer_float(surface,
683 sx, sy, &bxf, &byf);
684
Alexander Larsson4ea95522013-05-22 14:41:37 +0200685 *bx = floorf(bxf);
686 *by = floorf(byf);
687}
688
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200689WL_EXPORT pixman_box32_t
690weston_surface_to_buffer_rect(struct weston_surface *surface,
691 pixman_box32_t rect)
692{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200693 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100694 float xf, yf;
695
696 /* first transform box coordinates if the scaler is set */
697 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
698 rect.x1 = floorf(xf);
699 rect.y1 = floorf(yf);
700
701 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
702 rect.x2 = floorf(xf);
703 rect.y2 = floorf(yf);
704
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200705 return weston_transformed_rect(surface->width, surface->height,
706 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200707 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200708}
709
710WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400712 struct weston_plane *plane)
713{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500714 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400715 return;
716
Jason Ekstranda7af7042013-10-12 22:38:11 -0500717 weston_view_damage_below(view);
718 view->plane = plane;
719 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400720}
721
722WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500723weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200724{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500725 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200726
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500727 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500728 pixman_region32_subtract(&damage, &view->transform.boundingbox,
729 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800730 if (view->plane)
731 pixman_region32_union(&view->plane->damage,
732 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500733 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800734 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200735}
736
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400737static void
738weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
739{
740 uint32_t different = es->output_mask ^ mask;
741 uint32_t entered = mask & different;
742 uint32_t left = es->output_mask & different;
743 struct weston_output *output;
744 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500745 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400746
747 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500748 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400749 return;
750 if (different == 0)
751 return;
752
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500753 client = wl_resource_get_client(es->resource);
754
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400755 wl_list_for_each(output, &es->compositor->output_list, link) {
756 if (1 << output->id & different)
757 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500758 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400759 client);
760 if (resource == NULL)
761 continue;
762 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500763 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400764 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500765 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400766 }
767}
768
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200769
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400770static void
771weston_surface_assign_output(struct weston_surface *es)
772{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773 struct weston_output *new_output;
774 struct weston_view *view;
775 pixman_region32_t region;
776 uint32_t max, area, mask;
777 pixman_box32_t *e;
778
779 new_output = NULL;
780 max = 0;
781 mask = 0;
782 pixman_region32_init(&region);
783 wl_list_for_each(view, &es->views, surface_link) {
784 if (!view->output)
785 continue;
786
787 pixman_region32_intersect(&region, &view->transform.boundingbox,
788 &view->output->region);
789
790 e = pixman_region32_extents(&region);
791 area = (e->x2 - e->x1) * (e->y2 - e->y1);
792
793 mask |= view->output_mask;
794
795 if (area >= max) {
796 new_output = view->output;
797 max = area;
798 }
799 }
800 pixman_region32_fini(&region);
801
802 es->output = new_output;
803 weston_surface_update_output_mask(es, mask);
804}
805
806static void
807weston_view_assign_output(struct weston_view *ev)
808{
809 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400810 struct weston_output *output, *new_output;
811 pixman_region32_t region;
812 uint32_t max, area, mask;
813 pixman_box32_t *e;
814
815 new_output = NULL;
816 max = 0;
817 mask = 0;
818 pixman_region32_init(&region);
819 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500820 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400821 &output->region);
822
823 e = pixman_region32_extents(&region);
824 area = (e->x2 - e->x1) * (e->y2 - e->y1);
825
826 if (area > 0)
827 mask |= 1 << output->id;
828
829 if (area >= max) {
830 new_output = output;
831 max = area;
832 }
833 }
834 pixman_region32_fini(&region);
835
Jason Ekstranda7af7042013-10-12 22:38:11 -0500836 ev->output = new_output;
837 ev->output_mask = mask;
838
839 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400840}
841
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200842static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500843view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
844 int32_t width, int32_t height,
845 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200846{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200847 float min_x = HUGE_VALF, min_y = HUGE_VALF;
848 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200849 int32_t s[4][2] = {
850 { sx, sy },
851 { sx, sy + height },
852 { sx + width, sy },
853 { sx + width, sy + height }
854 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200855 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200856 int i;
857
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300858 if (width == 0 || height == 0) {
859 /* avoid rounding empty bbox to 1x1 */
860 pixman_region32_init(bbox);
861 return;
862 }
863
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200864 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200865 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500866 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200867 if (x < min_x)
868 min_x = x;
869 if (x > max_x)
870 max_x = x;
871 if (y < min_y)
872 min_y = y;
873 if (y > max_y)
874 max_y = y;
875 }
876
Pekka Paalanen219b9822012-02-08 15:38:37 +0200877 int_x = floorf(min_x);
878 int_y = floorf(min_y);
879 pixman_region32_init_rect(bbox, int_x, int_y,
880 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200881}
882
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200883static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500884weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200885{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200887
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200888 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 view->geometry.x = roundf(view->geometry.x);
890 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200891
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500892 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
894 view->transform.position.matrix.d[12] = view->geometry.x;
895 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500896
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500898
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 view->transform.inverse = view->transform.position.matrix;
900 view->transform.inverse.d[12] = -view->geometry.x;
901 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500902
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903 pixman_region32_init_rect(&view->transform.boundingbox,
904 view->geometry.x,
905 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600906 view->surface->width,
907 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500908
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 if (view->alpha == 1.0) {
910 pixman_region32_copy(&view->transform.opaque,
911 &view->surface->opaque);
912 pixman_region32_translate(&view->transform.opaque,
913 view->geometry.x,
914 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500915 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200916}
917
918static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200920{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 struct weston_view *parent = view->geometry.parent;
922 struct weston_matrix *matrix = &view->transform.matrix;
923 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200924 struct weston_transform *tform;
925
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200927
928 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
930 view->transform.position.matrix.d[12] = view->geometry.x;
931 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200932
933 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500934 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200935 weston_matrix_multiply(matrix, &tform->matrix);
936
Pekka Paalanen483243f2013-03-08 14:56:50 +0200937 if (parent)
938 weston_matrix_multiply(matrix, &parent->transform.matrix);
939
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200940 if (weston_matrix_invert(inverse, matrix) < 0) {
941 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 weston_log("error: weston_view %p"
943 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200944 return -1;
945 }
946
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600947 view_compute_bbox(view, 0, 0,
948 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500950
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200951 return 0;
952}
953
954WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200956{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200958
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200960 return;
961
Pekka Paalanen483243f2013-03-08 14:56:50 +0200962 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200964
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200968
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 pixman_region32_fini(&view->transform.boundingbox);
970 pixman_region32_fini(&view->transform.opaque);
971 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500972
Pekka Paalanencd403622012-01-25 13:37:39 +0200973 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500974 if (view->geometry.transformation_list.next ==
975 &view->transform.position.link &&
976 view->geometry.transformation_list.prev ==
977 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200978 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200980 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500981 if (weston_view_update_transform_enable(view) < 0)
982 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200983 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200984
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300988
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 wl_signal_emit(&view->surface->compositor->transform_signal,
990 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200991}
992
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200993WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200995{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200997
998 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 * The invariant: if view->geometry.dirty, then all views
1000 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001001 * Corollary: if not parent->geometry.dirty, then all ancestors
1002 * are not dirty.
1003 */
1004
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001006 return;
1007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001009
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001011 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001013}
1014
1015WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016weston_view_to_global_fixed(struct weston_view *view,
1017 wl_fixed_t vx, wl_fixed_t vy,
1018 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001019{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001020 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_to_global_float(view,
1023 wl_fixed_to_double(vx),
1024 wl_fixed_to_double(vy),
1025 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001026 *x = wl_fixed_from_double(xf);
1027 *y = wl_fixed_from_double(yf);
1028}
1029
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001030WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031weston_view_from_global_float(struct weston_view *view,
1032 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001033{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001035 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1036
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001038
1039 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001040 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001042 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 *vx = 0;
1044 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001045 return;
1046 }
1047
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 *vx = v.f[0] / v.f[3];
1049 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001050 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 *vx = x - view->geometry.x;
1052 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001053 }
1054}
1055
1056WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057weston_view_from_global_fixed(struct weston_view *view,
1058 wl_fixed_t x, wl_fixed_t y,
1059 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001060{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001061 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001062
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 weston_view_from_global_float(view,
1064 wl_fixed_to_double(x),
1065 wl_fixed_to_double(y),
1066 &vxf, &vyf);
1067 *vx = wl_fixed_from_double(vxf);
1068 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001069}
1070
1071WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072weston_view_from_global(struct weston_view *view,
1073 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001074{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001076
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1078 *vx = floorf(vxf);
1079 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001080}
1081
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001082WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001083weston_surface_schedule_repaint(struct weston_surface *surface)
1084{
1085 struct weston_output *output;
1086
1087 wl_list_for_each(output, &surface->compositor->output_list, link)
1088 if (surface->output_mask & (1 << output->id))
1089 weston_output_schedule_repaint(output);
1090}
1091
1092WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093weston_view_schedule_repaint(struct weston_view *view)
1094{
1095 struct weston_output *output;
1096
1097 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1098 if (view->output_mask & (1 << output->id))
1099 weston_output_schedule_repaint(output);
1100}
1101
1102WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001103weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001104{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001105 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 0, 0, surface->width,
1107 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001108
Kristian Høgsberg98238702012-08-03 16:29:12 -04001109 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001110}
1111
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001112WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001114{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001115 if (view->geometry.x == x && view->geometry.y == y)
1116 return;
1117
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 view->geometry.x = x;
1119 view->geometry.y = y;
1120 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001121}
1122
Pekka Paalanen483243f2013-03-08 14:56:50 +02001123static void
1124transform_parent_handle_parent_destroy(struct wl_listener *listener,
1125 void *data)
1126{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 struct weston_view *view =
1128 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001129 geometry.parent_destroy_listener);
1130
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001132}
1133
1134WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135weston_view_set_transform_parent(struct weston_view *view,
1136 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001137{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138 if (view->geometry.parent) {
1139 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1140 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001141 }
1142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001144
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001146 transform_parent_handle_parent_destroy;
1147 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001148 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001150 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001152 }
1153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 weston_view_geometry_dirty(view);
1155}
1156
1157WL_EXPORT int
1158weston_view_is_mapped(struct weston_view *view)
1159{
1160 if (view->output)
1161 return 1;
1162 else
1163 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001164}
1165
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001166WL_EXPORT int
1167weston_surface_is_mapped(struct weston_surface *surface)
1168{
1169 if (surface->output)
1170 return 1;
1171 else
1172 return 0;
1173}
1174
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001175static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001176surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001177{
1178 struct weston_view *view;
1179
1180 if (surface->width == width && surface->height == height)
1181 return;
1182
1183 surface->width = width;
1184 surface->height = height;
1185
1186 wl_list_for_each(view, &surface->views, surface_link)
1187 weston_view_geometry_dirty(view);
1188}
1189
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001190WL_EXPORT void
1191weston_surface_set_size(struct weston_surface *surface,
1192 int32_t width, int32_t height)
1193{
1194 assert(!surface->resource);
1195 surface_set_size(surface, width, height);
1196}
1197
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001198static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001199weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001200{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001201 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001202 int32_t width, height;
1203
1204 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001205 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001206 return;
1207 }
1208
Pekka Paalanenf0cad482014-03-14 14:38:16 +02001209 if (vp->buffer.src_width != wl_fixed_from_int(-1) &&
1210 vp->surface.width != -1) {
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001211 surface_set_size(surface, vp->surface.width,
1212 vp->surface.height);
Jonny Lamb74130762013-11-26 18:19:46 +01001213 return;
1214 }
1215
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001216 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001217 case WL_OUTPUT_TRANSFORM_90:
1218 case WL_OUTPUT_TRANSFORM_270:
1219 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1220 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001221 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001222 height = surface->buffer_ref.buffer->width;
1223 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001224 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001225 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001226 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001227 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001228 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001229
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001230 width = width / vp->buffer.scale;
1231 height = height / vp->buffer.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001232 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001233}
1234
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001235WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001236weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001237{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001238 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001239
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001240 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001241
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001242 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001243}
1244
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245WL_EXPORT struct weston_view *
1246weston_compositor_pick_view(struct weston_compositor *compositor,
1247 wl_fixed_t x, wl_fixed_t y,
1248 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001249{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001251
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252 wl_list_for_each(view, &compositor->view_list, link) {
1253 weston_view_from_global_fixed(view, x, y, vx, vy);
1254 if (pixman_region32_contains_point(&view->surface->input,
1255 wl_fixed_to_int(*vx),
1256 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001257 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001259 }
1260
1261 return NULL;
1262}
1263
1264static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001265weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001266{
Daniel Stone37816df2012-05-16 18:45:18 +01001267 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001268
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001269 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001270 return;
1271
Daniel Stone37816df2012-05-16 18:45:18 +01001272 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001273 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001274}
1275
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001276WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001278{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001279 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 if (!weston_view_is_mapped(view))
1282 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 weston_view_damage_below(view);
1285 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001286 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 wl_list_remove(&view->layer_link);
1288 wl_list_init(&view->layer_link);
1289 wl_list_remove(&view->link);
1290 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291 view->output_mask = 0;
1292 weston_surface_assign_output(view->surface);
1293
1294 if (weston_surface_is_mapped(view->surface))
1295 return;
1296
1297 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1298 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001299 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001301 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001302 NULL,
1303 wl_fixed_from_int(0),
1304 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001306 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001307 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001309
Jason Ekstranda7af7042013-10-12 22:38:11 -05001310WL_EXPORT void
1311weston_surface_unmap(struct weston_surface *surface)
1312{
1313 struct weston_view *view;
1314
1315 wl_list_for_each(view, &surface->views, surface_link)
1316 weston_view_unmap(view);
1317 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001318}
1319
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001320static void
1321weston_surface_reset_pending_buffer(struct weston_surface *surface)
1322{
1323 if (surface->pending.buffer)
1324 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1325 surface->pending.buffer = NULL;
1326 surface->pending.sx = 0;
1327 surface->pending.sy = 0;
1328 surface->pending.newly_attached = 0;
1329}
1330
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001331struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001332 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001333 struct wl_list link;
1334};
1335
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336WL_EXPORT void
1337weston_view_destroy(struct weston_view *view)
1338{
1339 wl_signal_emit(&view->destroy_signal, view);
1340
1341 assert(wl_list_empty(&view->geometry.child_list));
1342
1343 if (weston_view_is_mapped(view)) {
1344 weston_view_unmap(view);
1345 weston_compositor_build_view_list(view->surface->compositor);
1346 }
1347
1348 wl_list_remove(&view->link);
1349 wl_list_remove(&view->layer_link);
1350
1351 pixman_region32_fini(&view->clip);
1352 pixman_region32_fini(&view->transform.boundingbox);
1353
1354 weston_view_set_transform_parent(view, NULL);
1355
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 wl_list_remove(&view->surface_link);
1357
1358 free(view);
1359}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001360
1361WL_EXPORT void
1362weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001363{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001364 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001366
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001367 if (--surface->ref_count > 0)
1368 return;
1369
1370 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1371
Pekka Paalanene67858b2013-04-25 13:57:42 +03001372 assert(wl_list_empty(&surface->subsurface_list_pending));
1373 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1376 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001377
Pekka Paalanenbc106382012-10-10 12:49:31 +03001378 wl_list_for_each_safe(cb, next,
1379 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001380 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001381
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001382 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001383 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001384 pixman_region32_fini(&surface->pending.damage);
1385
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001386 if (surface->pending.buffer)
1387 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1388
Pekka Paalanende685b82012-12-04 15:58:12 +02001389 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001390
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001391 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001392 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001393 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001394
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001395 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001396 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001397
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001398 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001399}
1400
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001401static void
1402destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001403{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001404 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001405
Giulio Camuffo0d379742013-11-15 22:06:15 +01001406 /* Set the resource to NULL, since we don't want to leave a
1407 * dangling pointer if the surface was refcounted and survives
1408 * the weston_surface_destroy() call. */
1409 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001410 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001411}
1412
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001413static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001414weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1415{
1416 struct weston_buffer *buffer =
1417 container_of(listener, struct weston_buffer, destroy_listener);
1418
1419 wl_signal_emit(&buffer->destroy_signal, buffer);
1420 free(buffer);
1421}
1422
1423struct weston_buffer *
1424weston_buffer_from_resource(struct wl_resource *resource)
1425{
1426 struct weston_buffer *buffer;
1427 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001428
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001429 listener = wl_resource_get_destroy_listener(resource,
1430 weston_buffer_destroy_handler);
1431
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001432 if (listener)
1433 return container_of(listener, struct weston_buffer,
1434 destroy_listener);
1435
1436 buffer = zalloc(sizeof *buffer);
1437 if (buffer == NULL)
1438 return NULL;
1439
1440 buffer->resource = resource;
1441 wl_signal_init(&buffer->destroy_signal);
1442 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001443 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001444 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001445
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001446 return buffer;
1447}
1448
1449static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001450weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1451 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001452{
Pekka Paalanende685b82012-12-04 15:58:12 +02001453 struct weston_buffer_reference *ref =
1454 container_of(listener, struct weston_buffer_reference,
1455 destroy_listener);
1456
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001457 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001458 ref->buffer = NULL;
1459}
1460
1461WL_EXPORT void
1462weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001463 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001464{
1465 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001466 ref->buffer->busy_count--;
1467 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001468 assert(wl_resource_get_client(ref->buffer->resource));
1469 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001470 WL_BUFFER_RELEASE);
1471 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001472 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001473 }
1474
Pekka Paalanende685b82012-12-04 15:58:12 +02001475 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001476 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001477 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001478 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001479 }
1480
Pekka Paalanende685b82012-12-04 15:58:12 +02001481 ref->buffer = buffer;
1482 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1483}
1484
1485static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001486weston_surface_attach(struct weston_surface *surface,
1487 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001488{
1489 weston_buffer_reference(&surface->buffer_ref, buffer);
1490
Pekka Paalanena6421c42012-12-04 15:58:10 +02001491 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001492 if (weston_surface_is_mapped(surface))
1493 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001494 }
1495
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001496 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001497
1498 weston_surface_set_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001499}
1500
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001501WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001502weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001503{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001505
1506 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001508}
1509
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001510WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001511weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001512{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001513 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001514
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001515 pixman_region32_union(&compositor->primary_plane.damage,
1516 &compositor->primary_plane.damage,
1517 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001518 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001519}
1520
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001521static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001523{
Pekka Paalanende685b82012-12-04 15:58:12 +02001524 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001525 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001526 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001527
Jason Ekstranda7af7042013-10-12 22:38:11 -05001528 empty_region(&surface->damage);
1529}
1530
1531static void
1532view_accumulate_damage(struct weston_view *view,
1533 pixman_region32_t *opaque)
1534{
1535 pixman_region32_t damage;
1536
1537 pixman_region32_init(&damage);
1538 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001539 pixman_box32_t *extents;
1540
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 extents = pixman_region32_extents(&view->surface->damage);
1542 view_compute_bbox(view, extents->x1, extents->y1,
1543 extents->x2 - extents->x1,
1544 extents->y2 - extents->y1,
1545 &damage);
1546 pixman_region32_translate(&damage,
1547 -view->plane->x,
1548 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001549 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 pixman_region32_copy(&damage, &view->surface->damage);
1551 pixman_region32_translate(&damage,
1552 view->geometry.x - view->plane->x,
1553 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001554 }
1555
Jason Ekstranda7af7042013-10-12 22:38:11 -05001556 pixman_region32_subtract(&damage, &damage, opaque);
1557 pixman_region32_union(&view->plane->damage,
1558 &view->plane->damage, &damage);
1559 pixman_region32_fini(&damage);
1560 pixman_region32_copy(&view->clip, opaque);
1561 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001562}
1563
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001564static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001565compositor_accumulate_damage(struct weston_compositor *ec)
1566{
1567 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001569 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001570
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001571 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001572
1573 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001574 pixman_region32_copy(&plane->clip, &clip);
1575
1576 pixman_region32_init(&opaque);
1577
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 wl_list_for_each(ev, &ec->view_list, link) {
1579 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001580 continue;
1581
Jason Ekstranda7af7042013-10-12 22:38:11 -05001582 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001583 }
1584
1585 pixman_region32_union(&clip, &clip, &opaque);
1586 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001587 }
1588
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001589 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001590
Jason Ekstranda7af7042013-10-12 22:38:11 -05001591 wl_list_for_each(ev, &ec->view_list, link)
1592 ev->surface->touched = 0;
1593
1594 wl_list_for_each(ev, &ec->view_list, link) {
1595 if (ev->surface->touched)
1596 continue;
1597 ev->surface->touched = 1;
1598
1599 surface_flush_damage(ev->surface);
1600
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001601 /* Both the renderer and the backend have seen the buffer
1602 * by now. If renderer needs the buffer, it has its own
1603 * reference set. If the backend wants to keep the buffer
1604 * around for migrating the surface into a non-primary plane
1605 * later, keep_buffer is true. Otherwise, drop the core
1606 * reference now, and allow early buffer release. This enables
1607 * clients to use single-buffering.
1608 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001609 if (!ev->surface->keep_buffer)
1610 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001611 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001612}
1613
1614static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001616{
1617 struct weston_subsurface *sub;
1618
Pekka Paalanene67858b2013-04-25 13:57:42 +03001619 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001621 continue;
1622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1624 wl_list_init(&sub->surface->views);
1625
1626 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001627 }
1628}
1629
1630static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001632{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633 struct weston_subsurface *sub;
1634 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001635
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1637 if (sub->surface == surface)
1638 continue;
1639
1640 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1641 weston_view_destroy(view);
1642
1643 surface_free_unused_subsurface_views(sub->surface);
1644 }
1645}
1646
1647static void
1648view_list_add_subsurface_view(struct weston_compositor *compositor,
1649 struct weston_subsurface *sub,
1650 struct weston_view *parent)
1651{
1652 struct weston_subsurface *child;
1653 struct weston_view *view = NULL, *iv;
1654
1655 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1656 if (iv->geometry.parent == parent) {
1657 view = iv;
1658 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001659 }
1660 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001661
1662 if (view) {
1663 /* Put it back in the surface's list of views */
1664 wl_list_remove(&view->surface_link);
1665 wl_list_insert(&sub->surface->views, &view->surface_link);
1666 } else {
1667 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001668 weston_view_set_position(view,
1669 sub->position.x,
1670 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 weston_view_set_transform_parent(view, parent);
1672 }
1673
1674 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675
Pekka Paalanenb188e912013-11-19 14:03:35 +02001676 if (wl_list_empty(&sub->surface->subsurface_list)) {
1677 wl_list_insert(compositor->view_list.prev, &view->link);
1678 return;
1679 }
1680
1681 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1682 if (child->surface == sub->surface)
1683 wl_list_insert(compositor->view_list.prev, &view->link);
1684 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001685 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001686 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687}
1688
1689static void
1690view_list_add(struct weston_compositor *compositor,
1691 struct weston_view *view)
1692{
1693 struct weston_subsurface *sub;
1694
1695 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001696
Pekka Paalanenb188e912013-11-19 14:03:35 +02001697 if (wl_list_empty(&view->surface->subsurface_list)) {
1698 wl_list_insert(compositor->view_list.prev, &view->link);
1699 return;
1700 }
1701
1702 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1703 if (sub->surface == view->surface)
1704 wl_list_insert(compositor->view_list.prev, &view->link);
1705 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001707 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708}
1709
1710static void
1711weston_compositor_build_view_list(struct weston_compositor *compositor)
1712{
1713 struct weston_view *view;
1714 struct weston_layer *layer;
1715
1716 wl_list_for_each(layer, &compositor->layer_list, link)
1717 wl_list_for_each(view, &layer->view_list, layer_link)
1718 surface_stash_subsurface_views(view->surface);
1719
1720 wl_list_init(&compositor->view_list);
1721 wl_list_for_each(layer, &compositor->layer_list, link) {
1722 wl_list_for_each(view, &layer->view_list, layer_link) {
1723 view_list_add(compositor, view);
1724 }
1725 }
1726
1727 wl_list_for_each(layer, &compositor->layer_list, link)
1728 wl_list_for_each(view, &layer->view_list, layer_link)
1729 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001730}
1731
David Herrmann1edf44c2013-10-22 17:11:26 +02001732static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001733weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001734{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001735 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001737 struct weston_animation *animation, *next;
1738 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001739 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001740 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001741 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001742
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001743 if (output->destroying)
1744 return 0;
1745
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001746 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001747 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001748
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001749 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001750 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001751 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001752 wl_list_for_each(ev, &ec->view_list, link)
1753 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001754
Pekka Paalanene67858b2013-04-25 13:57:42 +03001755 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756 wl_list_for_each(ev, &ec->view_list, link) {
1757 /* Note: This operation is safe to do multiple times on the
1758 * same surface.
1759 */
1760 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001761 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001762 &ev->surface->frame_callback_list);
1763 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001764 }
1765 }
1766
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001767 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001768
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001769 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001770 pixman_region32_intersect(&output_damage,
1771 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001772 pixman_region32_subtract(&output_damage,
1773 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001774
Scott Moreauccbf29d2012-02-22 14:21:41 -07001775 if (output->dirty)
1776 weston_output_update_matrix(output);
1777
David Herrmann1edf44c2013-10-22 17:11:26 +02001778 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001779
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001780 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001781
Kristian Høgsbergef044142011-06-21 15:02:12 -04001782 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001783
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001784 weston_compositor_repick(ec);
1785 wl_event_loop_dispatch(ec->input_loop, 0);
1786
Jonas Ådahldb773762012-06-13 00:01:21 +02001787 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001788 wl_callback_send_done(cb->resource, msecs);
1789 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001790 }
1791
Scott Moreaud64cf212012-06-08 19:40:54 -06001792 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001793 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001794 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001795 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001796
1797 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001798}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001799
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001800static int
1801weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001802{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001803 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001804
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001805 wl_event_loop_dispatch(compositor->input_loop, 0);
1806
1807 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001808}
1809
1810WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001811weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001812{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001813 struct weston_compositor *compositor = output->compositor;
1814 struct wl_event_loop *loop =
1815 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001816 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001817
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001818 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001819
1820 if (output->repaint_needed &&
1821 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1822 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001823 r = weston_output_repaint(output, msecs);
1824 if (!r)
1825 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001826 }
1827
1828 output->repaint_scheduled = 0;
1829 if (compositor->input_loop_source)
1830 return;
1831
1832 fd = wl_event_loop_get_fd(compositor->input_loop);
1833 compositor->input_loop_source =
1834 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1835 weston_compositor_read_input, compositor);
1836}
1837
1838static void
1839idle_repaint(void *data)
1840{
1841 struct weston_output *output = data;
1842
Jonas Ådahle5a12252013-04-05 23:07:11 +02001843 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001844}
1845
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001846WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001847weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1848{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001849 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001850 if (below != NULL)
1851 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001852}
1853
1854WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001855weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001856{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001857 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001858 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001859
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001860 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1861 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001862 return;
1863
Kristian Høgsbergef044142011-06-21 15:02:12 -04001864 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001865 output->repaint_needed = 1;
1866 if (output->repaint_scheduled)
1867 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001868
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001869 wl_event_loop_add_idle(loop, idle_repaint, output);
1870 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001871
1872 if (compositor->input_loop_source) {
1873 wl_event_source_remove(compositor->input_loop_source);
1874 compositor->input_loop_source = NULL;
1875 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001876}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001877
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001878WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001879weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1880{
1881 struct weston_output *output;
1882
1883 wl_list_for_each(output, &compositor->output_list, link)
1884 weston_output_schedule_repaint(output);
1885}
1886
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001887static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001888surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001889{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001890 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001891}
1892
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001893static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001894surface_attach(struct wl_client *client,
1895 struct wl_resource *resource,
1896 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1897{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001898 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001900
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001901 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001902 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001903 if (buffer == NULL) {
1904 wl_client_post_no_memory(client);
1905 return;
1906 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001907 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001908
Pekka Paalanende685b82012-12-04 15:58:12 +02001909 /* Attach, attach, without commit in between does not send
1910 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001911 if (surface->pending.buffer)
1912 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001913
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001914 surface->pending.sx = sx;
1915 surface->pending.sy = sy;
1916 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001917 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001918 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001919 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001920 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001921 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001922}
1923
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001924static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001925surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001926 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001927 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001928{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001929 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001930
Pekka Paalanen8e159182012-10-10 12:49:25 +03001931 pixman_region32_union_rect(&surface->pending.damage,
1932 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001933 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001934}
1935
Kristian Høgsberg33418202011-08-16 23:01:28 -04001936static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001937destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001938{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001939 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001940
1941 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001942 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001943}
1944
1945static void
1946surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001947 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001948{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001949 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001950 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001951
1952 cb = malloc(sizeof *cb);
1953 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001954 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001955 return;
1956 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001957
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001958 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1959 callback);
1960 if (cb->resource == NULL) {
1961 free(cb);
1962 wl_resource_post_no_memory(resource);
1963 return;
1964 }
1965
Jason Ekstranda85118c2013-06-27 20:17:02 -05001966 wl_resource_set_implementation(cb->resource, NULL, cb,
1967 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001968
Pekka Paalanenbc106382012-10-10 12:49:31 +03001969 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001970}
1971
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001972static void
1973surface_set_opaque_region(struct wl_client *client,
1974 struct wl_resource *resource,
1975 struct wl_resource *region_resource)
1976{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001977 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001978 struct weston_region *region;
1979
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001980 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001981 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001982 pixman_region32_copy(&surface->pending.opaque,
1983 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001984 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001985 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001986 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001987}
1988
1989static void
1990surface_set_input_region(struct wl_client *client,
1991 struct wl_resource *resource,
1992 struct wl_resource *region_resource)
1993{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001994 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001995 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001996
1997 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001998 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001999 pixman_region32_copy(&surface->pending.input,
2000 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002001 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002002 pixman_region32_fini(&surface->pending.input);
2003 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002004 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002005}
2006
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002007static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002008weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002009{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002010 struct weston_subsurface *sub;
2011
2012 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2013 parent_link_pending) {
2014 wl_list_remove(&sub->parent_link);
2015 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2016 }
2017}
2018
2019static void
2020weston_surface_commit(struct weston_surface *surface)
2021{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002022 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002023 pixman_region32_t opaque;
2024
Pekka Paalanen260ba382014-03-14 14:38:12 +02002025 /* XXX: wl_viewport.set without an attach should call configure */
2026
Alexander Larsson4ea95522013-05-22 14:41:37 +02002027 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002028 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002029 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002030 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002031
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002032 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002033 if (surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002034 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002035
2036 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002037 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002038 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002039
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002040 weston_surface_reset_pending_buffer(surface);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002041
2042 /* wl_surface.damage */
2043 pixman_region32_union(&surface->damage, &surface->damage,
2044 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002045 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2046 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 surface->width,
2048 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002049 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002050
2051 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002052 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 surface->width,
2054 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002055 pixman_region32_intersect(&opaque,
2056 &opaque, &surface->pending.opaque);
2057
2058 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2059 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 wl_list_for_each(view, &surface->views, surface_link)
2061 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002062 }
2063
2064 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002065
2066 /* wl_surface.set_input_region */
2067 pixman_region32_fini(&surface->input);
2068 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002069 surface->width,
2070 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002071 pixman_region32_intersect(&surface->input,
2072 &surface->input, &surface->pending.input);
2073
Pekka Paalanenbc106382012-10-10 12:49:31 +03002074 /* wl_surface.frame */
2075 wl_list_insert_list(&surface->frame_callback_list,
2076 &surface->pending.frame_callback_list);
2077 wl_list_init(&surface->pending.frame_callback_list);
2078
Pekka Paalanene67858b2013-04-25 13:57:42 +03002079 weston_surface_commit_subsurface_order(surface);
2080
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002081 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002082}
2083
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002084static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002085weston_subsurface_commit(struct weston_subsurface *sub);
2086
2087static void
2088weston_subsurface_parent_commit(struct weston_subsurface *sub,
2089 int parent_is_synchronized);
2090
2091static void
2092surface_commit(struct wl_client *client, struct wl_resource *resource)
2093{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002094 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002095 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2096
2097 if (sub) {
2098 weston_subsurface_commit(sub);
2099 return;
2100 }
2101
2102 weston_surface_commit(surface);
2103
2104 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2105 if (sub->surface != surface)
2106 weston_subsurface_parent_commit(sub, 0);
2107 }
2108}
2109
2110static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002111surface_set_buffer_transform(struct wl_client *client,
2112 struct wl_resource *resource, int transform)
2113{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002114 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002115
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002116 surface->pending.buffer_viewport.buffer.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002117}
2118
Alexander Larsson4ea95522013-05-22 14:41:37 +02002119static void
2120surface_set_buffer_scale(struct wl_client *client,
2121 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002122 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002123{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002124 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002125
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002126 surface->pending.buffer_viewport.buffer.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002127}
2128
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002129static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002130 surface_destroy,
2131 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002132 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002133 surface_frame,
2134 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002135 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002136 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002137 surface_set_buffer_transform,
2138 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002139};
2140
2141static void
2142compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002143 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002144{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002145 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002146 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002147
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002148 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002149 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002150 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002151 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002152 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002153
Jason Ekstranda85118c2013-06-27 20:17:02 -05002154 surface->resource =
2155 wl_resource_create(client, &wl_surface_interface,
2156 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002157 if (surface->resource == NULL) {
2158 weston_surface_destroy(surface);
2159 wl_resource_post_no_memory(resource);
2160 return;
2161 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002162 wl_resource_set_implementation(surface->resource, &surface_interface,
2163 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002164}
2165
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002166static void
2167destroy_region(struct wl_resource *resource)
2168{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002169 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002170
2171 pixman_region32_fini(&region->region);
2172 free(region);
2173}
2174
2175static void
2176region_destroy(struct wl_client *client, struct wl_resource *resource)
2177{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002178 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002179}
2180
2181static void
2182region_add(struct wl_client *client, struct wl_resource *resource,
2183 int32_t x, int32_t y, int32_t width, int32_t height)
2184{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002185 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002186
2187 pixman_region32_union_rect(&region->region, &region->region,
2188 x, y, width, height);
2189}
2190
2191static void
2192region_subtract(struct wl_client *client, struct wl_resource *resource,
2193 int32_t x, int32_t y, int32_t width, int32_t height)
2194{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002195 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002196 pixman_region32_t rect;
2197
2198 pixman_region32_init_rect(&rect, x, y, width, height);
2199 pixman_region32_subtract(&region->region, &region->region, &rect);
2200 pixman_region32_fini(&rect);
2201}
2202
2203static const struct wl_region_interface region_interface = {
2204 region_destroy,
2205 region_add,
2206 region_subtract
2207};
2208
2209static void
2210compositor_create_region(struct wl_client *client,
2211 struct wl_resource *resource, uint32_t id)
2212{
2213 struct weston_region *region;
2214
2215 region = malloc(sizeof *region);
2216 if (region == NULL) {
2217 wl_resource_post_no_memory(resource);
2218 return;
2219 }
2220
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002221 pixman_region32_init(&region->region);
2222
Jason Ekstranda85118c2013-06-27 20:17:02 -05002223 region->resource =
2224 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002225 if (region->resource == NULL) {
2226 free(region);
2227 wl_resource_post_no_memory(resource);
2228 return;
2229 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002230 wl_resource_set_implementation(region->resource, &region_interface,
2231 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002232}
2233
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002234static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002235 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002236 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002237};
2238
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002239static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002240weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2241{
2242 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002243 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002244 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245
Alexander Larsson4ea95522013-05-22 14:41:37 +02002246 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002247 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002248 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002249 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002250
Pekka Paalanene67858b2013-04-25 13:57:42 +03002251 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002252 if (sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2254 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2255
Pekka Paalanene67858b2013-04-25 13:57:42 +03002256 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002257 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258 sub->cached.sx = 0;
2259 sub->cached.sy = 0;
2260 sub->cached.newly_attached = 0;
2261
2262 /* wl_surface.damage */
2263 pixman_region32_union(&surface->damage, &surface->damage,
2264 &sub->cached.damage);
2265 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2266 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 surface->width,
2268 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002269 empty_region(&sub->cached.damage);
2270
2271 /* wl_surface.set_opaque_region */
2272 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002273 surface->width,
2274 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002275 pixman_region32_intersect(&opaque,
2276 &opaque, &sub->cached.opaque);
2277
2278 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2279 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002280 wl_list_for_each(view, &surface->views, surface_link)
2281 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002282 }
2283
2284 pixman_region32_fini(&opaque);
2285
2286 /* wl_surface.set_input_region */
2287 pixman_region32_fini(&surface->input);
2288 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002289 surface->width,
2290 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002291 pixman_region32_intersect(&surface->input,
2292 &surface->input, &sub->cached.input);
2293
2294 /* wl_surface.frame */
2295 wl_list_insert_list(&surface->frame_callback_list,
2296 &sub->cached.frame_callback_list);
2297 wl_list_init(&sub->cached.frame_callback_list);
2298
2299 weston_surface_commit_subsurface_order(surface);
2300
2301 weston_surface_schedule_repaint(surface);
2302
2303 sub->cached.has_data = 0;
2304}
2305
2306static void
2307weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2308{
2309 struct weston_surface *surface = sub->surface;
2310
2311 /*
2312 * If this commit would cause the surface to move by the
2313 * attach(dx, dy) parameters, the old damage region must be
2314 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002315 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 */
2317 pixman_region32_translate(&sub->cached.damage,
2318 -surface->pending.sx, -surface->pending.sy);
2319 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2320 &surface->pending.damage);
2321 empty_region(&surface->pending.damage);
2322
2323 if (surface->pending.newly_attached) {
2324 sub->cached.newly_attached = 1;
2325 weston_buffer_reference(&sub->cached.buffer_ref,
2326 surface->pending.buffer);
2327 }
2328 sub->cached.sx += surface->pending.sx;
2329 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002330
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002331 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002332
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002333 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002334
2335 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2336
2337 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2338
2339 wl_list_insert_list(&sub->cached.frame_callback_list,
2340 &surface->pending.frame_callback_list);
2341 wl_list_init(&surface->pending.frame_callback_list);
2342
2343 sub->cached.has_data = 1;
2344}
2345
2346static int
2347weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2348{
2349 while (sub) {
2350 if (sub->synchronized)
2351 return 1;
2352
2353 if (!sub->parent)
2354 return 0;
2355
2356 sub = weston_surface_to_subsurface(sub->parent);
2357 }
2358
2359 return 0;
2360}
2361
2362static void
2363weston_subsurface_commit(struct weston_subsurface *sub)
2364{
2365 struct weston_surface *surface = sub->surface;
2366 struct weston_subsurface *tmp;
2367
2368 /* Recursive check for effectively synchronized. */
2369 if (weston_subsurface_is_synchronized(sub)) {
2370 weston_subsurface_commit_to_cache(sub);
2371 } else {
2372 if (sub->cached.has_data) {
2373 /* flush accumulated state from cache */
2374 weston_subsurface_commit_to_cache(sub);
2375 weston_subsurface_commit_from_cache(sub);
2376 } else {
2377 weston_surface_commit(surface);
2378 }
2379
2380 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2381 if (tmp->surface != surface)
2382 weston_subsurface_parent_commit(tmp, 0);
2383 }
2384 }
2385}
2386
2387static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002388weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002389{
2390 struct weston_surface *surface = sub->surface;
2391 struct weston_subsurface *tmp;
2392
Pekka Paalanene67858b2013-04-25 13:57:42 +03002393 /* From now on, commit_from_cache the whole sub-tree, regardless of
2394 * the synchronized mode of each child. This sub-surface or some
2395 * of its ancestors were synchronized, so we are synchronized
2396 * all the way down.
2397 */
2398
2399 if (sub->cached.has_data)
2400 weston_subsurface_commit_from_cache(sub);
2401
2402 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2403 if (tmp->surface != surface)
2404 weston_subsurface_parent_commit(tmp, 1);
2405 }
2406}
2407
2408static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002409weston_subsurface_parent_commit(struct weston_subsurface *sub,
2410 int parent_is_synchronized)
2411{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002412 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002413 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002414 wl_list_for_each(view, &sub->surface->views, surface_link)
2415 weston_view_set_position(view,
2416 sub->position.x,
2417 sub->position.y);
2418
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002419 sub->position.set = 0;
2420 }
2421
2422 if (parent_is_synchronized || sub->synchronized)
2423 weston_subsurface_synchronized_commit(sub);
2424}
2425
2426static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002427subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428{
2429 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002430 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002431
Jason Ekstranda7af7042013-10-12 22:38:11 -05002432 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002433 weston_view_set_position(view,
2434 view->geometry.x + dx,
2435 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002436
2437 /* No need to check parent mappedness, because if parent is not
2438 * mapped, parent is not in a visible layer, so this sub-surface
2439 * will not be drawn either.
2440 */
2441 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002442 /* Cannot call weston_surface_update_transform(),
2443 * because that would call it also for the parent surface,
2444 * which might not be mapped yet. That would lead to
2445 * inconsistent state, where the window could never be
2446 * mapped.
2447 *
2448 * Instead just assing any output, to make
2449 * weston_surface_is_mapped() return true, so that when the
2450 * parent surface does get mapped, this one will get
2451 * included, too. See surface_list_add().
2452 */
2453 assert(!wl_list_empty(&compositor->output_list));
2454 surface->output = container_of(compositor->output_list.next,
2455 struct weston_output, link);
2456 }
2457}
2458
2459static struct weston_subsurface *
2460weston_surface_to_subsurface(struct weston_surface *surface)
2461{
2462 if (surface->configure == subsurface_configure)
2463 return surface->configure_private;
2464
2465 return NULL;
2466}
2467
Pekka Paalanen01388e22013-04-25 13:57:44 +03002468WL_EXPORT struct weston_surface *
2469weston_surface_get_main_surface(struct weston_surface *surface)
2470{
2471 struct weston_subsurface *sub;
2472
2473 while (surface && (sub = weston_surface_to_subsurface(surface)))
2474 surface = sub->parent;
2475
2476 return surface;
2477}
2478
Pekka Paalanene67858b2013-04-25 13:57:42 +03002479static void
2480subsurface_set_position(struct wl_client *client,
2481 struct wl_resource *resource, int32_t x, int32_t y)
2482{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002483 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002484
2485 if (!sub)
2486 return;
2487
2488 sub->position.x = x;
2489 sub->position.y = y;
2490 sub->position.set = 1;
2491}
2492
2493static struct weston_subsurface *
2494subsurface_from_surface(struct weston_surface *surface)
2495{
2496 struct weston_subsurface *sub;
2497
2498 sub = weston_surface_to_subsurface(surface);
2499 if (sub)
2500 return sub;
2501
2502 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2503 if (sub->surface == surface)
2504 return sub;
2505
2506 return NULL;
2507}
2508
2509static struct weston_subsurface *
2510subsurface_sibling_check(struct weston_subsurface *sub,
2511 struct weston_surface *surface,
2512 const char *request)
2513{
2514 struct weston_subsurface *sibling;
2515
2516 sibling = subsurface_from_surface(surface);
2517
2518 if (!sibling) {
2519 wl_resource_post_error(sub->resource,
2520 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2521 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002522 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002523 return NULL;
2524 }
2525
2526 if (sibling->parent != sub->parent) {
2527 wl_resource_post_error(sub->resource,
2528 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2529 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002530 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002531 return NULL;
2532 }
2533
2534 return sibling;
2535}
2536
2537static void
2538subsurface_place_above(struct wl_client *client,
2539 struct wl_resource *resource,
2540 struct wl_resource *sibling_resource)
2541{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002542 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002543 struct weston_surface *surface =
2544 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002545 struct weston_subsurface *sibling;
2546
2547 if (!sub)
2548 return;
2549
2550 sibling = subsurface_sibling_check(sub, surface, "place_above");
2551 if (!sibling)
2552 return;
2553
2554 wl_list_remove(&sub->parent_link_pending);
2555 wl_list_insert(sibling->parent_link_pending.prev,
2556 &sub->parent_link_pending);
2557}
2558
2559static void
2560subsurface_place_below(struct wl_client *client,
2561 struct wl_resource *resource,
2562 struct wl_resource *sibling_resource)
2563{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002564 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002565 struct weston_surface *surface =
2566 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567 struct weston_subsurface *sibling;
2568
2569 if (!sub)
2570 return;
2571
2572 sibling = subsurface_sibling_check(sub, surface, "place_below");
2573 if (!sibling)
2574 return;
2575
2576 wl_list_remove(&sub->parent_link_pending);
2577 wl_list_insert(&sibling->parent_link_pending,
2578 &sub->parent_link_pending);
2579}
2580
2581static void
2582subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2583{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002584 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002585
2586 if (sub)
2587 sub->synchronized = 1;
2588}
2589
2590static void
2591subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2592{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002593 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002594
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002595 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002596 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002597
2598 /* If sub became effectively desynchronized, flush. */
2599 if (!weston_subsurface_is_synchronized(sub))
2600 weston_subsurface_synchronized_commit(sub);
2601 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002602}
2603
2604static void
2605weston_subsurface_cache_init(struct weston_subsurface *sub)
2606{
2607 pixman_region32_init(&sub->cached.damage);
2608 pixman_region32_init(&sub->cached.opaque);
2609 pixman_region32_init(&sub->cached.input);
2610 wl_list_init(&sub->cached.frame_callback_list);
2611 sub->cached.buffer_ref.buffer = NULL;
2612}
2613
2614static void
2615weston_subsurface_cache_fini(struct weston_subsurface *sub)
2616{
2617 struct weston_frame_callback *cb, *tmp;
2618
2619 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002620 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002621
2622 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2623 pixman_region32_fini(&sub->cached.damage);
2624 pixman_region32_fini(&sub->cached.opaque);
2625 pixman_region32_fini(&sub->cached.input);
2626}
2627
2628static void
2629weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2630{
2631 wl_list_remove(&sub->parent_link);
2632 wl_list_remove(&sub->parent_link_pending);
2633 wl_list_remove(&sub->parent_destroy_listener.link);
2634 sub->parent = NULL;
2635}
2636
2637static void
2638weston_subsurface_destroy(struct weston_subsurface *sub);
2639
2640static void
2641subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2642{
2643 struct weston_subsurface *sub =
2644 container_of(listener, struct weston_subsurface,
2645 surface_destroy_listener);
2646 assert(data == &sub->surface->resource);
2647
2648 /* The protocol object (wl_resource) is left inert. */
2649 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002650 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002651
2652 weston_subsurface_destroy(sub);
2653}
2654
2655static void
2656subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2657{
2658 struct weston_subsurface *sub =
2659 container_of(listener, struct weston_subsurface,
2660 parent_destroy_listener);
2661 assert(data == &sub->parent->resource);
2662 assert(sub->surface != sub->parent);
2663
2664 if (weston_surface_is_mapped(sub->surface))
2665 weston_surface_unmap(sub->surface);
2666
2667 weston_subsurface_unlink_parent(sub);
2668}
2669
2670static void
2671subsurface_resource_destroy(struct wl_resource *resource)
2672{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002673 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674
2675 if (sub)
2676 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002677}
2678
2679static void
2680subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2681{
2682 wl_resource_destroy(resource);
2683}
2684
2685static void
2686weston_subsurface_link_parent(struct weston_subsurface *sub,
2687 struct weston_surface *parent)
2688{
2689 sub->parent = parent;
2690 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002691 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692 &sub->parent_destroy_listener);
2693
2694 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2695 wl_list_insert(&parent->subsurface_list_pending,
2696 &sub->parent_link_pending);
2697}
2698
2699static void
2700weston_subsurface_link_surface(struct weston_subsurface *sub,
2701 struct weston_surface *surface)
2702{
2703 sub->surface = surface;
2704 sub->surface_destroy_listener.notify =
2705 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002706 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002707 &sub->surface_destroy_listener);
2708}
2709
2710static void
2711weston_subsurface_destroy(struct weston_subsurface *sub)
2712{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 struct weston_view *view, *next;
2714
Pekka Paalanene67858b2013-04-25 13:57:42 +03002715 assert(sub->surface);
2716
2717 if (sub->resource) {
2718 assert(weston_surface_to_subsurface(sub->surface) == sub);
2719 assert(sub->parent_destroy_listener.notify ==
2720 subsurface_handle_parent_destroy);
2721
Jason Ekstranda7af7042013-10-12 22:38:11 -05002722 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2723 weston_view_destroy(view);
2724
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725 if (sub->parent)
2726 weston_subsurface_unlink_parent(sub);
2727
2728 weston_subsurface_cache_fini(sub);
2729
2730 sub->surface->configure = NULL;
2731 sub->surface->configure_private = NULL;
2732 } else {
2733 /* the dummy weston_subsurface for the parent itself */
2734 assert(sub->parent_destroy_listener.notify == NULL);
2735 wl_list_remove(&sub->parent_link);
2736 wl_list_remove(&sub->parent_link_pending);
2737 }
2738
2739 wl_list_remove(&sub->surface_destroy_listener.link);
2740 free(sub);
2741}
2742
2743static const struct wl_subsurface_interface subsurface_implementation = {
2744 subsurface_destroy,
2745 subsurface_set_position,
2746 subsurface_place_above,
2747 subsurface_place_below,
2748 subsurface_set_sync,
2749 subsurface_set_desync
2750};
2751
2752static struct weston_subsurface *
2753weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2754 struct weston_surface *parent)
2755{
2756 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002757 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758
2759 sub = calloc(1, sizeof *sub);
2760 if (!sub)
2761 return NULL;
2762
Jason Ekstranda7af7042013-10-12 22:38:11 -05002763 wl_list_init(&sub->unused_views);
2764
Jason Ekstranda85118c2013-06-27 20:17:02 -05002765 sub->resource =
2766 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767 if (!sub->resource) {
2768 free(sub);
2769 return NULL;
2770 }
2771
Jason Ekstranda85118c2013-06-27 20:17:02 -05002772 wl_resource_set_implementation(sub->resource,
2773 &subsurface_implementation,
2774 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002775 weston_subsurface_link_surface(sub, surface);
2776 weston_subsurface_link_parent(sub, parent);
2777 weston_subsurface_cache_init(sub);
2778 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779
2780 return sub;
2781}
2782
2783/* Create a dummy subsurface for having the parent itself in its
2784 * sub-surface lists. Makes stacking order manipulation easy.
2785 */
2786static struct weston_subsurface *
2787weston_subsurface_create_for_parent(struct weston_surface *parent)
2788{
2789 struct weston_subsurface *sub;
2790
2791 sub = calloc(1, sizeof *sub);
2792 if (!sub)
2793 return NULL;
2794
2795 weston_subsurface_link_surface(sub, parent);
2796 sub->parent = parent;
2797 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2798 wl_list_insert(&parent->subsurface_list_pending,
2799 &sub->parent_link_pending);
2800
2801 return sub;
2802}
2803
2804static void
2805subcompositor_get_subsurface(struct wl_client *client,
2806 struct wl_resource *resource,
2807 uint32_t id,
2808 struct wl_resource *surface_resource,
2809 struct wl_resource *parent_resource)
2810{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002811 struct weston_surface *surface =
2812 wl_resource_get_user_data(surface_resource);
2813 struct weston_surface *parent =
2814 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002815 struct weston_subsurface *sub;
2816 static const char where[] = "get_subsurface: wl_subsurface@";
2817
2818 if (surface == parent) {
2819 wl_resource_post_error(resource,
2820 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2821 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002822 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002823 return;
2824 }
2825
2826 if (weston_surface_to_subsurface(surface)) {
2827 wl_resource_post_error(resource,
2828 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2829 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002830 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002831 return;
2832 }
2833
2834 if (surface->configure) {
2835 wl_resource_post_error(resource,
2836 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2837 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002838 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002839 return;
2840 }
2841
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002842 if (weston_surface_get_main_surface(parent) == surface) {
2843 wl_resource_post_error(resource,
2844 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2845 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002846 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002847 return;
2848 }
2849
Pekka Paalanene67858b2013-04-25 13:57:42 +03002850 /* make sure the parent is in its own list */
2851 if (wl_list_empty(&parent->subsurface_list)) {
2852 if (!weston_subsurface_create_for_parent(parent)) {
2853 wl_resource_post_no_memory(resource);
2854 return;
2855 }
2856 }
2857
2858 sub = weston_subsurface_create(id, surface, parent);
2859 if (!sub) {
2860 wl_resource_post_no_memory(resource);
2861 return;
2862 }
2863
2864 surface->configure = subsurface_configure;
2865 surface->configure_private = sub;
2866}
2867
2868static void
2869subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2870{
2871 wl_resource_destroy(resource);
2872}
2873
2874static const struct wl_subcompositor_interface subcompositor_interface = {
2875 subcompositor_destroy,
2876 subcompositor_get_subsurface
2877};
2878
2879static void
2880bind_subcompositor(struct wl_client *client,
2881 void *data, uint32_t version, uint32_t id)
2882{
2883 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002884 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885
Jason Ekstranda85118c2013-06-27 20:17:02 -05002886 resource =
2887 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002888 if (resource == NULL) {
2889 wl_client_post_no_memory(client);
2890 return;
2891 }
2892 wl_resource_set_implementation(resource, &subcompositor_interface,
2893 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002894}
2895
2896static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002897weston_compositor_dpms(struct weston_compositor *compositor,
2898 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002899{
2900 struct weston_output *output;
2901
2902 wl_list_for_each(output, &compositor->output_list, link)
2903 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002904 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002905}
2906
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002907WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002908weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002909{
Neil Roberts8b62e202013-09-30 13:14:47 +01002910 uint32_t old_state = compositor->state;
2911
2912 /* The state needs to be changed before emitting the wake
2913 * signal because that may try to schedule a repaint which
2914 * will not work if the compositor is still sleeping */
2915 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2916
2917 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002918 case WESTON_COMPOSITOR_SLEEPING:
2919 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2920 /* fall through */
2921 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002922 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002923 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002924 /* fall through */
2925 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002926 wl_event_source_timer_update(compositor->idle_source,
2927 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002928 }
2929}
2930
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002931WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002932weston_compositor_offscreen(struct weston_compositor *compositor)
2933{
2934 switch (compositor->state) {
2935 case WESTON_COMPOSITOR_OFFSCREEN:
2936 return;
2937 case WESTON_COMPOSITOR_SLEEPING:
2938 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2939 /* fall through */
2940 default:
2941 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2942 wl_event_source_timer_update(compositor->idle_source, 0);
2943 }
2944}
2945
2946WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002947weston_compositor_sleep(struct weston_compositor *compositor)
2948{
2949 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2950 return;
2951
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002952 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002953 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2954 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2955}
2956
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002957static int
2958idle_handler(void *data)
2959{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002960 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002961
2962 if (compositor->idle_inhibit)
2963 return 1;
2964
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002965 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002966 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002967
2968 return 1;
2969}
2970
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002971WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002972weston_plane_init(struct weston_plane *plane,
2973 struct weston_compositor *ec,
2974 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002975{
2976 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002977 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002978 plane->x = x;
2979 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002980 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002981
2982 /* Init the link so that the call to wl_list_remove() when releasing
2983 * the plane without ever stacking doesn't lead to a crash */
2984 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002985}
2986
2987WL_EXPORT void
2988weston_plane_release(struct weston_plane *plane)
2989{
Xiong Zhang97116532013-10-23 13:58:31 +08002990 struct weston_view *view;
2991
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002992 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002993 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002994
Xiong Zhang97116532013-10-23 13:58:31 +08002995 wl_list_for_each(view, &plane->compositor->view_list, link) {
2996 if (view->plane == plane)
2997 view->plane = NULL;
2998 }
2999
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003000 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003001}
3002
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003003WL_EXPORT void
3004weston_compositor_stack_plane(struct weston_compositor *ec,
3005 struct weston_plane *plane,
3006 struct weston_plane *above)
3007{
3008 if (above)
3009 wl_list_insert(above->link.prev, &plane->link);
3010 else
3011 wl_list_insert(&ec->plane_list, &plane->link);
3012}
3013
Casey Dahlin9074db52012-04-19 22:50:09 -04003014static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003015{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003016 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003017}
3018
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003019static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003020bind_output(struct wl_client *client,
3021 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003022{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003023 struct weston_output *output = data;
3024 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003025 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003026
Jason Ekstranda85118c2013-06-27 20:17:02 -05003027 resource = wl_resource_create(client, &wl_output_interface,
3028 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003029 if (resource == NULL) {
3030 wl_client_post_no_memory(client);
3031 return;
3032 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003033
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003034 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003035 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003036
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003037 wl_output_send_geometry(resource,
3038 output->x,
3039 output->y,
3040 output->mm_width,
3041 output->mm_height,
3042 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003043 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003044 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003045 if (version >= 2)
3046 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003047 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003048
3049 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003050 wl_output_send_mode(resource,
3051 mode->flags,
3052 mode->width,
3053 mode->height,
3054 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003055 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003056
3057 if (version >= 2)
3058 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003059}
3060
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003061/* Move other outputs when one is removed so the space remains contiguos. */
3062static void
3063weston_compositor_remove_output(struct weston_compositor *compositor,
3064 struct weston_output *remove_output)
3065{
3066 struct weston_output *output;
3067 int offset = 0;
3068
3069 wl_list_for_each(output, &compositor->output_list, link) {
3070 if (output == remove_output) {
3071 offset = output->width;
3072 continue;
3073 }
3074
3075 if (offset > 0) {
3076 weston_output_move(output,
3077 output->x - offset, output->y);
3078 output->dirty = 1;
3079 }
3080 }
3081}
3082
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003083WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003084weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003085{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003086 output->destroying = 1;
3087
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003088 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003089 wl_list_remove(&output->link);
3090
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003091 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003092 wl_signal_emit(&output->destroy_signal, output);
3093
Richard Hughesafe690c2013-05-02 10:10:04 +01003094 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003095 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003096 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003097 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003098
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003099 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003100}
3101
Scott Moreau1bad5db2012-08-18 01:04:05 -06003102static void
3103weston_output_compute_transform(struct weston_output *output)
3104{
3105 struct weston_matrix transform;
3106 int flip;
3107
3108 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003109 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003110
3111 switch(output->transform) {
3112 case WL_OUTPUT_TRANSFORM_FLIPPED:
3113 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3114 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3115 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003116 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003117 flip = -1;
3118 break;
3119 default:
3120 flip = 1;
3121 break;
3122 }
3123
3124 switch(output->transform) {
3125 case WL_OUTPUT_TRANSFORM_NORMAL:
3126 case WL_OUTPUT_TRANSFORM_FLIPPED:
3127 transform.d[0] = flip;
3128 transform.d[1] = 0;
3129 transform.d[4] = 0;
3130 transform.d[5] = 1;
3131 break;
3132 case WL_OUTPUT_TRANSFORM_90:
3133 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3134 transform.d[0] = 0;
3135 transform.d[1] = -flip;
3136 transform.d[4] = 1;
3137 transform.d[5] = 0;
3138 break;
3139 case WL_OUTPUT_TRANSFORM_180:
3140 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3141 transform.d[0] = -flip;
3142 transform.d[1] = 0;
3143 transform.d[4] = 0;
3144 transform.d[5] = -1;
3145 break;
3146 case WL_OUTPUT_TRANSFORM_270:
3147 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3148 transform.d[0] = 0;
3149 transform.d[1] = flip;
3150 transform.d[4] = -1;
3151 transform.d[5] = 0;
3152 break;
3153 default:
3154 break;
3155 }
3156
3157 weston_matrix_multiply(&output->matrix, &transform);
3158}
3159
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003160WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003161weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003162{
Scott Moreau850ca422012-05-21 15:21:25 -06003163 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003164 struct weston_matrix camera;
3165 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003166
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003167 weston_matrix_init(&output->matrix);
3168 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003169 -(output->x + output->width / 2.0),
3170 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003171
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003172 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003173 2.0 / output->width,
3174 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003175
3176 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003177
Scott Moreauccbf29d2012-02-22 14:21:41 -07003178 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003179 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003180 weston_matrix_init(&camera);
3181 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003182 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003183 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003184 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003185 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003186 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003187 weston_matrix_multiply(&output->matrix, &modelview);
3188 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003189
Scott Moreauccbf29d2012-02-22 14:21:41 -07003190 output->dirty = 0;
3191}
3192
Scott Moreau1bad5db2012-08-18 01:04:05 -06003193static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003194weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003195{
3196 output->transform = transform;
3197
3198 switch (transform) {
3199 case WL_OUTPUT_TRANSFORM_90:
3200 case WL_OUTPUT_TRANSFORM_270:
3201 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3202 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3203 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003204 output->width = output->current_mode->height;
3205 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003206 break;
3207 case WL_OUTPUT_TRANSFORM_NORMAL:
3208 case WL_OUTPUT_TRANSFORM_180:
3209 case WL_OUTPUT_TRANSFORM_FLIPPED:
3210 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003211 output->width = output->current_mode->width;
3212 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003213 break;
3214 default:
3215 break;
3216 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003217
Hardening57388e42013-09-18 23:56:36 +02003218 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003219 output->width /= scale;
3220 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003221}
3222
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003223static void
3224weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003225{
3226 output->x = x;
3227 output->y = y;
3228
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003229 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003230 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003231 output->width,
3232 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003233}
3234
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003235WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003236weston_output_move(struct weston_output *output, int x, int y)
3237{
3238 pixman_region32_t old_region;
3239 struct wl_resource *resource;
3240
3241 output->move_x = x - output->x;
3242 output->move_y = y - output->y;
3243
3244 if (output->move_x == 0 && output->move_y == 0)
3245 return;
3246
3247 pixman_region32_init(&old_region);
3248 pixman_region32_copy(&old_region, &output->region);
3249
3250 weston_output_init_geometry(output, x, y);
3251
3252 output->dirty = 1;
3253
3254 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003255 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003256
3257 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003258 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003259 wl_output_send_geometry(resource,
3260 output->x,
3261 output->y,
3262 output->mm_width,
3263 output->mm_height,
3264 output->subpixel,
3265 output->make,
3266 output->model,
3267 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003268
3269 if (wl_resource_get_version(resource) >= 2)
3270 wl_output_send_done(resource);
3271 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003272}
3273
3274WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003275weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003276 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003277 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003278{
3279 output->compositor = c;
3280 output->x = x;
3281 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003282 output->mm_width = mm_width;
3283 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003284 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003285 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003286
Alexander Larsson0b135062013-05-28 16:23:36 +02003287 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003288 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003289
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003290 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003291 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003292
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003293 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003294 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003295 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003296 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003297
Casey Dahlin58ba1372012-04-19 22:50:08 -04003298 output->id = ffs(~output->compositor->output_id_pool) - 1;
3299 output->compositor->output_id_pool |= 1 << output->id;
3300
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003301 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003302 wl_global_create(c->wl_display, &wl_output_interface, 2,
3303 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003304 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003305}
3306
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003307WL_EXPORT void
3308weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003309 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003310 wl_fixed_t *x, wl_fixed_t *y)
3311{
3312 wl_fixed_t tx, ty;
3313 wl_fixed_t width, height;
3314
Hardeningff39efa2013-09-18 23:56:35 +02003315 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3316 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003317
3318 switch(output->transform) {
3319 case WL_OUTPUT_TRANSFORM_NORMAL:
3320 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003321 tx = device_x;
3322 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003323 break;
3324 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003325 tx = device_y;
3326 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003327 break;
3328 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003329 tx = width - device_x;
3330 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003331 break;
3332 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003333 tx = width - device_y;
3334 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003335 break;
3336 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003337 tx = width - device_x;
3338 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003339 break;
3340 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003341 tx = width - device_y;
3342 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003343 break;
3344 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003345 tx = device_x;
3346 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003347 break;
3348 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003349 tx = device_y;
3350 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003351 break;
3352 }
3353
Hardeningff39efa2013-09-18 23:56:35 +02003354 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3355 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003356}
3357
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003358static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003359destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003360{
Jonny Lamb74130762013-11-26 18:19:46 +01003361 struct weston_surface *surface =
3362 wl_resource_get_user_data(resource);
3363
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003364 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003365 surface->pending.buffer_viewport.buffer.src_width =
3366 wl_fixed_from_int(-1);
3367 surface->pending.buffer_viewport.surface.width = -1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003368}
3369
3370static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003371viewport_destroy(struct wl_client *client,
3372 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003373{
3374 wl_resource_destroy(resource);
3375}
3376
3377static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003378viewport_set(struct wl_client *client,
3379 struct wl_resource *resource,
3380 wl_fixed_t src_x,
3381 wl_fixed_t src_y,
3382 wl_fixed_t src_width,
3383 wl_fixed_t src_height,
3384 int32_t dst_width,
3385 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003386{
Jonny Lamb74130762013-11-26 18:19:46 +01003387 struct weston_surface *surface =
3388 wl_resource_get_user_data(resource);
3389
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003390 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003391
Jonny Lamb8ae35902013-11-26 18:19:45 +01003392 if (wl_fixed_to_double(src_width) < 0 ||
3393 wl_fixed_to_double(src_height) < 0) {
3394 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003395 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003396 "source dimensions must be non-negative (%fx%f)",
3397 wl_fixed_to_double(src_width),
3398 wl_fixed_to_double(src_height));
3399 return;
3400 }
3401
3402 if (dst_width <= 0 || dst_height <= 0) {
3403 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003404 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003405 "destination dimensions must be positive (%dx%d)",
3406 dst_width, dst_height);
3407 return;
3408 }
3409
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003410 surface->pending.buffer_viewport.buffer.src_x = src_x;
3411 surface->pending.buffer_viewport.buffer.src_y = src_y;
3412 surface->pending.buffer_viewport.buffer.src_width = src_width;
3413 surface->pending.buffer_viewport.buffer.src_height = src_height;
3414 surface->pending.buffer_viewport.surface.width = dst_width;
3415 surface->pending.buffer_viewport.surface.height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003416}
3417
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003418static const struct wl_viewport_interface viewport_interface = {
3419 viewport_destroy,
3420 viewport_set
Jonny Lamb8ae35902013-11-26 18:19:45 +01003421};
3422
3423static void
3424scaler_destroy(struct wl_client *client,
3425 struct wl_resource *resource)
3426{
3427 wl_resource_destroy(resource);
3428}
3429
3430static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003431scaler_get_viewport(struct wl_client *client,
3432 struct wl_resource *scaler,
3433 uint32_t id,
3434 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003435{
3436 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3437 struct wl_resource *resource;
3438
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003439 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003440 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003441 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3442 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003443 return;
3444 }
3445
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003446 resource = wl_resource_create(client, &wl_viewport_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003447 1, id);
3448 if (resource == NULL) {
3449 wl_client_post_no_memory(client);
3450 return;
3451 }
3452
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003453 wl_resource_set_implementation(resource, &viewport_interface,
3454 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003455
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003456 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003457}
3458
3459static const struct wl_scaler_interface scaler_interface = {
3460 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003461 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003462};
3463
3464static void
3465bind_scaler(struct wl_client *client,
3466 void *data, uint32_t version, uint32_t id)
3467{
3468 struct wl_resource *resource;
3469
3470 resource = wl_resource_create(client, &wl_scaler_interface,
3471 1, id);
3472 if (resource == NULL) {
3473 wl_client_post_no_memory(client);
3474 return;
3475 }
3476
3477 wl_resource_set_implementation(resource, &scaler_interface,
3478 NULL, NULL);
3479}
3480
3481static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003482compositor_bind(struct wl_client *client,
3483 void *data, uint32_t version, uint32_t id)
3484{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003485 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003486 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003487
Jason Ekstranda85118c2013-06-27 20:17:02 -05003488 resource = wl_resource_create(client, &wl_compositor_interface,
3489 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003490 if (resource == NULL) {
3491 wl_client_post_no_memory(client);
3492 return;
3493 }
3494
3495 wl_resource_set_implementation(resource, &compositor_interface,
3496 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003497}
3498
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003499static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003500log_uname(void)
3501{
3502 struct utsname usys;
3503
3504 uname(&usys);
3505
3506 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3507 usys.version, usys.machine);
3508}
3509
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003510WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003511weston_environment_get_fd(const char *env)
3512{
3513 char *e, *end;
3514 int fd, flags;
3515
3516 e = getenv(env);
3517 if (!e)
3518 return -1;
3519 fd = strtol(e, &end, 0);
3520 if (*end != '\0')
3521 return -1;
3522
3523 flags = fcntl(fd, F_GETFD);
3524 if (flags == -1)
3525 return -1;
3526
3527 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3528 unsetenv(env);
3529
3530 return fd;
3531}
3532
3533WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003534weston_compositor_init(struct weston_compositor *ec,
3535 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003536 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003537 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003538{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003539 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003540 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003541 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003542
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003543 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003544 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003545 wl_signal_init(&ec->destroy_signal);
3546 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003547 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003548 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003549 wl_signal_init(&ec->idle_signal);
3550 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003551 wl_signal_init(&ec->show_input_panel_signal);
3552 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003553 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003554 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003555 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003556 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003557 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003558 wl_signal_init(&ec->session_signal);
3559 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003560
Casey Dahlin58ba1372012-04-19 22:50:08 -04003561 ec->output_id_pool = 0;
3562
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003563 if (!wl_global_create(display, &wl_compositor_interface, 3,
3564 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003565 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003566
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003567 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3568 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003569 return -1;
3570
Jonny Lamb8ae35902013-11-26 18:19:45 +01003571 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3572 ec, bind_scaler))
3573 return -1;
3574
Jason Ekstranda7af7042013-10-12 22:38:11 -05003575 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003576 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003577 wl_list_init(&ec->layer_list);
3578 wl_list_init(&ec->seat_list);
3579 wl_list_init(&ec->output_list);
3580 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003581 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003582 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003583 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003584 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003585 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003586
Xiong Zhang97116532013-10-23 13:58:31 +08003587 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003588 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003589
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003590 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3591 weston_config_section_get_string(s, "keymap_rules",
3592 (char **) &xkb_names.rules, NULL);
3593 weston_config_section_get_string(s, "keymap_model",
3594 (char **) &xkb_names.model, NULL);
3595 weston_config_section_get_string(s, "keymap_layout",
3596 (char **) &xkb_names.layout, NULL);
3597 weston_config_section_get_string(s, "keymap_variant",
3598 (char **) &xkb_names.variant, NULL);
3599 weston_config_section_get_string(s, "keymap_options",
3600 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003601
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003602 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3603 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003604
Daniel Stone725c2c32012-06-22 14:04:36 +01003605 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003606 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003607
3608 wl_data_device_manager_init(ec->wl_display);
3609
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003610 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003611
Daniel Stone725c2c32012-06-22 14:04:36 +01003612 loop = wl_display_get_event_loop(ec->wl_display);
3613 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3614 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3615
3616 ec->input_loop = wl_event_loop_create();
3617
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003618 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3619 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3620
3621 weston_compositor_schedule_repaint(ec);
3622
Daniel Stone725c2c32012-06-22 14:04:36 +01003623 return 0;
3624}
3625
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003626WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003627weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003628{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003629 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003630
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003631 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003632 if (ec->input_loop_source)
3633 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003634
Matt Roper361d2ad2011-08-29 13:52:23 -07003635 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003636 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003637 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003638
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003639 if (ec->renderer)
3640 ec->renderer->destroy(ec);
3641
Daniel Stone325fc2d2012-05-30 16:31:58 +01003642 weston_binding_list_destroy_all(&ec->key_binding_list);
3643 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003644 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003645 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003646 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003647
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003648 weston_plane_release(&ec->primary_plane);
3649
Jonas Ådahlc97af922012-03-28 22:36:09 +02003650 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003651
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003652 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003653}
3654
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003655WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003656weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3657 const struct weston_pointer_grab_interface *interface)
3658{
3659 struct weston_seat *seat;
3660
3661 ec->default_pointer_grab = interface;
3662 wl_list_for_each(seat, &ec->seat_list, link) {
3663 if (seat->pointer) {
3664 weston_pointer_set_default_grab(seat->pointer,
3665 interface);
3666 }
3667 }
3668}
3669
3670WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003671weston_version(int *major, int *minor, int *micro)
3672{
3673 *major = WESTON_VERSION_MAJOR;
3674 *minor = WESTON_VERSION_MINOR;
3675 *micro = WESTON_VERSION_MICRO;
3676}
3677
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003678static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003679 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003680 const char *desc;
3681} capability_strings[] = {
3682 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003683 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003684};
3685
3686static void
3687weston_compositor_log_capabilities(struct weston_compositor *compositor)
3688{
3689 unsigned i;
3690 int yes;
3691
3692 weston_log("Compositor capabilities:\n");
3693 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3694 yes = compositor->capabilities & capability_strings[i].bit;
3695 weston_log_continue(STAMP_SPACE "%s %s\n",
3696 capability_strings[i].desc,
3697 yes ? "yes" : "no");
3698 }
3699}
3700
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003701static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003702{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003703 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003704
Martin Minarik6d118362012-06-07 18:01:59 +02003705 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003706 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003707
3708 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003709}
3710
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003711#ifdef HAVE_LIBUNWIND
3712
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003713static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003714print_backtrace(void)
3715{
3716 unw_cursor_t cursor;
3717 unw_context_t context;
3718 unw_word_t off;
3719 unw_proc_info_t pip;
3720 int ret, i = 0;
3721 char procname[256];
3722 const char *filename;
3723 Dl_info dlinfo;
3724
3725 pip.unwind_info = NULL;
3726 ret = unw_getcontext(&context);
3727 if (ret) {
3728 weston_log("unw_getcontext: %d\n", ret);
3729 return;
3730 }
3731
3732 ret = unw_init_local(&cursor, &context);
3733 if (ret) {
3734 weston_log("unw_init_local: %d\n", ret);
3735 return;
3736 }
3737
3738 ret = unw_step(&cursor);
3739 while (ret > 0) {
3740 ret = unw_get_proc_info(&cursor, &pip);
3741 if (ret) {
3742 weston_log("unw_get_proc_info: %d\n", ret);
3743 break;
3744 }
3745
3746 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3747 if (ret && ret != -UNW_ENOMEM) {
3748 if (ret != -UNW_EUNSPEC)
3749 weston_log("unw_get_proc_name: %d\n", ret);
3750 procname[0] = '?';
3751 procname[1] = 0;
3752 }
3753
3754 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3755 *dlinfo.dli_fname)
3756 filename = dlinfo.dli_fname;
3757 else
3758 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003759
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003760 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3761 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3762
3763 ret = unw_step(&cursor);
3764 if (ret < 0)
3765 weston_log("unw_step: %d\n", ret);
3766 }
3767}
3768
3769#else
3770
3771static void
3772print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003773{
3774 void *buffer[32];
3775 int i, count;
3776 Dl_info info;
3777
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003778 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3779 for (i = 0; i < count; i++) {
3780 dladdr(buffer[i], &info);
3781 weston_log(" [%016lx] %s (%s)\n",
3782 (long) buffer[i],
3783 info.dli_sname ? info.dli_sname : "--",
3784 info.dli_fname);
3785 }
3786}
3787
3788#endif
3789
3790static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003791on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003792{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003793 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003794 * then call the backend restore function, which will switch
3795 * back to the vt we launched from or ungrab X etc and then
3796 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003797 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003798 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003799 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003800
Peter Maatmane5b42e42013-03-27 22:38:53 +01003801 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003802
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003803 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003804
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003805 segv_compositor->restore(segv_compositor);
3806
3807 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003808}
3809
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003810WL_EXPORT void *
3811weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003812{
3813 char path[PATH_MAX];
3814 void *module, *init;
3815
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003816 if (name == NULL)
3817 return NULL;
3818
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003819 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003820 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003821 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003822 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003823
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003824 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3825 if (module) {
3826 weston_log("Module '%s' already loaded\n", path);
3827 dlclose(module);
3828 return NULL;
3829 }
3830
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003831 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003832 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003833 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003834 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003835 return NULL;
3836 }
3837
3838 init = dlsym(module, entrypoint);
3839 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003840 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003841 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003842 return NULL;
3843 }
3844
3845 return init;
3846}
3847
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003848static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003849load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003850 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003851{
3852 const char *p, *end;
3853 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003854 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003855 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003856
3857 if (modules == NULL)
3858 return 0;
3859
3860 p = modules;
3861 while (*p) {
3862 end = strchrnul(p, ',');
3863 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003864 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003865 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003866 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003867 p = end;
3868 while (*p == ',')
3869 p++;
3870
3871 }
3872
3873 return 0;
3874}
3875
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003876static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003877 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3878
3879static const char xdg_wrong_message[] =
3880 "fatal: environment variable XDG_RUNTIME_DIR\n"
3881 "is set to \"%s\", which is not a directory.\n";
3882
3883static const char xdg_wrong_mode_message[] =
3884 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01003885 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
3886 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003887
3888static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003889 "Refer to your distribution on how to get it, or\n"
3890 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3891 "on how to implement it.\n";
3892
Martin Minarik37032f82012-06-18 20:15:18 +02003893static void
3894verify_xdg_runtime_dir(void)
3895{
3896 char *dir = getenv("XDG_RUNTIME_DIR");
3897 struct stat s;
3898
3899 if (!dir) {
3900 weston_log(xdg_error_message);
3901 weston_log_continue(xdg_detail_message);
3902 exit(EXIT_FAILURE);
3903 }
3904
3905 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3906 weston_log(xdg_wrong_message, dir);
3907 weston_log_continue(xdg_detail_message);
3908 exit(EXIT_FAILURE);
3909 }
3910
3911 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3912 weston_log(xdg_wrong_mode_message,
3913 dir, s.st_mode & 0777, s.st_uid);
3914 weston_log_continue(xdg_detail_message);
3915 }
3916}
3917
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003918static int
3919usage(int error_code)
3920{
3921 fprintf(stderr,
3922 "Usage: weston [OPTIONS]\n\n"
3923 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3924 "Weston supports multiple backends, and depending on which backend is in use\n"
3925 "different options will be accepted.\n\n"
3926
3927
3928 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003929 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003930 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003931 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3932 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003933 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003934 " -S, --socket=NAME\tName of socket to listen on\n"
3935 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003936 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003937 " --log==FILE\t\tLog to the given file\n"
3938 " -h, --help\t\tThis help message\n\n");
3939
3940 fprintf(stderr,
3941 "Options for drm-backend.so:\n\n"
3942 " --connector=ID\tBring up only this connector\n"
3943 " --seat=SEAT\t\tThe seat that weston should run on\n"
3944 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003945 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003946 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3947
3948 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003949 "Options for fbdev-backend.so:\n\n"
3950 " --tty=TTY\t\tThe tty to use\n"
3951 " --device=DEVICE\tThe framebuffer device to use\n\n");
3952
3953 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003954 "Options for x11-backend.so:\n\n"
3955 " --width=WIDTH\t\tWidth of X window\n"
3956 " --height=HEIGHT\tHeight of X window\n"
3957 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003958 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003959 " --output-count=COUNT\tCreate multiple outputs\n"
3960 " --no-input\t\tDont create input devices\n\n");
3961
3962 fprintf(stderr,
3963 "Options for wayland-backend.so:\n\n"
3964 " --width=WIDTH\t\tWidth of Wayland surface\n"
3965 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003966 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003967 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003968 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003969 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003970 " --display=DISPLAY\tWayland display to connect to\n\n");
3971
Pekka Paalanene31e0532013-05-22 18:03:07 +03003972#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3973 fprintf(stderr,
3974 "Options for rpi-backend.so:\n\n"
3975 " --tty=TTY\t\tThe tty to use\n"
3976 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3977 " --transform=TR\tThe output transformation, TR is one of:\n"
3978 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003979 " --opaque-regions\tEnable support for opaque regions, can be "
3980 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003981 "\n");
3982#endif
3983
Hardeningc077a842013-07-08 00:51:35 +02003984#if defined(BUILD_RDP_COMPOSITOR)
3985 fprintf(stderr,
3986 "Options for rdp-backend.so:\n\n"
3987 " --width=WIDTH\t\tWidth of desktop\n"
3988 " --height=HEIGHT\tHeight of desktop\n"
3989 " --extra-modes=MODES\t\n"
3990 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3991 " --address=ADDR\tThe address to bind\n"
3992 " --port=PORT\tThe port to listen on\n"
3993 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3994 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3995 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3996 "\n");
3997#endif
3998
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003999 exit(error_code);
4000}
4001
Peter Maatmane5b42e42013-03-27 22:38:53 +01004002static void
4003catch_signals(void)
4004{
4005 struct sigaction action;
4006
4007 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4008 action.sa_sigaction = on_caught_signal;
4009 sigemptyset(&action.sa_mask);
4010 sigaction(SIGSEGV, &action, NULL);
4011 sigaction(SIGABRT, &action, NULL);
4012}
4013
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004014int main(int argc, char *argv[])
4015{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004016 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004017 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004018 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004019 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004020 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004021 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004022 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004023 int *argc, char *argv[],
4024 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004025 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004026 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004027 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004028 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004029 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004030 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004031 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004032 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004033 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004034 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004035 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004036 struct weston_config *config;
4037 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004038
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004039 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004040 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004041 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004042 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4043 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004044 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004045 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004046 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004047 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004048 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004049
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004050 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004051
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004052 if (help)
4053 usage(EXIT_SUCCESS);
4054
Scott Moreau12245142012-08-29 15:15:58 -06004055 if (version) {
4056 printf(PACKAGE_STRING "\n");
4057 return EXIT_SUCCESS;
4058 }
4059
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004060 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004061
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004062 weston_log("%s\n"
4063 STAMP_SPACE "%s\n"
4064 STAMP_SPACE "Bug reports to: %s\n"
4065 STAMP_SPACE "Build: %s\n",
4066 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004067 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004068 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004069
Martin Minarik37032f82012-06-18 20:15:18 +02004070 verify_xdg_runtime_dir();
4071
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004072 display = wl_display_create();
4073
Tiago Vignatti2116b892011-08-08 05:52:59 -07004074 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004075 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4076 display);
4077 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4078 display);
4079 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4080 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004081
4082 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004083 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4084 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004085
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004086 config = weston_config_parse("weston.ini");
4087 if (config != NULL) {
4088 weston_log("Using config file '%s'\n",
4089 weston_config_get_full_path(config));
4090 } else {
4091 weston_log("Starting with no config file.\n");
4092 }
4093 section = weston_config_get_section(config, "core", NULL, NULL);
4094
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004095 if (option_backend)
4096 backend = strdup(option_backend);
4097 else
4098 weston_config_section_get_string(section, "backend", &backend,
4099 NULL);
4100
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004101 if (!backend) {
4102 if (getenv("WAYLAND_DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004103 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004104 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004105 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004106 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004107 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004108 }
4109
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004110 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004111 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004112 if (!backend_init)
4113 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004114
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004115 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004116 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004117 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004118 exit(EXIT_FAILURE);
4119 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004120
Peter Maatmane5b42e42013-03-27 22:38:53 +01004121 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004122 segv_compositor = ec;
4123
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004124 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004125 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004126
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004127 setenv("WAYLAND_DISPLAY", socket_name, 1);
4128
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004129 if (option_shell)
4130 shell = strdup(option_shell);
4131 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004132 weston_config_section_get_string(section, "shell", &shell,
4133 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004134
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004135 if (load_modules(ec, shell, &argc, argv) < 0) {
4136 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004137 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004138 }
4139 free(shell);
4140
4141 weston_config_section_get_string(section, "modules", &modules, "");
4142 if (load_modules(ec, modules, &argc, argv) < 0) {
4143 free(modules);
4144 goto out;
4145 }
4146 free(modules);
4147
Ossama Othmana50e6e42013-05-14 09:48:26 -07004148 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004149 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004150
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004151 for (i = 1; i < argc; i++)
4152 weston_log("fatal: unhandled option: %s\n", argv[i]);
4153 if (argc > 1) {
4154 ret = EXIT_FAILURE;
4155 goto out;
4156 }
4157
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004158 weston_compositor_log_capabilities(ec);
4159
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004160 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004161 weston_log("fatal: failed to add socket: %m\n");
4162 ret = EXIT_FAILURE;
4163 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004164 }
4165
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004166 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004167
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004168 wl_display_run(display);
4169
4170 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004171 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004172 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004173
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004174 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004175
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004176 for (i = ARRAY_LENGTH(signals); i;)
4177 wl_event_source_remove(signals[--i]);
4178
Daniel Stone855028f2012-05-01 20:37:10 +01004179 weston_compositor_xkb_destroy(ec);
4180
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004181 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004182 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004183
Martin Minarik19e6f262012-06-07 13:08:46 +02004184 weston_log_file_close();
4185
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004186 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004187}