blob: e138e9976b3c2da104e309643302506ea3540cb8 [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
Rob Bradford27b17932013-06-26 18:08:46 +01001094 if (weston_surface_is_mapped(surface)) {
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001095 weston_surface_unmap(surface);
Rob Bradford27b17932013-06-26 18:08:46 +01001096 weston_compositor_build_surface_list(compositor);
1097 }
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001098
Pekka Paalanenbc106382012-10-10 12:49:31 +03001099 wl_list_for_each_safe(cb, next,
1100 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001101 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001102
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001103 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001104 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001105 pixman_region32_fini(&surface->pending.damage);
1106
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001107 if (surface->pending.buffer)
1108 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1109
Pekka Paalanende685b82012-12-04 15:58:12 +02001110 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001111
Kristian Høgsberg42263852012-09-06 21:59:29 -04001112 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001113
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001114 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001115 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001116 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001117 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001118 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001119
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001120 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001121 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001122
Pekka Paalanen483243f2013-03-08 14:56:50 +02001123 weston_surface_set_transform_parent(surface, NULL);
1124
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001125 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001126}
1127
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001128static void
1129destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001130{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001131 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001132
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001133 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001134}
1135
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001136static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001137weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1138{
1139 struct weston_buffer *buffer =
1140 container_of(listener, struct weston_buffer, destroy_listener);
1141
1142 wl_signal_emit(&buffer->destroy_signal, buffer);
1143 free(buffer);
1144}
1145
1146struct weston_buffer *
1147weston_buffer_from_resource(struct wl_resource *resource)
1148{
1149 struct weston_buffer *buffer;
1150 struct wl_listener *listener;
1151
1152 listener = wl_resource_get_destroy_listener(resource,
1153 weston_buffer_destroy_handler);
1154
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001155 if (listener)
1156 return container_of(listener, struct weston_buffer,
1157 destroy_listener);
1158
1159 buffer = zalloc(sizeof *buffer);
1160 if (buffer == NULL)
1161 return NULL;
1162
1163 buffer->resource = resource;
1164 wl_signal_init(&buffer->destroy_signal);
1165 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001166 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001167 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001168
1169 return buffer;
1170}
1171
1172static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001173weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1174 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001175{
Pekka Paalanende685b82012-12-04 15:58:12 +02001176 struct weston_buffer_reference *ref =
1177 container_of(listener, struct weston_buffer_reference,
1178 destroy_listener);
1179
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001180 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001181 ref->buffer = NULL;
1182}
1183
1184WL_EXPORT void
1185weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001186 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001187{
1188 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001189 ref->buffer->busy_count--;
1190 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001191 assert(wl_resource_get_client(ref->buffer->resource));
1192 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001193 WL_BUFFER_RELEASE);
1194 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001195 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001196 }
1197
Pekka Paalanende685b82012-12-04 15:58:12 +02001198 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001199 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001200 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001201 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001202 }
1203
Pekka Paalanende685b82012-12-04 15:58:12 +02001204 ref->buffer = buffer;
1205 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1206}
1207
1208static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001209weston_surface_attach(struct weston_surface *surface,
1210 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001211{
1212 weston_buffer_reference(&surface->buffer_ref, buffer);
1213
Pekka Paalanena6421c42012-12-04 15:58:10 +02001214 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001215 if (weston_surface_is_mapped(surface))
1216 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001217 }
1218
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001219 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001220}
1221
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001222WL_EXPORT void
1223weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001224{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225 wl_list_remove(&surface->layer_link);
1226 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001227 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001228 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001229}
1230
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001231WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001232weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001233{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001234 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001235
1236 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001237 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001238}
1239
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001240WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001241weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001242{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001243 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001244
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001245 pixman_region32_union(&compositor->primary_plane.damage,
1246 &compositor->primary_plane.damage,
1247 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001248 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001249}
1250
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001251static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001252surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001253 pixman_region32_t *opaque)
1254{
Pekka Paalanende685b82012-12-04 15:58:12 +02001255 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001256 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001257 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001258
1259 if (surface->transform.enabled) {
1260 pixman_box32_t *extents;
1261
1262 extents = pixman_region32_extents(&surface->damage);
1263 surface_compute_bbox(surface, extents->x1, extents->y1,
1264 extents->x2 - extents->x1,
1265 extents->y2 - extents->y1,
1266 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001267 pixman_region32_translate(&surface->damage,
1268 -surface->plane->x,
1269 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001270 } else {
1271 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001272 surface->geometry.x - surface->plane->x,
1273 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001274 }
1275
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001276 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001277 pixman_region32_union(&surface->plane->damage,
1278 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001279 empty_region(&surface->damage);
1280 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001281 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001282}
1283
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001284static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001285compositor_accumulate_damage(struct weston_compositor *ec)
1286{
1287 struct weston_plane *plane;
1288 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001289 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001290
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001291 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001292
1293 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001294 pixman_region32_copy(&plane->clip, &clip);
1295
1296 pixman_region32_init(&opaque);
1297
1298 wl_list_for_each(es, &ec->surface_list, link) {
1299 if (es->plane != plane)
1300 continue;
1301
1302 surface_accumulate_damage(es, &opaque);
1303 }
1304
1305 pixman_region32_union(&clip, &clip, &opaque);
1306 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001307 }
1308
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001309 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001310
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001311 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001312 /* Both the renderer and the backend have seen the buffer
1313 * by now. If renderer needs the buffer, it has its own
1314 * reference set. If the backend wants to keep the buffer
1315 * around for migrating the surface into a non-primary plane
1316 * later, keep_buffer is true. Otherwise, drop the core
1317 * reference now, and allow early buffer release. This enables
1318 * clients to use single-buffering.
1319 */
1320 if (!es->keep_buffer)
1321 weston_buffer_reference(&es->buffer_ref, NULL);
1322 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001323}
1324
1325static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001326surface_list_add(struct weston_compositor *compositor,
1327 struct weston_surface *surface)
1328{
1329 struct weston_subsurface *sub;
1330
1331 if (wl_list_empty(&surface->subsurface_list)) {
1332 weston_surface_update_transform(surface);
1333 wl_list_insert(compositor->surface_list.prev, &surface->link);
1334 return;
1335 }
1336
1337 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1338 if (!weston_surface_is_mapped(sub->surface))
1339 continue;
1340
1341 if (sub->surface == surface) {
1342 weston_surface_update_transform(sub->surface);
1343 wl_list_insert(compositor->surface_list.prev,
1344 &sub->surface->link);
1345 } else {
1346 surface_list_add(compositor, sub->surface);
1347 }
1348 }
1349}
1350
1351static void
1352weston_compositor_build_surface_list(struct weston_compositor *compositor)
1353{
1354 struct weston_surface *surface;
1355 struct weston_layer *layer;
1356
1357 wl_list_init(&compositor->surface_list);
1358 wl_list_for_each(layer, &compositor->layer_list, link) {
1359 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1360 surface_list_add(compositor, surface);
1361 }
1362 }
1363}
1364
1365static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001366weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001367{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001368 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001369 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001370 struct weston_animation *animation, *next;
1371 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001372 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001373 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001374
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001375 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001376 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001377
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001378 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001379 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001380 else
1381 wl_list_for_each(es, &ec->surface_list, link)
1382 weston_surface_move_to_plane(es, &ec->primary_plane);
1383
Pekka Paalanene67858b2013-04-25 13:57:42 +03001384 wl_list_init(&frame_callback_list);
1385 wl_list_for_each(es, &ec->surface_list, link) {
1386 if (es->output == output) {
1387 wl_list_insert_list(&frame_callback_list,
1388 &es->frame_callback_list);
1389 wl_list_init(&es->frame_callback_list);
1390 }
1391 }
1392
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001393 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001394
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001395 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001396 pixman_region32_intersect(&output_damage,
1397 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001398 pixman_region32_subtract(&output_damage,
1399 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001400
Scott Moreauccbf29d2012-02-22 14:21:41 -07001401 if (output->dirty)
1402 weston_output_update_matrix(output);
1403
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001404 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001405
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001406 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001407
Kristian Høgsbergef044142011-06-21 15:02:12 -04001408 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001409
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001410 weston_compositor_repick(ec);
1411 wl_event_loop_dispatch(ec->input_loop, 0);
1412
Jonas Ådahldb773762012-06-13 00:01:21 +02001413 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001414 wl_callback_send_done(cb->resource, msecs);
1415 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001416 }
1417
Scott Moreaud64cf212012-06-08 19:40:54 -06001418 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001419 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001420 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001421 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001422}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001423
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001424static int
1425weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001426{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001427 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001428
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001429 wl_event_loop_dispatch(compositor->input_loop, 0);
1430
1431 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001432}
1433
1434WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001435weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001436{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001437 struct weston_compositor *compositor = output->compositor;
1438 struct wl_event_loop *loop =
1439 wl_display_get_event_loop(compositor->wl_display);
1440 int fd;
1441
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001442 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001443 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001444 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001445 return;
1446 }
1447
1448 output->repaint_scheduled = 0;
1449 if (compositor->input_loop_source)
1450 return;
1451
1452 fd = wl_event_loop_get_fd(compositor->input_loop);
1453 compositor->input_loop_source =
1454 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1455 weston_compositor_read_input, compositor);
1456}
1457
1458static void
1459idle_repaint(void *data)
1460{
1461 struct weston_output *output = data;
1462
Jonas Ådahle5a12252013-04-05 23:07:11 +02001463 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001464}
1465
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001466WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001467weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1468{
1469 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001470 if (below != NULL)
1471 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001472}
1473
1474WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001475weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001476{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001477 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001478 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001479
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001480 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1481 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001482 return;
1483
Kristian Høgsbergef044142011-06-21 15:02:12 -04001484 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001485 output->repaint_needed = 1;
1486 if (output->repaint_scheduled)
1487 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001488
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001489 wl_event_loop_add_idle(loop, idle_repaint, output);
1490 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001491
1492 if (compositor->input_loop_source) {
1493 wl_event_source_remove(compositor->input_loop_source);
1494 compositor->input_loop_source = NULL;
1495 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001496}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001497
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001498WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001499weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1500{
1501 struct weston_output *output;
1502
1503 wl_list_for_each(output, &compositor->output_list, link)
1504 weston_output_schedule_repaint(output);
1505}
1506
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001507static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001508surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001509{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001510 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001511}
1512
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001513static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001514surface_attach(struct wl_client *client,
1515 struct wl_resource *resource,
1516 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1517{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001518 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001519 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001520
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001521 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001522 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001523 if (buffer == NULL) {
1524 wl_client_post_no_memory(client);
1525 return;
1526 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001527 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001528
Pekka Paalanende685b82012-12-04 15:58:12 +02001529 /* Attach, attach, without commit in between does not send
1530 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001531 if (surface->pending.buffer)
1532 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001533
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001534 surface->pending.sx = sx;
1535 surface->pending.sy = sy;
1536 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001537 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001538 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001539 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001540 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001541 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001542}
1543
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001544static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001545surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001546 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001547 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001548{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001549 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001550
Pekka Paalanen8e159182012-10-10 12:49:25 +03001551 pixman_region32_union_rect(&surface->pending.damage,
1552 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001553 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001554}
1555
Kristian Høgsberg33418202011-08-16 23:01:28 -04001556static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001557destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001558{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001559 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001560
1561 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001562 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001563}
1564
1565static void
1566surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001567 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001568{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001569 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001570 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001571
1572 cb = malloc(sizeof *cb);
1573 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001574 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001575 return;
1576 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001577
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001578 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1579 callback);
1580 if (cb->resource == NULL) {
1581 free(cb);
1582 wl_resource_post_no_memory(resource);
1583 return;
1584 }
1585
Jason Ekstranda85118c2013-06-27 20:17:02 -05001586 wl_resource_set_implementation(cb->resource, NULL, cb,
1587 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001588
Pekka Paalanenbc106382012-10-10 12:49:31 +03001589 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001590}
1591
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001592static void
1593surface_set_opaque_region(struct wl_client *client,
1594 struct wl_resource *resource,
1595 struct wl_resource *region_resource)
1596{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001597 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001598 struct weston_region *region;
1599
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001600 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001601 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001602 pixman_region32_copy(&surface->pending.opaque,
1603 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001604 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001605 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001606 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001607}
1608
1609static void
1610surface_set_input_region(struct wl_client *client,
1611 struct wl_resource *resource,
1612 struct wl_resource *region_resource)
1613{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001614 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001615 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001616
1617 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001618 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001619 pixman_region32_copy(&surface->pending.input,
1620 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001621 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001622 pixman_region32_fini(&surface->pending.input);
1623 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001624 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001625}
1626
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001627static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001628weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001629{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001630 struct weston_subsurface *sub;
1631
1632 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1633 parent_link_pending) {
1634 wl_list_remove(&sub->parent_link);
1635 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1636 }
1637}
1638
1639static void
1640weston_surface_commit(struct weston_surface *surface)
1641{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001642 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001643 int surface_width = 0;
1644 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001645
Alexander Larsson4ea95522013-05-22 14:41:37 +02001646 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001647 surface->buffer_transform = surface->pending.buffer_transform;
1648
Alexander Larsson4ea95522013-05-22 14:41:37 +02001649 /* wl_surface.set_buffer_scale */
1650 surface->buffer_scale = surface->pending.buffer_scale;
1651
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001652 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001653 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001654 weston_surface_attach(surface, surface->pending.buffer);
1655
Giulio Camuffo184df502013-02-21 11:29:21 +01001656 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001657 surface_width = weston_surface_buffer_width(surface);
1658 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001659 }
1660
1661 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001662 surface->configure(surface,
1663 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001664 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001665
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001666 if (surface->pending.buffer)
1667 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1668 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001669 surface->pending.sx = 0;
1670 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001671 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001672
1673 /* wl_surface.damage */
1674 pixman_region32_union(&surface->damage, &surface->damage,
1675 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001676 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1677 0, 0,
1678 surface->geometry.width,
1679 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001680 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001681
1682 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001683 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001684 surface->geometry.width,
1685 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001686 pixman_region32_intersect(&opaque,
1687 &opaque, &surface->pending.opaque);
1688
1689 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1690 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001691 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001692 }
1693
1694 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001695
1696 /* wl_surface.set_input_region */
1697 pixman_region32_fini(&surface->input);
1698 pixman_region32_init_rect(&surface->input, 0, 0,
1699 surface->geometry.width,
1700 surface->geometry.height);
1701 pixman_region32_intersect(&surface->input,
1702 &surface->input, &surface->pending.input);
1703
Pekka Paalanenbc106382012-10-10 12:49:31 +03001704 /* wl_surface.frame */
1705 wl_list_insert_list(&surface->frame_callback_list,
1706 &surface->pending.frame_callback_list);
1707 wl_list_init(&surface->pending.frame_callback_list);
1708
Pekka Paalanene67858b2013-04-25 13:57:42 +03001709 weston_surface_commit_subsurface_order(surface);
1710
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001711 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001712}
1713
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001714static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001715weston_subsurface_commit(struct weston_subsurface *sub);
1716
1717static void
1718weston_subsurface_parent_commit(struct weston_subsurface *sub,
1719 int parent_is_synchronized);
1720
1721static void
1722surface_commit(struct wl_client *client, struct wl_resource *resource)
1723{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001724 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001725 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1726
1727 if (sub) {
1728 weston_subsurface_commit(sub);
1729 return;
1730 }
1731
1732 weston_surface_commit(surface);
1733
1734 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1735 if (sub->surface != surface)
1736 weston_subsurface_parent_commit(sub, 0);
1737 }
1738}
1739
1740static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001741surface_set_buffer_transform(struct wl_client *client,
1742 struct wl_resource *resource, int transform)
1743{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001744 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001745
1746 surface->pending.buffer_transform = transform;
1747}
1748
Alexander Larsson4ea95522013-05-22 14:41:37 +02001749static void
1750surface_set_buffer_scale(struct wl_client *client,
1751 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001752 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001753{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001754 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001755
1756 surface->pending.buffer_scale = scale;
1757}
1758
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001759static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001760 surface_destroy,
1761 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001762 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001763 surface_frame,
1764 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001765 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001766 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001767 surface_set_buffer_transform,
1768 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001769};
1770
1771static void
1772compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001773 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001774{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001775 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001776 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001777
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001778 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001779 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001780 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001781 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001782 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001783
Jason Ekstranda85118c2013-06-27 20:17:02 -05001784 surface->resource =
1785 wl_resource_create(client, &wl_surface_interface,
1786 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001787 if (surface->resource == NULL) {
1788 weston_surface_destroy(surface);
1789 wl_resource_post_no_memory(resource);
1790 return;
1791 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001792 wl_resource_set_implementation(surface->resource, &surface_interface,
1793 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001794}
1795
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001796static void
1797destroy_region(struct wl_resource *resource)
1798{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001799 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001800
1801 pixman_region32_fini(&region->region);
1802 free(region);
1803}
1804
1805static void
1806region_destroy(struct wl_client *client, struct wl_resource *resource)
1807{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001808 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001809}
1810
1811static void
1812region_add(struct wl_client *client, struct wl_resource *resource,
1813 int32_t x, int32_t y, int32_t width, int32_t height)
1814{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001815 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001816
1817 pixman_region32_union_rect(&region->region, &region->region,
1818 x, y, width, height);
1819}
1820
1821static void
1822region_subtract(struct wl_client *client, struct wl_resource *resource,
1823 int32_t x, int32_t y, int32_t width, int32_t height)
1824{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001825 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001826 pixman_region32_t rect;
1827
1828 pixman_region32_init_rect(&rect, x, y, width, height);
1829 pixman_region32_subtract(&region->region, &region->region, &rect);
1830 pixman_region32_fini(&rect);
1831}
1832
1833static const struct wl_region_interface region_interface = {
1834 region_destroy,
1835 region_add,
1836 region_subtract
1837};
1838
1839static void
1840compositor_create_region(struct wl_client *client,
1841 struct wl_resource *resource, uint32_t id)
1842{
1843 struct weston_region *region;
1844
1845 region = malloc(sizeof *region);
1846 if (region == NULL) {
1847 wl_resource_post_no_memory(resource);
1848 return;
1849 }
1850
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001851 pixman_region32_init(&region->region);
1852
Jason Ekstranda85118c2013-06-27 20:17:02 -05001853 region->resource =
1854 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001855 if (region->resource == NULL) {
1856 free(region);
1857 wl_resource_post_no_memory(resource);
1858 return;
1859 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001860 wl_resource_set_implementation(region->resource, &region_interface,
1861 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001862}
1863
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001864static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001865 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001866 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001867};
1868
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001869static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001870weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1871{
1872 struct weston_surface *surface = sub->surface;
1873 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001874 int surface_width = 0;
1875 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001876
Alexander Larsson4ea95522013-05-22 14:41:37 +02001877 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001878 surface->buffer_transform = sub->cached.buffer_transform;
1879
Alexander Larsson4ea95522013-05-22 14:41:37 +02001880 /* wl_surface.set_buffer_scale */
1881 surface->buffer_scale = sub->cached.buffer_scale;
1882
Pekka Paalanene67858b2013-04-25 13:57:42 +03001883 /* wl_surface.attach */
1884 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1885 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1886 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1887
1888 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001889 surface_width = weston_surface_buffer_width(surface);
1890 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001891 }
1892
1893 if (surface->configure && sub->cached.newly_attached)
1894 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001895 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001896 sub->cached.sx = 0;
1897 sub->cached.sy = 0;
1898 sub->cached.newly_attached = 0;
1899
1900 /* wl_surface.damage */
1901 pixman_region32_union(&surface->damage, &surface->damage,
1902 &sub->cached.damage);
1903 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1904 0, 0,
1905 surface->geometry.width,
1906 surface->geometry.height);
1907 empty_region(&sub->cached.damage);
1908
1909 /* wl_surface.set_opaque_region */
1910 pixman_region32_init_rect(&opaque, 0, 0,
1911 surface->geometry.width,
1912 surface->geometry.height);
1913 pixman_region32_intersect(&opaque,
1914 &opaque, &sub->cached.opaque);
1915
1916 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1917 pixman_region32_copy(&surface->opaque, &opaque);
1918 weston_surface_geometry_dirty(surface);
1919 }
1920
1921 pixman_region32_fini(&opaque);
1922
1923 /* wl_surface.set_input_region */
1924 pixman_region32_fini(&surface->input);
1925 pixman_region32_init_rect(&surface->input, 0, 0,
1926 surface->geometry.width,
1927 surface->geometry.height);
1928 pixman_region32_intersect(&surface->input,
1929 &surface->input, &sub->cached.input);
1930
1931 /* wl_surface.frame */
1932 wl_list_insert_list(&surface->frame_callback_list,
1933 &sub->cached.frame_callback_list);
1934 wl_list_init(&sub->cached.frame_callback_list);
1935
1936 weston_surface_commit_subsurface_order(surface);
1937
1938 weston_surface_schedule_repaint(surface);
1939
1940 sub->cached.has_data = 0;
1941}
1942
1943static void
1944weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1945{
1946 struct weston_surface *surface = sub->surface;
1947
1948 /*
1949 * If this commit would cause the surface to move by the
1950 * attach(dx, dy) parameters, the old damage region must be
1951 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02001952 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03001953 */
1954 pixman_region32_translate(&sub->cached.damage,
1955 -surface->pending.sx, -surface->pending.sy);
1956 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1957 &surface->pending.damage);
1958 empty_region(&surface->pending.damage);
1959
1960 if (surface->pending.newly_attached) {
1961 sub->cached.newly_attached = 1;
1962 weston_buffer_reference(&sub->cached.buffer_ref,
1963 surface->pending.buffer);
1964 }
1965 sub->cached.sx += surface->pending.sx;
1966 sub->cached.sy += surface->pending.sy;
1967 surface->pending.sx = 0;
1968 surface->pending.sy = 0;
1969 surface->pending.newly_attached = 0;
1970
1971 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001972 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001973
1974 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1975
1976 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1977
1978 wl_list_insert_list(&sub->cached.frame_callback_list,
1979 &surface->pending.frame_callback_list);
1980 wl_list_init(&surface->pending.frame_callback_list);
1981
1982 sub->cached.has_data = 1;
1983}
1984
1985static int
1986weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1987{
1988 while (sub) {
1989 if (sub->synchronized)
1990 return 1;
1991
1992 if (!sub->parent)
1993 return 0;
1994
1995 sub = weston_surface_to_subsurface(sub->parent);
1996 }
1997
1998 return 0;
1999}
2000
2001static void
2002weston_subsurface_commit(struct weston_subsurface *sub)
2003{
2004 struct weston_surface *surface = sub->surface;
2005 struct weston_subsurface *tmp;
2006
2007 /* Recursive check for effectively synchronized. */
2008 if (weston_subsurface_is_synchronized(sub)) {
2009 weston_subsurface_commit_to_cache(sub);
2010 } else {
2011 if (sub->cached.has_data) {
2012 /* flush accumulated state from cache */
2013 weston_subsurface_commit_to_cache(sub);
2014 weston_subsurface_commit_from_cache(sub);
2015 } else {
2016 weston_surface_commit(surface);
2017 }
2018
2019 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2020 if (tmp->surface != surface)
2021 weston_subsurface_parent_commit(tmp, 0);
2022 }
2023 }
2024}
2025
2026static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002027weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002028{
2029 struct weston_surface *surface = sub->surface;
2030 struct weston_subsurface *tmp;
2031
Pekka Paalanene67858b2013-04-25 13:57:42 +03002032 /* From now on, commit_from_cache the whole sub-tree, regardless of
2033 * the synchronized mode of each child. This sub-surface or some
2034 * of its ancestors were synchronized, so we are synchronized
2035 * all the way down.
2036 */
2037
2038 if (sub->cached.has_data)
2039 weston_subsurface_commit_from_cache(sub);
2040
2041 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2042 if (tmp->surface != surface)
2043 weston_subsurface_parent_commit(tmp, 1);
2044 }
2045}
2046
2047static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002048weston_subsurface_parent_commit(struct weston_subsurface *sub,
2049 int parent_is_synchronized)
2050{
2051 if (sub->position.set) {
2052 weston_surface_set_position(sub->surface,
2053 sub->position.x, sub->position.y);
2054 sub->position.set = 0;
2055 }
2056
2057 if (parent_is_synchronized || sub->synchronized)
2058 weston_subsurface_synchronized_commit(sub);
2059}
2060
2061static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002062subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2063 int32_t width, int32_t height)
2064{
2065 struct weston_compositor *compositor = surface->compositor;
2066
2067 weston_surface_configure(surface,
2068 surface->geometry.x + dx,
2069 surface->geometry.y + dy,
2070 width, height);
2071
2072 /* No need to check parent mappedness, because if parent is not
2073 * mapped, parent is not in a visible layer, so this sub-surface
2074 * will not be drawn either.
2075 */
2076 if (!weston_surface_is_mapped(surface)) {
2077 wl_list_init(&surface->layer_link);
2078
2079 /* Cannot call weston_surface_update_transform(),
2080 * because that would call it also for the parent surface,
2081 * which might not be mapped yet. That would lead to
2082 * inconsistent state, where the window could never be
2083 * mapped.
2084 *
2085 * Instead just assing any output, to make
2086 * weston_surface_is_mapped() return true, so that when the
2087 * parent surface does get mapped, this one will get
2088 * included, too. See surface_list_add().
2089 */
2090 assert(!wl_list_empty(&compositor->output_list));
2091 surface->output = container_of(compositor->output_list.next,
2092 struct weston_output, link);
2093 }
2094}
2095
2096static struct weston_subsurface *
2097weston_surface_to_subsurface(struct weston_surface *surface)
2098{
2099 if (surface->configure == subsurface_configure)
2100 return surface->configure_private;
2101
2102 return NULL;
2103}
2104
Pekka Paalanen01388e22013-04-25 13:57:44 +03002105WL_EXPORT struct weston_surface *
2106weston_surface_get_main_surface(struct weston_surface *surface)
2107{
2108 struct weston_subsurface *sub;
2109
2110 while (surface && (sub = weston_surface_to_subsurface(surface)))
2111 surface = sub->parent;
2112
2113 return surface;
2114}
2115
Pekka Paalanene67858b2013-04-25 13:57:42 +03002116static void
2117subsurface_set_position(struct wl_client *client,
2118 struct wl_resource *resource, int32_t x, int32_t y)
2119{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002120 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121
2122 if (!sub)
2123 return;
2124
2125 sub->position.x = x;
2126 sub->position.y = y;
2127 sub->position.set = 1;
2128}
2129
2130static struct weston_subsurface *
2131subsurface_from_surface(struct weston_surface *surface)
2132{
2133 struct weston_subsurface *sub;
2134
2135 sub = weston_surface_to_subsurface(surface);
2136 if (sub)
2137 return sub;
2138
2139 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2140 if (sub->surface == surface)
2141 return sub;
2142
2143 return NULL;
2144}
2145
2146static struct weston_subsurface *
2147subsurface_sibling_check(struct weston_subsurface *sub,
2148 struct weston_surface *surface,
2149 const char *request)
2150{
2151 struct weston_subsurface *sibling;
2152
2153 sibling = subsurface_from_surface(surface);
2154
2155 if (!sibling) {
2156 wl_resource_post_error(sub->resource,
2157 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2158 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002159 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002160 return NULL;
2161 }
2162
2163 if (sibling->parent != sub->parent) {
2164 wl_resource_post_error(sub->resource,
2165 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2166 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002167 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002168 return NULL;
2169 }
2170
2171 return sibling;
2172}
2173
2174static void
2175subsurface_place_above(struct wl_client *client,
2176 struct wl_resource *resource,
2177 struct wl_resource *sibling_resource)
2178{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002179 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002180 struct weston_surface *surface =
2181 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002182 struct weston_subsurface *sibling;
2183
2184 if (!sub)
2185 return;
2186
2187 sibling = subsurface_sibling_check(sub, surface, "place_above");
2188 if (!sibling)
2189 return;
2190
2191 wl_list_remove(&sub->parent_link_pending);
2192 wl_list_insert(sibling->parent_link_pending.prev,
2193 &sub->parent_link_pending);
2194}
2195
2196static void
2197subsurface_place_below(struct wl_client *client,
2198 struct wl_resource *resource,
2199 struct wl_resource *sibling_resource)
2200{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002201 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002202 struct weston_surface *surface =
2203 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002204 struct weston_subsurface *sibling;
2205
2206 if (!sub)
2207 return;
2208
2209 sibling = subsurface_sibling_check(sub, surface, "place_below");
2210 if (!sibling)
2211 return;
2212
2213 wl_list_remove(&sub->parent_link_pending);
2214 wl_list_insert(&sibling->parent_link_pending,
2215 &sub->parent_link_pending);
2216}
2217
2218static void
2219subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2220{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002221 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222
2223 if (sub)
2224 sub->synchronized = 1;
2225}
2226
2227static void
2228subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2229{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002230 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002231
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002232 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002233 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002234
2235 /* If sub became effectively desynchronized, flush. */
2236 if (!weston_subsurface_is_synchronized(sub))
2237 weston_subsurface_synchronized_commit(sub);
2238 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002239}
2240
2241static void
2242weston_subsurface_cache_init(struct weston_subsurface *sub)
2243{
2244 pixman_region32_init(&sub->cached.damage);
2245 pixman_region32_init(&sub->cached.opaque);
2246 pixman_region32_init(&sub->cached.input);
2247 wl_list_init(&sub->cached.frame_callback_list);
2248 sub->cached.buffer_ref.buffer = NULL;
2249}
2250
2251static void
2252weston_subsurface_cache_fini(struct weston_subsurface *sub)
2253{
2254 struct weston_frame_callback *cb, *tmp;
2255
2256 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002257 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002258
2259 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2260 pixman_region32_fini(&sub->cached.damage);
2261 pixman_region32_fini(&sub->cached.opaque);
2262 pixman_region32_fini(&sub->cached.input);
2263}
2264
2265static void
2266weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2267{
2268 wl_list_remove(&sub->parent_link);
2269 wl_list_remove(&sub->parent_link_pending);
2270 wl_list_remove(&sub->parent_destroy_listener.link);
2271 sub->parent = NULL;
2272}
2273
2274static void
2275weston_subsurface_destroy(struct weston_subsurface *sub);
2276
2277static void
2278subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2279{
2280 struct weston_subsurface *sub =
2281 container_of(listener, struct weston_subsurface,
2282 surface_destroy_listener);
2283 assert(data == &sub->surface->resource);
2284
2285 /* The protocol object (wl_resource) is left inert. */
2286 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002287 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002288
2289 weston_subsurface_destroy(sub);
2290}
2291
2292static void
2293subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2294{
2295 struct weston_subsurface *sub =
2296 container_of(listener, struct weston_subsurface,
2297 parent_destroy_listener);
2298 assert(data == &sub->parent->resource);
2299 assert(sub->surface != sub->parent);
2300
2301 if (weston_surface_is_mapped(sub->surface))
2302 weston_surface_unmap(sub->surface);
2303
2304 weston_subsurface_unlink_parent(sub);
2305}
2306
2307static void
2308subsurface_resource_destroy(struct wl_resource *resource)
2309{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002310 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002311
2312 if (sub)
2313 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002314}
2315
2316static void
2317subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2318{
2319 wl_resource_destroy(resource);
2320}
2321
2322static void
2323weston_subsurface_link_parent(struct weston_subsurface *sub,
2324 struct weston_surface *parent)
2325{
2326 sub->parent = parent;
2327 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002328 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002329 &sub->parent_destroy_listener);
2330
2331 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2332 wl_list_insert(&parent->subsurface_list_pending,
2333 &sub->parent_link_pending);
2334}
2335
2336static void
2337weston_subsurface_link_surface(struct weston_subsurface *sub,
2338 struct weston_surface *surface)
2339{
2340 sub->surface = surface;
2341 sub->surface_destroy_listener.notify =
2342 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002343 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002344 &sub->surface_destroy_listener);
2345}
2346
2347static void
2348weston_subsurface_destroy(struct weston_subsurface *sub)
2349{
2350 assert(sub->surface);
2351
2352 if (sub->resource) {
2353 assert(weston_surface_to_subsurface(sub->surface) == sub);
2354 assert(sub->parent_destroy_listener.notify ==
2355 subsurface_handle_parent_destroy);
2356
2357 weston_surface_set_transform_parent(sub->surface, NULL);
2358 if (sub->parent)
2359 weston_subsurface_unlink_parent(sub);
2360
2361 weston_subsurface_cache_fini(sub);
2362
2363 sub->surface->configure = NULL;
2364 sub->surface->configure_private = NULL;
2365 } else {
2366 /* the dummy weston_subsurface for the parent itself */
2367 assert(sub->parent_destroy_listener.notify == NULL);
2368 wl_list_remove(&sub->parent_link);
2369 wl_list_remove(&sub->parent_link_pending);
2370 }
2371
2372 wl_list_remove(&sub->surface_destroy_listener.link);
2373 free(sub);
2374}
2375
2376static const struct wl_subsurface_interface subsurface_implementation = {
2377 subsurface_destroy,
2378 subsurface_set_position,
2379 subsurface_place_above,
2380 subsurface_place_below,
2381 subsurface_set_sync,
2382 subsurface_set_desync
2383};
2384
2385static struct weston_subsurface *
2386weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2387 struct weston_surface *parent)
2388{
2389 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002390 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002391
2392 sub = calloc(1, sizeof *sub);
2393 if (!sub)
2394 return NULL;
2395
Jason Ekstranda85118c2013-06-27 20:17:02 -05002396 sub->resource =
2397 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002398 if (!sub->resource) {
2399 free(sub);
2400 return NULL;
2401 }
2402
Jason Ekstranda85118c2013-06-27 20:17:02 -05002403 wl_resource_set_implementation(sub->resource,
2404 &subsurface_implementation,
2405 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002406 weston_subsurface_link_surface(sub, surface);
2407 weston_subsurface_link_parent(sub, parent);
2408 weston_subsurface_cache_init(sub);
2409 sub->synchronized = 1;
2410 weston_surface_set_transform_parent(surface, parent);
2411
2412 return sub;
2413}
2414
2415/* Create a dummy subsurface for having the parent itself in its
2416 * sub-surface lists. Makes stacking order manipulation easy.
2417 */
2418static struct weston_subsurface *
2419weston_subsurface_create_for_parent(struct weston_surface *parent)
2420{
2421 struct weston_subsurface *sub;
2422
2423 sub = calloc(1, sizeof *sub);
2424 if (!sub)
2425 return NULL;
2426
2427 weston_subsurface_link_surface(sub, parent);
2428 sub->parent = parent;
2429 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2430 wl_list_insert(&parent->subsurface_list_pending,
2431 &sub->parent_link_pending);
2432
2433 return sub;
2434}
2435
2436static void
2437subcompositor_get_subsurface(struct wl_client *client,
2438 struct wl_resource *resource,
2439 uint32_t id,
2440 struct wl_resource *surface_resource,
2441 struct wl_resource *parent_resource)
2442{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002443 struct weston_surface *surface =
2444 wl_resource_get_user_data(surface_resource);
2445 struct weston_surface *parent =
2446 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002447 struct weston_subsurface *sub;
2448 static const char where[] = "get_subsurface: wl_subsurface@";
2449
2450 if (surface == parent) {
2451 wl_resource_post_error(resource,
2452 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2453 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002454 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002455 return;
2456 }
2457
2458 if (weston_surface_to_subsurface(surface)) {
2459 wl_resource_post_error(resource,
2460 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2461 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002462 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002463 return;
2464 }
2465
2466 if (surface->configure) {
2467 wl_resource_post_error(resource,
2468 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2469 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002470 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002471 return;
2472 }
2473
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002474 if (weston_surface_get_main_surface(parent) == surface) {
2475 wl_resource_post_error(resource,
2476 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2477 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002478 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002479 return;
2480 }
2481
Pekka Paalanene67858b2013-04-25 13:57:42 +03002482 /* make sure the parent is in its own list */
2483 if (wl_list_empty(&parent->subsurface_list)) {
2484 if (!weston_subsurface_create_for_parent(parent)) {
2485 wl_resource_post_no_memory(resource);
2486 return;
2487 }
2488 }
2489
2490 sub = weston_subsurface_create(id, surface, parent);
2491 if (!sub) {
2492 wl_resource_post_no_memory(resource);
2493 return;
2494 }
2495
2496 surface->configure = subsurface_configure;
2497 surface->configure_private = sub;
2498}
2499
2500static void
2501subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2502{
2503 wl_resource_destroy(resource);
2504}
2505
2506static const struct wl_subcompositor_interface subcompositor_interface = {
2507 subcompositor_destroy,
2508 subcompositor_get_subsurface
2509};
2510
2511static void
2512bind_subcompositor(struct wl_client *client,
2513 void *data, uint32_t version, uint32_t id)
2514{
2515 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002516 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002517
Jason Ekstranda85118c2013-06-27 20:17:02 -05002518 resource =
2519 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002520 if (resource == NULL) {
2521 wl_client_post_no_memory(client);
2522 return;
2523 }
2524 wl_resource_set_implementation(resource, &subcompositor_interface,
2525 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002526}
2527
2528static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002529weston_compositor_dpms(struct weston_compositor *compositor,
2530 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002531{
2532 struct weston_output *output;
2533
2534 wl_list_for_each(output, &compositor->output_list, link)
2535 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002536 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002537}
2538
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002539WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002540weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002541{
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002542 switch (compositor->state) {
2543 case WESTON_COMPOSITOR_SLEEPING:
2544 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2545 /* fall through */
2546 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002547 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002548 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002549 /* fall through */
2550 default:
2551 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2552 wl_event_source_timer_update(compositor->idle_source,
2553 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002554 }
2555}
2556
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002557WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002558weston_compositor_offscreen(struct weston_compositor *compositor)
2559{
2560 switch (compositor->state) {
2561 case WESTON_COMPOSITOR_OFFSCREEN:
2562 return;
2563 case WESTON_COMPOSITOR_SLEEPING:
2564 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2565 /* fall through */
2566 default:
2567 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2568 wl_event_source_timer_update(compositor->idle_source, 0);
2569 }
2570}
2571
2572WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002573weston_compositor_sleep(struct weston_compositor *compositor)
2574{
2575 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2576 return;
2577
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002578 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002579 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2580 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2581}
2582
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002583static int
2584idle_handler(void *data)
2585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002586 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002587
2588 if (compositor->idle_inhibit)
2589 return 1;
2590
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002591 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002592 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002593
2594 return 1;
2595}
2596
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002597WL_EXPORT void
2598weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2599{
2600 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002601 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002602 plane->x = x;
2603 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002604
2605 /* Init the link so that the call to wl_list_remove() when releasing
2606 * the plane without ever stacking doesn't lead to a crash */
2607 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002608}
2609
2610WL_EXPORT void
2611weston_plane_release(struct weston_plane *plane)
2612{
2613 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002614 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002615
2616 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002617}
2618
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002619WL_EXPORT void
2620weston_compositor_stack_plane(struct weston_compositor *ec,
2621 struct weston_plane *plane,
2622 struct weston_plane *above)
2623{
2624 if (above)
2625 wl_list_insert(above->link.prev, &plane->link);
2626 else
2627 wl_list_insert(&ec->plane_list, &plane->link);
2628}
2629
Casey Dahlin9074db52012-04-19 22:50:09 -04002630static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002631{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002632 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002633}
2634
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002635static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002636bind_output(struct wl_client *client,
2637 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002638{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002639 struct weston_output *output = data;
2640 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002641 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002642
Jason Ekstranda85118c2013-06-27 20:17:02 -05002643 resource = wl_resource_create(client, &wl_output_interface,
2644 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002645 if (resource == NULL) {
2646 wl_client_post_no_memory(client);
2647 return;
2648 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002649
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002650 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002651 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002652
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002653 wl_output_send_geometry(resource,
2654 output->x,
2655 output->y,
2656 output->mm_width,
2657 output->mm_height,
2658 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002659 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002660 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002661 if (version >= 2)
2662 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002663 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002664
2665 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002666 wl_output_send_mode(resource,
2667 mode->flags,
2668 mode->width,
2669 mode->height,
2670 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002671 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002672
2673 if (version >= 2)
2674 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002675}
2676
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002677WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002678weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002679{
Richard Hughes64ddde12013-05-01 21:52:10 +01002680 wl_signal_emit(&output->destroy_signal, output);
2681
Richard Hughesafe690c2013-05-02 10:10:04 +01002682 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002683 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002684 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002685 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002686
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002687 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002688}
2689
Scott Moreau1bad5db2012-08-18 01:04:05 -06002690static void
2691weston_output_compute_transform(struct weston_output *output)
2692{
2693 struct weston_matrix transform;
2694 int flip;
2695
2696 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002697 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002698
2699 switch(output->transform) {
2700 case WL_OUTPUT_TRANSFORM_FLIPPED:
2701 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2702 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2703 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002704 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002705 flip = -1;
2706 break;
2707 default:
2708 flip = 1;
2709 break;
2710 }
2711
2712 switch(output->transform) {
2713 case WL_OUTPUT_TRANSFORM_NORMAL:
2714 case WL_OUTPUT_TRANSFORM_FLIPPED:
2715 transform.d[0] = flip;
2716 transform.d[1] = 0;
2717 transform.d[4] = 0;
2718 transform.d[5] = 1;
2719 break;
2720 case WL_OUTPUT_TRANSFORM_90:
2721 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2722 transform.d[0] = 0;
2723 transform.d[1] = -flip;
2724 transform.d[4] = 1;
2725 transform.d[5] = 0;
2726 break;
2727 case WL_OUTPUT_TRANSFORM_180:
2728 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2729 transform.d[0] = -flip;
2730 transform.d[1] = 0;
2731 transform.d[4] = 0;
2732 transform.d[5] = -1;
2733 break;
2734 case WL_OUTPUT_TRANSFORM_270:
2735 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2736 transform.d[0] = 0;
2737 transform.d[1] = flip;
2738 transform.d[4] = -1;
2739 transform.d[5] = 0;
2740 break;
2741 default:
2742 break;
2743 }
2744
2745 weston_matrix_multiply(&output->matrix, &transform);
2746}
2747
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002748WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002749weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002750{
Scott Moreau850ca422012-05-21 15:21:25 -06002751 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002752 struct weston_matrix camera;
2753 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002754
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002755 weston_matrix_init(&output->matrix);
2756 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002757 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2758 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002759
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002760 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002761 2.0 / (output->width + output->border.left + output->border.right),
2762 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2763
2764 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002765
Scott Moreauccbf29d2012-02-22 14:21:41 -07002766 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002767 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002768 weston_matrix_init(&camera);
2769 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002770 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002771 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002772 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002773 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002774 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002775 weston_matrix_multiply(&output->matrix, &modelview);
2776 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002777
Scott Moreauccbf29d2012-02-22 14:21:41 -07002778 output->dirty = 0;
2779}
2780
Scott Moreau1bad5db2012-08-18 01:04:05 -06002781static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002782weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002783{
2784 output->transform = transform;
2785
2786 switch (transform) {
2787 case WL_OUTPUT_TRANSFORM_90:
2788 case WL_OUTPUT_TRANSFORM_270:
2789 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2790 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2791 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02002792 output->width = output->current_mode->height;
2793 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002794 break;
2795 case WL_OUTPUT_TRANSFORM_NORMAL:
2796 case WL_OUTPUT_TRANSFORM_180:
2797 case WL_OUTPUT_TRANSFORM_FLIPPED:
2798 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02002799 output->width = output->current_mode->width;
2800 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002801 break;
2802 default:
2803 break;
2804 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002805
Hardening57388e42013-09-18 23:56:36 +02002806 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002807 output->width /= scale;
2808 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002809}
2810
Scott Moreauccbf29d2012-02-22 14:21:41 -07002811WL_EXPORT void
2812weston_output_move(struct weston_output *output, int x, int y)
2813{
2814 output->x = x;
2815 output->y = y;
2816
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002817 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002818 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002819 output->width,
2820 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002821}
2822
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002823WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002824weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002825 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002826 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002827{
2828 output->compositor = c;
2829 output->x = x;
2830 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002831 output->border.top = 0;
2832 output->border.bottom = 0;
2833 output->border.left = 0;
2834 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002835 output->mm_width = mm_width;
2836 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002837 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02002838 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002839
Alexander Larsson0b135062013-05-28 16:23:36 +02002840 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002841 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002842
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002843 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002844 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002845
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002846 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002847 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002848 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002849 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002850
Casey Dahlin58ba1372012-04-19 22:50:08 -04002851 output->id = ffs(~output->compositor->output_id_pool) - 1;
2852 output->compositor->output_id_pool |= 1 << output->id;
2853
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002854 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002855 wl_global_create(c->wl_display, &wl_output_interface, 2,
2856 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002857 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002858}
2859
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002860WL_EXPORT void
2861weston_output_transform_coordinate(struct weston_output *output,
2862 int device_x, int device_y,
2863 wl_fixed_t *x, wl_fixed_t *y)
2864{
2865 wl_fixed_t tx, ty;
2866 wl_fixed_t width, height;
2867
Hardeningff39efa2013-09-18 23:56:35 +02002868 width = wl_fixed_from_int(output->width * output->current_scale - 1);
2869 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002870
2871 switch(output->transform) {
2872 case WL_OUTPUT_TRANSFORM_NORMAL:
2873 default:
2874 tx = wl_fixed_from_int(device_x);
2875 ty = wl_fixed_from_int(device_y);
2876 break;
2877 case WL_OUTPUT_TRANSFORM_90:
2878 tx = wl_fixed_from_int(device_y);
2879 ty = height - wl_fixed_from_int(device_x);
2880 break;
2881 case WL_OUTPUT_TRANSFORM_180:
2882 tx = width - wl_fixed_from_int(device_x);
2883 ty = height - wl_fixed_from_int(device_y);
2884 break;
2885 case WL_OUTPUT_TRANSFORM_270:
2886 tx = width - wl_fixed_from_int(device_y);
2887 ty = wl_fixed_from_int(device_x);
2888 break;
2889 case WL_OUTPUT_TRANSFORM_FLIPPED:
2890 tx = width - wl_fixed_from_int(device_x);
2891 ty = wl_fixed_from_int(device_y);
2892 break;
2893 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2894 tx = width - wl_fixed_from_int(device_y);
2895 ty = height - wl_fixed_from_int(device_x);
2896 break;
2897 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2898 tx = wl_fixed_from_int(device_x);
2899 ty = height - wl_fixed_from_int(device_y);
2900 break;
2901 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2902 tx = wl_fixed_from_int(device_y);
2903 ty = wl_fixed_from_int(device_x);
2904 break;
2905 }
2906
Hardeningff39efa2013-09-18 23:56:35 +02002907 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
2908 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002909}
2910
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002911static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002912compositor_bind(struct wl_client *client,
2913 void *data, uint32_t version, uint32_t id)
2914{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002915 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002916 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002917
Jason Ekstranda85118c2013-06-27 20:17:02 -05002918 resource = wl_resource_create(client, &wl_compositor_interface,
2919 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002920 if (resource == NULL) {
2921 wl_client_post_no_memory(client);
2922 return;
2923 }
2924
2925 wl_resource_set_implementation(resource, &compositor_interface,
2926 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002927}
2928
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002929static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002930log_uname(void)
2931{
2932 struct utsname usys;
2933
2934 uname(&usys);
2935
2936 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2937 usys.version, usys.machine);
2938}
2939
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002940WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002941weston_environment_get_fd(const char *env)
2942{
2943 char *e, *end;
2944 int fd, flags;
2945
2946 e = getenv(env);
2947 if (!e)
2948 return -1;
2949 fd = strtol(e, &end, 0);
2950 if (*end != '\0')
2951 return -1;
2952
2953 flags = fcntl(fd, F_GETFD);
2954 if (flags == -1)
2955 return -1;
2956
2957 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2958 unsetenv(env);
2959
2960 return fd;
2961}
2962
2963WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002964weston_compositor_init(struct weston_compositor *ec,
2965 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002966 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002967 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002968{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002969 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002970 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002971 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002972
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002973 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002974 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002975 wl_signal_init(&ec->destroy_signal);
2976 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002977 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002978 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002979 wl_signal_init(&ec->idle_signal);
2980 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002981 wl_signal_init(&ec->show_input_panel_signal);
2982 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002983 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002984 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002985 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002986 wl_signal_init(&ec->session_signal);
2987 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002988
Casey Dahlin58ba1372012-04-19 22:50:08 -04002989 ec->output_id_pool = 0;
2990
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002991 if (!wl_global_create(display, &wl_compositor_interface, 3,
2992 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05002993 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002994
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002995 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
2996 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03002997 return -1;
2998
Daniel Stone725c2c32012-06-22 14:04:36 +01002999 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003000 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003001 wl_list_init(&ec->layer_list);
3002 wl_list_init(&ec->seat_list);
3003 wl_list_init(&ec->output_list);
3004 wl_list_init(&ec->key_binding_list);
3005 wl_list_init(&ec->button_binding_list);
3006 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003007 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003008
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003009 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003010 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003011
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003012 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3013 weston_config_section_get_string(s, "keymap_rules",
3014 (char **) &xkb_names.rules, NULL);
3015 weston_config_section_get_string(s, "keymap_model",
3016 (char **) &xkb_names.model, NULL);
3017 weston_config_section_get_string(s, "keymap_layout",
3018 (char **) &xkb_names.layout, NULL);
3019 weston_config_section_get_string(s, "keymap_variant",
3020 (char **) &xkb_names.variant, NULL);
3021 weston_config_section_get_string(s, "keymap_options",
3022 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003023
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003024 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3025 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003026
3027 ec->ping_handler = NULL;
3028
3029 screenshooter_create(ec);
3030 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003031 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003032
3033 wl_data_device_manager_init(ec->wl_display);
3034
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003035 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003036
Daniel Stone725c2c32012-06-22 14:04:36 +01003037 loop = wl_display_get_event_loop(ec->wl_display);
3038 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3039 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3040
3041 ec->input_loop = wl_event_loop_create();
3042
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003043 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3044 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3045
3046 weston_compositor_schedule_repaint(ec);
3047
Daniel Stone725c2c32012-06-22 14:04:36 +01003048 return 0;
3049}
3050
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003051WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003052weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003053{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003054 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003055
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003056 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003057 if (ec->input_loop_source)
3058 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003059
Matt Roper361d2ad2011-08-29 13:52:23 -07003060 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003061 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003062 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003063
Daniel Stone325fc2d2012-05-30 16:31:58 +01003064 weston_binding_list_destroy_all(&ec->key_binding_list);
3065 weston_binding_list_destroy_all(&ec->button_binding_list);
3066 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003067 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003068
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003069 weston_plane_release(&ec->primary_plane);
3070
Jonas Ådahlc97af922012-03-28 22:36:09 +02003071 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003072
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003073 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003074}
3075
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003076WL_EXPORT void
3077weston_version(int *major, int *minor, int *micro)
3078{
3079 *major = WESTON_VERSION_MAJOR;
3080 *minor = WESTON_VERSION_MINOR;
3081 *micro = WESTON_VERSION_MICRO;
3082}
3083
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003084static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003085 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003086 const char *desc;
3087} capability_strings[] = {
3088 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003089 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003090};
3091
3092static void
3093weston_compositor_log_capabilities(struct weston_compositor *compositor)
3094{
3095 unsigned i;
3096 int yes;
3097
3098 weston_log("Compositor capabilities:\n");
3099 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3100 yes = compositor->capabilities & capability_strings[i].bit;
3101 weston_log_continue(STAMP_SPACE "%s %s\n",
3102 capability_strings[i].desc,
3103 yes ? "yes" : "no");
3104 }
3105}
3106
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003107static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003108{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003109 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003110
Martin Minarik6d118362012-06-07 18:01:59 +02003111 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003112 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003113
3114 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003115}
3116
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003117#ifdef HAVE_LIBUNWIND
3118
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003119static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003120print_backtrace(void)
3121{
3122 unw_cursor_t cursor;
3123 unw_context_t context;
3124 unw_word_t off;
3125 unw_proc_info_t pip;
3126 int ret, i = 0;
3127 char procname[256];
3128 const char *filename;
3129 Dl_info dlinfo;
3130
3131 pip.unwind_info = NULL;
3132 ret = unw_getcontext(&context);
3133 if (ret) {
3134 weston_log("unw_getcontext: %d\n", ret);
3135 return;
3136 }
3137
3138 ret = unw_init_local(&cursor, &context);
3139 if (ret) {
3140 weston_log("unw_init_local: %d\n", ret);
3141 return;
3142 }
3143
3144 ret = unw_step(&cursor);
3145 while (ret > 0) {
3146 ret = unw_get_proc_info(&cursor, &pip);
3147 if (ret) {
3148 weston_log("unw_get_proc_info: %d\n", ret);
3149 break;
3150 }
3151
3152 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3153 if (ret && ret != -UNW_ENOMEM) {
3154 if (ret != -UNW_EUNSPEC)
3155 weston_log("unw_get_proc_name: %d\n", ret);
3156 procname[0] = '?';
3157 procname[1] = 0;
3158 }
3159
3160 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3161 *dlinfo.dli_fname)
3162 filename = dlinfo.dli_fname;
3163 else
3164 filename = "?";
3165
3166 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3167 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3168
3169 ret = unw_step(&cursor);
3170 if (ret < 0)
3171 weston_log("unw_step: %d\n", ret);
3172 }
3173}
3174
3175#else
3176
3177static void
3178print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003179{
3180 void *buffer[32];
3181 int i, count;
3182 Dl_info info;
3183
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003184 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3185 for (i = 0; i < count; i++) {
3186 dladdr(buffer[i], &info);
3187 weston_log(" [%016lx] %s (%s)\n",
3188 (long) buffer[i],
3189 info.dli_sname ? info.dli_sname : "--",
3190 info.dli_fname);
3191 }
3192}
3193
3194#endif
3195
3196static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003197on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003198{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003199 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003200 * then call the backend restore function, which will switch
3201 * back to the vt we launched from or ungrab X etc and then
3202 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003203 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003204 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003205 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003206
Peter Maatmane5b42e42013-03-27 22:38:53 +01003207 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003208
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003209 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003210
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003211 segv_compositor->restore(segv_compositor);
3212
3213 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003214}
3215
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003216static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003217load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003218{
3219 char path[PATH_MAX];
3220 void *module, *init;
3221
3222 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003223 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003224 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003225 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003226
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003227 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3228 if (module) {
3229 weston_log("Module '%s' already loaded\n", path);
3230 dlclose(module);
3231 return NULL;
3232 }
3233
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003234 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003235 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003236 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003237 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003238 return NULL;
3239 }
3240
3241 init = dlsym(module, entrypoint);
3242 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003243 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003244 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003245 return NULL;
3246 }
3247
3248 return init;
3249}
3250
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003251static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003252load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003253 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003254{
3255 const char *p, *end;
3256 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003257 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003258 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003259
3260 if (modules == NULL)
3261 return 0;
3262
3263 p = modules;
3264 while (*p) {
3265 end = strchrnul(p, ',');
3266 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3267 module_init = load_module(buffer, "module_init");
3268 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003269 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003270 p = end;
3271 while (*p == ',')
3272 p++;
3273
3274 }
3275
3276 return 0;
3277}
3278
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003279static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003280 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3281
3282static const char xdg_wrong_message[] =
3283 "fatal: environment variable XDG_RUNTIME_DIR\n"
3284 "is set to \"%s\", which is not a directory.\n";
3285
3286static const char xdg_wrong_mode_message[] =
3287 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3288 "correctly. Unix access mode must be 0700 but is %o,\n"
3289 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003290 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003291
3292static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003293 "Refer to your distribution on how to get it, or\n"
3294 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3295 "on how to implement it.\n";
3296
Martin Minarik37032f82012-06-18 20:15:18 +02003297static void
3298verify_xdg_runtime_dir(void)
3299{
3300 char *dir = getenv("XDG_RUNTIME_DIR");
3301 struct stat s;
3302
3303 if (!dir) {
3304 weston_log(xdg_error_message);
3305 weston_log_continue(xdg_detail_message);
3306 exit(EXIT_FAILURE);
3307 }
3308
3309 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3310 weston_log(xdg_wrong_message, dir);
3311 weston_log_continue(xdg_detail_message);
3312 exit(EXIT_FAILURE);
3313 }
3314
3315 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3316 weston_log(xdg_wrong_mode_message,
3317 dir, s.st_mode & 0777, s.st_uid);
3318 weston_log_continue(xdg_detail_message);
3319 }
3320}
3321
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003322static int
3323usage(int error_code)
3324{
3325 fprintf(stderr,
3326 "Usage: weston [OPTIONS]\n\n"
3327 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3328 "Weston supports multiple backends, and depending on which backend is in use\n"
3329 "different options will be accepted.\n\n"
3330
3331
3332 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003333 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003334 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003335 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3336 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003337 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003338 " -S, --socket=NAME\tName of socket to listen on\n"
3339 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003340 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003341 " --log==FILE\t\tLog to the given file\n"
3342 " -h, --help\t\tThis help message\n\n");
3343
3344 fprintf(stderr,
3345 "Options for drm-backend.so:\n\n"
3346 " --connector=ID\tBring up only this connector\n"
3347 " --seat=SEAT\t\tThe seat that weston should run on\n"
3348 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003349 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003350 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3351
3352 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003353 "Options for fbdev-backend.so:\n\n"
3354 " --tty=TTY\t\tThe tty to use\n"
3355 " --device=DEVICE\tThe framebuffer device to use\n\n");
3356
3357 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003358 "Options for x11-backend.so:\n\n"
3359 " --width=WIDTH\t\tWidth of X window\n"
3360 " --height=HEIGHT\tHeight of X window\n"
3361 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003362 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003363 " --output-count=COUNT\tCreate multiple outputs\n"
3364 " --no-input\t\tDont create input devices\n\n");
3365
3366 fprintf(stderr,
3367 "Options for wayland-backend.so:\n\n"
3368 " --width=WIDTH\t\tWidth of Wayland surface\n"
3369 " --height=HEIGHT\tHeight of Wayland surface\n"
3370 " --display=DISPLAY\tWayland display to connect to\n\n");
3371
Pekka Paalanene31e0532013-05-22 18:03:07 +03003372#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3373 fprintf(stderr,
3374 "Options for rpi-backend.so:\n\n"
3375 " --tty=TTY\t\tThe tty to use\n"
3376 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3377 " --transform=TR\tThe output transformation, TR is one of:\n"
3378 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3379 "\n");
3380#endif
3381
Hardeningc077a842013-07-08 00:51:35 +02003382#if defined(BUILD_RDP_COMPOSITOR)
3383 fprintf(stderr,
3384 "Options for rdp-backend.so:\n\n"
3385 " --width=WIDTH\t\tWidth of desktop\n"
3386 " --height=HEIGHT\tHeight of desktop\n"
3387 " --extra-modes=MODES\t\n"
3388 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3389 " --address=ADDR\tThe address to bind\n"
3390 " --port=PORT\tThe port to listen on\n"
3391 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3392 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3393 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3394 "\n");
3395#endif
3396
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003397 exit(error_code);
3398}
3399
Peter Maatmane5b42e42013-03-27 22:38:53 +01003400static void
3401catch_signals(void)
3402{
3403 struct sigaction action;
3404
3405 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3406 action.sa_sigaction = on_caught_signal;
3407 sigemptyset(&action.sa_mask);
3408 sigaction(SIGSEGV, &action, NULL);
3409 sigaction(SIGABRT, &action, NULL);
3410}
3411
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003412int main(int argc, char *argv[])
3413{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003414 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003415 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003416 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003417 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003418 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003419 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003420 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003421 int *argc, char *argv[],
3422 struct weston_config *config);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003423 int i, config_fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003424 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003425 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003426 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003427 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003428 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003429 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003430 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003431 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003432 struct weston_config *config;
3433 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003434
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003435 const struct weston_option core_options[] = {
3436 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003437 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003438 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3439 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003440 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003441 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003442 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003443 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003444 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003445
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003446 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003447
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003448 if (help)
3449 usage(EXIT_SUCCESS);
3450
Scott Moreau12245142012-08-29 15:15:58 -06003451 if (version) {
3452 printf(PACKAGE_STRING "\n");
3453 return EXIT_SUCCESS;
3454 }
3455
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003456 weston_log_file_open(log);
3457
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003458 weston_log("%s\n"
3459 STAMP_SPACE "%s\n"
3460 STAMP_SPACE "Bug reports to: %s\n"
3461 STAMP_SPACE "Build: %s\n",
3462 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003463 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003464 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003465
Martin Minarik37032f82012-06-18 20:15:18 +02003466 verify_xdg_runtime_dir();
3467
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003468 display = wl_display_create();
3469
Tiago Vignatti2116b892011-08-08 05:52:59 -07003470 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003471 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3472 display);
3473 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3474 display);
3475 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3476 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003477
3478 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003479 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3480 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003481
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003482 if (!backend) {
3483 if (getenv("WAYLAND_DISPLAY"))
3484 backend = "wayland-backend.so";
3485 else if (getenv("DISPLAY"))
3486 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003487 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003488 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003489 }
3490
Ossama Othmana50e6e42013-05-14 09:48:26 -07003491 config_fd = open_config_file("weston.ini");
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003492 config = weston_config_parse(config_fd);
3493 close(config_fd);
3494
3495 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003496 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003497
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003498 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003499 if (!backend_init)
3500 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003501
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003502 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003503 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003504 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003505 exit(EXIT_FAILURE);
3506 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003507
Peter Maatmane5b42e42013-03-27 22:38:53 +01003508 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003509 segv_compositor = ec;
3510
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003511 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003512
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003513 setenv("WAYLAND_DISPLAY", socket_name, 1);
3514
Jason Ekstranda985da42013-08-22 17:28:03 -05003515 if (!shell)
3516 weston_config_section_get_string(section, "shell", &shell,
3517 "desktop-shell.so");
3518 if (load_modules(ec, shell, &argc, argv) < 0)
3519 goto out;
3520
Ossama Othmana50e6e42013-05-14 09:48:26 -07003521 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003522 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003523 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003524 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003525
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003526 for (i = 1; i < argc; i++)
3527 weston_log("fatal: unhandled option: %s\n", argv[i]);
3528 if (argc > 1) {
3529 ret = EXIT_FAILURE;
3530 goto out;
3531 }
3532
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003533 weston_compositor_log_capabilities(ec);
3534
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003535 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003536 weston_log("fatal: failed to add socket: %m\n");
3537 ret = EXIT_FAILURE;
3538 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003539 }
3540
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003541 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003542
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003543 wl_display_run(display);
3544
3545 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003546 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003547 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003548
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003549 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003550
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003551 for (i = ARRAY_LENGTH(signals); i;)
3552 wl_event_source_remove(signals[--i]);
3553
Daniel Stone855028f2012-05-01 20:37:10 +01003554 weston_compositor_xkb_destroy(ec);
3555
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003556 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003557 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003558
Martin Minarik19e6f262012-06-07 13:08:46 +02003559 weston_log_file_close();
3560
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003561 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003562}