blob: 9ef3c4ebd6f7b02240662a7e688836382b1d8c4a [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010056#include "scaler-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
Hardening57388e42013-09-18 23:56:36 +0200141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200349static void
350weston_view_output_move_handler(struct wl_listener *listener,
351 void *data)
352{
353 struct weston_view *ev;
354 struct weston_output *output = data;
355
356 ev = container_of(listener, struct weston_view,
357 output_move_listener);
358
359 /* the child window's view->geometry is a relative coordinate to
360 * parent view, no need to move child_view. */
361 if (ev->geometry.parent)
362 return;
363
364 weston_view_set_position(ev,
365 ev->geometry.x + output->move_x,
366 ev->geometry.y + output->move_y);
367}
368
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200369static void
370weston_view_output_destroy_handler(struct wl_listener *listener,
371 void *data)
372{
373 struct weston_view *ev;
374 struct weston_compositor *ec;
375 struct weston_output *output, *first_output;
376 float x, y;
377 int visible;
378
379 ev = container_of(listener, struct weston_view,
380 output_destroy_listener);
381
382 ec = ev->surface->compositor;
383
384 /* the child window's view->geometry is a relative coordinate to
385 * parent view, no need to move child_view. */
386 if (ev->geometry.parent)
387 return;
388
389 x = ev->geometry.x;
390 y = ev->geometry.y;
391
392 /* At this point the destroyed output is not in the list anymore.
393 * If the view is still visible somewhere, we leave where it is,
394 * otherwise, move it to the first output. */
395 visible = 0;
396 wl_list_for_each(output, &ec->output_list, link) {
397 if (pixman_region32_contains_point(&output->region,
398 x, y, NULL)) {
399 visible = 1;
400 break;
401 }
402 }
403
404 if (!visible) {
405 first_output = container_of(ec->output_list.next,
406 struct weston_output, link);
407
408 x = first_output->x + first_output->width / 4;
409 y = first_output->y + first_output->height / 4;
410 }
411
412 weston_view_set_position(ev, x, y);
Zhang, Xiong Y31236932013-12-13 22:10:57 +0200413
414 if (ev->surface->output_destroyed)
415 ev->surface->output_destroyed(ev->surface);
Ander Conselvan de Oliveira24dff2b2013-12-19 18:34:28 +0200416
417 wl_list_remove(&ev->output_move_listener.link);
418 wl_list_remove(&ev->output_destroy_listener.link);
419
420 wl_list_init(&ev->output_move_listener.link);
421 wl_list_init(&ev->output_destroy_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200422}
423
Jason Ekstranda7af7042013-10-12 22:38:11 -0500424WL_EXPORT struct weston_view *
425weston_view_create(struct weston_surface *surface)
426{
427 struct weston_view *view;
428
429 view = calloc(1, sizeof *view);
430 if (view == NULL)
431 return NULL;
432
433 view->surface = surface;
434
Jason Ekstranda7af7042013-10-12 22:38:11 -0500435 /* Assign to surface */
436 wl_list_insert(&surface->views, &view->surface_link);
437
438 wl_signal_init(&view->destroy_signal);
439 wl_list_init(&view->link);
440 wl_list_init(&view->layer_link);
441
Xiong Zhang97116532013-10-23 13:58:31 +0800442 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443
444 pixman_region32_init(&view->clip);
445
446 view->alpha = 1.0;
447 pixman_region32_init(&view->transform.opaque);
448
449 wl_list_init(&view->geometry.transformation_list);
450 wl_list_insert(&view->geometry.transformation_list,
451 &view->transform.position.link);
452 weston_matrix_init(&view->transform.position.matrix);
453 wl_list_init(&view->geometry.child_list);
454 pixman_region32_init(&view->transform.boundingbox);
455 view->transform.dirty = 1;
456
457 view->output = NULL;
458
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200459 view->output_move_listener.notify = weston_view_output_move_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800460 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200461 view->output_destroy_listener.notify =
462 weston_view_output_destroy_handler;
Kristian Høgsbergdf42a802013-12-17 14:58:19 -0800463 wl_list_init(&view->output_destroy_listener.link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200464
Jason Ekstranda7af7042013-10-12 22:38:11 -0500465 return view;
466}
467
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500468WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500469weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500470{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500471 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400472
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200473 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400474 if (surface == NULL)
475 return NULL;
476
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500477 wl_signal_init(&surface->destroy_signal);
478
479 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500480
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500481 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200482 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400483
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100484 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
485 surface->buffer_viewport.scale = 1;
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200486 surface->buffer_viewport.viewport_set = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100487 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400488 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100489 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200490
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200491 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100492
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400493 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500494 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300495 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400496
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 wl_list_init(&surface->views);
498
499 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500500
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300501 surface->pending.buffer_destroy_listener.notify =
502 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300503 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300504 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300505 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300506 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300507
Pekka Paalanene67858b2013-04-25 13:57:42 +0300508 wl_list_init(&surface->subsurface_list);
509 wl_list_init(&surface->subsurface_list_pending);
510
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400511 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500512}
513
Alex Wu8811bf92012-02-28 18:07:54 +0800514WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500515weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200516 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500517{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100518 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500519}
520
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400521WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500522weston_view_to_global_float(struct weston_view *view,
523 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200524{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500525 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200526 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
527
Jason Ekstranda7af7042013-10-12 22:38:11 -0500528 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200529
530 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200531 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700532 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200533 v.f[3]);
534 *x = 0;
535 *y = 0;
536 return;
537 }
538
539 *x = v.f[0] / v.f[3];
540 *y = v.f[1] / v.f[3];
541 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500542 *x = sx + view->geometry.x;
543 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200544 }
545}
546
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500547WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200548weston_transformed_coord(int width, int height,
549 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200550 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200551 float sx, float sy, float *bx, float *by)
552{
553 switch (transform) {
554 case WL_OUTPUT_TRANSFORM_NORMAL:
555 default:
556 *bx = sx;
557 *by = sy;
558 break;
559 case WL_OUTPUT_TRANSFORM_FLIPPED:
560 *bx = width - sx;
561 *by = sy;
562 break;
563 case WL_OUTPUT_TRANSFORM_90:
564 *bx = height - sy;
565 *by = sx;
566 break;
567 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
568 *bx = height - sy;
569 *by = width - sx;
570 break;
571 case WL_OUTPUT_TRANSFORM_180:
572 *bx = width - sx;
573 *by = height - sy;
574 break;
575 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
576 *bx = sx;
577 *by = height - sy;
578 break;
579 case WL_OUTPUT_TRANSFORM_270:
580 *bx = sy;
581 *by = width - sx;
582 break;
583 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
584 *bx = sy;
585 *by = sx;
586 break;
587 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200588
589 *bx *= scale;
590 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200591}
592
593WL_EXPORT pixman_box32_t
594weston_transformed_rect(int width, int height,
595 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200596 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200597 pixman_box32_t rect)
598{
599 float x1, x2, y1, y2;
600
601 pixman_box32_t ret;
602
Alexander Larsson4ea95522013-05-22 14:41:37 +0200603 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200604 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200605 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200606 rect.x2, rect.y2, &x2, &y2);
607
608 if (x1 <= x2) {
609 ret.x1 = x1;
610 ret.x2 = x2;
611 } else {
612 ret.x1 = x2;
613 ret.x2 = x1;
614 }
615
616 if (y1 <= y2) {
617 ret.y1 = y1;
618 ret.y2 = y2;
619 } else {
620 ret.y1 = y2;
621 ret.y2 = y1;
622 }
623
624 return ret;
625}
626
627WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500628weston_transformed_region(int width, int height,
629 enum wl_output_transform transform,
630 int32_t scale,
631 pixman_region32_t *src, pixman_region32_t *dest)
632{
633 pixman_box32_t *src_rects, *dest_rects;
634 int nrects, i;
635
636 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
637 if (src != dest)
638 pixman_region32_copy(dest, src);
639 return;
640 }
641
642 src_rects = pixman_region32_rectangles(src, &nrects);
643 dest_rects = malloc(nrects * sizeof(*dest_rects));
644 if (!dest_rects)
645 return;
646
647 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
648 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
649 } else {
650 for (i = 0; i < nrects; i++) {
651 switch (transform) {
652 default:
653 case WL_OUTPUT_TRANSFORM_NORMAL:
654 dest_rects[i].x1 = src_rects[i].x1;
655 dest_rects[i].y1 = src_rects[i].y1;
656 dest_rects[i].x2 = src_rects[i].x2;
657 dest_rects[i].y2 = src_rects[i].y2;
658 break;
659 case WL_OUTPUT_TRANSFORM_90:
660 dest_rects[i].x1 = height - src_rects[i].y2;
661 dest_rects[i].y1 = src_rects[i].x1;
662 dest_rects[i].x2 = height - src_rects[i].y1;
663 dest_rects[i].y2 = src_rects[i].x2;
664 break;
665 case WL_OUTPUT_TRANSFORM_180:
666 dest_rects[i].x1 = width - src_rects[i].x2;
667 dest_rects[i].y1 = height - src_rects[i].y2;
668 dest_rects[i].x2 = width - src_rects[i].x1;
669 dest_rects[i].y2 = height - src_rects[i].y1;
670 break;
671 case WL_OUTPUT_TRANSFORM_270:
672 dest_rects[i].x1 = src_rects[i].y1;
673 dest_rects[i].y1 = width - src_rects[i].x2;
674 dest_rects[i].x2 = src_rects[i].y2;
675 dest_rects[i].y2 = width - src_rects[i].x1;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED:
678 dest_rects[i].x1 = width - src_rects[i].x2;
679 dest_rects[i].y1 = src_rects[i].y1;
680 dest_rects[i].x2 = width - src_rects[i].x1;
681 dest_rects[i].y2 = src_rects[i].y2;
682 break;
683 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
684 dest_rects[i].x1 = height - src_rects[i].y2;
685 dest_rects[i].y1 = width - src_rects[i].x2;
686 dest_rects[i].x2 = height - src_rects[i].y1;
687 dest_rects[i].y2 = width - src_rects[i].x1;
688 break;
689 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
690 dest_rects[i].x1 = src_rects[i].x1;
691 dest_rects[i].y1 = height - src_rects[i].y2;
692 dest_rects[i].x2 = src_rects[i].x2;
693 dest_rects[i].y2 = height - src_rects[i].y1;
694 break;
695 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
696 dest_rects[i].x1 = src_rects[i].y1;
697 dest_rects[i].y1 = src_rects[i].x1;
698 dest_rects[i].x2 = src_rects[i].y2;
699 dest_rects[i].y2 = src_rects[i].x2;
700 break;
701 }
702 }
703 }
704
705 if (scale != 1) {
706 for (i = 0; i < nrects; i++) {
707 dest_rects[i].x1 *= scale;
708 dest_rects[i].x2 *= scale;
709 dest_rects[i].y1 *= scale;
710 dest_rects[i].y2 *= scale;
711 }
712 }
713
714 pixman_region32_clear(dest);
715 pixman_region32_init_rects(dest, dest_rects, nrects);
716 free(dest_rects);
717}
718
Jonny Lamb74130762013-11-26 18:19:46 +0100719static void
720scaler_surface_to_buffer(struct weston_surface *surface,
721 float sx, float sy, float *bx, float *by)
722{
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200723 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +0100724 double a, b;
725
726 a = sx / surface->buffer_viewport.dst_width;
727 b = a * wl_fixed_to_double(surface->buffer_viewport.src_width);
728 *bx = b + wl_fixed_to_double(surface->buffer_viewport.src_x);
729
730 a = sy / surface->buffer_viewport.dst_height;
731 b = a * wl_fixed_to_double(surface->buffer_viewport.src_height);
732 *by = b + wl_fixed_to_double(surface->buffer_viewport.src_y);
733 } else {
734 *bx = sx;
735 *by = sy;
736 }
737}
738
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500739WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200740weston_surface_to_buffer_float(struct weston_surface *surface,
741 float sx, float sy, float *bx, float *by)
742{
Jonny Lamb74130762013-11-26 18:19:46 +0100743 /* first transform coordinates if the scaler is set */
744 scaler_surface_to_buffer(surface, sx, sy, bx, by);
745
Jason Ekstranda7af7042013-10-12 22:38:11 -0500746 weston_transformed_coord(surface->width,
747 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100748 surface->buffer_viewport.transform,
749 surface->buffer_viewport.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100750 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200751}
752
Alexander Larsson4ea95522013-05-22 14:41:37 +0200753WL_EXPORT void
754weston_surface_to_buffer(struct weston_surface *surface,
755 int sx, int sy, int *bx, int *by)
756{
757 float bxf, byf;
758
Jonny Lamb74130762013-11-26 18:19:46 +0100759 weston_surface_to_buffer_float(surface,
760 sx, sy, &bxf, &byf);
761
Alexander Larsson4ea95522013-05-22 14:41:37 +0200762 *bx = floorf(bxf);
763 *by = floorf(byf);
764}
765
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200766WL_EXPORT pixman_box32_t
767weston_surface_to_buffer_rect(struct weston_surface *surface,
768 pixman_box32_t rect)
769{
Jonny Lamb74130762013-11-26 18:19:46 +0100770 float xf, yf;
771
772 /* first transform box coordinates if the scaler is set */
773 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
774 rect.x1 = floorf(xf);
775 rect.y1 = floorf(yf);
776
777 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
778 rect.x2 = floorf(xf);
779 rect.y2 = floorf(yf);
780
Jason Ekstranda7af7042013-10-12 22:38:11 -0500781 return weston_transformed_rect(surface->width,
782 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100783 surface->buffer_viewport.transform,
784 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200785 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200786}
787
788WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400790 struct weston_plane *plane)
791{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500792 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400793 return;
794
Jason Ekstranda7af7042013-10-12 22:38:11 -0500795 weston_view_damage_below(view);
796 view->plane = plane;
797 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400798}
799
800WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200802{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500803 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200804
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500805 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 pixman_region32_subtract(&damage, &view->transform.boundingbox,
807 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800808 if (view->plane)
809 pixman_region32_union(&view->plane->damage,
810 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500811 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800812 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200813}
814
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400815static void
816weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
817{
818 uint32_t different = es->output_mask ^ mask;
819 uint32_t entered = mask & different;
820 uint32_t left = es->output_mask & different;
821 struct weston_output *output;
822 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500823 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400824
825 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500826 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400827 return;
828 if (different == 0)
829 return;
830
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500831 client = wl_resource_get_client(es->resource);
832
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400833 wl_list_for_each(output, &es->compositor->output_list, link) {
834 if (1 << output->id & different)
835 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500836 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400837 client);
838 if (resource == NULL)
839 continue;
840 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500841 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400842 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500843 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400844 }
845}
846
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200847
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400848static void
849weston_surface_assign_output(struct weston_surface *es)
850{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 struct weston_output *new_output;
852 struct weston_view *view;
853 pixman_region32_t region;
854 uint32_t max, area, mask;
855 pixman_box32_t *e;
856
857 new_output = NULL;
858 max = 0;
859 mask = 0;
860 pixman_region32_init(&region);
861 wl_list_for_each(view, &es->views, surface_link) {
862 if (!view->output)
863 continue;
864
865 pixman_region32_intersect(&region, &view->transform.boundingbox,
866 &view->output->region);
867
868 e = pixman_region32_extents(&region);
869 area = (e->x2 - e->x1) * (e->y2 - e->y1);
870
871 mask |= view->output_mask;
872
873 if (area >= max) {
874 new_output = view->output;
875 max = area;
876 }
877 }
878 pixman_region32_fini(&region);
879
880 es->output = new_output;
881 weston_surface_update_output_mask(es, mask);
882}
883
884static void
885weston_view_assign_output(struct weston_view *ev)
886{
887 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400888 struct weston_output *output, *new_output;
889 pixman_region32_t region;
890 uint32_t max, area, mask;
891 pixman_box32_t *e;
892
893 new_output = NULL;
894 max = 0;
895 mask = 0;
896 pixman_region32_init(&region);
897 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500898 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400899 &output->region);
900
901 e = pixman_region32_extents(&region);
902 area = (e->x2 - e->x1) * (e->y2 - e->y1);
903
904 if (area > 0)
905 mask |= 1 << output->id;
906
907 if (area >= max) {
908 new_output = output;
909 max = area;
910 }
911 }
912 pixman_region32_fini(&region);
913
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200914 if (ev->output_mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200915 wl_list_remove(&ev->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200916 wl_list_remove(&ev->output_destroy_listener.link);
917 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200918
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200919 if (mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200920 wl_signal_add(&new_output->move_signal,
921 &ev->output_move_listener);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200922 wl_signal_add(&new_output->destroy_signal,
923 &ev->output_destroy_listener);
Kristian Høgsbergb0fb25d2014-01-09 22:41:48 -0800924 } else {
925 wl_list_init(&ev->output_move_listener.link);
926 wl_list_init(&ev->output_destroy_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200927 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200928
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 ev->output = new_output;
930 ev->output_mask = mask;
931
932 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400933}
934
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200935static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
937 int32_t width, int32_t height,
938 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200939{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200940 float min_x = HUGE_VALF, min_y = HUGE_VALF;
941 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200942 int32_t s[4][2] = {
943 { sx, sy },
944 { sx, sy + height },
945 { sx + width, sy },
946 { sx + width, sy + height }
947 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200948 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200949 int i;
950
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300951 if (width == 0 || height == 0) {
952 /* avoid rounding empty bbox to 1x1 */
953 pixman_region32_init(bbox);
954 return;
955 }
956
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200957 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200958 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200960 if (x < min_x)
961 min_x = x;
962 if (x > max_x)
963 max_x = x;
964 if (y < min_y)
965 min_y = y;
966 if (y > max_y)
967 max_y = y;
968 }
969
Pekka Paalanen219b9822012-02-08 15:38:37 +0200970 int_x = floorf(min_x);
971 int_y = floorf(min_y);
972 pixman_region32_init_rect(bbox, int_x, int_y,
973 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200974}
975
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200976static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200978{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200980
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200981 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 view->geometry.x = roundf(view->geometry.x);
983 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200984
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500985 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
987 view->transform.position.matrix.d[12] = view->geometry.x;
988 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500989
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500991
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 view->transform.inverse = view->transform.position.matrix;
993 view->transform.inverse.d[12] = -view->geometry.x;
994 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500995
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 pixman_region32_init_rect(&view->transform.boundingbox,
997 view->geometry.x,
998 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600999 view->surface->width,
1000 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001001
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 if (view->alpha == 1.0) {
1003 pixman_region32_copy(&view->transform.opaque,
1004 &view->surface->opaque);
1005 pixman_region32_translate(&view->transform.opaque,
1006 view->geometry.x,
1007 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001008 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001009}
1010
1011static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001013{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 struct weston_view *parent = view->geometry.parent;
1015 struct weston_matrix *matrix = &view->transform.matrix;
1016 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001017 struct weston_transform *tform;
1018
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001020
1021 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1023 view->transform.position.matrix.d[12] = view->geometry.x;
1024 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001025
1026 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001028 weston_matrix_multiply(matrix, &tform->matrix);
1029
Pekka Paalanen483243f2013-03-08 14:56:50 +02001030 if (parent)
1031 weston_matrix_multiply(matrix, &parent->transform.matrix);
1032
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001033 if (weston_matrix_invert(inverse, matrix) < 0) {
1034 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 weston_log("error: weston_view %p"
1036 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001037 return -1;
1038 }
1039
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001040 view_compute_bbox(view, 0, 0,
1041 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001043
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001044 return 0;
1045}
1046
1047WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001049{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001051
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001053 return;
1054
Pekka Paalanen483243f2013-03-08 14:56:50 +02001055 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001057
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001059
Jason Ekstranda7af7042013-10-12 22:38:11 -05001060 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001061
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 pixman_region32_fini(&view->transform.boundingbox);
1063 pixman_region32_fini(&view->transform.opaque);
1064 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001065
Pekka Paalanencd403622012-01-25 13:37:39 +02001066 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001067 if (view->geometry.transformation_list.next ==
1068 &view->transform.position.link &&
1069 view->geometry.transformation_list.prev ==
1070 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001071 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001073 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074 if (weston_view_update_transform_enable(view) < 0)
1075 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001076 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001077
Jason Ekstranda7af7042013-10-12 22:38:11 -05001078 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001081
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082 wl_signal_emit(&view->surface->compositor->transform_signal,
1083 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001084}
1085
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001086WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001090
1091 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 * The invariant: if view->geometry.dirty, then all views
1093 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001094 * Corollary: if not parent->geometry.dirty, then all ancestors
1095 * are not dirty.
1096 */
1097
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001099 return;
1100
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001102
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001104 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001106}
1107
1108WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109weston_view_to_global_fixed(struct weston_view *view,
1110 wl_fixed_t vx, wl_fixed_t vy,
1111 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001112{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001113 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001114
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 weston_view_to_global_float(view,
1116 wl_fixed_to_double(vx),
1117 wl_fixed_to_double(vy),
1118 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001119 *x = wl_fixed_from_double(xf);
1120 *y = wl_fixed_from_double(yf);
1121}
1122
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001123WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124weston_view_from_global_float(struct weston_view *view,
1125 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001126{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001127 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001128 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001131
1132 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001133 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001134 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001135 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136 *vx = 0;
1137 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001138 return;
1139 }
1140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 *vx = v.f[0] / v.f[3];
1142 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001143 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 *vx = x - view->geometry.x;
1145 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001146 }
1147}
1148
1149WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001150weston_view_from_global_fixed(struct weston_view *view,
1151 wl_fixed_t x, wl_fixed_t y,
1152 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001153{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001155
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 weston_view_from_global_float(view,
1157 wl_fixed_to_double(x),
1158 wl_fixed_to_double(y),
1159 &vxf, &vyf);
1160 *vx = wl_fixed_from_double(vxf);
1161 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001162}
1163
1164WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165weston_view_from_global(struct weston_view *view,
1166 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001167{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001169
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1171 *vx = floorf(vxf);
1172 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001173}
1174
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001175WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001176weston_surface_schedule_repaint(struct weston_surface *surface)
1177{
1178 struct weston_output *output;
1179
1180 wl_list_for_each(output, &surface->compositor->output_list, link)
1181 if (surface->output_mask & (1 << output->id))
1182 weston_output_schedule_repaint(output);
1183}
1184
1185WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186weston_view_schedule_repaint(struct weston_view *view)
1187{
1188 struct weston_output *output;
1189
1190 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1191 if (view->output_mask & (1 << output->id))
1192 weston_output_schedule_repaint(output);
1193}
1194
1195WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001196weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001197{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001198 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 0, 0, surface->width,
1200 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001201
Kristian Høgsberg98238702012-08-03 16:29:12 -04001202 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001203}
1204
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001205WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001207{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001208 if (view->geometry.x == x && view->geometry.y == y)
1209 return;
1210
Jason Ekstranda7af7042013-10-12 22:38:11 -05001211 view->geometry.x = x;
1212 view->geometry.y = y;
1213 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001214}
1215
Pekka Paalanen483243f2013-03-08 14:56:50 +02001216static void
1217transform_parent_handle_parent_destroy(struct wl_listener *listener,
1218 void *data)
1219{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 struct weston_view *view =
1221 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222 geometry.parent_destroy_listener);
1223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225}
1226
1227WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228weston_view_set_transform_parent(struct weston_view *view,
1229 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001230{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 if (view->geometry.parent) {
1232 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1233 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001234 }
1235
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001237
Jason Ekstranda7af7042013-10-12 22:38:11 -05001238 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001239 transform_parent_handle_parent_destroy;
1240 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001241 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001243 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001245 }
1246
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 weston_view_geometry_dirty(view);
1248}
1249
1250WL_EXPORT int
1251weston_view_is_mapped(struct weston_view *view)
1252{
1253 if (view->output)
1254 return 1;
1255 else
1256 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001257}
1258
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001259WL_EXPORT int
1260weston_surface_is_mapped(struct weston_surface *surface)
1261{
1262 if (surface->output)
1263 return 1;
1264 else
1265 return 0;
1266}
1267
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001268static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001269surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001270{
1271 struct weston_view *view;
1272
1273 if (surface->width == width && surface->height == height)
1274 return;
1275
1276 surface->width = width;
1277 surface->height = height;
1278
1279 wl_list_for_each(view, &surface->views, surface_link)
1280 weston_view_geometry_dirty(view);
1281}
1282
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001283WL_EXPORT void
1284weston_surface_set_size(struct weston_surface *surface,
1285 int32_t width, int32_t height)
1286{
1287 assert(!surface->resource);
1288 surface_set_size(surface, width, height);
1289}
1290
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001291static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001292weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001293{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001294 int32_t width, height;
1295
1296 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001297 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001298 return;
1299 }
1300
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02001301 if (surface->buffer_viewport.viewport_set) {
Jonny Lamb74130762013-11-26 18:19:46 +01001302 surface->width = surface->buffer_viewport.dst_width;
1303 surface->height = surface->buffer_viewport.dst_height;
1304 return;
1305 }
1306
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001307 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001308 case WL_OUTPUT_TRANSFORM_90:
1309 case WL_OUTPUT_TRANSFORM_270:
1310 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1311 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001312 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001313 height = surface->buffer_ref.buffer->width;
1314 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001315 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001316 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001317 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001318 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001319 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001320
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001321 width = width / surface->buffer_viewport.scale;
1322 height = height / surface->buffer_viewport.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001323 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001324}
1325
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001326WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001327weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001328{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001329 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001330
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001331 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001332
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001333 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001334}
1335
Jason Ekstranda7af7042013-10-12 22:38:11 -05001336WL_EXPORT struct weston_view *
1337weston_compositor_pick_view(struct weston_compositor *compositor,
1338 wl_fixed_t x, wl_fixed_t y,
1339 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001340{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001342
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343 wl_list_for_each(view, &compositor->view_list, link) {
1344 weston_view_from_global_fixed(view, x, y, vx, vy);
1345 if (pixman_region32_contains_point(&view->surface->input,
1346 wl_fixed_to_int(*vx),
1347 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001348 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001349 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001350 }
1351
1352 return NULL;
1353}
1354
1355static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001356weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001357{
Daniel Stone37816df2012-05-16 18:45:18 +01001358 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001359
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001360 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001361 return;
1362
Daniel Stone37816df2012-05-16 18:45:18 +01001363 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001364 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001365}
1366
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001367WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001368weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001369{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001370 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 if (!weston_view_is_mapped(view))
1373 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001374
Jason Ekstranda7af7042013-10-12 22:38:11 -05001375 weston_view_damage_below(view);
1376 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001377 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001378 wl_list_remove(&view->layer_link);
1379 wl_list_init(&view->layer_link);
1380 wl_list_remove(&view->link);
1381 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001382 wl_list_remove(&view->output_move_listener.link);
1383 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001384 wl_list_remove(&view->output_destroy_listener.link);
1385 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 view->output_mask = 0;
1387 weston_surface_assign_output(view->surface);
1388
1389 if (weston_surface_is_mapped(view->surface))
1390 return;
1391
1392 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1393 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001394 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001396 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001397 NULL,
1398 wl_fixed_from_int(0),
1399 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001401 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001402 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001403}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001404
Jason Ekstranda7af7042013-10-12 22:38:11 -05001405WL_EXPORT void
1406weston_surface_unmap(struct weston_surface *surface)
1407{
1408 struct weston_view *view;
1409
1410 wl_list_for_each(view, &surface->views, surface_link)
1411 weston_view_unmap(view);
1412 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001413}
1414
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001415struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001416 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001417 struct wl_list link;
1418};
1419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420WL_EXPORT void
1421weston_view_destroy(struct weston_view *view)
1422{
1423 wl_signal_emit(&view->destroy_signal, view);
1424
1425 assert(wl_list_empty(&view->geometry.child_list));
1426
1427 if (weston_view_is_mapped(view)) {
1428 weston_view_unmap(view);
1429 weston_compositor_build_view_list(view->surface->compositor);
1430 }
1431
1432 wl_list_remove(&view->link);
1433 wl_list_remove(&view->layer_link);
1434
1435 pixman_region32_fini(&view->clip);
1436 pixman_region32_fini(&view->transform.boundingbox);
1437
1438 weston_view_set_transform_parent(view, NULL);
1439
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440 wl_list_remove(&view->surface_link);
1441
1442 free(view);
1443}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001444
1445WL_EXPORT void
1446weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001447{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001448 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001449 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001450
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001451 if (--surface->ref_count > 0)
1452 return;
1453
1454 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1455
Pekka Paalanene67858b2013-04-25 13:57:42 +03001456 assert(wl_list_empty(&surface->subsurface_list_pending));
1457 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001458
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1460 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001461
Pekka Paalanenbc106382012-10-10 12:49:31 +03001462 wl_list_for_each_safe(cb, next,
1463 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001464 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001465
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001466 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001467 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001468 pixman_region32_fini(&surface->pending.damage);
1469
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001470 if (surface->pending.buffer)
1471 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1472
Pekka Paalanende685b82012-12-04 15:58:12 +02001473 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001474
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001475 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001476 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001477 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001478
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001479 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001480 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001481
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001482 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001483}
1484
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001485static void
1486destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001487{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001488 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001489
Giulio Camuffo0d379742013-11-15 22:06:15 +01001490 /* Set the resource to NULL, since we don't want to leave a
1491 * dangling pointer if the surface was refcounted and survives
1492 * the weston_surface_destroy() call. */
1493 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001494 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001495}
1496
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001497static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001498weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1499{
1500 struct weston_buffer *buffer =
1501 container_of(listener, struct weston_buffer, destroy_listener);
1502
1503 wl_signal_emit(&buffer->destroy_signal, buffer);
1504 free(buffer);
1505}
1506
1507struct weston_buffer *
1508weston_buffer_from_resource(struct wl_resource *resource)
1509{
1510 struct weston_buffer *buffer;
1511 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001512
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001513 listener = wl_resource_get_destroy_listener(resource,
1514 weston_buffer_destroy_handler);
1515
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001516 if (listener)
1517 return container_of(listener, struct weston_buffer,
1518 destroy_listener);
1519
1520 buffer = zalloc(sizeof *buffer);
1521 if (buffer == NULL)
1522 return NULL;
1523
1524 buffer->resource = resource;
1525 wl_signal_init(&buffer->destroy_signal);
1526 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001527 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001528 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001529
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001530 return buffer;
1531}
1532
1533static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001534weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1535 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001536{
Pekka Paalanende685b82012-12-04 15:58:12 +02001537 struct weston_buffer_reference *ref =
1538 container_of(listener, struct weston_buffer_reference,
1539 destroy_listener);
1540
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001541 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001542 ref->buffer = NULL;
1543}
1544
1545WL_EXPORT void
1546weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001547 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001548{
1549 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001550 ref->buffer->busy_count--;
1551 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001552 assert(wl_resource_get_client(ref->buffer->resource));
1553 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001554 WL_BUFFER_RELEASE);
1555 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001556 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001557 }
1558
Pekka Paalanende685b82012-12-04 15:58:12 +02001559 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001560 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001561 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001562 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001563 }
1564
Pekka Paalanende685b82012-12-04 15:58:12 +02001565 ref->buffer = buffer;
1566 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1567}
1568
1569static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001570weston_surface_attach(struct weston_surface *surface,
1571 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001572{
1573 weston_buffer_reference(&surface->buffer_ref, buffer);
1574
Pekka Paalanena6421c42012-12-04 15:58:10 +02001575 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001576 if (weston_surface_is_mapped(surface))
1577 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001578 }
1579
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001580 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001581}
1582
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001583WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001584weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001586 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001587
1588 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001589 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001590}
1591
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001592WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001593weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001594{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001595 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001596
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001597 pixman_region32_union(&compositor->primary_plane.damage,
1598 &compositor->primary_plane.damage,
1599 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001600 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001601}
1602
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001603static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001605{
Pekka Paalanende685b82012-12-04 15:58:12 +02001606 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001607 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001608 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001609
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 empty_region(&surface->damage);
1611}
1612
1613static void
1614view_accumulate_damage(struct weston_view *view,
1615 pixman_region32_t *opaque)
1616{
1617 pixman_region32_t damage;
1618
1619 pixman_region32_init(&damage);
1620 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001621 pixman_box32_t *extents;
1622
Jason Ekstranda7af7042013-10-12 22:38:11 -05001623 extents = pixman_region32_extents(&view->surface->damage);
1624 view_compute_bbox(view, extents->x1, extents->y1,
1625 extents->x2 - extents->x1,
1626 extents->y2 - extents->y1,
1627 &damage);
1628 pixman_region32_translate(&damage,
1629 -view->plane->x,
1630 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001631 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001632 pixman_region32_copy(&damage, &view->surface->damage);
1633 pixman_region32_translate(&damage,
1634 view->geometry.x - view->plane->x,
1635 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001636 }
1637
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 pixman_region32_subtract(&damage, &damage, opaque);
1639 pixman_region32_union(&view->plane->damage,
1640 &view->plane->damage, &damage);
1641 pixman_region32_fini(&damage);
1642 pixman_region32_copy(&view->clip, opaque);
1643 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001644}
1645
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001646static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001647compositor_accumulate_damage(struct weston_compositor *ec)
1648{
1649 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001651 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001652
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001653 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001654
1655 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001656 pixman_region32_copy(&plane->clip, &clip);
1657
1658 pixman_region32_init(&opaque);
1659
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660 wl_list_for_each(ev, &ec->view_list, link) {
1661 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001662 continue;
1663
Jason Ekstranda7af7042013-10-12 22:38:11 -05001664 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001665 }
1666
1667 pixman_region32_union(&clip, &clip, &opaque);
1668 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001669 }
1670
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001671 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001672
Jason Ekstranda7af7042013-10-12 22:38:11 -05001673 wl_list_for_each(ev, &ec->view_list, link)
1674 ev->surface->touched = 0;
1675
1676 wl_list_for_each(ev, &ec->view_list, link) {
1677 if (ev->surface->touched)
1678 continue;
1679 ev->surface->touched = 1;
1680
1681 surface_flush_damage(ev->surface);
1682
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001683 /* Both the renderer and the backend have seen the buffer
1684 * by now. If renderer needs the buffer, it has its own
1685 * reference set. If the backend wants to keep the buffer
1686 * around for migrating the surface into a non-primary plane
1687 * later, keep_buffer is true. Otherwise, drop the core
1688 * reference now, and allow early buffer release. This enables
1689 * clients to use single-buffering.
1690 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001691 if (!ev->surface->keep_buffer)
1692 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001693 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001694}
1695
1696static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001698{
1699 struct weston_subsurface *sub;
1700
Pekka Paalanene67858b2013-04-25 13:57:42 +03001701 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001702 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001703 continue;
1704
Jason Ekstranda7af7042013-10-12 22:38:11 -05001705 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1706 wl_list_init(&sub->surface->views);
1707
1708 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001709 }
1710}
1711
1712static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001714{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 struct weston_subsurface *sub;
1716 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001717
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1719 if (sub->surface == surface)
1720 continue;
1721
1722 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1723 weston_view_destroy(view);
1724
1725 surface_free_unused_subsurface_views(sub->surface);
1726 }
1727}
1728
1729static void
1730view_list_add_subsurface_view(struct weston_compositor *compositor,
1731 struct weston_subsurface *sub,
1732 struct weston_view *parent)
1733{
1734 struct weston_subsurface *child;
1735 struct weston_view *view = NULL, *iv;
1736
1737 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1738 if (iv->geometry.parent == parent) {
1739 view = iv;
1740 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001741 }
1742 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001743
1744 if (view) {
1745 /* Put it back in the surface's list of views */
1746 wl_list_remove(&view->surface_link);
1747 wl_list_insert(&sub->surface->views, &view->surface_link);
1748 } else {
1749 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001750 weston_view_set_position(view,
1751 sub->position.x,
1752 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001753 weston_view_set_transform_parent(view, parent);
1754 }
1755
1756 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001757
Pekka Paalanenb188e912013-11-19 14:03:35 +02001758 if (wl_list_empty(&sub->surface->subsurface_list)) {
1759 wl_list_insert(compositor->view_list.prev, &view->link);
1760 return;
1761 }
1762
1763 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1764 if (child->surface == sub->surface)
1765 wl_list_insert(compositor->view_list.prev, &view->link);
1766 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001767 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001768 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769}
1770
1771static void
1772view_list_add(struct weston_compositor *compositor,
1773 struct weston_view *view)
1774{
1775 struct weston_subsurface *sub;
1776
1777 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778
Pekka Paalanenb188e912013-11-19 14:03:35 +02001779 if (wl_list_empty(&view->surface->subsurface_list)) {
1780 wl_list_insert(compositor->view_list.prev, &view->link);
1781 return;
1782 }
1783
1784 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1785 if (sub->surface == view->surface)
1786 wl_list_insert(compositor->view_list.prev, &view->link);
1787 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001789 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790}
1791
1792static void
1793weston_compositor_build_view_list(struct weston_compositor *compositor)
1794{
1795 struct weston_view *view;
1796 struct weston_layer *layer;
1797
1798 wl_list_for_each(layer, &compositor->layer_list, link)
1799 wl_list_for_each(view, &layer->view_list, layer_link)
1800 surface_stash_subsurface_views(view->surface);
1801
1802 wl_list_init(&compositor->view_list);
1803 wl_list_for_each(layer, &compositor->layer_list, link) {
1804 wl_list_for_each(view, &layer->view_list, layer_link) {
1805 view_list_add(compositor, view);
1806 }
1807 }
1808
1809 wl_list_for_each(layer, &compositor->layer_list, link)
1810 wl_list_for_each(view, &layer->view_list, layer_link)
1811 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001812}
1813
David Herrmann1edf44c2013-10-22 17:11:26 +02001814static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001815weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001816{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001817 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001819 struct weston_animation *animation, *next;
1820 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001821 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001822 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001823 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001824
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001825 if (output->destroying)
1826 return 0;
1827
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001828 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001830
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001831 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001832 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001833 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001834 wl_list_for_each(ev, &ec->view_list, link)
1835 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001836
Pekka Paalanene67858b2013-04-25 13:57:42 +03001837 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838 wl_list_for_each(ev, &ec->view_list, link) {
1839 /* Note: This operation is safe to do multiple times on the
1840 * same surface.
1841 */
1842 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001843 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844 &ev->surface->frame_callback_list);
1845 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001846 }
1847 }
1848
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001849 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001850
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001851 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001852 pixman_region32_intersect(&output_damage,
1853 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001854 pixman_region32_subtract(&output_damage,
1855 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001856
Scott Moreauccbf29d2012-02-22 14:21:41 -07001857 if (output->dirty)
1858 weston_output_update_matrix(output);
1859
David Herrmann1edf44c2013-10-22 17:11:26 +02001860 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001861
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001862 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001863
Kristian Høgsbergef044142011-06-21 15:02:12 -04001864 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001865
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001866 weston_compositor_repick(ec);
1867 wl_event_loop_dispatch(ec->input_loop, 0);
1868
Jonas Ådahldb773762012-06-13 00:01:21 +02001869 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001870 wl_callback_send_done(cb->resource, msecs);
1871 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001872 }
1873
Scott Moreaud64cf212012-06-08 19:40:54 -06001874 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001875 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001876 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001877 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001878
1879 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001880}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001881
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001882static int
1883weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001884{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001885 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001886
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001887 wl_event_loop_dispatch(compositor->input_loop, 0);
1888
1889 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001890}
1891
1892WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001893weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001894{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001895 struct weston_compositor *compositor = output->compositor;
1896 struct wl_event_loop *loop =
1897 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001898 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001899
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001900 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001901
1902 if (output->repaint_needed &&
1903 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1904 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001905 r = weston_output_repaint(output, msecs);
1906 if (!r)
1907 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001908 }
1909
1910 output->repaint_scheduled = 0;
1911 if (compositor->input_loop_source)
1912 return;
1913
1914 fd = wl_event_loop_get_fd(compositor->input_loop);
1915 compositor->input_loop_source =
1916 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1917 weston_compositor_read_input, compositor);
1918}
1919
1920static void
1921idle_repaint(void *data)
1922{
1923 struct weston_output *output = data;
1924
Jonas Ådahle5a12252013-04-05 23:07:11 +02001925 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001926}
1927
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001928WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001929weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1930{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001932 if (below != NULL)
1933 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001934}
1935
1936WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001937weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001938{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001939 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001940 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001941
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001942 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1943 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001944 return;
1945
Kristian Høgsbergef044142011-06-21 15:02:12 -04001946 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001947 output->repaint_needed = 1;
1948 if (output->repaint_scheduled)
1949 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001950
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001951 wl_event_loop_add_idle(loop, idle_repaint, output);
1952 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001953
1954 if (compositor->input_loop_source) {
1955 wl_event_source_remove(compositor->input_loop_source);
1956 compositor->input_loop_source = NULL;
1957 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001958}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001959
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001960WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001961weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1962{
1963 struct weston_output *output;
1964
1965 wl_list_for_each(output, &compositor->output_list, link)
1966 weston_output_schedule_repaint(output);
1967}
1968
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001969static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001970surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001971{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001972 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001973}
1974
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001975static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001976surface_attach(struct wl_client *client,
1977 struct wl_resource *resource,
1978 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1979{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001980 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001981 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001982
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001983 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001984 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001985 if (buffer == NULL) {
1986 wl_client_post_no_memory(client);
1987 return;
1988 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001989 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001990
Pekka Paalanende685b82012-12-04 15:58:12 +02001991 /* Attach, attach, without commit in between does not send
1992 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001993 if (surface->pending.buffer)
1994 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001995
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001996 surface->pending.sx = sx;
1997 surface->pending.sy = sy;
1998 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001999 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002000 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002001 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002002 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002003 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002004}
2005
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002006static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002007surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002008 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002009 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002010{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002011 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002012
Pekka Paalanen8e159182012-10-10 12:49:25 +03002013 pixman_region32_union_rect(&surface->pending.damage,
2014 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002015 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002016}
2017
Kristian Høgsberg33418202011-08-16 23:01:28 -04002018static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002019destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002020{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002021 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002022
2023 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002024 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002025}
2026
2027static void
2028surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002029 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002030{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002031 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002032 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002033
2034 cb = malloc(sizeof *cb);
2035 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002036 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002037 return;
2038 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002039
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002040 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2041 callback);
2042 if (cb->resource == NULL) {
2043 free(cb);
2044 wl_resource_post_no_memory(resource);
2045 return;
2046 }
2047
Jason Ekstranda85118c2013-06-27 20:17:02 -05002048 wl_resource_set_implementation(cb->resource, NULL, cb,
2049 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002050
Pekka Paalanenbc106382012-10-10 12:49:31 +03002051 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002052}
2053
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002054static void
2055surface_set_opaque_region(struct wl_client *client,
2056 struct wl_resource *resource,
2057 struct wl_resource *region_resource)
2058{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002059 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002060 struct weston_region *region;
2061
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002062 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002063 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002064 pixman_region32_copy(&surface->pending.opaque,
2065 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002066 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002067 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002068 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002069}
2070
2071static void
2072surface_set_input_region(struct wl_client *client,
2073 struct wl_resource *resource,
2074 struct wl_resource *region_resource)
2075{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002076 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002077 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002078
2079 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002080 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002081 pixman_region32_copy(&surface->pending.input,
2082 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002083 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002084 pixman_region32_fini(&surface->pending.input);
2085 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002086 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002087}
2088
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002089static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002090weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002091{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002092 struct weston_subsurface *sub;
2093
2094 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2095 parent_link_pending) {
2096 wl_list_remove(&sub->parent_link);
2097 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2098 }
2099}
2100
2101static void
2102weston_surface_commit(struct weston_surface *surface)
2103{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002105 pixman_region32_t opaque;
2106
Alexander Larsson4ea95522013-05-22 14:41:37 +02002107 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002108 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002109 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002110 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002111
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002112 /* wl_surface.attach */
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002113 if (surface->pending.buffer || surface->pending.newly_attached) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002114 weston_surface_attach(surface, surface->pending.buffer);
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002115 weston_surface_set_size_from_buffer(surface);
2116 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002117
2118 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002119 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002120 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002121
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002122 if (surface->pending.buffer)
2123 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2124 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002125 surface->pending.sx = 0;
2126 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002127 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002128
2129 /* wl_surface.damage */
2130 pixman_region32_union(&surface->damage, &surface->damage,
2131 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002132 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2133 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002134 surface->width,
2135 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002136 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002137
2138 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002139 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002140 surface->width,
2141 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002142 pixman_region32_intersect(&opaque,
2143 &opaque, &surface->pending.opaque);
2144
2145 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2146 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002147 wl_list_for_each(view, &surface->views, surface_link)
2148 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002149 }
2150
2151 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002152
2153 /* wl_surface.set_input_region */
2154 pixman_region32_fini(&surface->input);
2155 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002156 surface->width,
2157 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002158 pixman_region32_intersect(&surface->input,
2159 &surface->input, &surface->pending.input);
2160
Pekka Paalanenbc106382012-10-10 12:49:31 +03002161 /* wl_surface.frame */
2162 wl_list_insert_list(&surface->frame_callback_list,
2163 &surface->pending.frame_callback_list);
2164 wl_list_init(&surface->pending.frame_callback_list);
2165
Pekka Paalanene67858b2013-04-25 13:57:42 +03002166 weston_surface_commit_subsurface_order(surface);
2167
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002168 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002169}
2170
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002171static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002172weston_subsurface_commit(struct weston_subsurface *sub);
2173
2174static void
2175weston_subsurface_parent_commit(struct weston_subsurface *sub,
2176 int parent_is_synchronized);
2177
2178static void
2179surface_commit(struct wl_client *client, struct wl_resource *resource)
2180{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002181 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2183
2184 if (sub) {
2185 weston_subsurface_commit(sub);
2186 return;
2187 }
2188
2189 weston_surface_commit(surface);
2190
2191 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2192 if (sub->surface != surface)
2193 weston_subsurface_parent_commit(sub, 0);
2194 }
2195}
2196
2197static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002198surface_set_buffer_transform(struct wl_client *client,
2199 struct wl_resource *resource, int transform)
2200{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002201 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002202
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002203 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002204}
2205
Alexander Larsson4ea95522013-05-22 14:41:37 +02002206static void
2207surface_set_buffer_scale(struct wl_client *client,
2208 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002209 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002210{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002211 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002212
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002213 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002214}
2215
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002216static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002217 surface_destroy,
2218 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002219 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002220 surface_frame,
2221 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002222 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002223 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002224 surface_set_buffer_transform,
2225 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002226};
2227
2228static void
2229compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002230 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002231{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002232 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002233 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002234
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002235 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002236 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002237 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002238 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002239 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002240
Jason Ekstranda85118c2013-06-27 20:17:02 -05002241 surface->resource =
2242 wl_resource_create(client, &wl_surface_interface,
2243 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002244 if (surface->resource == NULL) {
2245 weston_surface_destroy(surface);
2246 wl_resource_post_no_memory(resource);
2247 return;
2248 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002249 wl_resource_set_implementation(surface->resource, &surface_interface,
2250 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002251}
2252
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002253static void
2254destroy_region(struct wl_resource *resource)
2255{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002256 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002257
2258 pixman_region32_fini(&region->region);
2259 free(region);
2260}
2261
2262static void
2263region_destroy(struct wl_client *client, struct wl_resource *resource)
2264{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002265 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002266}
2267
2268static void
2269region_add(struct wl_client *client, struct wl_resource *resource,
2270 int32_t x, int32_t y, int32_t width, int32_t height)
2271{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002272 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002273
2274 pixman_region32_union_rect(&region->region, &region->region,
2275 x, y, width, height);
2276}
2277
2278static void
2279region_subtract(struct wl_client *client, struct wl_resource *resource,
2280 int32_t x, int32_t y, int32_t width, int32_t height)
2281{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002282 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002283 pixman_region32_t rect;
2284
2285 pixman_region32_init_rect(&rect, x, y, width, height);
2286 pixman_region32_subtract(&region->region, &region->region, &rect);
2287 pixman_region32_fini(&rect);
2288}
2289
2290static const struct wl_region_interface region_interface = {
2291 region_destroy,
2292 region_add,
2293 region_subtract
2294};
2295
2296static void
2297compositor_create_region(struct wl_client *client,
2298 struct wl_resource *resource, uint32_t id)
2299{
2300 struct weston_region *region;
2301
2302 region = malloc(sizeof *region);
2303 if (region == NULL) {
2304 wl_resource_post_no_memory(resource);
2305 return;
2306 }
2307
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002308 pixman_region32_init(&region->region);
2309
Jason Ekstranda85118c2013-06-27 20:17:02 -05002310 region->resource =
2311 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002312 if (region->resource == NULL) {
2313 free(region);
2314 wl_resource_post_no_memory(resource);
2315 return;
2316 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002317 wl_resource_set_implementation(region->resource, &region_interface,
2318 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002319}
2320
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002321static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002322 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002323 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002324};
2325
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002326static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002327weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2328{
2329 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002330 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002331 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002332
Alexander Larsson4ea95522013-05-22 14:41:37 +02002333 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002334 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002335 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002336 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002337
Pekka Paalanene67858b2013-04-25 13:57:42 +03002338 /* wl_surface.attach */
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002339 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002340 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
Jason Ekstrand81038fb2014-01-11 12:12:19 -06002341 weston_surface_set_size_from_buffer(surface);
2342 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002343 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2344
Pekka Paalanene67858b2013-04-25 13:57:42 +03002345 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002346 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002347 sub->cached.sx = 0;
2348 sub->cached.sy = 0;
2349 sub->cached.newly_attached = 0;
2350
2351 /* wl_surface.damage */
2352 pixman_region32_union(&surface->damage, &surface->damage,
2353 &sub->cached.damage);
2354 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2355 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002356 surface->width,
2357 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002358 empty_region(&sub->cached.damage);
2359
2360 /* wl_surface.set_opaque_region */
2361 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002362 surface->width,
2363 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002364 pixman_region32_intersect(&opaque,
2365 &opaque, &sub->cached.opaque);
2366
2367 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2368 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002369 wl_list_for_each(view, &surface->views, surface_link)
2370 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002371 }
2372
2373 pixman_region32_fini(&opaque);
2374
2375 /* wl_surface.set_input_region */
2376 pixman_region32_fini(&surface->input);
2377 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 surface->width,
2379 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002380 pixman_region32_intersect(&surface->input,
2381 &surface->input, &sub->cached.input);
2382
2383 /* wl_surface.frame */
2384 wl_list_insert_list(&surface->frame_callback_list,
2385 &sub->cached.frame_callback_list);
2386 wl_list_init(&sub->cached.frame_callback_list);
2387
2388 weston_surface_commit_subsurface_order(surface);
2389
2390 weston_surface_schedule_repaint(surface);
2391
2392 sub->cached.has_data = 0;
2393}
2394
2395static void
2396weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2397{
2398 struct weston_surface *surface = sub->surface;
2399
2400 /*
2401 * If this commit would cause the surface to move by the
2402 * attach(dx, dy) parameters, the old damage region must be
2403 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002404 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002405 */
2406 pixman_region32_translate(&sub->cached.damage,
2407 -surface->pending.sx, -surface->pending.sy);
2408 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2409 &surface->pending.damage);
2410 empty_region(&surface->pending.damage);
2411
2412 if (surface->pending.newly_attached) {
2413 sub->cached.newly_attached = 1;
2414 weston_buffer_reference(&sub->cached.buffer_ref,
2415 surface->pending.buffer);
2416 }
2417 sub->cached.sx += surface->pending.sx;
2418 sub->cached.sy += surface->pending.sy;
2419 surface->pending.sx = 0;
2420 surface->pending.sy = 0;
2421 surface->pending.newly_attached = 0;
2422
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002423 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002424
2425 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2426
2427 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2428
2429 wl_list_insert_list(&sub->cached.frame_callback_list,
2430 &surface->pending.frame_callback_list);
2431 wl_list_init(&surface->pending.frame_callback_list);
2432
2433 sub->cached.has_data = 1;
2434}
2435
2436static int
2437weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2438{
2439 while (sub) {
2440 if (sub->synchronized)
2441 return 1;
2442
2443 if (!sub->parent)
2444 return 0;
2445
2446 sub = weston_surface_to_subsurface(sub->parent);
2447 }
2448
2449 return 0;
2450}
2451
2452static void
2453weston_subsurface_commit(struct weston_subsurface *sub)
2454{
2455 struct weston_surface *surface = sub->surface;
2456 struct weston_subsurface *tmp;
2457
2458 /* Recursive check for effectively synchronized. */
2459 if (weston_subsurface_is_synchronized(sub)) {
2460 weston_subsurface_commit_to_cache(sub);
2461 } else {
2462 if (sub->cached.has_data) {
2463 /* flush accumulated state from cache */
2464 weston_subsurface_commit_to_cache(sub);
2465 weston_subsurface_commit_from_cache(sub);
2466 } else {
2467 weston_surface_commit(surface);
2468 }
2469
2470 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2471 if (tmp->surface != surface)
2472 weston_subsurface_parent_commit(tmp, 0);
2473 }
2474 }
2475}
2476
2477static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002478weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002479{
2480 struct weston_surface *surface = sub->surface;
2481 struct weston_subsurface *tmp;
2482
Pekka Paalanene67858b2013-04-25 13:57:42 +03002483 /* From now on, commit_from_cache the whole sub-tree, regardless of
2484 * the synchronized mode of each child. This sub-surface or some
2485 * of its ancestors were synchronized, so we are synchronized
2486 * all the way down.
2487 */
2488
2489 if (sub->cached.has_data)
2490 weston_subsurface_commit_from_cache(sub);
2491
2492 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2493 if (tmp->surface != surface)
2494 weston_subsurface_parent_commit(tmp, 1);
2495 }
2496}
2497
2498static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002499weston_subsurface_parent_commit(struct weston_subsurface *sub,
2500 int parent_is_synchronized)
2501{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002502 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002503 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002504 wl_list_for_each(view, &sub->surface->views, surface_link)
2505 weston_view_set_position(view,
2506 sub->position.x,
2507 sub->position.y);
2508
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002509 sub->position.set = 0;
2510 }
2511
2512 if (parent_is_synchronized || sub->synchronized)
2513 weston_subsurface_synchronized_commit(sub);
2514}
2515
2516static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002517subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002518{
2519 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002520 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002521
Jason Ekstranda7af7042013-10-12 22:38:11 -05002522 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002523 weston_view_set_position(view,
2524 view->geometry.x + dx,
2525 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002526
2527 /* No need to check parent mappedness, because if parent is not
2528 * mapped, parent is not in a visible layer, so this sub-surface
2529 * will not be drawn either.
2530 */
2531 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002532 /* Cannot call weston_surface_update_transform(),
2533 * because that would call it also for the parent surface,
2534 * which might not be mapped yet. That would lead to
2535 * inconsistent state, where the window could never be
2536 * mapped.
2537 *
2538 * Instead just assing any output, to make
2539 * weston_surface_is_mapped() return true, so that when the
2540 * parent surface does get mapped, this one will get
2541 * included, too. See surface_list_add().
2542 */
2543 assert(!wl_list_empty(&compositor->output_list));
2544 surface->output = container_of(compositor->output_list.next,
2545 struct weston_output, link);
2546 }
2547}
2548
2549static struct weston_subsurface *
2550weston_surface_to_subsurface(struct weston_surface *surface)
2551{
2552 if (surface->configure == subsurface_configure)
2553 return surface->configure_private;
2554
2555 return NULL;
2556}
2557
Pekka Paalanen01388e22013-04-25 13:57:44 +03002558WL_EXPORT struct weston_surface *
2559weston_surface_get_main_surface(struct weston_surface *surface)
2560{
2561 struct weston_subsurface *sub;
2562
2563 while (surface && (sub = weston_surface_to_subsurface(surface)))
2564 surface = sub->parent;
2565
2566 return surface;
2567}
2568
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569static void
2570subsurface_set_position(struct wl_client *client,
2571 struct wl_resource *resource, int32_t x, int32_t y)
2572{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002573 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002574
2575 if (!sub)
2576 return;
2577
2578 sub->position.x = x;
2579 sub->position.y = y;
2580 sub->position.set = 1;
2581}
2582
2583static struct weston_subsurface *
2584subsurface_from_surface(struct weston_surface *surface)
2585{
2586 struct weston_subsurface *sub;
2587
2588 sub = weston_surface_to_subsurface(surface);
2589 if (sub)
2590 return sub;
2591
2592 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2593 if (sub->surface == surface)
2594 return sub;
2595
2596 return NULL;
2597}
2598
2599static struct weston_subsurface *
2600subsurface_sibling_check(struct weston_subsurface *sub,
2601 struct weston_surface *surface,
2602 const char *request)
2603{
2604 struct weston_subsurface *sibling;
2605
2606 sibling = subsurface_from_surface(surface);
2607
2608 if (!sibling) {
2609 wl_resource_post_error(sub->resource,
2610 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2611 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002612 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613 return NULL;
2614 }
2615
2616 if (sibling->parent != sub->parent) {
2617 wl_resource_post_error(sub->resource,
2618 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2619 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002620 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002621 return NULL;
2622 }
2623
2624 return sibling;
2625}
2626
2627static void
2628subsurface_place_above(struct wl_client *client,
2629 struct wl_resource *resource,
2630 struct wl_resource *sibling_resource)
2631{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002632 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002633 struct weston_surface *surface =
2634 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002635 struct weston_subsurface *sibling;
2636
2637 if (!sub)
2638 return;
2639
2640 sibling = subsurface_sibling_check(sub, surface, "place_above");
2641 if (!sibling)
2642 return;
2643
2644 wl_list_remove(&sub->parent_link_pending);
2645 wl_list_insert(sibling->parent_link_pending.prev,
2646 &sub->parent_link_pending);
2647}
2648
2649static void
2650subsurface_place_below(struct wl_client *client,
2651 struct wl_resource *resource,
2652 struct wl_resource *sibling_resource)
2653{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002654 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002655 struct weston_surface *surface =
2656 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002657 struct weston_subsurface *sibling;
2658
2659 if (!sub)
2660 return;
2661
2662 sibling = subsurface_sibling_check(sub, surface, "place_below");
2663 if (!sibling)
2664 return;
2665
2666 wl_list_remove(&sub->parent_link_pending);
2667 wl_list_insert(&sibling->parent_link_pending,
2668 &sub->parent_link_pending);
2669}
2670
2671static void
2672subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2673{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002674 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002675
2676 if (sub)
2677 sub->synchronized = 1;
2678}
2679
2680static void
2681subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2682{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002683 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002684
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002685 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002686 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002687
2688 /* If sub became effectively desynchronized, flush. */
2689 if (!weston_subsurface_is_synchronized(sub))
2690 weston_subsurface_synchronized_commit(sub);
2691 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692}
2693
2694static void
2695weston_subsurface_cache_init(struct weston_subsurface *sub)
2696{
2697 pixman_region32_init(&sub->cached.damage);
2698 pixman_region32_init(&sub->cached.opaque);
2699 pixman_region32_init(&sub->cached.input);
2700 wl_list_init(&sub->cached.frame_callback_list);
2701 sub->cached.buffer_ref.buffer = NULL;
2702}
2703
2704static void
2705weston_subsurface_cache_fini(struct weston_subsurface *sub)
2706{
2707 struct weston_frame_callback *cb, *tmp;
2708
2709 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002710 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002711
2712 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2713 pixman_region32_fini(&sub->cached.damage);
2714 pixman_region32_fini(&sub->cached.opaque);
2715 pixman_region32_fini(&sub->cached.input);
2716}
2717
2718static void
2719weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2720{
2721 wl_list_remove(&sub->parent_link);
2722 wl_list_remove(&sub->parent_link_pending);
2723 wl_list_remove(&sub->parent_destroy_listener.link);
2724 sub->parent = NULL;
2725}
2726
2727static void
2728weston_subsurface_destroy(struct weston_subsurface *sub);
2729
2730static void
2731subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2732{
2733 struct weston_subsurface *sub =
2734 container_of(listener, struct weston_subsurface,
2735 surface_destroy_listener);
2736 assert(data == &sub->surface->resource);
2737
2738 /* The protocol object (wl_resource) is left inert. */
2739 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002740 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002741
2742 weston_subsurface_destroy(sub);
2743}
2744
2745static void
2746subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2747{
2748 struct weston_subsurface *sub =
2749 container_of(listener, struct weston_subsurface,
2750 parent_destroy_listener);
2751 assert(data == &sub->parent->resource);
2752 assert(sub->surface != sub->parent);
2753
2754 if (weston_surface_is_mapped(sub->surface))
2755 weston_surface_unmap(sub->surface);
2756
2757 weston_subsurface_unlink_parent(sub);
2758}
2759
2760static void
2761subsurface_resource_destroy(struct wl_resource *resource)
2762{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002763 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764
2765 if (sub)
2766 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767}
2768
2769static void
2770subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2771{
2772 wl_resource_destroy(resource);
2773}
2774
2775static void
2776weston_subsurface_link_parent(struct weston_subsurface *sub,
2777 struct weston_surface *parent)
2778{
2779 sub->parent = parent;
2780 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002781 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 &sub->parent_destroy_listener);
2783
2784 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2785 wl_list_insert(&parent->subsurface_list_pending,
2786 &sub->parent_link_pending);
2787}
2788
2789static void
2790weston_subsurface_link_surface(struct weston_subsurface *sub,
2791 struct weston_surface *surface)
2792{
2793 sub->surface = surface;
2794 sub->surface_destroy_listener.notify =
2795 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002796 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002797 &sub->surface_destroy_listener);
2798}
2799
2800static void
2801weston_subsurface_destroy(struct weston_subsurface *sub)
2802{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002803 struct weston_view *view, *next;
2804
Pekka Paalanene67858b2013-04-25 13:57:42 +03002805 assert(sub->surface);
2806
2807 if (sub->resource) {
2808 assert(weston_surface_to_subsurface(sub->surface) == sub);
2809 assert(sub->parent_destroy_listener.notify ==
2810 subsurface_handle_parent_destroy);
2811
Jason Ekstranda7af7042013-10-12 22:38:11 -05002812 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2813 weston_view_destroy(view);
2814
Pekka Paalanene67858b2013-04-25 13:57:42 +03002815 if (sub->parent)
2816 weston_subsurface_unlink_parent(sub);
2817
2818 weston_subsurface_cache_fini(sub);
2819
2820 sub->surface->configure = NULL;
2821 sub->surface->configure_private = NULL;
2822 } else {
2823 /* the dummy weston_subsurface for the parent itself */
2824 assert(sub->parent_destroy_listener.notify == NULL);
2825 wl_list_remove(&sub->parent_link);
2826 wl_list_remove(&sub->parent_link_pending);
2827 }
2828
2829 wl_list_remove(&sub->surface_destroy_listener.link);
2830 free(sub);
2831}
2832
2833static const struct wl_subsurface_interface subsurface_implementation = {
2834 subsurface_destroy,
2835 subsurface_set_position,
2836 subsurface_place_above,
2837 subsurface_place_below,
2838 subsurface_set_sync,
2839 subsurface_set_desync
2840};
2841
2842static struct weston_subsurface *
2843weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2844 struct weston_surface *parent)
2845{
2846 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002847 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002848
2849 sub = calloc(1, sizeof *sub);
2850 if (!sub)
2851 return NULL;
2852
Jason Ekstranda7af7042013-10-12 22:38:11 -05002853 wl_list_init(&sub->unused_views);
2854
Jason Ekstranda85118c2013-06-27 20:17:02 -05002855 sub->resource =
2856 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002857 if (!sub->resource) {
2858 free(sub);
2859 return NULL;
2860 }
2861
Jason Ekstranda85118c2013-06-27 20:17:02 -05002862 wl_resource_set_implementation(sub->resource,
2863 &subsurface_implementation,
2864 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002865 weston_subsurface_link_surface(sub, surface);
2866 weston_subsurface_link_parent(sub, parent);
2867 weston_subsurface_cache_init(sub);
2868 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002869
2870 return sub;
2871}
2872
2873/* Create a dummy subsurface for having the parent itself in its
2874 * sub-surface lists. Makes stacking order manipulation easy.
2875 */
2876static struct weston_subsurface *
2877weston_subsurface_create_for_parent(struct weston_surface *parent)
2878{
2879 struct weston_subsurface *sub;
2880
2881 sub = calloc(1, sizeof *sub);
2882 if (!sub)
2883 return NULL;
2884
2885 weston_subsurface_link_surface(sub, parent);
2886 sub->parent = parent;
2887 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2888 wl_list_insert(&parent->subsurface_list_pending,
2889 &sub->parent_link_pending);
2890
2891 return sub;
2892}
2893
2894static void
2895subcompositor_get_subsurface(struct wl_client *client,
2896 struct wl_resource *resource,
2897 uint32_t id,
2898 struct wl_resource *surface_resource,
2899 struct wl_resource *parent_resource)
2900{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002901 struct weston_surface *surface =
2902 wl_resource_get_user_data(surface_resource);
2903 struct weston_surface *parent =
2904 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002905 struct weston_subsurface *sub;
2906 static const char where[] = "get_subsurface: wl_subsurface@";
2907
2908 if (surface == parent) {
2909 wl_resource_post_error(resource,
2910 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2911 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002912 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002913 return;
2914 }
2915
2916 if (weston_surface_to_subsurface(surface)) {
2917 wl_resource_post_error(resource,
2918 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2919 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002920 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002921 return;
2922 }
2923
2924 if (surface->configure) {
2925 wl_resource_post_error(resource,
2926 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2927 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002928 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929 return;
2930 }
2931
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002932 if (weston_surface_get_main_surface(parent) == surface) {
2933 wl_resource_post_error(resource,
2934 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2935 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002936 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002937 return;
2938 }
2939
Pekka Paalanene67858b2013-04-25 13:57:42 +03002940 /* make sure the parent is in its own list */
2941 if (wl_list_empty(&parent->subsurface_list)) {
2942 if (!weston_subsurface_create_for_parent(parent)) {
2943 wl_resource_post_no_memory(resource);
2944 return;
2945 }
2946 }
2947
2948 sub = weston_subsurface_create(id, surface, parent);
2949 if (!sub) {
2950 wl_resource_post_no_memory(resource);
2951 return;
2952 }
2953
2954 surface->configure = subsurface_configure;
2955 surface->configure_private = sub;
2956}
2957
2958static void
2959subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2960{
2961 wl_resource_destroy(resource);
2962}
2963
2964static const struct wl_subcompositor_interface subcompositor_interface = {
2965 subcompositor_destroy,
2966 subcompositor_get_subsurface
2967};
2968
2969static void
2970bind_subcompositor(struct wl_client *client,
2971 void *data, uint32_t version, uint32_t id)
2972{
2973 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002974 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002975
Jason Ekstranda85118c2013-06-27 20:17:02 -05002976 resource =
2977 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002978 if (resource == NULL) {
2979 wl_client_post_no_memory(client);
2980 return;
2981 }
2982 wl_resource_set_implementation(resource, &subcompositor_interface,
2983 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002984}
2985
2986static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002987weston_compositor_dpms(struct weston_compositor *compositor,
2988 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002989{
2990 struct weston_output *output;
2991
2992 wl_list_for_each(output, &compositor->output_list, link)
2993 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002994 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002995}
2996
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002997WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002998weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002999{
Neil Roberts8b62e202013-09-30 13:14:47 +01003000 uint32_t old_state = compositor->state;
3001
3002 /* The state needs to be changed before emitting the wake
3003 * signal because that may try to schedule a repaint which
3004 * will not work if the compositor is still sleeping */
3005 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3006
3007 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003008 case WESTON_COMPOSITOR_SLEEPING:
3009 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3010 /* fall through */
3011 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003012 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003013 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003014 /* fall through */
3015 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003016 wl_event_source_timer_update(compositor->idle_source,
3017 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003018 }
3019}
3020
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003021WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003022weston_compositor_offscreen(struct weston_compositor *compositor)
3023{
3024 switch (compositor->state) {
3025 case WESTON_COMPOSITOR_OFFSCREEN:
3026 return;
3027 case WESTON_COMPOSITOR_SLEEPING:
3028 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3029 /* fall through */
3030 default:
3031 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3032 wl_event_source_timer_update(compositor->idle_source, 0);
3033 }
3034}
3035
3036WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003037weston_compositor_sleep(struct weston_compositor *compositor)
3038{
3039 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3040 return;
3041
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003042 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003043 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3044 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3045}
3046
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003047static int
3048idle_handler(void *data)
3049{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003050 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003051
3052 if (compositor->idle_inhibit)
3053 return 1;
3054
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003055 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003056 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003057
3058 return 1;
3059}
3060
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003061WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003062weston_plane_init(struct weston_plane *plane,
3063 struct weston_compositor *ec,
3064 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003065{
3066 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003067 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003068 plane->x = x;
3069 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003070 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003071
3072 /* Init the link so that the call to wl_list_remove() when releasing
3073 * the plane without ever stacking doesn't lead to a crash */
3074 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003075}
3076
3077WL_EXPORT void
3078weston_plane_release(struct weston_plane *plane)
3079{
Xiong Zhang97116532013-10-23 13:58:31 +08003080 struct weston_view *view;
3081
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003082 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003083 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003084
Xiong Zhang97116532013-10-23 13:58:31 +08003085 wl_list_for_each(view, &plane->compositor->view_list, link) {
3086 if (view->plane == plane)
3087 view->plane = NULL;
3088 }
3089
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003090 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003091}
3092
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003093WL_EXPORT void
3094weston_compositor_stack_plane(struct weston_compositor *ec,
3095 struct weston_plane *plane,
3096 struct weston_plane *above)
3097{
3098 if (above)
3099 wl_list_insert(above->link.prev, &plane->link);
3100 else
3101 wl_list_insert(&ec->plane_list, &plane->link);
3102}
3103
Casey Dahlin9074db52012-04-19 22:50:09 -04003104static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003105{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003106 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003107}
3108
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003109static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003110bind_output(struct wl_client *client,
3111 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003112{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003113 struct weston_output *output = data;
3114 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003115 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003116
Jason Ekstranda85118c2013-06-27 20:17:02 -05003117 resource = wl_resource_create(client, &wl_output_interface,
3118 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003119 if (resource == NULL) {
3120 wl_client_post_no_memory(client);
3121 return;
3122 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003123
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003124 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003125 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003126
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003127 wl_output_send_geometry(resource,
3128 output->x,
3129 output->y,
3130 output->mm_width,
3131 output->mm_height,
3132 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003133 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003134 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003135 if (version >= 2)
3136 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003137 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003138
3139 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003140 wl_output_send_mode(resource,
3141 mode->flags,
3142 mode->width,
3143 mode->height,
3144 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003145 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003146
3147 if (version >= 2)
3148 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003149}
3150
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003151/* Move other outputs when one is removed so the space remains contiguos. */
3152static void
3153weston_compositor_remove_output(struct weston_compositor *compositor,
3154 struct weston_output *remove_output)
3155{
3156 struct weston_output *output;
3157 int offset = 0;
3158
3159 wl_list_for_each(output, &compositor->output_list, link) {
3160 if (output == remove_output) {
3161 offset = output->width;
3162 continue;
3163 }
3164
3165 if (offset > 0) {
3166 weston_output_move(output,
3167 output->x - offset, output->y);
3168 output->dirty = 1;
3169 }
3170 }
3171}
3172
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003173WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003174weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003175{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003176 output->destroying = 1;
3177
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003178 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003179 wl_list_remove(&output->link);
3180
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003181 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003182 wl_signal_emit(&output->destroy_signal, output);
3183
Richard Hughesafe690c2013-05-02 10:10:04 +01003184 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003185 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003186 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003187 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003188
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003189 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003190}
3191
Scott Moreau1bad5db2012-08-18 01:04:05 -06003192static void
3193weston_output_compute_transform(struct weston_output *output)
3194{
3195 struct weston_matrix transform;
3196 int flip;
3197
3198 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003199 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003200
3201 switch(output->transform) {
3202 case WL_OUTPUT_TRANSFORM_FLIPPED:
3203 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3204 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3205 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003206 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003207 flip = -1;
3208 break;
3209 default:
3210 flip = 1;
3211 break;
3212 }
3213
3214 switch(output->transform) {
3215 case WL_OUTPUT_TRANSFORM_NORMAL:
3216 case WL_OUTPUT_TRANSFORM_FLIPPED:
3217 transform.d[0] = flip;
3218 transform.d[1] = 0;
3219 transform.d[4] = 0;
3220 transform.d[5] = 1;
3221 break;
3222 case WL_OUTPUT_TRANSFORM_90:
3223 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3224 transform.d[0] = 0;
3225 transform.d[1] = -flip;
3226 transform.d[4] = 1;
3227 transform.d[5] = 0;
3228 break;
3229 case WL_OUTPUT_TRANSFORM_180:
3230 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3231 transform.d[0] = -flip;
3232 transform.d[1] = 0;
3233 transform.d[4] = 0;
3234 transform.d[5] = -1;
3235 break;
3236 case WL_OUTPUT_TRANSFORM_270:
3237 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3238 transform.d[0] = 0;
3239 transform.d[1] = flip;
3240 transform.d[4] = -1;
3241 transform.d[5] = 0;
3242 break;
3243 default:
3244 break;
3245 }
3246
3247 weston_matrix_multiply(&output->matrix, &transform);
3248}
3249
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003250WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003251weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003252{
Scott Moreau850ca422012-05-21 15:21:25 -06003253 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003254 struct weston_matrix camera;
3255 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003256
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003257 weston_matrix_init(&output->matrix);
3258 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003259 -(output->x + output->width / 2.0),
3260 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003261
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003262 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003263 2.0 / output->width,
3264 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003265
3266 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003267
Scott Moreauccbf29d2012-02-22 14:21:41 -07003268 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003269 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003270 weston_matrix_init(&camera);
3271 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003272 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003273 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003274 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003275 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003276 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003277 weston_matrix_multiply(&output->matrix, &modelview);
3278 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003279
Scott Moreauccbf29d2012-02-22 14:21:41 -07003280 output->dirty = 0;
3281}
3282
Scott Moreau1bad5db2012-08-18 01:04:05 -06003283static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003284weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003285{
3286 output->transform = transform;
3287
3288 switch (transform) {
3289 case WL_OUTPUT_TRANSFORM_90:
3290 case WL_OUTPUT_TRANSFORM_270:
3291 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3292 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3293 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003294 output->width = output->current_mode->height;
3295 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003296 break;
3297 case WL_OUTPUT_TRANSFORM_NORMAL:
3298 case WL_OUTPUT_TRANSFORM_180:
3299 case WL_OUTPUT_TRANSFORM_FLIPPED:
3300 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003301 output->width = output->current_mode->width;
3302 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003303 break;
3304 default:
3305 break;
3306 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003307
Hardening57388e42013-09-18 23:56:36 +02003308 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003309 output->width /= scale;
3310 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003311}
3312
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003313static void
3314weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003315{
3316 output->x = x;
3317 output->y = y;
3318
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003319 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003320 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003321 output->width,
3322 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003323}
3324
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003325WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003326weston_output_move(struct weston_output *output, int x, int y)
3327{
3328 pixman_region32_t old_region;
3329 struct wl_resource *resource;
3330
3331 output->move_x = x - output->x;
3332 output->move_y = y - output->y;
3333
3334 if (output->move_x == 0 && output->move_y == 0)
3335 return;
3336
3337 pixman_region32_init(&old_region);
3338 pixman_region32_copy(&old_region, &output->region);
3339
3340 weston_output_init_geometry(output, x, y);
3341
3342 output->dirty = 1;
3343
3344 /* Move views on this output. */
3345 wl_signal_emit(&output->move_signal, output);
3346
3347 /* Notify clients of the change for output position. */
3348 wl_resource_for_each(resource, &output->resource_list)
3349 wl_output_send_geometry(resource,
3350 output->x,
3351 output->y,
3352 output->mm_width,
3353 output->mm_height,
3354 output->subpixel,
3355 output->make,
3356 output->model,
3357 output->transform);
3358}
3359
3360WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003361weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003362 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003363 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003364{
3365 output->compositor = c;
3366 output->x = x;
3367 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003368 output->mm_width = mm_width;
3369 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003370 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003371 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003372
Alexander Larsson0b135062013-05-28 16:23:36 +02003373 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003374 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003375
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003376 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003377 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003378
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003379 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003380 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003381 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003382 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003383 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003384
Casey Dahlin58ba1372012-04-19 22:50:08 -04003385 output->id = ffs(~output->compositor->output_id_pool) - 1;
3386 output->compositor->output_id_pool |= 1 << output->id;
3387
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003388 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003389 wl_global_create(c->wl_display, &wl_output_interface, 2,
3390 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003391 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003392}
3393
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003394WL_EXPORT void
3395weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003396 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003397 wl_fixed_t *x, wl_fixed_t *y)
3398{
3399 wl_fixed_t tx, ty;
3400 wl_fixed_t width, height;
3401
Hardeningff39efa2013-09-18 23:56:35 +02003402 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3403 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003404
3405 switch(output->transform) {
3406 case WL_OUTPUT_TRANSFORM_NORMAL:
3407 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003408 tx = device_x;
3409 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003410 break;
3411 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003412 tx = device_y;
3413 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003414 break;
3415 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003416 tx = width - device_x;
3417 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003418 break;
3419 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003420 tx = width - device_y;
3421 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003422 break;
3423 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003424 tx = width - device_x;
3425 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003426 break;
3427 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003428 tx = width - device_y;
3429 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003430 break;
3431 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003432 tx = device_x;
3433 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003434 break;
3435 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003436 tx = device_y;
3437 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003438 break;
3439 }
3440
Hardeningff39efa2013-09-18 23:56:35 +02003441 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3442 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003443}
3444
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003445static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003446destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003447{
Jonny Lamb74130762013-11-26 18:19:46 +01003448 struct weston_surface *surface =
3449 wl_resource_get_user_data(resource);
3450
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003451 surface->viewport_resource = NULL;
3452 surface->pending.buffer_viewport.viewport_set = 0;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003453}
3454
3455static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003456viewport_destroy(struct wl_client *client,
3457 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003458{
3459 wl_resource_destroy(resource);
3460}
3461
3462static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003463viewport_set(struct wl_client *client,
3464 struct wl_resource *resource,
3465 wl_fixed_t src_x,
3466 wl_fixed_t src_y,
3467 wl_fixed_t src_width,
3468 wl_fixed_t src_height,
3469 int32_t dst_width,
3470 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003471{
Jonny Lamb74130762013-11-26 18:19:46 +01003472 struct weston_surface *surface =
3473 wl_resource_get_user_data(resource);
3474
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003475 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003476
Jonny Lamb8ae35902013-11-26 18:19:45 +01003477 if (wl_fixed_to_double(src_width) < 0 ||
3478 wl_fixed_to_double(src_height) < 0) {
3479 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003480 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003481 "source dimensions must be non-negative (%fx%f)",
3482 wl_fixed_to_double(src_width),
3483 wl_fixed_to_double(src_height));
3484 return;
3485 }
3486
3487 if (dst_width <= 0 || dst_height <= 0) {
3488 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003489 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003490 "destination dimensions must be positive (%dx%d)",
3491 dst_width, dst_height);
3492 return;
3493 }
3494
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003495 surface->pending.buffer_viewport.viewport_set = 1;
Jonny Lamb74130762013-11-26 18:19:46 +01003496
3497 surface->pending.buffer_viewport.src_x = src_x;
3498 surface->pending.buffer_viewport.src_y = src_y;
3499 surface->pending.buffer_viewport.src_width = src_width;
3500 surface->pending.buffer_viewport.src_height = src_height;
3501 surface->pending.buffer_viewport.dst_width = dst_width;
3502 surface->pending.buffer_viewport.dst_height = dst_height;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003503}
3504
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003505static const struct wl_viewport_interface viewport_interface = {
3506 viewport_destroy,
3507 viewport_set
Jonny Lamb8ae35902013-11-26 18:19:45 +01003508};
3509
3510static void
3511scaler_destroy(struct wl_client *client,
3512 struct wl_resource *resource)
3513{
3514 wl_resource_destroy(resource);
3515}
3516
3517static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003518scaler_get_viewport(struct wl_client *client,
3519 struct wl_resource *scaler,
3520 uint32_t id,
3521 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003522{
3523 struct weston_surface *surface = wl_resource_get_user_data(surface_resource);
3524 struct wl_resource *resource;
3525
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003526 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003527 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003528 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3529 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003530 return;
3531 }
3532
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003533 resource = wl_resource_create(client, &wl_viewport_interface,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003534 1, id);
3535 if (resource == NULL) {
3536 wl_client_post_no_memory(client);
3537 return;
3538 }
3539
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003540 wl_resource_set_implementation(resource, &viewport_interface,
3541 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003542
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003543 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003544}
3545
3546static const struct wl_scaler_interface scaler_interface = {
3547 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003548 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003549};
3550
3551static void
3552bind_scaler(struct wl_client *client,
3553 void *data, uint32_t version, uint32_t id)
3554{
3555 struct wl_resource *resource;
3556
3557 resource = wl_resource_create(client, &wl_scaler_interface,
3558 1, id);
3559 if (resource == NULL) {
3560 wl_client_post_no_memory(client);
3561 return;
3562 }
3563
3564 wl_resource_set_implementation(resource, &scaler_interface,
3565 NULL, NULL);
3566}
3567
3568static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003569compositor_bind(struct wl_client *client,
3570 void *data, uint32_t version, uint32_t id)
3571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003572 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003573 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003574
Jason Ekstranda85118c2013-06-27 20:17:02 -05003575 resource = wl_resource_create(client, &wl_compositor_interface,
3576 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003577 if (resource == NULL) {
3578 wl_client_post_no_memory(client);
3579 return;
3580 }
3581
3582 wl_resource_set_implementation(resource, &compositor_interface,
3583 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003584}
3585
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003586static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003587log_uname(void)
3588{
3589 struct utsname usys;
3590
3591 uname(&usys);
3592
3593 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3594 usys.version, usys.machine);
3595}
3596
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003597WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003598weston_environment_get_fd(const char *env)
3599{
3600 char *e, *end;
3601 int fd, flags;
3602
3603 e = getenv(env);
3604 if (!e)
3605 return -1;
3606 fd = strtol(e, &end, 0);
3607 if (*end != '\0')
3608 return -1;
3609
3610 flags = fcntl(fd, F_GETFD);
3611 if (flags == -1)
3612 return -1;
3613
3614 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3615 unsetenv(env);
3616
3617 return fd;
3618}
3619
3620WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003621weston_compositor_init(struct weston_compositor *ec,
3622 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003623 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003624 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003625{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003626 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003627 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003628 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003629
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003630 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003631 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003632 wl_signal_init(&ec->destroy_signal);
3633 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003634 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003635 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003636 wl_signal_init(&ec->idle_signal);
3637 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003638 wl_signal_init(&ec->show_input_panel_signal);
3639 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003640 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003641 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003642 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003643 wl_signal_init(&ec->output_destroyed_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003644 wl_signal_init(&ec->session_signal);
3645 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003646
Casey Dahlin58ba1372012-04-19 22:50:08 -04003647 ec->output_id_pool = 0;
3648
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003649 if (!wl_global_create(display, &wl_compositor_interface, 3,
3650 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003651 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003652
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003653 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3654 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003655 return -1;
3656
Jonny Lamb8ae35902013-11-26 18:19:45 +01003657 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 1,
3658 ec, bind_scaler))
3659 return -1;
3660
Jason Ekstranda7af7042013-10-12 22:38:11 -05003661 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003662 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003663 wl_list_init(&ec->layer_list);
3664 wl_list_init(&ec->seat_list);
3665 wl_list_init(&ec->output_list);
3666 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003667 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003668 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003669 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003670 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003671 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003672
Xiong Zhang97116532013-10-23 13:58:31 +08003673 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003674 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003675
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003676 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3677 weston_config_section_get_string(s, "keymap_rules",
3678 (char **) &xkb_names.rules, NULL);
3679 weston_config_section_get_string(s, "keymap_model",
3680 (char **) &xkb_names.model, NULL);
3681 weston_config_section_get_string(s, "keymap_layout",
3682 (char **) &xkb_names.layout, NULL);
3683 weston_config_section_get_string(s, "keymap_variant",
3684 (char **) &xkb_names.variant, NULL);
3685 weston_config_section_get_string(s, "keymap_options",
3686 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003687
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003688 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3689 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003690
3691 ec->ping_handler = NULL;
3692
3693 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003694 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003695
3696 wl_data_device_manager_init(ec->wl_display);
3697
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003698 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003699
Daniel Stone725c2c32012-06-22 14:04:36 +01003700 loop = wl_display_get_event_loop(ec->wl_display);
3701 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3702 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3703
3704 ec->input_loop = wl_event_loop_create();
3705
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003706 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3707 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3708
3709 weston_compositor_schedule_repaint(ec);
3710
Daniel Stone725c2c32012-06-22 14:04:36 +01003711 return 0;
3712}
3713
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003714WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003715weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003716{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003717 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003718
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003719 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003720 if (ec->input_loop_source)
3721 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003722
Matt Roper361d2ad2011-08-29 13:52:23 -07003723 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003724 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003725 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003726
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003727 if (ec->renderer)
3728 ec->renderer->destroy(ec);
3729
Daniel Stone325fc2d2012-05-30 16:31:58 +01003730 weston_binding_list_destroy_all(&ec->key_binding_list);
3731 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003732 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003733 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003734 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003735
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003736 weston_plane_release(&ec->primary_plane);
3737
Jonas Ådahlc97af922012-03-28 22:36:09 +02003738 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003739
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003740 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003741}
3742
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003743WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003744weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3745 const struct weston_pointer_grab_interface *interface)
3746{
3747 struct weston_seat *seat;
3748
3749 ec->default_pointer_grab = interface;
3750 wl_list_for_each(seat, &ec->seat_list, link) {
3751 if (seat->pointer) {
3752 weston_pointer_set_default_grab(seat->pointer,
3753 interface);
3754 }
3755 }
3756}
3757
3758WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003759weston_version(int *major, int *minor, int *micro)
3760{
3761 *major = WESTON_VERSION_MAJOR;
3762 *minor = WESTON_VERSION_MINOR;
3763 *micro = WESTON_VERSION_MICRO;
3764}
3765
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003766static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003767 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003768 const char *desc;
3769} capability_strings[] = {
3770 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003771 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003772};
3773
3774static void
3775weston_compositor_log_capabilities(struct weston_compositor *compositor)
3776{
3777 unsigned i;
3778 int yes;
3779
3780 weston_log("Compositor capabilities:\n");
3781 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3782 yes = compositor->capabilities & capability_strings[i].bit;
3783 weston_log_continue(STAMP_SPACE "%s %s\n",
3784 capability_strings[i].desc,
3785 yes ? "yes" : "no");
3786 }
3787}
3788
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003789static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003790{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003791 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003792
Martin Minarik6d118362012-06-07 18:01:59 +02003793 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003794 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003795
3796 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003797}
3798
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003799#ifdef HAVE_LIBUNWIND
3800
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003801static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003802print_backtrace(void)
3803{
3804 unw_cursor_t cursor;
3805 unw_context_t context;
3806 unw_word_t off;
3807 unw_proc_info_t pip;
3808 int ret, i = 0;
3809 char procname[256];
3810 const char *filename;
3811 Dl_info dlinfo;
3812
3813 pip.unwind_info = NULL;
3814 ret = unw_getcontext(&context);
3815 if (ret) {
3816 weston_log("unw_getcontext: %d\n", ret);
3817 return;
3818 }
3819
3820 ret = unw_init_local(&cursor, &context);
3821 if (ret) {
3822 weston_log("unw_init_local: %d\n", ret);
3823 return;
3824 }
3825
3826 ret = unw_step(&cursor);
3827 while (ret > 0) {
3828 ret = unw_get_proc_info(&cursor, &pip);
3829 if (ret) {
3830 weston_log("unw_get_proc_info: %d\n", ret);
3831 break;
3832 }
3833
3834 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3835 if (ret && ret != -UNW_ENOMEM) {
3836 if (ret != -UNW_EUNSPEC)
3837 weston_log("unw_get_proc_name: %d\n", ret);
3838 procname[0] = '?';
3839 procname[1] = 0;
3840 }
3841
3842 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3843 *dlinfo.dli_fname)
3844 filename = dlinfo.dli_fname;
3845 else
3846 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003847
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003848 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3849 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3850
3851 ret = unw_step(&cursor);
3852 if (ret < 0)
3853 weston_log("unw_step: %d\n", ret);
3854 }
3855}
3856
3857#else
3858
3859static void
3860print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003861{
3862 void *buffer[32];
3863 int i, count;
3864 Dl_info info;
3865
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003866 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3867 for (i = 0; i < count; i++) {
3868 dladdr(buffer[i], &info);
3869 weston_log(" [%016lx] %s (%s)\n",
3870 (long) buffer[i],
3871 info.dli_sname ? info.dli_sname : "--",
3872 info.dli_fname);
3873 }
3874}
3875
3876#endif
3877
3878static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003879on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003880{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003881 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003882 * then call the backend restore function, which will switch
3883 * back to the vt we launched from or ungrab X etc and then
3884 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003885 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003886 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003887 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003888
Peter Maatmane5b42e42013-03-27 22:38:53 +01003889 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003890
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003891 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003892
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003893 segv_compositor->restore(segv_compositor);
3894
3895 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003896}
3897
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003898WL_EXPORT void *
3899weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003900{
3901 char path[PATH_MAX];
3902 void *module, *init;
3903
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003904 if (name == NULL)
3905 return NULL;
3906
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003907 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003908 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003909 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003910 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003911
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003912 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3913 if (module) {
3914 weston_log("Module '%s' already loaded\n", path);
3915 dlclose(module);
3916 return NULL;
3917 }
3918
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003919 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003920 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003921 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003922 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003923 return NULL;
3924 }
3925
3926 init = dlsym(module, entrypoint);
3927 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003928 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003929 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003930 return NULL;
3931 }
3932
3933 return init;
3934}
3935
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003936static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003937load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003938 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003939{
3940 const char *p, *end;
3941 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003942 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003943 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003944
3945 if (modules == NULL)
3946 return 0;
3947
3948 p = modules;
3949 while (*p) {
3950 end = strchrnul(p, ',');
3951 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003952 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003953 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003954 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003955 p = end;
3956 while (*p == ',')
3957 p++;
3958
3959 }
3960
3961 return 0;
3962}
3963
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003964static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003965 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3966
3967static const char xdg_wrong_message[] =
3968 "fatal: environment variable XDG_RUNTIME_DIR\n"
3969 "is set to \"%s\", which is not a directory.\n";
3970
3971static const char xdg_wrong_mode_message[] =
3972 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01003973 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
3974 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003975
3976static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003977 "Refer to your distribution on how to get it, or\n"
3978 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3979 "on how to implement it.\n";
3980
Martin Minarik37032f82012-06-18 20:15:18 +02003981static void
3982verify_xdg_runtime_dir(void)
3983{
3984 char *dir = getenv("XDG_RUNTIME_DIR");
3985 struct stat s;
3986
3987 if (!dir) {
3988 weston_log(xdg_error_message);
3989 weston_log_continue(xdg_detail_message);
3990 exit(EXIT_FAILURE);
3991 }
3992
3993 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3994 weston_log(xdg_wrong_message, dir);
3995 weston_log_continue(xdg_detail_message);
3996 exit(EXIT_FAILURE);
3997 }
3998
3999 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4000 weston_log(xdg_wrong_mode_message,
4001 dir, s.st_mode & 0777, s.st_uid);
4002 weston_log_continue(xdg_detail_message);
4003 }
4004}
4005
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004006static int
4007usage(int error_code)
4008{
4009 fprintf(stderr,
4010 "Usage: weston [OPTIONS]\n\n"
4011 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4012 "Weston supports multiple backends, and depending on which backend is in use\n"
4013 "different options will be accepted.\n\n"
4014
4015
4016 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004017 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004018 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004019 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4020 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004021 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004022 " -S, --socket=NAME\tName of socket to listen on\n"
4023 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004024 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004025 " --log==FILE\t\tLog to the given file\n"
4026 " -h, --help\t\tThis help message\n\n");
4027
4028 fprintf(stderr,
4029 "Options for drm-backend.so:\n\n"
4030 " --connector=ID\tBring up only this connector\n"
4031 " --seat=SEAT\t\tThe seat that weston should run on\n"
4032 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004033 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004034 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4035
4036 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004037 "Options for fbdev-backend.so:\n\n"
4038 " --tty=TTY\t\tThe tty to use\n"
4039 " --device=DEVICE\tThe framebuffer device to use\n\n");
4040
4041 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004042 "Options for x11-backend.so:\n\n"
4043 " --width=WIDTH\t\tWidth of X window\n"
4044 " --height=HEIGHT\tHeight of X window\n"
4045 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004046 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004047 " --output-count=COUNT\tCreate multiple outputs\n"
4048 " --no-input\t\tDont create input devices\n\n");
4049
4050 fprintf(stderr,
4051 "Options for wayland-backend.so:\n\n"
4052 " --width=WIDTH\t\tWidth of Wayland surface\n"
4053 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004054 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004055 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004056 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004057 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004058 " --display=DISPLAY\tWayland display to connect to\n\n");
4059
Pekka Paalanene31e0532013-05-22 18:03:07 +03004060#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4061 fprintf(stderr,
4062 "Options for rpi-backend.so:\n\n"
4063 " --tty=TTY\t\tThe tty to use\n"
4064 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4065 " --transform=TR\tThe output transformation, TR is one of:\n"
4066 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004067 " --opaque-regions\tEnable support for opaque regions, can be "
4068 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004069 "\n");
4070#endif
4071
Hardeningc077a842013-07-08 00:51:35 +02004072#if defined(BUILD_RDP_COMPOSITOR)
4073 fprintf(stderr,
4074 "Options for rdp-backend.so:\n\n"
4075 " --width=WIDTH\t\tWidth of desktop\n"
4076 " --height=HEIGHT\tHeight of desktop\n"
4077 " --extra-modes=MODES\t\n"
4078 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4079 " --address=ADDR\tThe address to bind\n"
4080 " --port=PORT\tThe port to listen on\n"
4081 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4082 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4083 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4084 "\n");
4085#endif
4086
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004087 exit(error_code);
4088}
4089
Peter Maatmane5b42e42013-03-27 22:38:53 +01004090static void
4091catch_signals(void)
4092{
4093 struct sigaction action;
4094
4095 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4096 action.sa_sigaction = on_caught_signal;
4097 sigemptyset(&action.sa_mask);
4098 sigaction(SIGSEGV, &action, NULL);
4099 sigaction(SIGABRT, &action, NULL);
4100}
4101
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004102int main(int argc, char *argv[])
4103{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004104 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004105 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004106 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004107 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004108 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004109 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004110 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004111 int *argc, char *argv[],
4112 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07004113 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004114 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004115 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004116 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004117 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004118 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004119 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004120 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004121 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004122 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004123 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004124 struct weston_config *config;
4125 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004126
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004127 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004128 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004129 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004130 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4131 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004132 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004133 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004134 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004135 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004136 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004137
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004138 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004139
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004140 if (help)
4141 usage(EXIT_SUCCESS);
4142
Scott Moreau12245142012-08-29 15:15:58 -06004143 if (version) {
4144 printf(PACKAGE_STRING "\n");
4145 return EXIT_SUCCESS;
4146 }
4147
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004148 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004149
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004150 weston_log("%s\n"
4151 STAMP_SPACE "%s\n"
4152 STAMP_SPACE "Bug reports to: %s\n"
4153 STAMP_SPACE "Build: %s\n",
4154 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004155 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004156 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004157
Martin Minarik37032f82012-06-18 20:15:18 +02004158 verify_xdg_runtime_dir();
4159
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004160 display = wl_display_create();
4161
Tiago Vignatti2116b892011-08-08 05:52:59 -07004162 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004163 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4164 display);
4165 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4166 display);
4167 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4168 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004169
4170 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004171 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4172 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004173
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004174 config = weston_config_parse("weston.ini");
4175 if (config != NULL) {
4176 weston_log("Using config file '%s'\n",
4177 weston_config_get_full_path(config));
4178 } else {
4179 weston_log("Starting with no config file.\n");
4180 }
4181 section = weston_config_get_section(config, "core", NULL, NULL);
4182
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004183 if (option_backend)
4184 backend = strdup(option_backend);
4185 else
4186 weston_config_section_get_string(section, "backend", &backend,
4187 NULL);
4188
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004189 if (!backend) {
4190 if (getenv("WAYLAND_DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004191 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004192 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004193 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004194 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004195 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004196 }
4197
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004198 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004199 free(backend);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004200 if (!backend_init)
4201 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004202
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004203 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004204 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004205 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004206 exit(EXIT_FAILURE);
4207 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004208
Peter Maatmane5b42e42013-03-27 22:38:53 +01004209 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004210 segv_compositor = ec;
4211
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004212 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004213 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004214
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004215 setenv("WAYLAND_DISPLAY", socket_name, 1);
4216
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004217 if (option_shell)
4218 shell = strdup(option_shell);
4219 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004220 weston_config_section_get_string(section, "shell", &shell,
4221 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004222
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004223 if (load_modules(ec, shell, &argc, argv) < 0) {
4224 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004225 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004226 }
4227 free(shell);
4228
4229 weston_config_section_get_string(section, "modules", &modules, "");
4230 if (load_modules(ec, modules, &argc, argv) < 0) {
4231 free(modules);
4232 goto out;
4233 }
4234 free(modules);
4235
Ossama Othmana50e6e42013-05-14 09:48:26 -07004236 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004237 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004238
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004239 for (i = 1; i < argc; i++)
4240 weston_log("fatal: unhandled option: %s\n", argv[i]);
4241 if (argc > 1) {
4242 ret = EXIT_FAILURE;
4243 goto out;
4244 }
4245
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004246 weston_compositor_log_capabilities(ec);
4247
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004248 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004249 weston_log("fatal: failed to add socket: %m\n");
4250 ret = EXIT_FAILURE;
4251 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004252 }
4253
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004254 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004255
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004256 wl_display_run(display);
4257
4258 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004259 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004260 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004261
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004262 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004263
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004264 for (i = ARRAY_LENGTH(signals); i;)
4265 wl_event_source_remove(signals[--i]);
4266
Daniel Stone855028f2012-05-01 20:37:10 +01004267 weston_compositor_xkb_destroy(ec);
4268
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004269 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004270 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004271
Martin Minarik19e6f262012-06-07 13:08:46 +02004272 weston_log_file_close();
4273
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004274 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004275}