blob: c62077c474d70b7060e06ff693a0bb5189f61747 [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
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300322struct process_info {
323 struct weston_process proc;
324 char *path;
325};
326
327static void
328process_handle_sigchld(struct weston_process *process, int status)
329{
330 struct process_info *pinfo =
331 container_of(process, struct process_info, proc);
332
333 /*
334 * There are no guarantees whether this runs before or after
335 * the wl_client destructor.
336 */
337
338 if (WIFEXITED(status)) {
339 weston_log("%s exited with status %d\n", pinfo->path,
340 WEXITSTATUS(status));
341 } else if (WIFSIGNALED(status)) {
342 weston_log("%s died on signal %d\n", pinfo->path,
343 WTERMSIG(status));
344 } else {
345 weston_log("%s disappeared\n", pinfo->path);
346 }
347
348 free(pinfo->path);
349 free(pinfo);
350}
351
352WL_EXPORT struct wl_client *
353weston_client_start(struct weston_compositor *compositor, const char *path)
354{
355 struct process_info *pinfo;
356 struct wl_client *client;
357
358 pinfo = zalloc(sizeof *pinfo);
359 if (!pinfo)
360 return NULL;
361
362 pinfo->path = strdup(path);
363 if (!pinfo->path)
364 goto out_free;
365
366 client = weston_client_launch(compositor, &pinfo->proc, path,
367 process_handle_sigchld);
368 if (!client)
369 goto out_str;
370
371 return client;
372
373out_str:
374 free(pinfo->path);
375
376out_free:
377 free(pinfo);
378
379 return NULL;
380}
381
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200382static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300383region_init_infinite(pixman_region32_t *region)
384{
385 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
386 UINT32_MAX, UINT32_MAX);
387}
388
Pekka Paalanene67858b2013-04-25 13:57:42 +0300389static struct weston_subsurface *
390weston_surface_to_subsurface(struct weston_surface *surface);
391
Jason Ekstranda7af7042013-10-12 22:38:11 -0500392WL_EXPORT struct weston_view *
393weston_view_create(struct weston_surface *surface)
394{
395 struct weston_view *view;
396
397 view = calloc(1, sizeof *view);
398 if (view == NULL)
399 return NULL;
400
401 view->surface = surface;
402
Jason Ekstranda7af7042013-10-12 22:38:11 -0500403 /* Assign to surface */
404 wl_list_insert(&surface->views, &view->surface_link);
405
406 wl_signal_init(&view->destroy_signal);
407 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300408 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500409
Xiong Zhang97116532013-10-23 13:58:31 +0800410 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300411 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300412 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500413
414 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300415 pixman_region32_init(&view->transform.masked_boundingbox);
416 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417
418 view->alpha = 1.0;
419 pixman_region32_init(&view->transform.opaque);
420
421 wl_list_init(&view->geometry.transformation_list);
422 wl_list_insert(&view->geometry.transformation_list,
423 &view->transform.position.link);
424 weston_matrix_init(&view->transform.position.matrix);
425 wl_list_init(&view->geometry.child_list);
426 pixman_region32_init(&view->transform.boundingbox);
427 view->transform.dirty = 1;
428
429 view->output = NULL;
430
431 return view;
432}
433
Jason Ekstrand108865d2014-06-26 10:04:49 -0700434struct weston_frame_callback {
435 struct wl_resource *resource;
436 struct wl_list link;
437};
438
Jason Ekstrand7b982072014-05-20 14:33:03 -0500439static void
440surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
441{
442 struct weston_surface_state *state =
443 container_of(listener, struct weston_surface_state,
444 buffer_destroy_listener);
445
446 state->buffer = NULL;
447}
448
449static void
450weston_surface_state_init(struct weston_surface_state *state)
451{
452 state->newly_attached = 0;
453 state->buffer = NULL;
454 state->buffer_destroy_listener.notify =
455 surface_state_handle_buffer_destroy;
456 state->sx = 0;
457 state->sy = 0;
458
459 pixman_region32_init(&state->damage);
460 pixman_region32_init(&state->opaque);
461 region_init_infinite(&state->input);
462
463 wl_list_init(&state->frame_callback_list);
464
465 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
466 state->buffer_viewport.buffer.scale = 1;
467 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
468 state->buffer_viewport.surface.width = -1;
469 state->buffer_viewport.changed = 0;
470}
471
472static void
473weston_surface_state_fini(struct weston_surface_state *state)
474{
475 struct weston_frame_callback *cb, *next;
476
477 wl_list_for_each_safe(cb, next,
478 &state->frame_callback_list, link)
479 wl_resource_destroy(cb->resource);
480
481 pixman_region32_fini(&state->input);
482 pixman_region32_fini(&state->opaque);
483 pixman_region32_fini(&state->damage);
484
485 if (state->buffer)
486 wl_list_remove(&state->buffer_destroy_listener.link);
487 state->buffer = NULL;
488}
489
490static void
491weston_surface_state_set_buffer(struct weston_surface_state *state,
492 struct weston_buffer *buffer)
493{
494 if (state->buffer == buffer)
495 return;
496
497 if (state->buffer)
498 wl_list_remove(&state->buffer_destroy_listener.link);
499 state->buffer = buffer;
500 if (state->buffer)
501 wl_signal_add(&state->buffer->destroy_signal,
502 &state->buffer_destroy_listener);
503}
504
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500505WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500506weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500507{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500508 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400509
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200510 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400511 if (surface == NULL)
512 return NULL;
513
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500514 wl_signal_init(&surface->destroy_signal);
515
516 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500517
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500518 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200519 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400520
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200521 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
522 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200523 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
524 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500525
526 weston_surface_state_init(&surface->pending);
527
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400528 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200529
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200530 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100531
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400532 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500533 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300534 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400535
Jason Ekstranda7af7042013-10-12 22:38:11 -0500536 wl_list_init(&surface->views);
537
538 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500539
Pekka Paalanene67858b2013-04-25 13:57:42 +0300540 wl_list_init(&surface->subsurface_list);
541 wl_list_init(&surface->subsurface_list_pending);
542
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400543 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500544}
545
Alex Wu8811bf92012-02-28 18:07:54 +0800546WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500547weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200548 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500549{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100550 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500551}
552
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400553WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500554weston_view_to_global_float(struct weston_view *view,
555 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200556{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200558 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
559
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200561
562 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200563 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700564 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200565 v.f[3]);
566 *x = 0;
567 *y = 0;
568 return;
569 }
570
571 *x = v.f[0] / v.f[3];
572 *y = v.f[1] / v.f[3];
573 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500574 *x = sx + view->geometry.x;
575 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200576 }
577}
578
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500579WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200580weston_transformed_coord(int width, int height,
581 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200582 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200583 float sx, float sy, float *bx, float *by)
584{
585 switch (transform) {
586 case WL_OUTPUT_TRANSFORM_NORMAL:
587 default:
588 *bx = sx;
589 *by = sy;
590 break;
591 case WL_OUTPUT_TRANSFORM_FLIPPED:
592 *bx = width - sx;
593 *by = sy;
594 break;
595 case WL_OUTPUT_TRANSFORM_90:
596 *bx = height - sy;
597 *by = sx;
598 break;
599 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
600 *bx = height - sy;
601 *by = width - sx;
602 break;
603 case WL_OUTPUT_TRANSFORM_180:
604 *bx = width - sx;
605 *by = height - sy;
606 break;
607 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
608 *bx = sx;
609 *by = height - sy;
610 break;
611 case WL_OUTPUT_TRANSFORM_270:
612 *bx = sy;
613 *by = width - sx;
614 break;
615 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
616 *bx = sy;
617 *by = sx;
618 break;
619 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200620
621 *bx *= scale;
622 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200623}
624
625WL_EXPORT pixman_box32_t
626weston_transformed_rect(int width, int height,
627 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200628 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200629 pixman_box32_t rect)
630{
631 float x1, x2, y1, y2;
632
633 pixman_box32_t ret;
634
Alexander Larsson4ea95522013-05-22 14:41:37 +0200635 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200636 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200637 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200638 rect.x2, rect.y2, &x2, &y2);
639
640 if (x1 <= x2) {
641 ret.x1 = x1;
642 ret.x2 = x2;
643 } else {
644 ret.x1 = x2;
645 ret.x2 = x1;
646 }
647
648 if (y1 <= y2) {
649 ret.y1 = y1;
650 ret.y2 = y2;
651 } else {
652 ret.y1 = y2;
653 ret.y2 = y1;
654 }
655
656 return ret;
657}
658
659WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500660weston_transformed_region(int width, int height,
661 enum wl_output_transform transform,
662 int32_t scale,
663 pixman_region32_t *src, pixman_region32_t *dest)
664{
665 pixman_box32_t *src_rects, *dest_rects;
666 int nrects, i;
667
668 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
669 if (src != dest)
670 pixman_region32_copy(dest, src);
671 return;
672 }
673
674 src_rects = pixman_region32_rectangles(src, &nrects);
675 dest_rects = malloc(nrects * sizeof(*dest_rects));
676 if (!dest_rects)
677 return;
678
679 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
680 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
681 } else {
682 for (i = 0; i < nrects; i++) {
683 switch (transform) {
684 default:
685 case WL_OUTPUT_TRANSFORM_NORMAL:
686 dest_rects[i].x1 = src_rects[i].x1;
687 dest_rects[i].y1 = src_rects[i].y1;
688 dest_rects[i].x2 = src_rects[i].x2;
689 dest_rects[i].y2 = src_rects[i].y2;
690 break;
691 case WL_OUTPUT_TRANSFORM_90:
692 dest_rects[i].x1 = height - src_rects[i].y2;
693 dest_rects[i].y1 = src_rects[i].x1;
694 dest_rects[i].x2 = height - src_rects[i].y1;
695 dest_rects[i].y2 = src_rects[i].x2;
696 break;
697 case WL_OUTPUT_TRANSFORM_180:
698 dest_rects[i].x1 = width - src_rects[i].x2;
699 dest_rects[i].y1 = height - src_rects[i].y2;
700 dest_rects[i].x2 = width - src_rects[i].x1;
701 dest_rects[i].y2 = height - src_rects[i].y1;
702 break;
703 case WL_OUTPUT_TRANSFORM_270:
704 dest_rects[i].x1 = src_rects[i].y1;
705 dest_rects[i].y1 = width - src_rects[i].x2;
706 dest_rects[i].x2 = src_rects[i].y2;
707 dest_rects[i].y2 = width - src_rects[i].x1;
708 break;
709 case WL_OUTPUT_TRANSFORM_FLIPPED:
710 dest_rects[i].x1 = width - src_rects[i].x2;
711 dest_rects[i].y1 = src_rects[i].y1;
712 dest_rects[i].x2 = width - src_rects[i].x1;
713 dest_rects[i].y2 = src_rects[i].y2;
714 break;
715 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
716 dest_rects[i].x1 = height - src_rects[i].y2;
717 dest_rects[i].y1 = width - src_rects[i].x2;
718 dest_rects[i].x2 = height - src_rects[i].y1;
719 dest_rects[i].y2 = width - src_rects[i].x1;
720 break;
721 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
722 dest_rects[i].x1 = src_rects[i].x1;
723 dest_rects[i].y1 = height - src_rects[i].y2;
724 dest_rects[i].x2 = src_rects[i].x2;
725 dest_rects[i].y2 = height - src_rects[i].y1;
726 break;
727 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
728 dest_rects[i].x1 = src_rects[i].y1;
729 dest_rects[i].y1 = src_rects[i].x1;
730 dest_rects[i].x2 = src_rects[i].y2;
731 dest_rects[i].y2 = src_rects[i].x2;
732 break;
733 }
734 }
735 }
736
737 if (scale != 1) {
738 for (i = 0; i < nrects; i++) {
739 dest_rects[i].x1 *= scale;
740 dest_rects[i].x2 *= scale;
741 dest_rects[i].y1 *= scale;
742 dest_rects[i].y2 *= scale;
743 }
744 }
745
746 pixman_region32_clear(dest);
747 pixman_region32_init_rects(dest, dest_rects, nrects);
748 free(dest_rects);
749}
750
Jonny Lamb74130762013-11-26 18:19:46 +0100751static void
752scaler_surface_to_buffer(struct weston_surface *surface,
753 float sx, float sy, float *bx, float *by)
754{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200755 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200756 double src_width, src_height;
757 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200758
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200759 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
760 if (vp->surface.width == -1) {
761 *bx = sx;
762 *by = sy;
763 return;
764 }
Jonny Lamb74130762013-11-26 18:19:46 +0100765
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200766 src_x = 0.0;
767 src_y = 0.0;
768 src_width = surface->width_from_buffer;
769 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100770 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200771 src_x = wl_fixed_to_double(vp->buffer.src_x);
772 src_y = wl_fixed_to_double(vp->buffer.src_y);
773 src_width = wl_fixed_to_double(vp->buffer.src_width);
774 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100775 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200776
777 *bx = sx * src_width / surface->width + src_x;
778 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100779}
780
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500781WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200782weston_surface_to_buffer_float(struct weston_surface *surface,
783 float sx, float sy, float *bx, float *by)
784{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200785 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
786
Jonny Lamb74130762013-11-26 18:19:46 +0100787 /* first transform coordinates if the scaler is set */
788 scaler_surface_to_buffer(surface, sx, sy, bx, by);
789
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500790 weston_transformed_coord(surface->width_from_buffer,
791 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200792 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100793 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200794}
795
Alexander Larsson4ea95522013-05-22 14:41:37 +0200796WL_EXPORT void
797weston_surface_to_buffer(struct weston_surface *surface,
798 int sx, int sy, int *bx, int *by)
799{
800 float bxf, byf;
801
Jonny Lamb74130762013-11-26 18:19:46 +0100802 weston_surface_to_buffer_float(surface,
803 sx, sy, &bxf, &byf);
804
Alexander Larsson4ea95522013-05-22 14:41:37 +0200805 *bx = floorf(bxf);
806 *by = floorf(byf);
807}
808
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200809WL_EXPORT pixman_box32_t
810weston_surface_to_buffer_rect(struct weston_surface *surface,
811 pixman_box32_t rect)
812{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200813 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100814 float xf, yf;
815
816 /* first transform box coordinates if the scaler is set */
817 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
818 rect.x1 = floorf(xf);
819 rect.y1 = floorf(yf);
820
821 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
822 rect.x2 = floorf(xf);
823 rect.y2 = floorf(yf);
824
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500825 return weston_transformed_rect(surface->width_from_buffer,
826 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200827 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200828 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200829}
830
831WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500832weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400833 struct weston_plane *plane)
834{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500835 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400836 return;
837
Jason Ekstranda7af7042013-10-12 22:38:11 -0500838 weston_view_damage_below(view);
839 view->plane = plane;
840 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400841}
842
843WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500844weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200845{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500846 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200847
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500848 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300849 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500850 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800851 if (view->plane)
852 pixman_region32_union(&view->plane->damage,
853 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500854 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800855 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200856}
857
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400858static void
859weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
860{
861 uint32_t different = es->output_mask ^ mask;
862 uint32_t entered = mask & different;
863 uint32_t left = es->output_mask & different;
864 struct weston_output *output;
865 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500866 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400867
868 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500869 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400870 return;
871 if (different == 0)
872 return;
873
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500874 client = wl_resource_get_client(es->resource);
875
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400876 wl_list_for_each(output, &es->compositor->output_list, link) {
877 if (1 << output->id & different)
878 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500879 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400880 client);
881 if (resource == NULL)
882 continue;
883 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500884 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400885 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500886 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400887 }
888}
889
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200890
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400891static void
892weston_surface_assign_output(struct weston_surface *es)
893{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894 struct weston_output *new_output;
895 struct weston_view *view;
896 pixman_region32_t region;
897 uint32_t max, area, mask;
898 pixman_box32_t *e;
899
900 new_output = NULL;
901 max = 0;
902 mask = 0;
903 pixman_region32_init(&region);
904 wl_list_for_each(view, &es->views, surface_link) {
905 if (!view->output)
906 continue;
907
908 pixman_region32_intersect(&region, &view->transform.boundingbox,
909 &view->output->region);
910
911 e = pixman_region32_extents(&region);
912 area = (e->x2 - e->x1) * (e->y2 - e->y1);
913
914 mask |= view->output_mask;
915
916 if (area >= max) {
917 new_output = view->output;
918 max = area;
919 }
920 }
921 pixman_region32_fini(&region);
922
923 es->output = new_output;
924 weston_surface_update_output_mask(es, mask);
925}
926
927static void
928weston_view_assign_output(struct weston_view *ev)
929{
930 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400931 struct weston_output *output, *new_output;
932 pixman_region32_t region;
933 uint32_t max, area, mask;
934 pixman_box32_t *e;
935
936 new_output = NULL;
937 max = 0;
938 mask = 0;
939 pixman_region32_init(&region);
940 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400942 &output->region);
943
944 e = pixman_region32_extents(&region);
945 area = (e->x2 - e->x1) * (e->y2 - e->y1);
946
947 if (area > 0)
948 mask |= 1 << output->id;
949
950 if (area >= max) {
951 new_output = output;
952 max = area;
953 }
954 }
955 pixman_region32_fini(&region);
956
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 ev->output = new_output;
958 ev->output_mask = mask;
959
960 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400961}
962
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200963static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
965 int32_t width, int32_t height,
966 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200967{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200968 float min_x = HUGE_VALF, min_y = HUGE_VALF;
969 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200970 int32_t s[4][2] = {
971 { sx, sy },
972 { sx, sy + height },
973 { sx + width, sy },
974 { sx + width, sy + height }
975 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200976 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200977 int i;
978
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300979 if (width == 0 || height == 0) {
980 /* avoid rounding empty bbox to 1x1 */
981 pixman_region32_init(bbox);
982 return;
983 }
984
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200985 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200986 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200988 if (x < min_x)
989 min_x = x;
990 if (x > max_x)
991 max_x = x;
992 if (y < min_y)
993 min_y = y;
994 if (y > max_y)
995 max_y = y;
996 }
997
Pekka Paalanen219b9822012-02-08 15:38:37 +0200998 int_x = floorf(min_x);
999 int_y = floorf(min_y);
1000 pixman_region32_init_rect(bbox, int_x, int_y,
1001 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001002}
1003
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001004static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001006{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001008
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001009 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 view->geometry.x = roundf(view->geometry.x);
1011 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001012
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001013 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001014 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1015 view->transform.position.matrix.d[12] = view->geometry.x;
1016 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001017
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 view->transform.inverse = view->transform.position.matrix;
1021 view->transform.inverse.d[12] = -view->geometry.x;
1022 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001023
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 pixman_region32_init_rect(&view->transform.boundingbox,
1025 view->geometry.x,
1026 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001027 view->surface->width,
1028 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001029
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 if (view->alpha == 1.0) {
1031 pixman_region32_copy(&view->transform.opaque,
1032 &view->surface->opaque);
1033 pixman_region32_translate(&view->transform.opaque,
1034 view->geometry.x,
1035 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001036 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001037}
1038
1039static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001041{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 struct weston_view *parent = view->geometry.parent;
1043 struct weston_matrix *matrix = &view->transform.matrix;
1044 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001045 struct weston_transform *tform;
1046
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001048
1049 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1051 view->transform.position.matrix.d[12] = view->geometry.x;
1052 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001053
1054 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001056 weston_matrix_multiply(matrix, &tform->matrix);
1057
Pekka Paalanen483243f2013-03-08 14:56:50 +02001058 if (parent)
1059 weston_matrix_multiply(matrix, &parent->transform.matrix);
1060
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001061 if (weston_matrix_invert(inverse, matrix) < 0) {
1062 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001063 weston_log("error: weston_view %p"
1064 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001065 return -1;
1066 }
1067
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001068 view_compute_bbox(view, 0, 0,
1069 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001071
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001072 return 0;
1073}
1074
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001075static struct weston_layer *
1076get_view_layer(struct weston_view *view)
1077{
1078 if (view->parent_view)
1079 return get_view_layer(view->parent_view);
1080 return view->layer_link.layer;
1081}
1082
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001083WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001085{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001087 struct weston_layer *layer;
1088 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001089
Jason Ekstranda7af7042013-10-12 22:38:11 -05001090 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001091 return;
1092
Pekka Paalanen483243f2013-03-08 14:56:50 +02001093 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001095
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001097
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001099
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 pixman_region32_fini(&view->transform.boundingbox);
1101 pixman_region32_fini(&view->transform.opaque);
1102 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001103
Pekka Paalanencd403622012-01-25 13:37:39 +02001104 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001105 if (view->geometry.transformation_list.next ==
1106 &view->transform.position.link &&
1107 view->geometry.transformation_list.prev ==
1108 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001109 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001111 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 if (weston_view_update_transform_enable(view) < 0)
1113 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001114 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001115
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001116 layer = get_view_layer(view);
1117 if (layer) {
1118 pixman_region32_init_with_extents(&mask, &layer->mask);
1119 pixman_region32_intersect(&view->transform.masked_boundingbox,
1120 &view->transform.boundingbox, &mask);
1121 pixman_region32_intersect(&view->transform.masked_opaque,
1122 &view->transform.opaque, &mask);
1123 pixman_region32_fini(&mask);
1124 }
1125
Jason Ekstranda7af7042013-10-12 22:38:11 -05001126 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001127
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 wl_signal_emit(&view->surface->compositor->transform_signal,
1131 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001132}
1133
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001134WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001136{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001138
1139 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 * The invariant: if view->geometry.dirty, then all views
1141 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001142 * Corollary: if not parent->geometry.dirty, then all ancestors
1143 * are not dirty.
1144 */
1145
Jason Ekstranda7af7042013-10-12 22:38:11 -05001146 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001147 return;
1148
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001150
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001152 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001154}
1155
1156WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001157weston_view_to_global_fixed(struct weston_view *view,
1158 wl_fixed_t vx, wl_fixed_t vy,
1159 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001160{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001161 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001162
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 weston_view_to_global_float(view,
1164 wl_fixed_to_double(vx),
1165 wl_fixed_to_double(vy),
1166 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001167 *x = wl_fixed_from_double(xf);
1168 *y = wl_fixed_from_double(yf);
1169}
1170
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001171WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001172weston_view_from_global_float(struct weston_view *view,
1173 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001174{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001176 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1177
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001179
1180 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001181 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001182 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001183 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 *vx = 0;
1185 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001186 return;
1187 }
1188
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 *vx = v.f[0] / v.f[3];
1190 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001191 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 *vx = x - view->geometry.x;
1193 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001194 }
1195}
1196
1197WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198weston_view_from_global_fixed(struct weston_view *view,
1199 wl_fixed_t x, wl_fixed_t y,
1200 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001201{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001202 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001203
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 weston_view_from_global_float(view,
1205 wl_fixed_to_double(x),
1206 wl_fixed_to_double(y),
1207 &vxf, &vyf);
1208 *vx = wl_fixed_from_double(vxf);
1209 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001210}
1211
1212WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001213weston_view_from_global(struct weston_view *view,
1214 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001215{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001217
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1219 *vx = floorf(vxf);
1220 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001221}
1222
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001223WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001224weston_surface_schedule_repaint(struct weston_surface *surface)
1225{
1226 struct weston_output *output;
1227
1228 wl_list_for_each(output, &surface->compositor->output_list, link)
1229 if (surface->output_mask & (1 << output->id))
1230 weston_output_schedule_repaint(output);
1231}
1232
1233WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001234weston_view_schedule_repaint(struct weston_view *view)
1235{
1236 struct weston_output *output;
1237
1238 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1239 if (view->output_mask & (1 << output->id))
1240 weston_output_schedule_repaint(output);
1241}
1242
1243WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001244weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001245{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001246 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001247 0, 0, surface->width,
1248 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001249
Kristian Høgsberg98238702012-08-03 16:29:12 -04001250 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001251}
1252
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001253WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001255{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001256 if (view->geometry.x == x && view->geometry.y == y)
1257 return;
1258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 view->geometry.x = x;
1260 view->geometry.y = y;
1261 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001262}
1263
Pekka Paalanen483243f2013-03-08 14:56:50 +02001264static void
1265transform_parent_handle_parent_destroy(struct wl_listener *listener,
1266 void *data)
1267{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 struct weston_view *view =
1269 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001270 geometry.parent_destroy_listener);
1271
Jason Ekstranda7af7042013-10-12 22:38:11 -05001272 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001273}
1274
1275WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276weston_view_set_transform_parent(struct weston_view *view,
1277 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001278{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279 if (view->geometry.parent) {
1280 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1281 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001282 }
1283
Jason Ekstranda7af7042013-10-12 22:38:11 -05001284 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001285
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001287 transform_parent_handle_parent_destroy;
1288 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001289 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001291 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293 }
1294
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 weston_view_geometry_dirty(view);
1296}
1297
1298WL_EXPORT int
1299weston_view_is_mapped(struct weston_view *view)
1300{
1301 if (view->output)
1302 return 1;
1303 else
1304 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001305}
1306
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001307WL_EXPORT int
1308weston_surface_is_mapped(struct weston_surface *surface)
1309{
1310 if (surface->output)
1311 return 1;
1312 else
1313 return 0;
1314}
1315
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001316static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001317surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001318{
1319 struct weston_view *view;
1320
1321 if (surface->width == width && surface->height == height)
1322 return;
1323
1324 surface->width = width;
1325 surface->height = height;
1326
1327 wl_list_for_each(view, &surface->views, surface_link)
1328 weston_view_geometry_dirty(view);
1329}
1330
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001331WL_EXPORT void
1332weston_surface_set_size(struct weston_surface *surface,
1333 int32_t width, int32_t height)
1334{
1335 assert(!surface->resource);
1336 surface_set_size(surface, width, height);
1337}
1338
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001339static int
1340fixed_round_up_to_int(wl_fixed_t f)
1341{
1342 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1343}
1344
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001345static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001346weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001347{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001348 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001349 int32_t width, height;
1350
1351 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001352 surface->width_from_buffer = 0;
1353 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001354 return;
1355 }
1356
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001357 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001358 case WL_OUTPUT_TRANSFORM_90:
1359 case WL_OUTPUT_TRANSFORM_270:
1360 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1361 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001362 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1363 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001364 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001365 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001366 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1367 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001368 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001369 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001370
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001371 surface->width_from_buffer = width;
1372 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001373}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001374
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001375static void
1376weston_surface_update_size(struct weston_surface *surface)
1377{
1378 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1379 int32_t width, height;
1380
1381 width = surface->width_from_buffer;
1382 height = surface->height_from_buffer;
1383
1384 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001385 surface_set_size(surface,
1386 vp->surface.width, vp->surface.height);
1387 return;
1388 }
1389
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001390 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001391 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1392 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1393
1394 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001395 return;
1396 }
1397
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001398 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001399}
1400
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001401WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001402weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001403{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001404 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001405
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001406 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001407
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001408 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001409}
1410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411WL_EXPORT struct weston_view *
1412weston_compositor_pick_view(struct weston_compositor *compositor,
1413 wl_fixed_t x, wl_fixed_t y,
1414 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001415{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001416 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001417 int ix = wl_fixed_to_int(x);
1418 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420 wl_list_for_each(view, &compositor->view_list, link) {
1421 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001422 if (pixman_region32_contains_point(
1423 &view->transform.masked_boundingbox,
1424 ix, iy, NULL) &&
1425 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001426 wl_fixed_to_int(*vx),
1427 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001428 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001430 }
1431
1432 return NULL;
1433}
1434
1435static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001436weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001437{
Daniel Stone37816df2012-05-16 18:45:18 +01001438 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001439
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001440 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001441 return;
1442
Daniel Stone37816df2012-05-16 18:45:18 +01001443 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001444 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001445}
1446
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001447WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001448weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001449{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001450 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001451
Jason Ekstranda7af7042013-10-12 22:38:11 -05001452 if (!weston_view_is_mapped(view))
1453 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001454
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 weston_view_damage_below(view);
1456 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001457 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001458 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459 wl_list_remove(&view->link);
1460 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 view->output_mask = 0;
1462 weston_surface_assign_output(view->surface);
1463
1464 if (weston_surface_is_mapped(view->surface))
1465 return;
1466
1467 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1468 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001469 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001470 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001471 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001472 NULL,
1473 wl_fixed_from_int(0),
1474 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001475 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001476 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001477 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001479
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480WL_EXPORT void
1481weston_surface_unmap(struct weston_surface *surface)
1482{
1483 struct weston_view *view;
1484
1485 wl_list_for_each(view, &surface->views, surface_link)
1486 weston_view_unmap(view);
1487 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001488}
1489
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001490static void
1491weston_surface_reset_pending_buffer(struct weston_surface *surface)
1492{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001493 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001494 surface->pending.sx = 0;
1495 surface->pending.sy = 0;
1496 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001497 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001498}
1499
Jason Ekstranda7af7042013-10-12 22:38:11 -05001500WL_EXPORT void
1501weston_view_destroy(struct weston_view *view)
1502{
1503 wl_signal_emit(&view->destroy_signal, view);
1504
1505 assert(wl_list_empty(&view->geometry.child_list));
1506
1507 if (weston_view_is_mapped(view)) {
1508 weston_view_unmap(view);
1509 weston_compositor_build_view_list(view->surface->compositor);
1510 }
1511
1512 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001513 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514
1515 pixman_region32_fini(&view->clip);
1516 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001517 pixman_region32_fini(&view->transform.masked_boundingbox);
1518 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519
1520 weston_view_set_transform_parent(view, NULL);
1521
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 wl_list_remove(&view->surface_link);
1523
1524 free(view);
1525}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001526
1527WL_EXPORT void
1528weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001529{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001530 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001532
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001533 if (--surface->ref_count > 0)
1534 return;
1535
1536 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1537
Pekka Paalanene67858b2013-04-25 13:57:42 +03001538 assert(wl_list_empty(&surface->subsurface_list_pending));
1539 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001540
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1542 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001543
Jason Ekstrand7b982072014-05-20 14:33:03 -05001544 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001545
Pekka Paalanende685b82012-12-04 15:58:12 +02001546 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001547
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001548 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001549 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001550 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001551
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001552 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001553 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001554
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001555 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001556}
1557
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001558static void
1559destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001560{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001561 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001562
Giulio Camuffo0d379742013-11-15 22:06:15 +01001563 /* Set the resource to NULL, since we don't want to leave a
1564 * dangling pointer if the surface was refcounted and survives
1565 * the weston_surface_destroy() call. */
1566 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001567 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001568}
1569
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001570static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001571weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1572{
1573 struct weston_buffer *buffer =
1574 container_of(listener, struct weston_buffer, destroy_listener);
1575
1576 wl_signal_emit(&buffer->destroy_signal, buffer);
1577 free(buffer);
1578}
1579
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001580WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001581weston_buffer_from_resource(struct wl_resource *resource)
1582{
1583 struct weston_buffer *buffer;
1584 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001585
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001586 listener = wl_resource_get_destroy_listener(resource,
1587 weston_buffer_destroy_handler);
1588
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001589 if (listener)
1590 return container_of(listener, struct weston_buffer,
1591 destroy_listener);
1592
1593 buffer = zalloc(sizeof *buffer);
1594 if (buffer == NULL)
1595 return NULL;
1596
1597 buffer->resource = resource;
1598 wl_signal_init(&buffer->destroy_signal);
1599 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001600 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001601 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001602
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001603 return buffer;
1604}
1605
1606static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001607weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1608 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001609{
Pekka Paalanende685b82012-12-04 15:58:12 +02001610 struct weston_buffer_reference *ref =
1611 container_of(listener, struct weston_buffer_reference,
1612 destroy_listener);
1613
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001614 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001615 ref->buffer = NULL;
1616}
1617
1618WL_EXPORT void
1619weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001620 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001621{
1622 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001623 ref->buffer->busy_count--;
1624 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001625 assert(wl_resource_get_client(ref->buffer->resource));
1626 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001627 WL_BUFFER_RELEASE);
1628 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001629 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001630 }
1631
Pekka Paalanende685b82012-12-04 15:58:12 +02001632 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001633 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001634 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001635 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001636 }
1637
Pekka Paalanende685b82012-12-04 15:58:12 +02001638 ref->buffer = buffer;
1639 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1640}
1641
1642static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001643weston_surface_attach(struct weston_surface *surface,
1644 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001645{
1646 weston_buffer_reference(&surface->buffer_ref, buffer);
1647
Pekka Paalanena6421c42012-12-04 15:58:10 +02001648 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001649 if (weston_surface_is_mapped(surface))
1650 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001651 }
1652
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001653 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001654
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001655 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001656}
1657
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001658WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001659weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001660{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001662
1663 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001664 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001665}
1666
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001667WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001668weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001669{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001670 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001671
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001672 pixman_region32_union(&compositor->primary_plane.damage,
1673 &compositor->primary_plane.damage,
1674 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001675 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001676}
1677
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001678static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001680{
Pekka Paalanende685b82012-12-04 15:58:12 +02001681 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001682 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001683 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001684
Jason Ekstrandef540082014-06-26 10:37:36 -07001685 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001686}
1687
1688static void
1689view_accumulate_damage(struct weston_view *view,
1690 pixman_region32_t *opaque)
1691{
1692 pixman_region32_t damage;
1693
1694 pixman_region32_init(&damage);
1695 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001696 pixman_box32_t *extents;
1697
Jason Ekstranda7af7042013-10-12 22:38:11 -05001698 extents = pixman_region32_extents(&view->surface->damage);
1699 view_compute_bbox(view, extents->x1, extents->y1,
1700 extents->x2 - extents->x1,
1701 extents->y2 - extents->y1,
1702 &damage);
1703 pixman_region32_translate(&damage,
1704 -view->plane->x,
1705 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001706 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 pixman_region32_copy(&damage, &view->surface->damage);
1708 pixman_region32_translate(&damage,
1709 view->geometry.x - view->plane->x,
1710 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001711 }
1712
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 pixman_region32_subtract(&damage, &damage, opaque);
1714 pixman_region32_union(&view->plane->damage,
1715 &view->plane->damage, &damage);
1716 pixman_region32_fini(&damage);
1717 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001718 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001719}
1720
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001721static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001722compositor_accumulate_damage(struct weston_compositor *ec)
1723{
1724 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001726 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001727
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001728 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001729
1730 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001731 pixman_region32_copy(&plane->clip, &clip);
1732
1733 pixman_region32_init(&opaque);
1734
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 wl_list_for_each(ev, &ec->view_list, link) {
1736 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001737 continue;
1738
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001740 }
1741
1742 pixman_region32_union(&clip, &clip, &opaque);
1743 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001744 }
1745
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001746 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001747
Jason Ekstranda7af7042013-10-12 22:38:11 -05001748 wl_list_for_each(ev, &ec->view_list, link)
1749 ev->surface->touched = 0;
1750
1751 wl_list_for_each(ev, &ec->view_list, link) {
1752 if (ev->surface->touched)
1753 continue;
1754 ev->surface->touched = 1;
1755
1756 surface_flush_damage(ev->surface);
1757
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001758 /* Both the renderer and the backend have seen the buffer
1759 * by now. If renderer needs the buffer, it has its own
1760 * reference set. If the backend wants to keep the buffer
1761 * around for migrating the surface into a non-primary plane
1762 * later, keep_buffer is true. Otherwise, drop the core
1763 * reference now, and allow early buffer release. This enables
1764 * clients to use single-buffering.
1765 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 if (!ev->surface->keep_buffer)
1767 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001768 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001769}
1770
1771static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001773{
1774 struct weston_subsurface *sub;
1775
Pekka Paalanene67858b2013-04-25 13:57:42 +03001776 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001777 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001778 continue;
1779
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1781 wl_list_init(&sub->surface->views);
1782
1783 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001784 }
1785}
1786
1787static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001788surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001789{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 struct weston_subsurface *sub;
1791 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001792
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1794 if (sub->surface == surface)
1795 continue;
1796
George Kiagiadakised04d382014-06-13 18:10:26 +02001797 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1798 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001799 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001800 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801
1802 surface_free_unused_subsurface_views(sub->surface);
1803 }
1804}
1805
1806static void
1807view_list_add_subsurface_view(struct weston_compositor *compositor,
1808 struct weston_subsurface *sub,
1809 struct weston_view *parent)
1810{
1811 struct weston_subsurface *child;
1812 struct weston_view *view = NULL, *iv;
1813
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001814 if (!weston_surface_is_mapped(sub->surface))
1815 return;
1816
Jason Ekstranda7af7042013-10-12 22:38:11 -05001817 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1818 if (iv->geometry.parent == parent) {
1819 view = iv;
1820 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001821 }
1822 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823
1824 if (view) {
1825 /* Put it back in the surface's list of views */
1826 wl_list_remove(&view->surface_link);
1827 wl_list_insert(&sub->surface->views, &view->surface_link);
1828 } else {
1829 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001830 weston_view_set_position(view,
1831 sub->position.x,
1832 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 weston_view_set_transform_parent(view, parent);
1834 }
1835
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001836 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001837 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001838
Pekka Paalanenb188e912013-11-19 14:03:35 +02001839 if (wl_list_empty(&sub->surface->subsurface_list)) {
1840 wl_list_insert(compositor->view_list.prev, &view->link);
1841 return;
1842 }
1843
1844 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1845 if (child->surface == sub->surface)
1846 wl_list_insert(compositor->view_list.prev, &view->link);
1847 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001848 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001849 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850}
1851
1852static void
1853view_list_add(struct weston_compositor *compositor,
1854 struct weston_view *view)
1855{
1856 struct weston_subsurface *sub;
1857
1858 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001859
Pekka Paalanenb188e912013-11-19 14:03:35 +02001860 if (wl_list_empty(&view->surface->subsurface_list)) {
1861 wl_list_insert(compositor->view_list.prev, &view->link);
1862 return;
1863 }
1864
1865 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1866 if (sub->surface == view->surface)
1867 wl_list_insert(compositor->view_list.prev, &view->link);
1868 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001869 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001870 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871}
1872
1873static void
1874weston_compositor_build_view_list(struct weston_compositor *compositor)
1875{
1876 struct weston_view *view;
1877 struct weston_layer *layer;
1878
1879 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001880 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001881 surface_stash_subsurface_views(view->surface);
1882
1883 wl_list_init(&compositor->view_list);
1884 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001885 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 view_list_add(compositor, view);
1887 }
1888 }
1889
1890 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001891 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001892 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001893}
1894
David Herrmann1edf44c2013-10-22 17:11:26 +02001895static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001896weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001897{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001899 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001900 struct weston_animation *animation, *next;
1901 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001902 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001903 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001904 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001905
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001906 if (output->destroying)
1907 return 0;
1908
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001909 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001910 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001911
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001912 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001913 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001914 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001915 wl_list_for_each(ev, &ec->view_list, link)
1916 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001917
Pekka Paalanene67858b2013-04-25 13:57:42 +03001918 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919 wl_list_for_each(ev, &ec->view_list, link) {
1920 /* Note: This operation is safe to do multiple times on the
1921 * same surface.
1922 */
1923 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001924 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 &ev->surface->frame_callback_list);
1926 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001927 }
1928 }
1929
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001930 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001931
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001932 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001933 pixman_region32_intersect(&output_damage,
1934 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001935 pixman_region32_subtract(&output_damage,
1936 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001937
Scott Moreauccbf29d2012-02-22 14:21:41 -07001938 if (output->dirty)
1939 weston_output_update_matrix(output);
1940
David Herrmann1edf44c2013-10-22 17:11:26 +02001941 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001942
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001943 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001944
Kristian Høgsbergef044142011-06-21 15:02:12 -04001945 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001946
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001947 weston_compositor_repick(ec);
1948 wl_event_loop_dispatch(ec->input_loop, 0);
1949
Jonas Ådahldb773762012-06-13 00:01:21 +02001950 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001951 wl_callback_send_done(cb->resource, msecs);
1952 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001953 }
1954
Scott Moreaud64cf212012-06-08 19:40:54 -06001955 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001956 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001957 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001958 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001959
1960 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001961}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001962
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001963static int
1964weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001965{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001966 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001967
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001968 wl_event_loop_dispatch(compositor->input_loop, 0);
1969
1970 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001971}
1972
1973WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001974weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001975{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001976 struct weston_compositor *compositor = output->compositor;
1977 struct wl_event_loop *loop =
1978 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001979 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001980
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001981 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001982
1983 if (output->repaint_needed &&
1984 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1985 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001986 r = weston_output_repaint(output, msecs);
1987 if (!r)
1988 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001989 }
1990
1991 output->repaint_scheduled = 0;
1992 if (compositor->input_loop_source)
1993 return;
1994
1995 fd = wl_event_loop_get_fd(compositor->input_loop);
1996 compositor->input_loop_source =
1997 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1998 weston_compositor_read_input, compositor);
1999}
2000
2001static void
2002idle_repaint(void *data)
2003{
2004 struct weston_output *output = data;
2005
Jonas Ådahle5a12252013-04-05 23:07:11 +02002006 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002007}
2008
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002009WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002010weston_layer_entry_insert(struct weston_layer_entry *list,
2011 struct weston_layer_entry *entry)
2012{
2013 wl_list_insert(&list->link, &entry->link);
2014 entry->layer = list->layer;
2015}
2016
2017WL_EXPORT void
2018weston_layer_entry_remove(struct weston_layer_entry *entry)
2019{
2020 wl_list_remove(&entry->link);
2021 wl_list_init(&entry->link);
2022 entry->layer = NULL;
2023}
2024
2025WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002026weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2027{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002028 wl_list_init(&layer->view_list.link);
2029 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002030 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002031 if (below != NULL)
2032 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002033}
2034
2035WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002036weston_layer_set_mask(struct weston_layer *layer,
2037 int x, int y, int width, int height)
2038{
2039 struct weston_view *view;
2040
2041 layer->mask.x1 = x;
2042 layer->mask.x2 = x + width;
2043 layer->mask.y1 = y;
2044 layer->mask.y2 = y + height;
2045
2046 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2047 weston_view_geometry_dirty(view);
2048 }
2049}
2050
2051WL_EXPORT void
2052weston_layer_set_mask_infinite(struct weston_layer *layer)
2053{
2054 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2055 UINT32_MAX, UINT32_MAX);
2056}
2057
2058WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002059weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002060{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002061 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002062 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002063
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002064 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2065 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002066 return;
2067
Kristian Høgsbergef044142011-06-21 15:02:12 -04002068 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002069 output->repaint_needed = 1;
2070 if (output->repaint_scheduled)
2071 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002072
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002073 wl_event_loop_add_idle(loop, idle_repaint, output);
2074 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002075
2076 if (compositor->input_loop_source) {
2077 wl_event_source_remove(compositor->input_loop_source);
2078 compositor->input_loop_source = NULL;
2079 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002080}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002081
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002082WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002083weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2084{
2085 struct weston_output *output;
2086
2087 wl_list_for_each(output, &compositor->output_list, link)
2088 weston_output_schedule_repaint(output);
2089}
2090
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002091static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002092surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002093{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002094 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002095}
2096
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002097static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002098surface_attach(struct wl_client *client,
2099 struct wl_resource *resource,
2100 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2101{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002102 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002103 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002104
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002105 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002106 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002107 if (buffer == NULL) {
2108 wl_client_post_no_memory(client);
2109 return;
2110 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002111 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002112
Pekka Paalanende685b82012-12-04 15:58:12 +02002113 /* Attach, attach, without commit in between does not send
2114 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002115 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002116
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002117 surface->pending.sx = sx;
2118 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002119 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002120}
2121
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002122static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002123surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002124 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002125 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002126{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002127 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002128
Pekka Paalanen8e159182012-10-10 12:49:25 +03002129 pixman_region32_union_rect(&surface->pending.damage,
2130 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002131 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002132}
2133
Kristian Høgsberg33418202011-08-16 23:01:28 -04002134static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002135destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002136{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002137 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002138
2139 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002140 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002141}
2142
2143static void
2144surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002145 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002146{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002147 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002148 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002149
2150 cb = malloc(sizeof *cb);
2151 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002152 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002153 return;
2154 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002155
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002156 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2157 callback);
2158 if (cb->resource == NULL) {
2159 free(cb);
2160 wl_resource_post_no_memory(resource);
2161 return;
2162 }
2163
Jason Ekstranda85118c2013-06-27 20:17:02 -05002164 wl_resource_set_implementation(cb->resource, NULL, cb,
2165 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002166
Pekka Paalanenbc106382012-10-10 12:49:31 +03002167 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002168}
2169
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002170static void
2171surface_set_opaque_region(struct wl_client *client,
2172 struct wl_resource *resource,
2173 struct wl_resource *region_resource)
2174{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002175 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002176 struct weston_region *region;
2177
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002178 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002179 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002180 pixman_region32_copy(&surface->pending.opaque,
2181 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002182 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002183 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002184 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002185}
2186
2187static void
2188surface_set_input_region(struct wl_client *client,
2189 struct wl_resource *resource,
2190 struct wl_resource *region_resource)
2191{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002192 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002193 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002194
2195 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002196 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002197 pixman_region32_copy(&surface->pending.input,
2198 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002199 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002200 pixman_region32_fini(&surface->pending.input);
2201 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002202 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002203}
2204
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002205static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002206weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002207{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002208 struct weston_subsurface *sub;
2209
2210 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2211 parent_link_pending) {
2212 wl_list_remove(&sub->parent_link);
2213 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2214 }
2215}
2216
2217static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002218weston_surface_commit_state(struct weston_surface *surface,
2219 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002220{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002221 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002222 pixman_region32_t opaque;
2223
Alexander Larsson4ea95522013-05-22 14:41:37 +02002224 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002225 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002226 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002227 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002228
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002229 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002230 if (state->newly_attached)
2231 weston_surface_attach(surface, state->buffer);
2232 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002233
Jason Ekstrand7b982072014-05-20 14:33:03 -05002234 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002235 weston_surface_update_size(surface);
2236 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002237 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002238 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002239
Jason Ekstrand7b982072014-05-20 14:33:03 -05002240 state->sx = 0;
2241 state->sy = 0;
2242 state->newly_attached = 0;
2243 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002244
2245 /* wl_surface.damage */
2246 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002247 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002248 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002249 0, 0, surface->width, surface->height);
2250 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002251
2252 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002253 pixman_region32_init(&opaque);
2254 pixman_region32_intersect_rect(&opaque, &state->opaque,
2255 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002256
2257 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2258 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002259 wl_list_for_each(view, &surface->views, surface_link)
2260 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002261 }
2262
2263 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002264
2265 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002266 pixman_region32_intersect_rect(&surface->input, &state->input,
2267 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002268
Pekka Paalanenbc106382012-10-10 12:49:31 +03002269 /* wl_surface.frame */
2270 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002271 &state->frame_callback_list);
2272 wl_list_init(&state->frame_callback_list);
2273}
2274
2275static void
2276weston_surface_commit(struct weston_surface *surface)
2277{
2278 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002279
Pekka Paalanene67858b2013-04-25 13:57:42 +03002280 weston_surface_commit_subsurface_order(surface);
2281
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002282 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002283}
2284
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002285static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002286weston_subsurface_commit(struct weston_subsurface *sub);
2287
2288static void
2289weston_subsurface_parent_commit(struct weston_subsurface *sub,
2290 int parent_is_synchronized);
2291
2292static void
2293surface_commit(struct wl_client *client, struct wl_resource *resource)
2294{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002295 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002296 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2297
2298 if (sub) {
2299 weston_subsurface_commit(sub);
2300 return;
2301 }
2302
2303 weston_surface_commit(surface);
2304
2305 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2306 if (sub->surface != surface)
2307 weston_subsurface_parent_commit(sub, 0);
2308 }
2309}
2310
2311static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002312surface_set_buffer_transform(struct wl_client *client,
2313 struct wl_resource *resource, int transform)
2314{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002315 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002316
Jonny Lamba55f1392014-05-30 12:07:15 +02002317 /* if wl_output.transform grows more members this will need to be updated. */
2318 if (transform < 0 ||
2319 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2320 wl_resource_post_error(resource,
2321 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2322 "buffer transform must be a valid transform "
2323 "('%d' specified)", transform);
2324 return;
2325 }
2326
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002327 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002328 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002329}
2330
Alexander Larsson4ea95522013-05-22 14:41:37 +02002331static void
2332surface_set_buffer_scale(struct wl_client *client,
2333 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002334 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002335{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002336 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002337
Jonny Lamba55f1392014-05-30 12:07:15 +02002338 if (scale < 1) {
2339 wl_resource_post_error(resource,
2340 WL_SURFACE_ERROR_INVALID_SCALE,
2341 "buffer scale must be at least one "
2342 "('%d' specified)", scale);
2343 return;
2344 }
2345
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002346 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002347 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002348}
2349
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002350static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002351 surface_destroy,
2352 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002353 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002354 surface_frame,
2355 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002356 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002357 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002358 surface_set_buffer_transform,
2359 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002360};
2361
2362static void
2363compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002364 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002365{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002366 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002367 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002368
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002369 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002370 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002371 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002372 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002373 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002374
Jason Ekstranda85118c2013-06-27 20:17:02 -05002375 surface->resource =
2376 wl_resource_create(client, &wl_surface_interface,
2377 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002378 if (surface->resource == NULL) {
2379 weston_surface_destroy(surface);
2380 wl_resource_post_no_memory(resource);
2381 return;
2382 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002383 wl_resource_set_implementation(surface->resource, &surface_interface,
2384 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002385
2386 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002387}
2388
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002389static void
2390destroy_region(struct wl_resource *resource)
2391{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002392 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002393
2394 pixman_region32_fini(&region->region);
2395 free(region);
2396}
2397
2398static void
2399region_destroy(struct wl_client *client, struct wl_resource *resource)
2400{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002401 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002402}
2403
2404static void
2405region_add(struct wl_client *client, struct wl_resource *resource,
2406 int32_t x, int32_t y, int32_t width, int32_t height)
2407{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002408 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002409
2410 pixman_region32_union_rect(&region->region, &region->region,
2411 x, y, width, height);
2412}
2413
2414static void
2415region_subtract(struct wl_client *client, struct wl_resource *resource,
2416 int32_t x, int32_t y, int32_t width, int32_t height)
2417{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002418 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002419 pixman_region32_t rect;
2420
2421 pixman_region32_init_rect(&rect, x, y, width, height);
2422 pixman_region32_subtract(&region->region, &region->region, &rect);
2423 pixman_region32_fini(&rect);
2424}
2425
2426static const struct wl_region_interface region_interface = {
2427 region_destroy,
2428 region_add,
2429 region_subtract
2430};
2431
2432static void
2433compositor_create_region(struct wl_client *client,
2434 struct wl_resource *resource, uint32_t id)
2435{
2436 struct weston_region *region;
2437
2438 region = malloc(sizeof *region);
2439 if (region == NULL) {
2440 wl_resource_post_no_memory(resource);
2441 return;
2442 }
2443
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002444 pixman_region32_init(&region->region);
2445
Jason Ekstranda85118c2013-06-27 20:17:02 -05002446 region->resource =
2447 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002448 if (region->resource == NULL) {
2449 free(region);
2450 wl_resource_post_no_memory(resource);
2451 return;
2452 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002453 wl_resource_set_implementation(region->resource, &region_interface,
2454 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002455}
2456
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002457static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002458 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002459 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002460};
2461
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002462static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002463weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2464{
2465 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002466
Jason Ekstrand7b982072014-05-20 14:33:03 -05002467 weston_surface_commit_state(surface, &sub->cached);
2468 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002469
2470 weston_surface_commit_subsurface_order(surface);
2471
2472 weston_surface_schedule_repaint(surface);
2473
Jason Ekstrand7b982072014-05-20 14:33:03 -05002474 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002475}
2476
2477static void
2478weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2479{
2480 struct weston_surface *surface = sub->surface;
2481
2482 /*
2483 * If this commit would cause the surface to move by the
2484 * attach(dx, dy) parameters, the old damage region must be
2485 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002486 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002487 */
2488 pixman_region32_translate(&sub->cached.damage,
2489 -surface->pending.sx, -surface->pending.sy);
2490 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2491 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002492 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002493
2494 if (surface->pending.newly_attached) {
2495 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002496 weston_surface_state_set_buffer(&sub->cached,
2497 surface->pending.buffer);
2498 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002499 surface->pending.buffer);
2500 }
2501 sub->cached.sx += surface->pending.sx;
2502 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002503
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002504 sub->cached.buffer_viewport.changed |=
2505 surface->pending.buffer_viewport.changed;
2506 sub->cached.buffer_viewport.buffer =
2507 surface->pending.buffer_viewport.buffer;
2508 sub->cached.buffer_viewport.surface =
2509 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002510
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002511 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512
2513 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2514
2515 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2516
2517 wl_list_insert_list(&sub->cached.frame_callback_list,
2518 &surface->pending.frame_callback_list);
2519 wl_list_init(&surface->pending.frame_callback_list);
2520
Jason Ekstrand7b982072014-05-20 14:33:03 -05002521 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002522}
2523
2524static int
2525weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2526{
2527 while (sub) {
2528 if (sub->synchronized)
2529 return 1;
2530
2531 if (!sub->parent)
2532 return 0;
2533
2534 sub = weston_surface_to_subsurface(sub->parent);
2535 }
2536
2537 return 0;
2538}
2539
2540static void
2541weston_subsurface_commit(struct weston_subsurface *sub)
2542{
2543 struct weston_surface *surface = sub->surface;
2544 struct weston_subsurface *tmp;
2545
2546 /* Recursive check for effectively synchronized. */
2547 if (weston_subsurface_is_synchronized(sub)) {
2548 weston_subsurface_commit_to_cache(sub);
2549 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002550 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551 /* flush accumulated state from cache */
2552 weston_subsurface_commit_to_cache(sub);
2553 weston_subsurface_commit_from_cache(sub);
2554 } else {
2555 weston_surface_commit(surface);
2556 }
2557
2558 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2559 if (tmp->surface != surface)
2560 weston_subsurface_parent_commit(tmp, 0);
2561 }
2562 }
2563}
2564
2565static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002566weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567{
2568 struct weston_surface *surface = sub->surface;
2569 struct weston_subsurface *tmp;
2570
Pekka Paalanene67858b2013-04-25 13:57:42 +03002571 /* From now on, commit_from_cache the whole sub-tree, regardless of
2572 * the synchronized mode of each child. This sub-surface or some
2573 * of its ancestors were synchronized, so we are synchronized
2574 * all the way down.
2575 */
2576
Jason Ekstrand7b982072014-05-20 14:33:03 -05002577 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002578 weston_subsurface_commit_from_cache(sub);
2579
2580 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2581 if (tmp->surface != surface)
2582 weston_subsurface_parent_commit(tmp, 1);
2583 }
2584}
2585
2586static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002587weston_subsurface_parent_commit(struct weston_subsurface *sub,
2588 int parent_is_synchronized)
2589{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002590 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002591 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 wl_list_for_each(view, &sub->surface->views, surface_link)
2593 weston_view_set_position(view,
2594 sub->position.x,
2595 sub->position.y);
2596
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002597 sub->position.set = 0;
2598 }
2599
2600 if (parent_is_synchronized || sub->synchronized)
2601 weston_subsurface_synchronized_commit(sub);
2602}
2603
2604static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002605subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002606{
2607 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002608 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002609
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002611 weston_view_set_position(view,
2612 view->geometry.x + dx,
2613 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002614
2615 /* No need to check parent mappedness, because if parent is not
2616 * mapped, parent is not in a visible layer, so this sub-surface
2617 * will not be drawn either.
2618 */
2619 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002620 struct weston_output *output;
2621
Pekka Paalanene67858b2013-04-25 13:57:42 +03002622 /* Cannot call weston_surface_update_transform(),
2623 * because that would call it also for the parent surface,
2624 * which might not be mapped yet. That would lead to
2625 * inconsistent state, where the window could never be
2626 * mapped.
2627 *
2628 * Instead just assing any output, to make
2629 * weston_surface_is_mapped() return true, so that when the
2630 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002631 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632 */
2633 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002634 output = container_of(compositor->output_list.next,
2635 struct weston_output, link);
2636
2637 surface->output = output;
2638 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002639 }
2640}
2641
2642static struct weston_subsurface *
2643weston_surface_to_subsurface(struct weston_surface *surface)
2644{
2645 if (surface->configure == subsurface_configure)
2646 return surface->configure_private;
2647
2648 return NULL;
2649}
2650
Pekka Paalanen01388e22013-04-25 13:57:44 +03002651WL_EXPORT struct weston_surface *
2652weston_surface_get_main_surface(struct weston_surface *surface)
2653{
2654 struct weston_subsurface *sub;
2655
2656 while (surface && (sub = weston_surface_to_subsurface(surface)))
2657 surface = sub->parent;
2658
2659 return surface;
2660}
2661
Pekka Paalanene67858b2013-04-25 13:57:42 +03002662static void
2663subsurface_set_position(struct wl_client *client,
2664 struct wl_resource *resource, int32_t x, int32_t y)
2665{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002666 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002667
2668 if (!sub)
2669 return;
2670
2671 sub->position.x = x;
2672 sub->position.y = y;
2673 sub->position.set = 1;
2674}
2675
2676static struct weston_subsurface *
2677subsurface_from_surface(struct weston_surface *surface)
2678{
2679 struct weston_subsurface *sub;
2680
2681 sub = weston_surface_to_subsurface(surface);
2682 if (sub)
2683 return sub;
2684
2685 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2686 if (sub->surface == surface)
2687 return sub;
2688
2689 return NULL;
2690}
2691
2692static struct weston_subsurface *
2693subsurface_sibling_check(struct weston_subsurface *sub,
2694 struct weston_surface *surface,
2695 const char *request)
2696{
2697 struct weston_subsurface *sibling;
2698
2699 sibling = subsurface_from_surface(surface);
2700
2701 if (!sibling) {
2702 wl_resource_post_error(sub->resource,
2703 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2704 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002705 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002706 return NULL;
2707 }
2708
2709 if (sibling->parent != sub->parent) {
2710 wl_resource_post_error(sub->resource,
2711 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2712 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002713 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002714 return NULL;
2715 }
2716
2717 return sibling;
2718}
2719
2720static void
2721subsurface_place_above(struct wl_client *client,
2722 struct wl_resource *resource,
2723 struct wl_resource *sibling_resource)
2724{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002725 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002726 struct weston_surface *surface =
2727 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002728 struct weston_subsurface *sibling;
2729
2730 if (!sub)
2731 return;
2732
2733 sibling = subsurface_sibling_check(sub, surface, "place_above");
2734 if (!sibling)
2735 return;
2736
2737 wl_list_remove(&sub->parent_link_pending);
2738 wl_list_insert(sibling->parent_link_pending.prev,
2739 &sub->parent_link_pending);
2740}
2741
2742static void
2743subsurface_place_below(struct wl_client *client,
2744 struct wl_resource *resource,
2745 struct wl_resource *sibling_resource)
2746{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002747 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002748 struct weston_surface *surface =
2749 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750 struct weston_subsurface *sibling;
2751
2752 if (!sub)
2753 return;
2754
2755 sibling = subsurface_sibling_check(sub, surface, "place_below");
2756 if (!sibling)
2757 return;
2758
2759 wl_list_remove(&sub->parent_link_pending);
2760 wl_list_insert(&sibling->parent_link_pending,
2761 &sub->parent_link_pending);
2762}
2763
2764static void
2765subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2766{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002767 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002768
2769 if (sub)
2770 sub->synchronized = 1;
2771}
2772
2773static void
2774subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2775{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002776 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002777
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002778 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002780
2781 /* If sub became effectively desynchronized, flush. */
2782 if (!weston_subsurface_is_synchronized(sub))
2783 weston_subsurface_synchronized_commit(sub);
2784 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002785}
2786
2787static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2789{
2790 wl_list_remove(&sub->parent_link);
2791 wl_list_remove(&sub->parent_link_pending);
2792 wl_list_remove(&sub->parent_destroy_listener.link);
2793 sub->parent = NULL;
2794}
2795
2796static void
2797weston_subsurface_destroy(struct weston_subsurface *sub);
2798
2799static void
2800subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2801{
2802 struct weston_subsurface *sub =
2803 container_of(listener, struct weston_subsurface,
2804 surface_destroy_listener);
2805 assert(data == &sub->surface->resource);
2806
2807 /* The protocol object (wl_resource) is left inert. */
2808 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002809 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002810
2811 weston_subsurface_destroy(sub);
2812}
2813
2814static void
2815subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2816{
2817 struct weston_subsurface *sub =
2818 container_of(listener, struct weston_subsurface,
2819 parent_destroy_listener);
2820 assert(data == &sub->parent->resource);
2821 assert(sub->surface != sub->parent);
2822
2823 if (weston_surface_is_mapped(sub->surface))
2824 weston_surface_unmap(sub->surface);
2825
2826 weston_subsurface_unlink_parent(sub);
2827}
2828
2829static void
2830subsurface_resource_destroy(struct wl_resource *resource)
2831{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002832 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833
2834 if (sub)
2835 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836}
2837
2838static void
2839subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2840{
2841 wl_resource_destroy(resource);
2842}
2843
2844static void
2845weston_subsurface_link_parent(struct weston_subsurface *sub,
2846 struct weston_surface *parent)
2847{
2848 sub->parent = parent;
2849 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002850 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002851 &sub->parent_destroy_listener);
2852
2853 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2854 wl_list_insert(&parent->subsurface_list_pending,
2855 &sub->parent_link_pending);
2856}
2857
2858static void
2859weston_subsurface_link_surface(struct weston_subsurface *sub,
2860 struct weston_surface *surface)
2861{
2862 sub->surface = surface;
2863 sub->surface_destroy_listener.notify =
2864 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002865 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002866 &sub->surface_destroy_listener);
2867}
2868
2869static void
2870weston_subsurface_destroy(struct weston_subsurface *sub)
2871{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002872 struct weston_view *view, *next;
2873
Pekka Paalanene67858b2013-04-25 13:57:42 +03002874 assert(sub->surface);
2875
2876 if (sub->resource) {
2877 assert(weston_surface_to_subsurface(sub->surface) == sub);
2878 assert(sub->parent_destroy_listener.notify ==
2879 subsurface_handle_parent_destroy);
2880
George Kiagiadakised04d382014-06-13 18:10:26 +02002881 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2882 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002883 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002884 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002885
Pekka Paalanene67858b2013-04-25 13:57:42 +03002886 if (sub->parent)
2887 weston_subsurface_unlink_parent(sub);
2888
Jason Ekstrand7b982072014-05-20 14:33:03 -05002889 weston_surface_state_fini(&sub->cached);
2890 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002891
2892 sub->surface->configure = NULL;
2893 sub->surface->configure_private = NULL;
2894 } else {
2895 /* the dummy weston_subsurface for the parent itself */
2896 assert(sub->parent_destroy_listener.notify == NULL);
2897 wl_list_remove(&sub->parent_link);
2898 wl_list_remove(&sub->parent_link_pending);
2899 }
2900
2901 wl_list_remove(&sub->surface_destroy_listener.link);
2902 free(sub);
2903}
2904
2905static const struct wl_subsurface_interface subsurface_implementation = {
2906 subsurface_destroy,
2907 subsurface_set_position,
2908 subsurface_place_above,
2909 subsurface_place_below,
2910 subsurface_set_sync,
2911 subsurface_set_desync
2912};
2913
2914static struct weston_subsurface *
2915weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2916 struct weston_surface *parent)
2917{
2918 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002919 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002920
2921 sub = calloc(1, sizeof *sub);
2922 if (!sub)
2923 return NULL;
2924
Jason Ekstranda7af7042013-10-12 22:38:11 -05002925 wl_list_init(&sub->unused_views);
2926
Jason Ekstranda85118c2013-06-27 20:17:02 -05002927 sub->resource =
2928 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929 if (!sub->resource) {
2930 free(sub);
2931 return NULL;
2932 }
2933
Jason Ekstranda85118c2013-06-27 20:17:02 -05002934 wl_resource_set_implementation(sub->resource,
2935 &subsurface_implementation,
2936 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002937 weston_subsurface_link_surface(sub, surface);
2938 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002939 weston_surface_state_init(&sub->cached);
2940 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002942
2943 return sub;
2944}
2945
2946/* Create a dummy subsurface for having the parent itself in its
2947 * sub-surface lists. Makes stacking order manipulation easy.
2948 */
2949static struct weston_subsurface *
2950weston_subsurface_create_for_parent(struct weston_surface *parent)
2951{
2952 struct weston_subsurface *sub;
2953
2954 sub = calloc(1, sizeof *sub);
2955 if (!sub)
2956 return NULL;
2957
2958 weston_subsurface_link_surface(sub, parent);
2959 sub->parent = parent;
2960 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2961 wl_list_insert(&parent->subsurface_list_pending,
2962 &sub->parent_link_pending);
2963
2964 return sub;
2965}
2966
2967static void
2968subcompositor_get_subsurface(struct wl_client *client,
2969 struct wl_resource *resource,
2970 uint32_t id,
2971 struct wl_resource *surface_resource,
2972 struct wl_resource *parent_resource)
2973{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002974 struct weston_surface *surface =
2975 wl_resource_get_user_data(surface_resource);
2976 struct weston_surface *parent =
2977 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002978 struct weston_subsurface *sub;
2979 static const char where[] = "get_subsurface: wl_subsurface@";
2980
2981 if (surface == parent) {
2982 wl_resource_post_error(resource,
2983 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2984 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002985 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002986 return;
2987 }
2988
2989 if (weston_surface_to_subsurface(surface)) {
2990 wl_resource_post_error(resource,
2991 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2992 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002993 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002994 return;
2995 }
2996
2997 if (surface->configure) {
2998 wl_resource_post_error(resource,
2999 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3000 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003001 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003002 return;
3003 }
3004
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003005 if (weston_surface_get_main_surface(parent) == surface) {
3006 wl_resource_post_error(resource,
3007 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3008 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003009 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003010 return;
3011 }
3012
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013 /* make sure the parent is in its own list */
3014 if (wl_list_empty(&parent->subsurface_list)) {
3015 if (!weston_subsurface_create_for_parent(parent)) {
3016 wl_resource_post_no_memory(resource);
3017 return;
3018 }
3019 }
3020
3021 sub = weston_subsurface_create(id, surface, parent);
3022 if (!sub) {
3023 wl_resource_post_no_memory(resource);
3024 return;
3025 }
3026
3027 surface->configure = subsurface_configure;
3028 surface->configure_private = sub;
3029}
3030
3031static void
3032subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3033{
3034 wl_resource_destroy(resource);
3035}
3036
3037static const struct wl_subcompositor_interface subcompositor_interface = {
3038 subcompositor_destroy,
3039 subcompositor_get_subsurface
3040};
3041
3042static void
3043bind_subcompositor(struct wl_client *client,
3044 void *data, uint32_t version, uint32_t id)
3045{
3046 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003047 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003048
Jason Ekstranda85118c2013-06-27 20:17:02 -05003049 resource =
3050 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003051 if (resource == NULL) {
3052 wl_client_post_no_memory(client);
3053 return;
3054 }
3055 wl_resource_set_implementation(resource, &subcompositor_interface,
3056 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003057}
3058
3059static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003060weston_compositor_dpms(struct weston_compositor *compositor,
3061 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003062{
3063 struct weston_output *output;
3064
3065 wl_list_for_each(output, &compositor->output_list, link)
3066 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003067 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003068}
3069
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003070WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003071weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003072{
Neil Roberts8b62e202013-09-30 13:14:47 +01003073 uint32_t old_state = compositor->state;
3074
3075 /* The state needs to be changed before emitting the wake
3076 * signal because that may try to schedule a repaint which
3077 * will not work if the compositor is still sleeping */
3078 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3079
3080 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003081 case WESTON_COMPOSITOR_SLEEPING:
3082 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3083 /* fall through */
3084 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003085 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003086 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003087 /* fall through */
3088 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003089 wl_event_source_timer_update(compositor->idle_source,
3090 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003091 }
3092}
3093
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003094WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003095weston_compositor_offscreen(struct weston_compositor *compositor)
3096{
3097 switch (compositor->state) {
3098 case WESTON_COMPOSITOR_OFFSCREEN:
3099 return;
3100 case WESTON_COMPOSITOR_SLEEPING:
3101 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3102 /* fall through */
3103 default:
3104 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3105 wl_event_source_timer_update(compositor->idle_source, 0);
3106 }
3107}
3108
3109WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003110weston_compositor_sleep(struct weston_compositor *compositor)
3111{
3112 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3113 return;
3114
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003115 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003116 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3117 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3118}
3119
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003120static int
3121idle_handler(void *data)
3122{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003123 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003124
3125 if (compositor->idle_inhibit)
3126 return 1;
3127
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003128 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003129 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003130
3131 return 1;
3132}
3133
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003134WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003135weston_plane_init(struct weston_plane *plane,
3136 struct weston_compositor *ec,
3137 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003138{
3139 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003140 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003141 plane->x = x;
3142 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003143 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003144
3145 /* Init the link so that the call to wl_list_remove() when releasing
3146 * the plane without ever stacking doesn't lead to a crash */
3147 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003148}
3149
3150WL_EXPORT void
3151weston_plane_release(struct weston_plane *plane)
3152{
Xiong Zhang97116532013-10-23 13:58:31 +08003153 struct weston_view *view;
3154
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003155 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003156 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003157
Xiong Zhang97116532013-10-23 13:58:31 +08003158 wl_list_for_each(view, &plane->compositor->view_list, link) {
3159 if (view->plane == plane)
3160 view->plane = NULL;
3161 }
3162
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003163 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003164}
3165
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003166WL_EXPORT void
3167weston_compositor_stack_plane(struct weston_compositor *ec,
3168 struct weston_plane *plane,
3169 struct weston_plane *above)
3170{
3171 if (above)
3172 wl_list_insert(above->link.prev, &plane->link);
3173 else
3174 wl_list_insert(&ec->plane_list, &plane->link);
3175}
3176
Casey Dahlin9074db52012-04-19 22:50:09 -04003177static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003178{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003179 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003180}
3181
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003182static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003183bind_output(struct wl_client *client,
3184 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003186 struct weston_output *output = data;
3187 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003188 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003189
Jason Ekstranda85118c2013-06-27 20:17:02 -05003190 resource = wl_resource_create(client, &wl_output_interface,
3191 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003192 if (resource == NULL) {
3193 wl_client_post_no_memory(client);
3194 return;
3195 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003196
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003197 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003198 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003199
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003200 wl_output_send_geometry(resource,
3201 output->x,
3202 output->y,
3203 output->mm_width,
3204 output->mm_height,
3205 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003206 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003207 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003208 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003209 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003210 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003211
3212 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003213 wl_output_send_mode(resource,
3214 mode->flags,
3215 mode->width,
3216 mode->height,
3217 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003218 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003219
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003220 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003221 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003222}
3223
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003224/* Move other outputs when one is removed so the space remains contiguos. */
3225static void
3226weston_compositor_remove_output(struct weston_compositor *compositor,
3227 struct weston_output *remove_output)
3228{
3229 struct weston_output *output;
3230 int offset = 0;
3231
3232 wl_list_for_each(output, &compositor->output_list, link) {
3233 if (output == remove_output) {
3234 offset = output->width;
3235 continue;
3236 }
3237
3238 if (offset > 0) {
3239 weston_output_move(output,
3240 output->x - offset, output->y);
3241 output->dirty = 1;
3242 }
3243 }
3244}
3245
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003246WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003247weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003248{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003249 output->destroying = 1;
3250
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003251 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003252 wl_list_remove(&output->link);
3253
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003254 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003255 wl_signal_emit(&output->destroy_signal, output);
3256
Richard Hughesafe690c2013-05-02 10:10:04 +01003257 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003258 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003259 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003260 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003261
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003262 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003263}
3264
Scott Moreau1bad5db2012-08-18 01:04:05 -06003265static void
3266weston_output_compute_transform(struct weston_output *output)
3267{
3268 struct weston_matrix transform;
3269 int flip;
3270
3271 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003272 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003273
3274 switch(output->transform) {
3275 case WL_OUTPUT_TRANSFORM_FLIPPED:
3276 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3277 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3278 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003279 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003280 flip = -1;
3281 break;
3282 default:
3283 flip = 1;
3284 break;
3285 }
3286
3287 switch(output->transform) {
3288 case WL_OUTPUT_TRANSFORM_NORMAL:
3289 case WL_OUTPUT_TRANSFORM_FLIPPED:
3290 transform.d[0] = flip;
3291 transform.d[1] = 0;
3292 transform.d[4] = 0;
3293 transform.d[5] = 1;
3294 break;
3295 case WL_OUTPUT_TRANSFORM_90:
3296 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3297 transform.d[0] = 0;
3298 transform.d[1] = -flip;
3299 transform.d[4] = 1;
3300 transform.d[5] = 0;
3301 break;
3302 case WL_OUTPUT_TRANSFORM_180:
3303 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3304 transform.d[0] = -flip;
3305 transform.d[1] = 0;
3306 transform.d[4] = 0;
3307 transform.d[5] = -1;
3308 break;
3309 case WL_OUTPUT_TRANSFORM_270:
3310 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3311 transform.d[0] = 0;
3312 transform.d[1] = flip;
3313 transform.d[4] = -1;
3314 transform.d[5] = 0;
3315 break;
3316 default:
3317 break;
3318 }
3319
3320 weston_matrix_multiply(&output->matrix, &transform);
3321}
3322
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003323WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003324weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003325{
Scott Moreau850ca422012-05-21 15:21:25 -06003326 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003327
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003328 weston_matrix_init(&output->matrix);
3329 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003330 -(output->x + output->width / 2.0),
3331 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003332
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003333 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003334 2.0 / output->width,
3335 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003336
Scott Moreauccbf29d2012-02-22 14:21:41 -07003337 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003338 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003339 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003340 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3341 output->zoom.trans_y, 0);
3342 weston_matrix_scale(&output->matrix, magnification,
3343 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003344 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003345
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003346 weston_output_compute_transform(output);
3347
Scott Moreauccbf29d2012-02-22 14:21:41 -07003348 output->dirty = 0;
3349}
3350
Scott Moreau1bad5db2012-08-18 01:04:05 -06003351static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003352weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003353{
3354 output->transform = transform;
3355
3356 switch (transform) {
3357 case WL_OUTPUT_TRANSFORM_90:
3358 case WL_OUTPUT_TRANSFORM_270:
3359 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3360 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3361 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003362 output->width = output->current_mode->height;
3363 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003364 break;
3365 case WL_OUTPUT_TRANSFORM_NORMAL:
3366 case WL_OUTPUT_TRANSFORM_180:
3367 case WL_OUTPUT_TRANSFORM_FLIPPED:
3368 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003369 output->width = output->current_mode->width;
3370 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003371 break;
3372 default:
3373 break;
3374 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003375
Hardening57388e42013-09-18 23:56:36 +02003376 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003377 output->width /= scale;
3378 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003379}
3380
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003381static void
3382weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003383{
3384 output->x = x;
3385 output->y = y;
3386
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003387 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003388 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003389 output->width,
3390 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003391}
3392
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003393WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003394weston_output_move(struct weston_output *output, int x, int y)
3395{
3396 pixman_region32_t old_region;
3397 struct wl_resource *resource;
3398
3399 output->move_x = x - output->x;
3400 output->move_y = y - output->y;
3401
3402 if (output->move_x == 0 && output->move_y == 0)
3403 return;
3404
3405 pixman_region32_init(&old_region);
3406 pixman_region32_copy(&old_region, &output->region);
3407
3408 weston_output_init_geometry(output, x, y);
3409
3410 output->dirty = 1;
3411
3412 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003413 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003414
3415 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003416 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003417 wl_output_send_geometry(resource,
3418 output->x,
3419 output->y,
3420 output->mm_width,
3421 output->mm_height,
3422 output->subpixel,
3423 output->make,
3424 output->model,
3425 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003426
3427 if (wl_resource_get_version(resource) >= 2)
3428 wl_output_send_done(resource);
3429 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003430}
3431
3432WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003433weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003434 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003435 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003436{
3437 output->compositor = c;
3438 output->x = x;
3439 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003440 output->mm_width = mm_width;
3441 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003442 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003443 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003444
Alexander Larsson0b135062013-05-28 16:23:36 +02003445 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003446 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003447
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003448 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003449 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003450
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003451 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003452 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003453 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003454 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003455
Casey Dahlin58ba1372012-04-19 22:50:08 -04003456 output->id = ffs(~output->compositor->output_id_pool) - 1;
3457 output->compositor->output_id_pool |= 1 << output->id;
3458
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003459 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003460 wl_global_create(c->wl_display, &wl_output_interface, 2,
3461 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003462 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003463}
3464
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003465WL_EXPORT void
3466weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003467 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003468 wl_fixed_t *x, wl_fixed_t *y)
3469{
3470 wl_fixed_t tx, ty;
3471 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003472 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003473
Hardeningff39efa2013-09-18 23:56:35 +02003474 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3475 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003476
3477 switch(output->transform) {
3478 case WL_OUTPUT_TRANSFORM_NORMAL:
3479 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003480 tx = device_x;
3481 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003482 break;
3483 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003484 tx = device_y;
3485 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003486 break;
3487 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003488 tx = width - device_x;
3489 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003490 break;
3491 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003492 tx = width - device_y;
3493 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003494 break;
3495 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003496 tx = width - device_x;
3497 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003498 break;
3499 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003500 tx = width - device_y;
3501 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003502 break;
3503 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003504 tx = device_x;
3505 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003506 break;
3507 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003508 tx = device_y;
3509 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003510 break;
3511 }
3512
Neil Robertseb5a2002014-05-01 16:13:55 +01003513 tx /= output->current_scale;
3514 ty /= output->current_scale;
3515
3516 if (output->zoom.active) {
3517 zoom_scale = output->zoom.spring_z.current;
3518 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3519 output->width / 2.0f *
3520 (zoom_scale + output->zoom.trans_x));
3521 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3522 output->height / 2.0f *
3523 (zoom_scale + output->zoom.trans_y));
3524 tx = wl_fixed_from_double(zx);
3525 ty = wl_fixed_from_double(zy);
3526 }
3527
3528 *x = tx + wl_fixed_from_int(output->x);
3529 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003530}
3531
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003532static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003533destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003534{
Jonny Lamb74130762013-11-26 18:19:46 +01003535 struct weston_surface *surface =
3536 wl_resource_get_user_data(resource);
3537
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003538 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003539 surface->pending.buffer_viewport.buffer.src_width =
3540 wl_fixed_from_int(-1);
3541 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003542 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003543}
3544
3545static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003546viewport_destroy(struct wl_client *client,
3547 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003548{
3549 wl_resource_destroy(resource);
3550}
3551
3552static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003553viewport_set(struct wl_client *client,
3554 struct wl_resource *resource,
3555 wl_fixed_t src_x,
3556 wl_fixed_t src_y,
3557 wl_fixed_t src_width,
3558 wl_fixed_t src_height,
3559 int32_t dst_width,
3560 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003561{
Jonny Lamb74130762013-11-26 18:19:46 +01003562 struct weston_surface *surface =
3563 wl_resource_get_user_data(resource);
3564
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003565 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003566
Jonny Lamb8ae35902013-11-26 18:19:45 +01003567 if (wl_fixed_to_double(src_width) < 0 ||
3568 wl_fixed_to_double(src_height) < 0) {
3569 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003570 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003571 "source dimensions must be non-negative (%fx%f)",
3572 wl_fixed_to_double(src_width),
3573 wl_fixed_to_double(src_height));
3574 return;
3575 }
3576
3577 if (dst_width <= 0 || dst_height <= 0) {
3578 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003579 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003580 "destination dimensions must be positive (%dx%d)",
3581 dst_width, dst_height);
3582 return;
3583 }
3584
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003585 surface->pending.buffer_viewport.buffer.src_x = src_x;
3586 surface->pending.buffer_viewport.buffer.src_y = src_y;
3587 surface->pending.buffer_viewport.buffer.src_width = src_width;
3588 surface->pending.buffer_viewport.buffer.src_height = src_height;
3589 surface->pending.buffer_viewport.surface.width = dst_width;
3590 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003591 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003592}
3593
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003594static void
3595viewport_set_source(struct wl_client *client,
3596 struct wl_resource *resource,
3597 wl_fixed_t src_x,
3598 wl_fixed_t src_y,
3599 wl_fixed_t src_width,
3600 wl_fixed_t src_height)
3601{
3602 struct weston_surface *surface =
3603 wl_resource_get_user_data(resource);
3604
3605 assert(surface->viewport_resource != NULL);
3606
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003607 if (src_width == wl_fixed_from_int(-1) &&
3608 src_height == wl_fixed_from_int(-1)) {
3609 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003610 surface->pending.buffer_viewport.buffer.src_width =
3611 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003612 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003613 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003614 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003615
3616 if (src_width <= 0 || src_height <= 0) {
3617 wl_resource_post_error(resource,
3618 WL_VIEWPORT_ERROR_BAD_VALUE,
3619 "source size must be positive (%fx%f)",
3620 wl_fixed_to_double(src_width),
3621 wl_fixed_to_double(src_height));
3622 return;
3623 }
3624
3625 surface->pending.buffer_viewport.buffer.src_x = src_x;
3626 surface->pending.buffer_viewport.buffer.src_y = src_y;
3627 surface->pending.buffer_viewport.buffer.src_width = src_width;
3628 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003629 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003630}
3631
3632static void
3633viewport_set_destination(struct wl_client *client,
3634 struct wl_resource *resource,
3635 int32_t dst_width,
3636 int32_t dst_height)
3637{
3638 struct weston_surface *surface =
3639 wl_resource_get_user_data(resource);
3640
3641 assert(surface->viewport_resource != NULL);
3642
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003643 if (dst_width == -1 && dst_height == -1) {
3644 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003645 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003646 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003647 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003648 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003649
3650 if (dst_width <= 0 || dst_height <= 0) {
3651 wl_resource_post_error(resource,
3652 WL_VIEWPORT_ERROR_BAD_VALUE,
3653 "destination size must be positive (%dx%d)",
3654 dst_width, dst_height);
3655 return;
3656 }
3657
3658 surface->pending.buffer_viewport.surface.width = dst_width;
3659 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003660 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003661}
3662
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003663static const struct wl_viewport_interface viewport_interface = {
3664 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003665 viewport_set,
3666 viewport_set_source,
3667 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003668};
3669
3670static void
3671scaler_destroy(struct wl_client *client,
3672 struct wl_resource *resource)
3673{
3674 wl_resource_destroy(resource);
3675}
3676
3677static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003678scaler_get_viewport(struct wl_client *client,
3679 struct wl_resource *scaler,
3680 uint32_t id,
3681 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003682{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003683 int version = wl_resource_get_version(scaler);
3684 struct weston_surface *surface =
3685 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003686 struct wl_resource *resource;
3687
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003688 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003689 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003690 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3691 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003692 return;
3693 }
3694
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003695 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003696 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003697 if (resource == NULL) {
3698 wl_client_post_no_memory(client);
3699 return;
3700 }
3701
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003702 wl_resource_set_implementation(resource, &viewport_interface,
3703 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003704
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003705 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003706}
3707
3708static const struct wl_scaler_interface scaler_interface = {
3709 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003710 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003711};
3712
3713static void
3714bind_scaler(struct wl_client *client,
3715 void *data, uint32_t version, uint32_t id)
3716{
3717 struct wl_resource *resource;
3718
3719 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003720 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003721 if (resource == NULL) {
3722 wl_client_post_no_memory(client);
3723 return;
3724 }
3725
3726 wl_resource_set_implementation(resource, &scaler_interface,
3727 NULL, NULL);
3728}
3729
3730static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003731compositor_bind(struct wl_client *client,
3732 void *data, uint32_t version, uint32_t id)
3733{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003734 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003735 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003736
Jason Ekstranda85118c2013-06-27 20:17:02 -05003737 resource = wl_resource_create(client, &wl_compositor_interface,
3738 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003739 if (resource == NULL) {
3740 wl_client_post_no_memory(client);
3741 return;
3742 }
3743
3744 wl_resource_set_implementation(resource, &compositor_interface,
3745 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003746}
3747
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003748static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003749log_uname(void)
3750{
3751 struct utsname usys;
3752
3753 uname(&usys);
3754
3755 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3756 usys.version, usys.machine);
3757}
3758
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003759WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003760weston_environment_get_fd(const char *env)
3761{
3762 char *e, *end;
3763 int fd, flags;
3764
3765 e = getenv(env);
3766 if (!e)
3767 return -1;
3768 fd = strtol(e, &end, 0);
3769 if (*end != '\0')
3770 return -1;
3771
3772 flags = fcntl(fd, F_GETFD);
3773 if (flags == -1)
3774 return -1;
3775
3776 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3777 unsetenv(env);
3778
3779 return fd;
3780}
3781
3782WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003783weston_compositor_init(struct weston_compositor *ec,
3784 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003785 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003786 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003787{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003788 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003789 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003790 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003791
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003792 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003793 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003794 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003795 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003796 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003797 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003798 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003799 wl_signal_init(&ec->idle_signal);
3800 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003801 wl_signal_init(&ec->show_input_panel_signal);
3802 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003803 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003804 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003805 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003806 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003807 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003808 wl_signal_init(&ec->session_signal);
3809 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003810
Casey Dahlin58ba1372012-04-19 22:50:08 -04003811 ec->output_id_pool = 0;
3812
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003813 if (!wl_global_create(display, &wl_compositor_interface, 3,
3814 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003815 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003816
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003817 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3818 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003819 return -1;
3820
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003821 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003822 ec, bind_scaler))
3823 return -1;
3824
Jason Ekstranda7af7042013-10-12 22:38:11 -05003825 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003826 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003827 wl_list_init(&ec->layer_list);
3828 wl_list_init(&ec->seat_list);
3829 wl_list_init(&ec->output_list);
3830 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003831 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003832 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003833 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003834 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003835 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003836
Xiong Zhang97116532013-10-23 13:58:31 +08003837 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003838 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003839
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003840 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3841 weston_config_section_get_string(s, "keymap_rules",
3842 (char **) &xkb_names.rules, NULL);
3843 weston_config_section_get_string(s, "keymap_model",
3844 (char **) &xkb_names.model, NULL);
3845 weston_config_section_get_string(s, "keymap_layout",
3846 (char **) &xkb_names.layout, NULL);
3847 weston_config_section_get_string(s, "keymap_variant",
3848 (char **) &xkb_names.variant, NULL);
3849 weston_config_section_get_string(s, "keymap_options",
3850 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003851
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003852 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3853 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003854
Jonny Lamb66a41a02014-08-12 14:58:25 +02003855 weston_config_section_get_int(s, "repeat-rate",
3856 &ec->kb_repeat_rate, 40);
3857 weston_config_section_get_int(s, "repeat-delay",
3858 &ec->kb_repeat_delay, 400);
3859
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003860 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003861
3862 wl_data_device_manager_init(ec->wl_display);
3863
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003864 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003865
Daniel Stone725c2c32012-06-22 14:04:36 +01003866 loop = wl_display_get_event_loop(ec->wl_display);
3867 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3868 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3869
3870 ec->input_loop = wl_event_loop_create();
3871
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003872 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3873 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3874
3875 weston_compositor_schedule_repaint(ec);
3876
Daniel Stone725c2c32012-06-22 14:04:36 +01003877 return 0;
3878}
3879
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003880WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003881weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003882{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003883 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003884
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003885 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003886 if (ec->input_loop_source)
3887 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003888
Matt Roper361d2ad2011-08-29 13:52:23 -07003889 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003890 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003891 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003892
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003893 if (ec->renderer)
3894 ec->renderer->destroy(ec);
3895
Daniel Stone325fc2d2012-05-30 16:31:58 +01003896 weston_binding_list_destroy_all(&ec->key_binding_list);
3897 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003898 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003899 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003900 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003901
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003902 weston_plane_release(&ec->primary_plane);
3903
Jonas Ådahlc97af922012-03-28 22:36:09 +02003904 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003905
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003906 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003907}
3908
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003909WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003910weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3911 const struct weston_pointer_grab_interface *interface)
3912{
3913 struct weston_seat *seat;
3914
3915 ec->default_pointer_grab = interface;
3916 wl_list_for_each(seat, &ec->seat_list, link) {
3917 if (seat->pointer) {
3918 weston_pointer_set_default_grab(seat->pointer,
3919 interface);
3920 }
3921 }
3922}
3923
3924WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003925weston_version(int *major, int *minor, int *micro)
3926{
3927 *major = WESTON_VERSION_MAJOR;
3928 *minor = WESTON_VERSION_MINOR;
3929 *micro = WESTON_VERSION_MICRO;
3930}
3931
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003932static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003933 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003934 const char *desc;
3935} capability_strings[] = {
3936 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003937 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003938};
3939
3940static void
3941weston_compositor_log_capabilities(struct weston_compositor *compositor)
3942{
3943 unsigned i;
3944 int yes;
3945
3946 weston_log("Compositor capabilities:\n");
3947 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3948 yes = compositor->capabilities & capability_strings[i].bit;
3949 weston_log_continue(STAMP_SPACE "%s %s\n",
3950 capability_strings[i].desc,
3951 yes ? "yes" : "no");
3952 }
3953}
3954
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003955static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003956{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003957 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003958
Martin Minarik6d118362012-06-07 18:01:59 +02003959 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003960 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003961
3962 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003963}
3964
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003965#ifdef HAVE_LIBUNWIND
3966
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003967static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003968print_backtrace(void)
3969{
3970 unw_cursor_t cursor;
3971 unw_context_t context;
3972 unw_word_t off;
3973 unw_proc_info_t pip;
3974 int ret, i = 0;
3975 char procname[256];
3976 const char *filename;
3977 Dl_info dlinfo;
3978
3979 pip.unwind_info = NULL;
3980 ret = unw_getcontext(&context);
3981 if (ret) {
3982 weston_log("unw_getcontext: %d\n", ret);
3983 return;
3984 }
3985
3986 ret = unw_init_local(&cursor, &context);
3987 if (ret) {
3988 weston_log("unw_init_local: %d\n", ret);
3989 return;
3990 }
3991
3992 ret = unw_step(&cursor);
3993 while (ret > 0) {
3994 ret = unw_get_proc_info(&cursor, &pip);
3995 if (ret) {
3996 weston_log("unw_get_proc_info: %d\n", ret);
3997 break;
3998 }
3999
4000 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4001 if (ret && ret != -UNW_ENOMEM) {
4002 if (ret != -UNW_EUNSPEC)
4003 weston_log("unw_get_proc_name: %d\n", ret);
4004 procname[0] = '?';
4005 procname[1] = 0;
4006 }
4007
4008 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4009 *dlinfo.dli_fname)
4010 filename = dlinfo.dli_fname;
4011 else
4012 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004013
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004014 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4015 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4016
4017 ret = unw_step(&cursor);
4018 if (ret < 0)
4019 weston_log("unw_step: %d\n", ret);
4020 }
4021}
4022
4023#else
4024
4025static void
4026print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004027{
4028 void *buffer[32];
4029 int i, count;
4030 Dl_info info;
4031
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004032 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4033 for (i = 0; i < count; i++) {
4034 dladdr(buffer[i], &info);
4035 weston_log(" [%016lx] %s (%s)\n",
4036 (long) buffer[i],
4037 info.dli_sname ? info.dli_sname : "--",
4038 info.dli_fname);
4039 }
4040}
4041
4042#endif
4043
4044static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004045on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004046{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004047 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004048 * then call the backend restore function, which will switch
4049 * back to the vt we launched from or ungrab X etc and then
4050 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004051 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004052 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004053 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004054
Peter Maatmane5b42e42013-03-27 22:38:53 +01004055 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004056
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004057 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004058
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004059 segv_compositor->restore(segv_compositor);
4060
4061 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004062}
4063
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004064WL_EXPORT void *
4065weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004066{
4067 char path[PATH_MAX];
4068 void *module, *init;
4069
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004070 if (name == NULL)
4071 return NULL;
4072
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004073 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004074 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004075 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004076 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004077
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004078 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4079 if (module) {
4080 weston_log("Module '%s' already loaded\n", path);
4081 dlclose(module);
4082 return NULL;
4083 }
4084
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004085 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004086 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004087 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004088 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004089 return NULL;
4090 }
4091
4092 init = dlsym(module, entrypoint);
4093 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004094 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004095 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004096 return NULL;
4097 }
4098
4099 return init;
4100}
4101
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004102static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004103load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004104 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004105{
4106 const char *p, *end;
4107 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004108 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004109 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004110
4111 if (modules == NULL)
4112 return 0;
4113
4114 p = modules;
4115 while (*p) {
4116 end = strchrnul(p, ',');
4117 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004118 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004119 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004120 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004121 p = end;
4122 while (*p == ',')
4123 p++;
4124
4125 }
4126
4127 return 0;
4128}
4129
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004130static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004131 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4132
4133static const char xdg_wrong_message[] =
4134 "fatal: environment variable XDG_RUNTIME_DIR\n"
4135 "is set to \"%s\", which is not a directory.\n";
4136
4137static const char xdg_wrong_mode_message[] =
4138 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004139 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4140 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004141
4142static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004143 "Refer to your distribution on how to get it, or\n"
4144 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4145 "on how to implement it.\n";
4146
Martin Minarik37032f82012-06-18 20:15:18 +02004147static void
4148verify_xdg_runtime_dir(void)
4149{
4150 char *dir = getenv("XDG_RUNTIME_DIR");
4151 struct stat s;
4152
4153 if (!dir) {
4154 weston_log(xdg_error_message);
4155 weston_log_continue(xdg_detail_message);
4156 exit(EXIT_FAILURE);
4157 }
4158
4159 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4160 weston_log(xdg_wrong_message, dir);
4161 weston_log_continue(xdg_detail_message);
4162 exit(EXIT_FAILURE);
4163 }
4164
4165 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4166 weston_log(xdg_wrong_mode_message,
4167 dir, s.st_mode & 0777, s.st_uid);
4168 weston_log_continue(xdg_detail_message);
4169 }
4170}
4171
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004172static int
4173usage(int error_code)
4174{
4175 fprintf(stderr,
4176 "Usage: weston [OPTIONS]\n\n"
4177 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4178 "Weston supports multiple backends, and depending on which backend is in use\n"
4179 "different options will be accepted.\n\n"
4180
4181
4182 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004183 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004184 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004185 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4186 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004187 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004188 " -S, --socket=NAME\tName of socket to listen on\n"
4189 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004190 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004191 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004192 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004193 " -h, --help\t\tThis help message\n\n");
4194
4195 fprintf(stderr,
4196 "Options for drm-backend.so:\n\n"
4197 " --connector=ID\tBring up only this connector\n"
4198 " --seat=SEAT\t\tThe seat that weston should run on\n"
4199 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004200 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004201 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4202
4203 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004204 "Options for fbdev-backend.so:\n\n"
4205 " --tty=TTY\t\tThe tty to use\n"
4206 " --device=DEVICE\tThe framebuffer device to use\n\n");
4207
4208 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004209 "Options for x11-backend.so:\n\n"
4210 " --width=WIDTH\t\tWidth of X window\n"
4211 " --height=HEIGHT\tHeight of X window\n"
4212 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004213 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004214 " --output-count=COUNT\tCreate multiple outputs\n"
4215 " --no-input\t\tDont create input devices\n\n");
4216
4217 fprintf(stderr,
4218 "Options for wayland-backend.so:\n\n"
4219 " --width=WIDTH\t\tWidth of Wayland surface\n"
4220 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004221 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004222 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004223 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004224 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004225 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004226 " --display=DISPLAY\tWayland display to connect to\n\n");
4227
Pekka Paalanene31e0532013-05-22 18:03:07 +03004228#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4229 fprintf(stderr,
4230 "Options for rpi-backend.so:\n\n"
4231 " --tty=TTY\t\tThe tty to use\n"
4232 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4233 " --transform=TR\tThe output transformation, TR is one of:\n"
4234 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004235 " --opaque-regions\tEnable support for opaque regions, can be "
4236 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004237 "\n");
4238#endif
4239
Hardeningc077a842013-07-08 00:51:35 +02004240#if defined(BUILD_RDP_COMPOSITOR)
4241 fprintf(stderr,
4242 "Options for rdp-backend.so:\n\n"
4243 " --width=WIDTH\t\tWidth of desktop\n"
4244 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004245 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4246 " --address=ADDR\tThe address to bind\n"
4247 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004248 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004249 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4250 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4251 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4252 "\n");
4253#endif
4254
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004255 exit(error_code);
4256}
4257
Peter Maatmane5b42e42013-03-27 22:38:53 +01004258static void
4259catch_signals(void)
4260{
4261 struct sigaction action;
4262
4263 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4264 action.sa_sigaction = on_caught_signal;
4265 sigemptyset(&action.sa_mask);
4266 sigaction(SIGSEGV, &action, NULL);
4267 sigaction(SIGABRT, &action, NULL);
4268}
4269
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004270static void
4271handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4272{
4273 struct wl_client *client = data;
4274
4275 weston_log("Primary client died. Closing...\n");
4276
4277 wl_display_terminate(wl_client_get_display(client));
4278}
4279
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004280int main(int argc, char *argv[])
4281{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004282 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004283 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004284 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004285 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004286 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004287 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004288 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004289 int *argc, char *argv[],
4290 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004291 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004292 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004293 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004294 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004295 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004296 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004297 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004298 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004299 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004300 int32_t help = 0;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004301 const char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004302 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004303 int32_t noconfig = 0;
4304 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004305 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004306 struct wl_client *primary_client;
4307 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004308
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004309 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004310 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004311 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004312 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4313 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004314 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004315 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004316 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004317 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004318 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004319 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004320
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004321 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004322
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004323 if (help)
4324 usage(EXIT_SUCCESS);
4325
Scott Moreau12245142012-08-29 15:15:58 -06004326 if (version) {
4327 printf(PACKAGE_STRING "\n");
4328 return EXIT_SUCCESS;
4329 }
4330
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004331 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004332
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004333 weston_log("%s\n"
4334 STAMP_SPACE "%s\n"
4335 STAMP_SPACE "Bug reports to: %s\n"
4336 STAMP_SPACE "Build: %s\n",
4337 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004338 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004339 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004340
Martin Minarik37032f82012-06-18 20:15:18 +02004341 verify_xdg_runtime_dir();
4342
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004343 display = wl_display_create();
4344
Tiago Vignatti2116b892011-08-08 05:52:59 -07004345 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004346 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4347 display);
4348 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4349 display);
4350 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4351 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004352
4353 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004354 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4355 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004356
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304357 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4358 ret = EXIT_FAILURE;
4359 goto out_signals;
4360 }
4361
Pekka Paalanen588bee12014-05-07 16:26:25 +03004362 if (noconfig == 0)
4363 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004364 if (config != NULL) {
4365 weston_log("Using config file '%s'\n",
4366 weston_config_get_full_path(config));
4367 } else {
4368 weston_log("Starting with no config file.\n");
4369 }
4370 section = weston_config_get_section(config, "core", NULL, NULL);
4371
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004372 if (option_backend)
4373 backend = strdup(option_backend);
4374 else
4375 weston_config_section_get_string(section, "backend", &backend,
4376 NULL);
4377
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004378 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004379 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004380 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004381 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004382 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004383 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004384 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004385 }
4386
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004387 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004388 free(backend);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304389 if (!backend_init) {
4390 ret = EXIT_FAILURE;
4391 goto out_signals;
4392 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004393
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004394 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004395 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004396 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304397 ret = EXIT_FAILURE;
4398 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004399 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004400
Peter Maatmane5b42e42013-03-27 22:38:53 +01004401 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004402 segv_compositor = ec;
4403
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004404 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004405 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004406
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004407 for (i = 1; i < argc; i++)
4408 weston_log("fatal: unhandled option: %s\n", argv[i]);
4409 if (argc > 1) {
4410 ret = EXIT_FAILURE;
4411 goto out;
4412 }
4413
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004414 weston_compositor_log_capabilities(ec);
4415
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004416 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4417 if (server_socket) {
4418 weston_log("Running with single client\n");
4419 fd = strtol(server_socket, &end, 0);
4420 if (*end != '\0')
4421 fd = -1;
4422 } else {
4423 fd = -1;
4424 }
4425
4426 if (fd != -1) {
4427 primary_client = wl_client_create(display, fd);
4428 if (!primary_client) {
4429 weston_log("fatal: failed to add client: %m\n");
4430 ret = EXIT_FAILURE;
4431 goto out;
4432 }
4433 primary_client_destroyed.notify =
4434 handle_primary_client_destroyed;
4435 wl_client_add_destroy_listener(primary_client,
4436 &primary_client_destroyed);
4437 } else {
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004438 if (socket_name) {
4439 if (wl_display_add_socket(display, socket_name)) {
4440 weston_log("fatal: failed to add socket: %m\n");
4441 ret = EXIT_FAILURE;
4442 goto out;
4443 }
4444 } else {
4445 socket_name = wl_display_add_socket_auto(display);
4446 if (!socket_name) {
4447 weston_log("fatal: failed to add socket: %m\n");
4448 ret = EXIT_FAILURE;
4449 goto out;
4450 }
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004451 }
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004452
4453 setenv("WAYLAND_DISPLAY", socket_name, 1);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004454 }
4455
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004456 if (option_shell)
4457 shell = strdup(option_shell);
4458 else
4459 weston_config_section_get_string(section, "shell", &shell,
4460 "desktop-shell.so");
4461
4462 if (load_modules(ec, shell, &argc, argv) < 0) {
4463 free(shell);
4464 goto out;
4465 }
4466 free(shell);
4467
4468 weston_config_section_get_string(section, "modules", &modules, "");
4469 if (load_modules(ec, modules, &argc, argv) < 0) {
4470 free(modules);
4471 goto out;
4472 }
4473 free(modules);
4474
4475 if (load_modules(ec, option_modules, &argc, argv) < 0)
4476 goto out;
4477
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004478 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004479
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004480 wl_display_run(display);
4481
4482 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004483 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004484 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004485
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004486 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004487
Daniel Stone855028f2012-05-01 20:37:10 +01004488 weston_compositor_xkb_destroy(ec);
4489
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004490 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304491
4492out_signals:
4493 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4494 if (signals[i])
4495 wl_event_source_remove(signals[i]);
4496
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004497 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004498
Martin Minarik19e6f262012-06-07 13:08:46 +02004499 weston_log_file_close();
4500
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004501 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004502}