blob: 0abc93bf10d538c3c615a723721bdda7bea00e10 [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"
U. Artie Eoffec08f332013-05-13 15:55:47 -070056#include "subsurface-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
96weston_compositor_build_surface_list(struct weston_compositor *compositor);
97
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
141 output->current_mode = mode;
142 output->current_scale = scale;
143 break;
144 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
145 if (!temporary_mode) {
146 weston_log("already in the native mode\n");
147 return -1;
148 }
149
150 notify_mode_changed = (output->original_mode != output->native_mode);
151
152 ret = output->switch_mode(output, mode);
153 if (ret < 0)
154 return ret;
155
156 if (output->original_scale != output->native_scale) {
157 notify_scale_changed = 1;
158 scale = output->native_scale;
159 output->original_scale = scale;
160 }
161 output->original_mode = 0;
162
163 output->current_scale = output->native_scale;
164 break;
165 default:
166 weston_log("unknown weston_switch_mode_op %d\n", op);
167 break;
168 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200169
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200170 pixman_region32_init(&old_output_region);
171 pixman_region32_copy(&old_output_region, &output->region);
172
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200173 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200174 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200175
176 pixman_region32_init(&output->previous_damage);
177 pixman_region32_init_rect(&output->region, output->x, output->y,
178 output->width, output->height);
179
180 weston_output_update_matrix(output);
181
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200182 /* If a pointer falls outside the outputs new geometry, move it to its
183 * lower-right corner */
184 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400185 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200186 int32_t x, y;
187
188 if (!pointer)
189 continue;
190
191 x = wl_fixed_to_int(pointer->x);
192 y = wl_fixed_to_int(pointer->y);
193
194 if (!pixman_region32_contains_point(&old_output_region,
195 x, y, NULL) ||
196 pixman_region32_contains_point(&output->region,
197 x, y, NULL))
198 continue;
199
200 if (x >= output->x + output->width)
201 x = output->x + output->width - 1;
202 if (y >= output->y + output->height)
203 y = output->y + output->height - 1;
204
205 pointer->x = wl_fixed_from_int(x);
206 pointer->y = wl_fixed_from_int(y);
207 }
208
209 pixman_region32_fini(&old_output_region);
210
Hardening57388e42013-09-18 23:56:36 +0200211 /* notify clients of the changes */
212 if (notify_mode_changed || notify_scale_changed) {
213 wl_resource_for_each(resource, &output->resource_list) {
214 if(notify_mode_changed) {
215 wl_output_send_mode(resource,
216 mode->flags | WL_OUTPUT_MODE_CURRENT,
217 mode->width,
218 mode->height,
219 mode->refresh);
220 }
221
222 if (notify_scale_changed)
223 wl_output_send_scale(resource, scale);
224
225 if (wl_resource_get_version(resource) >= 2)
226 wl_output_send_done(resource);
227 }
228 }
229
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200230 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800231}
232
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400233WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400235{
236 wl_list_insert(&child_process_list, &process->link);
237}
238
Benjamin Franzke06286262011-05-06 19:12:33 +0200239static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200240child_client_exec(int sockfd, const char *path)
241{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500242 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200243 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200244 sigset_t allsigs;
245
246 /* do not give our signal mask to the new process */
247 sigfillset(&allsigs);
248 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200249
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500250 /* Launch clients as the user. */
251 seteuid(getuid());
252
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500253 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
254 * non-CLOEXEC fd to pass through exec. */
255 clientfd = dup(sockfd);
256 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200257 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500258 return;
259 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200260
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500261 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262 setenv("WAYLAND_SOCKET", s, 1);
263
264 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200265 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200266 path);
267}
268
269WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500270weston_client_launch(struct weston_compositor *compositor,
271 struct weston_process *proc,
272 const char *path,
273 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200274{
275 int sv[2];
276 pid_t pid;
277 struct wl_client *client;
278
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300279 weston_log("launching '%s'\n", path);
280
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300281 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200282 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200283 "socketpair failed while launching '%s': %m\n",
284 path);
285 return NULL;
286 }
287
288 pid = fork();
289 if (pid == -1) {
290 close(sv[0]);
291 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200293 "fork failed while launching '%s': %m\n", path);
294 return NULL;
295 }
296
297 if (pid == 0) {
298 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700299 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200300 }
301
302 close(sv[1]);
303
304 client = wl_client_create(compositor->wl_display, sv[0]);
305 if (!client) {
306 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200307 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200308 "wl_client_create failed while launching '%s'.\n",
309 path);
310 return NULL;
311 }
312
313 proc->pid = pid;
314 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500315 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200316
317 return client;
318}
319
320static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300321surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
322{
323 struct weston_surface *surface =
324 container_of(listener, struct weston_surface,
325 pending.buffer_destroy_listener);
326
327 surface->pending.buffer = NULL;
328}
329
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200330static void
331empty_region(pixman_region32_t *region)
332{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300333 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200334 pixman_region32_init(region);
335}
336
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300337static void
338region_init_infinite(pixman_region32_t *region)
339{
340 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
341 UINT32_MAX, UINT32_MAX);
342}
343
Pekka Paalanene67858b2013-04-25 13:57:42 +0300344static struct weston_subsurface *
345weston_surface_to_subsurface(struct weston_surface *surface);
346
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500347WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500348weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500349{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500350 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400351
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200352 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400353 if (surface == NULL)
354 return NULL;
355
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500356 wl_signal_init(&surface->destroy_signal);
357
358 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500359
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500360 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500361 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500362
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500363 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400364 surface->alpha = 1.0;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200365 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400366
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100367 if (compositor->renderer->create_surface(surface) < 0) {
368 free(surface);
369 return NULL;
370 }
371
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200372 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200373 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200374 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200375 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400376 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400377 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100378 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200379
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400380 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500381 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500382 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300383 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200384 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500385 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400386
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200387 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200388 wl_list_insert(&surface->geometry.transformation_list,
389 &surface->transform.position.link);
390 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200391 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200392 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200393 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500394
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300395 surface->pending.buffer_destroy_listener.notify =
396 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300397 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300398 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300399 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300400 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300401
Pekka Paalanene67858b2013-04-25 13:57:42 +0300402 wl_list_init(&surface->subsurface_list);
403 wl_list_init(&surface->subsurface_list_pending);
404
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400405 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500406}
407
Alex Wu8811bf92012-02-28 18:07:54 +0800408WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500409weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200410 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500411{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100412 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500413}
414
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400415WL_EXPORT void
416weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200417 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200418{
419 if (surface->transform.enabled) {
420 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
421
422 weston_matrix_transform(&surface->transform.matrix, &v);
423
424 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200425 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700426 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200427 v.f[3]);
428 *x = 0;
429 *y = 0;
430 return;
431 }
432
433 *x = v.f[0] / v.f[3];
434 *y = v.f[1] / v.f[3];
435 } else {
436 *x = sx + surface->geometry.x;
437 *y = sy + surface->geometry.y;
438 }
439}
440
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500441WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200442weston_transformed_coord(int width, int height,
443 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200444 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200445 float sx, float sy, float *bx, float *by)
446{
447 switch (transform) {
448 case WL_OUTPUT_TRANSFORM_NORMAL:
449 default:
450 *bx = sx;
451 *by = sy;
452 break;
453 case WL_OUTPUT_TRANSFORM_FLIPPED:
454 *bx = width - sx;
455 *by = sy;
456 break;
457 case WL_OUTPUT_TRANSFORM_90:
458 *bx = height - sy;
459 *by = sx;
460 break;
461 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
462 *bx = height - sy;
463 *by = width - sx;
464 break;
465 case WL_OUTPUT_TRANSFORM_180:
466 *bx = width - sx;
467 *by = height - sy;
468 break;
469 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
470 *bx = sx;
471 *by = height - sy;
472 break;
473 case WL_OUTPUT_TRANSFORM_270:
474 *bx = sy;
475 *by = width - sx;
476 break;
477 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
478 *bx = sy;
479 *by = sx;
480 break;
481 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200482
483 *bx *= scale;
484 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200485}
486
487WL_EXPORT pixman_box32_t
488weston_transformed_rect(int width, int height,
489 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200490 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200491 pixman_box32_t rect)
492{
493 float x1, x2, y1, y2;
494
495 pixman_box32_t ret;
496
Alexander Larsson4ea95522013-05-22 14:41:37 +0200497 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200498 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200499 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200500 rect.x2, rect.y2, &x2, &y2);
501
502 if (x1 <= x2) {
503 ret.x1 = x1;
504 ret.x2 = x2;
505 } else {
506 ret.x1 = x2;
507 ret.x2 = x1;
508 }
509
510 if (y1 <= y2) {
511 ret.y1 = y1;
512 ret.y2 = y2;
513 } else {
514 ret.y1 = y2;
515 ret.y2 = y1;
516 }
517
518 return ret;
519}
520
521WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200522weston_surface_to_buffer_float(struct weston_surface *surface,
523 float sx, float sy, float *bx, float *by)
524{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200525 weston_transformed_coord(surface->geometry.width,
526 surface->geometry.height,
527 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200528 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200529 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200530}
531
Alexander Larsson4ea95522013-05-22 14:41:37 +0200532WL_EXPORT void
533weston_surface_to_buffer(struct weston_surface *surface,
534 int sx, int sy, int *bx, int *by)
535{
536 float bxf, byf;
537
538 weston_transformed_coord(surface->geometry.width,
539 surface->geometry.height,
540 surface->buffer_transform,
541 surface->buffer_scale,
542 sx, sy, &bxf, &byf);
543 *bx = floorf(bxf);
544 *by = floorf(byf);
545}
546
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200547WL_EXPORT pixman_box32_t
548weston_surface_to_buffer_rect(struct weston_surface *surface,
549 pixman_box32_t rect)
550{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200551 return weston_transformed_rect(surface->geometry.width,
552 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200553 surface->buffer_transform,
554 surface->buffer_scale,
555 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200556}
557
558WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400559weston_surface_move_to_plane(struct weston_surface *surface,
560 struct weston_plane *plane)
561{
562 if (surface->plane == plane)
563 return;
564
565 weston_surface_damage_below(surface);
566 surface->plane = plane;
567 weston_surface_damage(surface);
568}
569
570WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500571weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200572{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500573 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200574
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500575 pixman_region32_init(&damage);
576 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
577 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400578 pixman_region32_union(&surface->plane->damage,
579 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500580 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200581}
582
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400583static void
584weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
585{
586 uint32_t different = es->output_mask ^ mask;
587 uint32_t entered = mask & different;
588 uint32_t left = es->output_mask & different;
589 struct weston_output *output;
590 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500591 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400592
593 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500594 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400595 return;
596 if (different == 0)
597 return;
598
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500599 client = wl_resource_get_client(es->resource);
600
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400601 wl_list_for_each(output, &es->compositor->output_list, link) {
602 if (1 << output->id & different)
603 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500604 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400605 client);
606 if (resource == NULL)
607 continue;
608 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500609 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400610 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500611 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400612 }
613}
614
615static void
616weston_surface_assign_output(struct weston_surface *es)
617{
618 struct weston_compositor *ec = es->compositor;
619 struct weston_output *output, *new_output;
620 pixman_region32_t region;
621 uint32_t max, area, mask;
622 pixman_box32_t *e;
623
624 new_output = NULL;
625 max = 0;
626 mask = 0;
627 pixman_region32_init(&region);
628 wl_list_for_each(output, &ec->output_list, link) {
629 pixman_region32_intersect(&region, &es->transform.boundingbox,
630 &output->region);
631
632 e = pixman_region32_extents(&region);
633 area = (e->x2 - e->x1) * (e->y2 - e->y1);
634
635 if (area > 0)
636 mask |= 1 << output->id;
637
638 if (area >= max) {
639 new_output = output;
640 max = area;
641 }
642 }
643 pixman_region32_fini(&region);
644
645 es->output = new_output;
646 weston_surface_update_output_mask(es, mask);
647}
648
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200649static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200650surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
651 int32_t width, int32_t height,
652 pixman_region32_t *bbox)
653{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200654 float min_x = HUGE_VALF, min_y = HUGE_VALF;
655 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200656 int32_t s[4][2] = {
657 { sx, sy },
658 { sx, sy + height },
659 { sx + width, sy },
660 { sx + width, sy + height }
661 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200662 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200663 int i;
664
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300665 if (width == 0 || height == 0) {
666 /* avoid rounding empty bbox to 1x1 */
667 pixman_region32_init(bbox);
668 return;
669 }
670
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200671 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200672 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400673 weston_surface_to_global_float(surface,
674 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200675 if (x < min_x)
676 min_x = x;
677 if (x > max_x)
678 max_x = x;
679 if (y < min_y)
680 min_y = y;
681 if (y > max_y)
682 max_y = y;
683 }
684
Pekka Paalanen219b9822012-02-08 15:38:37 +0200685 int_x = floorf(min_x);
686 int_y = floorf(min_y);
687 pixman_region32_init_rect(bbox, int_x, int_y,
688 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200689}
690
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200691static void
692weston_surface_update_transform_disable(struct weston_surface *surface)
693{
694 surface->transform.enabled = 0;
695
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200696 /* round off fractions when not transformed */
697 surface->geometry.x = roundf(surface->geometry.x);
698 surface->geometry.y = roundf(surface->geometry.y);
699
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500700 /* Otherwise identity matrix, but with x and y translation. */
701 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
702 surface->transform.position.matrix.d[12] = surface->geometry.x;
703 surface->transform.position.matrix.d[13] = surface->geometry.y;
704
705 surface->transform.matrix = surface->transform.position.matrix;
706
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500707 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500708 surface->transform.inverse.d[12] = -surface->geometry.x;
709 surface->transform.inverse.d[13] = -surface->geometry.y;
710
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200711 pixman_region32_init_rect(&surface->transform.boundingbox,
712 surface->geometry.x,
713 surface->geometry.y,
714 surface->geometry.width,
715 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500716
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400717 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500718 pixman_region32_copy(&surface->transform.opaque,
719 &surface->opaque);
720 pixman_region32_translate(&surface->transform.opaque,
721 surface->geometry.x,
722 surface->geometry.y);
723 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200724}
725
726static int
727weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200728{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200729 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200730 struct weston_matrix *matrix = &surface->transform.matrix;
731 struct weston_matrix *inverse = &surface->transform.inverse;
732 struct weston_transform *tform;
733
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200734 surface->transform.enabled = 1;
735
736 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300737 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200738 surface->transform.position.matrix.d[12] = surface->geometry.x;
739 surface->transform.position.matrix.d[13] = surface->geometry.y;
740
741 weston_matrix_init(matrix);
742 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
743 weston_matrix_multiply(matrix, &tform->matrix);
744
Pekka Paalanen483243f2013-03-08 14:56:50 +0200745 if (parent)
746 weston_matrix_multiply(matrix, &parent->transform.matrix);
747
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200748 if (weston_matrix_invert(inverse, matrix) < 0) {
749 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200750 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200751 " transformation not invertible.\n", surface);
752 return -1;
753 }
754
755 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
756 surface->geometry.height,
757 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500758
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200759 return 0;
760}
761
762WL_EXPORT void
763weston_surface_update_transform(struct weston_surface *surface)
764{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200765 struct weston_surface *parent = surface->geometry.parent;
766
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200767 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200768 return;
769
Pekka Paalanen483243f2013-03-08 14:56:50 +0200770 if (parent)
771 weston_surface_update_transform(parent);
772
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200773 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200774
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500775 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200776
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200777 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500778 pixman_region32_fini(&surface->transform.opaque);
779 pixman_region32_init(&surface->transform.opaque);
780
Pekka Paalanencd403622012-01-25 13:37:39 +0200781 /* transform.position is always in transformation_list */
782 if (surface->geometry.transformation_list.next ==
783 &surface->transform.position.link &&
784 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200785 &surface->transform.position.link &&
786 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200787 weston_surface_update_transform_disable(surface);
788 } else {
789 if (weston_surface_update_transform_enable(surface) < 0)
790 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200791 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200792
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400793 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200794
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300795 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300796
797 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200798}
799
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200800WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200801weston_surface_geometry_dirty(struct weston_surface *surface)
802{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200803 struct weston_surface *child;
804
805 /*
806 * The invariant: if surface->geometry.dirty, then all surfaces
807 * in surface->geometry.child_list have geometry.dirty too.
808 * Corollary: if not parent->geometry.dirty, then all ancestors
809 * are not dirty.
810 */
811
812 if (surface->transform.dirty)
813 return;
814
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200815 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200816
817 wl_list_for_each(child, &surface->geometry.child_list,
818 geometry.parent_link)
819 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200820}
821
822WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100823weston_surface_to_global_fixed(struct weston_surface *surface,
824 wl_fixed_t sx, wl_fixed_t sy,
825 wl_fixed_t *x, wl_fixed_t *y)
826{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200827 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100828
829 weston_surface_to_global_float(surface,
830 wl_fixed_to_double(sx),
831 wl_fixed_to_double(sy),
832 &xf, &yf);
833 *x = wl_fixed_from_double(xf);
834 *y = wl_fixed_from_double(yf);
835}
836
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400837WL_EXPORT void
838weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200839 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200840{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200841 if (surface->transform.enabled) {
842 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
843
844 weston_matrix_transform(&surface->transform.inverse, &v);
845
846 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200847 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200848 "weston_surface_from_global(), divisor = %g\n",
849 v.f[3]);
850 *sx = 0;
851 *sy = 0;
852 return;
853 }
854
Pekka Paalanencd403622012-01-25 13:37:39 +0200855 *sx = v.f[0] / v.f[3];
856 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200857 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200858 *sx = x - surface->geometry.x;
859 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200860 }
861}
862
863WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100864weston_surface_from_global_fixed(struct weston_surface *surface,
865 wl_fixed_t x, wl_fixed_t y,
866 wl_fixed_t *sx, wl_fixed_t *sy)
867{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200868 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100869
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400870 weston_surface_from_global_float(surface,
871 wl_fixed_to_double(x),
872 wl_fixed_to_double(y),
873 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100874 *sx = wl_fixed_from_double(sxf);
875 *sy = wl_fixed_from_double(syf);
876}
877
878WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200879weston_surface_from_global(struct weston_surface *surface,
880 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
881{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200882 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200883
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400884 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200885 *sx = floorf(sxf);
886 *sy = floorf(syf);
887}
888
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400889WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400890weston_surface_schedule_repaint(struct weston_surface *surface)
891{
892 struct weston_output *output;
893
894 wl_list_for_each(output, &surface->compositor->output_list, link)
895 if (surface->output_mask & (1 << output->id))
896 weston_output_schedule_repaint(output);
897}
898
899WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500900weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500901{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400902 pixman_region32_union_rect(&surface->damage, &surface->damage,
903 0, 0, surface->geometry.width,
904 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200905
Kristian Høgsberg98238702012-08-03 16:29:12 -0400906 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500907}
908
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400909WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500910weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200911 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400912{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200913 surface->geometry.x = x;
914 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200915 surface->geometry.width = width;
916 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200917 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400918}
919
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200920WL_EXPORT void
921weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200922 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200923{
924 surface->geometry.x = x;
925 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200926 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200927}
928
Pekka Paalanen483243f2013-03-08 14:56:50 +0200929static void
930transform_parent_handle_parent_destroy(struct wl_listener *listener,
931 void *data)
932{
933 struct weston_surface *surface =
934 container_of(listener, struct weston_surface,
935 geometry.parent_destroy_listener);
936
937 weston_surface_set_transform_parent(surface, NULL);
938}
939
940WL_EXPORT void
941weston_surface_set_transform_parent(struct weston_surface *surface,
942 struct weston_surface *parent)
943{
944 if (surface->geometry.parent) {
945 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
946 wl_list_remove(&surface->geometry.parent_link);
947 }
948
949 surface->geometry.parent = parent;
950
951 surface->geometry.parent_destroy_listener.notify =
952 transform_parent_handle_parent_destroy;
953 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500954 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200955 &surface->geometry.parent_destroy_listener);
956 wl_list_insert(&parent->geometry.child_list,
957 &surface->geometry.parent_link);
958 }
959
960 weston_surface_geometry_dirty(surface);
961}
962
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300963WL_EXPORT int
964weston_surface_is_mapped(struct weston_surface *surface)
965{
966 if (surface->output)
967 return 1;
968 else
969 return 0;
970}
971
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200972WL_EXPORT int32_t
973weston_surface_buffer_width(struct weston_surface *surface)
974{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200975 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200976 switch (surface->buffer_transform) {
977 case WL_OUTPUT_TRANSFORM_90:
978 case WL_OUTPUT_TRANSFORM_270:
979 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
980 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200981 width = surface->buffer_ref.buffer->height;
982 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200983 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200984 width = surface->buffer_ref.buffer->width;
985 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200986 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200987 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200988}
989
990WL_EXPORT int32_t
991weston_surface_buffer_height(struct weston_surface *surface)
992{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200993 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200994 switch (surface->buffer_transform) {
995 case WL_OUTPUT_TRANSFORM_90:
996 case WL_OUTPUT_TRANSFORM_270:
997 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
998 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200999 height = surface->buffer_ref.buffer->width;
1000 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001001 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001002 height = surface->buffer_ref.buffer->height;
1003 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001004 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001005 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001006}
1007
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001008WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001009weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001010{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001011 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001012
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001013 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001014
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001015 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001016}
1017
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001018WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +02001019weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +01001020 wl_fixed_t x, wl_fixed_t y,
1021 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001022{
1023 struct weston_surface *surface;
1024
1025 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001026 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001027 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +01001028 wl_fixed_to_int(*sx),
1029 wl_fixed_to_int(*sy),
1030 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +02001031 return surface;
1032 }
1033
1034 return NULL;
1035}
1036
1037static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001038weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001039{
Daniel Stone37816df2012-05-16 18:45:18 +01001040 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001041
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001042 if (!compositor->focus)
1043 return;
1044
Daniel Stone37816df2012-05-16 18:45:18 +01001045 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001046 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001047}
1048
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001049WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001050weston_surface_unmap(struct weston_surface *surface)
1051{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001052 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001053
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001054 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001055 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001056 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001057
Daniel Stone4dab5db2012-05-30 16:31:53 +01001058 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001059 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001060 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001061 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001062 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001063 NULL,
1064 wl_fixed_from_int(0),
1065 wl_fixed_from_int(0));
Michael Fua2bb7912013-07-23 15:51:06 +08001066 if (seat->touch && seat->touch->focus == surface)
1067 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001068 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001069
Kristian Høgsberg98238702012-08-03 16:29:12 -04001070 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001071}
1072
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001073struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001074 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001075 struct wl_list link;
1076};
1077
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001078
1079WL_EXPORT void
1080weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001081{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001082 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001083 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001084
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001085 if (--surface->ref_count > 0)
1086 return;
1087
1088 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1089
Pekka Paalanen483243f2013-03-08 14:56:50 +02001090 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001091 assert(wl_list_empty(&surface->subsurface_list_pending));
1092 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001093
Kristian Høgsbergb12e3562013-09-21 21:26:05 -07001094 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001095 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001096
Pekka Paalanenbc106382012-10-10 12:49:31 +03001097 wl_list_for_each_safe(cb, next,
1098 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001099 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001100
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001101 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001102 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001103 pixman_region32_fini(&surface->pending.damage);
1104
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001105 if (surface->pending.buffer)
1106 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1107
Pekka Paalanende685b82012-12-04 15:58:12 +02001108 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001109
Kristian Høgsberg42263852012-09-06 21:59:29 -04001110 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001111
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001112 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001113 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001114 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001115 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001116 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001117
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001118 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001119 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001120
Pekka Paalanen483243f2013-03-08 14:56:50 +02001121 weston_surface_set_transform_parent(surface, NULL);
1122
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001123 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001124}
1125
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001126static void
1127destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001128{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001129 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001130
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001131 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001132}
1133
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001134static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001135weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1136{
1137 struct weston_buffer *buffer =
1138 container_of(listener, struct weston_buffer, destroy_listener);
1139
1140 wl_signal_emit(&buffer->destroy_signal, buffer);
1141 free(buffer);
1142}
1143
1144struct weston_buffer *
1145weston_buffer_from_resource(struct wl_resource *resource)
1146{
1147 struct weston_buffer *buffer;
1148 struct wl_listener *listener;
1149
1150 listener = wl_resource_get_destroy_listener(resource,
1151 weston_buffer_destroy_handler);
1152
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001153 if (listener)
1154 return container_of(listener, struct weston_buffer,
1155 destroy_listener);
1156
1157 buffer = zalloc(sizeof *buffer);
1158 if (buffer == NULL)
1159 return NULL;
1160
1161 buffer->resource = resource;
1162 wl_signal_init(&buffer->destroy_signal);
1163 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001164 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001165 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001166
1167 return buffer;
1168}
1169
1170static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001171weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1172 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001173{
Pekka Paalanende685b82012-12-04 15:58:12 +02001174 struct weston_buffer_reference *ref =
1175 container_of(listener, struct weston_buffer_reference,
1176 destroy_listener);
1177
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001178 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001179 ref->buffer = NULL;
1180}
1181
1182WL_EXPORT void
1183weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001184 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001185{
1186 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001187 ref->buffer->busy_count--;
1188 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001189 assert(wl_resource_get_client(ref->buffer->resource));
1190 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001191 WL_BUFFER_RELEASE);
1192 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001193 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001194 }
1195
Pekka Paalanende685b82012-12-04 15:58:12 +02001196 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001197 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001198 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001199 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001200 }
1201
Pekka Paalanende685b82012-12-04 15:58:12 +02001202 ref->buffer = buffer;
1203 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1204}
1205
1206static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001207weston_surface_attach(struct weston_surface *surface,
1208 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001209{
1210 weston_buffer_reference(&surface->buffer_ref, buffer);
1211
Pekka Paalanena6421c42012-12-04 15:58:10 +02001212 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001213 if (weston_surface_is_mapped(surface))
1214 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001215 }
1216
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001217 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001218}
1219
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001220WL_EXPORT void
1221weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001222{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001223 wl_list_remove(&surface->layer_link);
1224 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001226 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001227}
1228
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001229WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001230weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001231{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001232 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001233
1234 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001236}
1237
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001238WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001239weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001240{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001241 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001242
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001243 pixman_region32_union(&compositor->primary_plane.damage,
1244 &compositor->primary_plane.damage,
1245 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001246 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001247}
1248
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001249static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001250surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001251 pixman_region32_t *opaque)
1252{
Pekka Paalanende685b82012-12-04 15:58:12 +02001253 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001254 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001255 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001256
1257 if (surface->transform.enabled) {
1258 pixman_box32_t *extents;
1259
1260 extents = pixman_region32_extents(&surface->damage);
1261 surface_compute_bbox(surface, extents->x1, extents->y1,
1262 extents->x2 - extents->x1,
1263 extents->y2 - extents->y1,
1264 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001265 pixman_region32_translate(&surface->damage,
1266 -surface->plane->x,
1267 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001268 } else {
1269 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001270 surface->geometry.x - surface->plane->x,
1271 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001272 }
1273
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001274 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001275 pixman_region32_union(&surface->plane->damage,
1276 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001277 empty_region(&surface->damage);
1278 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001279 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001280}
1281
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001282static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001283compositor_accumulate_damage(struct weston_compositor *ec)
1284{
1285 struct weston_plane *plane;
1286 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001287 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001288
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001289 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001290
1291 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001292 pixman_region32_copy(&plane->clip, &clip);
1293
1294 pixman_region32_init(&opaque);
1295
1296 wl_list_for_each(es, &ec->surface_list, link) {
1297 if (es->plane != plane)
1298 continue;
1299
1300 surface_accumulate_damage(es, &opaque);
1301 }
1302
1303 pixman_region32_union(&clip, &clip, &opaque);
1304 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001305 }
1306
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001307 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001308
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001309 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001310 /* Both the renderer and the backend have seen the buffer
1311 * by now. If renderer needs the buffer, it has its own
1312 * reference set. If the backend wants to keep the buffer
1313 * around for migrating the surface into a non-primary plane
1314 * later, keep_buffer is true. Otherwise, drop the core
1315 * reference now, and allow early buffer release. This enables
1316 * clients to use single-buffering.
1317 */
1318 if (!es->keep_buffer)
1319 weston_buffer_reference(&es->buffer_ref, NULL);
1320 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001321}
1322
1323static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001324surface_list_add(struct weston_compositor *compositor,
1325 struct weston_surface *surface)
1326{
1327 struct weston_subsurface *sub;
1328
1329 if (wl_list_empty(&surface->subsurface_list)) {
1330 weston_surface_update_transform(surface);
1331 wl_list_insert(compositor->surface_list.prev, &surface->link);
1332 return;
1333 }
1334
1335 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1336 if (!weston_surface_is_mapped(sub->surface))
1337 continue;
1338
1339 if (sub->surface == surface) {
1340 weston_surface_update_transform(sub->surface);
1341 wl_list_insert(compositor->surface_list.prev,
1342 &sub->surface->link);
1343 } else {
1344 surface_list_add(compositor, sub->surface);
1345 }
1346 }
1347}
1348
1349static void
1350weston_compositor_build_surface_list(struct weston_compositor *compositor)
1351{
1352 struct weston_surface *surface;
1353 struct weston_layer *layer;
1354
1355 wl_list_init(&compositor->surface_list);
1356 wl_list_for_each(layer, &compositor->layer_list, link) {
1357 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1358 surface_list_add(compositor, surface);
1359 }
1360 }
1361}
1362
1363static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001364weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001365{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001366 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001367 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001368 struct weston_animation *animation, *next;
1369 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001370 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001371 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001372
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001373 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001374 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001375
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001376 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001377 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001378 else
1379 wl_list_for_each(es, &ec->surface_list, link)
1380 weston_surface_move_to_plane(es, &ec->primary_plane);
1381
Pekka Paalanene67858b2013-04-25 13:57:42 +03001382 wl_list_init(&frame_callback_list);
1383 wl_list_for_each(es, &ec->surface_list, link) {
1384 if (es->output == output) {
1385 wl_list_insert_list(&frame_callback_list,
1386 &es->frame_callback_list);
1387 wl_list_init(&es->frame_callback_list);
1388 }
1389 }
1390
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001391 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001392
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001393 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001394 pixman_region32_intersect(&output_damage,
1395 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001396 pixman_region32_subtract(&output_damage,
1397 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001398
Scott Moreauccbf29d2012-02-22 14:21:41 -07001399 if (output->dirty)
1400 weston_output_update_matrix(output);
1401
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001402 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001403
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001404 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001405
Kristian Høgsbergef044142011-06-21 15:02:12 -04001406 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001407
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001408 weston_compositor_repick(ec);
1409 wl_event_loop_dispatch(ec->input_loop, 0);
1410
Jonas Ådahldb773762012-06-13 00:01:21 +02001411 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001412 wl_callback_send_done(cb->resource, msecs);
1413 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001414 }
1415
Scott Moreaud64cf212012-06-08 19:40:54 -06001416 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001417 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001418 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001419 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001420}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001421
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001422static int
1423weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001424{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001425 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001426
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001427 wl_event_loop_dispatch(compositor->input_loop, 0);
1428
1429 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001430}
1431
1432WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001433weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001434{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001435 struct weston_compositor *compositor = output->compositor;
1436 struct wl_event_loop *loop =
1437 wl_display_get_event_loop(compositor->wl_display);
1438 int fd;
1439
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001440 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001441 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001442 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001443 return;
1444 }
1445
1446 output->repaint_scheduled = 0;
1447 if (compositor->input_loop_source)
1448 return;
1449
1450 fd = wl_event_loop_get_fd(compositor->input_loop);
1451 compositor->input_loop_source =
1452 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1453 weston_compositor_read_input, compositor);
1454}
1455
1456static void
1457idle_repaint(void *data)
1458{
1459 struct weston_output *output = data;
1460
Jonas Ådahle5a12252013-04-05 23:07:11 +02001461 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001462}
1463
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001464WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001465weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1466{
1467 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001468 if (below != NULL)
1469 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001470}
1471
1472WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001473weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001474{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001475 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001476 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001477
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001478 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1479 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001480 return;
1481
Kristian Høgsbergef044142011-06-21 15:02:12 -04001482 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001483 output->repaint_needed = 1;
1484 if (output->repaint_scheduled)
1485 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001486
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001487 wl_event_loop_add_idle(loop, idle_repaint, output);
1488 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001489
1490 if (compositor->input_loop_source) {
1491 wl_event_source_remove(compositor->input_loop_source);
1492 compositor->input_loop_source = NULL;
1493 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001494}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001495
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001496WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001497weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1498{
1499 struct weston_output *output;
1500
1501 wl_list_for_each(output, &compositor->output_list, link)
1502 weston_output_schedule_repaint(output);
1503}
1504
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001505static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001506surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001507{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001508 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001509}
1510
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001511static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001512surface_attach(struct wl_client *client,
1513 struct wl_resource *resource,
1514 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1515{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001516 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001517 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001518
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001519 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001520 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001521 if (buffer == NULL) {
1522 wl_client_post_no_memory(client);
1523 return;
1524 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001525 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001526
Pekka Paalanende685b82012-12-04 15:58:12 +02001527 /* Attach, attach, without commit in between does not send
1528 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001529 if (surface->pending.buffer)
1530 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001531
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001532 surface->pending.sx = sx;
1533 surface->pending.sy = sy;
1534 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001535 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001536 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001537 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001538 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001539 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001540}
1541
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001542static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001543surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001544 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001545 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001546{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001547 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001548
Pekka Paalanen8e159182012-10-10 12:49:25 +03001549 pixman_region32_union_rect(&surface->pending.damage,
1550 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001551 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001552}
1553
Kristian Høgsberg33418202011-08-16 23:01:28 -04001554static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001555destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001556{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001557 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001558
1559 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001560 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001561}
1562
1563static void
1564surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001565 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001566{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001567 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001568 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001569
1570 cb = malloc(sizeof *cb);
1571 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001572 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001573 return;
1574 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001575
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001576 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1577 callback);
1578 if (cb->resource == NULL) {
1579 free(cb);
1580 wl_resource_post_no_memory(resource);
1581 return;
1582 }
1583
Jason Ekstranda85118c2013-06-27 20:17:02 -05001584 wl_resource_set_implementation(cb->resource, NULL, cb,
1585 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001586
Pekka Paalanenbc106382012-10-10 12:49:31 +03001587 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001588}
1589
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001590static void
1591surface_set_opaque_region(struct wl_client *client,
1592 struct wl_resource *resource,
1593 struct wl_resource *region_resource)
1594{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001595 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001596 struct weston_region *region;
1597
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001598 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001599 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001600 pixman_region32_copy(&surface->pending.opaque,
1601 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001602 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001603 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001604 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001605}
1606
1607static void
1608surface_set_input_region(struct wl_client *client,
1609 struct wl_resource *resource,
1610 struct wl_resource *region_resource)
1611{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001612 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001613 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001614
1615 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001616 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001617 pixman_region32_copy(&surface->pending.input,
1618 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001619 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001620 pixman_region32_fini(&surface->pending.input);
1621 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001622 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001623}
1624
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001625static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001626weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001627{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001628 struct weston_subsurface *sub;
1629
1630 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1631 parent_link_pending) {
1632 wl_list_remove(&sub->parent_link);
1633 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1634 }
1635}
1636
1637static void
1638weston_surface_commit(struct weston_surface *surface)
1639{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001640 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001641 int surface_width = 0;
1642 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001643
Alexander Larsson4ea95522013-05-22 14:41:37 +02001644 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001645 surface->buffer_transform = surface->pending.buffer_transform;
1646
Alexander Larsson4ea95522013-05-22 14:41:37 +02001647 /* wl_surface.set_buffer_scale */
1648 surface->buffer_scale = surface->pending.buffer_scale;
1649
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001650 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001651 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001652 weston_surface_attach(surface, surface->pending.buffer);
1653
Giulio Camuffo184df502013-02-21 11:29:21 +01001654 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001655 surface_width = weston_surface_buffer_width(surface);
1656 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001657 }
1658
1659 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001660 surface->configure(surface,
1661 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001662 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001663
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001664 if (surface->pending.buffer)
1665 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1666 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001667 surface->pending.sx = 0;
1668 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001669 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001670
1671 /* wl_surface.damage */
1672 pixman_region32_union(&surface->damage, &surface->damage,
1673 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001674 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1675 0, 0,
1676 surface->geometry.width,
1677 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001678 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001679
1680 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001681 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001682 surface->geometry.width,
1683 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001684 pixman_region32_intersect(&opaque,
1685 &opaque, &surface->pending.opaque);
1686
1687 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1688 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001689 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001690 }
1691
1692 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001693
1694 /* wl_surface.set_input_region */
1695 pixman_region32_fini(&surface->input);
1696 pixman_region32_init_rect(&surface->input, 0, 0,
1697 surface->geometry.width,
1698 surface->geometry.height);
1699 pixman_region32_intersect(&surface->input,
1700 &surface->input, &surface->pending.input);
1701
Pekka Paalanenbc106382012-10-10 12:49:31 +03001702 /* wl_surface.frame */
1703 wl_list_insert_list(&surface->frame_callback_list,
1704 &surface->pending.frame_callback_list);
1705 wl_list_init(&surface->pending.frame_callback_list);
1706
Pekka Paalanene67858b2013-04-25 13:57:42 +03001707 weston_surface_commit_subsurface_order(surface);
1708
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001709 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001710}
1711
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001712static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001713weston_subsurface_commit(struct weston_subsurface *sub);
1714
1715static void
1716weston_subsurface_parent_commit(struct weston_subsurface *sub,
1717 int parent_is_synchronized);
1718
1719static void
1720surface_commit(struct wl_client *client, struct wl_resource *resource)
1721{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001722 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001723 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1724
1725 if (sub) {
1726 weston_subsurface_commit(sub);
1727 return;
1728 }
1729
1730 weston_surface_commit(surface);
1731
1732 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1733 if (sub->surface != surface)
1734 weston_subsurface_parent_commit(sub, 0);
1735 }
1736}
1737
1738static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001739surface_set_buffer_transform(struct wl_client *client,
1740 struct wl_resource *resource, int transform)
1741{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001742 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001743
1744 surface->pending.buffer_transform = transform;
1745}
1746
Alexander Larsson4ea95522013-05-22 14:41:37 +02001747static void
1748surface_set_buffer_scale(struct wl_client *client,
1749 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001750 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001751{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001752 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001753
1754 surface->pending.buffer_scale = scale;
1755}
1756
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001757static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001758 surface_destroy,
1759 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001760 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001761 surface_frame,
1762 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001763 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001764 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001765 surface_set_buffer_transform,
1766 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001767};
1768
1769static void
1770compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001771 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001772{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001773 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001774 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001775
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001776 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001777 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001778 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001779 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001780 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001781
Jason Ekstranda85118c2013-06-27 20:17:02 -05001782 surface->resource =
1783 wl_resource_create(client, &wl_surface_interface,
1784 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001785 if (surface->resource == NULL) {
1786 weston_surface_destroy(surface);
1787 wl_resource_post_no_memory(resource);
1788 return;
1789 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001790 wl_resource_set_implementation(surface->resource, &surface_interface,
1791 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001792}
1793
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001794static void
1795destroy_region(struct wl_resource *resource)
1796{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001797 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001798
1799 pixman_region32_fini(&region->region);
1800 free(region);
1801}
1802
1803static void
1804region_destroy(struct wl_client *client, struct wl_resource *resource)
1805{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001806 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001807}
1808
1809static void
1810region_add(struct wl_client *client, struct wl_resource *resource,
1811 int32_t x, int32_t y, int32_t width, int32_t height)
1812{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001813 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001814
1815 pixman_region32_union_rect(&region->region, &region->region,
1816 x, y, width, height);
1817}
1818
1819static void
1820region_subtract(struct wl_client *client, struct wl_resource *resource,
1821 int32_t x, int32_t y, int32_t width, int32_t height)
1822{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001823 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001824 pixman_region32_t rect;
1825
1826 pixman_region32_init_rect(&rect, x, y, width, height);
1827 pixman_region32_subtract(&region->region, &region->region, &rect);
1828 pixman_region32_fini(&rect);
1829}
1830
1831static const struct wl_region_interface region_interface = {
1832 region_destroy,
1833 region_add,
1834 region_subtract
1835};
1836
1837static void
1838compositor_create_region(struct wl_client *client,
1839 struct wl_resource *resource, uint32_t id)
1840{
1841 struct weston_region *region;
1842
1843 region = malloc(sizeof *region);
1844 if (region == NULL) {
1845 wl_resource_post_no_memory(resource);
1846 return;
1847 }
1848
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001849 pixman_region32_init(&region->region);
1850
Jason Ekstranda85118c2013-06-27 20:17:02 -05001851 region->resource =
1852 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001853 if (region->resource == NULL) {
1854 free(region);
1855 wl_resource_post_no_memory(resource);
1856 return;
1857 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001858 wl_resource_set_implementation(region->resource, &region_interface,
1859 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001860}
1861
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001862static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001863 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001864 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001865};
1866
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001867static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001868weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1869{
1870 struct weston_surface *surface = sub->surface;
1871 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001872 int surface_width = 0;
1873 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001874
Alexander Larsson4ea95522013-05-22 14:41:37 +02001875 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001876 surface->buffer_transform = sub->cached.buffer_transform;
1877
Alexander Larsson4ea95522013-05-22 14:41:37 +02001878 /* wl_surface.set_buffer_scale */
1879 surface->buffer_scale = sub->cached.buffer_scale;
1880
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881 /* wl_surface.attach */
1882 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1883 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1884 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1885
1886 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001887 surface_width = weston_surface_buffer_width(surface);
1888 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001889 }
1890
1891 if (surface->configure && sub->cached.newly_attached)
1892 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001893 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001894 sub->cached.sx = 0;
1895 sub->cached.sy = 0;
1896 sub->cached.newly_attached = 0;
1897
1898 /* wl_surface.damage */
1899 pixman_region32_union(&surface->damage, &surface->damage,
1900 &sub->cached.damage);
1901 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1902 0, 0,
1903 surface->geometry.width,
1904 surface->geometry.height);
1905 empty_region(&sub->cached.damage);
1906
1907 /* wl_surface.set_opaque_region */
1908 pixman_region32_init_rect(&opaque, 0, 0,
1909 surface->geometry.width,
1910 surface->geometry.height);
1911 pixman_region32_intersect(&opaque,
1912 &opaque, &sub->cached.opaque);
1913
1914 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1915 pixman_region32_copy(&surface->opaque, &opaque);
1916 weston_surface_geometry_dirty(surface);
1917 }
1918
1919 pixman_region32_fini(&opaque);
1920
1921 /* wl_surface.set_input_region */
1922 pixman_region32_fini(&surface->input);
1923 pixman_region32_init_rect(&surface->input, 0, 0,
1924 surface->geometry.width,
1925 surface->geometry.height);
1926 pixman_region32_intersect(&surface->input,
1927 &surface->input, &sub->cached.input);
1928
1929 /* wl_surface.frame */
1930 wl_list_insert_list(&surface->frame_callback_list,
1931 &sub->cached.frame_callback_list);
1932 wl_list_init(&sub->cached.frame_callback_list);
1933
1934 weston_surface_commit_subsurface_order(surface);
1935
1936 weston_surface_schedule_repaint(surface);
1937
1938 sub->cached.has_data = 0;
1939}
1940
1941static void
1942weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1943{
1944 struct weston_surface *surface = sub->surface;
1945
1946 /*
1947 * If this commit would cause the surface to move by the
1948 * attach(dx, dy) parameters, the old damage region must be
1949 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02001950 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03001951 */
1952 pixman_region32_translate(&sub->cached.damage,
1953 -surface->pending.sx, -surface->pending.sy);
1954 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1955 &surface->pending.damage);
1956 empty_region(&surface->pending.damage);
1957
1958 if (surface->pending.newly_attached) {
1959 sub->cached.newly_attached = 1;
1960 weston_buffer_reference(&sub->cached.buffer_ref,
1961 surface->pending.buffer);
1962 }
1963 sub->cached.sx += surface->pending.sx;
1964 sub->cached.sy += surface->pending.sy;
1965 surface->pending.sx = 0;
1966 surface->pending.sy = 0;
1967 surface->pending.newly_attached = 0;
1968
1969 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001970 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001971
1972 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1973
1974 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1975
1976 wl_list_insert_list(&sub->cached.frame_callback_list,
1977 &surface->pending.frame_callback_list);
1978 wl_list_init(&surface->pending.frame_callback_list);
1979
1980 sub->cached.has_data = 1;
1981}
1982
1983static int
1984weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1985{
1986 while (sub) {
1987 if (sub->synchronized)
1988 return 1;
1989
1990 if (!sub->parent)
1991 return 0;
1992
1993 sub = weston_surface_to_subsurface(sub->parent);
1994 }
1995
1996 return 0;
1997}
1998
1999static void
2000weston_subsurface_commit(struct weston_subsurface *sub)
2001{
2002 struct weston_surface *surface = sub->surface;
2003 struct weston_subsurface *tmp;
2004
2005 /* Recursive check for effectively synchronized. */
2006 if (weston_subsurface_is_synchronized(sub)) {
2007 weston_subsurface_commit_to_cache(sub);
2008 } else {
2009 if (sub->cached.has_data) {
2010 /* flush accumulated state from cache */
2011 weston_subsurface_commit_to_cache(sub);
2012 weston_subsurface_commit_from_cache(sub);
2013 } else {
2014 weston_surface_commit(surface);
2015 }
2016
2017 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2018 if (tmp->surface != surface)
2019 weston_subsurface_parent_commit(tmp, 0);
2020 }
2021 }
2022}
2023
2024static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002025weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002026{
2027 struct weston_surface *surface = sub->surface;
2028 struct weston_subsurface *tmp;
2029
Pekka Paalanene67858b2013-04-25 13:57:42 +03002030 /* From now on, commit_from_cache the whole sub-tree, regardless of
2031 * the synchronized mode of each child. This sub-surface or some
2032 * of its ancestors were synchronized, so we are synchronized
2033 * all the way down.
2034 */
2035
2036 if (sub->cached.has_data)
2037 weston_subsurface_commit_from_cache(sub);
2038
2039 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2040 if (tmp->surface != surface)
2041 weston_subsurface_parent_commit(tmp, 1);
2042 }
2043}
2044
2045static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002046weston_subsurface_parent_commit(struct weston_subsurface *sub,
2047 int parent_is_synchronized)
2048{
2049 if (sub->position.set) {
2050 weston_surface_set_position(sub->surface,
2051 sub->position.x, sub->position.y);
2052 sub->position.set = 0;
2053 }
2054
2055 if (parent_is_synchronized || sub->synchronized)
2056 weston_subsurface_synchronized_commit(sub);
2057}
2058
2059static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002060subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2061 int32_t width, int32_t height)
2062{
2063 struct weston_compositor *compositor = surface->compositor;
2064
2065 weston_surface_configure(surface,
2066 surface->geometry.x + dx,
2067 surface->geometry.y + dy,
2068 width, height);
2069
2070 /* No need to check parent mappedness, because if parent is not
2071 * mapped, parent is not in a visible layer, so this sub-surface
2072 * will not be drawn either.
2073 */
2074 if (!weston_surface_is_mapped(surface)) {
2075 wl_list_init(&surface->layer_link);
2076
2077 /* Cannot call weston_surface_update_transform(),
2078 * because that would call it also for the parent surface,
2079 * which might not be mapped yet. That would lead to
2080 * inconsistent state, where the window could never be
2081 * mapped.
2082 *
2083 * Instead just assing any output, to make
2084 * weston_surface_is_mapped() return true, so that when the
2085 * parent surface does get mapped, this one will get
2086 * included, too. See surface_list_add().
2087 */
2088 assert(!wl_list_empty(&compositor->output_list));
2089 surface->output = container_of(compositor->output_list.next,
2090 struct weston_output, link);
2091 }
2092}
2093
2094static struct weston_subsurface *
2095weston_surface_to_subsurface(struct weston_surface *surface)
2096{
2097 if (surface->configure == subsurface_configure)
2098 return surface->configure_private;
2099
2100 return NULL;
2101}
2102
Pekka Paalanen01388e22013-04-25 13:57:44 +03002103WL_EXPORT struct weston_surface *
2104weston_surface_get_main_surface(struct weston_surface *surface)
2105{
2106 struct weston_subsurface *sub;
2107
2108 while (surface && (sub = weston_surface_to_subsurface(surface)))
2109 surface = sub->parent;
2110
2111 return surface;
2112}
2113
Pekka Paalanene67858b2013-04-25 13:57:42 +03002114static void
2115subsurface_set_position(struct wl_client *client,
2116 struct wl_resource *resource, int32_t x, int32_t y)
2117{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002118 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002119
2120 if (!sub)
2121 return;
2122
2123 sub->position.x = x;
2124 sub->position.y = y;
2125 sub->position.set = 1;
2126}
2127
2128static struct weston_subsurface *
2129subsurface_from_surface(struct weston_surface *surface)
2130{
2131 struct weston_subsurface *sub;
2132
2133 sub = weston_surface_to_subsurface(surface);
2134 if (sub)
2135 return sub;
2136
2137 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2138 if (sub->surface == surface)
2139 return sub;
2140
2141 return NULL;
2142}
2143
2144static struct weston_subsurface *
2145subsurface_sibling_check(struct weston_subsurface *sub,
2146 struct weston_surface *surface,
2147 const char *request)
2148{
2149 struct weston_subsurface *sibling;
2150
2151 sibling = subsurface_from_surface(surface);
2152
2153 if (!sibling) {
2154 wl_resource_post_error(sub->resource,
2155 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2156 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002157 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002158 return NULL;
2159 }
2160
2161 if (sibling->parent != sub->parent) {
2162 wl_resource_post_error(sub->resource,
2163 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2164 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002165 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002166 return NULL;
2167 }
2168
2169 return sibling;
2170}
2171
2172static void
2173subsurface_place_above(struct wl_client *client,
2174 struct wl_resource *resource,
2175 struct wl_resource *sibling_resource)
2176{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002177 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002178 struct weston_surface *surface =
2179 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002180 struct weston_subsurface *sibling;
2181
2182 if (!sub)
2183 return;
2184
2185 sibling = subsurface_sibling_check(sub, surface, "place_above");
2186 if (!sibling)
2187 return;
2188
2189 wl_list_remove(&sub->parent_link_pending);
2190 wl_list_insert(sibling->parent_link_pending.prev,
2191 &sub->parent_link_pending);
2192}
2193
2194static void
2195subsurface_place_below(struct wl_client *client,
2196 struct wl_resource *resource,
2197 struct wl_resource *sibling_resource)
2198{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002199 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002200 struct weston_surface *surface =
2201 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002202 struct weston_subsurface *sibling;
2203
2204 if (!sub)
2205 return;
2206
2207 sibling = subsurface_sibling_check(sub, surface, "place_below");
2208 if (!sibling)
2209 return;
2210
2211 wl_list_remove(&sub->parent_link_pending);
2212 wl_list_insert(&sibling->parent_link_pending,
2213 &sub->parent_link_pending);
2214}
2215
2216static void
2217subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2218{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002219 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002220
2221 if (sub)
2222 sub->synchronized = 1;
2223}
2224
2225static void
2226subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2227{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002228 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002229
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002230 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002232
2233 /* If sub became effectively desynchronized, flush. */
2234 if (!weston_subsurface_is_synchronized(sub))
2235 weston_subsurface_synchronized_commit(sub);
2236 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237}
2238
2239static void
2240weston_subsurface_cache_init(struct weston_subsurface *sub)
2241{
2242 pixman_region32_init(&sub->cached.damage);
2243 pixman_region32_init(&sub->cached.opaque);
2244 pixman_region32_init(&sub->cached.input);
2245 wl_list_init(&sub->cached.frame_callback_list);
2246 sub->cached.buffer_ref.buffer = NULL;
2247}
2248
2249static void
2250weston_subsurface_cache_fini(struct weston_subsurface *sub)
2251{
2252 struct weston_frame_callback *cb, *tmp;
2253
2254 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002255 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002256
2257 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2258 pixman_region32_fini(&sub->cached.damage);
2259 pixman_region32_fini(&sub->cached.opaque);
2260 pixman_region32_fini(&sub->cached.input);
2261}
2262
2263static void
2264weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2265{
2266 wl_list_remove(&sub->parent_link);
2267 wl_list_remove(&sub->parent_link_pending);
2268 wl_list_remove(&sub->parent_destroy_listener.link);
2269 sub->parent = NULL;
2270}
2271
2272static void
2273weston_subsurface_destroy(struct weston_subsurface *sub);
2274
2275static void
2276subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2277{
2278 struct weston_subsurface *sub =
2279 container_of(listener, struct weston_subsurface,
2280 surface_destroy_listener);
2281 assert(data == &sub->surface->resource);
2282
2283 /* The protocol object (wl_resource) is left inert. */
2284 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002285 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002286
2287 weston_subsurface_destroy(sub);
2288}
2289
2290static void
2291subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2292{
2293 struct weston_subsurface *sub =
2294 container_of(listener, struct weston_subsurface,
2295 parent_destroy_listener);
2296 assert(data == &sub->parent->resource);
2297 assert(sub->surface != sub->parent);
2298
2299 if (weston_surface_is_mapped(sub->surface))
2300 weston_surface_unmap(sub->surface);
2301
2302 weston_subsurface_unlink_parent(sub);
2303}
2304
2305static void
2306subsurface_resource_destroy(struct wl_resource *resource)
2307{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002308 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002309
2310 if (sub)
2311 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002312}
2313
2314static void
2315subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2316{
2317 wl_resource_destroy(resource);
2318}
2319
2320static void
2321weston_subsurface_link_parent(struct weston_subsurface *sub,
2322 struct weston_surface *parent)
2323{
2324 sub->parent = parent;
2325 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002326 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002327 &sub->parent_destroy_listener);
2328
2329 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2330 wl_list_insert(&parent->subsurface_list_pending,
2331 &sub->parent_link_pending);
2332}
2333
2334static void
2335weston_subsurface_link_surface(struct weston_subsurface *sub,
2336 struct weston_surface *surface)
2337{
2338 sub->surface = surface;
2339 sub->surface_destroy_listener.notify =
2340 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002341 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002342 &sub->surface_destroy_listener);
2343}
2344
2345static void
2346weston_subsurface_destroy(struct weston_subsurface *sub)
2347{
2348 assert(sub->surface);
2349
2350 if (sub->resource) {
2351 assert(weston_surface_to_subsurface(sub->surface) == sub);
2352 assert(sub->parent_destroy_listener.notify ==
2353 subsurface_handle_parent_destroy);
2354
2355 weston_surface_set_transform_parent(sub->surface, NULL);
2356 if (sub->parent)
2357 weston_subsurface_unlink_parent(sub);
2358
2359 weston_subsurface_cache_fini(sub);
2360
2361 sub->surface->configure = NULL;
2362 sub->surface->configure_private = NULL;
2363 } else {
2364 /* the dummy weston_subsurface for the parent itself */
2365 assert(sub->parent_destroy_listener.notify == NULL);
2366 wl_list_remove(&sub->parent_link);
2367 wl_list_remove(&sub->parent_link_pending);
2368 }
2369
2370 wl_list_remove(&sub->surface_destroy_listener.link);
2371 free(sub);
2372}
2373
2374static const struct wl_subsurface_interface subsurface_implementation = {
2375 subsurface_destroy,
2376 subsurface_set_position,
2377 subsurface_place_above,
2378 subsurface_place_below,
2379 subsurface_set_sync,
2380 subsurface_set_desync
2381};
2382
2383static struct weston_subsurface *
2384weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2385 struct weston_surface *parent)
2386{
2387 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002388 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002389
2390 sub = calloc(1, sizeof *sub);
2391 if (!sub)
2392 return NULL;
2393
Jason Ekstranda85118c2013-06-27 20:17:02 -05002394 sub->resource =
2395 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002396 if (!sub->resource) {
2397 free(sub);
2398 return NULL;
2399 }
2400
Jason Ekstranda85118c2013-06-27 20:17:02 -05002401 wl_resource_set_implementation(sub->resource,
2402 &subsurface_implementation,
2403 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002404 weston_subsurface_link_surface(sub, surface);
2405 weston_subsurface_link_parent(sub, parent);
2406 weston_subsurface_cache_init(sub);
2407 sub->synchronized = 1;
2408 weston_surface_set_transform_parent(surface, parent);
2409
2410 return sub;
2411}
2412
2413/* Create a dummy subsurface for having the parent itself in its
2414 * sub-surface lists. Makes stacking order manipulation easy.
2415 */
2416static struct weston_subsurface *
2417weston_subsurface_create_for_parent(struct weston_surface *parent)
2418{
2419 struct weston_subsurface *sub;
2420
2421 sub = calloc(1, sizeof *sub);
2422 if (!sub)
2423 return NULL;
2424
2425 weston_subsurface_link_surface(sub, parent);
2426 sub->parent = parent;
2427 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2428 wl_list_insert(&parent->subsurface_list_pending,
2429 &sub->parent_link_pending);
2430
2431 return sub;
2432}
2433
2434static void
2435subcompositor_get_subsurface(struct wl_client *client,
2436 struct wl_resource *resource,
2437 uint32_t id,
2438 struct wl_resource *surface_resource,
2439 struct wl_resource *parent_resource)
2440{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002441 struct weston_surface *surface =
2442 wl_resource_get_user_data(surface_resource);
2443 struct weston_surface *parent =
2444 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002445 struct weston_subsurface *sub;
2446 static const char where[] = "get_subsurface: wl_subsurface@";
2447
2448 if (surface == parent) {
2449 wl_resource_post_error(resource,
2450 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2451 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002452 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453 return;
2454 }
2455
2456 if (weston_surface_to_subsurface(surface)) {
2457 wl_resource_post_error(resource,
2458 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2459 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002460 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002461 return;
2462 }
2463
2464 if (surface->configure) {
2465 wl_resource_post_error(resource,
2466 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2467 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002468 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002469 return;
2470 }
2471
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002472 if (weston_surface_get_main_surface(parent) == surface) {
2473 wl_resource_post_error(resource,
2474 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2475 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002476 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002477 return;
2478 }
2479
Pekka Paalanene67858b2013-04-25 13:57:42 +03002480 /* make sure the parent is in its own list */
2481 if (wl_list_empty(&parent->subsurface_list)) {
2482 if (!weston_subsurface_create_for_parent(parent)) {
2483 wl_resource_post_no_memory(resource);
2484 return;
2485 }
2486 }
2487
2488 sub = weston_subsurface_create(id, surface, parent);
2489 if (!sub) {
2490 wl_resource_post_no_memory(resource);
2491 return;
2492 }
2493
2494 surface->configure = subsurface_configure;
2495 surface->configure_private = sub;
2496}
2497
2498static void
2499subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2500{
2501 wl_resource_destroy(resource);
2502}
2503
2504static const struct wl_subcompositor_interface subcompositor_interface = {
2505 subcompositor_destroy,
2506 subcompositor_get_subsurface
2507};
2508
2509static void
2510bind_subcompositor(struct wl_client *client,
2511 void *data, uint32_t version, uint32_t id)
2512{
2513 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002514 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002515
Jason Ekstranda85118c2013-06-27 20:17:02 -05002516 resource =
2517 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002518 if (resource == NULL) {
2519 wl_client_post_no_memory(client);
2520 return;
2521 }
2522 wl_resource_set_implementation(resource, &subcompositor_interface,
2523 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002524}
2525
2526static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002527weston_compositor_dpms(struct weston_compositor *compositor,
2528 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002529{
2530 struct weston_output *output;
2531
2532 wl_list_for_each(output, &compositor->output_list, link)
2533 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002534 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002535}
2536
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002537WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002538weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002539{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002540 switch (compositor->state) {
2541 case WESTON_COMPOSITOR_SLEEPING:
2542 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2543 /* fall through */
2544 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002545 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002546 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002547 /* fall through */
2548 default:
2549 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2550 wl_event_source_timer_update(compositor->idle_source,
2551 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002552 }
2553}
2554
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002555WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002556weston_compositor_offscreen(struct weston_compositor *compositor)
2557{
2558 switch (compositor->state) {
2559 case WESTON_COMPOSITOR_OFFSCREEN:
2560 return;
2561 case WESTON_COMPOSITOR_SLEEPING:
2562 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2563 /* fall through */
2564 default:
2565 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2566 wl_event_source_timer_update(compositor->idle_source, 0);
2567 }
2568}
2569
2570WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002571weston_compositor_sleep(struct weston_compositor *compositor)
2572{
2573 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2574 return;
2575
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002576 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002577 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2578 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2579}
2580
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002581static int
2582idle_handler(void *data)
2583{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002584 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002585
2586 if (compositor->idle_inhibit)
2587 return 1;
2588
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002589 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002590 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002591
2592 return 1;
2593}
2594
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002595WL_EXPORT void
2596weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2597{
2598 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002599 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002600 plane->x = x;
2601 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002602
2603 /* Init the link so that the call to wl_list_remove() when releasing
2604 * the plane without ever stacking doesn't lead to a crash */
2605 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002606}
2607
2608WL_EXPORT void
2609weston_plane_release(struct weston_plane *plane)
2610{
2611 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002612 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002613
2614 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002615}
2616
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002617WL_EXPORT void
2618weston_compositor_stack_plane(struct weston_compositor *ec,
2619 struct weston_plane *plane,
2620 struct weston_plane *above)
2621{
2622 if (above)
2623 wl_list_insert(above->link.prev, &plane->link);
2624 else
2625 wl_list_insert(&ec->plane_list, &plane->link);
2626}
2627
Casey Dahlin9074db52012-04-19 22:50:09 -04002628static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002629{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002630 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002631}
2632
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002633static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002634bind_output(struct wl_client *client,
2635 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002636{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002637 struct weston_output *output = data;
2638 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002639 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002640
Jason Ekstranda85118c2013-06-27 20:17:02 -05002641 resource = wl_resource_create(client, &wl_output_interface,
2642 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002643 if (resource == NULL) {
2644 wl_client_post_no_memory(client);
2645 return;
2646 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002647
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002648 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002649 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002650
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002651 wl_output_send_geometry(resource,
2652 output->x,
2653 output->y,
2654 output->mm_width,
2655 output->mm_height,
2656 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002657 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002658 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002659 if (version >= 2)
2660 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002661 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002662
2663 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002664 wl_output_send_mode(resource,
2665 mode->flags,
2666 mode->width,
2667 mode->height,
2668 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002669 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002670
2671 if (version >= 2)
2672 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002673}
2674
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002675WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002676weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002677{
Richard Hughes64ddde12013-05-01 21:52:10 +01002678 wl_signal_emit(&output->destroy_signal, output);
2679
Richard Hughesafe690c2013-05-02 10:10:04 +01002680 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002681 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002682 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002683 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002684
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002685 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002686}
2687
Scott Moreau1bad5db2012-08-18 01:04:05 -06002688static void
2689weston_output_compute_transform(struct weston_output *output)
2690{
2691 struct weston_matrix transform;
2692 int flip;
2693
2694 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002695 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002696
2697 switch(output->transform) {
2698 case WL_OUTPUT_TRANSFORM_FLIPPED:
2699 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2701 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002702 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002703 flip = -1;
2704 break;
2705 default:
2706 flip = 1;
2707 break;
2708 }
2709
2710 switch(output->transform) {
2711 case WL_OUTPUT_TRANSFORM_NORMAL:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED:
2713 transform.d[0] = flip;
2714 transform.d[1] = 0;
2715 transform.d[4] = 0;
2716 transform.d[5] = 1;
2717 break;
2718 case WL_OUTPUT_TRANSFORM_90:
2719 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2720 transform.d[0] = 0;
2721 transform.d[1] = -flip;
2722 transform.d[4] = 1;
2723 transform.d[5] = 0;
2724 break;
2725 case WL_OUTPUT_TRANSFORM_180:
2726 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2727 transform.d[0] = -flip;
2728 transform.d[1] = 0;
2729 transform.d[4] = 0;
2730 transform.d[5] = -1;
2731 break;
2732 case WL_OUTPUT_TRANSFORM_270:
2733 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2734 transform.d[0] = 0;
2735 transform.d[1] = flip;
2736 transform.d[4] = -1;
2737 transform.d[5] = 0;
2738 break;
2739 default:
2740 break;
2741 }
2742
2743 weston_matrix_multiply(&output->matrix, &transform);
2744}
2745
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002746WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002747weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002748{
Scott Moreau850ca422012-05-21 15:21:25 -06002749 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002750 struct weston_matrix camera;
2751 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002752
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002753 weston_matrix_init(&output->matrix);
2754 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002755 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2756 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002757
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002758 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002759 2.0 / (output->width + output->border.left + output->border.right),
2760 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2761
2762 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002763
Scott Moreauccbf29d2012-02-22 14:21:41 -07002764 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002765 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002766 weston_matrix_init(&camera);
2767 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002768 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002769 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002770 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002771 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002772 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002773 weston_matrix_multiply(&output->matrix, &modelview);
2774 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002775
Scott Moreauccbf29d2012-02-22 14:21:41 -07002776 output->dirty = 0;
2777}
2778
Scott Moreau1bad5db2012-08-18 01:04:05 -06002779static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002780weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002781{
2782 output->transform = transform;
2783
2784 switch (transform) {
2785 case WL_OUTPUT_TRANSFORM_90:
2786 case WL_OUTPUT_TRANSFORM_270:
2787 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2788 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2789 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02002790 output->width = output->current_mode->height;
2791 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002792 break;
2793 case WL_OUTPUT_TRANSFORM_NORMAL:
2794 case WL_OUTPUT_TRANSFORM_180:
2795 case WL_OUTPUT_TRANSFORM_FLIPPED:
2796 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02002797 output->width = output->current_mode->width;
2798 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002799 break;
2800 default:
2801 break;
2802 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002803
Hardening57388e42013-09-18 23:56:36 +02002804 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002805 output->width /= scale;
2806 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002807}
2808
Scott Moreauccbf29d2012-02-22 14:21:41 -07002809WL_EXPORT void
2810weston_output_move(struct weston_output *output, int x, int y)
2811{
2812 output->x = x;
2813 output->y = y;
2814
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002815 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002816 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002817 output->width,
2818 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002819}
2820
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002821WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002822weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002823 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002824 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002825{
2826 output->compositor = c;
2827 output->x = x;
2828 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002829 output->border.top = 0;
2830 output->border.bottom = 0;
2831 output->border.left = 0;
2832 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002833 output->mm_width = mm_width;
2834 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002835 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02002836 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002837
Alexander Larsson0b135062013-05-28 16:23:36 +02002838 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002839 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002840
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002841 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002842 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002843
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002844 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002845 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002846 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002847 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002848
Casey Dahlin58ba1372012-04-19 22:50:08 -04002849 output->id = ffs(~output->compositor->output_id_pool) - 1;
2850 output->compositor->output_id_pool |= 1 << output->id;
2851
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002852 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002853 wl_global_create(c->wl_display, &wl_output_interface, 2,
2854 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002855 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002856}
2857
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002858WL_EXPORT void
2859weston_output_transform_coordinate(struct weston_output *output,
2860 int device_x, int device_y,
2861 wl_fixed_t *x, wl_fixed_t *y)
2862{
2863 wl_fixed_t tx, ty;
2864 wl_fixed_t width, height;
2865
Hardeningff39efa2013-09-18 23:56:35 +02002866 width = wl_fixed_from_int(output->width * output->current_scale - 1);
2867 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002868
2869 switch(output->transform) {
2870 case WL_OUTPUT_TRANSFORM_NORMAL:
2871 default:
2872 tx = wl_fixed_from_int(device_x);
2873 ty = wl_fixed_from_int(device_y);
2874 break;
2875 case WL_OUTPUT_TRANSFORM_90:
2876 tx = wl_fixed_from_int(device_y);
2877 ty = height - wl_fixed_from_int(device_x);
2878 break;
2879 case WL_OUTPUT_TRANSFORM_180:
2880 tx = width - wl_fixed_from_int(device_x);
2881 ty = height - wl_fixed_from_int(device_y);
2882 break;
2883 case WL_OUTPUT_TRANSFORM_270:
2884 tx = width - wl_fixed_from_int(device_y);
2885 ty = wl_fixed_from_int(device_x);
2886 break;
2887 case WL_OUTPUT_TRANSFORM_FLIPPED:
2888 tx = width - wl_fixed_from_int(device_x);
2889 ty = wl_fixed_from_int(device_y);
2890 break;
2891 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2892 tx = width - wl_fixed_from_int(device_y);
2893 ty = height - wl_fixed_from_int(device_x);
2894 break;
2895 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2896 tx = wl_fixed_from_int(device_x);
2897 ty = height - wl_fixed_from_int(device_y);
2898 break;
2899 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2900 tx = wl_fixed_from_int(device_y);
2901 ty = wl_fixed_from_int(device_x);
2902 break;
2903 }
2904
Hardeningff39efa2013-09-18 23:56:35 +02002905 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
2906 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002907}
2908
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002909static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002910compositor_bind(struct wl_client *client,
2911 void *data, uint32_t version, uint32_t id)
2912{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002913 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002914 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002915
Jason Ekstranda85118c2013-06-27 20:17:02 -05002916 resource = wl_resource_create(client, &wl_compositor_interface,
2917 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002918 if (resource == NULL) {
2919 wl_client_post_no_memory(client);
2920 return;
2921 }
2922
2923 wl_resource_set_implementation(resource, &compositor_interface,
2924 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002925}
2926
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002927static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002928log_uname(void)
2929{
2930 struct utsname usys;
2931
2932 uname(&usys);
2933
2934 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2935 usys.version, usys.machine);
2936}
2937
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002938WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002939weston_environment_get_fd(const char *env)
2940{
2941 char *e, *end;
2942 int fd, flags;
2943
2944 e = getenv(env);
2945 if (!e)
2946 return -1;
2947 fd = strtol(e, &end, 0);
2948 if (*end != '\0')
2949 return -1;
2950
2951 flags = fcntl(fd, F_GETFD);
2952 if (flags == -1)
2953 return -1;
2954
2955 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2956 unsetenv(env);
2957
2958 return fd;
2959}
2960
2961WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002962weston_compositor_init(struct weston_compositor *ec,
2963 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002964 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002965 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002966{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002967 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002968 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002969 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002970
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002971 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002972 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002973 wl_signal_init(&ec->destroy_signal);
2974 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002975 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002976 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002977 wl_signal_init(&ec->idle_signal);
2978 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002979 wl_signal_init(&ec->show_input_panel_signal);
2980 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002981 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002982 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002983 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002984 wl_signal_init(&ec->session_signal);
2985 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002986
Casey Dahlin58ba1372012-04-19 22:50:08 -04002987 ec->output_id_pool = 0;
2988
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002989 if (!wl_global_create(display, &wl_compositor_interface, 3,
2990 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05002991 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002992
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002993 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
2994 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03002995 return -1;
2996
Daniel Stone725c2c32012-06-22 14:04:36 +01002997 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002998 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002999 wl_list_init(&ec->layer_list);
3000 wl_list_init(&ec->seat_list);
3001 wl_list_init(&ec->output_list);
3002 wl_list_init(&ec->key_binding_list);
3003 wl_list_init(&ec->button_binding_list);
3004 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003005 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003006
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003007 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003008 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003009
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003010 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3011 weston_config_section_get_string(s, "keymap_rules",
3012 (char **) &xkb_names.rules, NULL);
3013 weston_config_section_get_string(s, "keymap_model",
3014 (char **) &xkb_names.model, NULL);
3015 weston_config_section_get_string(s, "keymap_layout",
3016 (char **) &xkb_names.layout, NULL);
3017 weston_config_section_get_string(s, "keymap_variant",
3018 (char **) &xkb_names.variant, NULL);
3019 weston_config_section_get_string(s, "keymap_options",
3020 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003021
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003022 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3023 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003024
3025 ec->ping_handler = NULL;
3026
3027 screenshooter_create(ec);
3028 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003029 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003030
3031 wl_data_device_manager_init(ec->wl_display);
3032
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003033 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003034
Daniel Stone725c2c32012-06-22 14:04:36 +01003035 loop = wl_display_get_event_loop(ec->wl_display);
3036 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3037 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3038
3039 ec->input_loop = wl_event_loop_create();
3040
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003041 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3042 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3043
3044 weston_compositor_schedule_repaint(ec);
3045
Daniel Stone725c2c32012-06-22 14:04:36 +01003046 return 0;
3047}
3048
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003049WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003050weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003051{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003052 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003053
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003054 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003055 if (ec->input_loop_source)
3056 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003057
Matt Roper361d2ad2011-08-29 13:52:23 -07003058 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003059 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003060 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003061
Daniel Stone325fc2d2012-05-30 16:31:58 +01003062 weston_binding_list_destroy_all(&ec->key_binding_list);
3063 weston_binding_list_destroy_all(&ec->button_binding_list);
3064 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003065 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003066
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003067 weston_plane_release(&ec->primary_plane);
3068
Jonas Ådahlc97af922012-03-28 22:36:09 +02003069 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003070
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003071 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003072}
3073
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003074WL_EXPORT void
3075weston_version(int *major, int *minor, int *micro)
3076{
3077 *major = WESTON_VERSION_MAJOR;
3078 *minor = WESTON_VERSION_MINOR;
3079 *micro = WESTON_VERSION_MICRO;
3080}
3081
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003082static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003083 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003084 const char *desc;
3085} capability_strings[] = {
3086 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003087 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003088};
3089
3090static void
3091weston_compositor_log_capabilities(struct weston_compositor *compositor)
3092{
3093 unsigned i;
3094 int yes;
3095
3096 weston_log("Compositor capabilities:\n");
3097 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3098 yes = compositor->capabilities & capability_strings[i].bit;
3099 weston_log_continue(STAMP_SPACE "%s %s\n",
3100 capability_strings[i].desc,
3101 yes ? "yes" : "no");
3102 }
3103}
3104
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003105static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003106{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003107 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003108
Martin Minarik6d118362012-06-07 18:01:59 +02003109 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003110 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003111
3112 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003113}
3114
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003115#ifdef HAVE_LIBUNWIND
3116
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003117static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003118print_backtrace(void)
3119{
3120 unw_cursor_t cursor;
3121 unw_context_t context;
3122 unw_word_t off;
3123 unw_proc_info_t pip;
3124 int ret, i = 0;
3125 char procname[256];
3126 const char *filename;
3127 Dl_info dlinfo;
3128
3129 pip.unwind_info = NULL;
3130 ret = unw_getcontext(&context);
3131 if (ret) {
3132 weston_log("unw_getcontext: %d\n", ret);
3133 return;
3134 }
3135
3136 ret = unw_init_local(&cursor, &context);
3137 if (ret) {
3138 weston_log("unw_init_local: %d\n", ret);
3139 return;
3140 }
3141
3142 ret = unw_step(&cursor);
3143 while (ret > 0) {
3144 ret = unw_get_proc_info(&cursor, &pip);
3145 if (ret) {
3146 weston_log("unw_get_proc_info: %d\n", ret);
3147 break;
3148 }
3149
3150 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3151 if (ret && ret != -UNW_ENOMEM) {
3152 if (ret != -UNW_EUNSPEC)
3153 weston_log("unw_get_proc_name: %d\n", ret);
3154 procname[0] = '?';
3155 procname[1] = 0;
3156 }
3157
3158 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3159 *dlinfo.dli_fname)
3160 filename = dlinfo.dli_fname;
3161 else
3162 filename = "?";
3163
3164 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3165 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3166
3167 ret = unw_step(&cursor);
3168 if (ret < 0)
3169 weston_log("unw_step: %d\n", ret);
3170 }
3171}
3172
3173#else
3174
3175static void
3176print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003177{
3178 void *buffer[32];
3179 int i, count;
3180 Dl_info info;
3181
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003182 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3183 for (i = 0; i < count; i++) {
3184 dladdr(buffer[i], &info);
3185 weston_log(" [%016lx] %s (%s)\n",
3186 (long) buffer[i],
3187 info.dli_sname ? info.dli_sname : "--",
3188 info.dli_fname);
3189 }
3190}
3191
3192#endif
3193
3194static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003195on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003196{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003197 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003198 * then call the backend restore function, which will switch
3199 * back to the vt we launched from or ungrab X etc and then
3200 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003201 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003202 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003203 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003204
Peter Maatmane5b42e42013-03-27 22:38:53 +01003205 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003206
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003207 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003208
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003209 segv_compositor->restore(segv_compositor);
3210
3211 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003212}
3213
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003214static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003215load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003216{
3217 char path[PATH_MAX];
3218 void *module, *init;
3219
3220 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003221 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003222 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003223 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003224
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003225 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3226 if (module) {
3227 weston_log("Module '%s' already loaded\n", path);
3228 dlclose(module);
3229 return NULL;
3230 }
3231
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003232 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003233 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003234 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003235 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003236 return NULL;
3237 }
3238
3239 init = dlsym(module, entrypoint);
3240 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003241 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003242 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003243 return NULL;
3244 }
3245
3246 return init;
3247}
3248
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003249static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003250load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003251 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003252{
3253 const char *p, *end;
3254 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003255 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003256 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003257
3258 if (modules == NULL)
3259 return 0;
3260
3261 p = modules;
3262 while (*p) {
3263 end = strchrnul(p, ',');
3264 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3265 module_init = load_module(buffer, "module_init");
3266 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003267 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003268 p = end;
3269 while (*p == ',')
3270 p++;
3271
3272 }
3273
3274 return 0;
3275}
3276
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003277static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003278 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3279
3280static const char xdg_wrong_message[] =
3281 "fatal: environment variable XDG_RUNTIME_DIR\n"
3282 "is set to \"%s\", which is not a directory.\n";
3283
3284static const char xdg_wrong_mode_message[] =
3285 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3286 "correctly. Unix access mode must be 0700 but is %o,\n"
3287 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003288 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003289
3290static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003291 "Refer to your distribution on how to get it, or\n"
3292 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3293 "on how to implement it.\n";
3294
Martin Minarik37032f82012-06-18 20:15:18 +02003295static void
3296verify_xdg_runtime_dir(void)
3297{
3298 char *dir = getenv("XDG_RUNTIME_DIR");
3299 struct stat s;
3300
3301 if (!dir) {
3302 weston_log(xdg_error_message);
3303 weston_log_continue(xdg_detail_message);
3304 exit(EXIT_FAILURE);
3305 }
3306
3307 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3308 weston_log(xdg_wrong_message, dir);
3309 weston_log_continue(xdg_detail_message);
3310 exit(EXIT_FAILURE);
3311 }
3312
3313 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3314 weston_log(xdg_wrong_mode_message,
3315 dir, s.st_mode & 0777, s.st_uid);
3316 weston_log_continue(xdg_detail_message);
3317 }
3318}
3319
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003320static int
3321usage(int error_code)
3322{
3323 fprintf(stderr,
3324 "Usage: weston [OPTIONS]\n\n"
3325 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3326 "Weston supports multiple backends, and depending on which backend is in use\n"
3327 "different options will be accepted.\n\n"
3328
3329
3330 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003331 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003332 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003333 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3334 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003335 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003336 " -S, --socket=NAME\tName of socket to listen on\n"
3337 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003338 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003339 " --log==FILE\t\tLog to the given file\n"
3340 " -h, --help\t\tThis help message\n\n");
3341
3342 fprintf(stderr,
3343 "Options for drm-backend.so:\n\n"
3344 " --connector=ID\tBring up only this connector\n"
3345 " --seat=SEAT\t\tThe seat that weston should run on\n"
3346 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003347 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003348 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3349
3350 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003351 "Options for fbdev-backend.so:\n\n"
3352 " --tty=TTY\t\tThe tty to use\n"
3353 " --device=DEVICE\tThe framebuffer device to use\n\n");
3354
3355 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003356 "Options for x11-backend.so:\n\n"
3357 " --width=WIDTH\t\tWidth of X window\n"
3358 " --height=HEIGHT\tHeight of X window\n"
3359 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003360 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003361 " --output-count=COUNT\tCreate multiple outputs\n"
3362 " --no-input\t\tDont create input devices\n\n");
3363
3364 fprintf(stderr,
3365 "Options for wayland-backend.so:\n\n"
3366 " --width=WIDTH\t\tWidth of Wayland surface\n"
3367 " --height=HEIGHT\tHeight of Wayland surface\n"
3368 " --display=DISPLAY\tWayland display to connect to\n\n");
3369
Pekka Paalanene31e0532013-05-22 18:03:07 +03003370#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3371 fprintf(stderr,
3372 "Options for rpi-backend.so:\n\n"
3373 " --tty=TTY\t\tThe tty to use\n"
3374 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3375 " --transform=TR\tThe output transformation, TR is one of:\n"
3376 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3377 "\n");
3378#endif
3379
Hardeningc077a842013-07-08 00:51:35 +02003380#if defined(BUILD_RDP_COMPOSITOR)
3381 fprintf(stderr,
3382 "Options for rdp-backend.so:\n\n"
3383 " --width=WIDTH\t\tWidth of desktop\n"
3384 " --height=HEIGHT\tHeight of desktop\n"
3385 " --extra-modes=MODES\t\n"
3386 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3387 " --address=ADDR\tThe address to bind\n"
3388 " --port=PORT\tThe port to listen on\n"
3389 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3390 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3391 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3392 "\n");
3393#endif
3394
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003395 exit(error_code);
3396}
3397
Peter Maatmane5b42e42013-03-27 22:38:53 +01003398static void
3399catch_signals(void)
3400{
3401 struct sigaction action;
3402
3403 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3404 action.sa_sigaction = on_caught_signal;
3405 sigemptyset(&action.sa_mask);
3406 sigaction(SIGSEGV, &action, NULL);
3407 sigaction(SIGABRT, &action, NULL);
3408}
3409
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003410int main(int argc, char *argv[])
3411{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003412 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003413 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003414 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003415 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003416 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003417 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003418 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003419 int *argc, char *argv[],
3420 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003421 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003422 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003423 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003424 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003425 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003426 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003427 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003428 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003429 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003430 struct weston_config *config;
3431 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003432
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003433 const struct weston_option core_options[] = {
3434 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003435 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003436 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3437 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003438 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003439 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003440 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003441 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003442 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003443
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003444 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003445
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003446 if (help)
3447 usage(EXIT_SUCCESS);
3448
Scott Moreau12245142012-08-29 15:15:58 -06003449 if (version) {
3450 printf(PACKAGE_STRING "\n");
3451 return EXIT_SUCCESS;
3452 }
3453
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003454 weston_log_file_open(log);
3455
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003456 weston_log("%s\n"
3457 STAMP_SPACE "%s\n"
3458 STAMP_SPACE "Bug reports to: %s\n"
3459 STAMP_SPACE "Build: %s\n",
3460 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003461 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003462 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003463
Martin Minarik37032f82012-06-18 20:15:18 +02003464 verify_xdg_runtime_dir();
3465
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003466 display = wl_display_create();
3467
Tiago Vignatti2116b892011-08-08 05:52:59 -07003468 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003469 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3470 display);
3471 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3472 display);
3473 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3474 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003475
3476 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003477 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3478 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003479
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003480 if (!backend) {
3481 if (getenv("WAYLAND_DISPLAY"))
3482 backend = "wayland-backend.so";
3483 else if (getenv("DISPLAY"))
3484 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003485 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003486 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003487 }
3488
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003489 config = weston_config_parse("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003490 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003491 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003492
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003493 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003494 if (!backend_init)
3495 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003496
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003497 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003498 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003499 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003500 exit(EXIT_FAILURE);
3501 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003502
Peter Maatmane5b42e42013-03-27 22:38:53 +01003503 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003504 segv_compositor = ec;
3505
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003506 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003507
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003508 setenv("WAYLAND_DISPLAY", socket_name, 1);
3509
Jason Ekstranda985da42013-08-22 17:28:03 -05003510 if (!shell)
3511 weston_config_section_get_string(section, "shell", &shell,
3512 "desktop-shell.so");
3513 if (load_modules(ec, shell, &argc, argv) < 0)
3514 goto out;
3515
Ossama Othmana50e6e42013-05-14 09:48:26 -07003516 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003517 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003518 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003519 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003520
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003521 for (i = 1; i < argc; i++)
3522 weston_log("fatal: unhandled option: %s\n", argv[i]);
3523 if (argc > 1) {
3524 ret = EXIT_FAILURE;
3525 goto out;
3526 }
3527
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003528 weston_compositor_log_capabilities(ec);
3529
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003530 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003531 weston_log("fatal: failed to add socket: %m\n");
3532 ret = EXIT_FAILURE;
3533 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003534 }
3535
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003536 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003537
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003538 wl_display_run(display);
3539
3540 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003541 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003542 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003543
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003544 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003545
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003546 for (i = ARRAY_LENGTH(signals); i;)
3547 wl_event_source_remove(signals[--i]);
3548
Daniel Stone855028f2012-05-01 20:37:10 +01003549 weston_compositor_xkb_destroy(ec);
3550
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003551 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003552 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003553
Martin Minarik19e6f262012-06-07 13:08:46 +02003554 weston_log_file_close();
3555
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003556 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003557}