blob: fa8730f46c5aaed499258b850ee116f1f1303b25 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010056#include "scaler-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
Hardening57388e42013-09-18 23:56:36 +0200141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300323surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
324{
325 struct weston_surface *surface =
326 container_of(listener, struct weston_surface,
327 pending.buffer_destroy_listener);
328
329 surface->pending.buffer = NULL;
330}
331
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200332static void
333empty_region(pixman_region32_t *region)
334{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300335 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200336 pixman_region32_init(region);
337}
338
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300339static void
340region_init_infinite(pixman_region32_t *region)
341{
342 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
343 UINT32_MAX, UINT32_MAX);
344}
345
Pekka Paalanene67858b2013-04-25 13:57:42 +0300346static struct weston_subsurface *
347weston_surface_to_subsurface(struct weston_surface *surface);
348
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349WL_EXPORT struct weston_view *
350weston_view_create(struct weston_surface *surface)
351{
352 struct weston_view *view;
353
354 view = calloc(1, sizeof *view);
355 if (view == NULL)
356 return NULL;
357
358 view->surface = surface;
359
Jason Ekstranda7af7042013-10-12 22:38:11 -0500360 /* Assign to surface */
361 wl_list_insert(&surface->views, &view->surface_link);
362
363 wl_signal_init(&view->destroy_signal);
364 wl_list_init(&view->link);
365 wl_list_init(&view->layer_link);
366
Xiong Zhang97116532013-10-23 13:58:31 +0800367 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368
369 pixman_region32_init(&view->clip);
370
371 view->alpha = 1.0;
372 pixman_region32_init(&view->transform.opaque);
373
374 wl_list_init(&view->geometry.transformation_list);
375 wl_list_insert(&view->geometry.transformation_list,
376 &view->transform.position.link);
377 weston_matrix_init(&view->transform.position.matrix);
378 wl_list_init(&view->geometry.child_list);
379 pixman_region32_init(&view->transform.boundingbox);
380 view->transform.dirty = 1;
381
382 view->output = NULL;
383
384 return view;
385}
386
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500387WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500388weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500389{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500390 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400391
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200392 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400393 if (surface == NULL)
394 return NULL;
395
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500396 wl_signal_init(&surface->destroy_signal);
397
398 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500399
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500400 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200401 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400402
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200403 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
404 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200405 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
406 surface->buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +0200407 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100408 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400409 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100410 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200411
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200412 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100413
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400414 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500415 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300416 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400417
Jason Ekstranda7af7042013-10-12 22:38:11 -0500418 wl_list_init(&surface->views);
419
420 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500421
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300422 surface->pending.buffer_destroy_listener.notify =
423 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300424 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300425 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300426 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300427 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300428
Pekka Paalanene67858b2013-04-25 13:57:42 +0300429 wl_list_init(&surface->subsurface_list);
430 wl_list_init(&surface->subsurface_list_pending);
431
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400432 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500433}
434
Alex Wu8811bf92012-02-28 18:07:54 +0800435WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500436weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200437 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500438{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100439 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500440}
441
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400442WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443weston_view_to_global_float(struct weston_view *view,
444 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200445{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500446 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200447 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
448
Jason Ekstranda7af7042013-10-12 22:38:11 -0500449 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200450
451 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200452 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700453 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200454 v.f[3]);
455 *x = 0;
456 *y = 0;
457 return;
458 }
459
460 *x = v.f[0] / v.f[3];
461 *y = v.f[1] / v.f[3];
462 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500463 *x = sx + view->geometry.x;
464 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200465 }
466}
467
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500468WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200469weston_transformed_coord(int width, int height,
470 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200471 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200472 float sx, float sy, float *bx, float *by)
473{
474 switch (transform) {
475 case WL_OUTPUT_TRANSFORM_NORMAL:
476 default:
477 *bx = sx;
478 *by = sy;
479 break;
480 case WL_OUTPUT_TRANSFORM_FLIPPED:
481 *bx = width - sx;
482 *by = sy;
483 break;
484 case WL_OUTPUT_TRANSFORM_90:
485 *bx = height - sy;
486 *by = sx;
487 break;
488 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
489 *bx = height - sy;
490 *by = width - sx;
491 break;
492 case WL_OUTPUT_TRANSFORM_180:
493 *bx = width - sx;
494 *by = height - sy;
495 break;
496 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
497 *bx = sx;
498 *by = height - sy;
499 break;
500 case WL_OUTPUT_TRANSFORM_270:
501 *bx = sy;
502 *by = width - sx;
503 break;
504 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
505 *bx = sy;
506 *by = sx;
507 break;
508 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200509
510 *bx *= scale;
511 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200512}
513
514WL_EXPORT pixman_box32_t
515weston_transformed_rect(int width, int height,
516 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200517 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200518 pixman_box32_t rect)
519{
520 float x1, x2, y1, y2;
521
522 pixman_box32_t ret;
523
Alexander Larsson4ea95522013-05-22 14:41:37 +0200524 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200525 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200526 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200527 rect.x2, rect.y2, &x2, &y2);
528
529 if (x1 <= x2) {
530 ret.x1 = x1;
531 ret.x2 = x2;
532 } else {
533 ret.x1 = x2;
534 ret.x2 = x1;
535 }
536
537 if (y1 <= y2) {
538 ret.y1 = y1;
539 ret.y2 = y2;
540 } else {
541 ret.y1 = y2;
542 ret.y2 = y1;
543 }
544
545 return ret;
546}
547
548WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500549weston_transformed_region(int width, int height,
550 enum wl_output_transform transform,
551 int32_t scale,
552 pixman_region32_t *src, pixman_region32_t *dest)
553{
554 pixman_box32_t *src_rects, *dest_rects;
555 int nrects, i;
556
557 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
558 if (src != dest)
559 pixman_region32_copy(dest, src);
560 return;
561 }
562
563 src_rects = pixman_region32_rectangles(src, &nrects);
564 dest_rects = malloc(nrects * sizeof(*dest_rects));
565 if (!dest_rects)
566 return;
567
568 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
569 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
570 } else {
571 for (i = 0; i < nrects; i++) {
572 switch (transform) {
573 default:
574 case WL_OUTPUT_TRANSFORM_NORMAL:
575 dest_rects[i].x1 = src_rects[i].x1;
576 dest_rects[i].y1 = src_rects[i].y1;
577 dest_rects[i].x2 = src_rects[i].x2;
578 dest_rects[i].y2 = src_rects[i].y2;
579 break;
580 case WL_OUTPUT_TRANSFORM_90:
581 dest_rects[i].x1 = height - src_rects[i].y2;
582 dest_rects[i].y1 = src_rects[i].x1;
583 dest_rects[i].x2 = height - src_rects[i].y1;
584 dest_rects[i].y2 = src_rects[i].x2;
585 break;
586 case WL_OUTPUT_TRANSFORM_180:
587 dest_rects[i].x1 = width - src_rects[i].x2;
588 dest_rects[i].y1 = height - src_rects[i].y2;
589 dest_rects[i].x2 = width - src_rects[i].x1;
590 dest_rects[i].y2 = height - src_rects[i].y1;
591 break;
592 case WL_OUTPUT_TRANSFORM_270:
593 dest_rects[i].x1 = src_rects[i].y1;
594 dest_rects[i].y1 = width - src_rects[i].x2;
595 dest_rects[i].x2 = src_rects[i].y2;
596 dest_rects[i].y2 = width - src_rects[i].x1;
597 break;
598 case WL_OUTPUT_TRANSFORM_FLIPPED:
599 dest_rects[i].x1 = width - src_rects[i].x2;
600 dest_rects[i].y1 = src_rects[i].y1;
601 dest_rects[i].x2 = width - src_rects[i].x1;
602 dest_rects[i].y2 = src_rects[i].y2;
603 break;
604 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
605 dest_rects[i].x1 = height - src_rects[i].y2;
606 dest_rects[i].y1 = width - src_rects[i].x2;
607 dest_rects[i].x2 = height - src_rects[i].y1;
608 dest_rects[i].y2 = width - src_rects[i].x1;
609 break;
610 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
611 dest_rects[i].x1 = src_rects[i].x1;
612 dest_rects[i].y1 = height - src_rects[i].y2;
613 dest_rects[i].x2 = src_rects[i].x2;
614 dest_rects[i].y2 = height - src_rects[i].y1;
615 break;
616 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
617 dest_rects[i].x1 = src_rects[i].y1;
618 dest_rects[i].y1 = src_rects[i].x1;
619 dest_rects[i].x2 = src_rects[i].y2;
620 dest_rects[i].y2 = src_rects[i].x2;
621 break;
622 }
623 }
624 }
625
626 if (scale != 1) {
627 for (i = 0; i < nrects; i++) {
628 dest_rects[i].x1 *= scale;
629 dest_rects[i].x2 *= scale;
630 dest_rects[i].y1 *= scale;
631 dest_rects[i].y2 *= scale;
632 }
633 }
634
635 pixman_region32_clear(dest);
636 pixman_region32_init_rects(dest, dest_rects, nrects);
637 free(dest_rects);
638}
639
Jonny Lamb74130762013-11-26 18:19:46 +0100640static void
641scaler_surface_to_buffer(struct weston_surface *surface,
642 float sx, float sy, float *bx, float *by)
643{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200644 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200645 double src_width, src_height;
646 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200647
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200648 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
649 if (vp->surface.width == -1) {
650 *bx = sx;
651 *by = sy;
652 return;
653 }
Jonny Lamb74130762013-11-26 18:19:46 +0100654
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200655 src_x = 0.0;
656 src_y = 0.0;
657 src_width = surface->width_from_buffer;
658 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100659 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200660 src_x = wl_fixed_to_double(vp->buffer.src_x);
661 src_y = wl_fixed_to_double(vp->buffer.src_y);
662 src_width = wl_fixed_to_double(vp->buffer.src_width);
663 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100664 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200665
666 *bx = sx * src_width / surface->width + src_x;
667 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100668}
669
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500670WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200671weston_surface_to_buffer_float(struct weston_surface *surface,
672 float sx, float sy, float *bx, float *by)
673{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200674 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
675
Jonny Lamb74130762013-11-26 18:19:46 +0100676 /* first transform coordinates if the scaler is set */
677 scaler_surface_to_buffer(surface, sx, sy, bx, by);
678
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500679 weston_transformed_coord(surface->width_from_buffer,
680 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200681 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100682 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200683}
684
Alexander Larsson4ea95522013-05-22 14:41:37 +0200685WL_EXPORT void
686weston_surface_to_buffer(struct weston_surface *surface,
687 int sx, int sy, int *bx, int *by)
688{
689 float bxf, byf;
690
Jonny Lamb74130762013-11-26 18:19:46 +0100691 weston_surface_to_buffer_float(surface,
692 sx, sy, &bxf, &byf);
693
Alexander Larsson4ea95522013-05-22 14:41:37 +0200694 *bx = floorf(bxf);
695 *by = floorf(byf);
696}
697
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200698WL_EXPORT pixman_box32_t
699weston_surface_to_buffer_rect(struct weston_surface *surface,
700 pixman_box32_t rect)
701{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200702 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100703 float xf, yf;
704
705 /* first transform box coordinates if the scaler is set */
706 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
707 rect.x1 = floorf(xf);
708 rect.y1 = floorf(yf);
709
710 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
711 rect.x2 = floorf(xf);
712 rect.y2 = floorf(yf);
713
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500714 return weston_transformed_rect(surface->width_from_buffer,
715 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200716 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200717 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200718}
719
720WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400722 struct weston_plane *plane)
723{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500724 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400725 return;
726
Jason Ekstranda7af7042013-10-12 22:38:11 -0500727 weston_view_damage_below(view);
728 view->plane = plane;
729 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400730}
731
732WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500733weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200734{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500735 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200736
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500737 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500738 pixman_region32_subtract(&damage, &view->transform.boundingbox,
739 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800740 if (view->plane)
741 pixman_region32_union(&view->plane->damage,
742 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500743 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800744 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200745}
746
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400747static void
748weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
749{
750 uint32_t different = es->output_mask ^ mask;
751 uint32_t entered = mask & different;
752 uint32_t left = es->output_mask & different;
753 struct weston_output *output;
754 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500755 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400756
757 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500758 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400759 return;
760 if (different == 0)
761 return;
762
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500763 client = wl_resource_get_client(es->resource);
764
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400765 wl_list_for_each(output, &es->compositor->output_list, link) {
766 if (1 << output->id & different)
767 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500768 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400769 client);
770 if (resource == NULL)
771 continue;
772 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500773 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400774 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500775 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400776 }
777}
778
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200779
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400780static void
781weston_surface_assign_output(struct weston_surface *es)
782{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783 struct weston_output *new_output;
784 struct weston_view *view;
785 pixman_region32_t region;
786 uint32_t max, area, mask;
787 pixman_box32_t *e;
788
789 new_output = NULL;
790 max = 0;
791 mask = 0;
792 pixman_region32_init(&region);
793 wl_list_for_each(view, &es->views, surface_link) {
794 if (!view->output)
795 continue;
796
797 pixman_region32_intersect(&region, &view->transform.boundingbox,
798 &view->output->region);
799
800 e = pixman_region32_extents(&region);
801 area = (e->x2 - e->x1) * (e->y2 - e->y1);
802
803 mask |= view->output_mask;
804
805 if (area >= max) {
806 new_output = view->output;
807 max = area;
808 }
809 }
810 pixman_region32_fini(&region);
811
812 es->output = new_output;
813 weston_surface_update_output_mask(es, mask);
814}
815
816static void
817weston_view_assign_output(struct weston_view *ev)
818{
819 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400820 struct weston_output *output, *new_output;
821 pixman_region32_t region;
822 uint32_t max, area, mask;
823 pixman_box32_t *e;
824
825 new_output = NULL;
826 max = 0;
827 mask = 0;
828 pixman_region32_init(&region);
829 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500830 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400831 &output->region);
832
833 e = pixman_region32_extents(&region);
834 area = (e->x2 - e->x1) * (e->y2 - e->y1);
835
836 if (area > 0)
837 mask |= 1 << output->id;
838
839 if (area >= max) {
840 new_output = output;
841 max = area;
842 }
843 }
844 pixman_region32_fini(&region);
845
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846 ev->output = new_output;
847 ev->output_mask = mask;
848
849 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400850}
851
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200852static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500853view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
854 int32_t width, int32_t height,
855 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200856{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200857 float min_x = HUGE_VALF, min_y = HUGE_VALF;
858 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200859 int32_t s[4][2] = {
860 { sx, sy },
861 { sx, sy + height },
862 { sx + width, sy },
863 { sx + width, sy + height }
864 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200865 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200866 int i;
867
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300868 if (width == 0 || height == 0) {
869 /* avoid rounding empty bbox to 1x1 */
870 pixman_region32_init(bbox);
871 return;
872 }
873
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200874 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200875 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200877 if (x < min_x)
878 min_x = x;
879 if (x > max_x)
880 max_x = x;
881 if (y < min_y)
882 min_y = y;
883 if (y > max_y)
884 max_y = y;
885 }
886
Pekka Paalanen219b9822012-02-08 15:38:37 +0200887 int_x = floorf(min_x);
888 int_y = floorf(min_y);
889 pixman_region32_init_rect(bbox, int_x, int_y,
890 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200891}
892
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200893static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500894weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200895{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200897
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200898 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 view->geometry.x = roundf(view->geometry.x);
900 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200901
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500902 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500903 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
904 view->transform.position.matrix.d[12] = view->geometry.x;
905 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500906
Jason Ekstranda7af7042013-10-12 22:38:11 -0500907 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500908
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 view->transform.inverse = view->transform.position.matrix;
910 view->transform.inverse.d[12] = -view->geometry.x;
911 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500912
Jason Ekstranda7af7042013-10-12 22:38:11 -0500913 pixman_region32_init_rect(&view->transform.boundingbox,
914 view->geometry.x,
915 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600916 view->surface->width,
917 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500918
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 if (view->alpha == 1.0) {
920 pixman_region32_copy(&view->transform.opaque,
921 &view->surface->opaque);
922 pixman_region32_translate(&view->transform.opaque,
923 view->geometry.x,
924 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500925 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200926}
927
928static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200930{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 struct weston_view *parent = view->geometry.parent;
932 struct weston_matrix *matrix = &view->transform.matrix;
933 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200934 struct weston_transform *tform;
935
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200937
938 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
940 view->transform.position.matrix.d[12] = view->geometry.x;
941 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200942
943 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200945 weston_matrix_multiply(matrix, &tform->matrix);
946
Pekka Paalanen483243f2013-03-08 14:56:50 +0200947 if (parent)
948 weston_matrix_multiply(matrix, &parent->transform.matrix);
949
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200950 if (weston_matrix_invert(inverse, matrix) < 0) {
951 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 weston_log("error: weston_view %p"
953 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200954 return -1;
955 }
956
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600957 view_compute_bbox(view, 0, 0,
958 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500960
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200961 return 0;
962}
963
964WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200966{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200968
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200970 return;
971
Pekka Paalanen483243f2013-03-08 14:56:50 +0200972 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200974
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200976
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200978
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 pixman_region32_fini(&view->transform.boundingbox);
980 pixman_region32_fini(&view->transform.opaque);
981 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500982
Pekka Paalanencd403622012-01-25 13:37:39 +0200983 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984 if (view->geometry.transformation_list.next ==
985 &view->transform.position.link &&
986 view->geometry.transformation_list.prev ==
987 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200988 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200990 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500991 if (weston_view_update_transform_enable(view) < 0)
992 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200993 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200994
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200996
Jason Ekstranda7af7042013-10-12 22:38:11 -0500997 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300998
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 wl_signal_emit(&view->surface->compositor->transform_signal,
1000 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001001}
1002
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001003WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001005{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001007
1008 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 * The invariant: if view->geometry.dirty, then all views
1010 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001011 * Corollary: if not parent->geometry.dirty, then all ancestors
1012 * are not dirty.
1013 */
1014
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001016 return;
1017
Jason Ekstranda7af7042013-10-12 22:38:11 -05001018 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001021 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001023}
1024
1025WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026weston_view_to_global_fixed(struct weston_view *view,
1027 wl_fixed_t vx, wl_fixed_t vy,
1028 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001029{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001030 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 weston_view_to_global_float(view,
1033 wl_fixed_to_double(vx),
1034 wl_fixed_to_double(vy),
1035 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001036 *x = wl_fixed_from_double(xf);
1037 *y = wl_fixed_from_double(yf);
1038}
1039
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001040WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041weston_view_from_global_float(struct weston_view *view,
1042 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001043{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001045 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1046
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001048
1049 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001050 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001052 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 *vx = 0;
1054 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001055 return;
1056 }
1057
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 *vx = v.f[0] / v.f[3];
1059 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001060 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001061 *vx = x - view->geometry.x;
1062 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001063 }
1064}
1065
1066WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001067weston_view_from_global_fixed(struct weston_view *view,
1068 wl_fixed_t x, wl_fixed_t y,
1069 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001070{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001071 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001072
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 weston_view_from_global_float(view,
1074 wl_fixed_to_double(x),
1075 wl_fixed_to_double(y),
1076 &vxf, &vyf);
1077 *vx = wl_fixed_from_double(vxf);
1078 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001079}
1080
1081WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001082weston_view_from_global(struct weston_view *view,
1083 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001084{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001085 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001086
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1088 *vx = floorf(vxf);
1089 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001090}
1091
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001092WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001093weston_surface_schedule_repaint(struct weston_surface *surface)
1094{
1095 struct weston_output *output;
1096
1097 wl_list_for_each(output, &surface->compositor->output_list, link)
1098 if (surface->output_mask & (1 << output->id))
1099 weston_output_schedule_repaint(output);
1100}
1101
1102WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103weston_view_schedule_repaint(struct weston_view *view)
1104{
1105 struct weston_output *output;
1106
1107 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1108 if (view->output_mask & (1 << output->id))
1109 weston_output_schedule_repaint(output);
1110}
1111
1112WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001113weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001114{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001115 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 0, 0, surface->width,
1117 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001118
Kristian Høgsberg98238702012-08-03 16:29:12 -04001119 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001120}
1121
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001122WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001124{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001125 if (view->geometry.x == x && view->geometry.y == y)
1126 return;
1127
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 view->geometry.x = x;
1129 view->geometry.y = y;
1130 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001131}
1132
Pekka Paalanen483243f2013-03-08 14:56:50 +02001133static void
1134transform_parent_handle_parent_destroy(struct wl_listener *listener,
1135 void *data)
1136{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137 struct weston_view *view =
1138 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001139 geometry.parent_destroy_listener);
1140
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001142}
1143
1144WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145weston_view_set_transform_parent(struct weston_view *view,
1146 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001147{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 if (view->geometry.parent) {
1149 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1150 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001151 }
1152
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001154
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001156 transform_parent_handle_parent_destroy;
1157 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001158 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001160 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001162 }
1163
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 weston_view_geometry_dirty(view);
1165}
1166
1167WL_EXPORT int
1168weston_view_is_mapped(struct weston_view *view)
1169{
1170 if (view->output)
1171 return 1;
1172 else
1173 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001174}
1175
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001176WL_EXPORT int
1177weston_surface_is_mapped(struct weston_surface *surface)
1178{
1179 if (surface->output)
1180 return 1;
1181 else
1182 return 0;
1183}
1184
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001185static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001186surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001187{
1188 struct weston_view *view;
1189
1190 if (surface->width == width && surface->height == height)
1191 return;
1192
1193 surface->width = width;
1194 surface->height = height;
1195
1196 wl_list_for_each(view, &surface->views, surface_link)
1197 weston_view_geometry_dirty(view);
1198}
1199
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001200WL_EXPORT void
1201weston_surface_set_size(struct weston_surface *surface,
1202 int32_t width, int32_t height)
1203{
1204 assert(!surface->resource);
1205 surface_set_size(surface, width, height);
1206}
1207
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001208static int
1209fixed_round_up_to_int(wl_fixed_t f)
1210{
1211 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1212}
1213
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001214static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001215weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001216{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001217 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001218 int32_t width, height;
1219
1220 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001221 surface->width_from_buffer = 0;
1222 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001223 return;
1224 }
1225
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001226 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001227 case WL_OUTPUT_TRANSFORM_90:
1228 case WL_OUTPUT_TRANSFORM_270:
1229 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1230 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001231 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1232 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001233 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001234 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001235 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1236 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001237 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001238 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001239
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001240 surface->width_from_buffer = width;
1241 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001242}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001243
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001244static void
1245weston_surface_update_size(struct weston_surface *surface)
1246{
1247 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1248 int32_t width, height;
1249
1250 width = surface->width_from_buffer;
1251 height = surface->height_from_buffer;
1252
1253 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001254 surface_set_size(surface,
1255 vp->surface.width, vp->surface.height);
1256 return;
1257 }
1258
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001259 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001260 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1261 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1262
1263 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001264 return;
1265 }
1266
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001267 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001268}
1269
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001270WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001271weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001272{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001273 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001274
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001275 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001276
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001277 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001278}
1279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280WL_EXPORT struct weston_view *
1281weston_compositor_pick_view(struct weston_compositor *compositor,
1282 wl_fixed_t x, wl_fixed_t y,
1283 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001284{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 wl_list_for_each(view, &compositor->view_list, link) {
1288 weston_view_from_global_fixed(view, x, y, vx, vy);
1289 if (pixman_region32_contains_point(&view->surface->input,
1290 wl_fixed_to_int(*vx),
1291 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001292 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001294 }
1295
1296 return NULL;
1297}
1298
1299static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001300weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001301{
Daniel Stone37816df2012-05-16 18:45:18 +01001302 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001303
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001304 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001305 return;
1306
Daniel Stone37816df2012-05-16 18:45:18 +01001307 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001308 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001309}
1310
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001311WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001313{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001314 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001315
Jason Ekstranda7af7042013-10-12 22:38:11 -05001316 if (!weston_view_is_mapped(view))
1317 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 weston_view_damage_below(view);
1320 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001321 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 wl_list_remove(&view->layer_link);
1323 wl_list_init(&view->layer_link);
1324 wl_list_remove(&view->link);
1325 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 view->output_mask = 0;
1327 weston_surface_assign_output(view->surface);
1328
1329 if (weston_surface_is_mapped(view->surface))
1330 return;
1331
1332 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1333 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001334 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001336 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001337 NULL,
1338 wl_fixed_from_int(0),
1339 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001341 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001342 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345WL_EXPORT void
1346weston_surface_unmap(struct weston_surface *surface)
1347{
1348 struct weston_view *view;
1349
1350 wl_list_for_each(view, &surface->views, surface_link)
1351 weston_view_unmap(view);
1352 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001353}
1354
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001355static void
1356weston_surface_reset_pending_buffer(struct weston_surface *surface)
1357{
1358 if (surface->pending.buffer)
1359 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1360 surface->pending.buffer = NULL;
1361 surface->pending.sx = 0;
1362 surface->pending.sy = 0;
1363 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001364 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001365}
1366
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001367struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001368 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001369 struct wl_list link;
1370};
1371
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372WL_EXPORT void
1373weston_view_destroy(struct weston_view *view)
1374{
1375 wl_signal_emit(&view->destroy_signal, view);
1376
1377 assert(wl_list_empty(&view->geometry.child_list));
1378
1379 if (weston_view_is_mapped(view)) {
1380 weston_view_unmap(view);
1381 weston_compositor_build_view_list(view->surface->compositor);
1382 }
1383
1384 wl_list_remove(&view->link);
1385 wl_list_remove(&view->layer_link);
1386
1387 pixman_region32_fini(&view->clip);
1388 pixman_region32_fini(&view->transform.boundingbox);
1389
1390 weston_view_set_transform_parent(view, NULL);
1391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 wl_list_remove(&view->surface_link);
1393
1394 free(view);
1395}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001396
1397WL_EXPORT void
1398weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001399{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001400 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001402
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001403 if (--surface->ref_count > 0)
1404 return;
1405
1406 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1407
Pekka Paalanene67858b2013-04-25 13:57:42 +03001408 assert(wl_list_empty(&surface->subsurface_list_pending));
1409 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001410
Jason Ekstranda7af7042013-10-12 22:38:11 -05001411 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1412 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001413
Pekka Paalanenbc106382012-10-10 12:49:31 +03001414 wl_list_for_each_safe(cb, next,
1415 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001416 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001417
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001418 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001419 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001420 pixman_region32_fini(&surface->pending.damage);
1421
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001422 if (surface->pending.buffer)
1423 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1424
Pekka Paalanende685b82012-12-04 15:58:12 +02001425 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001426
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001427 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001428 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001429 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001430
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001431 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001432 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001433
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001434 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001435}
1436
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001437static void
1438destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001439{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001440 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001441
Giulio Camuffo0d379742013-11-15 22:06:15 +01001442 /* Set the resource to NULL, since we don't want to leave a
1443 * dangling pointer if the surface was refcounted and survives
1444 * the weston_surface_destroy() call. */
1445 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001446 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001447}
1448
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001449static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001450weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1451{
1452 struct weston_buffer *buffer =
1453 container_of(listener, struct weston_buffer, destroy_listener);
1454
1455 wl_signal_emit(&buffer->destroy_signal, buffer);
1456 free(buffer);
1457}
1458
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001459WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001460weston_buffer_from_resource(struct wl_resource *resource)
1461{
1462 struct weston_buffer *buffer;
1463 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001464
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001465 listener = wl_resource_get_destroy_listener(resource,
1466 weston_buffer_destroy_handler);
1467
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001468 if (listener)
1469 return container_of(listener, struct weston_buffer,
1470 destroy_listener);
1471
1472 buffer = zalloc(sizeof *buffer);
1473 if (buffer == NULL)
1474 return NULL;
1475
1476 buffer->resource = resource;
1477 wl_signal_init(&buffer->destroy_signal);
1478 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001479 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001480 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001481
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001482 return buffer;
1483}
1484
1485static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001486weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1487 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001488{
Pekka Paalanende685b82012-12-04 15:58:12 +02001489 struct weston_buffer_reference *ref =
1490 container_of(listener, struct weston_buffer_reference,
1491 destroy_listener);
1492
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001493 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001494 ref->buffer = NULL;
1495}
1496
1497WL_EXPORT void
1498weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001499 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001500{
1501 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001502 ref->buffer->busy_count--;
1503 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001504 assert(wl_resource_get_client(ref->buffer->resource));
1505 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001506 WL_BUFFER_RELEASE);
1507 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001508 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001509 }
1510
Pekka Paalanende685b82012-12-04 15:58:12 +02001511 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001512 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001513 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001514 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001515 }
1516
Pekka Paalanende685b82012-12-04 15:58:12 +02001517 ref->buffer = buffer;
1518 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1519}
1520
1521static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001522weston_surface_attach(struct weston_surface *surface,
1523 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001524{
1525 weston_buffer_reference(&surface->buffer_ref, buffer);
1526
Pekka Paalanena6421c42012-12-04 15:58:10 +02001527 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001528 if (weston_surface_is_mapped(surface))
1529 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001530 }
1531
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001532 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001533
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001534 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001535}
1536
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001537WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001538weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001539{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001540 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001541
1542 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001543 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001544}
1545
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001546WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001547weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001548{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001549 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001550
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001551 pixman_region32_union(&compositor->primary_plane.damage,
1552 &compositor->primary_plane.damage,
1553 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001554 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001555}
1556
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001557static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001558surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001559{
Pekka Paalanende685b82012-12-04 15:58:12 +02001560 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001561 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001562 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001563
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 empty_region(&surface->damage);
1565}
1566
1567static void
1568view_accumulate_damage(struct weston_view *view,
1569 pixman_region32_t *opaque)
1570{
1571 pixman_region32_t damage;
1572
1573 pixman_region32_init(&damage);
1574 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001575 pixman_box32_t *extents;
1576
Jason Ekstranda7af7042013-10-12 22:38:11 -05001577 extents = pixman_region32_extents(&view->surface->damage);
1578 view_compute_bbox(view, extents->x1, extents->y1,
1579 extents->x2 - extents->x1,
1580 extents->y2 - extents->y1,
1581 &damage);
1582 pixman_region32_translate(&damage,
1583 -view->plane->x,
1584 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001585 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001586 pixman_region32_copy(&damage, &view->surface->damage);
1587 pixman_region32_translate(&damage,
1588 view->geometry.x - view->plane->x,
1589 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001590 }
1591
Jason Ekstranda7af7042013-10-12 22:38:11 -05001592 pixman_region32_subtract(&damage, &damage, opaque);
1593 pixman_region32_union(&view->plane->damage,
1594 &view->plane->damage, &damage);
1595 pixman_region32_fini(&damage);
1596 pixman_region32_copy(&view->clip, opaque);
1597 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001598}
1599
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001600static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001601compositor_accumulate_damage(struct weston_compositor *ec)
1602{
1603 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001605 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001606
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001607 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001608
1609 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001610 pixman_region32_copy(&plane->clip, &clip);
1611
1612 pixman_region32_init(&opaque);
1613
Jason Ekstranda7af7042013-10-12 22:38:11 -05001614 wl_list_for_each(ev, &ec->view_list, link) {
1615 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001616 continue;
1617
Jason Ekstranda7af7042013-10-12 22:38:11 -05001618 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001619 }
1620
1621 pixman_region32_union(&clip, &clip, &opaque);
1622 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001623 }
1624
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001625 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001626
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 wl_list_for_each(ev, &ec->view_list, link)
1628 ev->surface->touched = 0;
1629
1630 wl_list_for_each(ev, &ec->view_list, link) {
1631 if (ev->surface->touched)
1632 continue;
1633 ev->surface->touched = 1;
1634
1635 surface_flush_damage(ev->surface);
1636
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001637 /* Both the renderer and the backend have seen the buffer
1638 * by now. If renderer needs the buffer, it has its own
1639 * reference set. If the backend wants to keep the buffer
1640 * around for migrating the surface into a non-primary plane
1641 * later, keep_buffer is true. Otherwise, drop the core
1642 * reference now, and allow early buffer release. This enables
1643 * clients to use single-buffering.
1644 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 if (!ev->surface->keep_buffer)
1646 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001647 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001648}
1649
1650static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001652{
1653 struct weston_subsurface *sub;
1654
Pekka Paalanene67858b2013-04-25 13:57:42 +03001655 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001656 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001657 continue;
1658
Jason Ekstranda7af7042013-10-12 22:38:11 -05001659 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1660 wl_list_init(&sub->surface->views);
1661
1662 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001663 }
1664}
1665
1666static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001667surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001668{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001669 struct weston_subsurface *sub;
1670 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001671
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1673 if (sub->surface == surface)
1674 continue;
1675
George Kiagiadakised04d382014-06-13 18:10:26 +02001676 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1677 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001678 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001679 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680
1681 surface_free_unused_subsurface_views(sub->surface);
1682 }
1683}
1684
1685static void
1686view_list_add_subsurface_view(struct weston_compositor *compositor,
1687 struct weston_subsurface *sub,
1688 struct weston_view *parent)
1689{
1690 struct weston_subsurface *child;
1691 struct weston_view *view = NULL, *iv;
1692
1693 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1694 if (iv->geometry.parent == parent) {
1695 view = iv;
1696 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001697 }
1698 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001699
1700 if (view) {
1701 /* Put it back in the surface's list of views */
1702 wl_list_remove(&view->surface_link);
1703 wl_list_insert(&sub->surface->views, &view->surface_link);
1704 } else {
1705 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001706 weston_view_set_position(view,
1707 sub->position.x,
1708 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 weston_view_set_transform_parent(view, parent);
1710 }
1711
1712 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713
Pekka Paalanenb188e912013-11-19 14:03:35 +02001714 if (wl_list_empty(&sub->surface->subsurface_list)) {
1715 wl_list_insert(compositor->view_list.prev, &view->link);
1716 return;
1717 }
1718
1719 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1720 if (child->surface == sub->surface)
1721 wl_list_insert(compositor->view_list.prev, &view->link);
1722 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001724 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725}
1726
1727static void
1728view_list_add(struct weston_compositor *compositor,
1729 struct weston_view *view)
1730{
1731 struct weston_subsurface *sub;
1732
1733 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001734
Pekka Paalanenb188e912013-11-19 14:03:35 +02001735 if (wl_list_empty(&view->surface->subsurface_list)) {
1736 wl_list_insert(compositor->view_list.prev, &view->link);
1737 return;
1738 }
1739
1740 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1741 if (sub->surface == view->surface)
1742 wl_list_insert(compositor->view_list.prev, &view->link);
1743 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001744 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001745 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001746}
1747
1748static void
1749weston_compositor_build_view_list(struct weston_compositor *compositor)
1750{
1751 struct weston_view *view;
1752 struct weston_layer *layer;
1753
1754 wl_list_for_each(layer, &compositor->layer_list, link)
1755 wl_list_for_each(view, &layer->view_list, layer_link)
1756 surface_stash_subsurface_views(view->surface);
1757
1758 wl_list_init(&compositor->view_list);
1759 wl_list_for_each(layer, &compositor->layer_list, link) {
1760 wl_list_for_each(view, &layer->view_list, layer_link) {
1761 view_list_add(compositor, view);
1762 }
1763 }
1764
1765 wl_list_for_each(layer, &compositor->layer_list, link)
1766 wl_list_for_each(view, &layer->view_list, layer_link)
1767 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001768}
1769
David Herrmann1edf44c2013-10-22 17:11:26 +02001770static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001771weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001772{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001773 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001775 struct weston_animation *animation, *next;
1776 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001777 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001778 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001779 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001780
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001781 if (output->destroying)
1782 return 0;
1783
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001784 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001786
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001787 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001788 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001789 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790 wl_list_for_each(ev, &ec->view_list, link)
1791 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001792
Pekka Paalanene67858b2013-04-25 13:57:42 +03001793 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 wl_list_for_each(ev, &ec->view_list, link) {
1795 /* Note: This operation is safe to do multiple times on the
1796 * same surface.
1797 */
1798 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001799 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001800 &ev->surface->frame_callback_list);
1801 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001802 }
1803 }
1804
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001805 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001806
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001807 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001808 pixman_region32_intersect(&output_damage,
1809 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001810 pixman_region32_subtract(&output_damage,
1811 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001812
Scott Moreauccbf29d2012-02-22 14:21:41 -07001813 if (output->dirty)
1814 weston_output_update_matrix(output);
1815
David Herrmann1edf44c2013-10-22 17:11:26 +02001816 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001817
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001818 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001819
Kristian Høgsbergef044142011-06-21 15:02:12 -04001820 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001821
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001822 weston_compositor_repick(ec);
1823 wl_event_loop_dispatch(ec->input_loop, 0);
1824
Jonas Ådahldb773762012-06-13 00:01:21 +02001825 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001826 wl_callback_send_done(cb->resource, msecs);
1827 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001828 }
1829
Scott Moreaud64cf212012-06-08 19:40:54 -06001830 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001831 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001832 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001833 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001834
1835 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001836}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001837
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001838static int
1839weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001840{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001841 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001842
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001843 wl_event_loop_dispatch(compositor->input_loop, 0);
1844
1845 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001846}
1847
1848WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001849weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001850{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001851 struct weston_compositor *compositor = output->compositor;
1852 struct wl_event_loop *loop =
1853 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001854 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001855
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001856 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001857
1858 if (output->repaint_needed &&
1859 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1860 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001861 r = weston_output_repaint(output, msecs);
1862 if (!r)
1863 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001864 }
1865
1866 output->repaint_scheduled = 0;
1867 if (compositor->input_loop_source)
1868 return;
1869
1870 fd = wl_event_loop_get_fd(compositor->input_loop);
1871 compositor->input_loop_source =
1872 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1873 weston_compositor_read_input, compositor);
1874}
1875
1876static void
1877idle_repaint(void *data)
1878{
1879 struct weston_output *output = data;
1880
Jonas Ådahle5a12252013-04-05 23:07:11 +02001881 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001882}
1883
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001884WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001885weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1886{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001887 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001888 if (below != NULL)
1889 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001890}
1891
1892WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001893weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001894{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001895 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001896 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001897
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001898 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1899 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001900 return;
1901
Kristian Høgsbergef044142011-06-21 15:02:12 -04001902 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001903 output->repaint_needed = 1;
1904 if (output->repaint_scheduled)
1905 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001906
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001907 wl_event_loop_add_idle(loop, idle_repaint, output);
1908 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001909
1910 if (compositor->input_loop_source) {
1911 wl_event_source_remove(compositor->input_loop_source);
1912 compositor->input_loop_source = NULL;
1913 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001914}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001915
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001916WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001917weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1918{
1919 struct weston_output *output;
1920
1921 wl_list_for_each(output, &compositor->output_list, link)
1922 weston_output_schedule_repaint(output);
1923}
1924
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001925static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001926surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001927{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001928 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001929}
1930
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001931static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001932surface_attach(struct wl_client *client,
1933 struct wl_resource *resource,
1934 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1935{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001936 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001937 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001938
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001939 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001940 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001941 if (buffer == NULL) {
1942 wl_client_post_no_memory(client);
1943 return;
1944 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001945 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001946
Pekka Paalanende685b82012-12-04 15:58:12 +02001947 /* Attach, attach, without commit in between does not send
1948 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001949 if (surface->pending.buffer)
1950 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001951
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001952 surface->pending.sx = sx;
1953 surface->pending.sy = sy;
1954 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001955 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001956 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001957 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001958 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001959 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001960}
1961
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001962static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001963surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001964 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001965 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001966{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001967 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001968
Pekka Paalanen8e159182012-10-10 12:49:25 +03001969 pixman_region32_union_rect(&surface->pending.damage,
1970 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001971 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001972}
1973
Kristian Høgsberg33418202011-08-16 23:01:28 -04001974static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001975destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001976{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001977 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001978
1979 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001980 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001981}
1982
1983static void
1984surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001985 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001986{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001987 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001988 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001989
1990 cb = malloc(sizeof *cb);
1991 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001992 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001993 return;
1994 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001995
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001996 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1997 callback);
1998 if (cb->resource == NULL) {
1999 free(cb);
2000 wl_resource_post_no_memory(resource);
2001 return;
2002 }
2003
Jason Ekstranda85118c2013-06-27 20:17:02 -05002004 wl_resource_set_implementation(cb->resource, NULL, cb,
2005 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002006
Pekka Paalanenbc106382012-10-10 12:49:31 +03002007 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002008}
2009
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002010static void
2011surface_set_opaque_region(struct wl_client *client,
2012 struct wl_resource *resource,
2013 struct wl_resource *region_resource)
2014{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002015 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002016 struct weston_region *region;
2017
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002018 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002019 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002020 pixman_region32_copy(&surface->pending.opaque,
2021 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002022 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002023 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002024 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002025}
2026
2027static void
2028surface_set_input_region(struct wl_client *client,
2029 struct wl_resource *resource,
2030 struct wl_resource *region_resource)
2031{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002032 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002033 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002034
2035 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002036 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002037 pixman_region32_copy(&surface->pending.input,
2038 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002039 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002040 pixman_region32_fini(&surface->pending.input);
2041 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002042 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002043}
2044
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002045static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002046weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002047{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002048 struct weston_subsurface *sub;
2049
2050 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2051 parent_link_pending) {
2052 wl_list_remove(&sub->parent_link);
2053 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2054 }
2055}
2056
2057static void
2058weston_surface_commit(struct weston_surface *surface)
2059{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002060 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002061 pixman_region32_t opaque;
2062
Alexander Larsson4ea95522013-05-22 14:41:37 +02002063 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002064 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002065 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002066 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002067
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002068 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002069 if (surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002070 weston_surface_attach(surface, surface->pending.buffer);
Giulio Camuffo184df502013-02-21 11:29:21 +01002071
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002072 if (surface->pending.newly_attached ||
2073 surface->pending.buffer_viewport.changed) {
2074 weston_surface_update_size(surface);
2075 if (surface->configure)
2076 surface->configure(surface, surface->pending.sx,
2077 surface->pending.sy);
2078 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002079
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02002080 weston_surface_reset_pending_buffer(surface);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002081
2082 /* wl_surface.damage */
2083 pixman_region32_union(&surface->damage, &surface->damage,
2084 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002085 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2086 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002087 surface->width,
2088 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002089 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002090
2091 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002092 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 surface->width,
2094 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002095 pixman_region32_intersect(&opaque,
2096 &opaque, &surface->pending.opaque);
2097
2098 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2099 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002100 wl_list_for_each(view, &surface->views, surface_link)
2101 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002102 }
2103
2104 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002105
2106 /* wl_surface.set_input_region */
2107 pixman_region32_fini(&surface->input);
2108 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002109 surface->width,
2110 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002111 pixman_region32_intersect(&surface->input,
2112 &surface->input, &surface->pending.input);
2113
Pekka Paalanenbc106382012-10-10 12:49:31 +03002114 /* wl_surface.frame */
2115 wl_list_insert_list(&surface->frame_callback_list,
2116 &surface->pending.frame_callback_list);
2117 wl_list_init(&surface->pending.frame_callback_list);
2118
Pekka Paalanene67858b2013-04-25 13:57:42 +03002119 weston_surface_commit_subsurface_order(surface);
2120
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002121 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002122}
2123
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002124static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002125weston_subsurface_commit(struct weston_subsurface *sub);
2126
2127static void
2128weston_subsurface_parent_commit(struct weston_subsurface *sub,
2129 int parent_is_synchronized);
2130
2131static void
2132surface_commit(struct wl_client *client, struct wl_resource *resource)
2133{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002134 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002135 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2136
2137 if (sub) {
2138 weston_subsurface_commit(sub);
2139 return;
2140 }
2141
2142 weston_surface_commit(surface);
2143
2144 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2145 if (sub->surface != surface)
2146 weston_subsurface_parent_commit(sub, 0);
2147 }
2148}
2149
2150static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002151surface_set_buffer_transform(struct wl_client *client,
2152 struct wl_resource *resource, int transform)
2153{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002154 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002155
Jonny Lamba55f1392014-05-30 12:07:15 +02002156 /* if wl_output.transform grows more members this will need to be updated. */
2157 if (transform < 0 ||
2158 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2159 wl_resource_post_error(resource,
2160 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2161 "buffer transform must be a valid transform "
2162 "('%d' specified)", transform);
2163 return;
2164 }
2165
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002166 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002167 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002168}
2169
Alexander Larsson4ea95522013-05-22 14:41:37 +02002170static void
2171surface_set_buffer_scale(struct wl_client *client,
2172 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002173 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002174{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002175 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002176
Jonny Lamba55f1392014-05-30 12:07:15 +02002177 if (scale < 1) {
2178 wl_resource_post_error(resource,
2179 WL_SURFACE_ERROR_INVALID_SCALE,
2180 "buffer scale must be at least one "
2181 "('%d' specified)", scale);
2182 return;
2183 }
2184
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002185 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002186 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002187}
2188
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002189static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002190 surface_destroy,
2191 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002192 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002193 surface_frame,
2194 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002195 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002196 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002197 surface_set_buffer_transform,
2198 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002199};
2200
2201static void
2202compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002203 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002204{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002205 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002206 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002207
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002208 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002209 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002210 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002211 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002212 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002213
Jason Ekstranda85118c2013-06-27 20:17:02 -05002214 surface->resource =
2215 wl_resource_create(client, &wl_surface_interface,
2216 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002217 if (surface->resource == NULL) {
2218 weston_surface_destroy(surface);
2219 wl_resource_post_no_memory(resource);
2220 return;
2221 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002222 wl_resource_set_implementation(surface->resource, &surface_interface,
2223 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002224
2225 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002226}
2227
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002228static void
2229destroy_region(struct wl_resource *resource)
2230{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002231 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002232
2233 pixman_region32_fini(&region->region);
2234 free(region);
2235}
2236
2237static void
2238region_destroy(struct wl_client *client, struct wl_resource *resource)
2239{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002240 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002241}
2242
2243static void
2244region_add(struct wl_client *client, struct wl_resource *resource,
2245 int32_t x, int32_t y, int32_t width, int32_t height)
2246{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002247 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002248
2249 pixman_region32_union_rect(&region->region, &region->region,
2250 x, y, width, height);
2251}
2252
2253static void
2254region_subtract(struct wl_client *client, struct wl_resource *resource,
2255 int32_t x, int32_t y, int32_t width, int32_t height)
2256{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002257 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002258 pixman_region32_t rect;
2259
2260 pixman_region32_init_rect(&rect, x, y, width, height);
2261 pixman_region32_subtract(&region->region, &region->region, &rect);
2262 pixman_region32_fini(&rect);
2263}
2264
2265static const struct wl_region_interface region_interface = {
2266 region_destroy,
2267 region_add,
2268 region_subtract
2269};
2270
2271static void
2272compositor_create_region(struct wl_client *client,
2273 struct wl_resource *resource, uint32_t id)
2274{
2275 struct weston_region *region;
2276
2277 region = malloc(sizeof *region);
2278 if (region == NULL) {
2279 wl_resource_post_no_memory(resource);
2280 return;
2281 }
2282
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002283 pixman_region32_init(&region->region);
2284
Jason Ekstranda85118c2013-06-27 20:17:02 -05002285 region->resource =
2286 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002287 if (region->resource == NULL) {
2288 free(region);
2289 wl_resource_post_no_memory(resource);
2290 return;
2291 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002292 wl_resource_set_implementation(region->resource, &region_interface,
2293 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002294}
2295
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002296static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002297 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002298 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002299};
2300
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002301static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002302weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2303{
2304 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002307
Alexander Larsson4ea95522013-05-22 14:41:37 +02002308 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002309 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002310 /* wl_viewport.set */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002311 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002312
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313 /* wl_surface.attach */
Pekka Paalanen260ba382014-03-14 14:38:12 +02002314 if (sub->cached.newly_attached)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002315 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2316 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2317
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002318 if (sub->cached.newly_attached || sub->cached.buffer_viewport.changed) {
2319 weston_surface_update_size(surface);
2320 if (surface->configure)
2321 surface->configure(surface, sub->cached.sx,
2322 sub->cached.sy);
2323 }
2324
Pekka Paalanene67858b2013-04-25 13:57:42 +03002325 sub->cached.sx = 0;
2326 sub->cached.sy = 0;
2327 sub->cached.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002328 sub->cached.buffer_viewport.changed = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329
2330 /* wl_surface.damage */
2331 pixman_region32_union(&surface->damage, &surface->damage,
2332 &sub->cached.damage);
2333 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2334 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002335 surface->width,
2336 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002337 empty_region(&sub->cached.damage);
2338
2339 /* wl_surface.set_opaque_region */
2340 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002341 surface->width,
2342 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002343 pixman_region32_intersect(&opaque,
2344 &opaque, &sub->cached.opaque);
2345
2346 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2347 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002348 wl_list_for_each(view, &surface->views, surface_link)
2349 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002350 }
2351
2352 pixman_region32_fini(&opaque);
2353
2354 /* wl_surface.set_input_region */
2355 pixman_region32_fini(&surface->input);
2356 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002357 surface->width,
2358 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002359 pixman_region32_intersect(&surface->input,
2360 &surface->input, &sub->cached.input);
2361
2362 /* wl_surface.frame */
2363 wl_list_insert_list(&surface->frame_callback_list,
2364 &sub->cached.frame_callback_list);
2365 wl_list_init(&sub->cached.frame_callback_list);
2366
2367 weston_surface_commit_subsurface_order(surface);
2368
2369 weston_surface_schedule_repaint(surface);
2370
2371 sub->cached.has_data = 0;
2372}
2373
2374static void
2375weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2376{
2377 struct weston_surface *surface = sub->surface;
2378
2379 /*
2380 * If this commit would cause the surface to move by the
2381 * attach(dx, dy) parameters, the old damage region must be
2382 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002383 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002384 */
2385 pixman_region32_translate(&sub->cached.damage,
2386 -surface->pending.sx, -surface->pending.sy);
2387 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2388 &surface->pending.damage);
2389 empty_region(&surface->pending.damage);
2390
2391 if (surface->pending.newly_attached) {
2392 sub->cached.newly_attached = 1;
2393 weston_buffer_reference(&sub->cached.buffer_ref,
2394 surface->pending.buffer);
2395 }
2396 sub->cached.sx += surface->pending.sx;
2397 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002398
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002399 sub->cached.buffer_viewport.changed |=
2400 surface->pending.buffer_viewport.changed;
2401 sub->cached.buffer_viewport.buffer =
2402 surface->pending.buffer_viewport.buffer;
2403 sub->cached.buffer_viewport.surface =
2404 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002405
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002406 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002407
2408 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2409
2410 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2411
2412 wl_list_insert_list(&sub->cached.frame_callback_list,
2413 &surface->pending.frame_callback_list);
2414 wl_list_init(&surface->pending.frame_callback_list);
2415
2416 sub->cached.has_data = 1;
2417}
2418
2419static int
2420weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2421{
2422 while (sub) {
2423 if (sub->synchronized)
2424 return 1;
2425
2426 if (!sub->parent)
2427 return 0;
2428
2429 sub = weston_surface_to_subsurface(sub->parent);
2430 }
2431
2432 return 0;
2433}
2434
2435static void
2436weston_subsurface_commit(struct weston_subsurface *sub)
2437{
2438 struct weston_surface *surface = sub->surface;
2439 struct weston_subsurface *tmp;
2440
2441 /* Recursive check for effectively synchronized. */
2442 if (weston_subsurface_is_synchronized(sub)) {
2443 weston_subsurface_commit_to_cache(sub);
2444 } else {
2445 if (sub->cached.has_data) {
2446 /* flush accumulated state from cache */
2447 weston_subsurface_commit_to_cache(sub);
2448 weston_subsurface_commit_from_cache(sub);
2449 } else {
2450 weston_surface_commit(surface);
2451 }
2452
2453 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2454 if (tmp->surface != surface)
2455 weston_subsurface_parent_commit(tmp, 0);
2456 }
2457 }
2458}
2459
2460static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002461weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002462{
2463 struct weston_surface *surface = sub->surface;
2464 struct weston_subsurface *tmp;
2465
Pekka Paalanene67858b2013-04-25 13:57:42 +03002466 /* From now on, commit_from_cache the whole sub-tree, regardless of
2467 * the synchronized mode of each child. This sub-surface or some
2468 * of its ancestors were synchronized, so we are synchronized
2469 * all the way down.
2470 */
2471
2472 if (sub->cached.has_data)
2473 weston_subsurface_commit_from_cache(sub);
2474
2475 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2476 if (tmp->surface != surface)
2477 weston_subsurface_parent_commit(tmp, 1);
2478 }
2479}
2480
2481static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002482weston_subsurface_parent_commit(struct weston_subsurface *sub,
2483 int parent_is_synchronized)
2484{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002485 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002486 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002487 wl_list_for_each(view, &sub->surface->views, surface_link)
2488 weston_view_set_position(view,
2489 sub->position.x,
2490 sub->position.y);
2491
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002492 sub->position.set = 0;
2493 }
2494
2495 if (parent_is_synchronized || sub->synchronized)
2496 weston_subsurface_synchronized_commit(sub);
2497}
2498
2499static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002500subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002501{
2502 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002503 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002504
Jason Ekstranda7af7042013-10-12 22:38:11 -05002505 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002506 weston_view_set_position(view,
2507 view->geometry.x + dx,
2508 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002509
2510 /* No need to check parent mappedness, because if parent is not
2511 * mapped, parent is not in a visible layer, so this sub-surface
2512 * will not be drawn either.
2513 */
2514 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002515 /* Cannot call weston_surface_update_transform(),
2516 * because that would call it also for the parent surface,
2517 * which might not be mapped yet. That would lead to
2518 * inconsistent state, where the window could never be
2519 * mapped.
2520 *
2521 * Instead just assing any output, to make
2522 * weston_surface_is_mapped() return true, so that when the
2523 * parent surface does get mapped, this one will get
2524 * included, too. See surface_list_add().
2525 */
2526 assert(!wl_list_empty(&compositor->output_list));
2527 surface->output = container_of(compositor->output_list.next,
2528 struct weston_output, link);
2529 }
2530}
2531
2532static struct weston_subsurface *
2533weston_surface_to_subsurface(struct weston_surface *surface)
2534{
2535 if (surface->configure == subsurface_configure)
2536 return surface->configure_private;
2537
2538 return NULL;
2539}
2540
Pekka Paalanen01388e22013-04-25 13:57:44 +03002541WL_EXPORT struct weston_surface *
2542weston_surface_get_main_surface(struct weston_surface *surface)
2543{
2544 struct weston_subsurface *sub;
2545
2546 while (surface && (sub = weston_surface_to_subsurface(surface)))
2547 surface = sub->parent;
2548
2549 return surface;
2550}
2551
Pekka Paalanene67858b2013-04-25 13:57:42 +03002552static void
2553subsurface_set_position(struct wl_client *client,
2554 struct wl_resource *resource, int32_t x, int32_t y)
2555{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002556 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002557
2558 if (!sub)
2559 return;
2560
2561 sub->position.x = x;
2562 sub->position.y = y;
2563 sub->position.set = 1;
2564}
2565
2566static struct weston_subsurface *
2567subsurface_from_surface(struct weston_surface *surface)
2568{
2569 struct weston_subsurface *sub;
2570
2571 sub = weston_surface_to_subsurface(surface);
2572 if (sub)
2573 return sub;
2574
2575 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2576 if (sub->surface == surface)
2577 return sub;
2578
2579 return NULL;
2580}
2581
2582static struct weston_subsurface *
2583subsurface_sibling_check(struct weston_subsurface *sub,
2584 struct weston_surface *surface,
2585 const char *request)
2586{
2587 struct weston_subsurface *sibling;
2588
2589 sibling = subsurface_from_surface(surface);
2590
2591 if (!sibling) {
2592 wl_resource_post_error(sub->resource,
2593 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2594 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002595 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002596 return NULL;
2597 }
2598
2599 if (sibling->parent != sub->parent) {
2600 wl_resource_post_error(sub->resource,
2601 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2602 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002603 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604 return NULL;
2605 }
2606
2607 return sibling;
2608}
2609
2610static void
2611subsurface_place_above(struct wl_client *client,
2612 struct wl_resource *resource,
2613 struct wl_resource *sibling_resource)
2614{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002615 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002616 struct weston_surface *surface =
2617 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618 struct weston_subsurface *sibling;
2619
2620 if (!sub)
2621 return;
2622
2623 sibling = subsurface_sibling_check(sub, surface, "place_above");
2624 if (!sibling)
2625 return;
2626
2627 wl_list_remove(&sub->parent_link_pending);
2628 wl_list_insert(sibling->parent_link_pending.prev,
2629 &sub->parent_link_pending);
2630}
2631
2632static void
2633subsurface_place_below(struct wl_client *client,
2634 struct wl_resource *resource,
2635 struct wl_resource *sibling_resource)
2636{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002637 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002638 struct weston_surface *surface =
2639 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002640 struct weston_subsurface *sibling;
2641
2642 if (!sub)
2643 return;
2644
2645 sibling = subsurface_sibling_check(sub, surface, "place_below");
2646 if (!sibling)
2647 return;
2648
2649 wl_list_remove(&sub->parent_link_pending);
2650 wl_list_insert(&sibling->parent_link_pending,
2651 &sub->parent_link_pending);
2652}
2653
2654static void
2655subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2656{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002657 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002658
2659 if (sub)
2660 sub->synchronized = 1;
2661}
2662
2663static void
2664subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
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
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002668 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002669 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002670
2671 /* If sub became effectively desynchronized, flush. */
2672 if (!weston_subsurface_is_synchronized(sub))
2673 weston_subsurface_synchronized_commit(sub);
2674 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002675}
2676
2677static void
2678weston_subsurface_cache_init(struct weston_subsurface *sub)
2679{
2680 pixman_region32_init(&sub->cached.damage);
2681 pixman_region32_init(&sub->cached.opaque);
2682 pixman_region32_init(&sub->cached.input);
2683 wl_list_init(&sub->cached.frame_callback_list);
2684 sub->cached.buffer_ref.buffer = NULL;
2685}
2686
2687static void
2688weston_subsurface_cache_fini(struct weston_subsurface *sub)
2689{
2690 struct weston_frame_callback *cb, *tmp;
2691
2692 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002693 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694
2695 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2696 pixman_region32_fini(&sub->cached.damage);
2697 pixman_region32_fini(&sub->cached.opaque);
2698 pixman_region32_fini(&sub->cached.input);
2699}
2700
2701static void
2702weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2703{
2704 wl_list_remove(&sub->parent_link);
2705 wl_list_remove(&sub->parent_link_pending);
2706 wl_list_remove(&sub->parent_destroy_listener.link);
2707 sub->parent = NULL;
2708}
2709
2710static void
2711weston_subsurface_destroy(struct weston_subsurface *sub);
2712
2713static void
2714subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2715{
2716 struct weston_subsurface *sub =
2717 container_of(listener, struct weston_subsurface,
2718 surface_destroy_listener);
2719 assert(data == &sub->surface->resource);
2720
2721 /* The protocol object (wl_resource) is left inert. */
2722 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002723 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002724
2725 weston_subsurface_destroy(sub);
2726}
2727
2728static void
2729subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2730{
2731 struct weston_subsurface *sub =
2732 container_of(listener, struct weston_subsurface,
2733 parent_destroy_listener);
2734 assert(data == &sub->parent->resource);
2735 assert(sub->surface != sub->parent);
2736
2737 if (weston_surface_is_mapped(sub->surface))
2738 weston_surface_unmap(sub->surface);
2739
2740 weston_subsurface_unlink_parent(sub);
2741}
2742
2743static void
2744subsurface_resource_destroy(struct wl_resource *resource)
2745{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002746 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002747
2748 if (sub)
2749 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750}
2751
2752static void
2753subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2754{
2755 wl_resource_destroy(resource);
2756}
2757
2758static void
2759weston_subsurface_link_parent(struct weston_subsurface *sub,
2760 struct weston_surface *parent)
2761{
2762 sub->parent = parent;
2763 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002764 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002765 &sub->parent_destroy_listener);
2766
2767 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2768 wl_list_insert(&parent->subsurface_list_pending,
2769 &sub->parent_link_pending);
2770}
2771
2772static void
2773weston_subsurface_link_surface(struct weston_subsurface *sub,
2774 struct weston_surface *surface)
2775{
2776 sub->surface = surface;
2777 sub->surface_destroy_listener.notify =
2778 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002779 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002780 &sub->surface_destroy_listener);
2781}
2782
2783static void
2784weston_subsurface_destroy(struct weston_subsurface *sub)
2785{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002786 struct weston_view *view, *next;
2787
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788 assert(sub->surface);
2789
2790 if (sub->resource) {
2791 assert(weston_surface_to_subsurface(sub->surface) == sub);
2792 assert(sub->parent_destroy_listener.notify ==
2793 subsurface_handle_parent_destroy);
2794
George Kiagiadakised04d382014-06-13 18:10:26 +02002795 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2796 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002797 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002798 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002799
Pekka Paalanene67858b2013-04-25 13:57:42 +03002800 if (sub->parent)
2801 weston_subsurface_unlink_parent(sub);
2802
2803 weston_subsurface_cache_fini(sub);
2804
2805 sub->surface->configure = NULL;
2806 sub->surface->configure_private = NULL;
2807 } else {
2808 /* the dummy weston_subsurface for the parent itself */
2809 assert(sub->parent_destroy_listener.notify == NULL);
2810 wl_list_remove(&sub->parent_link);
2811 wl_list_remove(&sub->parent_link_pending);
2812 }
2813
2814 wl_list_remove(&sub->surface_destroy_listener.link);
2815 free(sub);
2816}
2817
2818static const struct wl_subsurface_interface subsurface_implementation = {
2819 subsurface_destroy,
2820 subsurface_set_position,
2821 subsurface_place_above,
2822 subsurface_place_below,
2823 subsurface_set_sync,
2824 subsurface_set_desync
2825};
2826
2827static struct weston_subsurface *
2828weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2829 struct weston_surface *parent)
2830{
2831 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002832 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002833
2834 sub = calloc(1, sizeof *sub);
2835 if (!sub)
2836 return NULL;
2837
Jason Ekstranda7af7042013-10-12 22:38:11 -05002838 wl_list_init(&sub->unused_views);
2839
Jason Ekstranda85118c2013-06-27 20:17:02 -05002840 sub->resource =
2841 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002842 if (!sub->resource) {
2843 free(sub);
2844 return NULL;
2845 }
2846
Jason Ekstranda85118c2013-06-27 20:17:02 -05002847 wl_resource_set_implementation(sub->resource,
2848 &subsurface_implementation,
2849 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002850 weston_subsurface_link_surface(sub, surface);
2851 weston_subsurface_link_parent(sub, parent);
2852 weston_subsurface_cache_init(sub);
2853 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002854
2855 return sub;
2856}
2857
2858/* Create a dummy subsurface for having the parent itself in its
2859 * sub-surface lists. Makes stacking order manipulation easy.
2860 */
2861static struct weston_subsurface *
2862weston_subsurface_create_for_parent(struct weston_surface *parent)
2863{
2864 struct weston_subsurface *sub;
2865
2866 sub = calloc(1, sizeof *sub);
2867 if (!sub)
2868 return NULL;
2869
2870 weston_subsurface_link_surface(sub, parent);
2871 sub->parent = parent;
2872 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2873 wl_list_insert(&parent->subsurface_list_pending,
2874 &sub->parent_link_pending);
2875
2876 return sub;
2877}
2878
2879static void
2880subcompositor_get_subsurface(struct wl_client *client,
2881 struct wl_resource *resource,
2882 uint32_t id,
2883 struct wl_resource *surface_resource,
2884 struct wl_resource *parent_resource)
2885{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002886 struct weston_surface *surface =
2887 wl_resource_get_user_data(surface_resource);
2888 struct weston_surface *parent =
2889 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890 struct weston_subsurface *sub;
2891 static const char where[] = "get_subsurface: wl_subsurface@";
2892
2893 if (surface == parent) {
2894 wl_resource_post_error(resource,
2895 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2896 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002897 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002898 return;
2899 }
2900
2901 if (weston_surface_to_subsurface(surface)) {
2902 wl_resource_post_error(resource,
2903 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2904 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002905 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002906 return;
2907 }
2908
2909 if (surface->configure) {
2910 wl_resource_post_error(resource,
2911 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2912 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002913 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002914 return;
2915 }
2916
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002917 if (weston_surface_get_main_surface(parent) == surface) {
2918 wl_resource_post_error(resource,
2919 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2920 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002921 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002922 return;
2923 }
2924
Pekka Paalanene67858b2013-04-25 13:57:42 +03002925 /* make sure the parent is in its own list */
2926 if (wl_list_empty(&parent->subsurface_list)) {
2927 if (!weston_subsurface_create_for_parent(parent)) {
2928 wl_resource_post_no_memory(resource);
2929 return;
2930 }
2931 }
2932
2933 sub = weston_subsurface_create(id, surface, parent);
2934 if (!sub) {
2935 wl_resource_post_no_memory(resource);
2936 return;
2937 }
2938
2939 surface->configure = subsurface_configure;
2940 surface->configure_private = sub;
2941}
2942
2943static void
2944subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2945{
2946 wl_resource_destroy(resource);
2947}
2948
2949static const struct wl_subcompositor_interface subcompositor_interface = {
2950 subcompositor_destroy,
2951 subcompositor_get_subsurface
2952};
2953
2954static void
2955bind_subcompositor(struct wl_client *client,
2956 void *data, uint32_t version, uint32_t id)
2957{
2958 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002959 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002960
Jason Ekstranda85118c2013-06-27 20:17:02 -05002961 resource =
2962 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002963 if (resource == NULL) {
2964 wl_client_post_no_memory(client);
2965 return;
2966 }
2967 wl_resource_set_implementation(resource, &subcompositor_interface,
2968 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002969}
2970
2971static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002972weston_compositor_dpms(struct weston_compositor *compositor,
2973 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002974{
2975 struct weston_output *output;
2976
2977 wl_list_for_each(output, &compositor->output_list, link)
2978 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002979 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002980}
2981
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002982WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002983weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002984{
Neil Roberts8b62e202013-09-30 13:14:47 +01002985 uint32_t old_state = compositor->state;
2986
2987 /* The state needs to be changed before emitting the wake
2988 * signal because that may try to schedule a repaint which
2989 * will not work if the compositor is still sleeping */
2990 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2991
2992 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002993 case WESTON_COMPOSITOR_SLEEPING:
2994 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2995 /* fall through */
2996 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002997 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002998 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002999 /* fall through */
3000 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003001 wl_event_source_timer_update(compositor->idle_source,
3002 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003003 }
3004}
3005
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003006WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003007weston_compositor_offscreen(struct weston_compositor *compositor)
3008{
3009 switch (compositor->state) {
3010 case WESTON_COMPOSITOR_OFFSCREEN:
3011 return;
3012 case WESTON_COMPOSITOR_SLEEPING:
3013 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3014 /* fall through */
3015 default:
3016 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3017 wl_event_source_timer_update(compositor->idle_source, 0);
3018 }
3019}
3020
3021WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003022weston_compositor_sleep(struct weston_compositor *compositor)
3023{
3024 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3025 return;
3026
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003027 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003028 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3029 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3030}
3031
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003032static int
3033idle_handler(void *data)
3034{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003035 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003036
3037 if (compositor->idle_inhibit)
3038 return 1;
3039
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003040 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003041 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003042
3043 return 1;
3044}
3045
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003046WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003047weston_plane_init(struct weston_plane *plane,
3048 struct weston_compositor *ec,
3049 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003050{
3051 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003052 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003053 plane->x = x;
3054 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003055 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003056
3057 /* Init the link so that the call to wl_list_remove() when releasing
3058 * the plane without ever stacking doesn't lead to a crash */
3059 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003060}
3061
3062WL_EXPORT void
3063weston_plane_release(struct weston_plane *plane)
3064{
Xiong Zhang97116532013-10-23 13:58:31 +08003065 struct weston_view *view;
3066
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003067 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003068 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003069
Xiong Zhang97116532013-10-23 13:58:31 +08003070 wl_list_for_each(view, &plane->compositor->view_list, link) {
3071 if (view->plane == plane)
3072 view->plane = NULL;
3073 }
3074
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003075 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003076}
3077
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003078WL_EXPORT void
3079weston_compositor_stack_plane(struct weston_compositor *ec,
3080 struct weston_plane *plane,
3081 struct weston_plane *above)
3082{
3083 if (above)
3084 wl_list_insert(above->link.prev, &plane->link);
3085 else
3086 wl_list_insert(&ec->plane_list, &plane->link);
3087}
3088
Casey Dahlin9074db52012-04-19 22:50:09 -04003089static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003090{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003091 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003092}
3093
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003094static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003095bind_output(struct wl_client *client,
3096 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003097{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003098 struct weston_output *output = data;
3099 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003100 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003101
Jason Ekstranda85118c2013-06-27 20:17:02 -05003102 resource = wl_resource_create(client, &wl_output_interface,
3103 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003104 if (resource == NULL) {
3105 wl_client_post_no_memory(client);
3106 return;
3107 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003108
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003109 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003110 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003111
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003112 wl_output_send_geometry(resource,
3113 output->x,
3114 output->y,
3115 output->mm_width,
3116 output->mm_height,
3117 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003118 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003119 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003120 if (version >= 2)
3121 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003122 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003123
3124 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003125 wl_output_send_mode(resource,
3126 mode->flags,
3127 mode->width,
3128 mode->height,
3129 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003130 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003131
3132 if (version >= 2)
3133 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003134}
3135
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003136/* Move other outputs when one is removed so the space remains contiguos. */
3137static void
3138weston_compositor_remove_output(struct weston_compositor *compositor,
3139 struct weston_output *remove_output)
3140{
3141 struct weston_output *output;
3142 int offset = 0;
3143
3144 wl_list_for_each(output, &compositor->output_list, link) {
3145 if (output == remove_output) {
3146 offset = output->width;
3147 continue;
3148 }
3149
3150 if (offset > 0) {
3151 weston_output_move(output,
3152 output->x - offset, output->y);
3153 output->dirty = 1;
3154 }
3155 }
3156}
3157
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003158WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003159weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003160{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003161 output->destroying = 1;
3162
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003163 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003164 wl_list_remove(&output->link);
3165
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003166 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003167 wl_signal_emit(&output->destroy_signal, output);
3168
Richard Hughesafe690c2013-05-02 10:10:04 +01003169 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003170 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003171 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003172 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003173
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003174 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003175}
3176
Scott Moreau1bad5db2012-08-18 01:04:05 -06003177static void
3178weston_output_compute_transform(struct weston_output *output)
3179{
3180 struct weston_matrix transform;
3181 int flip;
3182
3183 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003184 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003185
3186 switch(output->transform) {
3187 case WL_OUTPUT_TRANSFORM_FLIPPED:
3188 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3189 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3190 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003191 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003192 flip = -1;
3193 break;
3194 default:
3195 flip = 1;
3196 break;
3197 }
3198
3199 switch(output->transform) {
3200 case WL_OUTPUT_TRANSFORM_NORMAL:
3201 case WL_OUTPUT_TRANSFORM_FLIPPED:
3202 transform.d[0] = flip;
3203 transform.d[1] = 0;
3204 transform.d[4] = 0;
3205 transform.d[5] = 1;
3206 break;
3207 case WL_OUTPUT_TRANSFORM_90:
3208 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3209 transform.d[0] = 0;
3210 transform.d[1] = -flip;
3211 transform.d[4] = 1;
3212 transform.d[5] = 0;
3213 break;
3214 case WL_OUTPUT_TRANSFORM_180:
3215 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3216 transform.d[0] = -flip;
3217 transform.d[1] = 0;
3218 transform.d[4] = 0;
3219 transform.d[5] = -1;
3220 break;
3221 case WL_OUTPUT_TRANSFORM_270:
3222 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3223 transform.d[0] = 0;
3224 transform.d[1] = flip;
3225 transform.d[4] = -1;
3226 transform.d[5] = 0;
3227 break;
3228 default:
3229 break;
3230 }
3231
3232 weston_matrix_multiply(&output->matrix, &transform);
3233}
3234
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003235WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003236weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003237{
Scott Moreau850ca422012-05-21 15:21:25 -06003238 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003239
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003240 weston_matrix_init(&output->matrix);
3241 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003242 -(output->x + output->width / 2.0),
3243 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003244
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003245 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003246 2.0 / output->width,
3247 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003248
Scott Moreauccbf29d2012-02-22 14:21:41 -07003249 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003250 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003251 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003252 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3253 output->zoom.trans_y, 0);
3254 weston_matrix_scale(&output->matrix, magnification,
3255 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003256 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003257
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003258 weston_output_compute_transform(output);
3259
Scott Moreauccbf29d2012-02-22 14:21:41 -07003260 output->dirty = 0;
3261}
3262
Scott Moreau1bad5db2012-08-18 01:04:05 -06003263static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003264weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003265{
3266 output->transform = transform;
3267
3268 switch (transform) {
3269 case WL_OUTPUT_TRANSFORM_90:
3270 case WL_OUTPUT_TRANSFORM_270:
3271 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3272 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3273 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003274 output->width = output->current_mode->height;
3275 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003276 break;
3277 case WL_OUTPUT_TRANSFORM_NORMAL:
3278 case WL_OUTPUT_TRANSFORM_180:
3279 case WL_OUTPUT_TRANSFORM_FLIPPED:
3280 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003281 output->width = output->current_mode->width;
3282 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003283 break;
3284 default:
3285 break;
3286 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003287
Hardening57388e42013-09-18 23:56:36 +02003288 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003289 output->width /= scale;
3290 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003291}
3292
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003293static void
3294weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003295{
3296 output->x = x;
3297 output->y = y;
3298
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003299 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003300 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003301 output->width,
3302 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003303}
3304
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003305WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003306weston_output_move(struct weston_output *output, int x, int y)
3307{
3308 pixman_region32_t old_region;
3309 struct wl_resource *resource;
3310
3311 output->move_x = x - output->x;
3312 output->move_y = y - output->y;
3313
3314 if (output->move_x == 0 && output->move_y == 0)
3315 return;
3316
3317 pixman_region32_init(&old_region);
3318 pixman_region32_copy(&old_region, &output->region);
3319
3320 weston_output_init_geometry(output, x, y);
3321
3322 output->dirty = 1;
3323
3324 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003325 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003326
3327 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003328 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003329 wl_output_send_geometry(resource,
3330 output->x,
3331 output->y,
3332 output->mm_width,
3333 output->mm_height,
3334 output->subpixel,
3335 output->make,
3336 output->model,
3337 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003338
3339 if (wl_resource_get_version(resource) >= 2)
3340 wl_output_send_done(resource);
3341 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003342}
3343
3344WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003345weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003346 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003347 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003348{
3349 output->compositor = c;
3350 output->x = x;
3351 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003352 output->mm_width = mm_width;
3353 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003354 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003355 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003356
Alexander Larsson0b135062013-05-28 16:23:36 +02003357 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003358 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003359
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003360 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003361 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003362
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003363 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003364 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003365 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003366 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003367
Casey Dahlin58ba1372012-04-19 22:50:08 -04003368 output->id = ffs(~output->compositor->output_id_pool) - 1;
3369 output->compositor->output_id_pool |= 1 << output->id;
3370
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003371 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003372 wl_global_create(c->wl_display, &wl_output_interface, 2,
3373 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003374 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003375}
3376
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003377WL_EXPORT void
3378weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003379 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003380 wl_fixed_t *x, wl_fixed_t *y)
3381{
3382 wl_fixed_t tx, ty;
3383 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003384 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003385
Hardeningff39efa2013-09-18 23:56:35 +02003386 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3387 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003388
3389 switch(output->transform) {
3390 case WL_OUTPUT_TRANSFORM_NORMAL:
3391 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003392 tx = device_x;
3393 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003394 break;
3395 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003396 tx = device_y;
3397 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003398 break;
3399 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003400 tx = width - device_x;
3401 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003402 break;
3403 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003404 tx = width - device_y;
3405 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003406 break;
3407 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003408 tx = width - device_x;
3409 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003410 break;
3411 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003412 tx = width - device_y;
3413 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003414 break;
3415 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003416 tx = device_x;
3417 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003418 break;
3419 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003420 tx = device_y;
3421 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003422 break;
3423 }
3424
Neil Robertseb5a2002014-05-01 16:13:55 +01003425 tx /= output->current_scale;
3426 ty /= output->current_scale;
3427
3428 if (output->zoom.active) {
3429 zoom_scale = output->zoom.spring_z.current;
3430 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3431 output->width / 2.0f *
3432 (zoom_scale + output->zoom.trans_x));
3433 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3434 output->height / 2.0f *
3435 (zoom_scale + output->zoom.trans_y));
3436 tx = wl_fixed_from_double(zx);
3437 ty = wl_fixed_from_double(zy);
3438 }
3439
3440 *x = tx + wl_fixed_from_int(output->x);
3441 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003442}
3443
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003444static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003445destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003446{
Jonny Lamb74130762013-11-26 18:19:46 +01003447 struct weston_surface *surface =
3448 wl_resource_get_user_data(resource);
3449
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003450 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003451 surface->pending.buffer_viewport.buffer.src_width =
3452 wl_fixed_from_int(-1);
3453 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003454 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003455}
3456
3457static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003458viewport_destroy(struct wl_client *client,
3459 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003460{
3461 wl_resource_destroy(resource);
3462}
3463
3464static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003465viewport_set(struct wl_client *client,
3466 struct wl_resource *resource,
3467 wl_fixed_t src_x,
3468 wl_fixed_t src_y,
3469 wl_fixed_t src_width,
3470 wl_fixed_t src_height,
3471 int32_t dst_width,
3472 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003473{
Jonny Lamb74130762013-11-26 18:19:46 +01003474 struct weston_surface *surface =
3475 wl_resource_get_user_data(resource);
3476
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003477 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003478
Jonny Lamb8ae35902013-11-26 18:19:45 +01003479 if (wl_fixed_to_double(src_width) < 0 ||
3480 wl_fixed_to_double(src_height) < 0) {
3481 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003482 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003483 "source dimensions must be non-negative (%fx%f)",
3484 wl_fixed_to_double(src_width),
3485 wl_fixed_to_double(src_height));
3486 return;
3487 }
3488
3489 if (dst_width <= 0 || dst_height <= 0) {
3490 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003491 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003492 "destination dimensions must be positive (%dx%d)",
3493 dst_width, dst_height);
3494 return;
3495 }
3496
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003497 surface->pending.buffer_viewport.buffer.src_x = src_x;
3498 surface->pending.buffer_viewport.buffer.src_y = src_y;
3499 surface->pending.buffer_viewport.buffer.src_width = src_width;
3500 surface->pending.buffer_viewport.buffer.src_height = src_height;
3501 surface->pending.buffer_viewport.surface.width = dst_width;
3502 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003503 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003504}
3505
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003506static void
3507viewport_set_source(struct wl_client *client,
3508 struct wl_resource *resource,
3509 wl_fixed_t src_x,
3510 wl_fixed_t src_y,
3511 wl_fixed_t src_width,
3512 wl_fixed_t src_height)
3513{
3514 struct weston_surface *surface =
3515 wl_resource_get_user_data(resource);
3516
3517 assert(surface->viewport_resource != NULL);
3518
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003519 if (src_width == wl_fixed_from_int(-1) &&
3520 src_height == wl_fixed_from_int(-1)) {
3521 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003522 surface->pending.buffer_viewport.buffer.src_width =
3523 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003524 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003525 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003526 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003527
3528 if (src_width <= 0 || src_height <= 0) {
3529 wl_resource_post_error(resource,
3530 WL_VIEWPORT_ERROR_BAD_VALUE,
3531 "source size must be positive (%fx%f)",
3532 wl_fixed_to_double(src_width),
3533 wl_fixed_to_double(src_height));
3534 return;
3535 }
3536
3537 surface->pending.buffer_viewport.buffer.src_x = src_x;
3538 surface->pending.buffer_viewport.buffer.src_y = src_y;
3539 surface->pending.buffer_viewport.buffer.src_width = src_width;
3540 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003541 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003542}
3543
3544static void
3545viewport_set_destination(struct wl_client *client,
3546 struct wl_resource *resource,
3547 int32_t dst_width,
3548 int32_t dst_height)
3549{
3550 struct weston_surface *surface =
3551 wl_resource_get_user_data(resource);
3552
3553 assert(surface->viewport_resource != NULL);
3554
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003555 if (dst_width == -1 && dst_height == -1) {
3556 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003557 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003558 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003559 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003560 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003561
3562 if (dst_width <= 0 || dst_height <= 0) {
3563 wl_resource_post_error(resource,
3564 WL_VIEWPORT_ERROR_BAD_VALUE,
3565 "destination size must be positive (%dx%d)",
3566 dst_width, dst_height);
3567 return;
3568 }
3569
3570 surface->pending.buffer_viewport.surface.width = dst_width;
3571 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003572 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003573}
3574
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003575static const struct wl_viewport_interface viewport_interface = {
3576 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003577 viewport_set,
3578 viewport_set_source,
3579 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003580};
3581
3582static void
3583scaler_destroy(struct wl_client *client,
3584 struct wl_resource *resource)
3585{
3586 wl_resource_destroy(resource);
3587}
3588
3589static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003590scaler_get_viewport(struct wl_client *client,
3591 struct wl_resource *scaler,
3592 uint32_t id,
3593 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003594{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003595 int version = wl_resource_get_version(scaler);
3596 struct weston_surface *surface =
3597 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003598 struct wl_resource *resource;
3599
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003600 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003601 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003602 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3603 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003604 return;
3605 }
3606
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003607 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003608 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003609 if (resource == NULL) {
3610 wl_client_post_no_memory(client);
3611 return;
3612 }
3613
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003614 wl_resource_set_implementation(resource, &viewport_interface,
3615 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003616
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003617 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003618}
3619
3620static const struct wl_scaler_interface scaler_interface = {
3621 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003622 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003623};
3624
3625static void
3626bind_scaler(struct wl_client *client,
3627 void *data, uint32_t version, uint32_t id)
3628{
3629 struct wl_resource *resource;
3630
3631 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003632 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003633 if (resource == NULL) {
3634 wl_client_post_no_memory(client);
3635 return;
3636 }
3637
3638 wl_resource_set_implementation(resource, &scaler_interface,
3639 NULL, NULL);
3640}
3641
3642static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003643compositor_bind(struct wl_client *client,
3644 void *data, uint32_t version, uint32_t id)
3645{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003646 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003647 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003648
Jason Ekstranda85118c2013-06-27 20:17:02 -05003649 resource = wl_resource_create(client, &wl_compositor_interface,
3650 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003651 if (resource == NULL) {
3652 wl_client_post_no_memory(client);
3653 return;
3654 }
3655
3656 wl_resource_set_implementation(resource, &compositor_interface,
3657 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003658}
3659
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003660static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003661log_uname(void)
3662{
3663 struct utsname usys;
3664
3665 uname(&usys);
3666
3667 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3668 usys.version, usys.machine);
3669}
3670
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003671WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003672weston_environment_get_fd(const char *env)
3673{
3674 char *e, *end;
3675 int fd, flags;
3676
3677 e = getenv(env);
3678 if (!e)
3679 return -1;
3680 fd = strtol(e, &end, 0);
3681 if (*end != '\0')
3682 return -1;
3683
3684 flags = fcntl(fd, F_GETFD);
3685 if (flags == -1)
3686 return -1;
3687
3688 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3689 unsetenv(env);
3690
3691 return fd;
3692}
3693
3694WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003695weston_compositor_init(struct weston_compositor *ec,
3696 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003697 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003698 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003699{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003700 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003701 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003702 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003703
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003704 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003705 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003706 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003707 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003708 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003709 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003710 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003711 wl_signal_init(&ec->idle_signal);
3712 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003713 wl_signal_init(&ec->show_input_panel_signal);
3714 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003715 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003716 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003717 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003718 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003719 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003720 wl_signal_init(&ec->session_signal);
3721 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003722
Casey Dahlin58ba1372012-04-19 22:50:08 -04003723 ec->output_id_pool = 0;
3724
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003725 if (!wl_global_create(display, &wl_compositor_interface, 3,
3726 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003727 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003728
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003729 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3730 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003731 return -1;
3732
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003733 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003734 ec, bind_scaler))
3735 return -1;
3736
Jason Ekstranda7af7042013-10-12 22:38:11 -05003737 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003738 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003739 wl_list_init(&ec->layer_list);
3740 wl_list_init(&ec->seat_list);
3741 wl_list_init(&ec->output_list);
3742 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003743 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003744 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003745 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003746 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003747 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003748
Xiong Zhang97116532013-10-23 13:58:31 +08003749 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003750 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003751
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003752 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3753 weston_config_section_get_string(s, "keymap_rules",
3754 (char **) &xkb_names.rules, NULL);
3755 weston_config_section_get_string(s, "keymap_model",
3756 (char **) &xkb_names.model, NULL);
3757 weston_config_section_get_string(s, "keymap_layout",
3758 (char **) &xkb_names.layout, NULL);
3759 weston_config_section_get_string(s, "keymap_variant",
3760 (char **) &xkb_names.variant, NULL);
3761 weston_config_section_get_string(s, "keymap_options",
3762 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003763
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003764 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3765 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003766
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003767 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003768
3769 wl_data_device_manager_init(ec->wl_display);
3770
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003771 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003772
Daniel Stone725c2c32012-06-22 14:04:36 +01003773 loop = wl_display_get_event_loop(ec->wl_display);
3774 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3775 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3776
3777 ec->input_loop = wl_event_loop_create();
3778
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003779 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3780 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3781
3782 weston_compositor_schedule_repaint(ec);
3783
Daniel Stone725c2c32012-06-22 14:04:36 +01003784 return 0;
3785}
3786
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003787WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003788weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003789{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003790 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003791
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003792 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003793 if (ec->input_loop_source)
3794 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003795
Matt Roper361d2ad2011-08-29 13:52:23 -07003796 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003797 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003798 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003799
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003800 if (ec->renderer)
3801 ec->renderer->destroy(ec);
3802
Daniel Stone325fc2d2012-05-30 16:31:58 +01003803 weston_binding_list_destroy_all(&ec->key_binding_list);
3804 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003805 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003806 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003807 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003808
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003809 weston_plane_release(&ec->primary_plane);
3810
Jonas Ådahlc97af922012-03-28 22:36:09 +02003811 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003812
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003813 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003814}
3815
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003816WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003817weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3818 const struct weston_pointer_grab_interface *interface)
3819{
3820 struct weston_seat *seat;
3821
3822 ec->default_pointer_grab = interface;
3823 wl_list_for_each(seat, &ec->seat_list, link) {
3824 if (seat->pointer) {
3825 weston_pointer_set_default_grab(seat->pointer,
3826 interface);
3827 }
3828 }
3829}
3830
3831WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003832weston_version(int *major, int *minor, int *micro)
3833{
3834 *major = WESTON_VERSION_MAJOR;
3835 *minor = WESTON_VERSION_MINOR;
3836 *micro = WESTON_VERSION_MICRO;
3837}
3838
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003839static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003840 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003841 const char *desc;
3842} capability_strings[] = {
3843 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003844 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003845};
3846
3847static void
3848weston_compositor_log_capabilities(struct weston_compositor *compositor)
3849{
3850 unsigned i;
3851 int yes;
3852
3853 weston_log("Compositor capabilities:\n");
3854 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3855 yes = compositor->capabilities & capability_strings[i].bit;
3856 weston_log_continue(STAMP_SPACE "%s %s\n",
3857 capability_strings[i].desc,
3858 yes ? "yes" : "no");
3859 }
3860}
3861
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003862static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003863{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003864 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003865
Martin Minarik6d118362012-06-07 18:01:59 +02003866 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003867 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003868
3869 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003870}
3871
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003872#ifdef HAVE_LIBUNWIND
3873
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003874static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003875print_backtrace(void)
3876{
3877 unw_cursor_t cursor;
3878 unw_context_t context;
3879 unw_word_t off;
3880 unw_proc_info_t pip;
3881 int ret, i = 0;
3882 char procname[256];
3883 const char *filename;
3884 Dl_info dlinfo;
3885
3886 pip.unwind_info = NULL;
3887 ret = unw_getcontext(&context);
3888 if (ret) {
3889 weston_log("unw_getcontext: %d\n", ret);
3890 return;
3891 }
3892
3893 ret = unw_init_local(&cursor, &context);
3894 if (ret) {
3895 weston_log("unw_init_local: %d\n", ret);
3896 return;
3897 }
3898
3899 ret = unw_step(&cursor);
3900 while (ret > 0) {
3901 ret = unw_get_proc_info(&cursor, &pip);
3902 if (ret) {
3903 weston_log("unw_get_proc_info: %d\n", ret);
3904 break;
3905 }
3906
3907 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3908 if (ret && ret != -UNW_ENOMEM) {
3909 if (ret != -UNW_EUNSPEC)
3910 weston_log("unw_get_proc_name: %d\n", ret);
3911 procname[0] = '?';
3912 procname[1] = 0;
3913 }
3914
3915 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3916 *dlinfo.dli_fname)
3917 filename = dlinfo.dli_fname;
3918 else
3919 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003920
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003921 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3922 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3923
3924 ret = unw_step(&cursor);
3925 if (ret < 0)
3926 weston_log("unw_step: %d\n", ret);
3927 }
3928}
3929
3930#else
3931
3932static void
3933print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003934{
3935 void *buffer[32];
3936 int i, count;
3937 Dl_info info;
3938
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003939 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3940 for (i = 0; i < count; i++) {
3941 dladdr(buffer[i], &info);
3942 weston_log(" [%016lx] %s (%s)\n",
3943 (long) buffer[i],
3944 info.dli_sname ? info.dli_sname : "--",
3945 info.dli_fname);
3946 }
3947}
3948
3949#endif
3950
3951static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003952on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003953{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003954 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003955 * then call the backend restore function, which will switch
3956 * back to the vt we launched from or ungrab X etc and then
3957 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003958 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003959 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003960 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003961
Peter Maatmane5b42e42013-03-27 22:38:53 +01003962 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003963
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003964 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003965
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003966 segv_compositor->restore(segv_compositor);
3967
3968 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003969}
3970
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003971WL_EXPORT void *
3972weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003973{
3974 char path[PATH_MAX];
3975 void *module, *init;
3976
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003977 if (name == NULL)
3978 return NULL;
3979
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003980 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003981 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003982 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003983 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003984
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003985 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3986 if (module) {
3987 weston_log("Module '%s' already loaded\n", path);
3988 dlclose(module);
3989 return NULL;
3990 }
3991
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003992 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003993 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003994 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003995 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003996 return NULL;
3997 }
3998
3999 init = dlsym(module, entrypoint);
4000 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004001 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004002 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004003 return NULL;
4004 }
4005
4006 return init;
4007}
4008
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004009static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004010load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004011 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004012{
4013 const char *p, *end;
4014 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004015 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004016 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004017
4018 if (modules == NULL)
4019 return 0;
4020
4021 p = modules;
4022 while (*p) {
4023 end = strchrnul(p, ',');
4024 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004025 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004026 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004027 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004028 p = end;
4029 while (*p == ',')
4030 p++;
4031
4032 }
4033
4034 return 0;
4035}
4036
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004037static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004038 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4039
4040static const char xdg_wrong_message[] =
4041 "fatal: environment variable XDG_RUNTIME_DIR\n"
4042 "is set to \"%s\", which is not a directory.\n";
4043
4044static const char xdg_wrong_mode_message[] =
4045 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004046 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4047 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004048
4049static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004050 "Refer to your distribution on how to get it, or\n"
4051 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4052 "on how to implement it.\n";
4053
Martin Minarik37032f82012-06-18 20:15:18 +02004054static void
4055verify_xdg_runtime_dir(void)
4056{
4057 char *dir = getenv("XDG_RUNTIME_DIR");
4058 struct stat s;
4059
4060 if (!dir) {
4061 weston_log(xdg_error_message);
4062 weston_log_continue(xdg_detail_message);
4063 exit(EXIT_FAILURE);
4064 }
4065
4066 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4067 weston_log(xdg_wrong_message, dir);
4068 weston_log_continue(xdg_detail_message);
4069 exit(EXIT_FAILURE);
4070 }
4071
4072 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4073 weston_log(xdg_wrong_mode_message,
4074 dir, s.st_mode & 0777, s.st_uid);
4075 weston_log_continue(xdg_detail_message);
4076 }
4077}
4078
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004079static int
4080usage(int error_code)
4081{
4082 fprintf(stderr,
4083 "Usage: weston [OPTIONS]\n\n"
4084 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4085 "Weston supports multiple backends, and depending on which backend is in use\n"
4086 "different options will be accepted.\n\n"
4087
4088
4089 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004090 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004091 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004092 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4093 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004094 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004095 " -S, --socket=NAME\tName of socket to listen on\n"
4096 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004097 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004098 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004099 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004100 " -h, --help\t\tThis help message\n\n");
4101
4102 fprintf(stderr,
4103 "Options for drm-backend.so:\n\n"
4104 " --connector=ID\tBring up only this connector\n"
4105 " --seat=SEAT\t\tThe seat that weston should run on\n"
4106 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004107 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004108 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4109
4110 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004111 "Options for fbdev-backend.so:\n\n"
4112 " --tty=TTY\t\tThe tty to use\n"
4113 " --device=DEVICE\tThe framebuffer device to use\n\n");
4114
4115 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004116 "Options for x11-backend.so:\n\n"
4117 " --width=WIDTH\t\tWidth of X window\n"
4118 " --height=HEIGHT\tHeight of X window\n"
4119 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004120 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004121 " --output-count=COUNT\tCreate multiple outputs\n"
4122 " --no-input\t\tDont create input devices\n\n");
4123
4124 fprintf(stderr,
4125 "Options for wayland-backend.so:\n\n"
4126 " --width=WIDTH\t\tWidth of Wayland surface\n"
4127 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004128 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004129 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004130 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004131 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004132 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004133 " --display=DISPLAY\tWayland display to connect to\n\n");
4134
Pekka Paalanene31e0532013-05-22 18:03:07 +03004135#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4136 fprintf(stderr,
4137 "Options for rpi-backend.so:\n\n"
4138 " --tty=TTY\t\tThe tty to use\n"
4139 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4140 " --transform=TR\tThe output transformation, TR is one of:\n"
4141 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004142 " --opaque-regions\tEnable support for opaque regions, can be "
4143 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004144 "\n");
4145#endif
4146
Hardeningc077a842013-07-08 00:51:35 +02004147#if defined(BUILD_RDP_COMPOSITOR)
4148 fprintf(stderr,
4149 "Options for rdp-backend.so:\n\n"
4150 " --width=WIDTH\t\tWidth of desktop\n"
4151 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004152 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4153 " --address=ADDR\tThe address to bind\n"
4154 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004155 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004156 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4157 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4158 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4159 "\n");
4160#endif
4161
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004162 exit(error_code);
4163}
4164
Peter Maatmane5b42e42013-03-27 22:38:53 +01004165static void
4166catch_signals(void)
4167{
4168 struct sigaction action;
4169
4170 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4171 action.sa_sigaction = on_caught_signal;
4172 sigemptyset(&action.sa_mask);
4173 sigaction(SIGSEGV, &action, NULL);
4174 sigaction(SIGABRT, &action, NULL);
4175}
4176
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004177static void
4178handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4179{
4180 struct wl_client *client = data;
4181
4182 weston_log("Primary client died. Closing...\n");
4183
4184 wl_display_terminate(wl_client_get_display(client));
4185}
4186
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004187int main(int argc, char *argv[])
4188{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004189 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004190 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004191 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004192 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004193 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004194 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004195 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004196 int *argc, char *argv[],
4197 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004198 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004199 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004200 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004201 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004202 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004203 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004204 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004205 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004206 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004207 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004208 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004209 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004210 int32_t noconfig = 0;
4211 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004212 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004213 struct wl_client *primary_client;
4214 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004215
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004216 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004217 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004218 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004219 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4220 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004221 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004222 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004223 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004224 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004225 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004226 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004227
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004228 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004229
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004230 if (help)
4231 usage(EXIT_SUCCESS);
4232
Scott Moreau12245142012-08-29 15:15:58 -06004233 if (version) {
4234 printf(PACKAGE_STRING "\n");
4235 return EXIT_SUCCESS;
4236 }
4237
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004238 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004239
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004240 weston_log("%s\n"
4241 STAMP_SPACE "%s\n"
4242 STAMP_SPACE "Bug reports to: %s\n"
4243 STAMP_SPACE "Build: %s\n",
4244 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004245 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004246 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004247
Martin Minarik37032f82012-06-18 20:15:18 +02004248 verify_xdg_runtime_dir();
4249
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004250 display = wl_display_create();
4251
Tiago Vignatti2116b892011-08-08 05:52:59 -07004252 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004253 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4254 display);
4255 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4256 display);
4257 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4258 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004259
4260 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004261 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4262 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004263
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304264 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4265 ret = EXIT_FAILURE;
4266 goto out_signals;
4267 }
4268
Pekka Paalanen588bee12014-05-07 16:26:25 +03004269 if (noconfig == 0)
4270 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004271 if (config != NULL) {
4272 weston_log("Using config file '%s'\n",
4273 weston_config_get_full_path(config));
4274 } else {
4275 weston_log("Starting with no config file.\n");
4276 }
4277 section = weston_config_get_section(config, "core", NULL, NULL);
4278
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004279 if (option_backend)
4280 backend = strdup(option_backend);
4281 else
4282 weston_config_section_get_string(section, "backend", &backend,
4283 NULL);
4284
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004285 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004286 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004287 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004288 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004289 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004290 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004291 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004292 }
4293
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004294 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004295 free(backend);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304296 if (!backend_init) {
4297 ret = EXIT_FAILURE;
4298 goto out_signals;
4299 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004300
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004301 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004302 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004303 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304304 ret = EXIT_FAILURE;
4305 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004306 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004307
Peter Maatmane5b42e42013-03-27 22:38:53 +01004308 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004309 segv_compositor = ec;
4310
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004311 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004312 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004313
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004314 setenv("WAYLAND_DISPLAY", socket_name, 1);
4315
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004316 if (option_shell)
4317 shell = strdup(option_shell);
4318 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004319 weston_config_section_get_string(section, "shell", &shell,
4320 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004321
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004322 if (load_modules(ec, shell, &argc, argv) < 0) {
4323 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004324 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004325 }
4326 free(shell);
4327
4328 weston_config_section_get_string(section, "modules", &modules, "");
4329 if (load_modules(ec, modules, &argc, argv) < 0) {
4330 free(modules);
4331 goto out;
4332 }
4333 free(modules);
4334
Ossama Othmana50e6e42013-05-14 09:48:26 -07004335 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004336 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004337
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004338 for (i = 1; i < argc; i++)
4339 weston_log("fatal: unhandled option: %s\n", argv[i]);
4340 if (argc > 1) {
4341 ret = EXIT_FAILURE;
4342 goto out;
4343 }
4344
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004345 weston_compositor_log_capabilities(ec);
4346
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004347 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4348 if (server_socket) {
4349 weston_log("Running with single client\n");
4350 fd = strtol(server_socket, &end, 0);
4351 if (*end != '\0')
4352 fd = -1;
4353 } else {
4354 fd = -1;
4355 }
4356
4357 if (fd != -1) {
4358 primary_client = wl_client_create(display, fd);
4359 if (!primary_client) {
4360 weston_log("fatal: failed to add client: %m\n");
4361 ret = EXIT_FAILURE;
4362 goto out;
4363 }
4364 primary_client_destroyed.notify =
4365 handle_primary_client_destroyed;
4366 wl_client_add_destroy_listener(primary_client,
4367 &primary_client_destroyed);
4368 } else {
4369 if (wl_display_add_socket(display, socket_name)) {
4370 weston_log("fatal: failed to add socket: %m\n");
4371 ret = EXIT_FAILURE;
4372 goto out;
4373 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004374 }
4375
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004376 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004377
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004378 wl_display_run(display);
4379
4380 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004381 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004382 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004383
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004384 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004385
Daniel Stone855028f2012-05-01 20:37:10 +01004386 weston_compositor_xkb_destroy(ec);
4387
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004388 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304389
4390out_signals:
4391 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4392 if (signals[i])
4393 wl_event_source_remove(signals[i]);
4394
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004395 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004396
Martin Minarik19e6f262012-06-07 13:08:46 +02004397 weston_log_file_close();
4398
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004399 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004400}