blob: 5e70e9ec4e3e44544fab5a93ba6d06d890791c8e [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 Paalanen1fd9c0f2013-11-26 18:19:41 +0100403 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
404 surface->buffer_viewport.scale = 1;
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200405 surface->buffer_viewport.viewport_set = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100406 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400407 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100408 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200409
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200410 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100411
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400412 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500413 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300414 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400415
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416 wl_list_init(&surface->views);
417
418 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500419
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300420 surface->pending.buffer_destroy_listener.notify =
421 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300422 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300423 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300424 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300425 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300426
Pekka Paalanene67858b2013-04-25 13:57:42 +0300427 wl_list_init(&surface->subsurface_list);
428 wl_list_init(&surface->subsurface_list_pending);
429
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400430 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500431}
432
Alex Wu8811bf92012-02-28 18:07:54 +0800433WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500434weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200435 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500436{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100437 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500438}
439
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400440WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500441weston_view_to_global_float(struct weston_view *view,
442 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200443{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500444 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200445 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
446
Jason Ekstranda7af7042013-10-12 22:38:11 -0500447 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200448
449 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200450 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700451 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200452 v.f[3]);
453 *x = 0;
454 *y = 0;
455 return;
456 }
457
458 *x = v.f[0] / v.f[3];
459 *y = v.f[1] / v.f[3];
460 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500461 *x = sx + view->geometry.x;
462 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200463 }
464}
465
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500466WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200467weston_transformed_coord(int width, int height,
468 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200469 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200470 float sx, float sy, float *bx, float *by)
471{
472 switch (transform) {
473 case WL_OUTPUT_TRANSFORM_NORMAL:
474 default:
475 *bx = sx;
476 *by = sy;
477 break;
478 case WL_OUTPUT_TRANSFORM_FLIPPED:
479 *bx = width - sx;
480 *by = sy;
481 break;
482 case WL_OUTPUT_TRANSFORM_90:
483 *bx = height - sy;
484 *by = sx;
485 break;
486 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
487 *bx = height - sy;
488 *by = width - sx;
489 break;
490 case WL_OUTPUT_TRANSFORM_180:
491 *bx = width - sx;
492 *by = height - sy;
493 break;
494 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
495 *bx = sx;
496 *by = height - sy;
497 break;
498 case WL_OUTPUT_TRANSFORM_270:
499 *bx = sy;
500 *by = width - sx;
501 break;
502 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
503 *bx = sy;
504 *by = sx;
505 break;
506 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200507
508 *bx *= scale;
509 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200510}
511
512WL_EXPORT pixman_box32_t
513weston_transformed_rect(int width, int height,
514 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200515 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200516 pixman_box32_t rect)
517{
518 float x1, x2, y1, y2;
519
520 pixman_box32_t ret;
521
Alexander Larsson4ea95522013-05-22 14:41:37 +0200522 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200523 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200524 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200525 rect.x2, rect.y2, &x2, &y2);
526
527 if (x1 <= x2) {
528 ret.x1 = x1;
529 ret.x2 = x2;
530 } else {
531 ret.x1 = x2;
532 ret.x2 = x1;
533 }
534
535 if (y1 <= y2) {
536 ret.y1 = y1;
537 ret.y2 = y2;
538 } else {
539 ret.y1 = y2;
540 ret.y2 = y1;
541 }
542
543 return ret;
544}
545
546WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500547weston_transformed_region(int width, int height,
548 enum wl_output_transform transform,
549 int32_t scale,
550 pixman_region32_t *src, pixman_region32_t *dest)
551{
552 pixman_box32_t *src_rects, *dest_rects;
553 int nrects, i;
554
555 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
556 if (src != dest)
557 pixman_region32_copy(dest, src);
558 return;
559 }
560
561 src_rects = pixman_region32_rectangles(src, &nrects);
562 dest_rects = malloc(nrects * sizeof(*dest_rects));
563 if (!dest_rects)
564 return;
565
566 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
567 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
568 } else {
569 for (i = 0; i < nrects; i++) {
570 switch (transform) {
571 default:
572 case WL_OUTPUT_TRANSFORM_NORMAL:
573 dest_rects[i].x1 = src_rects[i].x1;
574 dest_rects[i].y1 = src_rects[i].y1;
575 dest_rects[i].x2 = src_rects[i].x2;
576 dest_rects[i].y2 = src_rects[i].y2;
577 break;
578 case WL_OUTPUT_TRANSFORM_90:
579 dest_rects[i].x1 = height - src_rects[i].y2;
580 dest_rects[i].y1 = src_rects[i].x1;
581 dest_rects[i].x2 = height - src_rects[i].y1;
582 dest_rects[i].y2 = src_rects[i].x2;
583 break;
584 case WL_OUTPUT_TRANSFORM_180:
585 dest_rects[i].x1 = width - src_rects[i].x2;
586 dest_rects[i].y1 = height - src_rects[i].y2;
587 dest_rects[i].x2 = width - src_rects[i].x1;
588 dest_rects[i].y2 = height - src_rects[i].y1;
589 break;
590 case WL_OUTPUT_TRANSFORM_270:
591 dest_rects[i].x1 = src_rects[i].y1;
592 dest_rects[i].y1 = width - src_rects[i].x2;
593 dest_rects[i].x2 = src_rects[i].y2;
594 dest_rects[i].y2 = width - src_rects[i].x1;
595 break;
596 case WL_OUTPUT_TRANSFORM_FLIPPED:
597 dest_rects[i].x1 = width - src_rects[i].x2;
598 dest_rects[i].y1 = src_rects[i].y1;
599 dest_rects[i].x2 = width - src_rects[i].x1;
600 dest_rects[i].y2 = src_rects[i].y2;
601 break;
602 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
603 dest_rects[i].x1 = height - src_rects[i].y2;
604 dest_rects[i].y1 = width - src_rects[i].x2;
605 dest_rects[i].x2 = height - src_rects[i].y1;
606 dest_rects[i].y2 = width - src_rects[i].x1;
607 break;
608 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
609 dest_rects[i].x1 = src_rects[i].x1;
610 dest_rects[i].y1 = height - src_rects[i].y2;
611 dest_rects[i].x2 = src_rects[i].x2;
612 dest_rects[i].y2 = height - src_rects[i].y1;
613 break;
614 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
615 dest_rects[i].x1 = src_rects[i].y1;
616 dest_rects[i].y1 = src_rects[i].x1;
617 dest_rects[i].x2 = src_rects[i].y2;
618 dest_rects[i].y2 = src_rects[i].x2;
619 break;
620 }
621 }
622 }
623
624 if (scale != 1) {
625 for (i = 0; i < nrects; i++) {
626 dest_rects[i].x1 *= scale;
627 dest_rects[i].x2 *= scale;
628 dest_rects[i].y1 *= scale;
629 dest_rects[i].y2 *= scale;
630 }
631 }
632
633 pixman_region32_clear(dest);
634 pixman_region32_init_rects(dest, dest_rects, nrects);
635 free(dest_rects);
636}
637
Jonny Lamb74130762013-11-26 18:19:46 +0100638static void
639scaler_surface_to_buffer(struct weston_surface *surface,
640 float sx, float sy, float *bx, float *by)
641{
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200642 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +0100643 double a, b;
644
645 a = sx / surface->buffer_viewport.dst_width;
646 b = a * wl_fixed_to_double(surface->buffer_viewport.src_width);
647 *bx = b + wl_fixed_to_double(surface->buffer_viewport.src_x);
648
649 a = sy / surface->buffer_viewport.dst_height;
650 b = a * wl_fixed_to_double(surface->buffer_viewport.src_height);
651 *by = b + wl_fixed_to_double(surface->buffer_viewport.src_y);
652 } else {
653 *bx = sx;
654 *by = sy;
655 }
656}
657
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500658WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200659weston_surface_to_buffer_float(struct weston_surface *surface,
660 float sx, float sy, float *bx, float *by)
661{
Jonny Lamb74130762013-11-26 18:19:46 +0100662 /* first transform coordinates if the scaler is set */
663 scaler_surface_to_buffer(surface, sx, sy, bx, by);
664
Jason Ekstranda7af7042013-10-12 22:38:11 -0500665 weston_transformed_coord(surface->width,
666 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100667 surface->buffer_viewport.transform,
668 surface->buffer_viewport.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100669 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200670}
671
Alexander Larsson4ea95522013-05-22 14:41:37 +0200672WL_EXPORT void
673weston_surface_to_buffer(struct weston_surface *surface,
674 int sx, int sy, int *bx, int *by)
675{
676 float bxf, byf;
677
Jonny Lamb74130762013-11-26 18:19:46 +0100678 weston_surface_to_buffer_float(surface,
679 sx, sy, &bxf, &byf);
680
Alexander Larsson4ea95522013-05-22 14:41:37 +0200681 *bx = floorf(bxf);
682 *by = floorf(byf);
683}
684
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200685WL_EXPORT pixman_box32_t
686weston_surface_to_buffer_rect(struct weston_surface *surface,
687 pixman_box32_t rect)
688{
Jonny Lamb74130762013-11-26 18:19:46 +0100689 float xf, yf;
690
691 /* first transform box coordinates if the scaler is set */
692 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
693 rect.x1 = floorf(xf);
694 rect.y1 = floorf(yf);
695
696 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
697 rect.x2 = floorf(xf);
698 rect.y2 = floorf(yf);
699
Jason Ekstranda7af7042013-10-12 22:38:11 -0500700 return weston_transformed_rect(surface->width,
701 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100702 surface->buffer_viewport.transform,
703 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200704 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200705}
706
707WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500708weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400709 struct weston_plane *plane)
710{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400712 return;
713
Jason Ekstranda7af7042013-10-12 22:38:11 -0500714 weston_view_damage_below(view);
715 view->plane = plane;
716 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400717}
718
719WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500720weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200721{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500722 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200723
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500724 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500725 pixman_region32_subtract(&damage, &view->transform.boundingbox,
726 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800727 if (view->plane)
728 pixman_region32_union(&view->plane->damage,
729 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500730 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800731 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200732}
733
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400734static void
735weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
736{
737 uint32_t different = es->output_mask ^ mask;
738 uint32_t entered = mask & different;
739 uint32_t left = es->output_mask & different;
740 struct weston_output *output;
741 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500742 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400743
744 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500745 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400746 return;
747 if (different == 0)
748 return;
749
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500750 client = wl_resource_get_client(es->resource);
751
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400752 wl_list_for_each(output, &es->compositor->output_list, link) {
753 if (1 << output->id & different)
754 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500755 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400756 client);
757 if (resource == NULL)
758 continue;
759 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500760 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400761 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500762 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400763 }
764}
765
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200766
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400767static void
768weston_surface_assign_output(struct weston_surface *es)
769{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500770 struct weston_output *new_output;
771 struct weston_view *view;
772 pixman_region32_t region;
773 uint32_t max, area, mask;
774 pixman_box32_t *e;
775
776 new_output = NULL;
777 max = 0;
778 mask = 0;
779 pixman_region32_init(&region);
780 wl_list_for_each(view, &es->views, surface_link) {
781 if (!view->output)
782 continue;
783
784 pixman_region32_intersect(&region, &view->transform.boundingbox,
785 &view->output->region);
786
787 e = pixman_region32_extents(&region);
788 area = (e->x2 - e->x1) * (e->y2 - e->y1);
789
790 mask |= view->output_mask;
791
792 if (area >= max) {
793 new_output = view->output;
794 max = area;
795 }
796 }
797 pixman_region32_fini(&region);
798
799 es->output = new_output;
800 weston_surface_update_output_mask(es, mask);
801}
802
803static void
804weston_view_assign_output(struct weston_view *ev)
805{
806 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400807 struct weston_output *output, *new_output;
808 pixman_region32_t region;
809 uint32_t max, area, mask;
810 pixman_box32_t *e;
811
812 new_output = NULL;
813 max = 0;
814 mask = 0;
815 pixman_region32_init(&region);
816 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500817 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400818 &output->region);
819
820 e = pixman_region32_extents(&region);
821 area = (e->x2 - e->x1) * (e->y2 - e->y1);
822
823 if (area > 0)
824 mask |= 1 << output->id;
825
826 if (area >= max) {
827 new_output = output;
828 max = area;
829 }
830 }
831 pixman_region32_fini(&region);
832
Jason Ekstranda7af7042013-10-12 22:38:11 -0500833 ev->output = new_output;
834 ev->output_mask = mask;
835
836 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400837}
838
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200839static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500840view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
841 int32_t width, int32_t height,
842 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200843{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200844 float min_x = HUGE_VALF, min_y = HUGE_VALF;
845 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200846 int32_t s[4][2] = {
847 { sx, sy },
848 { sx, sy + height },
849 { sx + width, sy },
850 { sx + width, sy + height }
851 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200852 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200853 int i;
854
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300855 if (width == 0 || height == 0) {
856 /* avoid rounding empty bbox to 1x1 */
857 pixman_region32_init(bbox);
858 return;
859 }
860
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200861 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200862 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500863 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200864 if (x < min_x)
865 min_x = x;
866 if (x > max_x)
867 max_x = x;
868 if (y < min_y)
869 min_y = y;
870 if (y > max_y)
871 max_y = y;
872 }
873
Pekka Paalanen219b9822012-02-08 15:38:37 +0200874 int_x = floorf(min_x);
875 int_y = floorf(min_y);
876 pixman_region32_init_rect(bbox, int_x, int_y,
877 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200878}
879
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200880static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200882{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200884
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200885 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886 view->geometry.x = roundf(view->geometry.x);
887 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200888
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500889 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500890 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
891 view->transform.position.matrix.d[12] = view->geometry.x;
892 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500893
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500895
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 view->transform.inverse = view->transform.position.matrix;
897 view->transform.inverse.d[12] = -view->geometry.x;
898 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500899
Jason Ekstranda7af7042013-10-12 22:38:11 -0500900 pixman_region32_init_rect(&view->transform.boundingbox,
901 view->geometry.x,
902 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600903 view->surface->width,
904 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500905
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 if (view->alpha == 1.0) {
907 pixman_region32_copy(&view->transform.opaque,
908 &view->surface->opaque);
909 pixman_region32_translate(&view->transform.opaque,
910 view->geometry.x,
911 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500912 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200913}
914
915static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200917{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500918 struct weston_view *parent = view->geometry.parent;
919 struct weston_matrix *matrix = &view->transform.matrix;
920 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200921 struct weston_transform *tform;
922
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200924
925 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500926 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
927 view->transform.position.matrix.d[12] = view->geometry.x;
928 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200929
930 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200932 weston_matrix_multiply(matrix, &tform->matrix);
933
Pekka Paalanen483243f2013-03-08 14:56:50 +0200934 if (parent)
935 weston_matrix_multiply(matrix, &parent->transform.matrix);
936
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200937 if (weston_matrix_invert(inverse, matrix) < 0) {
938 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 weston_log("error: weston_view %p"
940 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200941 return -1;
942 }
943
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600944 view_compute_bbox(view, 0, 0,
945 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500947
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200948 return 0;
949}
950
951WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200953{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500954 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200955
Jason Ekstranda7af7042013-10-12 22:38:11 -0500956 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200957 return;
958
Pekka Paalanen483243f2013-03-08 14:56:50 +0200959 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200961
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200963
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200965
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 pixman_region32_fini(&view->transform.boundingbox);
967 pixman_region32_fini(&view->transform.opaque);
968 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500969
Pekka Paalanencd403622012-01-25 13:37:39 +0200970 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 if (view->geometry.transformation_list.next ==
972 &view->transform.position.link &&
973 view->geometry.transformation_list.prev ==
974 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200975 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500976 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200977 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500978 if (weston_view_update_transform_enable(view) < 0)
979 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200980 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200981
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200983
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300985
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 wl_signal_emit(&view->surface->compositor->transform_signal,
987 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200988}
989
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200990WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200992{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200994
995 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 * The invariant: if view->geometry.dirty, then all views
997 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200998 * Corollary: if not parent->geometry.dirty, then all ancestors
999 * are not dirty.
1000 */
1001
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001003 return;
1004
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001006
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001008 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001010}
1011
1012WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013weston_view_to_global_fixed(struct weston_view *view,
1014 wl_fixed_t vx, wl_fixed_t vy,
1015 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001016{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001017 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001018
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 weston_view_to_global_float(view,
1020 wl_fixed_to_double(vx),
1021 wl_fixed_to_double(vy),
1022 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001023 *x = wl_fixed_from_double(xf);
1024 *y = wl_fixed_from_double(yf);
1025}
1026
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001027WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028weston_view_from_global_float(struct weston_view *view,
1029 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001030{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001032 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1033
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001035
1036 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001037 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001039 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 *vx = 0;
1041 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001042 return;
1043 }
1044
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 *vx = v.f[0] / v.f[3];
1046 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001047 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 *vx = x - view->geometry.x;
1049 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001050 }
1051}
1052
1053WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054weston_view_from_global_fixed(struct weston_view *view,
1055 wl_fixed_t x, wl_fixed_t y,
1056 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001057{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001059
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 weston_view_from_global_float(view,
1061 wl_fixed_to_double(x),
1062 wl_fixed_to_double(y),
1063 &vxf, &vyf);
1064 *vx = wl_fixed_from_double(vxf);
1065 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001066}
1067
1068WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069weston_view_from_global(struct weston_view *view,
1070 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001071{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001073
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1075 *vx = floorf(vxf);
1076 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001077}
1078
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001079WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001080weston_surface_schedule_repaint(struct weston_surface *surface)
1081{
1082 struct weston_output *output;
1083
1084 wl_list_for_each(output, &surface->compositor->output_list, link)
1085 if (surface->output_mask & (1 << output->id))
1086 weston_output_schedule_repaint(output);
1087}
1088
1089WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001090weston_view_schedule_repaint(struct weston_view *view)
1091{
1092 struct weston_output *output;
1093
1094 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1095 if (view->output_mask & (1 << output->id))
1096 weston_output_schedule_repaint(output);
1097}
1098
1099WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001100weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001101{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001102 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 0, 0, surface->width,
1104 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001105
Kristian Høgsberg98238702012-08-03 16:29:12 -04001106 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001107}
1108
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001109WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001111{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001112 if (view->geometry.x == x && view->geometry.y == y)
1113 return;
1114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 view->geometry.x = x;
1116 view->geometry.y = y;
1117 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001118}
1119
Pekka Paalanen483243f2013-03-08 14:56:50 +02001120static void
1121transform_parent_handle_parent_destroy(struct wl_listener *listener,
1122 void *data)
1123{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 struct weston_view *view =
1125 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001126 geometry.parent_destroy_listener);
1127
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001129}
1130
1131WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132weston_view_set_transform_parent(struct weston_view *view,
1133 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001134{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 if (view->geometry.parent) {
1136 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1137 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001138 }
1139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001141
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001143 transform_parent_handle_parent_destroy;
1144 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001145 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001147 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001149 }
1150
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 weston_view_geometry_dirty(view);
1152}
1153
1154WL_EXPORT int
1155weston_view_is_mapped(struct weston_view *view)
1156{
1157 if (view->output)
1158 return 1;
1159 else
1160 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001161}
1162
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001163WL_EXPORT int
1164weston_surface_is_mapped(struct weston_surface *surface)
1165{
1166 if (surface->output)
1167 return 1;
1168 else
1169 return 0;
1170}
1171
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001172static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001173surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001174{
1175 struct weston_view *view;
1176
1177 if (surface->width == width && surface->height == height)
1178 return;
1179
1180 surface->width = width;
1181 surface->height = height;
1182
1183 wl_list_for_each(view, &surface->views, surface_link)
1184 weston_view_geometry_dirty(view);
1185}
1186
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001187WL_EXPORT void
1188weston_surface_set_size(struct weston_surface *surface,
1189 int32_t width, int32_t height)
1190{
1191 assert(!surface->resource);
1192 surface_set_size(surface, width, height);
1193}
1194
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001195static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001196weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001197{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001198 int32_t width, height;
1199
1200 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001201 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001202 return;
1203 }
1204
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02001205 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +01001206 surface->width = surface->buffer_viewport.dst_width;
1207 surface->height = surface->buffer_viewport.dst_height;
1208 return;
1209 }
1210
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001211 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001212 case WL_OUTPUT_TRANSFORM_90:
1213 case WL_OUTPUT_TRANSFORM_270:
1214 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1215 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001216 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001217 height = surface->buffer_ref.buffer->width;
1218 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001219 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001220 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001221 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001222 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001223 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001224
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001225 width = width / surface->buffer_viewport.scale;
1226 height = height / surface->buffer_viewport.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001227 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001228}
1229
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001230WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001231weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001232{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001233 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001234
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001235 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001236
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001237 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001238}
1239
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240WL_EXPORT struct weston_view *
1241weston_compositor_pick_view(struct weston_compositor *compositor,
1242 wl_fixed_t x, wl_fixed_t y,
1243 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001244{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 wl_list_for_each(view, &compositor->view_list, link) {
1248 weston_view_from_global_fixed(view, x, y, vx, vy);
1249 if (pixman_region32_contains_point(&view->surface->input,
1250 wl_fixed_to_int(*vx),
1251 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001252 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001253 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001254 }
1255
1256 return NULL;
1257}
1258
1259static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001260weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001261{
Daniel Stone37816df2012-05-16 18:45:18 +01001262 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001263
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001264 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001265 return;
1266
Daniel Stone37816df2012-05-16 18:45:18 +01001267 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001268 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001269}
1270
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001271WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001273{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001274 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276 if (!weston_view_is_mapped(view))
1277 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001278
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 weston_view_damage_below(view);
1280 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001281 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 wl_list_remove(&view->layer_link);
1283 wl_list_init(&view->layer_link);
1284 wl_list_remove(&view->link);
1285 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->output_mask = 0;
1287 weston_surface_assign_output(view->surface);
1288
1289 if (weston_surface_is_mapped(view->surface))
1290 return;
1291
1292 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1293 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001294 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001296 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001297 NULL,
1298 wl_fixed_from_int(0),
1299 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001300 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001301 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001302 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001304
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305WL_EXPORT void
1306weston_surface_unmap(struct weston_surface *surface)
1307{
1308 struct weston_view *view;
1309
1310 wl_list_for_each(view, &surface->views, surface_link)
1311 weston_view_unmap(view);
1312 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001313}
1314
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001315struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001316 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001317 struct wl_list link;
1318};
1319
Jason Ekstranda7af7042013-10-12 22:38:11 -05001320WL_EXPORT void
1321weston_view_destroy(struct weston_view *view)
1322{
1323 wl_signal_emit(&view->destroy_signal, view);
1324
1325 assert(wl_list_empty(&view->geometry.child_list));
1326
1327 if (weston_view_is_mapped(view)) {
1328 weston_view_unmap(view);
1329 weston_compositor_build_view_list(view->surface->compositor);
1330 }
1331
1332 wl_list_remove(&view->link);
1333 wl_list_remove(&view->layer_link);
1334
1335 pixman_region32_fini(&view->clip);
1336 pixman_region32_fini(&view->transform.boundingbox);
1337
1338 weston_view_set_transform_parent(view, NULL);
1339
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 wl_list_remove(&view->surface_link);
1341
1342 free(view);
1343}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001344
1345WL_EXPORT void
1346weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001347{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001348 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001350
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001351 if (--surface->ref_count > 0)
1352 return;
1353
1354 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1355
Pekka Paalanene67858b2013-04-25 13:57:42 +03001356 assert(wl_list_empty(&surface->subsurface_list_pending));
1357 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001358
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1360 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001361
Pekka Paalanenbc106382012-10-10 12:49:31 +03001362 wl_list_for_each_safe(cb, next,
1363 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001364 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001365
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001366 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001367 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001368 pixman_region32_fini(&surface->pending.damage);
1369
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001370 if (surface->pending.buffer)
1371 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1372
Pekka Paalanende685b82012-12-04 15:58:12 +02001373 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001374
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001375 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001376 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001377 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001378
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001379 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001380 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001381
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001382 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001383}
1384
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001385static void
1386destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001387{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001388 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001389
Giulio Camuffo0d379742013-11-15 22:06:15 +01001390 /* Set the resource to NULL, since we don't want to leave a
1391 * dangling pointer if the surface was refcounted and survives
1392 * the weston_surface_destroy() call. */
1393 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001394 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001395}
1396
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001397static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001398weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1399{
1400 struct weston_buffer *buffer =
1401 container_of(listener, struct weston_buffer, destroy_listener);
1402
1403 wl_signal_emit(&buffer->destroy_signal, buffer);
1404 free(buffer);
1405}
1406
1407struct weston_buffer *
1408weston_buffer_from_resource(struct wl_resource *resource)
1409{
1410 struct weston_buffer *buffer;
1411 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001412
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001413 listener = wl_resource_get_destroy_listener(resource,
1414 weston_buffer_destroy_handler);
1415
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001416 if (listener)
1417 return container_of(listener, struct weston_buffer,
1418 destroy_listener);
1419
1420 buffer = zalloc(sizeof *buffer);
1421 if (buffer == NULL)
1422 return NULL;
1423
1424 buffer->resource = resource;
1425 wl_signal_init(&buffer->destroy_signal);
1426 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001427 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001428 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001429
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001430 return buffer;
1431}
1432
1433static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001434weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1435 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001436{
Pekka Paalanende685b82012-12-04 15:58:12 +02001437 struct weston_buffer_reference *ref =
1438 container_of(listener, struct weston_buffer_reference,
1439 destroy_listener);
1440
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001441 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001442 ref->buffer = NULL;
1443}
1444
1445WL_EXPORT void
1446weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001447 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001448{
1449 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001450 ref->buffer->busy_count--;
1451 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001452 assert(wl_resource_get_client(ref->buffer->resource));
1453 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001454 WL_BUFFER_RELEASE);
1455 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001456 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001457 }
1458
Pekka Paalanende685b82012-12-04 15:58:12 +02001459 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001460 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001461 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001462 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001463 }
1464
Pekka Paalanende685b82012-12-04 15:58:12 +02001465 ref->buffer = buffer;
1466 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1467}
1468
1469static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001470weston_surface_attach(struct weston_surface *surface,
1471 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001472{
1473 weston_buffer_reference(&surface->buffer_ref, buffer);
1474
Pekka Paalanena6421c42012-12-04 15:58:10 +02001475 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001476 if (weston_surface_is_mapped(surface))
1477 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001478 }
1479
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001480 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001481
1482 weston_surface_set_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001483}
1484
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001485WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001486weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001487{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001488 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001489
1490 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001491 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001492}
1493
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001494WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001496{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001497 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001498
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001499 pixman_region32_union(&compositor->primary_plane.damage,
1500 &compositor->primary_plane.damage,
1501 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001502 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001503}
1504
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001505static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001506surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001507{
Pekka Paalanende685b82012-12-04 15:58:12 +02001508 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001509 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001510 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001511
Jason Ekstranda7af7042013-10-12 22:38:11 -05001512 empty_region(&surface->damage);
1513}
1514
1515static void
1516view_accumulate_damage(struct weston_view *view,
1517 pixman_region32_t *opaque)
1518{
1519 pixman_region32_t damage;
1520
1521 pixman_region32_init(&damage);
1522 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001523 pixman_box32_t *extents;
1524
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 extents = pixman_region32_extents(&view->surface->damage);
1526 view_compute_bbox(view, extents->x1, extents->y1,
1527 extents->x2 - extents->x1,
1528 extents->y2 - extents->y1,
1529 &damage);
1530 pixman_region32_translate(&damage,
1531 -view->plane->x,
1532 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001533 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 pixman_region32_copy(&damage, &view->surface->damage);
1535 pixman_region32_translate(&damage,
1536 view->geometry.x - view->plane->x,
1537 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001538 }
1539
Jason Ekstranda7af7042013-10-12 22:38:11 -05001540 pixman_region32_subtract(&damage, &damage, opaque);
1541 pixman_region32_union(&view->plane->damage,
1542 &view->plane->damage, &damage);
1543 pixman_region32_fini(&damage);
1544 pixman_region32_copy(&view->clip, opaque);
1545 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001546}
1547
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001548static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001549compositor_accumulate_damage(struct weston_compositor *ec)
1550{
1551 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001553 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001554
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001555 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001556
1557 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001558 pixman_region32_copy(&plane->clip, &clip);
1559
1560 pixman_region32_init(&opaque);
1561
Jason Ekstranda7af7042013-10-12 22:38:11 -05001562 wl_list_for_each(ev, &ec->view_list, link) {
1563 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001564 continue;
1565
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001567 }
1568
1569 pixman_region32_union(&clip, &clip, &opaque);
1570 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001571 }
1572
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001573 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 wl_list_for_each(ev, &ec->view_list, link)
1576 ev->surface->touched = 0;
1577
1578 wl_list_for_each(ev, &ec->view_list, link) {
1579 if (ev->surface->touched)
1580 continue;
1581 ev->surface->touched = 1;
1582
1583 surface_flush_damage(ev->surface);
1584
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001585 /* Both the renderer and the backend have seen the buffer
1586 * by now. If renderer needs the buffer, it has its own
1587 * reference set. If the backend wants to keep the buffer
1588 * around for migrating the surface into a non-primary plane
1589 * later, keep_buffer is true. Otherwise, drop the core
1590 * reference now, and allow early buffer release. This enables
1591 * clients to use single-buffering.
1592 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593 if (!ev->surface->keep_buffer)
1594 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001595 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001596}
1597
1598static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001599surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001600{
1601 struct weston_subsurface *sub;
1602
Pekka Paalanene67858b2013-04-25 13:57:42 +03001603 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001605 continue;
1606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1608 wl_list_init(&sub->surface->views);
1609
1610 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001611 }
1612}
1613
1614static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001616{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 struct weston_subsurface *sub;
1618 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1621 if (sub->surface == surface)
1622 continue;
1623
1624 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1625 weston_view_destroy(view);
1626
1627 surface_free_unused_subsurface_views(sub->surface);
1628 }
1629}
1630
1631static void
1632view_list_add_subsurface_view(struct weston_compositor *compositor,
1633 struct weston_subsurface *sub,
1634 struct weston_view *parent)
1635{
1636 struct weston_subsurface *child;
1637 struct weston_view *view = NULL, *iv;
1638
1639 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1640 if (iv->geometry.parent == parent) {
1641 view = iv;
1642 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001643 }
1644 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645
1646 if (view) {
1647 /* Put it back in the surface's list of views */
1648 wl_list_remove(&view->surface_link);
1649 wl_list_insert(&sub->surface->views, &view->surface_link);
1650 } else {
1651 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001652 weston_view_set_position(view,
1653 sub->position.x,
1654 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 weston_view_set_transform_parent(view, parent);
1656 }
1657
1658 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659
Pekka Paalanenb188e912013-11-19 14:03:35 +02001660 if (wl_list_empty(&sub->surface->subsurface_list)) {
1661 wl_list_insert(compositor->view_list.prev, &view->link);
1662 return;
1663 }
1664
1665 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1666 if (child->surface == sub->surface)
1667 wl_list_insert(compositor->view_list.prev, &view->link);
1668 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001670 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671}
1672
1673static void
1674view_list_add(struct weston_compositor *compositor,
1675 struct weston_view *view)
1676{
1677 struct weston_subsurface *sub;
1678
1679 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680
Pekka Paalanenb188e912013-11-19 14:03:35 +02001681 if (wl_list_empty(&view->surface->subsurface_list)) {
1682 wl_list_insert(compositor->view_list.prev, &view->link);
1683 return;
1684 }
1685
1686 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1687 if (sub->surface == view->surface)
1688 wl_list_insert(compositor->view_list.prev, &view->link);
1689 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001691 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692}
1693
1694static void
1695weston_compositor_build_view_list(struct weston_compositor *compositor)
1696{
1697 struct weston_view *view;
1698 struct weston_layer *layer;
1699
1700 wl_list_for_each(layer, &compositor->layer_list, link)
1701 wl_list_for_each(view, &layer->view_list, layer_link)
1702 surface_stash_subsurface_views(view->surface);
1703
1704 wl_list_init(&compositor->view_list);
1705 wl_list_for_each(layer, &compositor->layer_list, link) {
1706 wl_list_for_each(view, &layer->view_list, layer_link) {
1707 view_list_add(compositor, view);
1708 }
1709 }
1710
1711 wl_list_for_each(layer, &compositor->layer_list, link)
1712 wl_list_for_each(view, &layer->view_list, layer_link)
1713 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001714}
1715
David Herrmann1edf44c2013-10-22 17:11:26 +02001716static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001717weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001718{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001719 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001720 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001721 struct weston_animation *animation, *next;
1722 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001723 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001724 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001725 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001726
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001727 if (output->destroying)
1728 return 0;
1729
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001730 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001731 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001732
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001733 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001734 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001735 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001736 wl_list_for_each(ev, &ec->view_list, link)
1737 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001738
Pekka Paalanene67858b2013-04-25 13:57:42 +03001739 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 wl_list_for_each(ev, &ec->view_list, link) {
1741 /* Note: This operation is safe to do multiple times on the
1742 * same surface.
1743 */
1744 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001745 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746 &ev->surface->frame_callback_list);
1747 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001748 }
1749 }
1750
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001751 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001752
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001753 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001754 pixman_region32_intersect(&output_damage,
1755 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001756 pixman_region32_subtract(&output_damage,
1757 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001758
Scott Moreauccbf29d2012-02-22 14:21:41 -07001759 if (output->dirty)
1760 weston_output_update_matrix(output);
1761
David Herrmann1edf44c2013-10-22 17:11:26 +02001762 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001763
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001764 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001765
Kristian Høgsbergef044142011-06-21 15:02:12 -04001766 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001767
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001768 weston_compositor_repick(ec);
1769 wl_event_loop_dispatch(ec->input_loop, 0);
1770
Jonas Ådahldb773762012-06-13 00:01:21 +02001771 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001772 wl_callback_send_done(cb->resource, msecs);
1773 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001774 }
1775
Scott Moreaud64cf212012-06-08 19:40:54 -06001776 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001777 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001778 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001779 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001780
1781 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001782}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001783
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001784static int
1785weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001786{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001787 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001788
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001789 wl_event_loop_dispatch(compositor->input_loop, 0);
1790
1791 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001792}
1793
1794WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001795weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001796{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001797 struct weston_compositor *compositor = output->compositor;
1798 struct wl_event_loop *loop =
1799 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001800 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001801
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001802 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001803
1804 if (output->repaint_needed &&
1805 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1806 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001807 r = weston_output_repaint(output, msecs);
1808 if (!r)
1809 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001810 }
1811
1812 output->repaint_scheduled = 0;
1813 if (compositor->input_loop_source)
1814 return;
1815
1816 fd = wl_event_loop_get_fd(compositor->input_loop);
1817 compositor->input_loop_source =
1818 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1819 weston_compositor_read_input, compositor);
1820}
1821
1822static void
1823idle_repaint(void *data)
1824{
1825 struct weston_output *output = data;
1826
Jonas Ådahle5a12252013-04-05 23:07:11 +02001827 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001828}
1829
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001830WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001831weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1832{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001834 if (below != NULL)
1835 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001836}
1837
1838WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001839weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001840{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001841 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001842 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001843
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001844 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1845 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001846 return;
1847
Kristian Høgsbergef044142011-06-21 15:02:12 -04001848 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001849 output->repaint_needed = 1;
1850 if (output->repaint_scheduled)
1851 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001852
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001853 wl_event_loop_add_idle(loop, idle_repaint, output);
1854 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001855
1856 if (compositor->input_loop_source) {
1857 wl_event_source_remove(compositor->input_loop_source);
1858 compositor->input_loop_source = NULL;
1859 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001860}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001861
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001862WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001863weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1864{
1865 struct weston_output *output;
1866
1867 wl_list_for_each(output, &compositor->output_list, link)
1868 weston_output_schedule_repaint(output);
1869}
1870
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001871static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001872surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001873{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001874 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001875}
1876
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001877static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001878surface_attach(struct wl_client *client,
1879 struct wl_resource *resource,
1880 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1881{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001882 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001883 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001884
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001885 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001886 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001887 if (buffer == NULL) {
1888 wl_client_post_no_memory(client);
1889 return;
1890 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001891 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001892
Pekka Paalanende685b82012-12-04 15:58:12 +02001893 /* Attach, attach, without commit in between does not send
1894 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001895 if (surface->pending.buffer)
1896 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001897
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001898 surface->pending.sx = sx;
1899 surface->pending.sy = sy;
1900 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001901 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001902 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001903 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001904 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001905 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001906}
1907
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001908static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001909surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001910 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001911 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001912{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001913 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001914
Pekka Paalanen8e159182012-10-10 12:49:25 +03001915 pixman_region32_union_rect(&surface->pending.damage,
1916 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001917 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001918}
1919
Kristian Høgsberg33418202011-08-16 23:01:28 -04001920static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001921destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001922{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001923 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001924
1925 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001926 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001927}
1928
1929static void
1930surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001931 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001932{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001933 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001934 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001935
1936 cb = malloc(sizeof *cb);
1937 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001938 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001939 return;
1940 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001941
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001942 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1943 callback);
1944 if (cb->resource == NULL) {
1945 free(cb);
1946 wl_resource_post_no_memory(resource);
1947 return;
1948 }
1949
Jason Ekstranda85118c2013-06-27 20:17:02 -05001950 wl_resource_set_implementation(cb->resource, NULL, cb,
1951 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001952
Pekka Paalanenbc106382012-10-10 12:49:31 +03001953 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001954}
1955
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001956static void
1957surface_set_opaque_region(struct wl_client *client,
1958 struct wl_resource *resource,
1959 struct wl_resource *region_resource)
1960{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001961 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001962 struct weston_region *region;
1963
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001964 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001965 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001966 pixman_region32_copy(&surface->pending.opaque,
1967 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001968 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001969 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001970 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001971}
1972
1973static void
1974surface_set_input_region(struct wl_client *client,
1975 struct wl_resource *resource,
1976 struct wl_resource *region_resource)
1977{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001978 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001979 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001980
1981 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001982 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001983 pixman_region32_copy(&surface->pending.input,
1984 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001985 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001986 pixman_region32_fini(&surface->pending.input);
1987 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001988 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001989}
1990
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001991static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001992weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001993{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001994 struct weston_subsurface *sub;
1995
1996 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1997 parent_link_pending) {
1998 wl_list_remove(&sub->parent_link);
1999 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2000 }
2001}
2002
2003static void
2004weston_surface_commit(struct weston_surface *surface)
2005{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002007 pixman_region32_t opaque;
2008
Alexander Larsson4ea95522013-05-22 14:41:37 +02002009 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002010 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002011 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002012 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002013
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002014 /* wl_surface.attach */
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002015 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002016 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002017
2018 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002019 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002020 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002021
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002022 if (surface->pending.buffer)
2023 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2024 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002025 surface->pending.sx = 0;
2026 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002027 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002028
2029 /* wl_surface.damage */
2030 pixman_region32_union(&surface->damage, &surface->damage,
2031 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002032 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2033 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 surface->width,
2035 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002036 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002037
2038 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002039 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002040 surface->width,
2041 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002042 pixman_region32_intersect(&opaque,
2043 &opaque, &surface->pending.opaque);
2044
2045 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2046 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002047 wl_list_for_each(view, &surface->views, surface_link)
2048 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002049 }
2050
2051 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002052
2053 /* wl_surface.set_input_region */
2054 pixman_region32_fini(&surface->input);
2055 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002056 surface->width,
2057 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002058 pixman_region32_intersect(&surface->input,
2059 &surface->input, &surface->pending.input);
2060
Pekka Paalanenbc106382012-10-10 12:49:31 +03002061 /* wl_surface.frame */
2062 wl_list_insert_list(&surface->frame_callback_list,
2063 &surface->pending.frame_callback_list);
2064 wl_list_init(&surface->pending.frame_callback_list);
2065
Pekka Paalanene67858b2013-04-25 13:57:42 +03002066 weston_surface_commit_subsurface_order(surface);
2067
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002068 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002069}
2070
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002071static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072weston_subsurface_commit(struct weston_subsurface *sub);
2073
2074static void
2075weston_subsurface_parent_commit(struct weston_subsurface *sub,
2076 int parent_is_synchronized);
2077
2078static void
2079surface_commit(struct wl_client *client, struct wl_resource *resource)
2080{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002081 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2083
2084 if (sub) {
2085 weston_subsurface_commit(sub);
2086 return;
2087 }
2088
2089 weston_surface_commit(surface);
2090
2091 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2092 if (sub->surface != surface)
2093 weston_subsurface_parent_commit(sub, 0);
2094 }
2095}
2096
2097static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002098surface_set_buffer_transform(struct wl_client *client,
2099 struct wl_resource *resource, int transform)
2100{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002101 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002102
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002103 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002104}
2105
Alexander Larsson4ea95522013-05-22 14:41:37 +02002106static void
2107surface_set_buffer_scale(struct wl_client *client,
2108 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002109 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002110{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002111 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002112
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002113 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002114}
2115
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002116static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002117 surface_destroy,
2118 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002119 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002120 surface_frame,
2121 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002122 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002123 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002124 surface_set_buffer_transform,
2125 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002126};
2127
2128static void
2129compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002130 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002131{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002132 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002133 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002134
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002135 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002136 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002137 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002138 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002139 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002140
Jason Ekstranda85118c2013-06-27 20:17:02 -05002141 surface->resource =
2142 wl_resource_create(client, &wl_surface_interface,
2143 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002144 if (surface->resource == NULL) {
2145 weston_surface_destroy(surface);
2146 wl_resource_post_no_memory(resource);
2147 return;
2148 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002149 wl_resource_set_implementation(surface->resource, &surface_interface,
2150 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002151}
2152
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002153static void
2154destroy_region(struct wl_resource *resource)
2155{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002156 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002157
2158 pixman_region32_fini(&region->region);
2159 free(region);
2160}
2161
2162static void
2163region_destroy(struct wl_client *client, struct wl_resource *resource)
2164{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002165 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002166}
2167
2168static void
2169region_add(struct wl_client *client, struct wl_resource *resource,
2170 int32_t x, int32_t y, int32_t width, int32_t height)
2171{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002172 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002173
2174 pixman_region32_union_rect(&region->region, &region->region,
2175 x, y, width, height);
2176}
2177
2178static void
2179region_subtract(struct wl_client *client, struct wl_resource *resource,
2180 int32_t x, int32_t y, int32_t width, int32_t height)
2181{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002182 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002183 pixman_region32_t rect;
2184
2185 pixman_region32_init_rect(&rect, x, y, width, height);
2186 pixman_region32_subtract(&region->region, &region->region, &rect);
2187 pixman_region32_fini(&rect);
2188}
2189
2190static const struct wl_region_interface region_interface = {
2191 region_destroy,
2192 region_add,
2193 region_subtract
2194};
2195
2196static void
2197compositor_create_region(struct wl_client *client,
2198 struct wl_resource *resource, uint32_t id)
2199{
2200 struct weston_region *region;
2201
2202 region = malloc(sizeof *region);
2203 if (region == NULL) {
2204 wl_resource_post_no_memory(resource);
2205 return;
2206 }
2207
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002208 pixman_region32_init(&region->region);
2209
Jason Ekstranda85118c2013-06-27 20:17:02 -05002210 region->resource =
2211 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002212 if (region->resource == NULL) {
2213 free(region);
2214 wl_resource_post_no_memory(resource);
2215 return;
2216 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002217 wl_resource_set_implementation(region->resource, &region_interface,
2218 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002219}
2220
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002221static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002222 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002223 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002224};
2225
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002226static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002227weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2228{
2229 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002230 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002232
Alexander Larsson4ea95522013-05-22 14:41:37 +02002233 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002234 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002235 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002236 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002237
Pekka Paalanene67858b2013-04-25 13:57:42 +03002238 /* wl_surface.attach */
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02002239 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002240 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2241 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2242
Pekka Paalanene67858b2013-04-25 13:57:42 +03002243 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002244 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245 sub->cached.sx = 0;
2246 sub->cached.sy = 0;
2247 sub->cached.newly_attached = 0;
2248
2249 /* wl_surface.damage */
2250 pixman_region32_union(&surface->damage, &surface->damage,
2251 &sub->cached.damage);
2252 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2253 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002254 surface->width,
2255 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002256 empty_region(&sub->cached.damage);
2257
2258 /* wl_surface.set_opaque_region */
2259 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002260 surface->width,
2261 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002262 pixman_region32_intersect(&opaque,
2263 &opaque, &sub->cached.opaque);
2264
2265 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2266 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002267 wl_list_for_each(view, &surface->views, surface_link)
2268 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002269 }
2270
2271 pixman_region32_fini(&opaque);
2272
2273 /* wl_surface.set_input_region */
2274 pixman_region32_fini(&surface->input);
2275 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276 surface->width,
2277 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002278 pixman_region32_intersect(&surface->input,
2279 &surface->input, &sub->cached.input);
2280
2281 /* wl_surface.frame */
2282 wl_list_insert_list(&surface->frame_callback_list,
2283 &sub->cached.frame_callback_list);
2284 wl_list_init(&sub->cached.frame_callback_list);
2285
2286 weston_surface_commit_subsurface_order(surface);
2287
2288 weston_surface_schedule_repaint(surface);
2289
2290 sub->cached.has_data = 0;
2291}
2292
2293static void
2294weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2295{
2296 struct weston_surface *surface = sub->surface;
2297
2298 /*
2299 * If this commit would cause the surface to move by the
2300 * attach(dx, dy) parameters, the old damage region must be
2301 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002302 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 */
2304 pixman_region32_translate(&sub->cached.damage,
2305 -surface->pending.sx, -surface->pending.sy);
2306 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2307 &surface->pending.damage);
2308 empty_region(&surface->pending.damage);
2309
2310 if (surface->pending.newly_attached) {
2311 sub->cached.newly_attached = 1;
2312 weston_buffer_reference(&sub->cached.buffer_ref,
2313 surface->pending.buffer);
2314 }
2315 sub->cached.sx += surface->pending.sx;
2316 sub->cached.sy += surface->pending.sy;
2317 surface->pending.sx = 0;
2318 surface->pending.sy = 0;
2319 surface->pending.newly_attached = 0;
2320
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002321 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002322
2323 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2324
2325 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2326
2327 wl_list_insert_list(&sub->cached.frame_callback_list,
2328 &surface->pending.frame_callback_list);
2329 wl_list_init(&surface->pending.frame_callback_list);
2330
2331 sub->cached.has_data = 1;
2332}
2333
2334static int
2335weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2336{
2337 while (sub) {
2338 if (sub->synchronized)
2339 return 1;
2340
2341 if (!sub->parent)
2342 return 0;
2343
2344 sub = weston_surface_to_subsurface(sub->parent);
2345 }
2346
2347 return 0;
2348}
2349
2350static void
2351weston_subsurface_commit(struct weston_subsurface *sub)
2352{
2353 struct weston_surface *surface = sub->surface;
2354 struct weston_subsurface *tmp;
2355
2356 /* Recursive check for effectively synchronized. */
2357 if (weston_subsurface_is_synchronized(sub)) {
2358 weston_subsurface_commit_to_cache(sub);
2359 } else {
2360 if (sub->cached.has_data) {
2361 /* flush accumulated state from cache */
2362 weston_subsurface_commit_to_cache(sub);
2363 weston_subsurface_commit_from_cache(sub);
2364 } else {
2365 weston_surface_commit(surface);
2366 }
2367
2368 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2369 if (tmp->surface != surface)
2370 weston_subsurface_parent_commit(tmp, 0);
2371 }
2372 }
2373}
2374
2375static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002376weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002377{
2378 struct weston_surface *surface = sub->surface;
2379 struct weston_subsurface *tmp;
2380
Pekka Paalanene67858b2013-04-25 13:57:42 +03002381 /* From now on, commit_from_cache the whole sub-tree, regardless of
2382 * the synchronized mode of each child. This sub-surface or some
2383 * of its ancestors were synchronized, so we are synchronized
2384 * all the way down.
2385 */
2386
2387 if (sub->cached.has_data)
2388 weston_subsurface_commit_from_cache(sub);
2389
2390 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2391 if (tmp->surface != surface)
2392 weston_subsurface_parent_commit(tmp, 1);
2393 }
2394}
2395
2396static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002397weston_subsurface_parent_commit(struct weston_subsurface *sub,
2398 int parent_is_synchronized)
2399{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002400 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002401 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002402 wl_list_for_each(view, &sub->surface->views, surface_link)
2403 weston_view_set_position(view,
2404 sub->position.x,
2405 sub->position.y);
2406
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002407 sub->position.set = 0;
2408 }
2409
2410 if (parent_is_synchronized || sub->synchronized)
2411 weston_subsurface_synchronized_commit(sub);
2412}
2413
2414static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002415subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002416{
2417 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002418 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002419
Jason Ekstranda7af7042013-10-12 22:38:11 -05002420 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002421 weston_view_set_position(view,
2422 view->geometry.x + dx,
2423 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002424
2425 /* No need to check parent mappedness, because if parent is not
2426 * mapped, parent is not in a visible layer, so this sub-surface
2427 * will not be drawn either.
2428 */
2429 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002430 /* Cannot call weston_surface_update_transform(),
2431 * because that would call it also for the parent surface,
2432 * which might not be mapped yet. That would lead to
2433 * inconsistent state, where the window could never be
2434 * mapped.
2435 *
2436 * Instead just assing any output, to make
2437 * weston_surface_is_mapped() return true, so that when the
2438 * parent surface does get mapped, this one will get
2439 * included, too. See surface_list_add().
2440 */
2441 assert(!wl_list_empty(&compositor->output_list));
2442 surface->output = container_of(compositor->output_list.next,
2443 struct weston_output, link);
2444 }
2445}
2446
2447static struct weston_subsurface *
2448weston_surface_to_subsurface(struct weston_surface *surface)
2449{
2450 if (surface->configure == subsurface_configure)
2451 return surface->configure_private;
2452
2453 return NULL;
2454}
2455
Pekka Paalanen01388e22013-04-25 13:57:44 +03002456WL_EXPORT struct weston_surface *
2457weston_surface_get_main_surface(struct weston_surface *surface)
2458{
2459 struct weston_subsurface *sub;
2460
2461 while (surface && (sub = weston_surface_to_subsurface(surface)))
2462 surface = sub->parent;
2463
2464 return surface;
2465}
2466
Pekka Paalanene67858b2013-04-25 13:57:42 +03002467static void
2468subsurface_set_position(struct wl_client *client,
2469 struct wl_resource *resource, int32_t x, int32_t y)
2470{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002471 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002472
2473 if (!sub)
2474 return;
2475
2476 sub->position.x = x;
2477 sub->position.y = y;
2478 sub->position.set = 1;
2479}
2480
2481static struct weston_subsurface *
2482subsurface_from_surface(struct weston_surface *surface)
2483{
2484 struct weston_subsurface *sub;
2485
2486 sub = weston_surface_to_subsurface(surface);
2487 if (sub)
2488 return sub;
2489
2490 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2491 if (sub->surface == surface)
2492 return sub;
2493
2494 return NULL;
2495}
2496
2497static struct weston_subsurface *
2498subsurface_sibling_check(struct weston_subsurface *sub,
2499 struct weston_surface *surface,
2500 const char *request)
2501{
2502 struct weston_subsurface *sibling;
2503
2504 sibling = subsurface_from_surface(surface);
2505
2506 if (!sibling) {
2507 wl_resource_post_error(sub->resource,
2508 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2509 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002510 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002511 return NULL;
2512 }
2513
2514 if (sibling->parent != sub->parent) {
2515 wl_resource_post_error(sub->resource,
2516 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2517 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002518 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002519 return NULL;
2520 }
2521
2522 return sibling;
2523}
2524
2525static void
2526subsurface_place_above(struct wl_client *client,
2527 struct wl_resource *resource,
2528 struct wl_resource *sibling_resource)
2529{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002530 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002531 struct weston_surface *surface =
2532 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002533 struct weston_subsurface *sibling;
2534
2535 if (!sub)
2536 return;
2537
2538 sibling = subsurface_sibling_check(sub, surface, "place_above");
2539 if (!sibling)
2540 return;
2541
2542 wl_list_remove(&sub->parent_link_pending);
2543 wl_list_insert(sibling->parent_link_pending.prev,
2544 &sub->parent_link_pending);
2545}
2546
2547static void
2548subsurface_place_below(struct wl_client *client,
2549 struct wl_resource *resource,
2550 struct wl_resource *sibling_resource)
2551{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002552 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002553 struct weston_surface *surface =
2554 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002555 struct weston_subsurface *sibling;
2556
2557 if (!sub)
2558 return;
2559
2560 sibling = subsurface_sibling_check(sub, surface, "place_below");
2561 if (!sibling)
2562 return;
2563
2564 wl_list_remove(&sub->parent_link_pending);
2565 wl_list_insert(&sibling->parent_link_pending,
2566 &sub->parent_link_pending);
2567}
2568
2569static void
2570subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2571{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002572 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002573
2574 if (sub)
2575 sub->synchronized = 1;
2576}
2577
2578static void
2579subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2580{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002581 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002582
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002583 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002584 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002585
2586 /* If sub became effectively desynchronized, flush. */
2587 if (!weston_subsurface_is_synchronized(sub))
2588 weston_subsurface_synchronized_commit(sub);
2589 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002590}
2591
2592static void
2593weston_subsurface_cache_init(struct weston_subsurface *sub)
2594{
2595 pixman_region32_init(&sub->cached.damage);
2596 pixman_region32_init(&sub->cached.opaque);
2597 pixman_region32_init(&sub->cached.input);
2598 wl_list_init(&sub->cached.frame_callback_list);
2599 sub->cached.buffer_ref.buffer = NULL;
2600}
2601
2602static void
2603weston_subsurface_cache_fini(struct weston_subsurface *sub)
2604{
2605 struct weston_frame_callback *cb, *tmp;
2606
2607 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002608 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002609
2610 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2611 pixman_region32_fini(&sub->cached.damage);
2612 pixman_region32_fini(&sub->cached.opaque);
2613 pixman_region32_fini(&sub->cached.input);
2614}
2615
2616static void
2617weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2618{
2619 wl_list_remove(&sub->parent_link);
2620 wl_list_remove(&sub->parent_link_pending);
2621 wl_list_remove(&sub->parent_destroy_listener.link);
2622 sub->parent = NULL;
2623}
2624
2625static void
2626weston_subsurface_destroy(struct weston_subsurface *sub);
2627
2628static void
2629subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2630{
2631 struct weston_subsurface *sub =
2632 container_of(listener, struct weston_subsurface,
2633 surface_destroy_listener);
2634 assert(data == &sub->surface->resource);
2635
2636 /* The protocol object (wl_resource) is left inert. */
2637 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002638 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002639
2640 weston_subsurface_destroy(sub);
2641}
2642
2643static void
2644subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2645{
2646 struct weston_subsurface *sub =
2647 container_of(listener, struct weston_subsurface,
2648 parent_destroy_listener);
2649 assert(data == &sub->parent->resource);
2650 assert(sub->surface != sub->parent);
2651
2652 if (weston_surface_is_mapped(sub->surface))
2653 weston_surface_unmap(sub->surface);
2654
2655 weston_subsurface_unlink_parent(sub);
2656}
2657
2658static void
2659subsurface_resource_destroy(struct wl_resource *resource)
2660{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002661 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662
2663 if (sub)
2664 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665}
2666
2667static void
2668subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2669{
2670 wl_resource_destroy(resource);
2671}
2672
2673static void
2674weston_subsurface_link_parent(struct weston_subsurface *sub,
2675 struct weston_surface *parent)
2676{
2677 sub->parent = parent;
2678 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002679 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002680 &sub->parent_destroy_listener);
2681
2682 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2683 wl_list_insert(&parent->subsurface_list_pending,
2684 &sub->parent_link_pending);
2685}
2686
2687static void
2688weston_subsurface_link_surface(struct weston_subsurface *sub,
2689 struct weston_surface *surface)
2690{
2691 sub->surface = surface;
2692 sub->surface_destroy_listener.notify =
2693 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002694 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002695 &sub->surface_destroy_listener);
2696}
2697
2698static void
2699weston_subsurface_destroy(struct weston_subsurface *sub)
2700{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002701 struct weston_view *view, *next;
2702
Pekka Paalanene67858b2013-04-25 13:57:42 +03002703 assert(sub->surface);
2704
2705 if (sub->resource) {
2706 assert(weston_surface_to_subsurface(sub->surface) == sub);
2707 assert(sub->parent_destroy_listener.notify ==
2708 subsurface_handle_parent_destroy);
2709
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2711 weston_view_destroy(view);
2712
Pekka Paalanene67858b2013-04-25 13:57:42 +03002713 if (sub->parent)
2714 weston_subsurface_unlink_parent(sub);
2715
2716 weston_subsurface_cache_fini(sub);
2717
2718 sub->surface->configure = NULL;
2719 sub->surface->configure_private = NULL;
2720 } else {
2721 /* the dummy weston_subsurface for the parent itself */
2722 assert(sub->parent_destroy_listener.notify == NULL);
2723 wl_list_remove(&sub->parent_link);
2724 wl_list_remove(&sub->parent_link_pending);
2725 }
2726
2727 wl_list_remove(&sub->surface_destroy_listener.link);
2728 free(sub);
2729}
2730
2731static const struct wl_subsurface_interface subsurface_implementation = {
2732 subsurface_destroy,
2733 subsurface_set_position,
2734 subsurface_place_above,
2735 subsurface_place_below,
2736 subsurface_set_sync,
2737 subsurface_set_desync
2738};
2739
2740static struct weston_subsurface *
2741weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2742 struct weston_surface *parent)
2743{
2744 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002745 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002746
2747 sub = calloc(1, sizeof *sub);
2748 if (!sub)
2749 return NULL;
2750
Jason Ekstranda7af7042013-10-12 22:38:11 -05002751 wl_list_init(&sub->unused_views);
2752
Jason Ekstranda85118c2013-06-27 20:17:02 -05002753 sub->resource =
2754 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002755 if (!sub->resource) {
2756 free(sub);
2757 return NULL;
2758 }
2759
Jason Ekstranda85118c2013-06-27 20:17:02 -05002760 wl_resource_set_implementation(sub->resource,
2761 &subsurface_implementation,
2762 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002763 weston_subsurface_link_surface(sub, surface);
2764 weston_subsurface_link_parent(sub, parent);
2765 weston_subsurface_cache_init(sub);
2766 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767
2768 return sub;
2769}
2770
2771/* Create a dummy subsurface for having the parent itself in its
2772 * sub-surface lists. Makes stacking order manipulation easy.
2773 */
2774static struct weston_subsurface *
2775weston_subsurface_create_for_parent(struct weston_surface *parent)
2776{
2777 struct weston_subsurface *sub;
2778
2779 sub = calloc(1, sizeof *sub);
2780 if (!sub)
2781 return NULL;
2782
2783 weston_subsurface_link_surface(sub, parent);
2784 sub->parent = parent;
2785 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2786 wl_list_insert(&parent->subsurface_list_pending,
2787 &sub->parent_link_pending);
2788
2789 return sub;
2790}
2791
2792static void
2793subcompositor_get_subsurface(struct wl_client *client,
2794 struct wl_resource *resource,
2795 uint32_t id,
2796 struct wl_resource *surface_resource,
2797 struct wl_resource *parent_resource)
2798{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002799 struct weston_surface *surface =
2800 wl_resource_get_user_data(surface_resource);
2801 struct weston_surface *parent =
2802 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002803 struct weston_subsurface *sub;
2804 static const char where[] = "get_subsurface: wl_subsurface@";
2805
2806 if (surface == parent) {
2807 wl_resource_post_error(resource,
2808 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2809 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002810 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002811 return;
2812 }
2813
2814 if (weston_surface_to_subsurface(surface)) {
2815 wl_resource_post_error(resource,
2816 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2817 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002818 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819 return;
2820 }
2821
2822 if (surface->configure) {
2823 wl_resource_post_error(resource,
2824 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2825 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002826 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002827 return;
2828 }
2829
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002830 if (weston_surface_get_main_surface(parent) == surface) {
2831 wl_resource_post_error(resource,
2832 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2833 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002834 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002835 return;
2836 }
2837
Pekka Paalanene67858b2013-04-25 13:57:42 +03002838 /* make sure the parent is in its own list */
2839 if (wl_list_empty(&parent->subsurface_list)) {
2840 if (!weston_subsurface_create_for_parent(parent)) {
2841 wl_resource_post_no_memory(resource);
2842 return;
2843 }
2844 }
2845
2846 sub = weston_subsurface_create(id, surface, parent);
2847 if (!sub) {
2848 wl_resource_post_no_memory(resource);
2849 return;
2850 }
2851
2852 surface->configure = subsurface_configure;
2853 surface->configure_private = sub;
2854}
2855
2856static void
2857subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2858{
2859 wl_resource_destroy(resource);
2860}
2861
2862static const struct wl_subcompositor_interface subcompositor_interface = {
2863 subcompositor_destroy,
2864 subcompositor_get_subsurface
2865};
2866
2867static void
2868bind_subcompositor(struct wl_client *client,
2869 void *data, uint32_t version, uint32_t id)
2870{
2871 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002872 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002873
Jason Ekstranda85118c2013-06-27 20:17:02 -05002874 resource =
2875 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002876 if (resource == NULL) {
2877 wl_client_post_no_memory(client);
2878 return;
2879 }
2880 wl_resource_set_implementation(resource, &subcompositor_interface,
2881 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002882}
2883
2884static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002885weston_compositor_dpms(struct weston_compositor *compositor,
2886 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002887{
2888 struct weston_output *output;
2889
2890 wl_list_for_each(output, &compositor->output_list, link)
2891 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002892 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002893}
2894
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002895WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002896weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002897{
Neil Roberts8b62e202013-09-30 13:14:47 +01002898 uint32_t old_state = compositor->state;
2899
2900 /* The state needs to be changed before emitting the wake
2901 * signal because that may try to schedule a repaint which
2902 * will not work if the compositor is still sleeping */
2903 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2904
2905 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002906 case WESTON_COMPOSITOR_SLEEPING:
2907 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2908 /* fall through */
2909 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002910 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002911 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002912 /* fall through */
2913 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002914 wl_event_source_timer_update(compositor->idle_source,
2915 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002916 }
2917}
2918
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002919WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002920weston_compositor_offscreen(struct weston_compositor *compositor)
2921{
2922 switch (compositor->state) {
2923 case WESTON_COMPOSITOR_OFFSCREEN:
2924 return;
2925 case WESTON_COMPOSITOR_SLEEPING:
2926 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2927 /* fall through */
2928 default:
2929 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2930 wl_event_source_timer_update(compositor->idle_source, 0);
2931 }
2932}
2933
2934WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002935weston_compositor_sleep(struct weston_compositor *compositor)
2936{
2937 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2938 return;
2939
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002940 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002941 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2942 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2943}
2944
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002945static int
2946idle_handler(void *data)
2947{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002948 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002949
2950 if (compositor->idle_inhibit)
2951 return 1;
2952
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002953 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002954 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002955
2956 return 1;
2957}
2958
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002959WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002960weston_plane_init(struct weston_plane *plane,
2961 struct weston_compositor *ec,
2962 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002963{
2964 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002965 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002966 plane->x = x;
2967 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002968 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002969
2970 /* Init the link so that the call to wl_list_remove() when releasing
2971 * the plane without ever stacking doesn't lead to a crash */
2972 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002973}
2974
2975WL_EXPORT void
2976weston_plane_release(struct weston_plane *plane)
2977{
Xiong Zhang97116532013-10-23 13:58:31 +08002978 struct weston_view *view;
2979
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002980 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002981 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002982
Xiong Zhang97116532013-10-23 13:58:31 +08002983 wl_list_for_each(view, &plane->compositor->view_list, link) {
2984 if (view->plane == plane)
2985 view->plane = NULL;
2986 }
2987
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002988 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002989}
2990
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002991WL_EXPORT void
2992weston_compositor_stack_plane(struct weston_compositor *ec,
2993 struct weston_plane *plane,
2994 struct weston_plane *above)
2995{
2996 if (above)
2997 wl_list_insert(above->link.prev, &plane->link);
2998 else
2999 wl_list_insert(&ec->plane_list, &plane->link);
3000}
3001
Casey Dahlin9074db52012-04-19 22:50:09 -04003002static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003003{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003004 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003005}
3006
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003007static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003008bind_output(struct wl_client *client,
3009 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003010{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003011 struct weston_output *output = data;
3012 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003013 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003014
Jason Ekstranda85118c2013-06-27 20:17:02 -05003015 resource = wl_resource_create(client, &wl_output_interface,
3016 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003017 if (resource == NULL) {
3018 wl_client_post_no_memory(client);
3019 return;
3020 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003021
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003022 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003023 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003024
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003025 wl_output_send_geometry(resource,
3026 output->x,
3027 output->y,
3028 output->mm_width,
3029 output->mm_height,
3030 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003031 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003032 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003033 if (version >= 2)
3034 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003035 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003036
3037 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003038 wl_output_send_mode(resource,
3039 mode->flags,
3040 mode->width,
3041 mode->height,
3042 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003043 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003044
3045 if (version >= 2)
3046 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003047}
3048
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003049/* Move other outputs when one is removed so the space remains contiguos. */
3050static void
3051weston_compositor_remove_output(struct weston_compositor *compositor,
3052 struct weston_output *remove_output)
3053{
3054 struct weston_output *output;
3055 int offset = 0;
3056
3057 wl_list_for_each(output, &compositor->output_list, link) {
3058 if (output == remove_output) {
3059 offset = output->width;
3060 continue;
3061 }
3062
3063 if (offset > 0) {
3064 weston_output_move(output,
3065 output->x - offset, output->y);
3066 output->dirty = 1;
3067 }
3068 }
3069}
3070
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003071WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003072weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003073{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003074 output->destroying = 1;
3075
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003076 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003077 wl_list_remove(&output->link);
3078
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003079 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003080 wl_signal_emit(&output->destroy_signal, output);
3081
Richard Hughesafe690c2013-05-02 10:10:04 +01003082 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003083 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003084 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003085 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003086
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003087 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003088}
3089
Scott Moreau1bad5db2012-08-18 01:04:05 -06003090static void
3091weston_output_compute_transform(struct weston_output *output)
3092{
3093 struct weston_matrix transform;
3094 int flip;
3095
3096 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003097 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003098
3099 switch(output->transform) {
3100 case WL_OUTPUT_TRANSFORM_FLIPPED:
3101 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3102 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3103 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003104 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003105 flip = -1;
3106 break;
3107 default:
3108 flip = 1;
3109 break;
3110 }
3111
3112 switch(output->transform) {
3113 case WL_OUTPUT_TRANSFORM_NORMAL:
3114 case WL_OUTPUT_TRANSFORM_FLIPPED:
3115 transform.d[0] = flip;
3116 transform.d[1] = 0;
3117 transform.d[4] = 0;
3118 transform.d[5] = 1;
3119 break;
3120 case WL_OUTPUT_TRANSFORM_90:
3121 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3122 transform.d[0] = 0;
3123 transform.d[1] = -flip;
3124 transform.d[4] = 1;
3125 transform.d[5] = 0;
3126 break;
3127 case WL_OUTPUT_TRANSFORM_180:
3128 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3129 transform.d[0] = -flip;
3130 transform.d[1] = 0;
3131 transform.d[4] = 0;
3132 transform.d[5] = -1;
3133 break;
3134 case WL_OUTPUT_TRANSFORM_270:
3135 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3136 transform.d[0] = 0;
3137 transform.d[1] = flip;
3138 transform.d[4] = -1;
3139 transform.d[5] = 0;
3140 break;
3141 default:
3142 break;
3143 }
3144
3145 weston_matrix_multiply(&output->matrix, &transform);
3146}
3147
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003148WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003149weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003150{
Scott Moreau850ca422012-05-21 15:21:25 -06003151 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003152 struct weston_matrix camera;
3153 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003154
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003155 weston_matrix_init(&output->matrix);
3156 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003157 -(output->x + output->width / 2.0),
3158 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003159
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003160 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003161 2.0 / output->width,
3162 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003163
3164 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003165
Scott Moreauccbf29d2012-02-22 14:21:41 -07003166 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003167 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003168 weston_matrix_init(&camera);
3169 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003170 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003171 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003172 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003173 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003174 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003175 weston_matrix_multiply(&output->matrix, &modelview);
3176 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003177
Scott Moreauccbf29d2012-02-22 14:21:41 -07003178 output->dirty = 0;
3179}
3180
Scott Moreau1bad5db2012-08-18 01:04:05 -06003181static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003182weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003183{
3184 output->transform = transform;
3185
3186 switch (transform) {
3187 case WL_OUTPUT_TRANSFORM_90:
3188 case WL_OUTPUT_TRANSFORM_270:
3189 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3190 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3191 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003192 output->width = output->current_mode->height;
3193 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003194 break;
3195 case WL_OUTPUT_TRANSFORM_NORMAL:
3196 case WL_OUTPUT_TRANSFORM_180:
3197 case WL_OUTPUT_TRANSFORM_FLIPPED:
3198 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003199 output->width = output->current_mode->width;
3200 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003201 break;
3202 default:
3203 break;
3204 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003205
Hardening57388e42013-09-18 23:56:36 +02003206 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003207 output->width /= scale;
3208 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003209}
3210
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003211static void
3212weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003213{
3214 output->x = x;
3215 output->y = y;
3216
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003217 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003218 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003219 output->width,
3220 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003221}
3222
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003223WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003224weston_output_move(struct weston_output *output, int x, int y)
3225{
3226 pixman_region32_t old_region;
3227 struct wl_resource *resource;
3228
3229 output->move_x = x - output->x;
3230 output->move_y = y - output->y;
3231
3232 if (output->move_x == 0 && output->move_y == 0)
3233 return;
3234
3235 pixman_region32_init(&old_region);
3236 pixman_region32_copy(&old_region, &output->region);
3237
3238 weston_output_init_geometry(output, x, y);
3239
3240 output->dirty = 1;
3241
3242 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003243 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003244
3245 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003246 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003247 wl_output_send_geometry(resource,
3248 output->x,
3249 output->y,
3250 output->mm_width,
3251 output->mm_height,
3252 output->subpixel,
3253 output->make,
3254 output->model,
3255 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003256
3257 if (wl_resource_get_version(resource) >= 2)
3258 wl_output_send_done(resource);
3259 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003260}
3261
3262WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003263weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003264 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003265 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003266{
3267 output->compositor = c;
3268 output->x = x;
3269 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003270 output->mm_width = mm_width;
3271 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003272 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003273 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003274
Alexander Larsson0b135062013-05-28 16:23:36 +02003275 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003276 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003277
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003278 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003279 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003280
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003281 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003282 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003283 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003284 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003285
Casey Dahlin58ba1372012-04-19 22:50:08 -04003286 output->id = ffs(~output->compositor->output_id_pool) - 1;
3287 output->compositor->output_id_pool |= 1 << output->id;
3288
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003289 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003290 wl_global_create(c->wl_display, &wl_output_interface, 2,
3291 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003292 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003293}
3294
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003295WL_EXPORT void
3296weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003297 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003298 wl_fixed_t *x, wl_fixed_t *y)
3299{
3300 wl_fixed_t tx, ty;
3301 wl_fixed_t width, height;
3302
Hardeningff39efa2013-09-18 23:56:35 +02003303 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3304 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003305
3306 switch(output->transform) {
3307 case WL_OUTPUT_TRANSFORM_NORMAL:
3308 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003309 tx = device_x;
3310 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003311 break;
3312 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003313 tx = device_y;
3314 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003315 break;
3316 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003317 tx = width - device_x;
3318 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003319 break;
3320 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003321 tx = width - device_y;
3322 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003323 break;
3324 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003325 tx = width - device_x;
3326 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003327 break;
3328 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003329 tx = width - device_y;
3330 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003331 break;
3332 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003333 tx = device_x;
3334 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003335 break;
3336 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003337 tx = device_y;
3338 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003339 break;
3340 }
3341
Hardeningff39efa2013-09-18 23:56:35 +02003342 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3343 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003344}
3345
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003346static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003347destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003348{
Jonny Lamb74130762013-11-26 18:19:46 +01003349 struct weston_surface *surface =
3350 wl_resource_get_user_data(resource);
3351
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003352 surface->viewport_resource = NULL;
3353 surface->pending.buffer_viewport.viewport_set = 0;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003354}
3355
3356static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003357viewport_destroy(struct wl_client *client,
3358 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003359{
3360 wl_resource_destroy(resource);
3361}
3362
3363static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003364viewport_set(struct wl_client *client,
3365 struct wl_resource *resource,
3366 wl_fixed_t src_x,
3367 wl_fixed_t src_y,
3368 wl_fixed_t src_width,
3369 wl_fixed_t src_height,
3370 int32_t dst_width,
3371 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003372{
Jonny Lamb74130762013-11-26 18:19:46 +01003373 struct weston_surface *surface =
3374 wl_resource_get_user_data(resource);
3375
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003376 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003377
Jonny Lamb8ae35902013-11-26 18:19:45 +01003378 if (wl_fixed_to_double(src_width) < 0 ||
3379 wl_fixed_to_double(src_height) < 0) {
3380 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003381 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003382 "source dimensions must be non-negative (%fx%f)",
3383 wl_fixed_to_double(src_width),
3384 wl_fixed_to_double(src_height));
3385 return;
3386 }
3387
3388 if (dst_width <= 0 || dst_height <= 0) {
3389 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003390 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003391 "destination dimensions must be positive (%dx%d)",
3392 dst_width, dst_height);
3393 return;
3394 }
3395
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003396 surface->pending.buffer_viewport.viewport_set = 1;
Jonny Lamb74130762013-11-26 18:19:46 +01003397
3398 surface->pending.buffer_viewport.src_x = src_x;
3399 surface->pending.buffer_viewport.src_y = src_y;
3400 surface->pending.buffer_viewport.src_width = src_width;
3401 surface->pending.buffer_viewport.src_height = src_height;
3402 surface->pending.buffer_viewport.dst_width = dst_width;
3403 surface->pending.buffer_viewport.dst_height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003404}
3405
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003406static const struct wl_viewport_interface viewport_interface = {
3407 viewport_destroy,
3408 viewport_set
Jonny Lamb8ae35902013-11-26 18:19:45 +01003409};
3410
3411static void
3412scaler_destroy(struct wl_client *client,
3413 struct wl_resource *resource)
3414{
3415 wl_resource_destroy(resource);
3416}
3417
3418static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003419scaler_get_viewport(struct wl_client *client,
3420 struct wl_resource *scaler,
3421 uint32_t id,
3422 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003423{
3424 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3425 struct wl_resource *resource;
3426
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003427 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003428 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003429 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3430 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003431 return;
3432 }
3433
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003434 resource = wl_resource_create(client, &wl_viewport_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003435 1, id);
3436 if (resource == NULL) {
3437 wl_client_post_no_memory(client);
3438 return;
3439 }
3440
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003441 wl_resource_set_implementation(resource, &viewport_interface,
3442 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003443
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003444 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003445}
3446
3447static const struct wl_scaler_interface scaler_interface = {
3448 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003449 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003450};
3451
3452static void
3453bind_scaler(struct wl_client *client,
3454 void *data, uint32_t version, uint32_t id)
3455{
3456 struct wl_resource *resource;
3457
3458 resource = wl_resource_create(client, &wl_scaler_interface,
3459 1, id);
3460 if (resource == NULL) {
3461 wl_client_post_no_memory(client);
3462 return;
3463 }
3464
3465 wl_resource_set_implementation(resource, &scaler_interface,
3466 NULL, NULL);
3467}
3468
3469static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003470compositor_bind(struct wl_client *client,
3471 void *data, uint32_t version, uint32_t id)
3472{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003473 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003474 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003475
Jason Ekstranda85118c2013-06-27 20:17:02 -05003476 resource = wl_resource_create(client, &wl_compositor_interface,
3477 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003478 if (resource == NULL) {
3479 wl_client_post_no_memory(client);
3480 return;
3481 }
3482
3483 wl_resource_set_implementation(resource, &compositor_interface,
3484 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003485}
3486
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003487static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003488log_uname(void)
3489{
3490 struct utsname usys;
3491
3492 uname(&usys);
3493
3494 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3495 usys.version, usys.machine);
3496}
3497
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003498WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003499weston_environment_get_fd(const char *env)
3500{
3501 char *e, *end;
3502 int fd, flags;
3503
3504 e = getenv(env);
3505 if (!e)
3506 return -1;
3507 fd = strtol(e, &end, 0);
3508 if (*end != '\0')
3509 return -1;
3510
3511 flags = fcntl(fd, F_GETFD);
3512 if (flags == -1)
3513 return -1;
3514
3515 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3516 unsetenv(env);
3517
3518 return fd;
3519}
3520
3521WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003522weston_compositor_init(struct weston_compositor *ec,
3523 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003524 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003525 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003526{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003527 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003528 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003529 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003530
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003531 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003532 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003533 wl_signal_init(&ec->destroy_signal);
3534 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003535 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003536 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003537 wl_signal_init(&ec->idle_signal);
3538 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003539 wl_signal_init(&ec->show_input_panel_signal);
3540 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003541 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003542 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003543 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003544 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003545 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003546 wl_signal_init(&ec->session_signal);
3547 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003548
Casey Dahlin58ba1372012-04-19 22:50:08 -04003549 ec->output_id_pool = 0;
3550
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003551 if (!wl_global_create(display, &wl_compositor_interface, 3,
3552 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003553 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003554
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003555 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3556 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003557 return -1;
3558
Jonny Lamb8ae35902013-11-26 18:19:45 +01003559 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3560 ec, bind_scaler))
3561 return -1;
3562
Jason Ekstranda7af7042013-10-12 22:38:11 -05003563 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003564 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003565 wl_list_init(&ec->layer_list);
3566 wl_list_init(&ec->seat_list);
3567 wl_list_init(&ec->output_list);
3568 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003569 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003570 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003571 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003572 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003573 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003574
Xiong Zhang97116532013-10-23 13:58:31 +08003575 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003576 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003577
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003578 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3579 weston_config_section_get_string(s, "keymap_rules",
3580 (char **) &xkb_names.rules, NULL);
3581 weston_config_section_get_string(s, "keymap_model",
3582 (char **) &xkb_names.model, NULL);
3583 weston_config_section_get_string(s, "keymap_layout",
3584 (char **) &xkb_names.layout, NULL);
3585 weston_config_section_get_string(s, "keymap_variant",
3586 (char **) &xkb_names.variant, NULL);
3587 weston_config_section_get_string(s, "keymap_options",
3588 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003589
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003590 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3591 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003592
Daniel Stone725c2c32012-06-22 14:04:36 +01003593 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003594 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003595
3596 wl_data_device_manager_init(ec->wl_display);
3597
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003598 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003599
Daniel Stone725c2c32012-06-22 14:04:36 +01003600 loop = wl_display_get_event_loop(ec->wl_display);
3601 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3602 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3603
3604 ec->input_loop = wl_event_loop_create();
3605
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003606 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3607 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3608
3609 weston_compositor_schedule_repaint(ec);
3610
Daniel Stone725c2c32012-06-22 14:04:36 +01003611 return 0;
3612}
3613
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003614WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003615weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003616{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003617 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003618
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003619 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003620 if (ec->input_loop_source)
3621 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003622
Matt Roper361d2ad2011-08-29 13:52:23 -07003623 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003624 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003625 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003626
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003627 if (ec->renderer)
3628 ec->renderer->destroy(ec);
3629
Daniel Stone325fc2d2012-05-30 16:31:58 +01003630 weston_binding_list_destroy_all(&ec->key_binding_list);
3631 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003632 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003633 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003634 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003635
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003636 weston_plane_release(&ec->primary_plane);
3637
Jonas Ådahlc97af922012-03-28 22:36:09 +02003638 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003639
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003640 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003641}
3642
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003643WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003644weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3645 const struct weston_pointer_grab_interface *interface)
3646{
3647 struct weston_seat *seat;
3648
3649 ec->default_pointer_grab = interface;
3650 wl_list_for_each(seat, &ec->seat_list, link) {
3651 if (seat->pointer) {
3652 weston_pointer_set_default_grab(seat->pointer,
3653 interface);
3654 }
3655 }
3656}
3657
3658WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003659weston_version(int *major, int *minor, int *micro)
3660{
3661 *major = WESTON_VERSION_MAJOR;
3662 *minor = WESTON_VERSION_MINOR;
3663 *micro = WESTON_VERSION_MICRO;
3664}
3665
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003666static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003667 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003668 const char *desc;
3669} capability_strings[] = {
3670 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003671 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003672};
3673
3674static void
3675weston_compositor_log_capabilities(struct weston_compositor *compositor)
3676{
3677 unsigned i;
3678 int yes;
3679
3680 weston_log("Compositor capabilities:\n");
3681 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3682 yes = compositor->capabilities & capability_strings[i].bit;
3683 weston_log_continue(STAMP_SPACE "%s %s\n",
3684 capability_strings[i].desc,
3685 yes ? "yes" : "no");
3686 }
3687}
3688
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003689static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003690{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003691 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003692
Martin Minarik6d118362012-06-07 18:01:59 +02003693 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003694 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003695
3696 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003697}
3698
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003699#ifdef HAVE_LIBUNWIND
3700
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003701static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003702print_backtrace(void)
3703{
3704 unw_cursor_t cursor;
3705 unw_context_t context;
3706 unw_word_t off;
3707 unw_proc_info_t pip;
3708 int ret, i = 0;
3709 char procname[256];
3710 const char *filename;
3711 Dl_info dlinfo;
3712
3713 pip.unwind_info = NULL;
3714 ret = unw_getcontext(&context);
3715 if (ret) {
3716 weston_log("unw_getcontext: %d\n", ret);
3717 return;
3718 }
3719
3720 ret = unw_init_local(&cursor, &context);
3721 if (ret) {
3722 weston_log("unw_init_local: %d\n", ret);
3723 return;
3724 }
3725
3726 ret = unw_step(&cursor);
3727 while (ret > 0) {
3728 ret = unw_get_proc_info(&cursor, &pip);
3729 if (ret) {
3730 weston_log("unw_get_proc_info: %d\n", ret);
3731 break;
3732 }
3733
3734 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3735 if (ret && ret != -UNW_ENOMEM) {
3736 if (ret != -UNW_EUNSPEC)
3737 weston_log("unw_get_proc_name: %d\n", ret);
3738 procname[0] = '?';
3739 procname[1] = 0;
3740 }
3741
3742 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3743 *dlinfo.dli_fname)
3744 filename = dlinfo.dli_fname;
3745 else
3746 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003747
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003748 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3749 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3750
3751 ret = unw_step(&cursor);
3752 if (ret < 0)
3753 weston_log("unw_step: %d\n", ret);
3754 }
3755}
3756
3757#else
3758
3759static void
3760print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003761{
3762 void *buffer[32];
3763 int i, count;
3764 Dl_info info;
3765
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003766 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3767 for (i = 0; i < count; i++) {
3768 dladdr(buffer[i], &info);
3769 weston_log(" [%016lx] %s (%s)\n",
3770 (long) buffer[i],
3771 info.dli_sname ? info.dli_sname : "--",
3772 info.dli_fname);
3773 }
3774}
3775
3776#endif
3777
3778static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003779on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003780{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003781 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003782 * then call the backend restore function, which will switch
3783 * back to the vt we launched from or ungrab X etc and then
3784 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003785 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003786 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003787 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003788
Peter Maatmane5b42e42013-03-27 22:38:53 +01003789 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003790
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003791 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003792
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003793 segv_compositor->restore(segv_compositor);
3794
3795 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003796}
3797
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003798WL_EXPORT void *
3799weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003800{
3801 char path[PATH_MAX];
3802 void *module, *init;
3803
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003804 if (name == NULL)
3805 return NULL;
3806
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003807 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003808 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003809 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003810 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003811
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003812 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3813 if (module) {
3814 weston_log("Module '%s' already loaded\n", path);
3815 dlclose(module);
3816 return NULL;
3817 }
3818
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003819 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003820 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003821 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003822 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003823 return NULL;
3824 }
3825
3826 init = dlsym(module, entrypoint);
3827 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003828 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003829 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003830 return NULL;
3831 }
3832
3833 return init;
3834}
3835
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003836static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003837load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003838 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003839{
3840 const char *p, *end;
3841 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003842 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003843 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003844
3845 if (modules == NULL)
3846 return 0;
3847
3848 p = modules;
3849 while (*p) {
3850 end = strchrnul(p, ',');
3851 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003852 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003853 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003854 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003855 p = end;
3856 while (*p == ',')
3857 p++;
3858
3859 }
3860
3861 return 0;
3862}
3863
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003864static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003865 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3866
3867static const char xdg_wrong_message[] =
3868 "fatal: environment variable XDG_RUNTIME_DIR\n"
3869 "is set to \"%s\", which is not a directory.\n";
3870
3871static const char xdg_wrong_mode_message[] =
3872 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01003873 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
3874 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003875
3876static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003877 "Refer to your distribution on how to get it, or\n"
3878 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3879 "on how to implement it.\n";
3880
Martin Minarik37032f82012-06-18 20:15:18 +02003881static void
3882verify_xdg_runtime_dir(void)
3883{
3884 char *dir = getenv("XDG_RUNTIME_DIR");
3885 struct stat s;
3886
3887 if (!dir) {
3888 weston_log(xdg_error_message);
3889 weston_log_continue(xdg_detail_message);
3890 exit(EXIT_FAILURE);
3891 }
3892
3893 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3894 weston_log(xdg_wrong_message, dir);
3895 weston_log_continue(xdg_detail_message);
3896 exit(EXIT_FAILURE);
3897 }
3898
3899 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3900 weston_log(xdg_wrong_mode_message,
3901 dir, s.st_mode & 0777, s.st_uid);
3902 weston_log_continue(xdg_detail_message);
3903 }
3904}
3905
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003906static int
3907usage(int error_code)
3908{
3909 fprintf(stderr,
3910 "Usage: weston [OPTIONS]\n\n"
3911 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3912 "Weston supports multiple backends, and depending on which backend is in use\n"
3913 "different options will be accepted.\n\n"
3914
3915
3916 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003917 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003918 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003919 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3920 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003921 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003922 " -S, --socket=NAME\tName of socket to listen on\n"
3923 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003924 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003925 " --log==FILE\t\tLog to the given file\n"
3926 " -h, --help\t\tThis help message\n\n");
3927
3928 fprintf(stderr,
3929 "Options for drm-backend.so:\n\n"
3930 " --connector=ID\tBring up only this connector\n"
3931 " --seat=SEAT\t\tThe seat that weston should run on\n"
3932 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003933 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003934 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3935
3936 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003937 "Options for fbdev-backend.so:\n\n"
3938 " --tty=TTY\t\tThe tty to use\n"
3939 " --device=DEVICE\tThe framebuffer device to use\n\n");
3940
3941 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003942 "Options for x11-backend.so:\n\n"
3943 " --width=WIDTH\t\tWidth of X window\n"
3944 " --height=HEIGHT\tHeight of X window\n"
3945 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003946 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003947 " --output-count=COUNT\tCreate multiple outputs\n"
3948 " --no-input\t\tDont create input devices\n\n");
3949
3950 fprintf(stderr,
3951 "Options for wayland-backend.so:\n\n"
3952 " --width=WIDTH\t\tWidth of Wayland surface\n"
3953 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003954 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003955 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003956 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003957 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003958 " --display=DISPLAY\tWayland display to connect to\n\n");
3959
Pekka Paalanene31e0532013-05-22 18:03:07 +03003960#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3961 fprintf(stderr,
3962 "Options for rpi-backend.so:\n\n"
3963 " --tty=TTY\t\tThe tty to use\n"
3964 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3965 " --transform=TR\tThe output transformation, TR is one of:\n"
3966 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003967 " --opaque-regions\tEnable support for opaque regions, can be "
3968 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003969 "\n");
3970#endif
3971
Hardeningc077a842013-07-08 00:51:35 +02003972#if defined(BUILD_RDP_COMPOSITOR)
3973 fprintf(stderr,
3974 "Options for rdp-backend.so:\n\n"
3975 " --width=WIDTH\t\tWidth of desktop\n"
3976 " --height=HEIGHT\tHeight of desktop\n"
3977 " --extra-modes=MODES\t\n"
3978 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3979 " --address=ADDR\tThe address to bind\n"
3980 " --port=PORT\tThe port to listen on\n"
3981 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3982 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3983 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3984 "\n");
3985#endif
3986
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003987 exit(error_code);
3988}
3989
Peter Maatmane5b42e42013-03-27 22:38:53 +01003990static void
3991catch_signals(void)
3992{
3993 struct sigaction action;
3994
3995 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3996 action.sa_sigaction = on_caught_signal;
3997 sigemptyset(&action.sa_mask);
3998 sigaction(SIGSEGV, &action, NULL);
3999 sigaction(SIGABRT, &action, NULL);
4000}
4001
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004002int main(int argc, char *argv[])
4003{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004004 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004005 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004006 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004007 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004008 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004009 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004010 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004011 int *argc, char *argv[],
4012 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004013 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004014 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004015 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004016 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004017 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004018 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004019 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004020 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004021 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004022 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004023 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004024 struct weston_config *config;
4025 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004026
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004027 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004028 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004029 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004030 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4031 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004032 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004033 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004034 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004035 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004036 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004037
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004038 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004039
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004040 if (help)
4041 usage(EXIT_SUCCESS);
4042
Scott Moreau12245142012-08-29 15:15:58 -06004043 if (version) {
4044 printf(PACKAGE_STRING "\n");
4045 return EXIT_SUCCESS;
4046 }
4047
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004048 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004049
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004050 weston_log("%s\n"
4051 STAMP_SPACE "%s\n"
4052 STAMP_SPACE "Bug reports to: %s\n"
4053 STAMP_SPACE "Build: %s\n",
4054 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004055 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004056 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004057
Martin Minarik37032f82012-06-18 20:15:18 +02004058 verify_xdg_runtime_dir();
4059
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004060 display = wl_display_create();
4061
Tiago Vignatti2116b892011-08-08 05:52:59 -07004062 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004063 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4064 display);
4065 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4066 display);
4067 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4068 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004069
4070 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004071 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4072 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004073
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004074 config = weston_config_parse("weston.ini");
4075 if (config != NULL) {
4076 weston_log("Using config file '%s'\n",
4077 weston_config_get_full_path(config));
4078 } else {
4079 weston_log("Starting with no config file.\n");
4080 }
4081 section = weston_config_get_section(config, "core", NULL, NULL);
4082
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004083 if (option_backend)
4084 backend = strdup(option_backend);
4085 else
4086 weston_config_section_get_string(section, "backend", &backend,
4087 NULL);
4088
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004089 if (!backend) {
4090 if (getenv("WAYLAND_DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004091 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004092 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004093 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004094 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004095 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004096 }
4097
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004098 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004099 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004100 if (!backend_init)
4101 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004102
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004103 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004104 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004105 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004106 exit(EXIT_FAILURE);
4107 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004108
Peter Maatmane5b42e42013-03-27 22:38:53 +01004109 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004110 segv_compositor = ec;
4111
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004112 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004113 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004114
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004115 setenv("WAYLAND_DISPLAY", socket_name, 1);
4116
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004117 if (option_shell)
4118 shell = strdup(option_shell);
4119 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004120 weston_config_section_get_string(section, "shell", &shell,
4121 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004122
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004123 if (load_modules(ec, shell, &argc, argv) < 0) {
4124 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004125 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004126 }
4127 free(shell);
4128
4129 weston_config_section_get_string(section, "modules", &modules, "");
4130 if (load_modules(ec, modules, &argc, argv) < 0) {
4131 free(modules);
4132 goto out;
4133 }
4134 free(modules);
4135
Ossama Othmana50e6e42013-05-14 09:48:26 -07004136 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004137 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004138
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004139 for (i = 1; i < argc; i++)
4140 weston_log("fatal: unhandled option: %s\n", argv[i]);
4141 if (argc > 1) {
4142 ret = EXIT_FAILURE;
4143 goto out;
4144 }
4145
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004146 weston_compositor_log_capabilities(ec);
4147
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004148 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004149 weston_log("fatal: failed to add socket: %m\n");
4150 ret = EXIT_FAILURE;
4151 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004152 }
4153
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004154 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004155
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004156 wl_display_run(display);
4157
4158 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004159 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004160 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004161
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004162 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004163
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004164 for (i = ARRAY_LENGTH(signals); i;)
4165 wl_event_source_remove(signals[--i]);
4166
Daniel Stone855028f2012-05-01 20:37:10 +01004167 weston_compositor_xkb_destroy(ec);
4168
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004169 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004170 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004171
Martin Minarik19e6f262012-06-07 13:08:46 +02004172 weston_log_file_close();
4173
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004174 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004175}