blob: d414e274e7b42fe9c637c31b8a3414972aba110c [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
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300333region_init_infinite(pixman_region32_t *region)
334{
335 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
336 UINT32_MAX, UINT32_MAX);
337}
338
Pekka Paalanene67858b2013-04-25 13:57:42 +0300339static struct weston_subsurface *
340weston_surface_to_subsurface(struct weston_surface *surface);
341
Jason Ekstranda7af7042013-10-12 22:38:11 -0500342WL_EXPORT struct weston_view *
343weston_view_create(struct weston_surface *surface)
344{
345 struct weston_view *view;
346
347 view = calloc(1, sizeof *view);
348 if (view == NULL)
349 return NULL;
350
351 view->surface = surface;
352
Jason Ekstranda7af7042013-10-12 22:38:11 -0500353 /* Assign to surface */
354 wl_list_insert(&surface->views, &view->surface_link);
355
356 wl_signal_init(&view->destroy_signal);
357 wl_list_init(&view->link);
358 wl_list_init(&view->layer_link);
359
Xiong Zhang97116532013-10-23 13:58:31 +0800360 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500361
362 pixman_region32_init(&view->clip);
363
364 view->alpha = 1.0;
365 pixman_region32_init(&view->transform.opaque);
366
367 wl_list_init(&view->geometry.transformation_list);
368 wl_list_insert(&view->geometry.transformation_list,
369 &view->transform.position.link);
370 weston_matrix_init(&view->transform.position.matrix);
371 wl_list_init(&view->geometry.child_list);
372 pixman_region32_init(&view->transform.boundingbox);
373 view->transform.dirty = 1;
374
375 view->output = NULL;
376
377 return view;
378}
379
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500380WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500381weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500382{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500383 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400384
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200385 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400386 if (surface == NULL)
387 return NULL;
388
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500389 wl_signal_init(&surface->destroy_signal);
390
391 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500392
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500393 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200394 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400395
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200396 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
397 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200398 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
399 surface->buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +0200400 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100401 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400402 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100403 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200404
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200405 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100406
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400407 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500408 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300409 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400410
Jason Ekstranda7af7042013-10-12 22:38:11 -0500411 wl_list_init(&surface->views);
412
413 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500414
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300415 surface->pending.buffer_destroy_listener.notify =
416 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300417 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300418 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300419 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300420 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300421
Pekka Paalanene67858b2013-04-25 13:57:42 +0300422 wl_list_init(&surface->subsurface_list);
423 wl_list_init(&surface->subsurface_list_pending);
424
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400425 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500426}
427
Alex Wu8811bf92012-02-28 18:07:54 +0800428WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500429weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200430 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500431{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100432 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500433}
434
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400435WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500436weston_view_to_global_float(struct weston_view *view,
437 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200438{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500439 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200440 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
441
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200443
444 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200445 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700446 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200447 v.f[3]);
448 *x = 0;
449 *y = 0;
450 return;
451 }
452
453 *x = v.f[0] / v.f[3];
454 *y = v.f[1] / v.f[3];
455 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500456 *x = sx + view->geometry.x;
457 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200458 }
459}
460
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500461WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200462weston_transformed_coord(int width, int height,
463 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200464 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200465 float sx, float sy, float *bx, float *by)
466{
467 switch (transform) {
468 case WL_OUTPUT_TRANSFORM_NORMAL:
469 default:
470 *bx = sx;
471 *by = sy;
472 break;
473 case WL_OUTPUT_TRANSFORM_FLIPPED:
474 *bx = width - sx;
475 *by = sy;
476 break;
477 case WL_OUTPUT_TRANSFORM_90:
478 *bx = height - sy;
479 *by = sx;
480 break;
481 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
482 *bx = height - sy;
483 *by = width - sx;
484 break;
485 case WL_OUTPUT_TRANSFORM_180:
486 *bx = width - sx;
487 *by = height - sy;
488 break;
489 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
490 *bx = sx;
491 *by = height - sy;
492 break;
493 case WL_OUTPUT_TRANSFORM_270:
494 *bx = sy;
495 *by = width - sx;
496 break;
497 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
498 *bx = sy;
499 *by = sx;
500 break;
501 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200502
503 *bx *= scale;
504 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200505}
506
507WL_EXPORT pixman_box32_t
508weston_transformed_rect(int width, int height,
509 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200510 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200511 pixman_box32_t rect)
512{
513 float x1, x2, y1, y2;
514
515 pixman_box32_t ret;
516
Alexander Larsson4ea95522013-05-22 14:41:37 +0200517 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200518 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200519 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520 rect.x2, rect.y2, &x2, &y2);
521
522 if (x1 <= x2) {
523 ret.x1 = x1;
524 ret.x2 = x2;
525 } else {
526 ret.x1 = x2;
527 ret.x2 = x1;
528 }
529
530 if (y1 <= y2) {
531 ret.y1 = y1;
532 ret.y2 = y2;
533 } else {
534 ret.y1 = y2;
535 ret.y2 = y1;
536 }
537
538 return ret;
539}
540
541WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500542weston_transformed_region(int width, int height,
543 enum wl_output_transform transform,
544 int32_t scale,
545 pixman_region32_t *src, pixman_region32_t *dest)
546{
547 pixman_box32_t *src_rects, *dest_rects;
548 int nrects, i;
549
550 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
551 if (src != dest)
552 pixman_region32_copy(dest, src);
553 return;
554 }
555
556 src_rects = pixman_region32_rectangles(src, &nrects);
557 dest_rects = malloc(nrects * sizeof(*dest_rects));
558 if (!dest_rects)
559 return;
560
561 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
562 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
563 } else {
564 for (i = 0; i < nrects; i++) {
565 switch (transform) {
566 default:
567 case WL_OUTPUT_TRANSFORM_NORMAL:
568 dest_rects[i].x1 = src_rects[i].x1;
569 dest_rects[i].y1 = src_rects[i].y1;
570 dest_rects[i].x2 = src_rects[i].x2;
571 dest_rects[i].y2 = src_rects[i].y2;
572 break;
573 case WL_OUTPUT_TRANSFORM_90:
574 dest_rects[i].x1 = height - src_rects[i].y2;
575 dest_rects[i].y1 = src_rects[i].x1;
576 dest_rects[i].x2 = height - src_rects[i].y1;
577 dest_rects[i].y2 = src_rects[i].x2;
578 break;
579 case WL_OUTPUT_TRANSFORM_180:
580 dest_rects[i].x1 = width - src_rects[i].x2;
581 dest_rects[i].y1 = height - src_rects[i].y2;
582 dest_rects[i].x2 = width - src_rects[i].x1;
583 dest_rects[i].y2 = height - src_rects[i].y1;
584 break;
585 case WL_OUTPUT_TRANSFORM_270:
586 dest_rects[i].x1 = src_rects[i].y1;
587 dest_rects[i].y1 = width - src_rects[i].x2;
588 dest_rects[i].x2 = src_rects[i].y2;
589 dest_rects[i].y2 = width - src_rects[i].x1;
590 break;
591 case WL_OUTPUT_TRANSFORM_FLIPPED:
592 dest_rects[i].x1 = width - src_rects[i].x2;
593 dest_rects[i].y1 = src_rects[i].y1;
594 dest_rects[i].x2 = width - src_rects[i].x1;
595 dest_rects[i].y2 = src_rects[i].y2;
596 break;
597 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
598 dest_rects[i].x1 = height - src_rects[i].y2;
599 dest_rects[i].y1 = width - src_rects[i].x2;
600 dest_rects[i].x2 = height - src_rects[i].y1;
601 dest_rects[i].y2 = width - src_rects[i].x1;
602 break;
603 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
604 dest_rects[i].x1 = src_rects[i].x1;
605 dest_rects[i].y1 = height - src_rects[i].y2;
606 dest_rects[i].x2 = src_rects[i].x2;
607 dest_rects[i].y2 = height - src_rects[i].y1;
608 break;
609 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
610 dest_rects[i].x1 = src_rects[i].y1;
611 dest_rects[i].y1 = src_rects[i].x1;
612 dest_rects[i].x2 = src_rects[i].y2;
613 dest_rects[i].y2 = src_rects[i].x2;
614 break;
615 }
616 }
617 }
618
619 if (scale != 1) {
620 for (i = 0; i < nrects; i++) {
621 dest_rects[i].x1 *= scale;
622 dest_rects[i].x2 *= scale;
623 dest_rects[i].y1 *= scale;
624 dest_rects[i].y2 *= scale;
625 }
626 }
627
628 pixman_region32_clear(dest);
629 pixman_region32_init_rects(dest, dest_rects, nrects);
630 free(dest_rects);
631}
632
Jonny Lamb74130762013-11-26 18:19:46 +0100633static void
634scaler_surface_to_buffer(struct weston_surface *surface,
635 float sx, float sy, float *bx, float *by)
636{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200637 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200638 double src_width, src_height;
639 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200640
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200641 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
642 if (vp->surface.width == -1) {
643 *bx = sx;
644 *by = sy;
645 return;
646 }
Jonny Lamb74130762013-11-26 18:19:46 +0100647
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200648 src_x = 0.0;
649 src_y = 0.0;
650 src_width = surface->width_from_buffer;
651 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100652 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200653 src_x = wl_fixed_to_double(vp->buffer.src_x);
654 src_y = wl_fixed_to_double(vp->buffer.src_y);
655 src_width = wl_fixed_to_double(vp->buffer.src_width);
656 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100657 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200658
659 *bx = sx * src_width / surface->width + src_x;
660 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100661}
662
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500663WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200664weston_surface_to_buffer_float(struct weston_surface *surface,
665 float sx, float sy, float *bx, float *by)
666{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200667 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
668
Jonny Lamb74130762013-11-26 18:19:46 +0100669 /* first transform coordinates if the scaler is set */
670 scaler_surface_to_buffer(surface, sx, sy, bx, by);
671
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500672 weston_transformed_coord(surface->width_from_buffer,
673 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200674 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100675 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200676}
677
Alexander Larsson4ea95522013-05-22 14:41:37 +0200678WL_EXPORT void
679weston_surface_to_buffer(struct weston_surface *surface,
680 int sx, int sy, int *bx, int *by)
681{
682 float bxf, byf;
683
Jonny Lamb74130762013-11-26 18:19:46 +0100684 weston_surface_to_buffer_float(surface,
685 sx, sy, &bxf, &byf);
686
Alexander Larsson4ea95522013-05-22 14:41:37 +0200687 *bx = floorf(bxf);
688 *by = floorf(byf);
689}
690
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200691WL_EXPORT pixman_box32_t
692weston_surface_to_buffer_rect(struct weston_surface *surface,
693 pixman_box32_t rect)
694{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200695 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100696 float xf, yf;
697
698 /* first transform box coordinates if the scaler is set */
699 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
700 rect.x1 = floorf(xf);
701 rect.y1 = floorf(yf);
702
703 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
704 rect.x2 = floorf(xf);
705 rect.y2 = floorf(yf);
706
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500707 return weston_transformed_rect(surface->width_from_buffer,
708 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200709 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200710 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200711}
712
713WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500714weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400715 struct weston_plane *plane)
716{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500717 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400718 return;
719
Jason Ekstranda7af7042013-10-12 22:38:11 -0500720 weston_view_damage_below(view);
721 view->plane = plane;
722 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400723}
724
725WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500726weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200727{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500728 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200729
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500730 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500731 pixman_region32_subtract(&damage, &view->transform.boundingbox,
732 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800733 if (view->plane)
734 pixman_region32_union(&view->plane->damage,
735 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500736 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800737 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200738}
739
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400740static void
741weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
742{
743 uint32_t different = es->output_mask ^ mask;
744 uint32_t entered = mask & different;
745 uint32_t left = es->output_mask & different;
746 struct weston_output *output;
747 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500748 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400749
750 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500751 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400752 return;
753 if (different == 0)
754 return;
755
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500756 client = wl_resource_get_client(es->resource);
757
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400758 wl_list_for_each(output, &es->compositor->output_list, link) {
759 if (1 << output->id & different)
760 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500761 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400762 client);
763 if (resource == NULL)
764 continue;
765 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500766 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400767 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500768 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400769 }
770}
771
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200772
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400773static void
774weston_surface_assign_output(struct weston_surface *es)
775{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500776 struct weston_output *new_output;
777 struct weston_view *view;
778 pixman_region32_t region;
779 uint32_t max, area, mask;
780 pixman_box32_t *e;
781
782 new_output = NULL;
783 max = 0;
784 mask = 0;
785 pixman_region32_init(&region);
786 wl_list_for_each(view, &es->views, surface_link) {
787 if (!view->output)
788 continue;
789
790 pixman_region32_intersect(&region, &view->transform.boundingbox,
791 &view->output->region);
792
793 e = pixman_region32_extents(&region);
794 area = (e->x2 - e->x1) * (e->y2 - e->y1);
795
796 mask |= view->output_mask;
797
798 if (area >= max) {
799 new_output = view->output;
800 max = area;
801 }
802 }
803 pixman_region32_fini(&region);
804
805 es->output = new_output;
806 weston_surface_update_output_mask(es, mask);
807}
808
809static void
810weston_view_assign_output(struct weston_view *ev)
811{
812 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400813 struct weston_output *output, *new_output;
814 pixman_region32_t region;
815 uint32_t max, area, mask;
816 pixman_box32_t *e;
817
818 new_output = NULL;
819 max = 0;
820 mask = 0;
821 pixman_region32_init(&region);
822 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500823 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400824 &output->region);
825
826 e = pixman_region32_extents(&region);
827 area = (e->x2 - e->x1) * (e->y2 - e->y1);
828
829 if (area > 0)
830 mask |= 1 << output->id;
831
832 if (area >= max) {
833 new_output = output;
834 max = area;
835 }
836 }
837 pixman_region32_fini(&region);
838
Jason Ekstranda7af7042013-10-12 22:38:11 -0500839 ev->output = new_output;
840 ev->output_mask = mask;
841
842 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400843}
844
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200845static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
847 int32_t width, int32_t height,
848 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200849{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200850 float min_x = HUGE_VALF, min_y = HUGE_VALF;
851 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200852 int32_t s[4][2] = {
853 { sx, sy },
854 { sx, sy + height },
855 { sx + width, sy },
856 { sx + width, sy + height }
857 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200858 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200859 int i;
860
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300861 if (width == 0 || height == 0) {
862 /* avoid rounding empty bbox to 1x1 */
863 pixman_region32_init(bbox);
864 return;
865 }
866
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200867 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200868 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500869 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200870 if (x < min_x)
871 min_x = x;
872 if (x > max_x)
873 max_x = x;
874 if (y < min_y)
875 min_y = y;
876 if (y > max_y)
877 max_y = y;
878 }
879
Pekka Paalanen219b9822012-02-08 15:38:37 +0200880 int_x = floorf(min_x);
881 int_y = floorf(min_y);
882 pixman_region32_init_rect(bbox, int_x, int_y,
883 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200884}
885
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200886static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500887weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200888{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200890
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200891 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 view->geometry.x = roundf(view->geometry.x);
893 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200894
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500895 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
897 view->transform.position.matrix.d[12] = view->geometry.x;
898 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500899
Jason Ekstranda7af7042013-10-12 22:38:11 -0500900 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500901
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 view->transform.inverse = view->transform.position.matrix;
903 view->transform.inverse.d[12] = -view->geometry.x;
904 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500905
Jason Ekstranda7af7042013-10-12 22:38:11 -0500906 pixman_region32_init_rect(&view->transform.boundingbox,
907 view->geometry.x,
908 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600909 view->surface->width,
910 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500911
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912 if (view->alpha == 1.0) {
913 pixman_region32_copy(&view->transform.opaque,
914 &view->surface->opaque);
915 pixman_region32_translate(&view->transform.opaque,
916 view->geometry.x,
917 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500918 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200919}
920
921static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500922weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200923{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 struct weston_view *parent = view->geometry.parent;
925 struct weston_matrix *matrix = &view->transform.matrix;
926 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200927 struct weston_transform *tform;
928
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200930
931 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
933 view->transform.position.matrix.d[12] = view->geometry.x;
934 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200935
936 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200938 weston_matrix_multiply(matrix, &tform->matrix);
939
Pekka Paalanen483243f2013-03-08 14:56:50 +0200940 if (parent)
941 weston_matrix_multiply(matrix, &parent->transform.matrix);
942
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200943 if (weston_matrix_invert(inverse, matrix) < 0) {
944 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 weston_log("error: weston_view %p"
946 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200947 return -1;
948 }
949
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600950 view_compute_bbox(view, 0, 0,
951 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500953
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200954 return 0;
955}
956
957WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200959{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200961
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200963 return;
964
Pekka Paalanen483243f2013-03-08 14:56:50 +0200965 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200967
Jason Ekstranda7af7042013-10-12 22:38:11 -0500968 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 pixman_region32_fini(&view->transform.boundingbox);
973 pixman_region32_fini(&view->transform.opaque);
974 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500975
Pekka Paalanencd403622012-01-25 13:37:39 +0200976 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 if (view->geometry.transformation_list.next ==
978 &view->transform.position.link &&
979 view->geometry.transformation_list.prev ==
980 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200981 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200983 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 if (weston_view_update_transform_enable(view) < 0)
985 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200986 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200987
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200989
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300991
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 wl_signal_emit(&view->surface->compositor->transform_signal,
993 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200994}
995
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200996WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200998{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001000
1001 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 * The invariant: if view->geometry.dirty, then all views
1003 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001004 * Corollary: if not parent->geometry.dirty, then all ancestors
1005 * are not dirty.
1006 */
1007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001009 return;
1010
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001012
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001014 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001016}
1017
1018WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019weston_view_to_global_fixed(struct weston_view *view,
1020 wl_fixed_t vx, wl_fixed_t vy,
1021 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001022{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001023 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001024
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 weston_view_to_global_float(view,
1026 wl_fixed_to_double(vx),
1027 wl_fixed_to_double(vy),
1028 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001029 *x = wl_fixed_from_double(xf);
1030 *y = wl_fixed_from_double(yf);
1031}
1032
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001033WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034weston_view_from_global_float(struct weston_view *view,
1035 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001036{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001038 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1039
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001041
1042 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001043 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001045 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 *vx = 0;
1047 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001048 return;
1049 }
1050
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 *vx = v.f[0] / v.f[3];
1052 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001053 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 *vx = x - view->geometry.x;
1055 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001056 }
1057}
1058
1059WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060weston_view_from_global_fixed(struct weston_view *view,
1061 wl_fixed_t x, wl_fixed_t y,
1062 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001063{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001064 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001065
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 weston_view_from_global_float(view,
1067 wl_fixed_to_double(x),
1068 wl_fixed_to_double(y),
1069 &vxf, &vyf);
1070 *vx = wl_fixed_from_double(vxf);
1071 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001072}
1073
1074WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075weston_view_from_global(struct weston_view *view,
1076 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001077{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1081 *vx = floorf(vxf);
1082 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001083}
1084
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001085WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001086weston_surface_schedule_repaint(struct weston_surface *surface)
1087{
1088 struct weston_output *output;
1089
1090 wl_list_for_each(output, &surface->compositor->output_list, link)
1091 if (surface->output_mask & (1 << output->id))
1092 weston_output_schedule_repaint(output);
1093}
1094
1095WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096weston_view_schedule_repaint(struct weston_view *view)
1097{
1098 struct weston_output *output;
1099
1100 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1101 if (view->output_mask & (1 << output->id))
1102 weston_output_schedule_repaint(output);
1103}
1104
1105WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001106weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001107{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001108 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 0, 0, surface->width,
1110 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001111
Kristian Høgsberg98238702012-08-03 16:29:12 -04001112 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001113}
1114
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001115WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001117{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001118 if (view->geometry.x == x && view->geometry.y == y)
1119 return;
1120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 view->geometry.x = x;
1122 view->geometry.y = y;
1123 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001124}
1125
Pekka Paalanen483243f2013-03-08 14:56:50 +02001126static void
1127transform_parent_handle_parent_destroy(struct wl_listener *listener,
1128 void *data)
1129{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 struct weston_view *view =
1131 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001132 geometry.parent_destroy_listener);
1133
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001135}
1136
1137WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138weston_view_set_transform_parent(struct weston_view *view,
1139 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001140{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 if (view->geometry.parent) {
1142 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1143 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001144 }
1145
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001147
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001149 transform_parent_handle_parent_destroy;
1150 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001151 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001153 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001155 }
1156
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157 weston_view_geometry_dirty(view);
1158}
1159
1160WL_EXPORT int
1161weston_view_is_mapped(struct weston_view *view)
1162{
1163 if (view->output)
1164 return 1;
1165 else
1166 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001167}
1168
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001169WL_EXPORT int
1170weston_surface_is_mapped(struct weston_surface *surface)
1171{
1172 if (surface->output)
1173 return 1;
1174 else
1175 return 0;
1176}
1177
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001178static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001179surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001180{
1181 struct weston_view *view;
1182
1183 if (surface->width == width && surface->height == height)
1184 return;
1185
1186 surface->width = width;
1187 surface->height = height;
1188
1189 wl_list_for_each(view, &surface->views, surface_link)
1190 weston_view_geometry_dirty(view);
1191}
1192
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001193WL_EXPORT void
1194weston_surface_set_size(struct weston_surface *surface,
1195 int32_t width, int32_t height)
1196{
1197 assert(!surface->resource);
1198 surface_set_size(surface, width, height);
1199}
1200
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001201static int
1202fixed_round_up_to_int(wl_fixed_t f)
1203{
1204 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1205}
1206
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001207static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001208weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001209{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001210 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001211 int32_t width, height;
1212
1213 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001214 surface->width_from_buffer = 0;
1215 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001216 return;
1217 }
1218
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001219 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001220 case WL_OUTPUT_TRANSFORM_90:
1221 case WL_OUTPUT_TRANSFORM_270:
1222 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1223 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001224 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1225 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001226 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001227 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001228 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1229 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001230 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001231 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001232
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001233 surface->width_from_buffer = width;
1234 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001235}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001236
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001237static void
1238weston_surface_update_size(struct weston_surface *surface)
1239{
1240 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1241 int32_t width, height;
1242
1243 width = surface->width_from_buffer;
1244 height = surface->height_from_buffer;
1245
1246 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001247 surface_set_size(surface,
1248 vp->surface.width, vp->surface.height);
1249 return;
1250 }
1251
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001252 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001253 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1254 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1255
1256 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001257 return;
1258 }
1259
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001260 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001261}
1262
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001263WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001264weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001265{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001266 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001267
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001268 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001269
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001270 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001271}
1272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273WL_EXPORT struct weston_view *
1274weston_compositor_pick_view(struct weston_compositor *compositor,
1275 wl_fixed_t x, wl_fixed_t y,
1276 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001277{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 wl_list_for_each(view, &compositor->view_list, link) {
1281 weston_view_from_global_fixed(view, x, y, vx, vy);
1282 if (pixman_region32_contains_point(&view->surface->input,
1283 wl_fixed_to_int(*vx),
1284 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001285 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001287 }
1288
1289 return NULL;
1290}
1291
1292static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001294{
Daniel Stone37816df2012-05-16 18:45:18 +01001295 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001296
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001297 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001298 return;
1299
Daniel Stone37816df2012-05-16 18:45:18 +01001300 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001301 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001302}
1303
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001304WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001306{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001307 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 if (!weston_view_is_mapped(view))
1310 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 weston_view_damage_below(view);
1313 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001314 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 wl_list_remove(&view->layer_link);
1316 wl_list_init(&view->layer_link);
1317 wl_list_remove(&view->link);
1318 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 view->output_mask = 0;
1320 weston_surface_assign_output(view->surface);
1321
1322 if (weston_surface_is_mapped(view->surface))
1323 return;
1324
1325 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1326 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001327 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001329 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001330 NULL,
1331 wl_fixed_from_int(0),
1332 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001334 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001335 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001337
Jason Ekstranda7af7042013-10-12 22:38:11 -05001338WL_EXPORT void
1339weston_surface_unmap(struct weston_surface *surface)
1340{
1341 struct weston_view *view;
1342
1343 wl_list_for_each(view, &surface->views, surface_link)
1344 weston_view_unmap(view);
1345 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001346}
1347
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001348static void
1349weston_surface_reset_pending_buffer(struct weston_surface *surface)
1350{
1351 if (surface->pending.buffer)
1352 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1353 surface->pending.buffer = NULL;
1354 surface->pending.sx = 0;
1355 surface->pending.sy = 0;
1356 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001357 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001358}
1359
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001360struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001361 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001362 struct wl_list link;
1363};
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365WL_EXPORT void
1366weston_view_destroy(struct weston_view *view)
1367{
1368 wl_signal_emit(&view->destroy_signal, view);
1369
1370 assert(wl_list_empty(&view->geometry.child_list));
1371
1372 if (weston_view_is_mapped(view)) {
1373 weston_view_unmap(view);
1374 weston_compositor_build_view_list(view->surface->compositor);
1375 }
1376
1377 wl_list_remove(&view->link);
1378 wl_list_remove(&view->layer_link);
1379
1380 pixman_region32_fini(&view->clip);
1381 pixman_region32_fini(&view->transform.boundingbox);
1382
1383 weston_view_set_transform_parent(view, NULL);
1384
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 wl_list_remove(&view->surface_link);
1386
1387 free(view);
1388}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001389
1390WL_EXPORT void
1391weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001392{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001393 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001394 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001395
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001396 if (--surface->ref_count > 0)
1397 return;
1398
1399 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1400
Pekka Paalanene67858b2013-04-25 13:57:42 +03001401 assert(wl_list_empty(&surface->subsurface_list_pending));
1402 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001403
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1405 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001406
Pekka Paalanenbc106382012-10-10 12:49:31 +03001407 wl_list_for_each_safe(cb, next,
1408 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001409 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001410
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001411 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001412 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001413 pixman_region32_fini(&surface->pending.damage);
1414
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001415 if (surface->pending.buffer)
1416 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1417
Pekka Paalanende685b82012-12-04 15:58:12 +02001418 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001419
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001420 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001421 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001422 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001423
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001424 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001425 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001426
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001427 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001428}
1429
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001430static void
1431destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001432{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001433 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001434
Giulio Camuffo0d379742013-11-15 22:06:15 +01001435 /* Set the resource to NULL, since we don't want to leave a
1436 * dangling pointer if the surface was refcounted and survives
1437 * the weston_surface_destroy() call. */
1438 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001439 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001440}
1441
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001442static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001443weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1444{
1445 struct weston_buffer *buffer =
1446 container_of(listener, struct weston_buffer, destroy_listener);
1447
1448 wl_signal_emit(&buffer->destroy_signal, buffer);
1449 free(buffer);
1450}
1451
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001452WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001453weston_buffer_from_resource(struct wl_resource *resource)
1454{
1455 struct weston_buffer *buffer;
1456 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001457
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001458 listener = wl_resource_get_destroy_listener(resource,
1459 weston_buffer_destroy_handler);
1460
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001461 if (listener)
1462 return container_of(listener, struct weston_buffer,
1463 destroy_listener);
1464
1465 buffer = zalloc(sizeof *buffer);
1466 if (buffer == NULL)
1467 return NULL;
1468
1469 buffer->resource = resource;
1470 wl_signal_init(&buffer->destroy_signal);
1471 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001472 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001473 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001474
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001475 return buffer;
1476}
1477
1478static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001479weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1480 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001481{
Pekka Paalanende685b82012-12-04 15:58:12 +02001482 struct weston_buffer_reference *ref =
1483 container_of(listener, struct weston_buffer_reference,
1484 destroy_listener);
1485
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001486 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001487 ref->buffer = NULL;
1488}
1489
1490WL_EXPORT void
1491weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001492 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001493{
1494 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001495 ref->buffer->busy_count--;
1496 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001497 assert(wl_resource_get_client(ref->buffer->resource));
1498 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001499 WL_BUFFER_RELEASE);
1500 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001501 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001502 }
1503
Pekka Paalanende685b82012-12-04 15:58:12 +02001504 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001505 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001506 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001507 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001508 }
1509
Pekka Paalanende685b82012-12-04 15:58:12 +02001510 ref->buffer = buffer;
1511 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1512}
1513
1514static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001515weston_surface_attach(struct weston_surface *surface,
1516 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001517{
1518 weston_buffer_reference(&surface->buffer_ref, buffer);
1519
Pekka Paalanena6421c42012-12-04 15:58:10 +02001520 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001521 if (weston_surface_is_mapped(surface))
1522 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001523 }
1524
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001525 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001526
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001527 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001528}
1529
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001530WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001531weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001533 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001534
1535 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001537}
1538
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001539WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001540weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001543
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001544 pixman_region32_union(&compositor->primary_plane.damage,
1545 &compositor->primary_plane.damage,
1546 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001547 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001548}
1549
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001550static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001552{
Pekka Paalanende685b82012-12-04 15:58:12 +02001553 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001554 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001555 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001556
Jason Ekstrandef540082014-06-26 10:37:36 -07001557 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558}
1559
1560static void
1561view_accumulate_damage(struct weston_view *view,
1562 pixman_region32_t *opaque)
1563{
1564 pixman_region32_t damage;
1565
1566 pixman_region32_init(&damage);
1567 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001568 pixman_box32_t *extents;
1569
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 extents = pixman_region32_extents(&view->surface->damage);
1571 view_compute_bbox(view, extents->x1, extents->y1,
1572 extents->x2 - extents->x1,
1573 extents->y2 - extents->y1,
1574 &damage);
1575 pixman_region32_translate(&damage,
1576 -view->plane->x,
1577 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001578 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 pixman_region32_copy(&damage, &view->surface->damage);
1580 pixman_region32_translate(&damage,
1581 view->geometry.x - view->plane->x,
1582 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001583 }
1584
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 pixman_region32_subtract(&damage, &damage, opaque);
1586 pixman_region32_union(&view->plane->damage,
1587 &view->plane->damage, &damage);
1588 pixman_region32_fini(&damage);
1589 pixman_region32_copy(&view->clip, opaque);
1590 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001591}
1592
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001593static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001594compositor_accumulate_damage(struct weston_compositor *ec)
1595{
1596 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001598 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001599
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001600 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001601
1602 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001603 pixman_region32_copy(&plane->clip, &clip);
1604
1605 pixman_region32_init(&opaque);
1606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 wl_list_for_each(ev, &ec->view_list, link) {
1608 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001609 continue;
1610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001612 }
1613
1614 pixman_region32_union(&clip, &clip, &opaque);
1615 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001616 }
1617
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001618 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 wl_list_for_each(ev, &ec->view_list, link)
1621 ev->surface->touched = 0;
1622
1623 wl_list_for_each(ev, &ec->view_list, link) {
1624 if (ev->surface->touched)
1625 continue;
1626 ev->surface->touched = 1;
1627
1628 surface_flush_damage(ev->surface);
1629
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001630 /* Both the renderer and the backend have seen the buffer
1631 * by now. If renderer needs the buffer, it has its own
1632 * reference set. If the backend wants to keep the buffer
1633 * around for migrating the surface into a non-primary plane
1634 * later, keep_buffer is true. Otherwise, drop the core
1635 * reference now, and allow early buffer release. This enables
1636 * clients to use single-buffering.
1637 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 if (!ev->surface->keep_buffer)
1639 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001640 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001641}
1642
1643static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001645{
1646 struct weston_subsurface *sub;
1647
Pekka Paalanene67858b2013-04-25 13:57:42 +03001648 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001650 continue;
1651
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1653 wl_list_init(&sub->surface->views);
1654
1655 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001656 }
1657}
1658
1659static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 struct weston_subsurface *sub;
1663 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001664
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1666 if (sub->surface == surface)
1667 continue;
1668
George Kiagiadakised04d382014-06-13 18:10:26 +02001669 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1670 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001671 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001672 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673
1674 surface_free_unused_subsurface_views(sub->surface);
1675 }
1676}
1677
1678static void
1679view_list_add_subsurface_view(struct weston_compositor *compositor,
1680 struct weston_subsurface *sub,
1681 struct weston_view *parent)
1682{
1683 struct weston_subsurface *child;
1684 struct weston_view *view = NULL, *iv;
1685
1686 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1687 if (iv->geometry.parent == parent) {
1688 view = iv;
1689 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001690 }
1691 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001692
1693 if (view) {
1694 /* Put it back in the surface's list of views */
1695 wl_list_remove(&view->surface_link);
1696 wl_list_insert(&sub->surface->views, &view->surface_link);
1697 } else {
1698 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001699 weston_view_set_position(view,
1700 sub->position.x,
1701 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 weston_view_set_transform_parent(view, parent);
1703 }
1704
1705 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706
Pekka Paalanenb188e912013-11-19 14:03:35 +02001707 if (wl_list_empty(&sub->surface->subsurface_list)) {
1708 wl_list_insert(compositor->view_list.prev, &view->link);
1709 return;
1710 }
1711
1712 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1713 if (child->surface == sub->surface)
1714 wl_list_insert(compositor->view_list.prev, &view->link);
1715 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001717 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718}
1719
1720static void
1721view_list_add(struct weston_compositor *compositor,
1722 struct weston_view *view)
1723{
1724 struct weston_subsurface *sub;
1725
1726 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727
Pekka Paalanenb188e912013-11-19 14:03:35 +02001728 if (wl_list_empty(&view->surface->subsurface_list)) {
1729 wl_list_insert(compositor->view_list.prev, &view->link);
1730 return;
1731 }
1732
1733 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1734 if (sub->surface == view->surface)
1735 wl_list_insert(compositor->view_list.prev, &view->link);
1736 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001738 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739}
1740
1741static void
1742weston_compositor_build_view_list(struct weston_compositor *compositor)
1743{
1744 struct weston_view *view;
1745 struct weston_layer *layer;
1746
1747 wl_list_for_each(layer, &compositor->layer_list, link)
1748 wl_list_for_each(view, &layer->view_list, layer_link)
1749 surface_stash_subsurface_views(view->surface);
1750
1751 wl_list_init(&compositor->view_list);
1752 wl_list_for_each(layer, &compositor->layer_list, link) {
1753 wl_list_for_each(view, &layer->view_list, layer_link) {
1754 view_list_add(compositor, view);
1755 }
1756 }
1757
1758 wl_list_for_each(layer, &compositor->layer_list, link)
1759 wl_list_for_each(view, &layer->view_list, layer_link)
1760 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001761}
1762
David Herrmann1edf44c2013-10-22 17:11:26 +02001763static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001764weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001765{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001766 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001768 struct weston_animation *animation, *next;
1769 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001770 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001771 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001772 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001773
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001774 if (output->destroying)
1775 return 0;
1776
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001777 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001779
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001780 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001781 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001782 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 wl_list_for_each(ev, &ec->view_list, link)
1784 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001785
Pekka Paalanene67858b2013-04-25 13:57:42 +03001786 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 wl_list_for_each(ev, &ec->view_list, link) {
1788 /* Note: This operation is safe to do multiple times on the
1789 * same surface.
1790 */
1791 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001792 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 &ev->surface->frame_callback_list);
1794 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001795 }
1796 }
1797
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001798 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001799
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001800 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001801 pixman_region32_intersect(&output_damage,
1802 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001803 pixman_region32_subtract(&output_damage,
1804 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001805
Scott Moreauccbf29d2012-02-22 14:21:41 -07001806 if (output->dirty)
1807 weston_output_update_matrix(output);
1808
David Herrmann1edf44c2013-10-22 17:11:26 +02001809 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001810
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001811 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001812
Kristian Høgsbergef044142011-06-21 15:02:12 -04001813 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001814
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001815 weston_compositor_repick(ec);
1816 wl_event_loop_dispatch(ec->input_loop, 0);
1817
Jonas Ådahldb773762012-06-13 00:01:21 +02001818 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001819 wl_callback_send_done(cb->resource, msecs);
1820 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001821 }
1822
Scott Moreaud64cf212012-06-08 19:40:54 -06001823 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001824 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001825 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001826 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001827
1828 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001829}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001830
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001831static int
1832weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001833{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001834 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001835
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001836 wl_event_loop_dispatch(compositor->input_loop, 0);
1837
1838 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001839}
1840
1841WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001842weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001843{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001844 struct weston_compositor *compositor = output->compositor;
1845 struct wl_event_loop *loop =
1846 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001847 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001848
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001849 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001850
1851 if (output->repaint_needed &&
1852 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1853 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001854 r = weston_output_repaint(output, msecs);
1855 if (!r)
1856 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001857 }
1858
1859 output->repaint_scheduled = 0;
1860 if (compositor->input_loop_source)
1861 return;
1862
1863 fd = wl_event_loop_get_fd(compositor->input_loop);
1864 compositor->input_loop_source =
1865 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1866 weston_compositor_read_input, compositor);
1867}
1868
1869static void
1870idle_repaint(void *data)
1871{
1872 struct weston_output *output = data;
1873
Jonas Ådahle5a12252013-04-05 23:07:11 +02001874 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001875}
1876
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001877WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001878weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1879{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001881 if (below != NULL)
1882 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001883}
1884
1885WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001886weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001887{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001888 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001889 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001890
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001891 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1892 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001893 return;
1894
Kristian Høgsbergef044142011-06-21 15:02:12 -04001895 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001896 output->repaint_needed = 1;
1897 if (output->repaint_scheduled)
1898 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001899
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001900 wl_event_loop_add_idle(loop, idle_repaint, output);
1901 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001902
1903 if (compositor->input_loop_source) {
1904 wl_event_source_remove(compositor->input_loop_source);
1905 compositor->input_loop_source = NULL;
1906 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001907}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001908
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001909WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001910weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1911{
1912 struct weston_output *output;
1913
1914 wl_list_for_each(output, &compositor->output_list, link)
1915 weston_output_schedule_repaint(output);
1916}
1917
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001918static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001919surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001920{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001921 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001922}
1923
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001924static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001925surface_attach(struct wl_client *client,
1926 struct wl_resource *resource,
1927 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1928{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001929 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001930 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001931
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001932 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001933 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001934 if (buffer == NULL) {
1935 wl_client_post_no_memory(client);
1936 return;
1937 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001938 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001939
Pekka Paalanende685b82012-12-04 15:58:12 +02001940 /* Attach, attach, without commit in between does not send
1941 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001942 if (surface->pending.buffer)
1943 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001944
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001945 surface->pending.sx = sx;
1946 surface->pending.sy = sy;
1947 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001948 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001949 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001950 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001951 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001952 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001953}
1954
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001955static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001956surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001957 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001958 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001959{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001960 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001961
Pekka Paalanen8e159182012-10-10 12:49:25 +03001962 pixman_region32_union_rect(&surface->pending.damage,
1963 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001964 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001965}
1966
Kristian Høgsberg33418202011-08-16 23:01:28 -04001967static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001968destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001969{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001970 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001971
1972 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001973 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001974}
1975
1976static void
1977surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001978 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001979{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001980 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001981 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001982
1983 cb = malloc(sizeof *cb);
1984 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001985 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001986 return;
1987 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001988
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001989 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1990 callback);
1991 if (cb->resource == NULL) {
1992 free(cb);
1993 wl_resource_post_no_memory(resource);
1994 return;
1995 }
1996
Jason Ekstranda85118c2013-06-27 20:17:02 -05001997 wl_resource_set_implementation(cb->resource, NULL, cb,
1998 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001999
Pekka Paalanenbc106382012-10-10 12:49:31 +03002000 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002001}
2002
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002003static void
2004surface_set_opaque_region(struct wl_client *client,
2005 struct wl_resource *resource,
2006 struct wl_resource *region_resource)
2007{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002008 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002009 struct weston_region *region;
2010
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002011 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002012 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002013 pixman_region32_copy(&surface->pending.opaque,
2014 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002015 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002016 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002017 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002018}
2019
2020static void
2021surface_set_input_region(struct wl_client *client,
2022 struct wl_resource *resource,
2023 struct wl_resource *region_resource)
2024{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002025 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002026 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002027
2028 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002029 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002030 pixman_region32_copy(&surface->pending.input,
2031 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002032 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002033 pixman_region32_fini(&surface->pending.input);
2034 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002035 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002036}
2037
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002038static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002039weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002040{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002041 struct weston_subsurface *sub;
2042
2043 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2044 parent_link_pending) {
2045 wl_list_remove(&sub->parent_link);
2046 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2047 }
2048}
2049
2050static void
2051weston_surface_commit(struct weston_surface *surface)
2052{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002053 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002054 pixman_region32_t opaque;
2055
Alexander Larsson4ea95522013-05-22 14:41:37 +02002056 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002057 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002058 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002059 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002060
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002061 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002062 if (surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002063 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002064
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002065 if (surface->pending.newly_attached ||
2066 surface->pending.buffer_viewport.changed) {
2067 weston_surface_update_size(surface);
2068 if (surface->configure)
2069 surface->configure(surface, surface->pending.sx,
2070 surface->pending.sy);
2071 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002072
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002073 weston_surface_reset_pending_buffer(surface);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002074
2075 /* wl_surface.damage */
2076 pixman_region32_union(&surface->damage, &surface->damage,
2077 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002078 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002079 0, 0, surface->width, surface->height);
2080 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002081
2082 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002083 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002084 surface->width,
2085 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002086 pixman_region32_intersect(&opaque,
2087 &opaque, &surface->pending.opaque);
2088
2089 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2090 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002091 wl_list_for_each(view, &surface->views, surface_link)
2092 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002093 }
2094
2095 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002096
2097 /* wl_surface.set_input_region */
2098 pixman_region32_fini(&surface->input);
2099 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 surface->width,
2101 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002102 pixman_region32_intersect(&surface->input,
2103 &surface->input, &surface->pending.input);
2104
Pekka Paalanenbc106382012-10-10 12:49:31 +03002105 /* wl_surface.frame */
2106 wl_list_insert_list(&surface->frame_callback_list,
2107 &surface->pending.frame_callback_list);
2108 wl_list_init(&surface->pending.frame_callback_list);
2109
Pekka Paalanene67858b2013-04-25 13:57:42 +03002110 weston_surface_commit_subsurface_order(surface);
2111
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002112 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002113}
2114
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002115static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116weston_subsurface_commit(struct weston_subsurface *sub);
2117
2118static void
2119weston_subsurface_parent_commit(struct weston_subsurface *sub,
2120 int parent_is_synchronized);
2121
2122static void
2123surface_commit(struct wl_client *client, struct wl_resource *resource)
2124{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002125 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002126 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2127
2128 if (sub) {
2129 weston_subsurface_commit(sub);
2130 return;
2131 }
2132
2133 weston_surface_commit(surface);
2134
2135 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2136 if (sub->surface != surface)
2137 weston_subsurface_parent_commit(sub, 0);
2138 }
2139}
2140
2141static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002142surface_set_buffer_transform(struct wl_client *client,
2143 struct wl_resource *resource, int transform)
2144{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002145 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002146
Jonny Lamba55f1392014-05-30 12:07:15 +02002147 /* if wl_output.transform grows more members this will need to be updated. */
2148 if (transform < 0 ||
2149 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2150 wl_resource_post_error(resource,
2151 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2152 "buffer transform must be a valid transform "
2153 "('%d' specified)", transform);
2154 return;
2155 }
2156
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002157 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002158 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002159}
2160
Alexander Larsson4ea95522013-05-22 14:41:37 +02002161static void
2162surface_set_buffer_scale(struct wl_client *client,
2163 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002164 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002165{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002166 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002167
Jonny Lamba55f1392014-05-30 12:07:15 +02002168 if (scale < 1) {
2169 wl_resource_post_error(resource,
2170 WL_SURFACE_ERROR_INVALID_SCALE,
2171 "buffer scale must be at least one "
2172 "('%d' specified)", scale);
2173 return;
2174 }
2175
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002176 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002177 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002178}
2179
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002180static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002181 surface_destroy,
2182 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002183 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002184 surface_frame,
2185 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002186 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002187 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002188 surface_set_buffer_transform,
2189 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002190};
2191
2192static void
2193compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002194 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002195{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002196 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002197 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002198
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002199 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002200 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002201 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002202 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002203 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002204
Jason Ekstranda85118c2013-06-27 20:17:02 -05002205 surface->resource =
2206 wl_resource_create(client, &wl_surface_interface,
2207 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002208 if (surface->resource == NULL) {
2209 weston_surface_destroy(surface);
2210 wl_resource_post_no_memory(resource);
2211 return;
2212 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002213 wl_resource_set_implementation(surface->resource, &surface_interface,
2214 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002215
2216 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002217}
2218
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002219static void
2220destroy_region(struct wl_resource *resource)
2221{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002222 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002223
2224 pixman_region32_fini(&region->region);
2225 free(region);
2226}
2227
2228static void
2229region_destroy(struct wl_client *client, struct wl_resource *resource)
2230{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002231 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002232}
2233
2234static void
2235region_add(struct wl_client *client, struct wl_resource *resource,
2236 int32_t x, int32_t y, int32_t width, int32_t height)
2237{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002238 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002239
2240 pixman_region32_union_rect(&region->region, &region->region,
2241 x, y, width, height);
2242}
2243
2244static void
2245region_subtract(struct wl_client *client, struct wl_resource *resource,
2246 int32_t x, int32_t y, int32_t width, int32_t height)
2247{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002248 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002249 pixman_region32_t rect;
2250
2251 pixman_region32_init_rect(&rect, x, y, width, height);
2252 pixman_region32_subtract(&region->region, &region->region, &rect);
2253 pixman_region32_fini(&rect);
2254}
2255
2256static const struct wl_region_interface region_interface = {
2257 region_destroy,
2258 region_add,
2259 region_subtract
2260};
2261
2262static void
2263compositor_create_region(struct wl_client *client,
2264 struct wl_resource *resource, uint32_t id)
2265{
2266 struct weston_region *region;
2267
2268 region = malloc(sizeof *region);
2269 if (region == NULL) {
2270 wl_resource_post_no_memory(resource);
2271 return;
2272 }
2273
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002274 pixman_region32_init(&region->region);
2275
Jason Ekstranda85118c2013-06-27 20:17:02 -05002276 region->resource =
2277 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002278 if (region->resource == NULL) {
2279 free(region);
2280 wl_resource_post_no_memory(resource);
2281 return;
2282 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002283 wl_resource_set_implementation(region->resource, &region_interface,
2284 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002285}
2286
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002287static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002288 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002289 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002290};
2291
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002292static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002293weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2294{
2295 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002296 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002297 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298
Alexander Larsson4ea95522013-05-22 14:41:37 +02002299 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002300 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002301 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002302 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002303
Pekka Paalanene67858b2013-04-25 13:57:42 +03002304 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002305 if (sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2307 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2308
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002309 if (sub->cached.newly_attached || sub->cached.buffer_viewport.changed) {
2310 weston_surface_update_size(surface);
2311 if (surface->configure)
2312 surface->configure(surface, sub->cached.sx,
2313 sub->cached.sy);
2314 }
2315
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 sub->cached.sx = 0;
2317 sub->cached.sy = 0;
2318 sub->cached.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002319 sub->cached.buffer_viewport.changed = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002320
2321 /* wl_surface.damage */
2322 pixman_region32_union(&surface->damage, &surface->damage,
2323 &sub->cached.damage);
2324 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2325 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002326 surface->width,
2327 surface->height);
Jason Ekstrandef540082014-06-26 10:37:36 -07002328 pixman_region32_clear(&sub->cached.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329
2330 /* wl_surface.set_opaque_region */
2331 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002332 surface->width,
2333 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002334 pixman_region32_intersect(&opaque,
2335 &opaque, &sub->cached.opaque);
2336
2337 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2338 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002339 wl_list_for_each(view, &surface->views, surface_link)
2340 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002341 }
2342
2343 pixman_region32_fini(&opaque);
2344
2345 /* wl_surface.set_input_region */
2346 pixman_region32_fini(&surface->input);
2347 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 surface->width,
2349 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002350 pixman_region32_intersect(&surface->input,
2351 &surface->input, &sub->cached.input);
2352
2353 /* wl_surface.frame */
2354 wl_list_insert_list(&surface->frame_callback_list,
2355 &sub->cached.frame_callback_list);
2356 wl_list_init(&sub->cached.frame_callback_list);
2357
2358 weston_surface_commit_subsurface_order(surface);
2359
2360 weston_surface_schedule_repaint(surface);
2361
2362 sub->cached.has_data = 0;
2363}
2364
2365static void
2366weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2367{
2368 struct weston_surface *surface = sub->surface;
2369
2370 /*
2371 * If this commit would cause the surface to move by the
2372 * attach(dx, dy) parameters, the old damage region must be
2373 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002374 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002375 */
2376 pixman_region32_translate(&sub->cached.damage,
2377 -surface->pending.sx, -surface->pending.sy);
2378 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2379 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002380 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002381
2382 if (surface->pending.newly_attached) {
2383 sub->cached.newly_attached = 1;
2384 weston_buffer_reference(&sub->cached.buffer_ref,
2385 surface->pending.buffer);
2386 }
2387 sub->cached.sx += surface->pending.sx;
2388 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002389
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002390 sub->cached.buffer_viewport.changed |=
2391 surface->pending.buffer_viewport.changed;
2392 sub->cached.buffer_viewport.buffer =
2393 surface->pending.buffer_viewport.buffer;
2394 sub->cached.buffer_viewport.surface =
2395 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002396
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002397 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002398
2399 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2400
2401 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2402
2403 wl_list_insert_list(&sub->cached.frame_callback_list,
2404 &surface->pending.frame_callback_list);
2405 wl_list_init(&surface->pending.frame_callback_list);
2406
2407 sub->cached.has_data = 1;
2408}
2409
2410static int
2411weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2412{
2413 while (sub) {
2414 if (sub->synchronized)
2415 return 1;
2416
2417 if (!sub->parent)
2418 return 0;
2419
2420 sub = weston_surface_to_subsurface(sub->parent);
2421 }
2422
2423 return 0;
2424}
2425
2426static void
2427weston_subsurface_commit(struct weston_subsurface *sub)
2428{
2429 struct weston_surface *surface = sub->surface;
2430 struct weston_subsurface *tmp;
2431
2432 /* Recursive check for effectively synchronized. */
2433 if (weston_subsurface_is_synchronized(sub)) {
2434 weston_subsurface_commit_to_cache(sub);
2435 } else {
2436 if (sub->cached.has_data) {
2437 /* flush accumulated state from cache */
2438 weston_subsurface_commit_to_cache(sub);
2439 weston_subsurface_commit_from_cache(sub);
2440 } else {
2441 weston_surface_commit(surface);
2442 }
2443
2444 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2445 if (tmp->surface != surface)
2446 weston_subsurface_parent_commit(tmp, 0);
2447 }
2448 }
2449}
2450
2451static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002452weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453{
2454 struct weston_surface *surface = sub->surface;
2455 struct weston_subsurface *tmp;
2456
Pekka Paalanene67858b2013-04-25 13:57:42 +03002457 /* From now on, commit_from_cache the whole sub-tree, regardless of
2458 * the synchronized mode of each child. This sub-surface or some
2459 * of its ancestors were synchronized, so we are synchronized
2460 * all the way down.
2461 */
2462
2463 if (sub->cached.has_data)
2464 weston_subsurface_commit_from_cache(sub);
2465
2466 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2467 if (tmp->surface != surface)
2468 weston_subsurface_parent_commit(tmp, 1);
2469 }
2470}
2471
2472static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002473weston_subsurface_parent_commit(struct weston_subsurface *sub,
2474 int parent_is_synchronized)
2475{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002476 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002477 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002478 wl_list_for_each(view, &sub->surface->views, surface_link)
2479 weston_view_set_position(view,
2480 sub->position.x,
2481 sub->position.y);
2482
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002483 sub->position.set = 0;
2484 }
2485
2486 if (parent_is_synchronized || sub->synchronized)
2487 weston_subsurface_synchronized_commit(sub);
2488}
2489
2490static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002491subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002492{
2493 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002494 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002495
Jason Ekstranda7af7042013-10-12 22:38:11 -05002496 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002497 weston_view_set_position(view,
2498 view->geometry.x + dx,
2499 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002500
2501 /* No need to check parent mappedness, because if parent is not
2502 * mapped, parent is not in a visible layer, so this sub-surface
2503 * will not be drawn either.
2504 */
2505 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002506 /* Cannot call weston_surface_update_transform(),
2507 * because that would call it also for the parent surface,
2508 * which might not be mapped yet. That would lead to
2509 * inconsistent state, where the window could never be
2510 * mapped.
2511 *
2512 * Instead just assing any output, to make
2513 * weston_surface_is_mapped() return true, so that when the
2514 * parent surface does get mapped, this one will get
2515 * included, too. See surface_list_add().
2516 */
2517 assert(!wl_list_empty(&compositor->output_list));
2518 surface->output = container_of(compositor->output_list.next,
2519 struct weston_output, link);
2520 }
2521}
2522
2523static struct weston_subsurface *
2524weston_surface_to_subsurface(struct weston_surface *surface)
2525{
2526 if (surface->configure == subsurface_configure)
2527 return surface->configure_private;
2528
2529 return NULL;
2530}
2531
Pekka Paalanen01388e22013-04-25 13:57:44 +03002532WL_EXPORT struct weston_surface *
2533weston_surface_get_main_surface(struct weston_surface *surface)
2534{
2535 struct weston_subsurface *sub;
2536
2537 while (surface && (sub = weston_surface_to_subsurface(surface)))
2538 surface = sub->parent;
2539
2540 return surface;
2541}
2542
Pekka Paalanene67858b2013-04-25 13:57:42 +03002543static void
2544subsurface_set_position(struct wl_client *client,
2545 struct wl_resource *resource, int32_t x, int32_t y)
2546{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002547 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002548
2549 if (!sub)
2550 return;
2551
2552 sub->position.x = x;
2553 sub->position.y = y;
2554 sub->position.set = 1;
2555}
2556
2557static struct weston_subsurface *
2558subsurface_from_surface(struct weston_surface *surface)
2559{
2560 struct weston_subsurface *sub;
2561
2562 sub = weston_surface_to_subsurface(surface);
2563 if (sub)
2564 return sub;
2565
2566 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2567 if (sub->surface == surface)
2568 return sub;
2569
2570 return NULL;
2571}
2572
2573static struct weston_subsurface *
2574subsurface_sibling_check(struct weston_subsurface *sub,
2575 struct weston_surface *surface,
2576 const char *request)
2577{
2578 struct weston_subsurface *sibling;
2579
2580 sibling = subsurface_from_surface(surface);
2581
2582 if (!sibling) {
2583 wl_resource_post_error(sub->resource,
2584 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2585 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002586 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002587 return NULL;
2588 }
2589
2590 if (sibling->parent != sub->parent) {
2591 wl_resource_post_error(sub->resource,
2592 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2593 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002594 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002595 return NULL;
2596 }
2597
2598 return sibling;
2599}
2600
2601static void
2602subsurface_place_above(struct wl_client *client,
2603 struct wl_resource *resource,
2604 struct wl_resource *sibling_resource)
2605{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002606 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002607 struct weston_surface *surface =
2608 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002609 struct weston_subsurface *sibling;
2610
2611 if (!sub)
2612 return;
2613
2614 sibling = subsurface_sibling_check(sub, surface, "place_above");
2615 if (!sibling)
2616 return;
2617
2618 wl_list_remove(&sub->parent_link_pending);
2619 wl_list_insert(sibling->parent_link_pending.prev,
2620 &sub->parent_link_pending);
2621}
2622
2623static void
2624subsurface_place_below(struct wl_client *client,
2625 struct wl_resource *resource,
2626 struct wl_resource *sibling_resource)
2627{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002628 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002629 struct weston_surface *surface =
2630 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002631 struct weston_subsurface *sibling;
2632
2633 if (!sub)
2634 return;
2635
2636 sibling = subsurface_sibling_check(sub, surface, "place_below");
2637 if (!sibling)
2638 return;
2639
2640 wl_list_remove(&sub->parent_link_pending);
2641 wl_list_insert(&sibling->parent_link_pending,
2642 &sub->parent_link_pending);
2643}
2644
2645static void
2646subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2647{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002648 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002649
2650 if (sub)
2651 sub->synchronized = 1;
2652}
2653
2654static void
2655subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2656{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002657 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002658
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002659 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002661
2662 /* If sub became effectively desynchronized, flush. */
2663 if (!weston_subsurface_is_synchronized(sub))
2664 weston_subsurface_synchronized_commit(sub);
2665 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002666}
2667
2668static void
2669weston_subsurface_cache_init(struct weston_subsurface *sub)
2670{
2671 pixman_region32_init(&sub->cached.damage);
2672 pixman_region32_init(&sub->cached.opaque);
2673 pixman_region32_init(&sub->cached.input);
2674 wl_list_init(&sub->cached.frame_callback_list);
2675 sub->cached.buffer_ref.buffer = NULL;
2676}
2677
2678static void
2679weston_subsurface_cache_fini(struct weston_subsurface *sub)
2680{
2681 struct weston_frame_callback *cb, *tmp;
2682
2683 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002684 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002685
2686 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2687 pixman_region32_fini(&sub->cached.damage);
2688 pixman_region32_fini(&sub->cached.opaque);
2689 pixman_region32_fini(&sub->cached.input);
2690}
2691
2692static void
2693weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2694{
2695 wl_list_remove(&sub->parent_link);
2696 wl_list_remove(&sub->parent_link_pending);
2697 wl_list_remove(&sub->parent_destroy_listener.link);
2698 sub->parent = NULL;
2699}
2700
2701static void
2702weston_subsurface_destroy(struct weston_subsurface *sub);
2703
2704static void
2705subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2706{
2707 struct weston_subsurface *sub =
2708 container_of(listener, struct weston_subsurface,
2709 surface_destroy_listener);
2710 assert(data == &sub->surface->resource);
2711
2712 /* The protocol object (wl_resource) is left inert. */
2713 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002714 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002715
2716 weston_subsurface_destroy(sub);
2717}
2718
2719static void
2720subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2721{
2722 struct weston_subsurface *sub =
2723 container_of(listener, struct weston_subsurface,
2724 parent_destroy_listener);
2725 assert(data == &sub->parent->resource);
2726 assert(sub->surface != sub->parent);
2727
2728 if (weston_surface_is_mapped(sub->surface))
2729 weston_surface_unmap(sub->surface);
2730
2731 weston_subsurface_unlink_parent(sub);
2732}
2733
2734static void
2735subsurface_resource_destroy(struct wl_resource *resource)
2736{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002737 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002738
2739 if (sub)
2740 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002741}
2742
2743static void
2744subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2745{
2746 wl_resource_destroy(resource);
2747}
2748
2749static void
2750weston_subsurface_link_parent(struct weston_subsurface *sub,
2751 struct weston_surface *parent)
2752{
2753 sub->parent = parent;
2754 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002755 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002756 &sub->parent_destroy_listener);
2757
2758 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2759 wl_list_insert(&parent->subsurface_list_pending,
2760 &sub->parent_link_pending);
2761}
2762
2763static void
2764weston_subsurface_link_surface(struct weston_subsurface *sub,
2765 struct weston_surface *surface)
2766{
2767 sub->surface = surface;
2768 sub->surface_destroy_listener.notify =
2769 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002770 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771 &sub->surface_destroy_listener);
2772}
2773
2774static void
2775weston_subsurface_destroy(struct weston_subsurface *sub)
2776{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002777 struct weston_view *view, *next;
2778
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779 assert(sub->surface);
2780
2781 if (sub->resource) {
2782 assert(weston_surface_to_subsurface(sub->surface) == sub);
2783 assert(sub->parent_destroy_listener.notify ==
2784 subsurface_handle_parent_destroy);
2785
George Kiagiadakised04d382014-06-13 18:10:26 +02002786 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2787 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002789 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002790
Pekka Paalanene67858b2013-04-25 13:57:42 +03002791 if (sub->parent)
2792 weston_subsurface_unlink_parent(sub);
2793
2794 weston_subsurface_cache_fini(sub);
2795
2796 sub->surface->configure = NULL;
2797 sub->surface->configure_private = NULL;
2798 } else {
2799 /* the dummy weston_subsurface for the parent itself */
2800 assert(sub->parent_destroy_listener.notify == NULL);
2801 wl_list_remove(&sub->parent_link);
2802 wl_list_remove(&sub->parent_link_pending);
2803 }
2804
2805 wl_list_remove(&sub->surface_destroy_listener.link);
2806 free(sub);
2807}
2808
2809static const struct wl_subsurface_interface subsurface_implementation = {
2810 subsurface_destroy,
2811 subsurface_set_position,
2812 subsurface_place_above,
2813 subsurface_place_below,
2814 subsurface_set_sync,
2815 subsurface_set_desync
2816};
2817
2818static struct weston_subsurface *
2819weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2820 struct weston_surface *parent)
2821{
2822 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002823 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824
2825 sub = calloc(1, sizeof *sub);
2826 if (!sub)
2827 return NULL;
2828
Jason Ekstranda7af7042013-10-12 22:38:11 -05002829 wl_list_init(&sub->unused_views);
2830
Jason Ekstranda85118c2013-06-27 20:17:02 -05002831 sub->resource =
2832 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833 if (!sub->resource) {
2834 free(sub);
2835 return NULL;
2836 }
2837
Jason Ekstranda85118c2013-06-27 20:17:02 -05002838 wl_resource_set_implementation(sub->resource,
2839 &subsurface_implementation,
2840 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002841 weston_subsurface_link_surface(sub, surface);
2842 weston_subsurface_link_parent(sub, parent);
2843 weston_subsurface_cache_init(sub);
2844 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002845
2846 return sub;
2847}
2848
2849/* Create a dummy subsurface for having the parent itself in its
2850 * sub-surface lists. Makes stacking order manipulation easy.
2851 */
2852static struct weston_subsurface *
2853weston_subsurface_create_for_parent(struct weston_surface *parent)
2854{
2855 struct weston_subsurface *sub;
2856
2857 sub = calloc(1, sizeof *sub);
2858 if (!sub)
2859 return NULL;
2860
2861 weston_subsurface_link_surface(sub, parent);
2862 sub->parent = parent;
2863 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2864 wl_list_insert(&parent->subsurface_list_pending,
2865 &sub->parent_link_pending);
2866
2867 return sub;
2868}
2869
2870static void
2871subcompositor_get_subsurface(struct wl_client *client,
2872 struct wl_resource *resource,
2873 uint32_t id,
2874 struct wl_resource *surface_resource,
2875 struct wl_resource *parent_resource)
2876{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002877 struct weston_surface *surface =
2878 wl_resource_get_user_data(surface_resource);
2879 struct weston_surface *parent =
2880 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002881 struct weston_subsurface *sub;
2882 static const char where[] = "get_subsurface: wl_subsurface@";
2883
2884 if (surface == parent) {
2885 wl_resource_post_error(resource,
2886 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2887 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002888 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889 return;
2890 }
2891
2892 if (weston_surface_to_subsurface(surface)) {
2893 wl_resource_post_error(resource,
2894 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2895 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002896 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002897 return;
2898 }
2899
2900 if (surface->configure) {
2901 wl_resource_post_error(resource,
2902 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2903 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002904 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002905 return;
2906 }
2907
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002908 if (weston_surface_get_main_surface(parent) == surface) {
2909 wl_resource_post_error(resource,
2910 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2911 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002912 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002913 return;
2914 }
2915
Pekka Paalanene67858b2013-04-25 13:57:42 +03002916 /* make sure the parent is in its own list */
2917 if (wl_list_empty(&parent->subsurface_list)) {
2918 if (!weston_subsurface_create_for_parent(parent)) {
2919 wl_resource_post_no_memory(resource);
2920 return;
2921 }
2922 }
2923
2924 sub = weston_subsurface_create(id, surface, parent);
2925 if (!sub) {
2926 wl_resource_post_no_memory(resource);
2927 return;
2928 }
2929
2930 surface->configure = subsurface_configure;
2931 surface->configure_private = sub;
2932}
2933
2934static void
2935subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2936{
2937 wl_resource_destroy(resource);
2938}
2939
2940static const struct wl_subcompositor_interface subcompositor_interface = {
2941 subcompositor_destroy,
2942 subcompositor_get_subsurface
2943};
2944
2945static void
2946bind_subcompositor(struct wl_client *client,
2947 void *data, uint32_t version, uint32_t id)
2948{
2949 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002950 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002951
Jason Ekstranda85118c2013-06-27 20:17:02 -05002952 resource =
2953 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002954 if (resource == NULL) {
2955 wl_client_post_no_memory(client);
2956 return;
2957 }
2958 wl_resource_set_implementation(resource, &subcompositor_interface,
2959 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002960}
2961
2962static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002963weston_compositor_dpms(struct weston_compositor *compositor,
2964 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002965{
2966 struct weston_output *output;
2967
2968 wl_list_for_each(output, &compositor->output_list, link)
2969 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002970 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002971}
2972
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002973WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002974weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002975{
Neil Roberts8b62e202013-09-30 13:14:47 +01002976 uint32_t old_state = compositor->state;
2977
2978 /* The state needs to be changed before emitting the wake
2979 * signal because that may try to schedule a repaint which
2980 * will not work if the compositor is still sleeping */
2981 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2982
2983 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002984 case WESTON_COMPOSITOR_SLEEPING:
2985 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2986 /* fall through */
2987 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002988 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002989 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002990 /* fall through */
2991 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002992 wl_event_source_timer_update(compositor->idle_source,
2993 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002994 }
2995}
2996
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002997WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002998weston_compositor_offscreen(struct weston_compositor *compositor)
2999{
3000 switch (compositor->state) {
3001 case WESTON_COMPOSITOR_OFFSCREEN:
3002 return;
3003 case WESTON_COMPOSITOR_SLEEPING:
3004 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3005 /* fall through */
3006 default:
3007 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3008 wl_event_source_timer_update(compositor->idle_source, 0);
3009 }
3010}
3011
3012WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003013weston_compositor_sleep(struct weston_compositor *compositor)
3014{
3015 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3016 return;
3017
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003018 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003019 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3020 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3021}
3022
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003023static int
3024idle_handler(void *data)
3025{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003026 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003027
3028 if (compositor->idle_inhibit)
3029 return 1;
3030
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003031 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003032 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003033
3034 return 1;
3035}
3036
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003037WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003038weston_plane_init(struct weston_plane *plane,
3039 struct weston_compositor *ec,
3040 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003041{
3042 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003043 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003044 plane->x = x;
3045 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003046 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003047
3048 /* Init the link so that the call to wl_list_remove() when releasing
3049 * the plane without ever stacking doesn't lead to a crash */
3050 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003051}
3052
3053WL_EXPORT void
3054weston_plane_release(struct weston_plane *plane)
3055{
Xiong Zhang97116532013-10-23 13:58:31 +08003056 struct weston_view *view;
3057
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003058 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003059 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003060
Xiong Zhang97116532013-10-23 13:58:31 +08003061 wl_list_for_each(view, &plane->compositor->view_list, link) {
3062 if (view->plane == plane)
3063 view->plane = NULL;
3064 }
3065
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003066 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003067}
3068
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003069WL_EXPORT void
3070weston_compositor_stack_plane(struct weston_compositor *ec,
3071 struct weston_plane *plane,
3072 struct weston_plane *above)
3073{
3074 if (above)
3075 wl_list_insert(above->link.prev, &plane->link);
3076 else
3077 wl_list_insert(&ec->plane_list, &plane->link);
3078}
3079
Casey Dahlin9074db52012-04-19 22:50:09 -04003080static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003081{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003082 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003083}
3084
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003085static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003086bind_output(struct wl_client *client,
3087 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003088{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003089 struct weston_output *output = data;
3090 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003091 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003092
Jason Ekstranda85118c2013-06-27 20:17:02 -05003093 resource = wl_resource_create(client, &wl_output_interface,
3094 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003095 if (resource == NULL) {
3096 wl_client_post_no_memory(client);
3097 return;
3098 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003099
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003100 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003101 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003102
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003103 wl_output_send_geometry(resource,
3104 output->x,
3105 output->y,
3106 output->mm_width,
3107 output->mm_height,
3108 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003109 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003110 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003111 if (version >= 2)
3112 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003113 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003114
3115 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003116 wl_output_send_mode(resource,
3117 mode->flags,
3118 mode->width,
3119 mode->height,
3120 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003121 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003122
3123 if (version >= 2)
3124 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003125}
3126
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003127/* Move other outputs when one is removed so the space remains contiguos. */
3128static void
3129weston_compositor_remove_output(struct weston_compositor *compositor,
3130 struct weston_output *remove_output)
3131{
3132 struct weston_output *output;
3133 int offset = 0;
3134
3135 wl_list_for_each(output, &compositor->output_list, link) {
3136 if (output == remove_output) {
3137 offset = output->width;
3138 continue;
3139 }
3140
3141 if (offset > 0) {
3142 weston_output_move(output,
3143 output->x - offset, output->y);
3144 output->dirty = 1;
3145 }
3146 }
3147}
3148
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003149WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003150weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003151{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003152 output->destroying = 1;
3153
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003154 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003155 wl_list_remove(&output->link);
3156
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003157 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003158 wl_signal_emit(&output->destroy_signal, output);
3159
Richard Hughesafe690c2013-05-02 10:10:04 +01003160 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003161 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003162 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003163 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003164
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003165 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003166}
3167
Scott Moreau1bad5db2012-08-18 01:04:05 -06003168static void
3169weston_output_compute_transform(struct weston_output *output)
3170{
3171 struct weston_matrix transform;
3172 int flip;
3173
3174 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003175 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003176
3177 switch(output->transform) {
3178 case WL_OUTPUT_TRANSFORM_FLIPPED:
3179 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3180 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3181 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003182 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003183 flip = -1;
3184 break;
3185 default:
3186 flip = 1;
3187 break;
3188 }
3189
3190 switch(output->transform) {
3191 case WL_OUTPUT_TRANSFORM_NORMAL:
3192 case WL_OUTPUT_TRANSFORM_FLIPPED:
3193 transform.d[0] = flip;
3194 transform.d[1] = 0;
3195 transform.d[4] = 0;
3196 transform.d[5] = 1;
3197 break;
3198 case WL_OUTPUT_TRANSFORM_90:
3199 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3200 transform.d[0] = 0;
3201 transform.d[1] = -flip;
3202 transform.d[4] = 1;
3203 transform.d[5] = 0;
3204 break;
3205 case WL_OUTPUT_TRANSFORM_180:
3206 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3207 transform.d[0] = -flip;
3208 transform.d[1] = 0;
3209 transform.d[4] = 0;
3210 transform.d[5] = -1;
3211 break;
3212 case WL_OUTPUT_TRANSFORM_270:
3213 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3214 transform.d[0] = 0;
3215 transform.d[1] = flip;
3216 transform.d[4] = -1;
3217 transform.d[5] = 0;
3218 break;
3219 default:
3220 break;
3221 }
3222
3223 weston_matrix_multiply(&output->matrix, &transform);
3224}
3225
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003226WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003227weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003228{
Scott Moreau850ca422012-05-21 15:21:25 -06003229 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003230
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003231 weston_matrix_init(&output->matrix);
3232 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003233 -(output->x + output->width / 2.0),
3234 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003235
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003236 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003237 2.0 / output->width,
3238 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003239
Scott Moreauccbf29d2012-02-22 14:21:41 -07003240 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003241 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003242 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003243 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3244 output->zoom.trans_y, 0);
3245 weston_matrix_scale(&output->matrix, magnification,
3246 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003247 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003248
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003249 weston_output_compute_transform(output);
3250
Scott Moreauccbf29d2012-02-22 14:21:41 -07003251 output->dirty = 0;
3252}
3253
Scott Moreau1bad5db2012-08-18 01:04:05 -06003254static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003255weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003256{
3257 output->transform = transform;
3258
3259 switch (transform) {
3260 case WL_OUTPUT_TRANSFORM_90:
3261 case WL_OUTPUT_TRANSFORM_270:
3262 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3263 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3264 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003265 output->width = output->current_mode->height;
3266 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003267 break;
3268 case WL_OUTPUT_TRANSFORM_NORMAL:
3269 case WL_OUTPUT_TRANSFORM_180:
3270 case WL_OUTPUT_TRANSFORM_FLIPPED:
3271 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003272 output->width = output->current_mode->width;
3273 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003274 break;
3275 default:
3276 break;
3277 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003278
Hardening57388e42013-09-18 23:56:36 +02003279 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003280 output->width /= scale;
3281 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003282}
3283
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003284static void
3285weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003286{
3287 output->x = x;
3288 output->y = y;
3289
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003290 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003291 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003292 output->width,
3293 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003294}
3295
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003296WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003297weston_output_move(struct weston_output *output, int x, int y)
3298{
3299 pixman_region32_t old_region;
3300 struct wl_resource *resource;
3301
3302 output->move_x = x - output->x;
3303 output->move_y = y - output->y;
3304
3305 if (output->move_x == 0 && output->move_y == 0)
3306 return;
3307
3308 pixman_region32_init(&old_region);
3309 pixman_region32_copy(&old_region, &output->region);
3310
3311 weston_output_init_geometry(output, x, y);
3312
3313 output->dirty = 1;
3314
3315 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003316 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003317
3318 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003319 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003320 wl_output_send_geometry(resource,
3321 output->x,
3322 output->y,
3323 output->mm_width,
3324 output->mm_height,
3325 output->subpixel,
3326 output->make,
3327 output->model,
3328 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003329
3330 if (wl_resource_get_version(resource) >= 2)
3331 wl_output_send_done(resource);
3332 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003333}
3334
3335WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003336weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003337 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003338 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003339{
3340 output->compositor = c;
3341 output->x = x;
3342 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003343 output->mm_width = mm_width;
3344 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003345 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003346 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003347
Alexander Larsson0b135062013-05-28 16:23:36 +02003348 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003349 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003350
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003351 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003352 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003353
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003354 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003355 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003356 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003357 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003358
Casey Dahlin58ba1372012-04-19 22:50:08 -04003359 output->id = ffs(~output->compositor->output_id_pool) - 1;
3360 output->compositor->output_id_pool |= 1 << output->id;
3361
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003362 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003363 wl_global_create(c->wl_display, &wl_output_interface, 2,
3364 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003365 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003366}
3367
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003368WL_EXPORT void
3369weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003370 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003371 wl_fixed_t *x, wl_fixed_t *y)
3372{
3373 wl_fixed_t tx, ty;
3374 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003375 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003376
Hardeningff39efa2013-09-18 23:56:35 +02003377 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3378 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003379
3380 switch(output->transform) {
3381 case WL_OUTPUT_TRANSFORM_NORMAL:
3382 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003383 tx = device_x;
3384 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003385 break;
3386 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003387 tx = device_y;
3388 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003389 break;
3390 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003391 tx = width - device_x;
3392 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003393 break;
3394 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003395 tx = width - device_y;
3396 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003397 break;
3398 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003399 tx = width - device_x;
3400 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003401 break;
3402 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003403 tx = width - device_y;
3404 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003405 break;
3406 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003407 tx = device_x;
3408 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003409 break;
3410 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003411 tx = device_y;
3412 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003413 break;
3414 }
3415
Neil Robertseb5a2002014-05-01 16:13:55 +01003416 tx /= output->current_scale;
3417 ty /= output->current_scale;
3418
3419 if (output->zoom.active) {
3420 zoom_scale = output->zoom.spring_z.current;
3421 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3422 output->width / 2.0f *
3423 (zoom_scale + output->zoom.trans_x));
3424 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3425 output->height / 2.0f *
3426 (zoom_scale + output->zoom.trans_y));
3427 tx = wl_fixed_from_double(zx);
3428 ty = wl_fixed_from_double(zy);
3429 }
3430
3431 *x = tx + wl_fixed_from_int(output->x);
3432 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003433}
3434
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003435static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003436destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003437{
Jonny Lamb74130762013-11-26 18:19:46 +01003438 struct weston_surface *surface =
3439 wl_resource_get_user_data(resource);
3440
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003441 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003442 surface->pending.buffer_viewport.buffer.src_width =
3443 wl_fixed_from_int(-1);
3444 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003445 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003446}
3447
3448static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003449viewport_destroy(struct wl_client *client,
3450 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003451{
3452 wl_resource_destroy(resource);
3453}
3454
3455static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003456viewport_set(struct wl_client *client,
3457 struct wl_resource *resource,
3458 wl_fixed_t src_x,
3459 wl_fixed_t src_y,
3460 wl_fixed_t src_width,
3461 wl_fixed_t src_height,
3462 int32_t dst_width,
3463 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003464{
Jonny Lamb74130762013-11-26 18:19:46 +01003465 struct weston_surface *surface =
3466 wl_resource_get_user_data(resource);
3467
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003468 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003469
Jonny Lamb8ae35902013-11-26 18:19:45 +01003470 if (wl_fixed_to_double(src_width) < 0 ||
3471 wl_fixed_to_double(src_height) < 0) {
3472 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003473 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003474 "source dimensions must be non-negative (%fx%f)",
3475 wl_fixed_to_double(src_width),
3476 wl_fixed_to_double(src_height));
3477 return;
3478 }
3479
3480 if (dst_width <= 0 || dst_height <= 0) {
3481 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003482 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003483 "destination dimensions must be positive (%dx%d)",
3484 dst_width, dst_height);
3485 return;
3486 }
3487
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003488 surface->pending.buffer_viewport.buffer.src_x = src_x;
3489 surface->pending.buffer_viewport.buffer.src_y = src_y;
3490 surface->pending.buffer_viewport.buffer.src_width = src_width;
3491 surface->pending.buffer_viewport.buffer.src_height = src_height;
3492 surface->pending.buffer_viewport.surface.width = dst_width;
3493 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003494 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003495}
3496
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003497static void
3498viewport_set_source(struct wl_client *client,
3499 struct wl_resource *resource,
3500 wl_fixed_t src_x,
3501 wl_fixed_t src_y,
3502 wl_fixed_t src_width,
3503 wl_fixed_t src_height)
3504{
3505 struct weston_surface *surface =
3506 wl_resource_get_user_data(resource);
3507
3508 assert(surface->viewport_resource != NULL);
3509
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003510 if (src_width == wl_fixed_from_int(-1) &&
3511 src_height == wl_fixed_from_int(-1)) {
3512 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003513 surface->pending.buffer_viewport.buffer.src_width =
3514 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003515 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003516 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003517 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003518
3519 if (src_width <= 0 || src_height <= 0) {
3520 wl_resource_post_error(resource,
3521 WL_VIEWPORT_ERROR_BAD_VALUE,
3522 "source size must be positive (%fx%f)",
3523 wl_fixed_to_double(src_width),
3524 wl_fixed_to_double(src_height));
3525 return;
3526 }
3527
3528 surface->pending.buffer_viewport.buffer.src_x = src_x;
3529 surface->pending.buffer_viewport.buffer.src_y = src_y;
3530 surface->pending.buffer_viewport.buffer.src_width = src_width;
3531 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003532 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003533}
3534
3535static void
3536viewport_set_destination(struct wl_client *client,
3537 struct wl_resource *resource,
3538 int32_t dst_width,
3539 int32_t dst_height)
3540{
3541 struct weston_surface *surface =
3542 wl_resource_get_user_data(resource);
3543
3544 assert(surface->viewport_resource != NULL);
3545
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003546 if (dst_width == -1 && dst_height == -1) {
3547 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003548 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003549 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003550 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003551 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003552
3553 if (dst_width <= 0 || dst_height <= 0) {
3554 wl_resource_post_error(resource,
3555 WL_VIEWPORT_ERROR_BAD_VALUE,
3556 "destination size must be positive (%dx%d)",
3557 dst_width, dst_height);
3558 return;
3559 }
3560
3561 surface->pending.buffer_viewport.surface.width = dst_width;
3562 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003563 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003564}
3565
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003566static const struct wl_viewport_interface viewport_interface = {
3567 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003568 viewport_set,
3569 viewport_set_source,
3570 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003571};
3572
3573static void
3574scaler_destroy(struct wl_client *client,
3575 struct wl_resource *resource)
3576{
3577 wl_resource_destroy(resource);
3578}
3579
3580static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003581scaler_get_viewport(struct wl_client *client,
3582 struct wl_resource *scaler,
3583 uint32_t id,
3584 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003585{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003586 int version = wl_resource_get_version(scaler);
3587 struct weston_surface *surface =
3588 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003589 struct wl_resource *resource;
3590
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003591 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003592 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003593 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3594 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003595 return;
3596 }
3597
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003598 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003599 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003600 if (resource == NULL) {
3601 wl_client_post_no_memory(client);
3602 return;
3603 }
3604
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003605 wl_resource_set_implementation(resource, &viewport_interface,
3606 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003607
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003608 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003609}
3610
3611static const struct wl_scaler_interface scaler_interface = {
3612 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003613 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003614};
3615
3616static void
3617bind_scaler(struct wl_client *client,
3618 void *data, uint32_t version, uint32_t id)
3619{
3620 struct wl_resource *resource;
3621
3622 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003623 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003624 if (resource == NULL) {
3625 wl_client_post_no_memory(client);
3626 return;
3627 }
3628
3629 wl_resource_set_implementation(resource, &scaler_interface,
3630 NULL, NULL);
3631}
3632
3633static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003634compositor_bind(struct wl_client *client,
3635 void *data, uint32_t version, uint32_t id)
3636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003637 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003638 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003639
Jason Ekstranda85118c2013-06-27 20:17:02 -05003640 resource = wl_resource_create(client, &wl_compositor_interface,
3641 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003642 if (resource == NULL) {
3643 wl_client_post_no_memory(client);
3644 return;
3645 }
3646
3647 wl_resource_set_implementation(resource, &compositor_interface,
3648 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003649}
3650
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003651static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003652log_uname(void)
3653{
3654 struct utsname usys;
3655
3656 uname(&usys);
3657
3658 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3659 usys.version, usys.machine);
3660}
3661
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003662WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003663weston_environment_get_fd(const char *env)
3664{
3665 char *e, *end;
3666 int fd, flags;
3667
3668 e = getenv(env);
3669 if (!e)
3670 return -1;
3671 fd = strtol(e, &end, 0);
3672 if (*end != '\0')
3673 return -1;
3674
3675 flags = fcntl(fd, F_GETFD);
3676 if (flags == -1)
3677 return -1;
3678
3679 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3680 unsetenv(env);
3681
3682 return fd;
3683}
3684
3685WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003686weston_compositor_init(struct weston_compositor *ec,
3687 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003688 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003689 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003690{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003691 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003692 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003693 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003694
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003695 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003696 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003697 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003698 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003699 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003700 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003701 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003702 wl_signal_init(&ec->idle_signal);
3703 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003704 wl_signal_init(&ec->show_input_panel_signal);
3705 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003706 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003707 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003708 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003709 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003710 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003711 wl_signal_init(&ec->session_signal);
3712 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003713
Casey Dahlin58ba1372012-04-19 22:50:08 -04003714 ec->output_id_pool = 0;
3715
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003716 if (!wl_global_create(display, &wl_compositor_interface, 3,
3717 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003718 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003719
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003720 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3721 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003722 return -1;
3723
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003724 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003725 ec, bind_scaler))
3726 return -1;
3727
Jason Ekstranda7af7042013-10-12 22:38:11 -05003728 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003729 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003730 wl_list_init(&ec->layer_list);
3731 wl_list_init(&ec->seat_list);
3732 wl_list_init(&ec->output_list);
3733 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003734 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003735 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003736 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003737 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003738 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003739
Xiong Zhang97116532013-10-23 13:58:31 +08003740 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003741 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003742
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003743 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3744 weston_config_section_get_string(s, "keymap_rules",
3745 (char **) &xkb_names.rules, NULL);
3746 weston_config_section_get_string(s, "keymap_model",
3747 (char **) &xkb_names.model, NULL);
3748 weston_config_section_get_string(s, "keymap_layout",
3749 (char **) &xkb_names.layout, NULL);
3750 weston_config_section_get_string(s, "keymap_variant",
3751 (char **) &xkb_names.variant, NULL);
3752 weston_config_section_get_string(s, "keymap_options",
3753 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003754
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003755 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3756 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003757
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003758 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003759
3760 wl_data_device_manager_init(ec->wl_display);
3761
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003762 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003763
Daniel Stone725c2c32012-06-22 14:04:36 +01003764 loop = wl_display_get_event_loop(ec->wl_display);
3765 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3766 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3767
3768 ec->input_loop = wl_event_loop_create();
3769
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003770 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3771 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3772
3773 weston_compositor_schedule_repaint(ec);
3774
Daniel Stone725c2c32012-06-22 14:04:36 +01003775 return 0;
3776}
3777
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003778WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003779weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003780{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003781 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003782
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003783 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003784 if (ec->input_loop_source)
3785 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003786
Matt Roper361d2ad2011-08-29 13:52:23 -07003787 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003788 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003789 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003790
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003791 if (ec->renderer)
3792 ec->renderer->destroy(ec);
3793
Daniel Stone325fc2d2012-05-30 16:31:58 +01003794 weston_binding_list_destroy_all(&ec->key_binding_list);
3795 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003796 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003797 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003798 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003799
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003800 weston_plane_release(&ec->primary_plane);
3801
Jonas Ådahlc97af922012-03-28 22:36:09 +02003802 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003803
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003804 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003805}
3806
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003807WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003808weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3809 const struct weston_pointer_grab_interface *interface)
3810{
3811 struct weston_seat *seat;
3812
3813 ec->default_pointer_grab = interface;
3814 wl_list_for_each(seat, &ec->seat_list, link) {
3815 if (seat->pointer) {
3816 weston_pointer_set_default_grab(seat->pointer,
3817 interface);
3818 }
3819 }
3820}
3821
3822WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003823weston_version(int *major, int *minor, int *micro)
3824{
3825 *major = WESTON_VERSION_MAJOR;
3826 *minor = WESTON_VERSION_MINOR;
3827 *micro = WESTON_VERSION_MICRO;
3828}
3829
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003830static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003831 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003832 const char *desc;
3833} capability_strings[] = {
3834 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003835 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003836};
3837
3838static void
3839weston_compositor_log_capabilities(struct weston_compositor *compositor)
3840{
3841 unsigned i;
3842 int yes;
3843
3844 weston_log("Compositor capabilities:\n");
3845 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3846 yes = compositor->capabilities & capability_strings[i].bit;
3847 weston_log_continue(STAMP_SPACE "%s %s\n",
3848 capability_strings[i].desc,
3849 yes ? "yes" : "no");
3850 }
3851}
3852
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003853static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003854{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003855 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003856
Martin Minarik6d118362012-06-07 18:01:59 +02003857 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003858 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003859
3860 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003861}
3862
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003863#ifdef HAVE_LIBUNWIND
3864
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003865static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003866print_backtrace(void)
3867{
3868 unw_cursor_t cursor;
3869 unw_context_t context;
3870 unw_word_t off;
3871 unw_proc_info_t pip;
3872 int ret, i = 0;
3873 char procname[256];
3874 const char *filename;
3875 Dl_info dlinfo;
3876
3877 pip.unwind_info = NULL;
3878 ret = unw_getcontext(&context);
3879 if (ret) {
3880 weston_log("unw_getcontext: %d\n", ret);
3881 return;
3882 }
3883
3884 ret = unw_init_local(&cursor, &context);
3885 if (ret) {
3886 weston_log("unw_init_local: %d\n", ret);
3887 return;
3888 }
3889
3890 ret = unw_step(&cursor);
3891 while (ret > 0) {
3892 ret = unw_get_proc_info(&cursor, &pip);
3893 if (ret) {
3894 weston_log("unw_get_proc_info: %d\n", ret);
3895 break;
3896 }
3897
3898 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3899 if (ret && ret != -UNW_ENOMEM) {
3900 if (ret != -UNW_EUNSPEC)
3901 weston_log("unw_get_proc_name: %d\n", ret);
3902 procname[0] = '?';
3903 procname[1] = 0;
3904 }
3905
3906 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3907 *dlinfo.dli_fname)
3908 filename = dlinfo.dli_fname;
3909 else
3910 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003911
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003912 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3913 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3914
3915 ret = unw_step(&cursor);
3916 if (ret < 0)
3917 weston_log("unw_step: %d\n", ret);
3918 }
3919}
3920
3921#else
3922
3923static void
3924print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003925{
3926 void *buffer[32];
3927 int i, count;
3928 Dl_info info;
3929
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003930 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3931 for (i = 0; i < count; i++) {
3932 dladdr(buffer[i], &info);
3933 weston_log(" [%016lx] %s (%s)\n",
3934 (long) buffer[i],
3935 info.dli_sname ? info.dli_sname : "--",
3936 info.dli_fname);
3937 }
3938}
3939
3940#endif
3941
3942static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003943on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003944{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003945 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003946 * then call the backend restore function, which will switch
3947 * back to the vt we launched from or ungrab X etc and then
3948 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003949 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003950 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003951 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003952
Peter Maatmane5b42e42013-03-27 22:38:53 +01003953 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003954
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003955 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003956
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003957 segv_compositor->restore(segv_compositor);
3958
3959 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003960}
3961
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003962WL_EXPORT void *
3963weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003964{
3965 char path[PATH_MAX];
3966 void *module, *init;
3967
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003968 if (name == NULL)
3969 return NULL;
3970
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003971 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003972 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003973 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003974 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003975
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003976 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3977 if (module) {
3978 weston_log("Module '%s' already loaded\n", path);
3979 dlclose(module);
3980 return NULL;
3981 }
3982
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003983 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003984 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003985 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003986 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003987 return NULL;
3988 }
3989
3990 init = dlsym(module, entrypoint);
3991 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003992 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003993 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003994 return NULL;
3995 }
3996
3997 return init;
3998}
3999
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004000static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004001load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004002 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004003{
4004 const char *p, *end;
4005 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004006 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004007 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004008
4009 if (modules == NULL)
4010 return 0;
4011
4012 p = modules;
4013 while (*p) {
4014 end = strchrnul(p, ',');
4015 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004016 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004017 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004018 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004019 p = end;
4020 while (*p == ',')
4021 p++;
4022
4023 }
4024
4025 return 0;
4026}
4027
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004028static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004029 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4030
4031static const char xdg_wrong_message[] =
4032 "fatal: environment variable XDG_RUNTIME_DIR\n"
4033 "is set to \"%s\", which is not a directory.\n";
4034
4035static const char xdg_wrong_mode_message[] =
4036 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004037 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4038 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004039
4040static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004041 "Refer to your distribution on how to get it, or\n"
4042 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4043 "on how to implement it.\n";
4044
Martin Minarik37032f82012-06-18 20:15:18 +02004045static void
4046verify_xdg_runtime_dir(void)
4047{
4048 char *dir = getenv("XDG_RUNTIME_DIR");
4049 struct stat s;
4050
4051 if (!dir) {
4052 weston_log(xdg_error_message);
4053 weston_log_continue(xdg_detail_message);
4054 exit(EXIT_FAILURE);
4055 }
4056
4057 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4058 weston_log(xdg_wrong_message, dir);
4059 weston_log_continue(xdg_detail_message);
4060 exit(EXIT_FAILURE);
4061 }
4062
4063 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4064 weston_log(xdg_wrong_mode_message,
4065 dir, s.st_mode & 0777, s.st_uid);
4066 weston_log_continue(xdg_detail_message);
4067 }
4068}
4069
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004070static int
4071usage(int error_code)
4072{
4073 fprintf(stderr,
4074 "Usage: weston [OPTIONS]\n\n"
4075 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4076 "Weston supports multiple backends, and depending on which backend is in use\n"
4077 "different options will be accepted.\n\n"
4078
4079
4080 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004081 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004082 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004083 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4084 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004085 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004086 " -S, --socket=NAME\tName of socket to listen on\n"
4087 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004088 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004089 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004090 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004091 " -h, --help\t\tThis help message\n\n");
4092
4093 fprintf(stderr,
4094 "Options for drm-backend.so:\n\n"
4095 " --connector=ID\tBring up only this connector\n"
4096 " --seat=SEAT\t\tThe seat that weston should run on\n"
4097 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004098 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004099 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4100
4101 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004102 "Options for fbdev-backend.so:\n\n"
4103 " --tty=TTY\t\tThe tty to use\n"
4104 " --device=DEVICE\tThe framebuffer device to use\n\n");
4105
4106 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004107 "Options for x11-backend.so:\n\n"
4108 " --width=WIDTH\t\tWidth of X window\n"
4109 " --height=HEIGHT\tHeight of X window\n"
4110 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004111 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004112 " --output-count=COUNT\tCreate multiple outputs\n"
4113 " --no-input\t\tDont create input devices\n\n");
4114
4115 fprintf(stderr,
4116 "Options for wayland-backend.so:\n\n"
4117 " --width=WIDTH\t\tWidth of Wayland surface\n"
4118 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004119 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004120 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004121 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004122 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004123 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004124 " --display=DISPLAY\tWayland display to connect to\n\n");
4125
Pekka Paalanene31e0532013-05-22 18:03:07 +03004126#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4127 fprintf(stderr,
4128 "Options for rpi-backend.so:\n\n"
4129 " --tty=TTY\t\tThe tty to use\n"
4130 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4131 " --transform=TR\tThe output transformation, TR is one of:\n"
4132 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004133 " --opaque-regions\tEnable support for opaque regions, can be "
4134 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004135 "\n");
4136#endif
4137
Hardeningc077a842013-07-08 00:51:35 +02004138#if defined(BUILD_RDP_COMPOSITOR)
4139 fprintf(stderr,
4140 "Options for rdp-backend.so:\n\n"
4141 " --width=WIDTH\t\tWidth of desktop\n"
4142 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004143 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4144 " --address=ADDR\tThe address to bind\n"
4145 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004146 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004147 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4148 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4149 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4150 "\n");
4151#endif
4152
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004153 exit(error_code);
4154}
4155
Peter Maatmane5b42e42013-03-27 22:38:53 +01004156static void
4157catch_signals(void)
4158{
4159 struct sigaction action;
4160
4161 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4162 action.sa_sigaction = on_caught_signal;
4163 sigemptyset(&action.sa_mask);
4164 sigaction(SIGSEGV, &action, NULL);
4165 sigaction(SIGABRT, &action, NULL);
4166}
4167
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004168static void
4169handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4170{
4171 struct wl_client *client = data;
4172
4173 weston_log("Primary client died. Closing...\n");
4174
4175 wl_display_terminate(wl_client_get_display(client));
4176}
4177
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004178int main(int argc, char *argv[])
4179{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004180 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004181 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004182 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004183 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004184 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004185 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004186 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004187 int *argc, char *argv[],
4188 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004189 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004190 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004191 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004192 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004193 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004194 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004195 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004196 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004197 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004198 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004199 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004200 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004201 int32_t noconfig = 0;
4202 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004203 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004204 struct wl_client *primary_client;
4205 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004206
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004207 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004208 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004209 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004210 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4211 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004212 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004213 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004214 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004215 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004216 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004217 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004218
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004219 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004220
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004221 if (help)
4222 usage(EXIT_SUCCESS);
4223
Scott Moreau12245142012-08-29 15:15:58 -06004224 if (version) {
4225 printf(PACKAGE_STRING "\n");
4226 return EXIT_SUCCESS;
4227 }
4228
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004229 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004230
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004231 weston_log("%s\n"
4232 STAMP_SPACE "%s\n"
4233 STAMP_SPACE "Bug reports to: %s\n"
4234 STAMP_SPACE "Build: %s\n",
4235 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004236 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004237 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004238
Martin Minarik37032f82012-06-18 20:15:18 +02004239 verify_xdg_runtime_dir();
4240
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004241 display = wl_display_create();
4242
Tiago Vignatti2116b892011-08-08 05:52:59 -07004243 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004244 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4245 display);
4246 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4247 display);
4248 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4249 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004250
4251 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004252 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4253 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004254
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304255 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4256 ret = EXIT_FAILURE;
4257 goto out_signals;
4258 }
4259
Pekka Paalanen588bee12014-05-07 16:26:25 +03004260 if (noconfig == 0)
4261 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004262 if (config != NULL) {
4263 weston_log("Using config file '%s'\n",
4264 weston_config_get_full_path(config));
4265 } else {
4266 weston_log("Starting with no config file.\n");
4267 }
4268 section = weston_config_get_section(config, "core", NULL, NULL);
4269
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004270 if (option_backend)
4271 backend = strdup(option_backend);
4272 else
4273 weston_config_section_get_string(section, "backend", &backend,
4274 NULL);
4275
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004276 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004277 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004278 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004279 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004280 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004281 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004282 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004283 }
4284
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004285 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004286 free(backend);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304287 if (!backend_init) {
4288 ret = EXIT_FAILURE;
4289 goto out_signals;
4290 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004291
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004292 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004293 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004294 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304295 ret = EXIT_FAILURE;
4296 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004297 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004298
Peter Maatmane5b42e42013-03-27 22:38:53 +01004299 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004300 segv_compositor = ec;
4301
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004302 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004303 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004304
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004305 setenv("WAYLAND_DISPLAY", socket_name, 1);
4306
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004307 if (option_shell)
4308 shell = strdup(option_shell);
4309 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004310 weston_config_section_get_string(section, "shell", &shell,
4311 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004312
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004313 if (load_modules(ec, shell, &argc, argv) < 0) {
4314 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004315 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004316 }
4317 free(shell);
4318
4319 weston_config_section_get_string(section, "modules", &modules, "");
4320 if (load_modules(ec, modules, &argc, argv) < 0) {
4321 free(modules);
4322 goto out;
4323 }
4324 free(modules);
4325
Ossama Othmana50e6e42013-05-14 09:48:26 -07004326 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004327 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004328
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004329 for (i = 1; i < argc; i++)
4330 weston_log("fatal: unhandled option: %s\n", argv[i]);
4331 if (argc > 1) {
4332 ret = EXIT_FAILURE;
4333 goto out;
4334 }
4335
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004336 weston_compositor_log_capabilities(ec);
4337
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004338 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4339 if (server_socket) {
4340 weston_log("Running with single client\n");
4341 fd = strtol(server_socket, &end, 0);
4342 if (*end != '\0')
4343 fd = -1;
4344 } else {
4345 fd = -1;
4346 }
4347
4348 if (fd != -1) {
4349 primary_client = wl_client_create(display, fd);
4350 if (!primary_client) {
4351 weston_log("fatal: failed to add client: %m\n");
4352 ret = EXIT_FAILURE;
4353 goto out;
4354 }
4355 primary_client_destroyed.notify =
4356 handle_primary_client_destroyed;
4357 wl_client_add_destroy_listener(primary_client,
4358 &primary_client_destroyed);
4359 } else {
4360 if (wl_display_add_socket(display, socket_name)) {
4361 weston_log("fatal: failed to add socket: %m\n");
4362 ret = EXIT_FAILURE;
4363 goto out;
4364 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004365 }
4366
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004367 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004368
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004369 wl_display_run(display);
4370
4371 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004372 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004373 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004374
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004375 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004376
Daniel Stone855028f2012-05-01 20:37:10 +01004377 weston_compositor_xkb_destroy(ec);
4378
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004379 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304380
4381out_signals:
4382 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4383 if (signals[i])
4384 wl_event_source_remove(signals[i]);
4385
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004386 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004387
Martin Minarik19e6f262012-06-07 13:08:46 +02004388 weston_log_file_close();
4389
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004390 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004391}