blob: 83ec9798dc4c42b3476e08ace51e856f4b66e10c [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010056#include "scaler-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
Hardening57388e42013-09-18 23:56:36 +0200141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349WL_EXPORT struct weston_view *
350weston_view_create(struct weston_surface *surface)
351{
352 struct weston_view *view;
353
354 view = calloc(1, sizeof *view);
355 if (view == NULL)
356 return NULL;
357
358 view->surface = surface;
359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 /* Assign to surface */
361 wl_list_insert(&surface->views, &view->surface_link);
362
363 wl_signal_init(&view->destroy_signal);
364 wl_list_init(&view->link);
365 wl_list_init(&view->layer_link);
366
Xiong Zhang97116532013-10-23 13:58:31 +0800367 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368
369 pixman_region32_init(&view->clip);
370
371 view->alpha = 1.0;
372 pixman_region32_init(&view->transform.opaque);
373
374 wl_list_init(&view->geometry.transformation_list);
375 wl_list_insert(&view->geometry.transformation_list,
376 &view->transform.position.link);
377 weston_matrix_init(&view->transform.position.matrix);
378 wl_list_init(&view->geometry.child_list);
379 pixman_region32_init(&view->transform.boundingbox);
380 view->transform.dirty = 1;
381
382 view->output = NULL;
383
384 return view;
385}
386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500387WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500388weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400391
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200392 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400393 if (surface == NULL)
394 return NULL;
395
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500396 wl_signal_init(&surface->destroy_signal);
397
398 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500399
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500400 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200401 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400402
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200403 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
404 surface->buffer_viewport.buffer.scale = 1;
405 surface->buffer_viewport.buffer.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 Paalanen952b6c82014-03-14 14:38:15 +0200642 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
643
644 if (vp->buffer.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +0100645 double a, b;
646
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200647 a = sx / vp->surface.width;
648 b = a * wl_fixed_to_double(vp->buffer.src_width);
649 *bx = b + wl_fixed_to_double(vp->buffer.src_x);
Jonny Lamb74130762013-11-26 18:19:46 +0100650
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200651 a = sy / vp->surface.height;
652 b = a * wl_fixed_to_double(vp->buffer.src_height);
653 *by = b + wl_fixed_to_double(vp->buffer.src_y);
Jonny Lamb74130762013-11-26 18:19:46 +0100654 } else {
655 *bx = sx;
656 *by = sy;
657 }
658}
659
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500660WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200661weston_surface_to_buffer_float(struct weston_surface *surface,
662 float sx, float sy, float *bx, float *by)
663{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200664 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
665
Jonny Lamb74130762013-11-26 18:19:46 +0100666 /* first transform coordinates if the scaler is set */
667 scaler_surface_to_buffer(surface, sx, sy, bx, by);
668
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200669 weston_transformed_coord(surface->width, surface->height,
670 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100671 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200672}
673
Alexander Larsson4ea95522013-05-22 14:41:37 +0200674WL_EXPORT void
675weston_surface_to_buffer(struct weston_surface *surface,
676 int sx, int sy, int *bx, int *by)
677{
678 float bxf, byf;
679
Jonny Lamb74130762013-11-26 18:19:46 +0100680 weston_surface_to_buffer_float(surface,
681 sx, sy, &bxf, &byf);
682
Alexander Larsson4ea95522013-05-22 14:41:37 +0200683 *bx = floorf(bxf);
684 *by = floorf(byf);
685}
686
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200687WL_EXPORT pixman_box32_t
688weston_surface_to_buffer_rect(struct weston_surface *surface,
689 pixman_box32_t rect)
690{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200691 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100692 float xf, yf;
693
694 /* first transform box coordinates if the scaler is set */
695 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
696 rect.x1 = floorf(xf);
697 rect.y1 = floorf(yf);
698
699 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
700 rect.x2 = floorf(xf);
701 rect.y2 = floorf(yf);
702
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200703 return weston_transformed_rect(surface->width, surface->height,
704 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200705 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200706}
707
708WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500709weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400710 struct weston_plane *plane)
711{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500712 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400713 return;
714
Jason Ekstranda7af7042013-10-12 22:38:11 -0500715 weston_view_damage_below(view);
716 view->plane = plane;
717 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400718}
719
720WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200722{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500723 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200724
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500725 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500726 pixman_region32_subtract(&damage, &view->transform.boundingbox,
727 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800728 if (view->plane)
729 pixman_region32_union(&view->plane->damage,
730 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500731 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800732 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200733}
734
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400735static void
736weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
737{
738 uint32_t different = es->output_mask ^ mask;
739 uint32_t entered = mask & different;
740 uint32_t left = es->output_mask & different;
741 struct weston_output *output;
742 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500743 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400744
745 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500746 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400747 return;
748 if (different == 0)
749 return;
750
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500751 client = wl_resource_get_client(es->resource);
752
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400753 wl_list_for_each(output, &es->compositor->output_list, link) {
754 if (1 << output->id & different)
755 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500756 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400757 client);
758 if (resource == NULL)
759 continue;
760 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500761 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400762 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500763 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400764 }
765}
766
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200767
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400768static void
769weston_surface_assign_output(struct weston_surface *es)
770{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500771 struct weston_output *new_output;
772 struct weston_view *view;
773 pixman_region32_t region;
774 uint32_t max, area, mask;
775 pixman_box32_t *e;
776
777 new_output = NULL;
778 max = 0;
779 mask = 0;
780 pixman_region32_init(&region);
781 wl_list_for_each(view, &es->views, surface_link) {
782 if (!view->output)
783 continue;
784
785 pixman_region32_intersect(&region, &view->transform.boundingbox,
786 &view->output->region);
787
788 e = pixman_region32_extents(&region);
789 area = (e->x2 - e->x1) * (e->y2 - e->y1);
790
791 mask |= view->output_mask;
792
793 if (area >= max) {
794 new_output = view->output;
795 max = area;
796 }
797 }
798 pixman_region32_fini(&region);
799
800 es->output = new_output;
801 weston_surface_update_output_mask(es, mask);
802}
803
804static void
805weston_view_assign_output(struct weston_view *ev)
806{
807 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400808 struct weston_output *output, *new_output;
809 pixman_region32_t region;
810 uint32_t max, area, mask;
811 pixman_box32_t *e;
812
813 new_output = NULL;
814 max = 0;
815 mask = 0;
816 pixman_region32_init(&region);
817 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500818 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400819 &output->region);
820
821 e = pixman_region32_extents(&region);
822 area = (e->x2 - e->x1) * (e->y2 - e->y1);
823
824 if (area > 0)
825 mask |= 1 << output->id;
826
827 if (area >= max) {
828 new_output = output;
829 max = area;
830 }
831 }
832 pixman_region32_fini(&region);
833
Jason Ekstranda7af7042013-10-12 22:38:11 -0500834 ev->output = new_output;
835 ev->output_mask = mask;
836
837 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400838}
839
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200840static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500841view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
842 int32_t width, int32_t height,
843 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200844{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200845 float min_x = HUGE_VALF, min_y = HUGE_VALF;
846 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200847 int32_t s[4][2] = {
848 { sx, sy },
849 { sx, sy + height },
850 { sx + width, sy },
851 { sx + width, sy + height }
852 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200853 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200854 int i;
855
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300856 if (width == 0 || height == 0) {
857 /* avoid rounding empty bbox to 1x1 */
858 pixman_region32_init(bbox);
859 return;
860 }
861
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200862 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200863 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500864 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200865 if (x < min_x)
866 min_x = x;
867 if (x > max_x)
868 max_x = x;
869 if (y < min_y)
870 min_y = y;
871 if (y > max_y)
872 max_y = y;
873 }
874
Pekka Paalanen219b9822012-02-08 15:38:37 +0200875 int_x = floorf(min_x);
876 int_y = floorf(min_y);
877 pixman_region32_init_rect(bbox, int_x, int_y,
878 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200879}
880
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200881static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200883{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500884 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200885
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200886 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500887 view->geometry.x = roundf(view->geometry.x);
888 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200889
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500890 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500891 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
892 view->transform.position.matrix.d[12] = view->geometry.x;
893 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500894
Jason Ekstranda7af7042013-10-12 22:38:11 -0500895 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500896
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 view->transform.inverse = view->transform.position.matrix;
898 view->transform.inverse.d[12] = -view->geometry.x;
899 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500900
Jason Ekstranda7af7042013-10-12 22:38:11 -0500901 pixman_region32_init_rect(&view->transform.boundingbox,
902 view->geometry.x,
903 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600904 view->surface->width,
905 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500906
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 if (view->alpha == 1.0) {
908 pixman_region32_copy(&view->transform.opaque,
909 &view->surface->opaque);
910 pixman_region32_translate(&view->transform.opaque,
911 view->geometry.x,
912 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500913 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200914}
915
916static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200918{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 struct weston_view *parent = view->geometry.parent;
920 struct weston_matrix *matrix = &view->transform.matrix;
921 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200922 struct weston_transform *tform;
923
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200925
926 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
928 view->transform.position.matrix.d[12] = view->geometry.x;
929 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200930
931 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200933 weston_matrix_multiply(matrix, &tform->matrix);
934
Pekka Paalanen483243f2013-03-08 14:56:50 +0200935 if (parent)
936 weston_matrix_multiply(matrix, &parent->transform.matrix);
937
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200938 if (weston_matrix_invert(inverse, matrix) < 0) {
939 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 weston_log("error: weston_view %p"
941 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200942 return -1;
943 }
944
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600945 view_compute_bbox(view, 0, 0,
946 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500948
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200949 return 0;
950}
951
952WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200954{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200956
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200958 return;
959
Pekka Paalanen483243f2013-03-08 14:56:50 +0200960 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200962
Jason Ekstranda7af7042013-10-12 22:38:11 -0500963 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200964
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 pixman_region32_fini(&view->transform.boundingbox);
968 pixman_region32_fini(&view->transform.opaque);
969 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500970
Pekka Paalanencd403622012-01-25 13:37:39 +0200971 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 if (view->geometry.transformation_list.next ==
973 &view->transform.position.link &&
974 view->geometry.transformation_list.prev ==
975 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200976 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200978 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 if (weston_view_update_transform_enable(view) < 0)
980 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200981 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200982
Jason Ekstranda7af7042013-10-12 22:38:11 -0500983 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200984
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 wl_signal_emit(&view->surface->compositor->transform_signal,
988 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200989}
990
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200991WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200993{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500994 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200995
996 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 * The invariant: if view->geometry.dirty, then all views
998 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200999 * Corollary: if not parent->geometry.dirty, then all ancestors
1000 * are not dirty.
1001 */
1002
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001004 return;
1005
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001009 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001011}
1012
1013WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014weston_view_to_global_fixed(struct weston_view *view,
1015 wl_fixed_t vx, wl_fixed_t vy,
1016 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001017{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001018 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 weston_view_to_global_float(view,
1021 wl_fixed_to_double(vx),
1022 wl_fixed_to_double(vy),
1023 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001024 *x = wl_fixed_from_double(xf);
1025 *y = wl_fixed_from_double(yf);
1026}
1027
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001028WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001029weston_view_from_global_float(struct weston_view *view,
1030 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001031{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001033 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1034
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001036
1037 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001038 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001040 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 *vx = 0;
1042 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001043 return;
1044 }
1045
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 *vx = v.f[0] / v.f[3];
1047 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001048 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 *vx = x - view->geometry.x;
1050 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001051 }
1052}
1053
1054WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055weston_view_from_global_fixed(struct weston_view *view,
1056 wl_fixed_t x, wl_fixed_t y,
1057 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001058{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001060
Jason Ekstranda7af7042013-10-12 22:38:11 -05001061 weston_view_from_global_float(view,
1062 wl_fixed_to_double(x),
1063 wl_fixed_to_double(y),
1064 &vxf, &vyf);
1065 *vx = wl_fixed_from_double(vxf);
1066 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001067}
1068
1069WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070weston_view_from_global(struct weston_view *view,
1071 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001072{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001074
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1076 *vx = floorf(vxf);
1077 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001078}
1079
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001080WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001081weston_surface_schedule_repaint(struct weston_surface *surface)
1082{
1083 struct weston_output *output;
1084
1085 wl_list_for_each(output, &surface->compositor->output_list, link)
1086 if (surface->output_mask & (1 << output->id))
1087 weston_output_schedule_repaint(output);
1088}
1089
1090WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001091weston_view_schedule_repaint(struct weston_view *view)
1092{
1093 struct weston_output *output;
1094
1095 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1096 if (view->output_mask & (1 << output->id))
1097 weston_output_schedule_repaint(output);
1098}
1099
1100WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001101weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001102{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001103 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 0, 0, surface->width,
1105 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001106
Kristian Høgsberg98238702012-08-03 16:29:12 -04001107 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001108}
1109
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001110WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001112{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001113 if (view->geometry.x == x && view->geometry.y == y)
1114 return;
1115
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 view->geometry.x = x;
1117 view->geometry.y = y;
1118 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001119}
1120
Pekka Paalanen483243f2013-03-08 14:56:50 +02001121static void
1122transform_parent_handle_parent_destroy(struct wl_listener *listener,
1123 void *data)
1124{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 struct weston_view *view =
1126 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001127 geometry.parent_destroy_listener);
1128
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001130}
1131
1132WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133weston_view_set_transform_parent(struct weston_view *view,
1134 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001135{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 if (view->geometry.parent) {
1137 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1138 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001139 }
1140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001142
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001144 transform_parent_handle_parent_destroy;
1145 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001146 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001148 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001150 }
1151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 weston_view_geometry_dirty(view);
1153}
1154
1155WL_EXPORT int
1156weston_view_is_mapped(struct weston_view *view)
1157{
1158 if (view->output)
1159 return 1;
1160 else
1161 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001162}
1163
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001164WL_EXPORT int
1165weston_surface_is_mapped(struct weston_surface *surface)
1166{
1167 if (surface->output)
1168 return 1;
1169 else
1170 return 0;
1171}
1172
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001173static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001174surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001175{
1176 struct weston_view *view;
1177
1178 if (surface->width == width && surface->height == height)
1179 return;
1180
1181 surface->width = width;
1182 surface->height = height;
1183
1184 wl_list_for_each(view, &surface->views, surface_link)
1185 weston_view_geometry_dirty(view);
1186}
1187
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001188WL_EXPORT void
1189weston_surface_set_size(struct weston_surface *surface,
1190 int32_t width, int32_t height)
1191{
1192 assert(!surface->resource);
1193 surface_set_size(surface, width, height);
1194}
1195
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001196static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001197weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001198{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001199 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001200 int32_t width, height;
1201
1202 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001203 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001204 return;
1205 }
1206
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001207 if (vp->buffer.viewport_set) {
1208 surface_set_size(surface, vp->surface.width,
1209 vp->surface.height);
Jonny Lamb74130762013-11-26 18:19:46 +01001210 return;
1211 }
1212
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001213 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001214 case WL_OUTPUT_TRANSFORM_90:
1215 case WL_OUTPUT_TRANSFORM_270:
1216 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1217 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001218 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001219 height = surface->buffer_ref.buffer->width;
1220 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001221 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001222 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001223 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001224 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001225 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001226
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001227 width = width / vp->buffer.scale;
1228 height = height / vp->buffer.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001229 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001230}
1231
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001232WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001233weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001234{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001235 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001236
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001237 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001238
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001239 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001240}
1241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242WL_EXPORT struct weston_view *
1243weston_compositor_pick_view(struct weston_compositor *compositor,
1244 wl_fixed_t x, wl_fixed_t y,
1245 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001246{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001248
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 wl_list_for_each(view, &compositor->view_list, link) {
1250 weston_view_from_global_fixed(view, x, y, vx, vy);
1251 if (pixman_region32_contains_point(&view->surface->input,
1252 wl_fixed_to_int(*vx),
1253 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001254 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001255 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001256 }
1257
1258 return NULL;
1259}
1260
1261static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001262weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001263{
Daniel Stone37816df2012-05-16 18:45:18 +01001264 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001265
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001266 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001267 return;
1268
Daniel Stone37816df2012-05-16 18:45:18 +01001269 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001270 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001271}
1272
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001273WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001275{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001276 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001277
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 if (!weston_view_is_mapped(view))
1279 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001280
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 weston_view_damage_below(view);
1282 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001283 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 wl_list_remove(&view->layer_link);
1285 wl_list_init(&view->layer_link);
1286 wl_list_remove(&view->link);
1287 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 view->output_mask = 0;
1289 weston_surface_assign_output(view->surface);
1290
1291 if (weston_surface_is_mapped(view->surface))
1292 return;
1293
1294 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1295 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001296 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001298 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001299 NULL,
1300 wl_fixed_from_int(0),
1301 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001303 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001304 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001306
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307WL_EXPORT void
1308weston_surface_unmap(struct weston_surface *surface)
1309{
1310 struct weston_view *view;
1311
1312 wl_list_for_each(view, &surface->views, surface_link)
1313 weston_view_unmap(view);
1314 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001315}
1316
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001317static void
1318weston_surface_reset_pending_buffer(struct weston_surface *surface)
1319{
1320 if (surface->pending.buffer)
1321 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1322 surface->pending.buffer = NULL;
1323 surface->pending.sx = 0;
1324 surface->pending.sy = 0;
1325 surface->pending.newly_attached = 0;
1326}
1327
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001328struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001329 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001330 struct wl_list link;
1331};
1332
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333WL_EXPORT void
1334weston_view_destroy(struct weston_view *view)
1335{
1336 wl_signal_emit(&view->destroy_signal, view);
1337
1338 assert(wl_list_empty(&view->geometry.child_list));
1339
1340 if (weston_view_is_mapped(view)) {
1341 weston_view_unmap(view);
1342 weston_compositor_build_view_list(view->surface->compositor);
1343 }
1344
1345 wl_list_remove(&view->link);
1346 wl_list_remove(&view->layer_link);
1347
1348 pixman_region32_fini(&view->clip);
1349 pixman_region32_fini(&view->transform.boundingbox);
1350
1351 weston_view_set_transform_parent(view, NULL);
1352
Jason Ekstranda7af7042013-10-12 22:38:11 -05001353 wl_list_remove(&view->surface_link);
1354
1355 free(view);
1356}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001357
1358WL_EXPORT void
1359weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001360{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001361 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001362 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001363
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001364 if (--surface->ref_count > 0)
1365 return;
1366
1367 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1368
Pekka Paalanene67858b2013-04-25 13:57:42 +03001369 assert(wl_list_empty(&surface->subsurface_list_pending));
1370 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1373 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001374
Pekka Paalanenbc106382012-10-10 12:49:31 +03001375 wl_list_for_each_safe(cb, next,
1376 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001377 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001378
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001379 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001380 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001381 pixman_region32_fini(&surface->pending.damage);
1382
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001383 if (surface->pending.buffer)
1384 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1385
Pekka Paalanende685b82012-12-04 15:58:12 +02001386 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001387
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001388 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001389 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001390 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001391
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001392 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001393 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001394
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001395 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001396}
1397
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001398static void
1399destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001400{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001401 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001402
Giulio Camuffo0d379742013-11-15 22:06:15 +01001403 /* Set the resource to NULL, since we don't want to leave a
1404 * dangling pointer if the surface was refcounted and survives
1405 * the weston_surface_destroy() call. */
1406 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001407 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001408}
1409
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001410static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001411weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1412{
1413 struct weston_buffer *buffer =
1414 container_of(listener, struct weston_buffer, destroy_listener);
1415
1416 wl_signal_emit(&buffer->destroy_signal, buffer);
1417 free(buffer);
1418}
1419
1420struct weston_buffer *
1421weston_buffer_from_resource(struct wl_resource *resource)
1422{
1423 struct weston_buffer *buffer;
1424 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001425
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001426 listener = wl_resource_get_destroy_listener(resource,
1427 weston_buffer_destroy_handler);
1428
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001429 if (listener)
1430 return container_of(listener, struct weston_buffer,
1431 destroy_listener);
1432
1433 buffer = zalloc(sizeof *buffer);
1434 if (buffer == NULL)
1435 return NULL;
1436
1437 buffer->resource = resource;
1438 wl_signal_init(&buffer->destroy_signal);
1439 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001440 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001441 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001442
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001443 return buffer;
1444}
1445
1446static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001447weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1448 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001449{
Pekka Paalanende685b82012-12-04 15:58:12 +02001450 struct weston_buffer_reference *ref =
1451 container_of(listener, struct weston_buffer_reference,
1452 destroy_listener);
1453
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001454 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001455 ref->buffer = NULL;
1456}
1457
1458WL_EXPORT void
1459weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001460 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001461{
1462 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001463 ref->buffer->busy_count--;
1464 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001465 assert(wl_resource_get_client(ref->buffer->resource));
1466 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001467 WL_BUFFER_RELEASE);
1468 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001469 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001470 }
1471
Pekka Paalanende685b82012-12-04 15:58:12 +02001472 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001473 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001474 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001475 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001476 }
1477
Pekka Paalanende685b82012-12-04 15:58:12 +02001478 ref->buffer = buffer;
1479 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1480}
1481
1482static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001483weston_surface_attach(struct weston_surface *surface,
1484 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001485{
1486 weston_buffer_reference(&surface->buffer_ref, buffer);
1487
Pekka Paalanena6421c42012-12-04 15:58:10 +02001488 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001489 if (weston_surface_is_mapped(surface))
1490 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001491 }
1492
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001493 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001494
1495 weston_surface_set_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001496}
1497
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001498WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001499weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001500{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001501 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001502
1503 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001504 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001505}
1506
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001507WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001508weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001510 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001511
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001512 pixman_region32_union(&compositor->primary_plane.damage,
1513 &compositor->primary_plane.damage,
1514 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001515 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001516}
1517
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001518static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001520{
Pekka Paalanende685b82012-12-04 15:58:12 +02001521 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001522 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001523 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001524
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 empty_region(&surface->damage);
1526}
1527
1528static void
1529view_accumulate_damage(struct weston_view *view,
1530 pixman_region32_t *opaque)
1531{
1532 pixman_region32_t damage;
1533
1534 pixman_region32_init(&damage);
1535 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001536 pixman_box32_t *extents;
1537
Jason Ekstranda7af7042013-10-12 22:38:11 -05001538 extents = pixman_region32_extents(&view->surface->damage);
1539 view_compute_bbox(view, extents->x1, extents->y1,
1540 extents->x2 - extents->x1,
1541 extents->y2 - extents->y1,
1542 &damage);
1543 pixman_region32_translate(&damage,
1544 -view->plane->x,
1545 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001546 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 pixman_region32_copy(&damage, &view->surface->damage);
1548 pixman_region32_translate(&damage,
1549 view->geometry.x - view->plane->x,
1550 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001551 }
1552
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553 pixman_region32_subtract(&damage, &damage, opaque);
1554 pixman_region32_union(&view->plane->damage,
1555 &view->plane->damage, &damage);
1556 pixman_region32_fini(&damage);
1557 pixman_region32_copy(&view->clip, opaque);
1558 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001559}
1560
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001561static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001562compositor_accumulate_damage(struct weston_compositor *ec)
1563{
1564 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001565 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001566 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001567
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001568 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001569
1570 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001571 pixman_region32_copy(&plane->clip, &clip);
1572
1573 pixman_region32_init(&opaque);
1574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 wl_list_for_each(ev, &ec->view_list, link) {
1576 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001577 continue;
1578
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001580 }
1581
1582 pixman_region32_union(&clip, &clip, &opaque);
1583 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001584 }
1585
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001586 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001587
Jason Ekstranda7af7042013-10-12 22:38:11 -05001588 wl_list_for_each(ev, &ec->view_list, link)
1589 ev->surface->touched = 0;
1590
1591 wl_list_for_each(ev, &ec->view_list, link) {
1592 if (ev->surface->touched)
1593 continue;
1594 ev->surface->touched = 1;
1595
1596 surface_flush_damage(ev->surface);
1597
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001598 /* Both the renderer and the backend have seen the buffer
1599 * by now. If renderer needs the buffer, it has its own
1600 * reference set. If the backend wants to keep the buffer
1601 * around for migrating the surface into a non-primary plane
1602 * later, keep_buffer is true. Otherwise, drop the core
1603 * reference now, and allow early buffer release. This enables
1604 * clients to use single-buffering.
1605 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001606 if (!ev->surface->keep_buffer)
1607 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001608 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001609}
1610
1611static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001613{
1614 struct weston_subsurface *sub;
1615
Pekka Paalanene67858b2013-04-25 13:57:42 +03001616 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001618 continue;
1619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1621 wl_list_init(&sub->surface->views);
1622
1623 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001624 }
1625}
1626
1627static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001629{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001630 struct weston_subsurface *sub;
1631 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001632
Jason Ekstranda7af7042013-10-12 22:38:11 -05001633 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1634 if (sub->surface == surface)
1635 continue;
1636
1637 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1638 weston_view_destroy(view);
1639
1640 surface_free_unused_subsurface_views(sub->surface);
1641 }
1642}
1643
1644static void
1645view_list_add_subsurface_view(struct weston_compositor *compositor,
1646 struct weston_subsurface *sub,
1647 struct weston_view *parent)
1648{
1649 struct weston_subsurface *child;
1650 struct weston_view *view = NULL, *iv;
1651
1652 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1653 if (iv->geometry.parent == parent) {
1654 view = iv;
1655 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001656 }
1657 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658
1659 if (view) {
1660 /* Put it back in the surface's list of views */
1661 wl_list_remove(&view->surface_link);
1662 wl_list_insert(&sub->surface->views, &view->surface_link);
1663 } else {
1664 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001665 weston_view_set_position(view,
1666 sub->position.x,
1667 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001668 weston_view_set_transform_parent(view, parent);
1669 }
1670
1671 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672
Pekka Paalanenb188e912013-11-19 14:03:35 +02001673 if (wl_list_empty(&sub->surface->subsurface_list)) {
1674 wl_list_insert(compositor->view_list.prev, &view->link);
1675 return;
1676 }
1677
1678 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1679 if (child->surface == sub->surface)
1680 wl_list_insert(compositor->view_list.prev, &view->link);
1681 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001683 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001684}
1685
1686static void
1687view_list_add(struct weston_compositor *compositor,
1688 struct weston_view *view)
1689{
1690 struct weston_subsurface *sub;
1691
1692 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693
Pekka Paalanenb188e912013-11-19 14:03:35 +02001694 if (wl_list_empty(&view->surface->subsurface_list)) {
1695 wl_list_insert(compositor->view_list.prev, &view->link);
1696 return;
1697 }
1698
1699 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1700 if (sub->surface == view->surface)
1701 wl_list_insert(compositor->view_list.prev, &view->link);
1702 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001704 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705}
1706
1707static void
1708weston_compositor_build_view_list(struct weston_compositor *compositor)
1709{
1710 struct weston_view *view;
1711 struct weston_layer *layer;
1712
1713 wl_list_for_each(layer, &compositor->layer_list, link)
1714 wl_list_for_each(view, &layer->view_list, layer_link)
1715 surface_stash_subsurface_views(view->surface);
1716
1717 wl_list_init(&compositor->view_list);
1718 wl_list_for_each(layer, &compositor->layer_list, link) {
1719 wl_list_for_each(view, &layer->view_list, layer_link) {
1720 view_list_add(compositor, view);
1721 }
1722 }
1723
1724 wl_list_for_each(layer, &compositor->layer_list, link)
1725 wl_list_for_each(view, &layer->view_list, layer_link)
1726 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001727}
1728
David Herrmann1edf44c2013-10-22 17:11:26 +02001729static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001730weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001731{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001732 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001734 struct weston_animation *animation, *next;
1735 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001736 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001737 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001738 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001739
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001740 if (output->destroying)
1741 return 0;
1742
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001743 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001745
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001746 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001747 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001748 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001749 wl_list_for_each(ev, &ec->view_list, link)
1750 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001751
Pekka Paalanene67858b2013-04-25 13:57:42 +03001752 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 wl_list_for_each(ev, &ec->view_list, link) {
1754 /* Note: This operation is safe to do multiple times on the
1755 * same surface.
1756 */
1757 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001758 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001759 &ev->surface->frame_callback_list);
1760 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001761 }
1762 }
1763
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001764 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001765
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001766 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001767 pixman_region32_intersect(&output_damage,
1768 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001769 pixman_region32_subtract(&output_damage,
1770 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001771
Scott Moreauccbf29d2012-02-22 14:21:41 -07001772 if (output->dirty)
1773 weston_output_update_matrix(output);
1774
David Herrmann1edf44c2013-10-22 17:11:26 +02001775 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001776
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001777 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001778
Kristian Høgsbergef044142011-06-21 15:02:12 -04001779 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001780
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001781 weston_compositor_repick(ec);
1782 wl_event_loop_dispatch(ec->input_loop, 0);
1783
Jonas Ådahldb773762012-06-13 00:01:21 +02001784 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001785 wl_callback_send_done(cb->resource, msecs);
1786 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001787 }
1788
Scott Moreaud64cf212012-06-08 19:40:54 -06001789 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001790 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001791 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001792 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001793
1794 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001795}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001796
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001797static int
1798weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001799{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001800 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001801
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001802 wl_event_loop_dispatch(compositor->input_loop, 0);
1803
1804 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001805}
1806
1807WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001808weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001809{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001810 struct weston_compositor *compositor = output->compositor;
1811 struct wl_event_loop *loop =
1812 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001813 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001814
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001815 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001816
1817 if (output->repaint_needed &&
1818 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1819 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001820 r = weston_output_repaint(output, msecs);
1821 if (!r)
1822 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001823 }
1824
1825 output->repaint_scheduled = 0;
1826 if (compositor->input_loop_source)
1827 return;
1828
1829 fd = wl_event_loop_get_fd(compositor->input_loop);
1830 compositor->input_loop_source =
1831 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1832 weston_compositor_read_input, compositor);
1833}
1834
1835static void
1836idle_repaint(void *data)
1837{
1838 struct weston_output *output = data;
1839
Jonas Ådahle5a12252013-04-05 23:07:11 +02001840 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001841}
1842
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001843WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001844weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1845{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001846 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001847 if (below != NULL)
1848 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001849}
1850
1851WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001852weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001853{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001854 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001855 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001856
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001857 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1858 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001859 return;
1860
Kristian Høgsbergef044142011-06-21 15:02:12 -04001861 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001862 output->repaint_needed = 1;
1863 if (output->repaint_scheduled)
1864 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001865
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001866 wl_event_loop_add_idle(loop, idle_repaint, output);
1867 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001868
1869 if (compositor->input_loop_source) {
1870 wl_event_source_remove(compositor->input_loop_source);
1871 compositor->input_loop_source = NULL;
1872 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001873}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001874
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001875WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001876weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1877{
1878 struct weston_output *output;
1879
1880 wl_list_for_each(output, &compositor->output_list, link)
1881 weston_output_schedule_repaint(output);
1882}
1883
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001884static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001885surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001886{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001887 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001888}
1889
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001890static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001891surface_attach(struct wl_client *client,
1892 struct wl_resource *resource,
1893 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1894{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001895 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001896 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001897
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001898 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001899 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001900 if (buffer == NULL) {
1901 wl_client_post_no_memory(client);
1902 return;
1903 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001904 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001905
Pekka Paalanende685b82012-12-04 15:58:12 +02001906 /* Attach, attach, without commit in between does not send
1907 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001908 if (surface->pending.buffer)
1909 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001910
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001911 surface->pending.sx = sx;
1912 surface->pending.sy = sy;
1913 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001914 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001915 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001916 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001917 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001918 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001919}
1920
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001921static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001922surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001923 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001924 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001925{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001926 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001927
Pekka Paalanen8e159182012-10-10 12:49:25 +03001928 pixman_region32_union_rect(&surface->pending.damage,
1929 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001930 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001931}
1932
Kristian Høgsberg33418202011-08-16 23:01:28 -04001933static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001934destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001935{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001936 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001937
1938 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001939 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001940}
1941
1942static void
1943surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001944 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001945{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001946 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001947 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001948
1949 cb = malloc(sizeof *cb);
1950 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001951 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001952 return;
1953 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001954
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001955 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1956 callback);
1957 if (cb->resource == NULL) {
1958 free(cb);
1959 wl_resource_post_no_memory(resource);
1960 return;
1961 }
1962
Jason Ekstranda85118c2013-06-27 20:17:02 -05001963 wl_resource_set_implementation(cb->resource, NULL, cb,
1964 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001965
Pekka Paalanenbc106382012-10-10 12:49:31 +03001966 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001967}
1968
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001969static void
1970surface_set_opaque_region(struct wl_client *client,
1971 struct wl_resource *resource,
1972 struct wl_resource *region_resource)
1973{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001974 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001975 struct weston_region *region;
1976
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001977 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001978 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001979 pixman_region32_copy(&surface->pending.opaque,
1980 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001981 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001982 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001983 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001984}
1985
1986static void
1987surface_set_input_region(struct wl_client *client,
1988 struct wl_resource *resource,
1989 struct wl_resource *region_resource)
1990{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001991 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001992 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001993
1994 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001995 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001996 pixman_region32_copy(&surface->pending.input,
1997 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001998 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001999 pixman_region32_fini(&surface->pending.input);
2000 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002001 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002002}
2003
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002004static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002005weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002006{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002007 struct weston_subsurface *sub;
2008
2009 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2010 parent_link_pending) {
2011 wl_list_remove(&sub->parent_link);
2012 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2013 }
2014}
2015
2016static void
2017weston_surface_commit(struct weston_surface *surface)
2018{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002019 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002020 pixman_region32_t opaque;
2021
Pekka Paalanen260ba382014-03-14 14:38:12 +02002022 /* XXX: wl_viewport.set without an attach should call configure */
2023
Alexander Larsson4ea95522013-05-22 14:41:37 +02002024 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002025 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002026 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002027 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002028
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002029 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002030 if (surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002031 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002032
2033 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002034 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002035 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002036
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002037 weston_surface_reset_pending_buffer(surface);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002038
2039 /* wl_surface.damage */
2040 pixman_region32_union(&surface->damage, &surface->damage,
2041 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002042 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2043 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 surface->width,
2045 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002046 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002047
2048 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002049 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002050 surface->width,
2051 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002052 pixman_region32_intersect(&opaque,
2053 &opaque, &surface->pending.opaque);
2054
2055 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2056 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002057 wl_list_for_each(view, &surface->views, surface_link)
2058 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002059 }
2060
2061 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002062
2063 /* wl_surface.set_input_region */
2064 pixman_region32_fini(&surface->input);
2065 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002066 surface->width,
2067 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002068 pixman_region32_intersect(&surface->input,
2069 &surface->input, &surface->pending.input);
2070
Pekka Paalanenbc106382012-10-10 12:49:31 +03002071 /* wl_surface.frame */
2072 wl_list_insert_list(&surface->frame_callback_list,
2073 &surface->pending.frame_callback_list);
2074 wl_list_init(&surface->pending.frame_callback_list);
2075
Pekka Paalanene67858b2013-04-25 13:57:42 +03002076 weston_surface_commit_subsurface_order(surface);
2077
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002078 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002079}
2080
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002081static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002082weston_subsurface_commit(struct weston_subsurface *sub);
2083
2084static void
2085weston_subsurface_parent_commit(struct weston_subsurface *sub,
2086 int parent_is_synchronized);
2087
2088static void
2089surface_commit(struct wl_client *client, struct wl_resource *resource)
2090{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002091 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002092 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2093
2094 if (sub) {
2095 weston_subsurface_commit(sub);
2096 return;
2097 }
2098
2099 weston_surface_commit(surface);
2100
2101 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2102 if (sub->surface != surface)
2103 weston_subsurface_parent_commit(sub, 0);
2104 }
2105}
2106
2107static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002108surface_set_buffer_transform(struct wl_client *client,
2109 struct wl_resource *resource, int transform)
2110{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002111 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002112
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002113 surface->pending.buffer_viewport.buffer.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002114}
2115
Alexander Larsson4ea95522013-05-22 14:41:37 +02002116static void
2117surface_set_buffer_scale(struct wl_client *client,
2118 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002119 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002120{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002121 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002122
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002123 surface->pending.buffer_viewport.buffer.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002124}
2125
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002126static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002127 surface_destroy,
2128 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002129 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002130 surface_frame,
2131 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002132 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002133 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002134 surface_set_buffer_transform,
2135 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002136};
2137
2138static void
2139compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002140 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002141{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002142 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002143 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002144
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002145 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002146 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002147 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002148 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002149 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002150
Jason Ekstranda85118c2013-06-27 20:17:02 -05002151 surface->resource =
2152 wl_resource_create(client, &wl_surface_interface,
2153 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002154 if (surface->resource == NULL) {
2155 weston_surface_destroy(surface);
2156 wl_resource_post_no_memory(resource);
2157 return;
2158 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002159 wl_resource_set_implementation(surface->resource, &surface_interface,
2160 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002161}
2162
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002163static void
2164destroy_region(struct wl_resource *resource)
2165{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002166 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002167
2168 pixman_region32_fini(&region->region);
2169 free(region);
2170}
2171
2172static void
2173region_destroy(struct wl_client *client, struct wl_resource *resource)
2174{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002175 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002176}
2177
2178static void
2179region_add(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
2184 pixman_region32_union_rect(&region->region, &region->region,
2185 x, y, width, height);
2186}
2187
2188static void
2189region_subtract(struct wl_client *client, struct wl_resource *resource,
2190 int32_t x, int32_t y, int32_t width, int32_t height)
2191{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002192 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002193 pixman_region32_t rect;
2194
2195 pixman_region32_init_rect(&rect, x, y, width, height);
2196 pixman_region32_subtract(&region->region, &region->region, &rect);
2197 pixman_region32_fini(&rect);
2198}
2199
2200static const struct wl_region_interface region_interface = {
2201 region_destroy,
2202 region_add,
2203 region_subtract
2204};
2205
2206static void
2207compositor_create_region(struct wl_client *client,
2208 struct wl_resource *resource, uint32_t id)
2209{
2210 struct weston_region *region;
2211
2212 region = malloc(sizeof *region);
2213 if (region == NULL) {
2214 wl_resource_post_no_memory(resource);
2215 return;
2216 }
2217
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002218 pixman_region32_init(&region->region);
2219
Jason Ekstranda85118c2013-06-27 20:17:02 -05002220 region->resource =
2221 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002222 if (region->resource == NULL) {
2223 free(region);
2224 wl_resource_post_no_memory(resource);
2225 return;
2226 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002227 wl_resource_set_implementation(region->resource, &region_interface,
2228 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002229}
2230
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002231static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002232 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002233 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002234};
2235
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002236static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2238{
2239 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002240 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002241 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002242
Alexander Larsson4ea95522013-05-22 14:41:37 +02002243 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002244 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002245 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002246 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002247
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002249 if (sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002250 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2251 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2252
Pekka Paalanene67858b2013-04-25 13:57:42 +03002253 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002254 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002255 sub->cached.sx = 0;
2256 sub->cached.sy = 0;
2257 sub->cached.newly_attached = 0;
2258
2259 /* wl_surface.damage */
2260 pixman_region32_union(&surface->damage, &surface->damage,
2261 &sub->cached.damage);
2262 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2263 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002264 surface->width,
2265 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002266 empty_region(&sub->cached.damage);
2267
2268 /* wl_surface.set_opaque_region */
2269 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002270 surface->width,
2271 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002272 pixman_region32_intersect(&opaque,
2273 &opaque, &sub->cached.opaque);
2274
2275 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2276 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002277 wl_list_for_each(view, &surface->views, surface_link)
2278 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279 }
2280
2281 pixman_region32_fini(&opaque);
2282
2283 /* wl_surface.set_input_region */
2284 pixman_region32_fini(&surface->input);
2285 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 surface->width,
2287 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002288 pixman_region32_intersect(&surface->input,
2289 &surface->input, &sub->cached.input);
2290
2291 /* wl_surface.frame */
2292 wl_list_insert_list(&surface->frame_callback_list,
2293 &sub->cached.frame_callback_list);
2294 wl_list_init(&sub->cached.frame_callback_list);
2295
2296 weston_surface_commit_subsurface_order(surface);
2297
2298 weston_surface_schedule_repaint(surface);
2299
2300 sub->cached.has_data = 0;
2301}
2302
2303static void
2304weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2305{
2306 struct weston_surface *surface = sub->surface;
2307
2308 /*
2309 * If this commit would cause the surface to move by the
2310 * attach(dx, dy) parameters, the old damage region must be
2311 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002312 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313 */
2314 pixman_region32_translate(&sub->cached.damage,
2315 -surface->pending.sx, -surface->pending.sy);
2316 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2317 &surface->pending.damage);
2318 empty_region(&surface->pending.damage);
2319
2320 if (surface->pending.newly_attached) {
2321 sub->cached.newly_attached = 1;
2322 weston_buffer_reference(&sub->cached.buffer_ref,
2323 surface->pending.buffer);
2324 }
2325 sub->cached.sx += surface->pending.sx;
2326 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002327
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002328 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002330 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002331
2332 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2333
2334 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2335
2336 wl_list_insert_list(&sub->cached.frame_callback_list,
2337 &surface->pending.frame_callback_list);
2338 wl_list_init(&surface->pending.frame_callback_list);
2339
2340 sub->cached.has_data = 1;
2341}
2342
2343static int
2344weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2345{
2346 while (sub) {
2347 if (sub->synchronized)
2348 return 1;
2349
2350 if (!sub->parent)
2351 return 0;
2352
2353 sub = weston_surface_to_subsurface(sub->parent);
2354 }
2355
2356 return 0;
2357}
2358
2359static void
2360weston_subsurface_commit(struct weston_subsurface *sub)
2361{
2362 struct weston_surface *surface = sub->surface;
2363 struct weston_subsurface *tmp;
2364
2365 /* Recursive check for effectively synchronized. */
2366 if (weston_subsurface_is_synchronized(sub)) {
2367 weston_subsurface_commit_to_cache(sub);
2368 } else {
2369 if (sub->cached.has_data) {
2370 /* flush accumulated state from cache */
2371 weston_subsurface_commit_to_cache(sub);
2372 weston_subsurface_commit_from_cache(sub);
2373 } else {
2374 weston_surface_commit(surface);
2375 }
2376
2377 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2378 if (tmp->surface != surface)
2379 weston_subsurface_parent_commit(tmp, 0);
2380 }
2381 }
2382}
2383
2384static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002385weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002386{
2387 struct weston_surface *surface = sub->surface;
2388 struct weston_subsurface *tmp;
2389
Pekka Paalanene67858b2013-04-25 13:57:42 +03002390 /* From now on, commit_from_cache the whole sub-tree, regardless of
2391 * the synchronized mode of each child. This sub-surface or some
2392 * of its ancestors were synchronized, so we are synchronized
2393 * all the way down.
2394 */
2395
2396 if (sub->cached.has_data)
2397 weston_subsurface_commit_from_cache(sub);
2398
2399 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2400 if (tmp->surface != surface)
2401 weston_subsurface_parent_commit(tmp, 1);
2402 }
2403}
2404
2405static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002406weston_subsurface_parent_commit(struct weston_subsurface *sub,
2407 int parent_is_synchronized)
2408{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002409 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002410 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002411 wl_list_for_each(view, &sub->surface->views, surface_link)
2412 weston_view_set_position(view,
2413 sub->position.x,
2414 sub->position.y);
2415
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002416 sub->position.set = 0;
2417 }
2418
2419 if (parent_is_synchronized || sub->synchronized)
2420 weston_subsurface_synchronized_commit(sub);
2421}
2422
2423static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002424subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002425{
2426 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002427 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428
Jason Ekstranda7af7042013-10-12 22:38:11 -05002429 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002430 weston_view_set_position(view,
2431 view->geometry.x + dx,
2432 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002433
2434 /* No need to check parent mappedness, because if parent is not
2435 * mapped, parent is not in a visible layer, so this sub-surface
2436 * will not be drawn either.
2437 */
2438 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002439 /* Cannot call weston_surface_update_transform(),
2440 * because that would call it also for the parent surface,
2441 * which might not be mapped yet. That would lead to
2442 * inconsistent state, where the window could never be
2443 * mapped.
2444 *
2445 * Instead just assing any output, to make
2446 * weston_surface_is_mapped() return true, so that when the
2447 * parent surface does get mapped, this one will get
2448 * included, too. See surface_list_add().
2449 */
2450 assert(!wl_list_empty(&compositor->output_list));
2451 surface->output = container_of(compositor->output_list.next,
2452 struct weston_output, link);
2453 }
2454}
2455
2456static struct weston_subsurface *
2457weston_surface_to_subsurface(struct weston_surface *surface)
2458{
2459 if (surface->configure == subsurface_configure)
2460 return surface->configure_private;
2461
2462 return NULL;
2463}
2464
Pekka Paalanen01388e22013-04-25 13:57:44 +03002465WL_EXPORT struct weston_surface *
2466weston_surface_get_main_surface(struct weston_surface *surface)
2467{
2468 struct weston_subsurface *sub;
2469
2470 while (surface && (sub = weston_surface_to_subsurface(surface)))
2471 surface = sub->parent;
2472
2473 return surface;
2474}
2475
Pekka Paalanene67858b2013-04-25 13:57:42 +03002476static void
2477subsurface_set_position(struct wl_client *client,
2478 struct wl_resource *resource, int32_t x, int32_t y)
2479{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002480 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002481
2482 if (!sub)
2483 return;
2484
2485 sub->position.x = x;
2486 sub->position.y = y;
2487 sub->position.set = 1;
2488}
2489
2490static struct weston_subsurface *
2491subsurface_from_surface(struct weston_surface *surface)
2492{
2493 struct weston_subsurface *sub;
2494
2495 sub = weston_surface_to_subsurface(surface);
2496 if (sub)
2497 return sub;
2498
2499 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2500 if (sub->surface == surface)
2501 return sub;
2502
2503 return NULL;
2504}
2505
2506static struct weston_subsurface *
2507subsurface_sibling_check(struct weston_subsurface *sub,
2508 struct weston_surface *surface,
2509 const char *request)
2510{
2511 struct weston_subsurface *sibling;
2512
2513 sibling = subsurface_from_surface(surface);
2514
2515 if (!sibling) {
2516 wl_resource_post_error(sub->resource,
2517 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2518 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002519 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002520 return NULL;
2521 }
2522
2523 if (sibling->parent != sub->parent) {
2524 wl_resource_post_error(sub->resource,
2525 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2526 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002527 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002528 return NULL;
2529 }
2530
2531 return sibling;
2532}
2533
2534static void
2535subsurface_place_above(struct wl_client *client,
2536 struct wl_resource *resource,
2537 struct wl_resource *sibling_resource)
2538{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002539 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002540 struct weston_surface *surface =
2541 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002542 struct weston_subsurface *sibling;
2543
2544 if (!sub)
2545 return;
2546
2547 sibling = subsurface_sibling_check(sub, surface, "place_above");
2548 if (!sibling)
2549 return;
2550
2551 wl_list_remove(&sub->parent_link_pending);
2552 wl_list_insert(sibling->parent_link_pending.prev,
2553 &sub->parent_link_pending);
2554}
2555
2556static void
2557subsurface_place_below(struct wl_client *client,
2558 struct wl_resource *resource,
2559 struct wl_resource *sibling_resource)
2560{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002561 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002562 struct weston_surface *surface =
2563 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002564 struct weston_subsurface *sibling;
2565
2566 if (!sub)
2567 return;
2568
2569 sibling = subsurface_sibling_check(sub, surface, "place_below");
2570 if (!sibling)
2571 return;
2572
2573 wl_list_remove(&sub->parent_link_pending);
2574 wl_list_insert(&sibling->parent_link_pending,
2575 &sub->parent_link_pending);
2576}
2577
2578static void
2579subsurface_set_sync(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
2583 if (sub)
2584 sub->synchronized = 1;
2585}
2586
2587static void
2588subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2589{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002590 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002591
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002592 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002593 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002594
2595 /* If sub became effectively desynchronized, flush. */
2596 if (!weston_subsurface_is_synchronized(sub))
2597 weston_subsurface_synchronized_commit(sub);
2598 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002599}
2600
2601static void
2602weston_subsurface_cache_init(struct weston_subsurface *sub)
2603{
2604 pixman_region32_init(&sub->cached.damage);
2605 pixman_region32_init(&sub->cached.opaque);
2606 pixman_region32_init(&sub->cached.input);
2607 wl_list_init(&sub->cached.frame_callback_list);
2608 sub->cached.buffer_ref.buffer = NULL;
2609}
2610
2611static void
2612weston_subsurface_cache_fini(struct weston_subsurface *sub)
2613{
2614 struct weston_frame_callback *cb, *tmp;
2615
2616 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002617 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618
2619 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2620 pixman_region32_fini(&sub->cached.damage);
2621 pixman_region32_fini(&sub->cached.opaque);
2622 pixman_region32_fini(&sub->cached.input);
2623}
2624
2625static void
2626weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2627{
2628 wl_list_remove(&sub->parent_link);
2629 wl_list_remove(&sub->parent_link_pending);
2630 wl_list_remove(&sub->parent_destroy_listener.link);
2631 sub->parent = NULL;
2632}
2633
2634static void
2635weston_subsurface_destroy(struct weston_subsurface *sub);
2636
2637static void
2638subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2639{
2640 struct weston_subsurface *sub =
2641 container_of(listener, struct weston_subsurface,
2642 surface_destroy_listener);
2643 assert(data == &sub->surface->resource);
2644
2645 /* The protocol object (wl_resource) is left inert. */
2646 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002647 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002648
2649 weston_subsurface_destroy(sub);
2650}
2651
2652static void
2653subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2654{
2655 struct weston_subsurface *sub =
2656 container_of(listener, struct weston_subsurface,
2657 parent_destroy_listener);
2658 assert(data == &sub->parent->resource);
2659 assert(sub->surface != sub->parent);
2660
2661 if (weston_surface_is_mapped(sub->surface))
2662 weston_surface_unmap(sub->surface);
2663
2664 weston_subsurface_unlink_parent(sub);
2665}
2666
2667static void
2668subsurface_resource_destroy(struct wl_resource *resource)
2669{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002670 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002671
2672 if (sub)
2673 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674}
2675
2676static void
2677subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2678{
2679 wl_resource_destroy(resource);
2680}
2681
2682static void
2683weston_subsurface_link_parent(struct weston_subsurface *sub,
2684 struct weston_surface *parent)
2685{
2686 sub->parent = parent;
2687 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002688 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002689 &sub->parent_destroy_listener);
2690
2691 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2692 wl_list_insert(&parent->subsurface_list_pending,
2693 &sub->parent_link_pending);
2694}
2695
2696static void
2697weston_subsurface_link_surface(struct weston_subsurface *sub,
2698 struct weston_surface *surface)
2699{
2700 sub->surface = surface;
2701 sub->surface_destroy_listener.notify =
2702 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002703 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002704 &sub->surface_destroy_listener);
2705}
2706
2707static void
2708weston_subsurface_destroy(struct weston_subsurface *sub)
2709{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002710 struct weston_view *view, *next;
2711
Pekka Paalanene67858b2013-04-25 13:57:42 +03002712 assert(sub->surface);
2713
2714 if (sub->resource) {
2715 assert(weston_surface_to_subsurface(sub->surface) == sub);
2716 assert(sub->parent_destroy_listener.notify ==
2717 subsurface_handle_parent_destroy);
2718
Jason Ekstranda7af7042013-10-12 22:38:11 -05002719 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2720 weston_view_destroy(view);
2721
Pekka Paalanene67858b2013-04-25 13:57:42 +03002722 if (sub->parent)
2723 weston_subsurface_unlink_parent(sub);
2724
2725 weston_subsurface_cache_fini(sub);
2726
2727 sub->surface->configure = NULL;
2728 sub->surface->configure_private = NULL;
2729 } else {
2730 /* the dummy weston_subsurface for the parent itself */
2731 assert(sub->parent_destroy_listener.notify == NULL);
2732 wl_list_remove(&sub->parent_link);
2733 wl_list_remove(&sub->parent_link_pending);
2734 }
2735
2736 wl_list_remove(&sub->surface_destroy_listener.link);
2737 free(sub);
2738}
2739
2740static const struct wl_subsurface_interface subsurface_implementation = {
2741 subsurface_destroy,
2742 subsurface_set_position,
2743 subsurface_place_above,
2744 subsurface_place_below,
2745 subsurface_set_sync,
2746 subsurface_set_desync
2747};
2748
2749static struct weston_subsurface *
2750weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2751 struct weston_surface *parent)
2752{
2753 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002754 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002755
2756 sub = calloc(1, sizeof *sub);
2757 if (!sub)
2758 return NULL;
2759
Jason Ekstranda7af7042013-10-12 22:38:11 -05002760 wl_list_init(&sub->unused_views);
2761
Jason Ekstranda85118c2013-06-27 20:17:02 -05002762 sub->resource =
2763 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764 if (!sub->resource) {
2765 free(sub);
2766 return NULL;
2767 }
2768
Jason Ekstranda85118c2013-06-27 20:17:02 -05002769 wl_resource_set_implementation(sub->resource,
2770 &subsurface_implementation,
2771 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002772 weston_subsurface_link_surface(sub, surface);
2773 weston_subsurface_link_parent(sub, parent);
2774 weston_subsurface_cache_init(sub);
2775 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002776
2777 return sub;
2778}
2779
2780/* Create a dummy subsurface for having the parent itself in its
2781 * sub-surface lists. Makes stacking order manipulation easy.
2782 */
2783static struct weston_subsurface *
2784weston_subsurface_create_for_parent(struct weston_surface *parent)
2785{
2786 struct weston_subsurface *sub;
2787
2788 sub = calloc(1, sizeof *sub);
2789 if (!sub)
2790 return NULL;
2791
2792 weston_subsurface_link_surface(sub, parent);
2793 sub->parent = parent;
2794 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2795 wl_list_insert(&parent->subsurface_list_pending,
2796 &sub->parent_link_pending);
2797
2798 return sub;
2799}
2800
2801static void
2802subcompositor_get_subsurface(struct wl_client *client,
2803 struct wl_resource *resource,
2804 uint32_t id,
2805 struct wl_resource *surface_resource,
2806 struct wl_resource *parent_resource)
2807{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002808 struct weston_surface *surface =
2809 wl_resource_get_user_data(surface_resource);
2810 struct weston_surface *parent =
2811 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002812 struct weston_subsurface *sub;
2813 static const char where[] = "get_subsurface: wl_subsurface@";
2814
2815 if (surface == parent) {
2816 wl_resource_post_error(resource,
2817 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2818 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002819 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002820 return;
2821 }
2822
2823 if (weston_surface_to_subsurface(surface)) {
2824 wl_resource_post_error(resource,
2825 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2826 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002827 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828 return;
2829 }
2830
2831 if (surface->configure) {
2832 wl_resource_post_error(resource,
2833 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2834 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002835 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836 return;
2837 }
2838
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002839 if (weston_surface_get_main_surface(parent) == surface) {
2840 wl_resource_post_error(resource,
2841 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2842 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002843 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002844 return;
2845 }
2846
Pekka Paalanene67858b2013-04-25 13:57:42 +03002847 /* make sure the parent is in its own list */
2848 if (wl_list_empty(&parent->subsurface_list)) {
2849 if (!weston_subsurface_create_for_parent(parent)) {
2850 wl_resource_post_no_memory(resource);
2851 return;
2852 }
2853 }
2854
2855 sub = weston_subsurface_create(id, surface, parent);
2856 if (!sub) {
2857 wl_resource_post_no_memory(resource);
2858 return;
2859 }
2860
2861 surface->configure = subsurface_configure;
2862 surface->configure_private = sub;
2863}
2864
2865static void
2866subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2867{
2868 wl_resource_destroy(resource);
2869}
2870
2871static const struct wl_subcompositor_interface subcompositor_interface = {
2872 subcompositor_destroy,
2873 subcompositor_get_subsurface
2874};
2875
2876static void
2877bind_subcompositor(struct wl_client *client,
2878 void *data, uint32_t version, uint32_t id)
2879{
2880 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002881 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002882
Jason Ekstranda85118c2013-06-27 20:17:02 -05002883 resource =
2884 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002885 if (resource == NULL) {
2886 wl_client_post_no_memory(client);
2887 return;
2888 }
2889 wl_resource_set_implementation(resource, &subcompositor_interface,
2890 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002891}
2892
2893static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002894weston_compositor_dpms(struct weston_compositor *compositor,
2895 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002896{
2897 struct weston_output *output;
2898
2899 wl_list_for_each(output, &compositor->output_list, link)
2900 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002901 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002902}
2903
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002904WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002905weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002906{
Neil Roberts8b62e202013-09-30 13:14:47 +01002907 uint32_t old_state = compositor->state;
2908
2909 /* The state needs to be changed before emitting the wake
2910 * signal because that may try to schedule a repaint which
2911 * will not work if the compositor is still sleeping */
2912 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2913
2914 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002915 case WESTON_COMPOSITOR_SLEEPING:
2916 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2917 /* fall through */
2918 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002919 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002920 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002921 /* fall through */
2922 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002923 wl_event_source_timer_update(compositor->idle_source,
2924 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002925 }
2926}
2927
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002928WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002929weston_compositor_offscreen(struct weston_compositor *compositor)
2930{
2931 switch (compositor->state) {
2932 case WESTON_COMPOSITOR_OFFSCREEN:
2933 return;
2934 case WESTON_COMPOSITOR_SLEEPING:
2935 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2936 /* fall through */
2937 default:
2938 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2939 wl_event_source_timer_update(compositor->idle_source, 0);
2940 }
2941}
2942
2943WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002944weston_compositor_sleep(struct weston_compositor *compositor)
2945{
2946 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2947 return;
2948
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002949 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002950 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2951 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2952}
2953
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002954static int
2955idle_handler(void *data)
2956{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002957 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002958
2959 if (compositor->idle_inhibit)
2960 return 1;
2961
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002962 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002963 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002964
2965 return 1;
2966}
2967
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002968WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002969weston_plane_init(struct weston_plane *plane,
2970 struct weston_compositor *ec,
2971 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002972{
2973 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002974 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002975 plane->x = x;
2976 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002977 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002978
2979 /* Init the link so that the call to wl_list_remove() when releasing
2980 * the plane without ever stacking doesn't lead to a crash */
2981 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002982}
2983
2984WL_EXPORT void
2985weston_plane_release(struct weston_plane *plane)
2986{
Xiong Zhang97116532013-10-23 13:58:31 +08002987 struct weston_view *view;
2988
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002989 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002990 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002991
Xiong Zhang97116532013-10-23 13:58:31 +08002992 wl_list_for_each(view, &plane->compositor->view_list, link) {
2993 if (view->plane == plane)
2994 view->plane = NULL;
2995 }
2996
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002997 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002998}
2999
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003000WL_EXPORT void
3001weston_compositor_stack_plane(struct weston_compositor *ec,
3002 struct weston_plane *plane,
3003 struct weston_plane *above)
3004{
3005 if (above)
3006 wl_list_insert(above->link.prev, &plane->link);
3007 else
3008 wl_list_insert(&ec->plane_list, &plane->link);
3009}
3010
Casey Dahlin9074db52012-04-19 22:50:09 -04003011static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003012{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003013 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003014}
3015
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003016static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003017bind_output(struct wl_client *client,
3018 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003019{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003020 struct weston_output *output = data;
3021 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003022 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003023
Jason Ekstranda85118c2013-06-27 20:17:02 -05003024 resource = wl_resource_create(client, &wl_output_interface,
3025 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003026 if (resource == NULL) {
3027 wl_client_post_no_memory(client);
3028 return;
3029 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003030
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003031 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003032 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003033
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003034 wl_output_send_geometry(resource,
3035 output->x,
3036 output->y,
3037 output->mm_width,
3038 output->mm_height,
3039 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003040 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003041 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003042 if (version >= 2)
3043 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003044 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003045
3046 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003047 wl_output_send_mode(resource,
3048 mode->flags,
3049 mode->width,
3050 mode->height,
3051 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003052 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003053
3054 if (version >= 2)
3055 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003056}
3057
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003058/* Move other outputs when one is removed so the space remains contiguos. */
3059static void
3060weston_compositor_remove_output(struct weston_compositor *compositor,
3061 struct weston_output *remove_output)
3062{
3063 struct weston_output *output;
3064 int offset = 0;
3065
3066 wl_list_for_each(output, &compositor->output_list, link) {
3067 if (output == remove_output) {
3068 offset = output->width;
3069 continue;
3070 }
3071
3072 if (offset > 0) {
3073 weston_output_move(output,
3074 output->x - offset, output->y);
3075 output->dirty = 1;
3076 }
3077 }
3078}
3079
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003080WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003081weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003082{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003083 output->destroying = 1;
3084
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003085 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003086 wl_list_remove(&output->link);
3087
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003088 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003089 wl_signal_emit(&output->destroy_signal, output);
3090
Richard Hughesafe690c2013-05-02 10:10:04 +01003091 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003092 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003093 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003094 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003095
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003096 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003097}
3098
Scott Moreau1bad5db2012-08-18 01:04:05 -06003099static void
3100weston_output_compute_transform(struct weston_output *output)
3101{
3102 struct weston_matrix transform;
3103 int flip;
3104
3105 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003106 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003107
3108 switch(output->transform) {
3109 case WL_OUTPUT_TRANSFORM_FLIPPED:
3110 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3111 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3112 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003113 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003114 flip = -1;
3115 break;
3116 default:
3117 flip = 1;
3118 break;
3119 }
3120
3121 switch(output->transform) {
3122 case WL_OUTPUT_TRANSFORM_NORMAL:
3123 case WL_OUTPUT_TRANSFORM_FLIPPED:
3124 transform.d[0] = flip;
3125 transform.d[1] = 0;
3126 transform.d[4] = 0;
3127 transform.d[5] = 1;
3128 break;
3129 case WL_OUTPUT_TRANSFORM_90:
3130 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3131 transform.d[0] = 0;
3132 transform.d[1] = -flip;
3133 transform.d[4] = 1;
3134 transform.d[5] = 0;
3135 break;
3136 case WL_OUTPUT_TRANSFORM_180:
3137 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3138 transform.d[0] = -flip;
3139 transform.d[1] = 0;
3140 transform.d[4] = 0;
3141 transform.d[5] = -1;
3142 break;
3143 case WL_OUTPUT_TRANSFORM_270:
3144 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3145 transform.d[0] = 0;
3146 transform.d[1] = flip;
3147 transform.d[4] = -1;
3148 transform.d[5] = 0;
3149 break;
3150 default:
3151 break;
3152 }
3153
3154 weston_matrix_multiply(&output->matrix, &transform);
3155}
3156
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003157WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003158weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003159{
Scott Moreau850ca422012-05-21 15:21:25 -06003160 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003161 struct weston_matrix camera;
3162 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003163
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003164 weston_matrix_init(&output->matrix);
3165 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003166 -(output->x + output->width / 2.0),
3167 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003168
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003169 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003170 2.0 / output->width,
3171 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003172
3173 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003174
Scott Moreauccbf29d2012-02-22 14:21:41 -07003175 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003176 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003177 weston_matrix_init(&camera);
3178 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003179 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003180 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003181 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003182 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003183 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003184 weston_matrix_multiply(&output->matrix, &modelview);
3185 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003186
Scott Moreauccbf29d2012-02-22 14:21:41 -07003187 output->dirty = 0;
3188}
3189
Scott Moreau1bad5db2012-08-18 01:04:05 -06003190static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003191weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003192{
3193 output->transform = transform;
3194
3195 switch (transform) {
3196 case WL_OUTPUT_TRANSFORM_90:
3197 case WL_OUTPUT_TRANSFORM_270:
3198 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3199 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3200 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003201 output->width = output->current_mode->height;
3202 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003203 break;
3204 case WL_OUTPUT_TRANSFORM_NORMAL:
3205 case WL_OUTPUT_TRANSFORM_180:
3206 case WL_OUTPUT_TRANSFORM_FLIPPED:
3207 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003208 output->width = output->current_mode->width;
3209 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003210 break;
3211 default:
3212 break;
3213 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003214
Hardening57388e42013-09-18 23:56:36 +02003215 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003216 output->width /= scale;
3217 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003218}
3219
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003220static void
3221weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003222{
3223 output->x = x;
3224 output->y = y;
3225
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003226 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003227 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003228 output->width,
3229 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003230}
3231
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003232WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003233weston_output_move(struct weston_output *output, int x, int y)
3234{
3235 pixman_region32_t old_region;
3236 struct wl_resource *resource;
3237
3238 output->move_x = x - output->x;
3239 output->move_y = y - output->y;
3240
3241 if (output->move_x == 0 && output->move_y == 0)
3242 return;
3243
3244 pixman_region32_init(&old_region);
3245 pixman_region32_copy(&old_region, &output->region);
3246
3247 weston_output_init_geometry(output, x, y);
3248
3249 output->dirty = 1;
3250
3251 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003252 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003253
3254 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003255 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003256 wl_output_send_geometry(resource,
3257 output->x,
3258 output->y,
3259 output->mm_width,
3260 output->mm_height,
3261 output->subpixel,
3262 output->make,
3263 output->model,
3264 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003265
3266 if (wl_resource_get_version(resource) >= 2)
3267 wl_output_send_done(resource);
3268 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003269}
3270
3271WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003272weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003273 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003274 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003275{
3276 output->compositor = c;
3277 output->x = x;
3278 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003279 output->mm_width = mm_width;
3280 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003281 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003282 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003283
Alexander Larsson0b135062013-05-28 16:23:36 +02003284 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003285 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003286
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003287 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003288 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003289
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003290 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003291 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003292 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003293 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003294
Casey Dahlin58ba1372012-04-19 22:50:08 -04003295 output->id = ffs(~output->compositor->output_id_pool) - 1;
3296 output->compositor->output_id_pool |= 1 << output->id;
3297
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003298 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003299 wl_global_create(c->wl_display, &wl_output_interface, 2,
3300 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003301 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003302}
3303
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003304WL_EXPORT void
3305weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003306 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003307 wl_fixed_t *x, wl_fixed_t *y)
3308{
3309 wl_fixed_t tx, ty;
3310 wl_fixed_t width, height;
3311
Hardeningff39efa2013-09-18 23:56:35 +02003312 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3313 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003314
3315 switch(output->transform) {
3316 case WL_OUTPUT_TRANSFORM_NORMAL:
3317 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003318 tx = device_x;
3319 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003320 break;
3321 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003322 tx = device_y;
3323 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003324 break;
3325 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003326 tx = width - device_x;
3327 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003328 break;
3329 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003330 tx = width - device_y;
3331 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003332 break;
3333 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003334 tx = width - device_x;
3335 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003336 break;
3337 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003338 tx = width - device_y;
3339 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003340 break;
3341 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003342 tx = device_x;
3343 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003344 break;
3345 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003346 tx = device_y;
3347 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003348 break;
3349 }
3350
Hardeningff39efa2013-09-18 23:56:35 +02003351 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3352 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003353}
3354
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003355static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003356destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003357{
Jonny Lamb74130762013-11-26 18:19:46 +01003358 struct weston_surface *surface =
3359 wl_resource_get_user_data(resource);
3360
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003361 surface->viewport_resource = NULL;
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003362 surface->pending.buffer_viewport.buffer.viewport_set = 0;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003363}
3364
3365static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003366viewport_destroy(struct wl_client *client,
3367 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003368{
3369 wl_resource_destroy(resource);
3370}
3371
3372static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003373viewport_set(struct wl_client *client,
3374 struct wl_resource *resource,
3375 wl_fixed_t src_x,
3376 wl_fixed_t src_y,
3377 wl_fixed_t src_width,
3378 wl_fixed_t src_height,
3379 int32_t dst_width,
3380 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003381{
Jonny Lamb74130762013-11-26 18:19:46 +01003382 struct weston_surface *surface =
3383 wl_resource_get_user_data(resource);
3384
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003385 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003386
Jonny Lamb8ae35902013-11-26 18:19:45 +01003387 if (wl_fixed_to_double(src_width) < 0 ||
3388 wl_fixed_to_double(src_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 "source dimensions must be non-negative (%fx%f)",
3392 wl_fixed_to_double(src_width),
3393 wl_fixed_to_double(src_height));
3394 return;
3395 }
3396
3397 if (dst_width <= 0 || dst_height <= 0) {
3398 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003399 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003400 "destination dimensions must be positive (%dx%d)",
3401 dst_width, dst_height);
3402 return;
3403 }
3404
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003405 surface->pending.buffer_viewport.buffer.viewport_set = 1;
Jonny Lamb74130762013-11-26 18:19:46 +01003406
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003407 surface->pending.buffer_viewport.buffer.src_x = src_x;
3408 surface->pending.buffer_viewport.buffer.src_y = src_y;
3409 surface->pending.buffer_viewport.buffer.src_width = src_width;
3410 surface->pending.buffer_viewport.buffer.src_height = src_height;
3411 surface->pending.buffer_viewport.surface.width = dst_width;
3412 surface->pending.buffer_viewport.surface.height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003413}
3414
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003415static const struct wl_viewport_interface viewport_interface = {
3416 viewport_destroy,
3417 viewport_set
Jonny Lamb8ae35902013-11-26 18:19:45 +01003418};
3419
3420static void
3421scaler_destroy(struct wl_client *client,
3422 struct wl_resource *resource)
3423{
3424 wl_resource_destroy(resource);
3425}
3426
3427static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003428scaler_get_viewport(struct wl_client *client,
3429 struct wl_resource *scaler,
3430 uint32_t id,
3431 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003432{
3433 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3434 struct wl_resource *resource;
3435
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003436 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003437 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003438 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3439 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003440 return;
3441 }
3442
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003443 resource = wl_resource_create(client, &wl_viewport_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003444 1, id);
3445 if (resource == NULL) {
3446 wl_client_post_no_memory(client);
3447 return;
3448 }
3449
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003450 wl_resource_set_implementation(resource, &viewport_interface,
3451 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003452
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003453 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003454}
3455
3456static const struct wl_scaler_interface scaler_interface = {
3457 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003458 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003459};
3460
3461static void
3462bind_scaler(struct wl_client *client,
3463 void *data, uint32_t version, uint32_t id)
3464{
3465 struct wl_resource *resource;
3466
3467 resource = wl_resource_create(client, &wl_scaler_interface,
3468 1, id);
3469 if (resource == NULL) {
3470 wl_client_post_no_memory(client);
3471 return;
3472 }
3473
3474 wl_resource_set_implementation(resource, &scaler_interface,
3475 NULL, NULL);
3476}
3477
3478static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003479compositor_bind(struct wl_client *client,
3480 void *data, uint32_t version, uint32_t id)
3481{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003482 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003483 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003484
Jason Ekstranda85118c2013-06-27 20:17:02 -05003485 resource = wl_resource_create(client, &wl_compositor_interface,
3486 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003487 if (resource == NULL) {
3488 wl_client_post_no_memory(client);
3489 return;
3490 }
3491
3492 wl_resource_set_implementation(resource, &compositor_interface,
3493 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003494}
3495
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003496static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003497log_uname(void)
3498{
3499 struct utsname usys;
3500
3501 uname(&usys);
3502
3503 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3504 usys.version, usys.machine);
3505}
3506
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003507WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003508weston_environment_get_fd(const char *env)
3509{
3510 char *e, *end;
3511 int fd, flags;
3512
3513 e = getenv(env);
3514 if (!e)
3515 return -1;
3516 fd = strtol(e, &end, 0);
3517 if (*end != '\0')
3518 return -1;
3519
3520 flags = fcntl(fd, F_GETFD);
3521 if (flags == -1)
3522 return -1;
3523
3524 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3525 unsetenv(env);
3526
3527 return fd;
3528}
3529
3530WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003531weston_compositor_init(struct weston_compositor *ec,
3532 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003533 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003534 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003535{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003536 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003537 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003538 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003539
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003540 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003541 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003542 wl_signal_init(&ec->destroy_signal);
3543 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003544 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003545 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003546 wl_signal_init(&ec->idle_signal);
3547 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003548 wl_signal_init(&ec->show_input_panel_signal);
3549 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003550 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003551 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003552 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003553 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003554 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003555 wl_signal_init(&ec->session_signal);
3556 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003557
Casey Dahlin58ba1372012-04-19 22:50:08 -04003558 ec->output_id_pool = 0;
3559
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003560 if (!wl_global_create(display, &wl_compositor_interface, 3,
3561 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003562 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003563
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003564 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3565 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003566 return -1;
3567
Jonny Lamb8ae35902013-11-26 18:19:45 +01003568 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3569 ec, bind_scaler))
3570 return -1;
3571
Jason Ekstranda7af7042013-10-12 22:38:11 -05003572 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003573 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003574 wl_list_init(&ec->layer_list);
3575 wl_list_init(&ec->seat_list);
3576 wl_list_init(&ec->output_list);
3577 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003578 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003579 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003580 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003581 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003582 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003583
Xiong Zhang97116532013-10-23 13:58:31 +08003584 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003585 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003586
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003587 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3588 weston_config_section_get_string(s, "keymap_rules",
3589 (char **) &xkb_names.rules, NULL);
3590 weston_config_section_get_string(s, "keymap_model",
3591 (char **) &xkb_names.model, NULL);
3592 weston_config_section_get_string(s, "keymap_layout",
3593 (char **) &xkb_names.layout, NULL);
3594 weston_config_section_get_string(s, "keymap_variant",
3595 (char **) &xkb_names.variant, NULL);
3596 weston_config_section_get_string(s, "keymap_options",
3597 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003598
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003599 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3600 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003601
Daniel Stone725c2c32012-06-22 14:04:36 +01003602 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003603 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003604
3605 wl_data_device_manager_init(ec->wl_display);
3606
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003607 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003608
Daniel Stone725c2c32012-06-22 14:04:36 +01003609 loop = wl_display_get_event_loop(ec->wl_display);
3610 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3611 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3612
3613 ec->input_loop = wl_event_loop_create();
3614
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003615 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3616 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3617
3618 weston_compositor_schedule_repaint(ec);
3619
Daniel Stone725c2c32012-06-22 14:04:36 +01003620 return 0;
3621}
3622
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003623WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003624weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003625{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003626 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003627
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003628 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003629 if (ec->input_loop_source)
3630 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003631
Matt Roper361d2ad2011-08-29 13:52:23 -07003632 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003633 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003634 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003635
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003636 if (ec->renderer)
3637 ec->renderer->destroy(ec);
3638
Daniel Stone325fc2d2012-05-30 16:31:58 +01003639 weston_binding_list_destroy_all(&ec->key_binding_list);
3640 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003641 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003642 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003643 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003644
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003645 weston_plane_release(&ec->primary_plane);
3646
Jonas Ådahlc97af922012-03-28 22:36:09 +02003647 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003648
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003649 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003650}
3651
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003652WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003653weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3654 const struct weston_pointer_grab_interface *interface)
3655{
3656 struct weston_seat *seat;
3657
3658 ec->default_pointer_grab = interface;
3659 wl_list_for_each(seat, &ec->seat_list, link) {
3660 if (seat->pointer) {
3661 weston_pointer_set_default_grab(seat->pointer,
3662 interface);
3663 }
3664 }
3665}
3666
3667WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003668weston_version(int *major, int *minor, int *micro)
3669{
3670 *major = WESTON_VERSION_MAJOR;
3671 *minor = WESTON_VERSION_MINOR;
3672 *micro = WESTON_VERSION_MICRO;
3673}
3674
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003675static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003676 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003677 const char *desc;
3678} capability_strings[] = {
3679 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003680 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003681};
3682
3683static void
3684weston_compositor_log_capabilities(struct weston_compositor *compositor)
3685{
3686 unsigned i;
3687 int yes;
3688
3689 weston_log("Compositor capabilities:\n");
3690 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3691 yes = compositor->capabilities & capability_strings[i].bit;
3692 weston_log_continue(STAMP_SPACE "%s %s\n",
3693 capability_strings[i].desc,
3694 yes ? "yes" : "no");
3695 }
3696}
3697
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003698static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003699{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003700 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003701
Martin Minarik6d118362012-06-07 18:01:59 +02003702 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003703 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003704
3705 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003706}
3707
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003708#ifdef HAVE_LIBUNWIND
3709
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003710static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003711print_backtrace(void)
3712{
3713 unw_cursor_t cursor;
3714 unw_context_t context;
3715 unw_word_t off;
3716 unw_proc_info_t pip;
3717 int ret, i = 0;
3718 char procname[256];
3719 const char *filename;
3720 Dl_info dlinfo;
3721
3722 pip.unwind_info = NULL;
3723 ret = unw_getcontext(&context);
3724 if (ret) {
3725 weston_log("unw_getcontext: %d\n", ret);
3726 return;
3727 }
3728
3729 ret = unw_init_local(&cursor, &context);
3730 if (ret) {
3731 weston_log("unw_init_local: %d\n", ret);
3732 return;
3733 }
3734
3735 ret = unw_step(&cursor);
3736 while (ret > 0) {
3737 ret = unw_get_proc_info(&cursor, &pip);
3738 if (ret) {
3739 weston_log("unw_get_proc_info: %d\n", ret);
3740 break;
3741 }
3742
3743 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3744 if (ret && ret != -UNW_ENOMEM) {
3745 if (ret != -UNW_EUNSPEC)
3746 weston_log("unw_get_proc_name: %d\n", ret);
3747 procname[0] = '?';
3748 procname[1] = 0;
3749 }
3750
3751 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3752 *dlinfo.dli_fname)
3753 filename = dlinfo.dli_fname;
3754 else
3755 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003756
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003757 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3758 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3759
3760 ret = unw_step(&cursor);
3761 if (ret < 0)
3762 weston_log("unw_step: %d\n", ret);
3763 }
3764}
3765
3766#else
3767
3768static void
3769print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003770{
3771 void *buffer[32];
3772 int i, count;
3773 Dl_info info;
3774
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003775 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3776 for (i = 0; i < count; i++) {
3777 dladdr(buffer[i], &info);
3778 weston_log(" [%016lx] %s (%s)\n",
3779 (long) buffer[i],
3780 info.dli_sname ? info.dli_sname : "--",
3781 info.dli_fname);
3782 }
3783}
3784
3785#endif
3786
3787static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003788on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003789{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003790 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003791 * then call the backend restore function, which will switch
3792 * back to the vt we launched from or ungrab X etc and then
3793 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003794 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003795 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003796 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003797
Peter Maatmane5b42e42013-03-27 22:38:53 +01003798 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003799
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003800 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003801
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003802 segv_compositor->restore(segv_compositor);
3803
3804 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003805}
3806
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003807WL_EXPORT void *
3808weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003809{
3810 char path[PATH_MAX];
3811 void *module, *init;
3812
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003813 if (name == NULL)
3814 return NULL;
3815
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003816 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003817 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003818 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003819 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003820
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003821 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3822 if (module) {
3823 weston_log("Module '%s' already loaded\n", path);
3824 dlclose(module);
3825 return NULL;
3826 }
3827
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003828 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003829 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003830 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003831 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003832 return NULL;
3833 }
3834
3835 init = dlsym(module, entrypoint);
3836 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003837 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003838 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003839 return NULL;
3840 }
3841
3842 return init;
3843}
3844
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003845static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003846load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003847 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003848{
3849 const char *p, *end;
3850 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003851 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003852 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003853
3854 if (modules == NULL)
3855 return 0;
3856
3857 p = modules;
3858 while (*p) {
3859 end = strchrnul(p, ',');
3860 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003861 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003862 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003863 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003864 p = end;
3865 while (*p == ',')
3866 p++;
3867
3868 }
3869
3870 return 0;
3871}
3872
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003873static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003874 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3875
3876static const char xdg_wrong_message[] =
3877 "fatal: environment variable XDG_RUNTIME_DIR\n"
3878 "is set to \"%s\", which is not a directory.\n";
3879
3880static const char xdg_wrong_mode_message[] =
3881 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01003882 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
3883 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003884
3885static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003886 "Refer to your distribution on how to get it, or\n"
3887 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3888 "on how to implement it.\n";
3889
Martin Minarik37032f82012-06-18 20:15:18 +02003890static void
3891verify_xdg_runtime_dir(void)
3892{
3893 char *dir = getenv("XDG_RUNTIME_DIR");
3894 struct stat s;
3895
3896 if (!dir) {
3897 weston_log(xdg_error_message);
3898 weston_log_continue(xdg_detail_message);
3899 exit(EXIT_FAILURE);
3900 }
3901
3902 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3903 weston_log(xdg_wrong_message, dir);
3904 weston_log_continue(xdg_detail_message);
3905 exit(EXIT_FAILURE);
3906 }
3907
3908 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3909 weston_log(xdg_wrong_mode_message,
3910 dir, s.st_mode & 0777, s.st_uid);
3911 weston_log_continue(xdg_detail_message);
3912 }
3913}
3914
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003915static int
3916usage(int error_code)
3917{
3918 fprintf(stderr,
3919 "Usage: weston [OPTIONS]\n\n"
3920 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3921 "Weston supports multiple backends, and depending on which backend is in use\n"
3922 "different options will be accepted.\n\n"
3923
3924
3925 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003926 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003927 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003928 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3929 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003930 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003931 " -S, --socket=NAME\tName of socket to listen on\n"
3932 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003933 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003934 " --log==FILE\t\tLog to the given file\n"
3935 " -h, --help\t\tThis help message\n\n");
3936
3937 fprintf(stderr,
3938 "Options for drm-backend.so:\n\n"
3939 " --connector=ID\tBring up only this connector\n"
3940 " --seat=SEAT\t\tThe seat that weston should run on\n"
3941 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003942 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003943 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3944
3945 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003946 "Options for fbdev-backend.so:\n\n"
3947 " --tty=TTY\t\tThe tty to use\n"
3948 " --device=DEVICE\tThe framebuffer device to use\n\n");
3949
3950 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003951 "Options for x11-backend.so:\n\n"
3952 " --width=WIDTH\t\tWidth of X window\n"
3953 " --height=HEIGHT\tHeight of X window\n"
3954 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003955 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003956 " --output-count=COUNT\tCreate multiple outputs\n"
3957 " --no-input\t\tDont create input devices\n\n");
3958
3959 fprintf(stderr,
3960 "Options for wayland-backend.so:\n\n"
3961 " --width=WIDTH\t\tWidth of Wayland surface\n"
3962 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003963 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003964 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003965 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003966 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003967 " --display=DISPLAY\tWayland display to connect to\n\n");
3968
Pekka Paalanene31e0532013-05-22 18:03:07 +03003969#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3970 fprintf(stderr,
3971 "Options for rpi-backend.so:\n\n"
3972 " --tty=TTY\t\tThe tty to use\n"
3973 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3974 " --transform=TR\tThe output transformation, TR is one of:\n"
3975 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003976 " --opaque-regions\tEnable support for opaque regions, can be "
3977 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003978 "\n");
3979#endif
3980
Hardeningc077a842013-07-08 00:51:35 +02003981#if defined(BUILD_RDP_COMPOSITOR)
3982 fprintf(stderr,
3983 "Options for rdp-backend.so:\n\n"
3984 " --width=WIDTH\t\tWidth of desktop\n"
3985 " --height=HEIGHT\tHeight of desktop\n"
3986 " --extra-modes=MODES\t\n"
3987 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3988 " --address=ADDR\tThe address to bind\n"
3989 " --port=PORT\tThe port to listen on\n"
3990 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3991 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3992 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3993 "\n");
3994#endif
3995
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003996 exit(error_code);
3997}
3998
Peter Maatmane5b42e42013-03-27 22:38:53 +01003999static void
4000catch_signals(void)
4001{
4002 struct sigaction action;
4003
4004 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4005 action.sa_sigaction = on_caught_signal;
4006 sigemptyset(&action.sa_mask);
4007 sigaction(SIGSEGV, &action, NULL);
4008 sigaction(SIGABRT, &action, NULL);
4009}
4010
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004011int main(int argc, char *argv[])
4012{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004013 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004014 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004015 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004016 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004017 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004018 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004019 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004020 int *argc, char *argv[],
4021 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004022 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004023 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004024 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004025 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004026 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004027 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004028 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004029 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004030 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004031 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004032 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004033 struct weston_config *config;
4034 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004035
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004036 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004037 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004038 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004039 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4040 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004041 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004042 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004043 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004044 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004045 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004046
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004047 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004048
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004049 if (help)
4050 usage(EXIT_SUCCESS);
4051
Scott Moreau12245142012-08-29 15:15:58 -06004052 if (version) {
4053 printf(PACKAGE_STRING "\n");
4054 return EXIT_SUCCESS;
4055 }
4056
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004057 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004058
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004059 weston_log("%s\n"
4060 STAMP_SPACE "%s\n"
4061 STAMP_SPACE "Bug reports to: %s\n"
4062 STAMP_SPACE "Build: %s\n",
4063 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004064 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004065 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004066
Martin Minarik37032f82012-06-18 20:15:18 +02004067 verify_xdg_runtime_dir();
4068
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004069 display = wl_display_create();
4070
Tiago Vignatti2116b892011-08-08 05:52:59 -07004071 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004072 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4073 display);
4074 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4075 display);
4076 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4077 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004078
4079 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004080 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4081 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004082
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004083 config = weston_config_parse("weston.ini");
4084 if (config != NULL) {
4085 weston_log("Using config file '%s'\n",
4086 weston_config_get_full_path(config));
4087 } else {
4088 weston_log("Starting with no config file.\n");
4089 }
4090 section = weston_config_get_section(config, "core", NULL, NULL);
4091
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004092 if (option_backend)
4093 backend = strdup(option_backend);
4094 else
4095 weston_config_section_get_string(section, "backend", &backend,
4096 NULL);
4097
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004098 if (!backend) {
4099 if (getenv("WAYLAND_DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004100 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004101 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004102 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004103 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004104 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004105 }
4106
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004107 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004108 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004109 if (!backend_init)
4110 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004111
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004112 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004113 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004114 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004115 exit(EXIT_FAILURE);
4116 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004117
Peter Maatmane5b42e42013-03-27 22:38:53 +01004118 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004119 segv_compositor = ec;
4120
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004121 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004122 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004123
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004124 setenv("WAYLAND_DISPLAY", socket_name, 1);
4125
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004126 if (option_shell)
4127 shell = strdup(option_shell);
4128 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004129 weston_config_section_get_string(section, "shell", &shell,
4130 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004131
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004132 if (load_modules(ec, shell, &argc, argv) < 0) {
4133 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004134 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004135 }
4136 free(shell);
4137
4138 weston_config_section_get_string(section, "modules", &modules, "");
4139 if (load_modules(ec, modules, &argc, argv) < 0) {
4140 free(modules);
4141 goto out;
4142 }
4143 free(modules);
4144
Ossama Othmana50e6e42013-05-14 09:48:26 -07004145 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004146 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004147
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004148 for (i = 1; i < argc; i++)
4149 weston_log("fatal: unhandled option: %s\n", argv[i]);
4150 if (argc > 1) {
4151 ret = EXIT_FAILURE;
4152 goto out;
4153 }
4154
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004155 weston_compositor_log_capabilities(ec);
4156
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004157 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004158 weston_log("fatal: failed to add socket: %m\n");
4159 ret = EXIT_FAILURE;
4160 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004161 }
4162
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004163 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004164
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004165 wl_display_run(display);
4166
4167 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004168 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004169 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004170
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004171 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004172
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004173 for (i = ARRAY_LENGTH(signals); i;)
4174 wl_event_source_remove(signals[--i]);
4175
Daniel Stone855028f2012-05-01 20:37:10 +01004176 weston_compositor_xkb_destroy(ec);
4177
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004178 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004179 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004180
Martin Minarik19e6f262012-06-07 13:08:46 +02004181 weston_log_file_close();
4182
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004183 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004184}