blob: 1d8d00e6b79402bd902a608b2ece6eaf0066cde7 [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 Paalanen0cbd3b52012-10-10 12:49:28 +0300323region_init_infinite(pixman_region32_t *region)
324{
325 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
326 UINT32_MAX, UINT32_MAX);
327}
328
Pekka Paalanene67858b2013-04-25 13:57:42 +0300329static struct weston_subsurface *
330weston_surface_to_subsurface(struct weston_surface *surface);
331
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332WL_EXPORT struct weston_view *
333weston_view_create(struct weston_surface *surface)
334{
335 struct weston_view *view;
336
337 view = calloc(1, sizeof *view);
338 if (view == NULL)
339 return NULL;
340
341 view->surface = surface;
342
Jason Ekstranda7af7042013-10-12 22:38:11 -0500343 /* Assign to surface */
344 wl_list_insert(&surface->views, &view->surface_link);
345
346 wl_signal_init(&view->destroy_signal);
347 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300348 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349
Xiong Zhang97116532013-10-23 13:58:31 +0800350 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300351 view->layer_link.layer = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500352
353 pixman_region32_init(&view->clip);
354
355 view->alpha = 1.0;
356 pixman_region32_init(&view->transform.opaque);
357
358 wl_list_init(&view->geometry.transformation_list);
359 wl_list_insert(&view->geometry.transformation_list,
360 &view->transform.position.link);
361 weston_matrix_init(&view->transform.position.matrix);
362 wl_list_init(&view->geometry.child_list);
363 pixman_region32_init(&view->transform.boundingbox);
364 view->transform.dirty = 1;
365
366 view->output = NULL;
367
368 return view;
369}
370
Jason Ekstrand108865d2014-06-26 10:04:49 -0700371struct weston_frame_callback {
372 struct wl_resource *resource;
373 struct wl_list link;
374};
375
Jason Ekstrand7b982072014-05-20 14:33:03 -0500376static void
377surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
378{
379 struct weston_surface_state *state =
380 container_of(listener, struct weston_surface_state,
381 buffer_destroy_listener);
382
383 state->buffer = NULL;
384}
385
386static void
387weston_surface_state_init(struct weston_surface_state *state)
388{
389 state->newly_attached = 0;
390 state->buffer = NULL;
391 state->buffer_destroy_listener.notify =
392 surface_state_handle_buffer_destroy;
393 state->sx = 0;
394 state->sy = 0;
395
396 pixman_region32_init(&state->damage);
397 pixman_region32_init(&state->opaque);
398 region_init_infinite(&state->input);
399
400 wl_list_init(&state->frame_callback_list);
401
402 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
403 state->buffer_viewport.buffer.scale = 1;
404 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
405 state->buffer_viewport.surface.width = -1;
406 state->buffer_viewport.changed = 0;
407}
408
409static void
410weston_surface_state_fini(struct weston_surface_state *state)
411{
412 struct weston_frame_callback *cb, *next;
413
414 wl_list_for_each_safe(cb, next,
415 &state->frame_callback_list, link)
416 wl_resource_destroy(cb->resource);
417
418 pixman_region32_fini(&state->input);
419 pixman_region32_fini(&state->opaque);
420 pixman_region32_fini(&state->damage);
421
422 if (state->buffer)
423 wl_list_remove(&state->buffer_destroy_listener.link);
424 state->buffer = NULL;
425}
426
427static void
428weston_surface_state_set_buffer(struct weston_surface_state *state,
429 struct weston_buffer *buffer)
430{
431 if (state->buffer == buffer)
432 return;
433
434 if (state->buffer)
435 wl_list_remove(&state->buffer_destroy_listener.link);
436 state->buffer = buffer;
437 if (state->buffer)
438 wl_signal_add(&state->buffer->destroy_signal,
439 &state->buffer_destroy_listener);
440}
441
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500442WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500443weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500444{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500445 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400446
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200447 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400448 if (surface == NULL)
449 return NULL;
450
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500451 wl_signal_init(&surface->destroy_signal);
452
453 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500454
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500455 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200456 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400457
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200458 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
459 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200460 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
461 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500462
463 weston_surface_state_init(&surface->pending);
464
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400465 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200466
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200467 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100468
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400469 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500470 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300471 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400472
Jason Ekstranda7af7042013-10-12 22:38:11 -0500473 wl_list_init(&surface->views);
474
475 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500476
Pekka Paalanene67858b2013-04-25 13:57:42 +0300477 wl_list_init(&surface->subsurface_list);
478 wl_list_init(&surface->subsurface_list_pending);
479
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400480 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500481}
482
Alex Wu8811bf92012-02-28 18:07:54 +0800483WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500484weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200485 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500486{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100487 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500488}
489
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400490WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500491weston_view_to_global_float(struct weston_view *view,
492 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200493{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200495 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
496
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200498
499 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200500 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700501 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200502 v.f[3]);
503 *x = 0;
504 *y = 0;
505 return;
506 }
507
508 *x = v.f[0] / v.f[3];
509 *y = v.f[1] / v.f[3];
510 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500511 *x = sx + view->geometry.x;
512 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200513 }
514}
515
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500516WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200517weston_transformed_coord(int width, int height,
518 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200519 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520 float sx, float sy, float *bx, float *by)
521{
522 switch (transform) {
523 case WL_OUTPUT_TRANSFORM_NORMAL:
524 default:
525 *bx = sx;
526 *by = sy;
527 break;
528 case WL_OUTPUT_TRANSFORM_FLIPPED:
529 *bx = width - sx;
530 *by = sy;
531 break;
532 case WL_OUTPUT_TRANSFORM_90:
533 *bx = height - sy;
534 *by = sx;
535 break;
536 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
537 *bx = height - sy;
538 *by = width - sx;
539 break;
540 case WL_OUTPUT_TRANSFORM_180:
541 *bx = width - sx;
542 *by = height - sy;
543 break;
544 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
545 *bx = sx;
546 *by = height - sy;
547 break;
548 case WL_OUTPUT_TRANSFORM_270:
549 *bx = sy;
550 *by = width - sx;
551 break;
552 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
553 *bx = sy;
554 *by = sx;
555 break;
556 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200557
558 *bx *= scale;
559 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200560}
561
562WL_EXPORT pixman_box32_t
563weston_transformed_rect(int width, int height,
564 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200565 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200566 pixman_box32_t rect)
567{
568 float x1, x2, y1, y2;
569
570 pixman_box32_t ret;
571
Alexander Larsson4ea95522013-05-22 14:41:37 +0200572 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200573 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200574 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200575 rect.x2, rect.y2, &x2, &y2);
576
577 if (x1 <= x2) {
578 ret.x1 = x1;
579 ret.x2 = x2;
580 } else {
581 ret.x1 = x2;
582 ret.x2 = x1;
583 }
584
585 if (y1 <= y2) {
586 ret.y1 = y1;
587 ret.y2 = y2;
588 } else {
589 ret.y1 = y2;
590 ret.y2 = y1;
591 }
592
593 return ret;
594}
595
596WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500597weston_transformed_region(int width, int height,
598 enum wl_output_transform transform,
599 int32_t scale,
600 pixman_region32_t *src, pixman_region32_t *dest)
601{
602 pixman_box32_t *src_rects, *dest_rects;
603 int nrects, i;
604
605 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
606 if (src != dest)
607 pixman_region32_copy(dest, src);
608 return;
609 }
610
611 src_rects = pixman_region32_rectangles(src, &nrects);
612 dest_rects = malloc(nrects * sizeof(*dest_rects));
613 if (!dest_rects)
614 return;
615
616 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
617 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
618 } else {
619 for (i = 0; i < nrects; i++) {
620 switch (transform) {
621 default:
622 case WL_OUTPUT_TRANSFORM_NORMAL:
623 dest_rects[i].x1 = src_rects[i].x1;
624 dest_rects[i].y1 = src_rects[i].y1;
625 dest_rects[i].x2 = src_rects[i].x2;
626 dest_rects[i].y2 = src_rects[i].y2;
627 break;
628 case WL_OUTPUT_TRANSFORM_90:
629 dest_rects[i].x1 = height - src_rects[i].y2;
630 dest_rects[i].y1 = src_rects[i].x1;
631 dest_rects[i].x2 = height - src_rects[i].y1;
632 dest_rects[i].y2 = src_rects[i].x2;
633 break;
634 case WL_OUTPUT_TRANSFORM_180:
635 dest_rects[i].x1 = width - src_rects[i].x2;
636 dest_rects[i].y1 = height - src_rects[i].y2;
637 dest_rects[i].x2 = width - src_rects[i].x1;
638 dest_rects[i].y2 = height - src_rects[i].y1;
639 break;
640 case WL_OUTPUT_TRANSFORM_270:
641 dest_rects[i].x1 = src_rects[i].y1;
642 dest_rects[i].y1 = width - src_rects[i].x2;
643 dest_rects[i].x2 = src_rects[i].y2;
644 dest_rects[i].y2 = width - src_rects[i].x1;
645 break;
646 case WL_OUTPUT_TRANSFORM_FLIPPED:
647 dest_rects[i].x1 = width - src_rects[i].x2;
648 dest_rects[i].y1 = src_rects[i].y1;
649 dest_rects[i].x2 = width - src_rects[i].x1;
650 dest_rects[i].y2 = src_rects[i].y2;
651 break;
652 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
653 dest_rects[i].x1 = height - src_rects[i].y2;
654 dest_rects[i].y1 = width - src_rects[i].x2;
655 dest_rects[i].x2 = height - src_rects[i].y1;
656 dest_rects[i].y2 = width - src_rects[i].x1;
657 break;
658 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
659 dest_rects[i].x1 = src_rects[i].x1;
660 dest_rects[i].y1 = height - src_rects[i].y2;
661 dest_rects[i].x2 = src_rects[i].x2;
662 dest_rects[i].y2 = height - src_rects[i].y1;
663 break;
664 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
665 dest_rects[i].x1 = src_rects[i].y1;
666 dest_rects[i].y1 = src_rects[i].x1;
667 dest_rects[i].x2 = src_rects[i].y2;
668 dest_rects[i].y2 = src_rects[i].x2;
669 break;
670 }
671 }
672 }
673
674 if (scale != 1) {
675 for (i = 0; i < nrects; i++) {
676 dest_rects[i].x1 *= scale;
677 dest_rects[i].x2 *= scale;
678 dest_rects[i].y1 *= scale;
679 dest_rects[i].y2 *= scale;
680 }
681 }
682
683 pixman_region32_clear(dest);
684 pixman_region32_init_rects(dest, dest_rects, nrects);
685 free(dest_rects);
686}
687
Jonny Lamb74130762013-11-26 18:19:46 +0100688static void
689scaler_surface_to_buffer(struct weston_surface *surface,
690 float sx, float sy, float *bx, float *by)
691{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200692 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200693 double src_width, src_height;
694 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200695
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200696 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
697 if (vp->surface.width == -1) {
698 *bx = sx;
699 *by = sy;
700 return;
701 }
Jonny Lamb74130762013-11-26 18:19:46 +0100702
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200703 src_x = 0.0;
704 src_y = 0.0;
705 src_width = surface->width_from_buffer;
706 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100707 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200708 src_x = wl_fixed_to_double(vp->buffer.src_x);
709 src_y = wl_fixed_to_double(vp->buffer.src_y);
710 src_width = wl_fixed_to_double(vp->buffer.src_width);
711 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100712 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200713
714 *bx = sx * src_width / surface->width + src_x;
715 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100716}
717
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500718WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200719weston_surface_to_buffer_float(struct weston_surface *surface,
720 float sx, float sy, float *bx, float *by)
721{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200722 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
723
Jonny Lamb74130762013-11-26 18:19:46 +0100724 /* first transform coordinates if the scaler is set */
725 scaler_surface_to_buffer(surface, sx, sy, bx, by);
726
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500727 weston_transformed_coord(surface->width_from_buffer,
728 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200729 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100730 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200731}
732
Alexander Larsson4ea95522013-05-22 14:41:37 +0200733WL_EXPORT void
734weston_surface_to_buffer(struct weston_surface *surface,
735 int sx, int sy, int *bx, int *by)
736{
737 float bxf, byf;
738
Jonny Lamb74130762013-11-26 18:19:46 +0100739 weston_surface_to_buffer_float(surface,
740 sx, sy, &bxf, &byf);
741
Alexander Larsson4ea95522013-05-22 14:41:37 +0200742 *bx = floorf(bxf);
743 *by = floorf(byf);
744}
745
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200746WL_EXPORT pixman_box32_t
747weston_surface_to_buffer_rect(struct weston_surface *surface,
748 pixman_box32_t rect)
749{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200750 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100751 float xf, yf;
752
753 /* first transform box coordinates if the scaler is set */
754 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
755 rect.x1 = floorf(xf);
756 rect.y1 = floorf(yf);
757
758 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
759 rect.x2 = floorf(xf);
760 rect.y2 = floorf(yf);
761
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500762 return weston_transformed_rect(surface->width_from_buffer,
763 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200764 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200765 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200766}
767
768WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500769weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400770 struct weston_plane *plane)
771{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500772 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400773 return;
774
Jason Ekstranda7af7042013-10-12 22:38:11 -0500775 weston_view_damage_below(view);
776 view->plane = plane;
777 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400778}
779
780WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500781weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200782{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500783 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200784
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500785 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500786 pixman_region32_subtract(&damage, &view->transform.boundingbox,
787 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800788 if (view->plane)
789 pixman_region32_union(&view->plane->damage,
790 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500791 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800792 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200793}
794
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400795static void
796weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
797{
798 uint32_t different = es->output_mask ^ mask;
799 uint32_t entered = mask & different;
800 uint32_t left = es->output_mask & different;
801 struct weston_output *output;
802 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500803 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400804
805 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500806 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400807 return;
808 if (different == 0)
809 return;
810
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500811 client = wl_resource_get_client(es->resource);
812
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400813 wl_list_for_each(output, &es->compositor->output_list, link) {
814 if (1 << output->id & different)
815 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500816 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400817 client);
818 if (resource == NULL)
819 continue;
820 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500821 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400822 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500823 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400824 }
825}
826
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200827
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400828static void
829weston_surface_assign_output(struct weston_surface *es)
830{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500831 struct weston_output *new_output;
832 struct weston_view *view;
833 pixman_region32_t region;
834 uint32_t max, area, mask;
835 pixman_box32_t *e;
836
837 new_output = NULL;
838 max = 0;
839 mask = 0;
840 pixman_region32_init(&region);
841 wl_list_for_each(view, &es->views, surface_link) {
842 if (!view->output)
843 continue;
844
845 pixman_region32_intersect(&region, &view->transform.boundingbox,
846 &view->output->region);
847
848 e = pixman_region32_extents(&region);
849 area = (e->x2 - e->x1) * (e->y2 - e->y1);
850
851 mask |= view->output_mask;
852
853 if (area >= max) {
854 new_output = view->output;
855 max = area;
856 }
857 }
858 pixman_region32_fini(&region);
859
860 es->output = new_output;
861 weston_surface_update_output_mask(es, mask);
862}
863
864static void
865weston_view_assign_output(struct weston_view *ev)
866{
867 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400868 struct weston_output *output, *new_output;
869 pixman_region32_t region;
870 uint32_t max, area, mask;
871 pixman_box32_t *e;
872
873 new_output = NULL;
874 max = 0;
875 mask = 0;
876 pixman_region32_init(&region);
877 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500878 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400879 &output->region);
880
881 e = pixman_region32_extents(&region);
882 area = (e->x2 - e->x1) * (e->y2 - e->y1);
883
884 if (area > 0)
885 mask |= 1 << output->id;
886
887 if (area >= max) {
888 new_output = output;
889 max = area;
890 }
891 }
892 pixman_region32_fini(&region);
893
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894 ev->output = new_output;
895 ev->output_mask = mask;
896
897 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400898}
899
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200900static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500901view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
902 int32_t width, int32_t height,
903 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200904{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200905 float min_x = HUGE_VALF, min_y = HUGE_VALF;
906 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200907 int32_t s[4][2] = {
908 { sx, sy },
909 { sx, sy + height },
910 { sx + width, sy },
911 { sx + width, sy + height }
912 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200913 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200914 int i;
915
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300916 if (width == 0 || height == 0) {
917 /* avoid rounding empty bbox to 1x1 */
918 pixman_region32_init(bbox);
919 return;
920 }
921
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200922 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200923 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200925 if (x < min_x)
926 min_x = x;
927 if (x > max_x)
928 max_x = x;
929 if (y < min_y)
930 min_y = y;
931 if (y > max_y)
932 max_y = y;
933 }
934
Pekka Paalanen219b9822012-02-08 15:38:37 +0200935 int_x = floorf(min_x);
936 int_y = floorf(min_y);
937 pixman_region32_init_rect(bbox, int_x, int_y,
938 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200939}
940
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200941static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200943{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200945
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200946 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 view->geometry.x = roundf(view->geometry.x);
948 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200949
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500950 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500951 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
952 view->transform.position.matrix.d[12] = view->geometry.x;
953 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500956
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 view->transform.inverse = view->transform.position.matrix;
958 view->transform.inverse.d[12] = -view->geometry.x;
959 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500960
Jason Ekstranda7af7042013-10-12 22:38:11 -0500961 pixman_region32_init_rect(&view->transform.boundingbox,
962 view->geometry.x,
963 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600964 view->surface->width,
965 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500966
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 if (view->alpha == 1.0) {
968 pixman_region32_copy(&view->transform.opaque,
969 &view->surface->opaque);
970 pixman_region32_translate(&view->transform.opaque,
971 view->geometry.x,
972 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500973 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200974}
975
976static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200978{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 struct weston_view *parent = view->geometry.parent;
980 struct weston_matrix *matrix = &view->transform.matrix;
981 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200982 struct weston_transform *tform;
983
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200985
986 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
988 view->transform.position.matrix.d[12] = view->geometry.x;
989 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200990
991 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200993 weston_matrix_multiply(matrix, &tform->matrix);
994
Pekka Paalanen483243f2013-03-08 14:56:50 +0200995 if (parent)
996 weston_matrix_multiply(matrix, &parent->transform.matrix);
997
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200998 if (weston_matrix_invert(inverse, matrix) < 0) {
999 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 weston_log("error: weston_view %p"
1001 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001002 return -1;
1003 }
1004
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001005 view_compute_bbox(view, 0, 0,
1006 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001008
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001009 return 0;
1010}
1011
1012WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001014{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001016
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001018 return;
1019
Pekka Paalanen483243f2013-03-08 14:56:50 +02001020 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001022
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001024
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 pixman_region32_fini(&view->transform.boundingbox);
1028 pixman_region32_fini(&view->transform.opaque);
1029 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001030
Pekka Paalanencd403622012-01-25 13:37:39 +02001031 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 if (view->geometry.transformation_list.next ==
1033 &view->transform.position.link &&
1034 view->geometry.transformation_list.prev ==
1035 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001036 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001038 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 if (weston_view_update_transform_enable(view) < 0)
1040 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001041 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001042
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001044
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001046
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 wl_signal_emit(&view->surface->compositor->transform_signal,
1048 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001049}
1050
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001051WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001053{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001055
1056 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 * The invariant: if view->geometry.dirty, then all views
1058 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001059 * Corollary: if not parent->geometry.dirty, then all ancestors
1060 * are not dirty.
1061 */
1062
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001064 return;
1065
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001067
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001069 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001071}
1072
1073WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074weston_view_to_global_fixed(struct weston_view *view,
1075 wl_fixed_t vx, wl_fixed_t vy,
1076 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001077{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001078 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 weston_view_to_global_float(view,
1081 wl_fixed_to_double(vx),
1082 wl_fixed_to_double(vy),
1083 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001084 *x = wl_fixed_from_double(xf);
1085 *y = wl_fixed_from_double(yf);
1086}
1087
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001088WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089weston_view_from_global_float(struct weston_view *view,
1090 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001091{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001093 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1094
Jason Ekstranda7af7042013-10-12 22:38:11 -05001095 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001096
1097 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001098 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001100 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 *vx = 0;
1102 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001103 return;
1104 }
1105
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 *vx = v.f[0] / v.f[3];
1107 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001108 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 *vx = x - view->geometry.x;
1110 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001111 }
1112}
1113
1114WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115weston_view_from_global_fixed(struct weston_view *view,
1116 wl_fixed_t x, wl_fixed_t y,
1117 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001118{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 weston_view_from_global_float(view,
1122 wl_fixed_to_double(x),
1123 wl_fixed_to_double(y),
1124 &vxf, &vyf);
1125 *vx = wl_fixed_from_double(vxf);
1126 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001127}
1128
1129WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130weston_view_from_global(struct weston_view *view,
1131 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001132{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1136 *vx = floorf(vxf);
1137 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001138}
1139
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001140WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001141weston_surface_schedule_repaint(struct weston_surface *surface)
1142{
1143 struct weston_output *output;
1144
1145 wl_list_for_each(output, &surface->compositor->output_list, link)
1146 if (surface->output_mask & (1 << output->id))
1147 weston_output_schedule_repaint(output);
1148}
1149
1150WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151weston_view_schedule_repaint(struct weston_view *view)
1152{
1153 struct weston_output *output;
1154
1155 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1156 if (view->output_mask & (1 << output->id))
1157 weston_output_schedule_repaint(output);
1158}
1159
1160WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001161weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001162{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001163 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 0, 0, surface->width,
1165 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001166
Kristian Høgsberg98238702012-08-03 16:29:12 -04001167 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001168}
1169
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001170WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001171weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001172{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001173 if (view->geometry.x == x && view->geometry.y == y)
1174 return;
1175
Jason Ekstranda7af7042013-10-12 22:38:11 -05001176 view->geometry.x = x;
1177 view->geometry.y = y;
1178 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001179}
1180
Pekka Paalanen483243f2013-03-08 14:56:50 +02001181static void
1182transform_parent_handle_parent_destroy(struct wl_listener *listener,
1183 void *data)
1184{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 struct weston_view *view =
1186 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001187 geometry.parent_destroy_listener);
1188
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001190}
1191
1192WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193weston_view_set_transform_parent(struct weston_view *view,
1194 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001195{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001196 if (view->geometry.parent) {
1197 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1198 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001199 }
1200
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001204 transform_parent_handle_parent_destroy;
1205 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001206 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001208 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001209 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001210 }
1211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 weston_view_geometry_dirty(view);
1213}
1214
1215WL_EXPORT int
1216weston_view_is_mapped(struct weston_view *view)
1217{
1218 if (view->output)
1219 return 1;
1220 else
1221 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222}
1223
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001224WL_EXPORT int
1225weston_surface_is_mapped(struct weston_surface *surface)
1226{
1227 if (surface->output)
1228 return 1;
1229 else
1230 return 0;
1231}
1232
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001233static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001234surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001235{
1236 struct weston_view *view;
1237
1238 if (surface->width == width && surface->height == height)
1239 return;
1240
1241 surface->width = width;
1242 surface->height = height;
1243
1244 wl_list_for_each(view, &surface->views, surface_link)
1245 weston_view_geometry_dirty(view);
1246}
1247
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001248WL_EXPORT void
1249weston_surface_set_size(struct weston_surface *surface,
1250 int32_t width, int32_t height)
1251{
1252 assert(!surface->resource);
1253 surface_set_size(surface, width, height);
1254}
1255
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001256static int
1257fixed_round_up_to_int(wl_fixed_t f)
1258{
1259 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1260}
1261
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001262static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001263weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001264{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001265 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001266 int32_t width, height;
1267
1268 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001269 surface->width_from_buffer = 0;
1270 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001271 return;
1272 }
1273
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001274 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001275 case WL_OUTPUT_TRANSFORM_90:
1276 case WL_OUTPUT_TRANSFORM_270:
1277 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1278 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001279 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1280 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001281 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001282 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001283 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1284 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001285 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001286 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001287
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001288 surface->width_from_buffer = width;
1289 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001290}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001291
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001292static void
1293weston_surface_update_size(struct weston_surface *surface)
1294{
1295 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1296 int32_t width, height;
1297
1298 width = surface->width_from_buffer;
1299 height = surface->height_from_buffer;
1300
1301 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001302 surface_set_size(surface,
1303 vp->surface.width, vp->surface.height);
1304 return;
1305 }
1306
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001307 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001308 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1309 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1310
1311 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001312 return;
1313 }
1314
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001315 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001316}
1317
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001318WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001319weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001320{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001321 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001322
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001323 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001324
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001325 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001326}
1327
Jason Ekstranda7af7042013-10-12 22:38:11 -05001328WL_EXPORT struct weston_view *
1329weston_compositor_pick_view(struct weston_compositor *compositor,
1330 wl_fixed_t x, wl_fixed_t y,
1331 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001332{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001334
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 wl_list_for_each(view, &compositor->view_list, link) {
1336 weston_view_from_global_fixed(view, x, y, vx, vy);
1337 if (pixman_region32_contains_point(&view->surface->input,
1338 wl_fixed_to_int(*vx),
1339 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001340 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001342 }
1343
1344 return NULL;
1345}
1346
1347static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001348weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001349{
Daniel Stone37816df2012-05-16 18:45:18 +01001350 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001351
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001352 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001353 return;
1354
Daniel Stone37816df2012-05-16 18:45:18 +01001355 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001356 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001357}
1358
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001359WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001361{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001362 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 if (!weston_view_is_mapped(view))
1365 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367 weston_view_damage_below(view);
1368 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001369 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001370 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001371 wl_list_remove(&view->link);
1372 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 view->output_mask = 0;
1374 weston_surface_assign_output(view->surface);
1375
1376 if (weston_surface_is_mapped(view->surface))
1377 return;
1378
1379 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1380 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001381 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001382 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001383 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001384 NULL,
1385 wl_fixed_from_int(0),
1386 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001388 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001389 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001390}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392WL_EXPORT void
1393weston_surface_unmap(struct weston_surface *surface)
1394{
1395 struct weston_view *view;
1396
1397 wl_list_for_each(view, &surface->views, surface_link)
1398 weston_view_unmap(view);
1399 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001400}
1401
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001402static void
1403weston_surface_reset_pending_buffer(struct weston_surface *surface)
1404{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001405 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001406 surface->pending.sx = 0;
1407 surface->pending.sy = 0;
1408 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001409 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001410}
1411
Jason Ekstranda7af7042013-10-12 22:38:11 -05001412WL_EXPORT void
1413weston_view_destroy(struct weston_view *view)
1414{
1415 wl_signal_emit(&view->destroy_signal, view);
1416
1417 assert(wl_list_empty(&view->geometry.child_list));
1418
1419 if (weston_view_is_mapped(view)) {
1420 weston_view_unmap(view);
1421 weston_compositor_build_view_list(view->surface->compositor);
1422 }
1423
1424 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001425 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426
1427 pixman_region32_fini(&view->clip);
1428 pixman_region32_fini(&view->transform.boundingbox);
1429
1430 weston_view_set_transform_parent(view, NULL);
1431
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 wl_list_remove(&view->surface_link);
1433
1434 free(view);
1435}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001436
1437WL_EXPORT void
1438weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001439{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001440 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001442
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001443 if (--surface->ref_count > 0)
1444 return;
1445
1446 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1447
Pekka Paalanene67858b2013-04-25 13:57:42 +03001448 assert(wl_list_empty(&surface->subsurface_list_pending));
1449 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001450
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1452 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001453
Jason Ekstrand7b982072014-05-20 14:33:03 -05001454 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001455
Pekka Paalanende685b82012-12-04 15:58:12 +02001456 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001457
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001458 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001459 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001460 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001461
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001462 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001463 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001464
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001465 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001466}
1467
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001468static void
1469destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001470{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001471 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001472
Giulio Camuffo0d379742013-11-15 22:06:15 +01001473 /* Set the resource to NULL, since we don't want to leave a
1474 * dangling pointer if the surface was refcounted and survives
1475 * the weston_surface_destroy() call. */
1476 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001477 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001478}
1479
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001480static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001481weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1482{
1483 struct weston_buffer *buffer =
1484 container_of(listener, struct weston_buffer, destroy_listener);
1485
1486 wl_signal_emit(&buffer->destroy_signal, buffer);
1487 free(buffer);
1488}
1489
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001490WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001491weston_buffer_from_resource(struct wl_resource *resource)
1492{
1493 struct weston_buffer *buffer;
1494 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001495
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001496 listener = wl_resource_get_destroy_listener(resource,
1497 weston_buffer_destroy_handler);
1498
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001499 if (listener)
1500 return container_of(listener, struct weston_buffer,
1501 destroy_listener);
1502
1503 buffer = zalloc(sizeof *buffer);
1504 if (buffer == NULL)
1505 return NULL;
1506
1507 buffer->resource = resource;
1508 wl_signal_init(&buffer->destroy_signal);
1509 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001510 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001511 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001512
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001513 return buffer;
1514}
1515
1516static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001517weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1518 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001519{
Pekka Paalanende685b82012-12-04 15:58:12 +02001520 struct weston_buffer_reference *ref =
1521 container_of(listener, struct weston_buffer_reference,
1522 destroy_listener);
1523
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001524 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001525 ref->buffer = NULL;
1526}
1527
1528WL_EXPORT void
1529weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001530 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001531{
1532 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001533 ref->buffer->busy_count--;
1534 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001535 assert(wl_resource_get_client(ref->buffer->resource));
1536 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001537 WL_BUFFER_RELEASE);
1538 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001539 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001540 }
1541
Pekka Paalanende685b82012-12-04 15:58:12 +02001542 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001543 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001544 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001545 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001546 }
1547
Pekka Paalanende685b82012-12-04 15:58:12 +02001548 ref->buffer = buffer;
1549 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1550}
1551
1552static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001553weston_surface_attach(struct weston_surface *surface,
1554 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001555{
1556 weston_buffer_reference(&surface->buffer_ref, buffer);
1557
Pekka Paalanena6421c42012-12-04 15:58:10 +02001558 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001559 if (weston_surface_is_mapped(surface))
1560 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001561 }
1562
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001563 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001564
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001565 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001566}
1567
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001568WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001569weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001570{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001571 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001572
1573 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001574 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001575}
1576
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001577WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001578weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001579{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001580 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001581
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001582 pixman_region32_union(&compositor->primary_plane.damage,
1583 &compositor->primary_plane.damage,
1584 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001585 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001586}
1587
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001588static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001589surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001590{
Pekka Paalanende685b82012-12-04 15:58:12 +02001591 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001592 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001593 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001594
Jason Ekstrandef540082014-06-26 10:37:36 -07001595 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001596}
1597
1598static void
1599view_accumulate_damage(struct weston_view *view,
1600 pixman_region32_t *opaque)
1601{
1602 pixman_region32_t damage;
1603
1604 pixman_region32_init(&damage);
1605 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001606 pixman_box32_t *extents;
1607
Jason Ekstranda7af7042013-10-12 22:38:11 -05001608 extents = pixman_region32_extents(&view->surface->damage);
1609 view_compute_bbox(view, extents->x1, extents->y1,
1610 extents->x2 - extents->x1,
1611 extents->y2 - extents->y1,
1612 &damage);
1613 pixman_region32_translate(&damage,
1614 -view->plane->x,
1615 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001616 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001617 pixman_region32_copy(&damage, &view->surface->damage);
1618 pixman_region32_translate(&damage,
1619 view->geometry.x - view->plane->x,
1620 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001621 }
1622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 pixman_region32_subtract(&damage, &damage, opaque);
1624 pixman_region32_union(&view->plane->damage,
1625 &view->plane->damage, &damage);
1626 pixman_region32_fini(&damage);
1627 pixman_region32_copy(&view->clip, opaque);
1628 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001629}
1630
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001631static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001632compositor_accumulate_damage(struct weston_compositor *ec)
1633{
1634 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001636 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001637
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001638 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001639
1640 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001641 pixman_region32_copy(&plane->clip, &clip);
1642
1643 pixman_region32_init(&opaque);
1644
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 wl_list_for_each(ev, &ec->view_list, link) {
1646 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001647 continue;
1648
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001650 }
1651
1652 pixman_region32_union(&clip, &clip, &opaque);
1653 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001654 }
1655
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001656 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001657
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658 wl_list_for_each(ev, &ec->view_list, link)
1659 ev->surface->touched = 0;
1660
1661 wl_list_for_each(ev, &ec->view_list, link) {
1662 if (ev->surface->touched)
1663 continue;
1664 ev->surface->touched = 1;
1665
1666 surface_flush_damage(ev->surface);
1667
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001668 /* Both the renderer and the backend have seen the buffer
1669 * by now. If renderer needs the buffer, it has its own
1670 * reference set. If the backend wants to keep the buffer
1671 * around for migrating the surface into a non-primary plane
1672 * later, keep_buffer is true. Otherwise, drop the core
1673 * reference now, and allow early buffer release. This enables
1674 * clients to use single-buffering.
1675 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001676 if (!ev->surface->keep_buffer)
1677 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001678 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001679}
1680
1681static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001683{
1684 struct weston_subsurface *sub;
1685
Pekka Paalanene67858b2013-04-25 13:57:42 +03001686 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001687 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001688 continue;
1689
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1691 wl_list_init(&sub->surface->views);
1692
1693 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001694 }
1695}
1696
1697static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001699{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 struct weston_subsurface *sub;
1701 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001702
Jason Ekstranda7af7042013-10-12 22:38:11 -05001703 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1704 if (sub->surface == surface)
1705 continue;
1706
George Kiagiadakised04d382014-06-13 18:10:26 +02001707 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1708 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001710 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001711
1712 surface_free_unused_subsurface_views(sub->surface);
1713 }
1714}
1715
1716static void
1717view_list_add_subsurface_view(struct weston_compositor *compositor,
1718 struct weston_subsurface *sub,
1719 struct weston_view *parent)
1720{
1721 struct weston_subsurface *child;
1722 struct weston_view *view = NULL, *iv;
1723
1724 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1725 if (iv->geometry.parent == parent) {
1726 view = iv;
1727 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001728 }
1729 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730
1731 if (view) {
1732 /* Put it back in the surface's list of views */
1733 wl_list_remove(&view->surface_link);
1734 wl_list_insert(&sub->surface->views, &view->surface_link);
1735 } else {
1736 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001737 weston_view_set_position(view,
1738 sub->position.x,
1739 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001740 weston_view_set_transform_parent(view, parent);
1741 }
1742
1743 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744
Pekka Paalanenb188e912013-11-19 14:03:35 +02001745 if (wl_list_empty(&sub->surface->subsurface_list)) {
1746 wl_list_insert(compositor->view_list.prev, &view->link);
1747 return;
1748 }
1749
1750 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1751 if (child->surface == sub->surface)
1752 wl_list_insert(compositor->view_list.prev, &view->link);
1753 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001754 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001755 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001756}
1757
1758static void
1759view_list_add(struct weston_compositor *compositor,
1760 struct weston_view *view)
1761{
1762 struct weston_subsurface *sub;
1763
1764 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765
Pekka Paalanenb188e912013-11-19 14:03:35 +02001766 if (wl_list_empty(&view->surface->subsurface_list)) {
1767 wl_list_insert(compositor->view_list.prev, &view->link);
1768 return;
1769 }
1770
1771 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1772 if (sub->surface == view->surface)
1773 wl_list_insert(compositor->view_list.prev, &view->link);
1774 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001776 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777}
1778
1779static void
1780weston_compositor_build_view_list(struct weston_compositor *compositor)
1781{
1782 struct weston_view *view;
1783 struct weston_layer *layer;
1784
1785 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001786 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787 surface_stash_subsurface_views(view->surface);
1788
1789 wl_list_init(&compositor->view_list);
1790 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001791 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 view_list_add(compositor, view);
1793 }
1794 }
1795
1796 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001797 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001798 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001799}
1800
David Herrmann1edf44c2013-10-22 17:11:26 +02001801static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001802weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001803{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001804 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001805 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001806 struct weston_animation *animation, *next;
1807 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001808 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001809 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001810 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001811
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001812 if (output->destroying)
1813 return 0;
1814
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001815 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001816 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001817
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001818 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001819 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001820 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 wl_list_for_each(ev, &ec->view_list, link)
1822 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001823
Pekka Paalanene67858b2013-04-25 13:57:42 +03001824 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825 wl_list_for_each(ev, &ec->view_list, link) {
1826 /* Note: This operation is safe to do multiple times on the
1827 * same surface.
1828 */
1829 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001830 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 &ev->surface->frame_callback_list);
1832 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001833 }
1834 }
1835
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001836 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001837
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001838 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001839 pixman_region32_intersect(&output_damage,
1840 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001841 pixman_region32_subtract(&output_damage,
1842 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001843
Scott Moreauccbf29d2012-02-22 14:21:41 -07001844 if (output->dirty)
1845 weston_output_update_matrix(output);
1846
David Herrmann1edf44c2013-10-22 17:11:26 +02001847 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001848
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001849 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001850
Kristian Høgsbergef044142011-06-21 15:02:12 -04001851 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001852
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001853 weston_compositor_repick(ec);
1854 wl_event_loop_dispatch(ec->input_loop, 0);
1855
Jonas Ådahldb773762012-06-13 00:01:21 +02001856 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001857 wl_callback_send_done(cb->resource, msecs);
1858 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001859 }
1860
Scott Moreaud64cf212012-06-08 19:40:54 -06001861 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001862 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001863 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001864 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001865
1866 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001867}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001868
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001869static int
1870weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001871{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001872 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001873
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001874 wl_event_loop_dispatch(compositor->input_loop, 0);
1875
1876 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001877}
1878
1879WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001880weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001881{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001882 struct weston_compositor *compositor = output->compositor;
1883 struct wl_event_loop *loop =
1884 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001885 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001886
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001887 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001888
1889 if (output->repaint_needed &&
1890 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1891 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001892 r = weston_output_repaint(output, msecs);
1893 if (!r)
1894 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001895 }
1896
1897 output->repaint_scheduled = 0;
1898 if (compositor->input_loop_source)
1899 return;
1900
1901 fd = wl_event_loop_get_fd(compositor->input_loop);
1902 compositor->input_loop_source =
1903 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1904 weston_compositor_read_input, compositor);
1905}
1906
1907static void
1908idle_repaint(void *data)
1909{
1910 struct weston_output *output = data;
1911
Jonas Ådahle5a12252013-04-05 23:07:11 +02001912 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001913}
1914
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001915WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001916weston_layer_entry_insert(struct weston_layer_entry *list,
1917 struct weston_layer_entry *entry)
1918{
1919 wl_list_insert(&list->link, &entry->link);
1920 entry->layer = list->layer;
1921}
1922
1923WL_EXPORT void
1924weston_layer_entry_remove(struct weston_layer_entry *entry)
1925{
1926 wl_list_remove(&entry->link);
1927 wl_list_init(&entry->link);
1928 entry->layer = NULL;
1929}
1930
1931WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001932weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1933{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001934 wl_list_init(&layer->view_list.link);
1935 layer->view_list.layer = layer;
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001936 if (below != NULL)
1937 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001938}
1939
1940WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001941weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001942{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001943 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001944 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001945
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001946 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1947 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001948 return;
1949
Kristian Høgsbergef044142011-06-21 15:02:12 -04001950 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001951 output->repaint_needed = 1;
1952 if (output->repaint_scheduled)
1953 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001954
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001955 wl_event_loop_add_idle(loop, idle_repaint, output);
1956 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001957
1958 if (compositor->input_loop_source) {
1959 wl_event_source_remove(compositor->input_loop_source);
1960 compositor->input_loop_source = NULL;
1961 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001962}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001963
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001964WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001965weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1966{
1967 struct weston_output *output;
1968
1969 wl_list_for_each(output, &compositor->output_list, link)
1970 weston_output_schedule_repaint(output);
1971}
1972
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001973static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001974surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001975{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001976 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001977}
1978
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001979static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001980surface_attach(struct wl_client *client,
1981 struct wl_resource *resource,
1982 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1983{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001984 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001985 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001986
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001987 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001988 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001989 if (buffer == NULL) {
1990 wl_client_post_no_memory(client);
1991 return;
1992 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001993 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001994
Pekka Paalanende685b82012-12-04 15:58:12 +02001995 /* Attach, attach, without commit in between does not send
1996 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05001997 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001998
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001999 surface->pending.sx = sx;
2000 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002001 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002002}
2003
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002004static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002005surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002006 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002007 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002008{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002009 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002010
Pekka Paalanen8e159182012-10-10 12:49:25 +03002011 pixman_region32_union_rect(&surface->pending.damage,
2012 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002013 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002014}
2015
Kristian Høgsberg33418202011-08-16 23:01:28 -04002016static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002017destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002018{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002019 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002020
2021 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002022 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002023}
2024
2025static void
2026surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002027 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002028{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002029 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002030 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002031
2032 cb = malloc(sizeof *cb);
2033 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002034 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002035 return;
2036 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002037
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002038 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2039 callback);
2040 if (cb->resource == NULL) {
2041 free(cb);
2042 wl_resource_post_no_memory(resource);
2043 return;
2044 }
2045
Jason Ekstranda85118c2013-06-27 20:17:02 -05002046 wl_resource_set_implementation(cb->resource, NULL, cb,
2047 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002048
Pekka Paalanenbc106382012-10-10 12:49:31 +03002049 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002050}
2051
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002052static void
2053surface_set_opaque_region(struct wl_client *client,
2054 struct wl_resource *resource,
2055 struct wl_resource *region_resource)
2056{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002057 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002058 struct weston_region *region;
2059
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002060 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002061 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002062 pixman_region32_copy(&surface->pending.opaque,
2063 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002064 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002065 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002066 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002067}
2068
2069static void
2070surface_set_input_region(struct wl_client *client,
2071 struct wl_resource *resource,
2072 struct wl_resource *region_resource)
2073{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002074 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002075 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002076
2077 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002078 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002079 pixman_region32_copy(&surface->pending.input,
2080 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002081 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002082 pixman_region32_fini(&surface->pending.input);
2083 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002084 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002085}
2086
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002087static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002088weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002089{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002090 struct weston_subsurface *sub;
2091
2092 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2093 parent_link_pending) {
2094 wl_list_remove(&sub->parent_link);
2095 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2096 }
2097}
2098
2099static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002100weston_surface_commit_state(struct weston_surface *surface,
2101 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002103 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002104 pixman_region32_t opaque;
2105
Alexander Larsson4ea95522013-05-22 14:41:37 +02002106 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002107 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002108 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002109 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002110
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002111 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002112 if (state->newly_attached)
2113 weston_surface_attach(surface, state->buffer);
2114 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002115
Jason Ekstrand7b982072014-05-20 14:33:03 -05002116 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002117 weston_surface_update_size(surface);
2118 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002119 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002120 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002121
Jason Ekstrand7b982072014-05-20 14:33:03 -05002122 state->sx = 0;
2123 state->sy = 0;
2124 state->newly_attached = 0;
2125 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002126
2127 /* wl_surface.damage */
2128 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002129 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002130 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002131 0, 0, surface->width, surface->height);
2132 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002133
2134 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002135 pixman_region32_init(&opaque);
2136 pixman_region32_intersect_rect(&opaque, &state->opaque,
2137 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002138
2139 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2140 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002141 wl_list_for_each(view, &surface->views, surface_link)
2142 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002143 }
2144
2145 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002146
2147 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002148 pixman_region32_intersect_rect(&surface->input, &state->input,
2149 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002150
Pekka Paalanenbc106382012-10-10 12:49:31 +03002151 /* wl_surface.frame */
2152 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002153 &state->frame_callback_list);
2154 wl_list_init(&state->frame_callback_list);
2155}
2156
2157static void
2158weston_surface_commit(struct weston_surface *surface)
2159{
2160 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002161
Pekka Paalanene67858b2013-04-25 13:57:42 +03002162 weston_surface_commit_subsurface_order(surface);
2163
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002164 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002165}
2166
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002167static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002168weston_subsurface_commit(struct weston_subsurface *sub);
2169
2170static void
2171weston_subsurface_parent_commit(struct weston_subsurface *sub,
2172 int parent_is_synchronized);
2173
2174static void
2175surface_commit(struct wl_client *client, struct wl_resource *resource)
2176{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002177 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2179
2180 if (sub) {
2181 weston_subsurface_commit(sub);
2182 return;
2183 }
2184
2185 weston_surface_commit(surface);
2186
2187 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2188 if (sub->surface != surface)
2189 weston_subsurface_parent_commit(sub, 0);
2190 }
2191}
2192
2193static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002194surface_set_buffer_transform(struct wl_client *client,
2195 struct wl_resource *resource, int transform)
2196{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002197 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002198
Jonny Lamba55f1392014-05-30 12:07:15 +02002199 /* if wl_output.transform grows more members this will need to be updated. */
2200 if (transform < 0 ||
2201 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2202 wl_resource_post_error(resource,
2203 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2204 "buffer transform must be a valid transform "
2205 "('%d' specified)", transform);
2206 return;
2207 }
2208
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002209 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002210 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002211}
2212
Alexander Larsson4ea95522013-05-22 14:41:37 +02002213static void
2214surface_set_buffer_scale(struct wl_client *client,
2215 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002216 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002217{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002218 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002219
Jonny Lamba55f1392014-05-30 12:07:15 +02002220 if (scale < 1) {
2221 wl_resource_post_error(resource,
2222 WL_SURFACE_ERROR_INVALID_SCALE,
2223 "buffer scale must be at least one "
2224 "('%d' specified)", scale);
2225 return;
2226 }
2227
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002228 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002229 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002230}
2231
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002232static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002233 surface_destroy,
2234 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002235 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002236 surface_frame,
2237 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002238 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002239 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002240 surface_set_buffer_transform,
2241 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002242};
2243
2244static void
2245compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002246 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002247{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002248 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002249 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002250
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002251 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002252 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002253 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002254 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002255 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002256
Jason Ekstranda85118c2013-06-27 20:17:02 -05002257 surface->resource =
2258 wl_resource_create(client, &wl_surface_interface,
2259 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002260 if (surface->resource == NULL) {
2261 weston_surface_destroy(surface);
2262 wl_resource_post_no_memory(resource);
2263 return;
2264 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002265 wl_resource_set_implementation(surface->resource, &surface_interface,
2266 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002267
2268 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002269}
2270
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002271static void
2272destroy_region(struct wl_resource *resource)
2273{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002274 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002275
2276 pixman_region32_fini(&region->region);
2277 free(region);
2278}
2279
2280static void
2281region_destroy(struct wl_client *client, struct wl_resource *resource)
2282{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002283 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002284}
2285
2286static void
2287region_add(struct wl_client *client, struct wl_resource *resource,
2288 int32_t x, int32_t y, int32_t width, int32_t height)
2289{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002290 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002291
2292 pixman_region32_union_rect(&region->region, &region->region,
2293 x, y, width, height);
2294}
2295
2296static void
2297region_subtract(struct wl_client *client, struct wl_resource *resource,
2298 int32_t x, int32_t y, int32_t width, int32_t height)
2299{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002300 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002301 pixman_region32_t rect;
2302
2303 pixman_region32_init_rect(&rect, x, y, width, height);
2304 pixman_region32_subtract(&region->region, &region->region, &rect);
2305 pixman_region32_fini(&rect);
2306}
2307
2308static const struct wl_region_interface region_interface = {
2309 region_destroy,
2310 region_add,
2311 region_subtract
2312};
2313
2314static void
2315compositor_create_region(struct wl_client *client,
2316 struct wl_resource *resource, uint32_t id)
2317{
2318 struct weston_region *region;
2319
2320 region = malloc(sizeof *region);
2321 if (region == NULL) {
2322 wl_resource_post_no_memory(resource);
2323 return;
2324 }
2325
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002326 pixman_region32_init(&region->region);
2327
Jason Ekstranda85118c2013-06-27 20:17:02 -05002328 region->resource =
2329 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002330 if (region->resource == NULL) {
2331 free(region);
2332 wl_resource_post_no_memory(resource);
2333 return;
2334 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002335 wl_resource_set_implementation(region->resource, &region_interface,
2336 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002337}
2338
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002339static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002340 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002341 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002342};
2343
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002344static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002345weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2346{
2347 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002348
Jason Ekstrand7b982072014-05-20 14:33:03 -05002349 weston_surface_commit_state(surface, &sub->cached);
2350 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002351
2352 weston_surface_commit_subsurface_order(surface);
2353
2354 weston_surface_schedule_repaint(surface);
2355
Jason Ekstrand7b982072014-05-20 14:33:03 -05002356 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002357}
2358
2359static void
2360weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2361{
2362 struct weston_surface *surface = sub->surface;
2363
2364 /*
2365 * If this commit would cause the surface to move by the
2366 * attach(dx, dy) parameters, the old damage region must be
2367 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002368 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002369 */
2370 pixman_region32_translate(&sub->cached.damage,
2371 -surface->pending.sx, -surface->pending.sy);
2372 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2373 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002374 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002375
2376 if (surface->pending.newly_attached) {
2377 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002378 weston_surface_state_set_buffer(&sub->cached,
2379 surface->pending.buffer);
2380 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002381 surface->pending.buffer);
2382 }
2383 sub->cached.sx += surface->pending.sx;
2384 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002385
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002386 sub->cached.buffer_viewport.changed |=
2387 surface->pending.buffer_viewport.changed;
2388 sub->cached.buffer_viewport.buffer =
2389 surface->pending.buffer_viewport.buffer;
2390 sub->cached.buffer_viewport.surface =
2391 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002392
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002393 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002394
2395 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2396
2397 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2398
2399 wl_list_insert_list(&sub->cached.frame_callback_list,
2400 &surface->pending.frame_callback_list);
2401 wl_list_init(&surface->pending.frame_callback_list);
2402
Jason Ekstrand7b982072014-05-20 14:33:03 -05002403 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002404}
2405
2406static int
2407weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2408{
2409 while (sub) {
2410 if (sub->synchronized)
2411 return 1;
2412
2413 if (!sub->parent)
2414 return 0;
2415
2416 sub = weston_surface_to_subsurface(sub->parent);
2417 }
2418
2419 return 0;
2420}
2421
2422static void
2423weston_subsurface_commit(struct weston_subsurface *sub)
2424{
2425 struct weston_surface *surface = sub->surface;
2426 struct weston_subsurface *tmp;
2427
2428 /* Recursive check for effectively synchronized. */
2429 if (weston_subsurface_is_synchronized(sub)) {
2430 weston_subsurface_commit_to_cache(sub);
2431 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002432 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002433 /* flush accumulated state from cache */
2434 weston_subsurface_commit_to_cache(sub);
2435 weston_subsurface_commit_from_cache(sub);
2436 } else {
2437 weston_surface_commit(surface);
2438 }
2439
2440 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2441 if (tmp->surface != surface)
2442 weston_subsurface_parent_commit(tmp, 0);
2443 }
2444 }
2445}
2446
2447static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002448weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002449{
2450 struct weston_surface *surface = sub->surface;
2451 struct weston_subsurface *tmp;
2452
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453 /* From now on, commit_from_cache the whole sub-tree, regardless of
2454 * the synchronized mode of each child. This sub-surface or some
2455 * of its ancestors were synchronized, so we are synchronized
2456 * all the way down.
2457 */
2458
Jason Ekstrand7b982072014-05-20 14:33:03 -05002459 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002460 weston_subsurface_commit_from_cache(sub);
2461
2462 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2463 if (tmp->surface != surface)
2464 weston_subsurface_parent_commit(tmp, 1);
2465 }
2466}
2467
2468static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002469weston_subsurface_parent_commit(struct weston_subsurface *sub,
2470 int parent_is_synchronized)
2471{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002472 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002473 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002474 wl_list_for_each(view, &sub->surface->views, surface_link)
2475 weston_view_set_position(view,
2476 sub->position.x,
2477 sub->position.y);
2478
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002479 sub->position.set = 0;
2480 }
2481
2482 if (parent_is_synchronized || sub->synchronized)
2483 weston_subsurface_synchronized_commit(sub);
2484}
2485
2486static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002487subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002488{
2489 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002490 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002491
Jason Ekstranda7af7042013-10-12 22:38:11 -05002492 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002493 weston_view_set_position(view,
2494 view->geometry.x + dx,
2495 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002496
2497 /* No need to check parent mappedness, because if parent is not
2498 * mapped, parent is not in a visible layer, so this sub-surface
2499 * will not be drawn either.
2500 */
2501 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002502 /* Cannot call weston_surface_update_transform(),
2503 * because that would call it also for the parent surface,
2504 * which might not be mapped yet. That would lead to
2505 * inconsistent state, where the window could never be
2506 * mapped.
2507 *
2508 * Instead just assing any output, to make
2509 * weston_surface_is_mapped() return true, so that when the
2510 * parent surface does get mapped, this one will get
2511 * included, too. See surface_list_add().
2512 */
2513 assert(!wl_list_empty(&compositor->output_list));
2514 surface->output = container_of(compositor->output_list.next,
2515 struct weston_output, link);
2516 }
2517}
2518
2519static struct weston_subsurface *
2520weston_surface_to_subsurface(struct weston_surface *surface)
2521{
2522 if (surface->configure == subsurface_configure)
2523 return surface->configure_private;
2524
2525 return NULL;
2526}
2527
Pekka Paalanen01388e22013-04-25 13:57:44 +03002528WL_EXPORT struct weston_surface *
2529weston_surface_get_main_surface(struct weston_surface *surface)
2530{
2531 struct weston_subsurface *sub;
2532
2533 while (surface && (sub = weston_surface_to_subsurface(surface)))
2534 surface = sub->parent;
2535
2536 return surface;
2537}
2538
Pekka Paalanene67858b2013-04-25 13:57:42 +03002539static void
2540subsurface_set_position(struct wl_client *client,
2541 struct wl_resource *resource, int32_t x, int32_t y)
2542{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002543 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002544
2545 if (!sub)
2546 return;
2547
2548 sub->position.x = x;
2549 sub->position.y = y;
2550 sub->position.set = 1;
2551}
2552
2553static struct weston_subsurface *
2554subsurface_from_surface(struct weston_surface *surface)
2555{
2556 struct weston_subsurface *sub;
2557
2558 sub = weston_surface_to_subsurface(surface);
2559 if (sub)
2560 return sub;
2561
2562 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2563 if (sub->surface == surface)
2564 return sub;
2565
2566 return NULL;
2567}
2568
2569static struct weston_subsurface *
2570subsurface_sibling_check(struct weston_subsurface *sub,
2571 struct weston_surface *surface,
2572 const char *request)
2573{
2574 struct weston_subsurface *sibling;
2575
2576 sibling = subsurface_from_surface(surface);
2577
2578 if (!sibling) {
2579 wl_resource_post_error(sub->resource,
2580 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2581 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002582 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002583 return NULL;
2584 }
2585
2586 if (sibling->parent != sub->parent) {
2587 wl_resource_post_error(sub->resource,
2588 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2589 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002590 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002591 return NULL;
2592 }
2593
2594 return sibling;
2595}
2596
2597static void
2598subsurface_place_above(struct wl_client *client,
2599 struct wl_resource *resource,
2600 struct wl_resource *sibling_resource)
2601{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002602 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002603 struct weston_surface *surface =
2604 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002605 struct weston_subsurface *sibling;
2606
2607 if (!sub)
2608 return;
2609
2610 sibling = subsurface_sibling_check(sub, surface, "place_above");
2611 if (!sibling)
2612 return;
2613
2614 wl_list_remove(&sub->parent_link_pending);
2615 wl_list_insert(sibling->parent_link_pending.prev,
2616 &sub->parent_link_pending);
2617}
2618
2619static void
2620subsurface_place_below(struct wl_client *client,
2621 struct wl_resource *resource,
2622 struct wl_resource *sibling_resource)
2623{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002624 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002625 struct weston_surface *surface =
2626 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002627 struct weston_subsurface *sibling;
2628
2629 if (!sub)
2630 return;
2631
2632 sibling = subsurface_sibling_check(sub, surface, "place_below");
2633 if (!sibling)
2634 return;
2635
2636 wl_list_remove(&sub->parent_link_pending);
2637 wl_list_insert(&sibling->parent_link_pending,
2638 &sub->parent_link_pending);
2639}
2640
2641static void
2642subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2643{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002644 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002645
2646 if (sub)
2647 sub->synchronized = 1;
2648}
2649
2650static void
2651subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2652{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002653 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002654
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002655 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002657
2658 /* If sub became effectively desynchronized, flush. */
2659 if (!weston_subsurface_is_synchronized(sub))
2660 weston_subsurface_synchronized_commit(sub);
2661 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662}
2663
2664static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2666{
2667 wl_list_remove(&sub->parent_link);
2668 wl_list_remove(&sub->parent_link_pending);
2669 wl_list_remove(&sub->parent_destroy_listener.link);
2670 sub->parent = NULL;
2671}
2672
2673static void
2674weston_subsurface_destroy(struct weston_subsurface *sub);
2675
2676static void
2677subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2678{
2679 struct weston_subsurface *sub =
2680 container_of(listener, struct weston_subsurface,
2681 surface_destroy_listener);
2682 assert(data == &sub->surface->resource);
2683
2684 /* The protocol object (wl_resource) is left inert. */
2685 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002686 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002687
2688 weston_subsurface_destroy(sub);
2689}
2690
2691static void
2692subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2693{
2694 struct weston_subsurface *sub =
2695 container_of(listener, struct weston_subsurface,
2696 parent_destroy_listener);
2697 assert(data == &sub->parent->resource);
2698 assert(sub->surface != sub->parent);
2699
2700 if (weston_surface_is_mapped(sub->surface))
2701 weston_surface_unmap(sub->surface);
2702
2703 weston_subsurface_unlink_parent(sub);
2704}
2705
2706static void
2707subsurface_resource_destroy(struct wl_resource *resource)
2708{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002709 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002710
2711 if (sub)
2712 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002713}
2714
2715static void
2716subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2717{
2718 wl_resource_destroy(resource);
2719}
2720
2721static void
2722weston_subsurface_link_parent(struct weston_subsurface *sub,
2723 struct weston_surface *parent)
2724{
2725 sub->parent = parent;
2726 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002727 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002728 &sub->parent_destroy_listener);
2729
2730 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2731 wl_list_insert(&parent->subsurface_list_pending,
2732 &sub->parent_link_pending);
2733}
2734
2735static void
2736weston_subsurface_link_surface(struct weston_subsurface *sub,
2737 struct weston_surface *surface)
2738{
2739 sub->surface = surface;
2740 sub->surface_destroy_listener.notify =
2741 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002742 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002743 &sub->surface_destroy_listener);
2744}
2745
2746static void
2747weston_subsurface_destroy(struct weston_subsurface *sub)
2748{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002749 struct weston_view *view, *next;
2750
Pekka Paalanene67858b2013-04-25 13:57:42 +03002751 assert(sub->surface);
2752
2753 if (sub->resource) {
2754 assert(weston_surface_to_subsurface(sub->surface) == sub);
2755 assert(sub->parent_destroy_listener.notify ==
2756 subsurface_handle_parent_destroy);
2757
George Kiagiadakised04d382014-06-13 18:10:26 +02002758 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2759 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002760 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002761 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002762
Pekka Paalanene67858b2013-04-25 13:57:42 +03002763 if (sub->parent)
2764 weston_subsurface_unlink_parent(sub);
2765
Jason Ekstrand7b982072014-05-20 14:33:03 -05002766 weston_surface_state_fini(&sub->cached);
2767 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002768
2769 sub->surface->configure = NULL;
2770 sub->surface->configure_private = NULL;
2771 } else {
2772 /* the dummy weston_subsurface for the parent itself */
2773 assert(sub->parent_destroy_listener.notify == NULL);
2774 wl_list_remove(&sub->parent_link);
2775 wl_list_remove(&sub->parent_link_pending);
2776 }
2777
2778 wl_list_remove(&sub->surface_destroy_listener.link);
2779 free(sub);
2780}
2781
2782static const struct wl_subsurface_interface subsurface_implementation = {
2783 subsurface_destroy,
2784 subsurface_set_position,
2785 subsurface_place_above,
2786 subsurface_place_below,
2787 subsurface_set_sync,
2788 subsurface_set_desync
2789};
2790
2791static struct weston_subsurface *
2792weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2793 struct weston_surface *parent)
2794{
2795 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002796 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002797
2798 sub = calloc(1, sizeof *sub);
2799 if (!sub)
2800 return NULL;
2801
Jason Ekstranda7af7042013-10-12 22:38:11 -05002802 wl_list_init(&sub->unused_views);
2803
Jason Ekstranda85118c2013-06-27 20:17:02 -05002804 sub->resource =
2805 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002806 if (!sub->resource) {
2807 free(sub);
2808 return NULL;
2809 }
2810
Jason Ekstranda85118c2013-06-27 20:17:02 -05002811 wl_resource_set_implementation(sub->resource,
2812 &subsurface_implementation,
2813 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002814 weston_subsurface_link_surface(sub, surface);
2815 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002816 weston_surface_state_init(&sub->cached);
2817 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002818 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002819
2820 return sub;
2821}
2822
2823/* Create a dummy subsurface for having the parent itself in its
2824 * sub-surface lists. Makes stacking order manipulation easy.
2825 */
2826static struct weston_subsurface *
2827weston_subsurface_create_for_parent(struct weston_surface *parent)
2828{
2829 struct weston_subsurface *sub;
2830
2831 sub = calloc(1, sizeof *sub);
2832 if (!sub)
2833 return NULL;
2834
2835 weston_subsurface_link_surface(sub, parent);
2836 sub->parent = parent;
2837 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2838 wl_list_insert(&parent->subsurface_list_pending,
2839 &sub->parent_link_pending);
2840
2841 return sub;
2842}
2843
2844static void
2845subcompositor_get_subsurface(struct wl_client *client,
2846 struct wl_resource *resource,
2847 uint32_t id,
2848 struct wl_resource *surface_resource,
2849 struct wl_resource *parent_resource)
2850{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002851 struct weston_surface *surface =
2852 wl_resource_get_user_data(surface_resource);
2853 struct weston_surface *parent =
2854 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002855 struct weston_subsurface *sub;
2856 static const char where[] = "get_subsurface: wl_subsurface@";
2857
2858 if (surface == parent) {
2859 wl_resource_post_error(resource,
2860 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2861 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002862 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002863 return;
2864 }
2865
2866 if (weston_surface_to_subsurface(surface)) {
2867 wl_resource_post_error(resource,
2868 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2869 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002870 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002871 return;
2872 }
2873
2874 if (surface->configure) {
2875 wl_resource_post_error(resource,
2876 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2877 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002878 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002879 return;
2880 }
2881
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002882 if (weston_surface_get_main_surface(parent) == surface) {
2883 wl_resource_post_error(resource,
2884 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2885 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002886 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002887 return;
2888 }
2889
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890 /* make sure the parent is in its own list */
2891 if (wl_list_empty(&parent->subsurface_list)) {
2892 if (!weston_subsurface_create_for_parent(parent)) {
2893 wl_resource_post_no_memory(resource);
2894 return;
2895 }
2896 }
2897
2898 sub = weston_subsurface_create(id, surface, parent);
2899 if (!sub) {
2900 wl_resource_post_no_memory(resource);
2901 return;
2902 }
2903
2904 surface->configure = subsurface_configure;
2905 surface->configure_private = sub;
2906}
2907
2908static void
2909subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2910{
2911 wl_resource_destroy(resource);
2912}
2913
2914static const struct wl_subcompositor_interface subcompositor_interface = {
2915 subcompositor_destroy,
2916 subcompositor_get_subsurface
2917};
2918
2919static void
2920bind_subcompositor(struct wl_client *client,
2921 void *data, uint32_t version, uint32_t id)
2922{
2923 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002924 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002925
Jason Ekstranda85118c2013-06-27 20:17:02 -05002926 resource =
2927 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002928 if (resource == NULL) {
2929 wl_client_post_no_memory(client);
2930 return;
2931 }
2932 wl_resource_set_implementation(resource, &subcompositor_interface,
2933 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002934}
2935
2936static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002937weston_compositor_dpms(struct weston_compositor *compositor,
2938 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002939{
2940 struct weston_output *output;
2941
2942 wl_list_for_each(output, &compositor->output_list, link)
2943 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002944 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002945}
2946
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002947WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002948weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002949{
Neil Roberts8b62e202013-09-30 13:14:47 +01002950 uint32_t old_state = compositor->state;
2951
2952 /* The state needs to be changed before emitting the wake
2953 * signal because that may try to schedule a repaint which
2954 * will not work if the compositor is still sleeping */
2955 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2956
2957 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002958 case WESTON_COMPOSITOR_SLEEPING:
2959 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2960 /* fall through */
2961 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002962 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002963 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002964 /* fall through */
2965 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002966 wl_event_source_timer_update(compositor->idle_source,
2967 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002968 }
2969}
2970
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002971WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002972weston_compositor_offscreen(struct weston_compositor *compositor)
2973{
2974 switch (compositor->state) {
2975 case WESTON_COMPOSITOR_OFFSCREEN:
2976 return;
2977 case WESTON_COMPOSITOR_SLEEPING:
2978 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2979 /* fall through */
2980 default:
2981 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2982 wl_event_source_timer_update(compositor->idle_source, 0);
2983 }
2984}
2985
2986WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002987weston_compositor_sleep(struct weston_compositor *compositor)
2988{
2989 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2990 return;
2991
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002992 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002993 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2994 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2995}
2996
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002997static int
2998idle_handler(void *data)
2999{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003000 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003001
3002 if (compositor->idle_inhibit)
3003 return 1;
3004
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003005 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003006 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003007
3008 return 1;
3009}
3010
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003011WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003012weston_plane_init(struct weston_plane *plane,
3013 struct weston_compositor *ec,
3014 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003015{
3016 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003017 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003018 plane->x = x;
3019 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003020 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003021
3022 /* Init the link so that the call to wl_list_remove() when releasing
3023 * the plane without ever stacking doesn't lead to a crash */
3024 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003025}
3026
3027WL_EXPORT void
3028weston_plane_release(struct weston_plane *plane)
3029{
Xiong Zhang97116532013-10-23 13:58:31 +08003030 struct weston_view *view;
3031
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003032 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003033 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003034
Xiong Zhang97116532013-10-23 13:58:31 +08003035 wl_list_for_each(view, &plane->compositor->view_list, link) {
3036 if (view->plane == plane)
3037 view->plane = NULL;
3038 }
3039
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003040 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003041}
3042
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003043WL_EXPORT void
3044weston_compositor_stack_plane(struct weston_compositor *ec,
3045 struct weston_plane *plane,
3046 struct weston_plane *above)
3047{
3048 if (above)
3049 wl_list_insert(above->link.prev, &plane->link);
3050 else
3051 wl_list_insert(&ec->plane_list, &plane->link);
3052}
3053
Casey Dahlin9074db52012-04-19 22:50:09 -04003054static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003055{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003056 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003057}
3058
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003059static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003060bind_output(struct wl_client *client,
3061 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003062{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003063 struct weston_output *output = data;
3064 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003065 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003066
Jason Ekstranda85118c2013-06-27 20:17:02 -05003067 resource = wl_resource_create(client, &wl_output_interface,
3068 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003069 if (resource == NULL) {
3070 wl_client_post_no_memory(client);
3071 return;
3072 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003073
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003074 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003075 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003076
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003077 wl_output_send_geometry(resource,
3078 output->x,
3079 output->y,
3080 output->mm_width,
3081 output->mm_height,
3082 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003083 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003084 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003085 if (version >= 2)
3086 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003087 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003088
3089 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003090 wl_output_send_mode(resource,
3091 mode->flags,
3092 mode->width,
3093 mode->height,
3094 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003095 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003096
3097 if (version >= 2)
3098 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003099}
3100
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003101/* Move other outputs when one is removed so the space remains contiguos. */
3102static void
3103weston_compositor_remove_output(struct weston_compositor *compositor,
3104 struct weston_output *remove_output)
3105{
3106 struct weston_output *output;
3107 int offset = 0;
3108
3109 wl_list_for_each(output, &compositor->output_list, link) {
3110 if (output == remove_output) {
3111 offset = output->width;
3112 continue;
3113 }
3114
3115 if (offset > 0) {
3116 weston_output_move(output,
3117 output->x - offset, output->y);
3118 output->dirty = 1;
3119 }
3120 }
3121}
3122
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003123WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003124weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003125{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003126 output->destroying = 1;
3127
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003128 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003129 wl_list_remove(&output->link);
3130
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003131 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003132 wl_signal_emit(&output->destroy_signal, output);
3133
Richard Hughesafe690c2013-05-02 10:10:04 +01003134 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003135 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003136 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003137 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003138
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003139 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003140}
3141
Scott Moreau1bad5db2012-08-18 01:04:05 -06003142static void
3143weston_output_compute_transform(struct weston_output *output)
3144{
3145 struct weston_matrix transform;
3146 int flip;
3147
3148 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003149 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003150
3151 switch(output->transform) {
3152 case WL_OUTPUT_TRANSFORM_FLIPPED:
3153 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3154 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3155 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003156 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003157 flip = -1;
3158 break;
3159 default:
3160 flip = 1;
3161 break;
3162 }
3163
3164 switch(output->transform) {
3165 case WL_OUTPUT_TRANSFORM_NORMAL:
3166 case WL_OUTPUT_TRANSFORM_FLIPPED:
3167 transform.d[0] = flip;
3168 transform.d[1] = 0;
3169 transform.d[4] = 0;
3170 transform.d[5] = 1;
3171 break;
3172 case WL_OUTPUT_TRANSFORM_90:
3173 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3174 transform.d[0] = 0;
3175 transform.d[1] = -flip;
3176 transform.d[4] = 1;
3177 transform.d[5] = 0;
3178 break;
3179 case WL_OUTPUT_TRANSFORM_180:
3180 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3181 transform.d[0] = -flip;
3182 transform.d[1] = 0;
3183 transform.d[4] = 0;
3184 transform.d[5] = -1;
3185 break;
3186 case WL_OUTPUT_TRANSFORM_270:
3187 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3188 transform.d[0] = 0;
3189 transform.d[1] = flip;
3190 transform.d[4] = -1;
3191 transform.d[5] = 0;
3192 break;
3193 default:
3194 break;
3195 }
3196
3197 weston_matrix_multiply(&output->matrix, &transform);
3198}
3199
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003200WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003201weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003202{
Scott Moreau850ca422012-05-21 15:21:25 -06003203 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003204
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003205 weston_matrix_init(&output->matrix);
3206 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003207 -(output->x + output->width / 2.0),
3208 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003209
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003210 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003211 2.0 / output->width,
3212 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003213
Scott Moreauccbf29d2012-02-22 14:21:41 -07003214 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003215 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003216 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003217 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3218 output->zoom.trans_y, 0);
3219 weston_matrix_scale(&output->matrix, magnification,
3220 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003221 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003222
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003223 weston_output_compute_transform(output);
3224
Scott Moreauccbf29d2012-02-22 14:21:41 -07003225 output->dirty = 0;
3226}
3227
Scott Moreau1bad5db2012-08-18 01:04:05 -06003228static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003229weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003230{
3231 output->transform = transform;
3232
3233 switch (transform) {
3234 case WL_OUTPUT_TRANSFORM_90:
3235 case WL_OUTPUT_TRANSFORM_270:
3236 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3237 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3238 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003239 output->width = output->current_mode->height;
3240 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003241 break;
3242 case WL_OUTPUT_TRANSFORM_NORMAL:
3243 case WL_OUTPUT_TRANSFORM_180:
3244 case WL_OUTPUT_TRANSFORM_FLIPPED:
3245 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003246 output->width = output->current_mode->width;
3247 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003248 break;
3249 default:
3250 break;
3251 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003252
Hardening57388e42013-09-18 23:56:36 +02003253 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003254 output->width /= scale;
3255 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003256}
3257
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003258static void
3259weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003260{
3261 output->x = x;
3262 output->y = y;
3263
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003264 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003265 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003266 output->width,
3267 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003268}
3269
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003270WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003271weston_output_move(struct weston_output *output, int x, int y)
3272{
3273 pixman_region32_t old_region;
3274 struct wl_resource *resource;
3275
3276 output->move_x = x - output->x;
3277 output->move_y = y - output->y;
3278
3279 if (output->move_x == 0 && output->move_y == 0)
3280 return;
3281
3282 pixman_region32_init(&old_region);
3283 pixman_region32_copy(&old_region, &output->region);
3284
3285 weston_output_init_geometry(output, x, y);
3286
3287 output->dirty = 1;
3288
3289 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003290 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003291
3292 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003293 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003294 wl_output_send_geometry(resource,
3295 output->x,
3296 output->y,
3297 output->mm_width,
3298 output->mm_height,
3299 output->subpixel,
3300 output->make,
3301 output->model,
3302 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003303
3304 if (wl_resource_get_version(resource) >= 2)
3305 wl_output_send_done(resource);
3306 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003307}
3308
3309WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003310weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003311 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003312 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003313{
3314 output->compositor = c;
3315 output->x = x;
3316 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003317 output->mm_width = mm_width;
3318 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003319 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003320 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003321
Alexander Larsson0b135062013-05-28 16:23:36 +02003322 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003323 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003324
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003325 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003326 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003327
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003328 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003329 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003330 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003331 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003332
Casey Dahlin58ba1372012-04-19 22:50:08 -04003333 output->id = ffs(~output->compositor->output_id_pool) - 1;
3334 output->compositor->output_id_pool |= 1 << output->id;
3335
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003336 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003337 wl_global_create(c->wl_display, &wl_output_interface, 2,
3338 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003339 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003340}
3341
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003342WL_EXPORT void
3343weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003344 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003345 wl_fixed_t *x, wl_fixed_t *y)
3346{
3347 wl_fixed_t tx, ty;
3348 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003349 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003350
Hardeningff39efa2013-09-18 23:56:35 +02003351 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3352 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003353
3354 switch(output->transform) {
3355 case WL_OUTPUT_TRANSFORM_NORMAL:
3356 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003357 tx = device_x;
3358 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003359 break;
3360 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003361 tx = device_y;
3362 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003363 break;
3364 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003365 tx = width - device_x;
3366 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003367 break;
3368 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003369 tx = width - device_y;
3370 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003371 break;
3372 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003373 tx = width - device_x;
3374 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003375 break;
3376 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003377 tx = width - device_y;
3378 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003379 break;
3380 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003381 tx = device_x;
3382 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003383 break;
3384 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003385 tx = device_y;
3386 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003387 break;
3388 }
3389
Neil Robertseb5a2002014-05-01 16:13:55 +01003390 tx /= output->current_scale;
3391 ty /= output->current_scale;
3392
3393 if (output->zoom.active) {
3394 zoom_scale = output->zoom.spring_z.current;
3395 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3396 output->width / 2.0f *
3397 (zoom_scale + output->zoom.trans_x));
3398 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3399 output->height / 2.0f *
3400 (zoom_scale + output->zoom.trans_y));
3401 tx = wl_fixed_from_double(zx);
3402 ty = wl_fixed_from_double(zy);
3403 }
3404
3405 *x = tx + wl_fixed_from_int(output->x);
3406 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003407}
3408
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003409static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003410destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003411{
Jonny Lamb74130762013-11-26 18:19:46 +01003412 struct weston_surface *surface =
3413 wl_resource_get_user_data(resource);
3414
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003415 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003416 surface->pending.buffer_viewport.buffer.src_width =
3417 wl_fixed_from_int(-1);
3418 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003419 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003420}
3421
3422static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003423viewport_destroy(struct wl_client *client,
3424 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003425{
3426 wl_resource_destroy(resource);
3427}
3428
3429static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003430viewport_set(struct wl_client *client,
3431 struct wl_resource *resource,
3432 wl_fixed_t src_x,
3433 wl_fixed_t src_y,
3434 wl_fixed_t src_width,
3435 wl_fixed_t src_height,
3436 int32_t dst_width,
3437 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003438{
Jonny Lamb74130762013-11-26 18:19:46 +01003439 struct weston_surface *surface =
3440 wl_resource_get_user_data(resource);
3441
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003442 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003443
Jonny Lamb8ae35902013-11-26 18:19:45 +01003444 if (wl_fixed_to_double(src_width) < 0 ||
3445 wl_fixed_to_double(src_height) < 0) {
3446 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003447 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003448 "source dimensions must be non-negative (%fx%f)",
3449 wl_fixed_to_double(src_width),
3450 wl_fixed_to_double(src_height));
3451 return;
3452 }
3453
3454 if (dst_width <= 0 || dst_height <= 0) {
3455 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003456 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003457 "destination dimensions must be positive (%dx%d)",
3458 dst_width, dst_height);
3459 return;
3460 }
3461
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003462 surface->pending.buffer_viewport.buffer.src_x = src_x;
3463 surface->pending.buffer_viewport.buffer.src_y = src_y;
3464 surface->pending.buffer_viewport.buffer.src_width = src_width;
3465 surface->pending.buffer_viewport.buffer.src_height = src_height;
3466 surface->pending.buffer_viewport.surface.width = dst_width;
3467 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003468 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003469}
3470
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003471static void
3472viewport_set_source(struct wl_client *client,
3473 struct wl_resource *resource,
3474 wl_fixed_t src_x,
3475 wl_fixed_t src_y,
3476 wl_fixed_t src_width,
3477 wl_fixed_t src_height)
3478{
3479 struct weston_surface *surface =
3480 wl_resource_get_user_data(resource);
3481
3482 assert(surface->viewport_resource != NULL);
3483
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003484 if (src_width == wl_fixed_from_int(-1) &&
3485 src_height == wl_fixed_from_int(-1)) {
3486 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003487 surface->pending.buffer_viewport.buffer.src_width =
3488 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003489 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003490 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003491 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003492
3493 if (src_width <= 0 || src_height <= 0) {
3494 wl_resource_post_error(resource,
3495 WL_VIEWPORT_ERROR_BAD_VALUE,
3496 "source size must be positive (%fx%f)",
3497 wl_fixed_to_double(src_width),
3498 wl_fixed_to_double(src_height));
3499 return;
3500 }
3501
3502 surface->pending.buffer_viewport.buffer.src_x = src_x;
3503 surface->pending.buffer_viewport.buffer.src_y = src_y;
3504 surface->pending.buffer_viewport.buffer.src_width = src_width;
3505 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003506 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003507}
3508
3509static void
3510viewport_set_destination(struct wl_client *client,
3511 struct wl_resource *resource,
3512 int32_t dst_width,
3513 int32_t dst_height)
3514{
3515 struct weston_surface *surface =
3516 wl_resource_get_user_data(resource);
3517
3518 assert(surface->viewport_resource != NULL);
3519
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003520 if (dst_width == -1 && dst_height == -1) {
3521 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003522 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003523 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003524 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003525 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003526
3527 if (dst_width <= 0 || dst_height <= 0) {
3528 wl_resource_post_error(resource,
3529 WL_VIEWPORT_ERROR_BAD_VALUE,
3530 "destination size must be positive (%dx%d)",
3531 dst_width, dst_height);
3532 return;
3533 }
3534
3535 surface->pending.buffer_viewport.surface.width = dst_width;
3536 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003537 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003538}
3539
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003540static const struct wl_viewport_interface viewport_interface = {
3541 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003542 viewport_set,
3543 viewport_set_source,
3544 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003545};
3546
3547static void
3548scaler_destroy(struct wl_client *client,
3549 struct wl_resource *resource)
3550{
3551 wl_resource_destroy(resource);
3552}
3553
3554static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003555scaler_get_viewport(struct wl_client *client,
3556 struct wl_resource *scaler,
3557 uint32_t id,
3558 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003559{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003560 int version = wl_resource_get_version(scaler);
3561 struct weston_surface *surface =
3562 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003563 struct wl_resource *resource;
3564
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003565 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003566 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003567 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3568 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003569 return;
3570 }
3571
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003572 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003573 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003574 if (resource == NULL) {
3575 wl_client_post_no_memory(client);
3576 return;
3577 }
3578
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003579 wl_resource_set_implementation(resource, &viewport_interface,
3580 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003581
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003582 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003583}
3584
3585static const struct wl_scaler_interface scaler_interface = {
3586 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003587 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003588};
3589
3590static void
3591bind_scaler(struct wl_client *client,
3592 void *data, uint32_t version, uint32_t id)
3593{
3594 struct wl_resource *resource;
3595
3596 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003597 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003598 if (resource == NULL) {
3599 wl_client_post_no_memory(client);
3600 return;
3601 }
3602
3603 wl_resource_set_implementation(resource, &scaler_interface,
3604 NULL, NULL);
3605}
3606
3607static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003608compositor_bind(struct wl_client *client,
3609 void *data, uint32_t version, uint32_t id)
3610{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003611 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003612 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003613
Jason Ekstranda85118c2013-06-27 20:17:02 -05003614 resource = wl_resource_create(client, &wl_compositor_interface,
3615 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003616 if (resource == NULL) {
3617 wl_client_post_no_memory(client);
3618 return;
3619 }
3620
3621 wl_resource_set_implementation(resource, &compositor_interface,
3622 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003623}
3624
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003625static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003626log_uname(void)
3627{
3628 struct utsname usys;
3629
3630 uname(&usys);
3631
3632 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3633 usys.version, usys.machine);
3634}
3635
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003636WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003637weston_environment_get_fd(const char *env)
3638{
3639 char *e, *end;
3640 int fd, flags;
3641
3642 e = getenv(env);
3643 if (!e)
3644 return -1;
3645 fd = strtol(e, &end, 0);
3646 if (*end != '\0')
3647 return -1;
3648
3649 flags = fcntl(fd, F_GETFD);
3650 if (flags == -1)
3651 return -1;
3652
3653 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3654 unsetenv(env);
3655
3656 return fd;
3657}
3658
3659WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003660weston_compositor_init(struct weston_compositor *ec,
3661 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003662 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003663 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003664{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003665 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003666 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003667 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003668
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003669 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003670 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003671 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003672 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003673 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003674 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003675 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003676 wl_signal_init(&ec->idle_signal);
3677 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003678 wl_signal_init(&ec->show_input_panel_signal);
3679 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003680 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003681 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003682 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003683 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003684 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003685 wl_signal_init(&ec->session_signal);
3686 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003687
Casey Dahlin58ba1372012-04-19 22:50:08 -04003688 ec->output_id_pool = 0;
3689
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003690 if (!wl_global_create(display, &wl_compositor_interface, 3,
3691 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003692 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003693
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003694 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3695 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003696 return -1;
3697
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003698 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003699 ec, bind_scaler))
3700 return -1;
3701
Jason Ekstranda7af7042013-10-12 22:38:11 -05003702 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003703 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003704 wl_list_init(&ec->layer_list);
3705 wl_list_init(&ec->seat_list);
3706 wl_list_init(&ec->output_list);
3707 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003708 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003709 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003710 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003711 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003712 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003713
Xiong Zhang97116532013-10-23 13:58:31 +08003714 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003715 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003716
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003717 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3718 weston_config_section_get_string(s, "keymap_rules",
3719 (char **) &xkb_names.rules, NULL);
3720 weston_config_section_get_string(s, "keymap_model",
3721 (char **) &xkb_names.model, NULL);
3722 weston_config_section_get_string(s, "keymap_layout",
3723 (char **) &xkb_names.layout, NULL);
3724 weston_config_section_get_string(s, "keymap_variant",
3725 (char **) &xkb_names.variant, NULL);
3726 weston_config_section_get_string(s, "keymap_options",
3727 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003728
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003729 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3730 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003731
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003732 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003733
3734 wl_data_device_manager_init(ec->wl_display);
3735
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003736 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003737
Daniel Stone725c2c32012-06-22 14:04:36 +01003738 loop = wl_display_get_event_loop(ec->wl_display);
3739 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3740 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3741
3742 ec->input_loop = wl_event_loop_create();
3743
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003744 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3745 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3746
3747 weston_compositor_schedule_repaint(ec);
3748
Daniel Stone725c2c32012-06-22 14:04:36 +01003749 return 0;
3750}
3751
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003752WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003753weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003754{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003755 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003756
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003757 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003758 if (ec->input_loop_source)
3759 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003760
Matt Roper361d2ad2011-08-29 13:52:23 -07003761 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003762 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003763 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003764
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003765 if (ec->renderer)
3766 ec->renderer->destroy(ec);
3767
Daniel Stone325fc2d2012-05-30 16:31:58 +01003768 weston_binding_list_destroy_all(&ec->key_binding_list);
3769 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003770 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003771 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003772 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003773
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003774 weston_plane_release(&ec->primary_plane);
3775
Jonas Ådahlc97af922012-03-28 22:36:09 +02003776 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003777
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003778 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003779}
3780
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003781WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003782weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3783 const struct weston_pointer_grab_interface *interface)
3784{
3785 struct weston_seat *seat;
3786
3787 ec->default_pointer_grab = interface;
3788 wl_list_for_each(seat, &ec->seat_list, link) {
3789 if (seat->pointer) {
3790 weston_pointer_set_default_grab(seat->pointer,
3791 interface);
3792 }
3793 }
3794}
3795
3796WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003797weston_version(int *major, int *minor, int *micro)
3798{
3799 *major = WESTON_VERSION_MAJOR;
3800 *minor = WESTON_VERSION_MINOR;
3801 *micro = WESTON_VERSION_MICRO;
3802}
3803
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003804static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003805 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003806 const char *desc;
3807} capability_strings[] = {
3808 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003809 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003810};
3811
3812static void
3813weston_compositor_log_capabilities(struct weston_compositor *compositor)
3814{
3815 unsigned i;
3816 int yes;
3817
3818 weston_log("Compositor capabilities:\n");
3819 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3820 yes = compositor->capabilities & capability_strings[i].bit;
3821 weston_log_continue(STAMP_SPACE "%s %s\n",
3822 capability_strings[i].desc,
3823 yes ? "yes" : "no");
3824 }
3825}
3826
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003827static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003828{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003829 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003830
Martin Minarik6d118362012-06-07 18:01:59 +02003831 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003832 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003833
3834 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003835}
3836
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003837#ifdef HAVE_LIBUNWIND
3838
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003839static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003840print_backtrace(void)
3841{
3842 unw_cursor_t cursor;
3843 unw_context_t context;
3844 unw_word_t off;
3845 unw_proc_info_t pip;
3846 int ret, i = 0;
3847 char procname[256];
3848 const char *filename;
3849 Dl_info dlinfo;
3850
3851 pip.unwind_info = NULL;
3852 ret = unw_getcontext(&context);
3853 if (ret) {
3854 weston_log("unw_getcontext: %d\n", ret);
3855 return;
3856 }
3857
3858 ret = unw_init_local(&cursor, &context);
3859 if (ret) {
3860 weston_log("unw_init_local: %d\n", ret);
3861 return;
3862 }
3863
3864 ret = unw_step(&cursor);
3865 while (ret > 0) {
3866 ret = unw_get_proc_info(&cursor, &pip);
3867 if (ret) {
3868 weston_log("unw_get_proc_info: %d\n", ret);
3869 break;
3870 }
3871
3872 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3873 if (ret && ret != -UNW_ENOMEM) {
3874 if (ret != -UNW_EUNSPEC)
3875 weston_log("unw_get_proc_name: %d\n", ret);
3876 procname[0] = '?';
3877 procname[1] = 0;
3878 }
3879
3880 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3881 *dlinfo.dli_fname)
3882 filename = dlinfo.dli_fname;
3883 else
3884 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003885
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003886 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3887 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3888
3889 ret = unw_step(&cursor);
3890 if (ret < 0)
3891 weston_log("unw_step: %d\n", ret);
3892 }
3893}
3894
3895#else
3896
3897static void
3898print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003899{
3900 void *buffer[32];
3901 int i, count;
3902 Dl_info info;
3903
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003904 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3905 for (i = 0; i < count; i++) {
3906 dladdr(buffer[i], &info);
3907 weston_log(" [%016lx] %s (%s)\n",
3908 (long) buffer[i],
3909 info.dli_sname ? info.dli_sname : "--",
3910 info.dli_fname);
3911 }
3912}
3913
3914#endif
3915
3916static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003917on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003918{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003919 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003920 * then call the backend restore function, which will switch
3921 * back to the vt we launched from or ungrab X etc and then
3922 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003923 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003924 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003925 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003926
Peter Maatmane5b42e42013-03-27 22:38:53 +01003927 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003928
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003929 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003930
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003931 segv_compositor->restore(segv_compositor);
3932
3933 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003934}
3935
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003936WL_EXPORT void *
3937weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003938{
3939 char path[PATH_MAX];
3940 void *module, *init;
3941
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003942 if (name == NULL)
3943 return NULL;
3944
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003945 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003946 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003947 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003948 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003949
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003950 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3951 if (module) {
3952 weston_log("Module '%s' already loaded\n", path);
3953 dlclose(module);
3954 return NULL;
3955 }
3956
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003957 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003958 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003959 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003960 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003961 return NULL;
3962 }
3963
3964 init = dlsym(module, entrypoint);
3965 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003966 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003967 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003968 return NULL;
3969 }
3970
3971 return init;
3972}
3973
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003974static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003975load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003976 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003977{
3978 const char *p, *end;
3979 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003980 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003981 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003982
3983 if (modules == NULL)
3984 return 0;
3985
3986 p = modules;
3987 while (*p) {
3988 end = strchrnul(p, ',');
3989 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003990 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003991 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003992 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003993 p = end;
3994 while (*p == ',')
3995 p++;
3996
3997 }
3998
3999 return 0;
4000}
4001
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004002static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004003 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4004
4005static const char xdg_wrong_message[] =
4006 "fatal: environment variable XDG_RUNTIME_DIR\n"
4007 "is set to \"%s\", which is not a directory.\n";
4008
4009static const char xdg_wrong_mode_message[] =
4010 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004011 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4012 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004013
4014static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004015 "Refer to your distribution on how to get it, or\n"
4016 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4017 "on how to implement it.\n";
4018
Martin Minarik37032f82012-06-18 20:15:18 +02004019static void
4020verify_xdg_runtime_dir(void)
4021{
4022 char *dir = getenv("XDG_RUNTIME_DIR");
4023 struct stat s;
4024
4025 if (!dir) {
4026 weston_log(xdg_error_message);
4027 weston_log_continue(xdg_detail_message);
4028 exit(EXIT_FAILURE);
4029 }
4030
4031 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4032 weston_log(xdg_wrong_message, dir);
4033 weston_log_continue(xdg_detail_message);
4034 exit(EXIT_FAILURE);
4035 }
4036
4037 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4038 weston_log(xdg_wrong_mode_message,
4039 dir, s.st_mode & 0777, s.st_uid);
4040 weston_log_continue(xdg_detail_message);
4041 }
4042}
4043
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004044static int
4045usage(int error_code)
4046{
4047 fprintf(stderr,
4048 "Usage: weston [OPTIONS]\n\n"
4049 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4050 "Weston supports multiple backends, and depending on which backend is in use\n"
4051 "different options will be accepted.\n\n"
4052
4053
4054 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004055 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004056 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004057 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4058 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004059 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004060 " -S, --socket=NAME\tName of socket to listen on\n"
4061 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004062 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004063 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004064 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004065 " -h, --help\t\tThis help message\n\n");
4066
4067 fprintf(stderr,
4068 "Options for drm-backend.so:\n\n"
4069 " --connector=ID\tBring up only this connector\n"
4070 " --seat=SEAT\t\tThe seat that weston should run on\n"
4071 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004072 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004073 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4074
4075 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004076 "Options for fbdev-backend.so:\n\n"
4077 " --tty=TTY\t\tThe tty to use\n"
4078 " --device=DEVICE\tThe framebuffer device to use\n\n");
4079
4080 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004081 "Options for x11-backend.so:\n\n"
4082 " --width=WIDTH\t\tWidth of X window\n"
4083 " --height=HEIGHT\tHeight of X window\n"
4084 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004085 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004086 " --output-count=COUNT\tCreate multiple outputs\n"
4087 " --no-input\t\tDont create input devices\n\n");
4088
4089 fprintf(stderr,
4090 "Options for wayland-backend.so:\n\n"
4091 " --width=WIDTH\t\tWidth of Wayland surface\n"
4092 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004093 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004094 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004095 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004096 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004097 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004098 " --display=DISPLAY\tWayland display to connect to\n\n");
4099
Pekka Paalanene31e0532013-05-22 18:03:07 +03004100#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4101 fprintf(stderr,
4102 "Options for rpi-backend.so:\n\n"
4103 " --tty=TTY\t\tThe tty to use\n"
4104 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4105 " --transform=TR\tThe output transformation, TR is one of:\n"
4106 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004107 " --opaque-regions\tEnable support for opaque regions, can be "
4108 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004109 "\n");
4110#endif
4111
Hardeningc077a842013-07-08 00:51:35 +02004112#if defined(BUILD_RDP_COMPOSITOR)
4113 fprintf(stderr,
4114 "Options for rdp-backend.so:\n\n"
4115 " --width=WIDTH\t\tWidth of desktop\n"
4116 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004117 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4118 " --address=ADDR\tThe address to bind\n"
4119 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004120 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004121 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4122 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4123 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4124 "\n");
4125#endif
4126
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004127 exit(error_code);
4128}
4129
Peter Maatmane5b42e42013-03-27 22:38:53 +01004130static void
4131catch_signals(void)
4132{
4133 struct sigaction action;
4134
4135 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4136 action.sa_sigaction = on_caught_signal;
4137 sigemptyset(&action.sa_mask);
4138 sigaction(SIGSEGV, &action, NULL);
4139 sigaction(SIGABRT, &action, NULL);
4140}
4141
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004142static void
4143handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4144{
4145 struct wl_client *client = data;
4146
4147 weston_log("Primary client died. Closing...\n");
4148
4149 wl_display_terminate(wl_client_get_display(client));
4150}
4151
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004152int main(int argc, char *argv[])
4153{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004154 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004155 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004156 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004157 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004158 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004159 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004160 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004161 int *argc, char *argv[],
4162 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004163 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004164 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004165 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004166 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004167 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004168 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004169 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004170 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004171 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004172 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004173 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004174 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004175 int32_t noconfig = 0;
4176 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004177 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004178 struct wl_client *primary_client;
4179 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004180
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004181 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004182 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004183 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004184 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4185 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004186 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004187 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004188 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004189 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004190 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004191 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004192
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004193 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004194
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004195 if (help)
4196 usage(EXIT_SUCCESS);
4197
Scott Moreau12245142012-08-29 15:15:58 -06004198 if (version) {
4199 printf(PACKAGE_STRING "\n");
4200 return EXIT_SUCCESS;
4201 }
4202
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004203 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004204
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004205 weston_log("%s\n"
4206 STAMP_SPACE "%s\n"
4207 STAMP_SPACE "Bug reports to: %s\n"
4208 STAMP_SPACE "Build: %s\n",
4209 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004210 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004211 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004212
Martin Minarik37032f82012-06-18 20:15:18 +02004213 verify_xdg_runtime_dir();
4214
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004215 display = wl_display_create();
4216
Tiago Vignatti2116b892011-08-08 05:52:59 -07004217 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004218 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4219 display);
4220 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4221 display);
4222 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4223 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004224
4225 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004226 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4227 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004228
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304229 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4230 ret = EXIT_FAILURE;
4231 goto out_signals;
4232 }
4233
Pekka Paalanen588bee12014-05-07 16:26:25 +03004234 if (noconfig == 0)
4235 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004236 if (config != NULL) {
4237 weston_log("Using config file '%s'\n",
4238 weston_config_get_full_path(config));
4239 } else {
4240 weston_log("Starting with no config file.\n");
4241 }
4242 section = weston_config_get_section(config, "core", NULL, NULL);
4243
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004244 if (option_backend)
4245 backend = strdup(option_backend);
4246 else
4247 weston_config_section_get_string(section, "backend", &backend,
4248 NULL);
4249
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004250 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004251 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004252 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004253 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004254 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004255 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004256 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004257 }
4258
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004259 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004260 free(backend);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304261 if (!backend_init) {
4262 ret = EXIT_FAILURE;
4263 goto out_signals;
4264 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004265
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004266 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004267 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004268 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304269 ret = EXIT_FAILURE;
4270 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004271 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004272
Peter Maatmane5b42e42013-03-27 22:38:53 +01004273 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004274 segv_compositor = ec;
4275
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004276 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004277 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004278
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004279 setenv("WAYLAND_DISPLAY", socket_name, 1);
4280
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004281 if (option_shell)
4282 shell = strdup(option_shell);
4283 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004284 weston_config_section_get_string(section, "shell", &shell,
4285 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004286
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004287 if (load_modules(ec, shell, &argc, argv) < 0) {
4288 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004289 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004290 }
4291 free(shell);
4292
4293 weston_config_section_get_string(section, "modules", &modules, "");
4294 if (load_modules(ec, modules, &argc, argv) < 0) {
4295 free(modules);
4296 goto out;
4297 }
4298 free(modules);
4299
Ossama Othmana50e6e42013-05-14 09:48:26 -07004300 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004301 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004302
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004303 for (i = 1; i < argc; i++)
4304 weston_log("fatal: unhandled option: %s\n", argv[i]);
4305 if (argc > 1) {
4306 ret = EXIT_FAILURE;
4307 goto out;
4308 }
4309
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004310 weston_compositor_log_capabilities(ec);
4311
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004312 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4313 if (server_socket) {
4314 weston_log("Running with single client\n");
4315 fd = strtol(server_socket, &end, 0);
4316 if (*end != '\0')
4317 fd = -1;
4318 } else {
4319 fd = -1;
4320 }
4321
4322 if (fd != -1) {
4323 primary_client = wl_client_create(display, fd);
4324 if (!primary_client) {
4325 weston_log("fatal: failed to add client: %m\n");
4326 ret = EXIT_FAILURE;
4327 goto out;
4328 }
4329 primary_client_destroyed.notify =
4330 handle_primary_client_destroyed;
4331 wl_client_add_destroy_listener(primary_client,
4332 &primary_client_destroyed);
4333 } else {
4334 if (wl_display_add_socket(display, socket_name)) {
4335 weston_log("fatal: failed to add socket: %m\n");
4336 ret = EXIT_FAILURE;
4337 goto out;
4338 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004339 }
4340
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004341 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004342
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004343 wl_display_run(display);
4344
4345 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004346 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004347 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004348
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004349 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004350
Daniel Stone855028f2012-05-01 20:37:10 +01004351 weston_compositor_xkb_destroy(ec);
4352
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004353 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304354
4355out_signals:
4356 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4357 if (signals[i])
4358 wl_event_source_remove(signals[i]);
4359
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004360 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004361
Martin Minarik19e6f262012-06-07 13:08:46 +02004362 weston_log_file_close();
4363
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004364 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004365}