blob: 7e2a39446d34df0024c32eeb4bc38d7727bcb833 [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
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100250 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
251 if (seteuid(getuid()) == -1) {
252 weston_log("compositor: failed seteuid\n");
253 return;
254 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500255
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500256 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
257 * non-CLOEXEC fd to pass through exec. */
258 clientfd = dup(sockfd);
259 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500261 return;
262 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200263
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500264 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200265 setenv("WAYLAND_SOCKET", s, 1);
266
267 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200268 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200269 path);
270}
271
272WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273weston_client_launch(struct weston_compositor *compositor,
274 struct weston_process *proc,
275 const char *path,
276 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277{
278 int sv[2];
279 pid_t pid;
280 struct wl_client *client;
281
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300282 weston_log("launching '%s'\n", path);
283
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300284 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200286 "socketpair failed while launching '%s': %m\n",
287 path);
288 return NULL;
289 }
290
291 pid = fork();
292 if (pid == -1) {
293 close(sv[0]);
294 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296 "fork failed while launching '%s': %m\n", path);
297 return NULL;
298 }
299
300 if (pid == 0) {
301 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700302 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 }
304
305 close(sv[1]);
306
307 client = wl_client_create(compositor->wl_display, sv[0]);
308 if (!client) {
309 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200310 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311 "wl_client_create failed while launching '%s'.\n",
312 path);
313 return NULL;
314 }
315
316 proc->pid = pid;
317 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200319
320 return client;
321}
322
323static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300324surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
325{
326 struct weston_surface *surface =
327 container_of(listener, struct weston_surface,
328 pending.buffer_destroy_listener);
329
330 surface->pending.buffer = NULL;
331}
332
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200333static void
334empty_region(pixman_region32_t *region)
335{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300336 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200337 pixman_region32_init(region);
338}
339
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300340static void
341region_init_infinite(pixman_region32_t *region)
342{
343 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
344 UINT32_MAX, UINT32_MAX);
345}
346
Pekka Paalanene67858b2013-04-25 13:57:42 +0300347static struct weston_subsurface *
348weston_surface_to_subsurface(struct weston_surface *surface);
349
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500350WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500351weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500352{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500353 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400354
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200355 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400356 if (surface == NULL)
357 return NULL;
358
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500359 wl_signal_init(&surface->destroy_signal);
360
361 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500362
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500363 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500364 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500365
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500366 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400367 surface->alpha = 1.0;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200368 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400369
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100370 if (compositor->renderer->create_surface(surface) < 0) {
371 free(surface);
372 return NULL;
373 }
374
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200375 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200376 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200377 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200378 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400379 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400380 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100381 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200382
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400383 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500384 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500385 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300386 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200387 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500388 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400389
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200390 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200391 wl_list_insert(&surface->geometry.transformation_list,
392 &surface->transform.position.link);
393 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200394 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200395 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200396 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500397
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300398 surface->pending.buffer_destroy_listener.notify =
399 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300400 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300401 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300402 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300403 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300404
Pekka Paalanene67858b2013-04-25 13:57:42 +0300405 wl_list_init(&surface->subsurface_list);
406 wl_list_init(&surface->subsurface_list_pending);
407
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400408 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500409}
410
Alex Wu8811bf92012-02-28 18:07:54 +0800411WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500412weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200413 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500414{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100415 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500416}
417
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400418WL_EXPORT void
419weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200420 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200421{
422 if (surface->transform.enabled) {
423 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
424
425 weston_matrix_transform(&surface->transform.matrix, &v);
426
427 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200428 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700429 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200430 v.f[3]);
431 *x = 0;
432 *y = 0;
433 return;
434 }
435
436 *x = v.f[0] / v.f[3];
437 *y = v.f[1] / v.f[3];
438 } else {
439 *x = sx + surface->geometry.x;
440 *y = sy + surface->geometry.y;
441 }
442}
443
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500444WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200445weston_transformed_coord(int width, int height,
446 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200447 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200448 float sx, float sy, float *bx, float *by)
449{
450 switch (transform) {
451 case WL_OUTPUT_TRANSFORM_NORMAL:
452 default:
453 *bx = sx;
454 *by = sy;
455 break;
456 case WL_OUTPUT_TRANSFORM_FLIPPED:
457 *bx = width - sx;
458 *by = sy;
459 break;
460 case WL_OUTPUT_TRANSFORM_90:
461 *bx = height - sy;
462 *by = sx;
463 break;
464 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
465 *bx = height - sy;
466 *by = width - sx;
467 break;
468 case WL_OUTPUT_TRANSFORM_180:
469 *bx = width - sx;
470 *by = height - sy;
471 break;
472 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
473 *bx = sx;
474 *by = height - sy;
475 break;
476 case WL_OUTPUT_TRANSFORM_270:
477 *bx = sy;
478 *by = width - sx;
479 break;
480 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
481 *bx = sy;
482 *by = sx;
483 break;
484 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200485
486 *bx *= scale;
487 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200488}
489
490WL_EXPORT pixman_box32_t
491weston_transformed_rect(int width, int height,
492 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200493 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200494 pixman_box32_t rect)
495{
496 float x1, x2, y1, y2;
497
498 pixman_box32_t ret;
499
Alexander Larsson4ea95522013-05-22 14:41:37 +0200500 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200501 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200502 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200503 rect.x2, rect.y2, &x2, &y2);
504
505 if (x1 <= x2) {
506 ret.x1 = x1;
507 ret.x2 = x2;
508 } else {
509 ret.x1 = x2;
510 ret.x2 = x1;
511 }
512
513 if (y1 <= y2) {
514 ret.y1 = y1;
515 ret.y2 = y2;
516 } else {
517 ret.y1 = y2;
518 ret.y2 = y1;
519 }
520
521 return ret;
522}
523
524WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200525weston_surface_to_buffer_float(struct weston_surface *surface,
526 float sx, float sy, float *bx, float *by)
527{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200528 weston_transformed_coord(surface->geometry.width,
529 surface->geometry.height,
530 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200531 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200532 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200533}
534
Alexander Larsson4ea95522013-05-22 14:41:37 +0200535WL_EXPORT void
536weston_surface_to_buffer(struct weston_surface *surface,
537 int sx, int sy, int *bx, int *by)
538{
539 float bxf, byf;
540
541 weston_transformed_coord(surface->geometry.width,
542 surface->geometry.height,
543 surface->buffer_transform,
544 surface->buffer_scale,
545 sx, sy, &bxf, &byf);
546 *bx = floorf(bxf);
547 *by = floorf(byf);
548}
549
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200550WL_EXPORT pixman_box32_t
551weston_surface_to_buffer_rect(struct weston_surface *surface,
552 pixman_box32_t rect)
553{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200554 return weston_transformed_rect(surface->geometry.width,
555 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200556 surface->buffer_transform,
557 surface->buffer_scale,
558 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200559}
560
561WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400562weston_surface_move_to_plane(struct weston_surface *surface,
563 struct weston_plane *plane)
564{
565 if (surface->plane == plane)
566 return;
567
568 weston_surface_damage_below(surface);
569 surface->plane = plane;
570 weston_surface_damage(surface);
571}
572
573WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500574weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200575{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500576 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200577
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500578 pixman_region32_init(&damage);
579 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
580 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400581 pixman_region32_union(&surface->plane->damage,
582 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500583 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200584}
585
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400586static void
587weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
588{
589 uint32_t different = es->output_mask ^ mask;
590 uint32_t entered = mask & different;
591 uint32_t left = es->output_mask & different;
592 struct weston_output *output;
593 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500594 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400595
596 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500597 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400598 return;
599 if (different == 0)
600 return;
601
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500602 client = wl_resource_get_client(es->resource);
603
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400604 wl_list_for_each(output, &es->compositor->output_list, link) {
605 if (1 << output->id & different)
606 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500607 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400608 client);
609 if (resource == NULL)
610 continue;
611 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500612 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400613 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500614 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400615 }
616}
617
618static void
619weston_surface_assign_output(struct weston_surface *es)
620{
621 struct weston_compositor *ec = es->compositor;
622 struct weston_output *output, *new_output;
623 pixman_region32_t region;
624 uint32_t max, area, mask;
625 pixman_box32_t *e;
626
627 new_output = NULL;
628 max = 0;
629 mask = 0;
630 pixman_region32_init(&region);
631 wl_list_for_each(output, &ec->output_list, link) {
632 pixman_region32_intersect(&region, &es->transform.boundingbox,
633 &output->region);
634
635 e = pixman_region32_extents(&region);
636 area = (e->x2 - e->x1) * (e->y2 - e->y1);
637
638 if (area > 0)
639 mask |= 1 << output->id;
640
641 if (area >= max) {
642 new_output = output;
643 max = area;
644 }
645 }
646 pixman_region32_fini(&region);
647
648 es->output = new_output;
649 weston_surface_update_output_mask(es, mask);
650}
651
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200652static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200653surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
654 int32_t width, int32_t height,
655 pixman_region32_t *bbox)
656{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200657 float min_x = HUGE_VALF, min_y = HUGE_VALF;
658 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200659 int32_t s[4][2] = {
660 { sx, sy },
661 { sx, sy + height },
662 { sx + width, sy },
663 { sx + width, sy + height }
664 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200665 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200666 int i;
667
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300668 if (width == 0 || height == 0) {
669 /* avoid rounding empty bbox to 1x1 */
670 pixman_region32_init(bbox);
671 return;
672 }
673
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200674 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200675 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400676 weston_surface_to_global_float(surface,
677 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200678 if (x < min_x)
679 min_x = x;
680 if (x > max_x)
681 max_x = x;
682 if (y < min_y)
683 min_y = y;
684 if (y > max_y)
685 max_y = y;
686 }
687
Pekka Paalanen219b9822012-02-08 15:38:37 +0200688 int_x = floorf(min_x);
689 int_y = floorf(min_y);
690 pixman_region32_init_rect(bbox, int_x, int_y,
691 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200692}
693
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200694static void
695weston_surface_update_transform_disable(struct weston_surface *surface)
696{
697 surface->transform.enabled = 0;
698
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200699 /* round off fractions when not transformed */
700 surface->geometry.x = roundf(surface->geometry.x);
701 surface->geometry.y = roundf(surface->geometry.y);
702
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500703 /* Otherwise identity matrix, but with x and y translation. */
704 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
705 surface->transform.position.matrix.d[12] = surface->geometry.x;
706 surface->transform.position.matrix.d[13] = surface->geometry.y;
707
708 surface->transform.matrix = surface->transform.position.matrix;
709
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500710 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500711 surface->transform.inverse.d[12] = -surface->geometry.x;
712 surface->transform.inverse.d[13] = -surface->geometry.y;
713
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200714 pixman_region32_init_rect(&surface->transform.boundingbox,
715 surface->geometry.x,
716 surface->geometry.y,
717 surface->geometry.width,
718 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500719
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400720 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500721 pixman_region32_copy(&surface->transform.opaque,
722 &surface->opaque);
723 pixman_region32_translate(&surface->transform.opaque,
724 surface->geometry.x,
725 surface->geometry.y);
726 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200727}
728
729static int
730weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200731{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200732 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200733 struct weston_matrix *matrix = &surface->transform.matrix;
734 struct weston_matrix *inverse = &surface->transform.inverse;
735 struct weston_transform *tform;
736
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200737 surface->transform.enabled = 1;
738
739 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300740 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200741 surface->transform.position.matrix.d[12] = surface->geometry.x;
742 surface->transform.position.matrix.d[13] = surface->geometry.y;
743
744 weston_matrix_init(matrix);
745 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
746 weston_matrix_multiply(matrix, &tform->matrix);
747
Pekka Paalanen483243f2013-03-08 14:56:50 +0200748 if (parent)
749 weston_matrix_multiply(matrix, &parent->transform.matrix);
750
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200751 if (weston_matrix_invert(inverse, matrix) < 0) {
752 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200753 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200754 " transformation not invertible.\n", surface);
755 return -1;
756 }
757
758 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
759 surface->geometry.height,
760 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500761
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200762 return 0;
763}
764
765WL_EXPORT void
766weston_surface_update_transform(struct weston_surface *surface)
767{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200768 struct weston_surface *parent = surface->geometry.parent;
769
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200770 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200771 return;
772
Pekka Paalanen483243f2013-03-08 14:56:50 +0200773 if (parent)
774 weston_surface_update_transform(parent);
775
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200776 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200777
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500778 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200779
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200780 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500781 pixman_region32_fini(&surface->transform.opaque);
782 pixman_region32_init(&surface->transform.opaque);
783
Pekka Paalanencd403622012-01-25 13:37:39 +0200784 /* transform.position is always in transformation_list */
785 if (surface->geometry.transformation_list.next ==
786 &surface->transform.position.link &&
787 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200788 &surface->transform.position.link &&
789 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200790 weston_surface_update_transform_disable(surface);
791 } else {
792 if (weston_surface_update_transform_enable(surface) < 0)
793 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200794 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200795
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400796 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200797
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300798 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300799
800 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200801}
802
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200803WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200804weston_surface_geometry_dirty(struct weston_surface *surface)
805{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200806 struct weston_surface *child;
807
808 /*
809 * The invariant: if surface->geometry.dirty, then all surfaces
810 * in surface->geometry.child_list have geometry.dirty too.
811 * Corollary: if not parent->geometry.dirty, then all ancestors
812 * are not dirty.
813 */
814
815 if (surface->transform.dirty)
816 return;
817
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200818 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200819
820 wl_list_for_each(child, &surface->geometry.child_list,
821 geometry.parent_link)
822 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200823}
824
825WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100826weston_surface_to_global_fixed(struct weston_surface *surface,
827 wl_fixed_t sx, wl_fixed_t sy,
828 wl_fixed_t *x, wl_fixed_t *y)
829{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200830 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100831
832 weston_surface_to_global_float(surface,
833 wl_fixed_to_double(sx),
834 wl_fixed_to_double(sy),
835 &xf, &yf);
836 *x = wl_fixed_from_double(xf);
837 *y = wl_fixed_from_double(yf);
838}
839
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400840WL_EXPORT void
841weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200842 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200843{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200844 if (surface->transform.enabled) {
845 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
846
847 weston_matrix_transform(&surface->transform.inverse, &v);
848
849 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200850 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200851 "weston_surface_from_global(), divisor = %g\n",
852 v.f[3]);
853 *sx = 0;
854 *sy = 0;
855 return;
856 }
857
Pekka Paalanencd403622012-01-25 13:37:39 +0200858 *sx = v.f[0] / v.f[3];
859 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200860 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200861 *sx = x - surface->geometry.x;
862 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200863 }
864}
865
866WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100867weston_surface_from_global_fixed(struct weston_surface *surface,
868 wl_fixed_t x, wl_fixed_t y,
869 wl_fixed_t *sx, wl_fixed_t *sy)
870{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200871 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100872
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400873 weston_surface_from_global_float(surface,
874 wl_fixed_to_double(x),
875 wl_fixed_to_double(y),
876 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100877 *sx = wl_fixed_from_double(sxf);
878 *sy = wl_fixed_from_double(syf);
879}
880
881WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200882weston_surface_from_global(struct weston_surface *surface,
883 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
884{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200885 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200886
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400887 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200888 *sx = floorf(sxf);
889 *sy = floorf(syf);
890}
891
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400892WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400893weston_surface_schedule_repaint(struct weston_surface *surface)
894{
895 struct weston_output *output;
896
897 wl_list_for_each(output, &surface->compositor->output_list, link)
898 if (surface->output_mask & (1 << output->id))
899 weston_output_schedule_repaint(output);
900}
901
902WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500903weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500904{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400905 pixman_region32_union_rect(&surface->damage, &surface->damage,
906 0, 0, surface->geometry.width,
907 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200908
Kristian Høgsberg98238702012-08-03 16:29:12 -0400909 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500910}
911
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400912WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200914 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400915{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200916 surface->geometry.x = x;
917 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200918 surface->geometry.width = width;
919 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200920 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400921}
922
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200923WL_EXPORT void
924weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200925 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200926{
927 surface->geometry.x = x;
928 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200929 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200930}
931
Pekka Paalanen483243f2013-03-08 14:56:50 +0200932static void
933transform_parent_handle_parent_destroy(struct wl_listener *listener,
934 void *data)
935{
936 struct weston_surface *surface =
937 container_of(listener, struct weston_surface,
938 geometry.parent_destroy_listener);
939
940 weston_surface_set_transform_parent(surface, NULL);
941}
942
943WL_EXPORT void
944weston_surface_set_transform_parent(struct weston_surface *surface,
945 struct weston_surface *parent)
946{
947 if (surface->geometry.parent) {
948 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
949 wl_list_remove(&surface->geometry.parent_link);
950 }
951
952 surface->geometry.parent = parent;
953
954 surface->geometry.parent_destroy_listener.notify =
955 transform_parent_handle_parent_destroy;
956 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500957 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200958 &surface->geometry.parent_destroy_listener);
959 wl_list_insert(&parent->geometry.child_list,
960 &surface->geometry.parent_link);
961 }
962
963 weston_surface_geometry_dirty(surface);
964}
965
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300966WL_EXPORT int
967weston_surface_is_mapped(struct weston_surface *surface)
968{
969 if (surface->output)
970 return 1;
971 else
972 return 0;
973}
974
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200975WL_EXPORT int32_t
976weston_surface_buffer_width(struct weston_surface *surface)
977{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200978 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200979 switch (surface->buffer_transform) {
980 case WL_OUTPUT_TRANSFORM_90:
981 case WL_OUTPUT_TRANSFORM_270:
982 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
983 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200984 width = surface->buffer_ref.buffer->height;
985 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200986 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200987 width = surface->buffer_ref.buffer->width;
988 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200989 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200990 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200991}
992
993WL_EXPORT int32_t
994weston_surface_buffer_height(struct weston_surface *surface)
995{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200996 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200997 switch (surface->buffer_transform) {
998 case WL_OUTPUT_TRANSFORM_90:
999 case WL_OUTPUT_TRANSFORM_270:
1000 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1001 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001002 height = surface->buffer_ref.buffer->width;
1003 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001004 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001005 height = surface->buffer_ref.buffer->height;
1006 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001007 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001008 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001009}
1010
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001011WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001013{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001014 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001015
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001016 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001017
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001018 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001019}
1020
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001021WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +02001022weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +01001023 wl_fixed_t x, wl_fixed_t y,
1024 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001025{
1026 struct weston_surface *surface;
1027
1028 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001029 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001030 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +01001031 wl_fixed_to_int(*sx),
1032 wl_fixed_to_int(*sy),
1033 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +02001034 return surface;
1035 }
1036
1037 return NULL;
1038}
1039
1040static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001041weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001042{
Daniel Stone37816df2012-05-16 18:45:18 +01001043 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001044
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001045 if (!compositor->focus)
1046 return;
1047
Daniel Stone37816df2012-05-16 18:45:18 +01001048 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001049 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001050}
1051
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001052WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001053weston_surface_unmap(struct weston_surface *surface)
1054{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001055 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001056
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001057 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001058 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001059 wl_list_remove(&surface->layer_link);
Giulio Camuffo4df790e2013-09-21 18:08:28 +02001060 wl_list_remove(&surface->link);
1061 wl_list_init(&surface->link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001062
Daniel Stone4dab5db2012-05-30 16:31:53 +01001063 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001064 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001065 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001066 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001067 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001068 NULL,
1069 wl_fixed_from_int(0),
1070 wl_fixed_from_int(0));
Michael Fua2bb7912013-07-23 15:51:06 +08001071 if (seat->touch && seat->touch->focus == surface)
1072 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001073 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001074
Kristian Høgsberg98238702012-08-03 16:29:12 -04001075 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001076}
1077
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001078struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001079 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001080 struct wl_list link;
1081};
1082
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001083
1084WL_EXPORT void
1085weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001086{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001087 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001088 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001089
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001090 if (--surface->ref_count > 0)
1091 return;
1092
1093 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1094
Pekka Paalanen483243f2013-03-08 14:56:50 +02001095 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001096 assert(wl_list_empty(&surface->subsurface_list_pending));
1097 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001098
Kristian Høgsbergb12e3562013-09-21 21:26:05 -07001099 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001100 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001101
Pekka Paalanenbc106382012-10-10 12:49:31 +03001102 wl_list_for_each_safe(cb, next,
1103 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001104 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001105
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001106 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001107 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001108 pixman_region32_fini(&surface->pending.damage);
1109
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001110 if (surface->pending.buffer)
1111 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1112
Pekka Paalanende685b82012-12-04 15:58:12 +02001113 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001114
Kristian Høgsberg42263852012-09-06 21:59:29 -04001115 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001116
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001117 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001118 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001119 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001120 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001121 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001122
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001123 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001124 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001125
Pekka Paalanen483243f2013-03-08 14:56:50 +02001126 weston_surface_set_transform_parent(surface, NULL);
1127
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001128 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001129}
1130
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001131static void
1132destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001133{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001134 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001135
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001136 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001137}
1138
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001139static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001140weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1141{
1142 struct weston_buffer *buffer =
1143 container_of(listener, struct weston_buffer, destroy_listener);
1144
1145 wl_signal_emit(&buffer->destroy_signal, buffer);
1146 free(buffer);
1147}
1148
1149struct weston_buffer *
1150weston_buffer_from_resource(struct wl_resource *resource)
1151{
1152 struct weston_buffer *buffer;
1153 struct wl_listener *listener;
1154
1155 listener = wl_resource_get_destroy_listener(resource,
1156 weston_buffer_destroy_handler);
1157
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001158 if (listener)
1159 return container_of(listener, struct weston_buffer,
1160 destroy_listener);
1161
1162 buffer = zalloc(sizeof *buffer);
1163 if (buffer == NULL)
1164 return NULL;
1165
1166 buffer->resource = resource;
1167 wl_signal_init(&buffer->destroy_signal);
1168 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001169 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001170 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001171
1172 return buffer;
1173}
1174
1175static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001176weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1177 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001178{
Pekka Paalanende685b82012-12-04 15:58:12 +02001179 struct weston_buffer_reference *ref =
1180 container_of(listener, struct weston_buffer_reference,
1181 destroy_listener);
1182
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001183 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001184 ref->buffer = NULL;
1185}
1186
1187WL_EXPORT void
1188weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001189 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001190{
1191 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001192 ref->buffer->busy_count--;
1193 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001194 assert(wl_resource_get_client(ref->buffer->resource));
1195 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001196 WL_BUFFER_RELEASE);
1197 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001198 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001199 }
1200
Pekka Paalanende685b82012-12-04 15:58:12 +02001201 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001202 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001203 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001204 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001205 }
1206
Pekka Paalanende685b82012-12-04 15:58:12 +02001207 ref->buffer = buffer;
1208 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1209}
1210
1211static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001212weston_surface_attach(struct weston_surface *surface,
1213 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001214{
1215 weston_buffer_reference(&surface->buffer_ref, buffer);
1216
Pekka Paalanena6421c42012-12-04 15:58:10 +02001217 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001218 if (weston_surface_is_mapped(surface))
1219 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001220 }
1221
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001222 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001223}
1224
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225WL_EXPORT void
1226weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001227{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001228 wl_list_remove(&surface->layer_link);
1229 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001230 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001231 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001232}
1233
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001234WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001236{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001237 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001238
1239 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001240 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001241}
1242
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001243WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001244weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001245{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001247
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001248 pixman_region32_union(&compositor->primary_plane.damage,
1249 &compositor->primary_plane.damage,
1250 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001251 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001252}
1253
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001254static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001255surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001256 pixman_region32_t *opaque)
1257{
Pekka Paalanende685b82012-12-04 15:58:12 +02001258 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001259 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001260 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001261
1262 if (surface->transform.enabled) {
1263 pixman_box32_t *extents;
1264
1265 extents = pixman_region32_extents(&surface->damage);
1266 surface_compute_bbox(surface, extents->x1, extents->y1,
1267 extents->x2 - extents->x1,
1268 extents->y2 - extents->y1,
1269 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001270 pixman_region32_translate(&surface->damage,
1271 -surface->plane->x,
1272 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001273 } else {
1274 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001275 surface->geometry.x - surface->plane->x,
1276 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001277 }
1278
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001279 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001280 pixman_region32_union(&surface->plane->damage,
1281 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001282 empty_region(&surface->damage);
1283 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001284 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001285}
1286
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001287static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001288compositor_accumulate_damage(struct weston_compositor *ec)
1289{
1290 struct weston_plane *plane;
1291 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001292 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001293
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001294 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001295
1296 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001297 pixman_region32_copy(&plane->clip, &clip);
1298
1299 pixman_region32_init(&opaque);
1300
1301 wl_list_for_each(es, &ec->surface_list, link) {
1302 if (es->plane != plane)
1303 continue;
1304
1305 surface_accumulate_damage(es, &opaque);
1306 }
1307
1308 pixman_region32_union(&clip, &clip, &opaque);
1309 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001310 }
1311
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001312 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001313
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001314 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001315 /* Both the renderer and the backend have seen the buffer
1316 * by now. If renderer needs the buffer, it has its own
1317 * reference set. If the backend wants to keep the buffer
1318 * around for migrating the surface into a non-primary plane
1319 * later, keep_buffer is true. Otherwise, drop the core
1320 * reference now, and allow early buffer release. This enables
1321 * clients to use single-buffering.
1322 */
1323 if (!es->keep_buffer)
1324 weston_buffer_reference(&es->buffer_ref, NULL);
1325 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001326}
1327
1328static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001329surface_list_add(struct weston_compositor *compositor,
1330 struct weston_surface *surface)
1331{
1332 struct weston_subsurface *sub;
1333
1334 if (wl_list_empty(&surface->subsurface_list)) {
1335 weston_surface_update_transform(surface);
1336 wl_list_insert(compositor->surface_list.prev, &surface->link);
1337 return;
1338 }
1339
1340 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1341 if (!weston_surface_is_mapped(sub->surface))
1342 continue;
1343
1344 if (sub->surface == surface) {
1345 weston_surface_update_transform(sub->surface);
1346 wl_list_insert(compositor->surface_list.prev,
1347 &sub->surface->link);
1348 } else {
1349 surface_list_add(compositor, sub->surface);
1350 }
1351 }
1352}
1353
1354static void
1355weston_compositor_build_surface_list(struct weston_compositor *compositor)
1356{
1357 struct weston_surface *surface;
1358 struct weston_layer *layer;
1359
1360 wl_list_init(&compositor->surface_list);
1361 wl_list_for_each(layer, &compositor->layer_list, link) {
1362 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1363 surface_list_add(compositor, surface);
1364 }
1365 }
1366}
1367
1368static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001369weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001371 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001372 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001373 struct weston_animation *animation, *next;
1374 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001375 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001376 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001377
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001378 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001379 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001380
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001381 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001382 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001383 else
1384 wl_list_for_each(es, &ec->surface_list, link)
1385 weston_surface_move_to_plane(es, &ec->primary_plane);
1386
Pekka Paalanene67858b2013-04-25 13:57:42 +03001387 wl_list_init(&frame_callback_list);
1388 wl_list_for_each(es, &ec->surface_list, link) {
1389 if (es->output == output) {
1390 wl_list_insert_list(&frame_callback_list,
1391 &es->frame_callback_list);
1392 wl_list_init(&es->frame_callback_list);
1393 }
1394 }
1395
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001396 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001397
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001398 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001399 pixman_region32_intersect(&output_damage,
1400 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001401 pixman_region32_subtract(&output_damage,
1402 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001403
Scott Moreauccbf29d2012-02-22 14:21:41 -07001404 if (output->dirty)
1405 weston_output_update_matrix(output);
1406
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001407 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001408
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001409 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001410
Kristian Høgsbergef044142011-06-21 15:02:12 -04001411 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001412
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001413 weston_compositor_repick(ec);
1414 wl_event_loop_dispatch(ec->input_loop, 0);
1415
Jonas Ådahldb773762012-06-13 00:01:21 +02001416 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001417 wl_callback_send_done(cb->resource, msecs);
1418 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001419 }
1420
Scott Moreaud64cf212012-06-08 19:40:54 -06001421 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001422 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001423 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001424 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001425}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001426
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001427static int
1428weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001429{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001430 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001431
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001432 wl_event_loop_dispatch(compositor->input_loop, 0);
1433
1434 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001435}
1436
1437WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001438weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001439{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001440 struct weston_compositor *compositor = output->compositor;
1441 struct wl_event_loop *loop =
1442 wl_display_get_event_loop(compositor->wl_display);
1443 int fd;
1444
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001445 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001446
1447 if (output->repaint_needed &&
1448 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1449 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001450 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001451 return;
1452 }
1453
1454 output->repaint_scheduled = 0;
1455 if (compositor->input_loop_source)
1456 return;
1457
1458 fd = wl_event_loop_get_fd(compositor->input_loop);
1459 compositor->input_loop_source =
1460 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1461 weston_compositor_read_input, compositor);
1462}
1463
1464static void
1465idle_repaint(void *data)
1466{
1467 struct weston_output *output = data;
1468
Jonas Ådahle5a12252013-04-05 23:07:11 +02001469 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001470}
1471
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001472WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001473weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1474{
1475 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001476 if (below != NULL)
1477 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001478}
1479
1480WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001481weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001482{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001483 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001484 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001485
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001486 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1487 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001488 return;
1489
Kristian Høgsbergef044142011-06-21 15:02:12 -04001490 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001491 output->repaint_needed = 1;
1492 if (output->repaint_scheduled)
1493 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001494
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001495 wl_event_loop_add_idle(loop, idle_repaint, output);
1496 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001497
1498 if (compositor->input_loop_source) {
1499 wl_event_source_remove(compositor->input_loop_source);
1500 compositor->input_loop_source = NULL;
1501 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001502}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001503
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001504WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001505weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1506{
1507 struct weston_output *output;
1508
1509 wl_list_for_each(output, &compositor->output_list, link)
1510 weston_output_schedule_repaint(output);
1511}
1512
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001513static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001514surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001515{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001516 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001517}
1518
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001519static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001520surface_attach(struct wl_client *client,
1521 struct wl_resource *resource,
1522 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1523{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001524 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001525 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001526
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001527 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001528 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001529 if (buffer == NULL) {
1530 wl_client_post_no_memory(client);
1531 return;
1532 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001533 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001534
Pekka Paalanende685b82012-12-04 15:58:12 +02001535 /* Attach, attach, without commit in between does not send
1536 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001537 if (surface->pending.buffer)
1538 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001539
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001540 surface->pending.sx = sx;
1541 surface->pending.sy = sy;
1542 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001543 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001544 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001545 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001546 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001547 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001548}
1549
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001550static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001551surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001552 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001553 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001554{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001555 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001556
Pekka Paalanen8e159182012-10-10 12:49:25 +03001557 pixman_region32_union_rect(&surface->pending.damage,
1558 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001559 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001560}
1561
Kristian Høgsberg33418202011-08-16 23:01:28 -04001562static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001563destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001564{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001565 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001566
1567 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001568 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001569}
1570
1571static void
1572surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001573 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001574{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001575 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001576 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001577
1578 cb = malloc(sizeof *cb);
1579 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001580 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001581 return;
1582 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001583
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001584 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1585 callback);
1586 if (cb->resource == NULL) {
1587 free(cb);
1588 wl_resource_post_no_memory(resource);
1589 return;
1590 }
1591
Jason Ekstranda85118c2013-06-27 20:17:02 -05001592 wl_resource_set_implementation(cb->resource, NULL, cb,
1593 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001594
Pekka Paalanenbc106382012-10-10 12:49:31 +03001595 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001596}
1597
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001598static void
1599surface_set_opaque_region(struct wl_client *client,
1600 struct wl_resource *resource,
1601 struct wl_resource *region_resource)
1602{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001603 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001604 struct weston_region *region;
1605
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001606 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001607 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001608 pixman_region32_copy(&surface->pending.opaque,
1609 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001610 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001611 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001612 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001613}
1614
1615static void
1616surface_set_input_region(struct wl_client *client,
1617 struct wl_resource *resource,
1618 struct wl_resource *region_resource)
1619{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001620 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001621 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001622
1623 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001624 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001625 pixman_region32_copy(&surface->pending.input,
1626 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001627 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001628 pixman_region32_fini(&surface->pending.input);
1629 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001630 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001631}
1632
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001633static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001634weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001635{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001636 struct weston_subsurface *sub;
1637
1638 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1639 parent_link_pending) {
1640 wl_list_remove(&sub->parent_link);
1641 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1642 }
1643}
1644
1645static void
1646weston_surface_commit(struct weston_surface *surface)
1647{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001648 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001649 int surface_width = 0;
1650 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001651
Alexander Larsson4ea95522013-05-22 14:41:37 +02001652 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001653 surface->buffer_transform = surface->pending.buffer_transform;
1654
Alexander Larsson4ea95522013-05-22 14:41:37 +02001655 /* wl_surface.set_buffer_scale */
1656 surface->buffer_scale = surface->pending.buffer_scale;
1657
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001658 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001659 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001660 weston_surface_attach(surface, surface->pending.buffer);
1661
Giulio Camuffo184df502013-02-21 11:29:21 +01001662 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001663 surface_width = weston_surface_buffer_width(surface);
1664 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001665 }
1666
1667 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001668 surface->configure(surface,
1669 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001670 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001671
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001672 if (surface->pending.buffer)
1673 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1674 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001675 surface->pending.sx = 0;
1676 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001677 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001678
1679 /* wl_surface.damage */
1680 pixman_region32_union(&surface->damage, &surface->damage,
1681 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001682 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1683 0, 0,
1684 surface->geometry.width,
1685 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001686 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001687
1688 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001689 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001690 surface->geometry.width,
1691 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001692 pixman_region32_intersect(&opaque,
1693 &opaque, &surface->pending.opaque);
1694
1695 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1696 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001697 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001698 }
1699
1700 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001701
1702 /* wl_surface.set_input_region */
1703 pixman_region32_fini(&surface->input);
1704 pixman_region32_init_rect(&surface->input, 0, 0,
1705 surface->geometry.width,
1706 surface->geometry.height);
1707 pixman_region32_intersect(&surface->input,
1708 &surface->input, &surface->pending.input);
1709
Pekka Paalanenbc106382012-10-10 12:49:31 +03001710 /* wl_surface.frame */
1711 wl_list_insert_list(&surface->frame_callback_list,
1712 &surface->pending.frame_callback_list);
1713 wl_list_init(&surface->pending.frame_callback_list);
1714
Pekka Paalanene67858b2013-04-25 13:57:42 +03001715 weston_surface_commit_subsurface_order(surface);
1716
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001717 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001718}
1719
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001720static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001721weston_subsurface_commit(struct weston_subsurface *sub);
1722
1723static void
1724weston_subsurface_parent_commit(struct weston_subsurface *sub,
1725 int parent_is_synchronized);
1726
1727static void
1728surface_commit(struct wl_client *client, struct wl_resource *resource)
1729{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001730 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001731 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1732
1733 if (sub) {
1734 weston_subsurface_commit(sub);
1735 return;
1736 }
1737
1738 weston_surface_commit(surface);
1739
1740 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1741 if (sub->surface != surface)
1742 weston_subsurface_parent_commit(sub, 0);
1743 }
1744}
1745
1746static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001747surface_set_buffer_transform(struct wl_client *client,
1748 struct wl_resource *resource, int transform)
1749{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001750 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001751
1752 surface->pending.buffer_transform = transform;
1753}
1754
Alexander Larsson4ea95522013-05-22 14:41:37 +02001755static void
1756surface_set_buffer_scale(struct wl_client *client,
1757 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001758 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001759{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001760 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001761
1762 surface->pending.buffer_scale = scale;
1763}
1764
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001765static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001766 surface_destroy,
1767 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001768 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001769 surface_frame,
1770 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001771 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001772 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001773 surface_set_buffer_transform,
1774 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001775};
1776
1777static void
1778compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001779 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001780{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001781 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001782 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001783
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001784 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001785 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001786 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001787 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001788 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001789
Jason Ekstranda85118c2013-06-27 20:17:02 -05001790 surface->resource =
1791 wl_resource_create(client, &wl_surface_interface,
1792 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001793 if (surface->resource == NULL) {
1794 weston_surface_destroy(surface);
1795 wl_resource_post_no_memory(resource);
1796 return;
1797 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001798 wl_resource_set_implementation(surface->resource, &surface_interface,
1799 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001800}
1801
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001802static void
1803destroy_region(struct wl_resource *resource)
1804{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001805 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001806
1807 pixman_region32_fini(&region->region);
1808 free(region);
1809}
1810
1811static void
1812region_destroy(struct wl_client *client, struct wl_resource *resource)
1813{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001814 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001815}
1816
1817static void
1818region_add(struct wl_client *client, struct wl_resource *resource,
1819 int32_t x, int32_t y, int32_t width, int32_t height)
1820{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001821 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001822
1823 pixman_region32_union_rect(&region->region, &region->region,
1824 x, y, width, height);
1825}
1826
1827static void
1828region_subtract(struct wl_client *client, struct wl_resource *resource,
1829 int32_t x, int32_t y, int32_t width, int32_t height)
1830{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001831 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001832 pixman_region32_t rect;
1833
1834 pixman_region32_init_rect(&rect, x, y, width, height);
1835 pixman_region32_subtract(&region->region, &region->region, &rect);
1836 pixman_region32_fini(&rect);
1837}
1838
1839static const struct wl_region_interface region_interface = {
1840 region_destroy,
1841 region_add,
1842 region_subtract
1843};
1844
1845static void
1846compositor_create_region(struct wl_client *client,
1847 struct wl_resource *resource, uint32_t id)
1848{
1849 struct weston_region *region;
1850
1851 region = malloc(sizeof *region);
1852 if (region == NULL) {
1853 wl_resource_post_no_memory(resource);
1854 return;
1855 }
1856
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001857 pixman_region32_init(&region->region);
1858
Jason Ekstranda85118c2013-06-27 20:17:02 -05001859 region->resource =
1860 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001861 if (region->resource == NULL) {
1862 free(region);
1863 wl_resource_post_no_memory(resource);
1864 return;
1865 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001866 wl_resource_set_implementation(region->resource, &region_interface,
1867 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001868}
1869
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001870static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001871 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001872 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001873};
1874
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001875static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001876weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1877{
1878 struct weston_surface *surface = sub->surface;
1879 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001880 int surface_width = 0;
1881 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001882
Alexander Larsson4ea95522013-05-22 14:41:37 +02001883 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001884 surface->buffer_transform = sub->cached.buffer_transform;
1885
Alexander Larsson4ea95522013-05-22 14:41:37 +02001886 /* wl_surface.set_buffer_scale */
1887 surface->buffer_scale = sub->cached.buffer_scale;
1888
Pekka Paalanene67858b2013-04-25 13:57:42 +03001889 /* wl_surface.attach */
1890 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1891 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1892 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1893
1894 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001895 surface_width = weston_surface_buffer_width(surface);
1896 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001897 }
1898
1899 if (surface->configure && sub->cached.newly_attached)
1900 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001901 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001902 sub->cached.sx = 0;
1903 sub->cached.sy = 0;
1904 sub->cached.newly_attached = 0;
1905
1906 /* wl_surface.damage */
1907 pixman_region32_union(&surface->damage, &surface->damage,
1908 &sub->cached.damage);
1909 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1910 0, 0,
1911 surface->geometry.width,
1912 surface->geometry.height);
1913 empty_region(&sub->cached.damage);
1914
1915 /* wl_surface.set_opaque_region */
1916 pixman_region32_init_rect(&opaque, 0, 0,
1917 surface->geometry.width,
1918 surface->geometry.height);
1919 pixman_region32_intersect(&opaque,
1920 &opaque, &sub->cached.opaque);
1921
1922 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1923 pixman_region32_copy(&surface->opaque, &opaque);
1924 weston_surface_geometry_dirty(surface);
1925 }
1926
1927 pixman_region32_fini(&opaque);
1928
1929 /* wl_surface.set_input_region */
1930 pixman_region32_fini(&surface->input);
1931 pixman_region32_init_rect(&surface->input, 0, 0,
1932 surface->geometry.width,
1933 surface->geometry.height);
1934 pixman_region32_intersect(&surface->input,
1935 &surface->input, &sub->cached.input);
1936
1937 /* wl_surface.frame */
1938 wl_list_insert_list(&surface->frame_callback_list,
1939 &sub->cached.frame_callback_list);
1940 wl_list_init(&sub->cached.frame_callback_list);
1941
1942 weston_surface_commit_subsurface_order(surface);
1943
1944 weston_surface_schedule_repaint(surface);
1945
1946 sub->cached.has_data = 0;
1947}
1948
1949static void
1950weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1951{
1952 struct weston_surface *surface = sub->surface;
1953
1954 /*
1955 * If this commit would cause the surface to move by the
1956 * attach(dx, dy) parameters, the old damage region must be
1957 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02001958 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03001959 */
1960 pixman_region32_translate(&sub->cached.damage,
1961 -surface->pending.sx, -surface->pending.sy);
1962 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1963 &surface->pending.damage);
1964 empty_region(&surface->pending.damage);
1965
1966 if (surface->pending.newly_attached) {
1967 sub->cached.newly_attached = 1;
1968 weston_buffer_reference(&sub->cached.buffer_ref,
1969 surface->pending.buffer);
1970 }
1971 sub->cached.sx += surface->pending.sx;
1972 sub->cached.sy += surface->pending.sy;
1973 surface->pending.sx = 0;
1974 surface->pending.sy = 0;
1975 surface->pending.newly_attached = 0;
1976
1977 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001978 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001979
1980 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1981
1982 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1983
1984 wl_list_insert_list(&sub->cached.frame_callback_list,
1985 &surface->pending.frame_callback_list);
1986 wl_list_init(&surface->pending.frame_callback_list);
1987
1988 sub->cached.has_data = 1;
1989}
1990
1991static int
1992weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1993{
1994 while (sub) {
1995 if (sub->synchronized)
1996 return 1;
1997
1998 if (!sub->parent)
1999 return 0;
2000
2001 sub = weston_surface_to_subsurface(sub->parent);
2002 }
2003
2004 return 0;
2005}
2006
2007static void
2008weston_subsurface_commit(struct weston_subsurface *sub)
2009{
2010 struct weston_surface *surface = sub->surface;
2011 struct weston_subsurface *tmp;
2012
2013 /* Recursive check for effectively synchronized. */
2014 if (weston_subsurface_is_synchronized(sub)) {
2015 weston_subsurface_commit_to_cache(sub);
2016 } else {
2017 if (sub->cached.has_data) {
2018 /* flush accumulated state from cache */
2019 weston_subsurface_commit_to_cache(sub);
2020 weston_subsurface_commit_from_cache(sub);
2021 } else {
2022 weston_surface_commit(surface);
2023 }
2024
2025 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2026 if (tmp->surface != surface)
2027 weston_subsurface_parent_commit(tmp, 0);
2028 }
2029 }
2030}
2031
2032static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002033weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002034{
2035 struct weston_surface *surface = sub->surface;
2036 struct weston_subsurface *tmp;
2037
Pekka Paalanene67858b2013-04-25 13:57:42 +03002038 /* From now on, commit_from_cache the whole sub-tree, regardless of
2039 * the synchronized mode of each child. This sub-surface or some
2040 * of its ancestors were synchronized, so we are synchronized
2041 * all the way down.
2042 */
2043
2044 if (sub->cached.has_data)
2045 weston_subsurface_commit_from_cache(sub);
2046
2047 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2048 if (tmp->surface != surface)
2049 weston_subsurface_parent_commit(tmp, 1);
2050 }
2051}
2052
2053static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002054weston_subsurface_parent_commit(struct weston_subsurface *sub,
2055 int parent_is_synchronized)
2056{
2057 if (sub->position.set) {
2058 weston_surface_set_position(sub->surface,
2059 sub->position.x, sub->position.y);
2060 sub->position.set = 0;
2061 }
2062
2063 if (parent_is_synchronized || sub->synchronized)
2064 weston_subsurface_synchronized_commit(sub);
2065}
2066
2067static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002068subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2069 int32_t width, int32_t height)
2070{
2071 struct weston_compositor *compositor = surface->compositor;
2072
2073 weston_surface_configure(surface,
2074 surface->geometry.x + dx,
2075 surface->geometry.y + dy,
2076 width, height);
2077
2078 /* No need to check parent mappedness, because if parent is not
2079 * mapped, parent is not in a visible layer, so this sub-surface
2080 * will not be drawn either.
2081 */
2082 if (!weston_surface_is_mapped(surface)) {
2083 wl_list_init(&surface->layer_link);
2084
2085 /* Cannot call weston_surface_update_transform(),
2086 * because that would call it also for the parent surface,
2087 * which might not be mapped yet. That would lead to
2088 * inconsistent state, where the window could never be
2089 * mapped.
2090 *
2091 * Instead just assing any output, to make
2092 * weston_surface_is_mapped() return true, so that when the
2093 * parent surface does get mapped, this one will get
2094 * included, too. See surface_list_add().
2095 */
2096 assert(!wl_list_empty(&compositor->output_list));
2097 surface->output = container_of(compositor->output_list.next,
2098 struct weston_output, link);
2099 }
2100}
2101
2102static struct weston_subsurface *
2103weston_surface_to_subsurface(struct weston_surface *surface)
2104{
2105 if (surface->configure == subsurface_configure)
2106 return surface->configure_private;
2107
2108 return NULL;
2109}
2110
Pekka Paalanen01388e22013-04-25 13:57:44 +03002111WL_EXPORT struct weston_surface *
2112weston_surface_get_main_surface(struct weston_surface *surface)
2113{
2114 struct weston_subsurface *sub;
2115
2116 while (surface && (sub = weston_surface_to_subsurface(surface)))
2117 surface = sub->parent;
2118
2119 return surface;
2120}
2121
Pekka Paalanene67858b2013-04-25 13:57:42 +03002122static void
2123subsurface_set_position(struct wl_client *client,
2124 struct wl_resource *resource, int32_t x, int32_t y)
2125{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002126 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002127
2128 if (!sub)
2129 return;
2130
2131 sub->position.x = x;
2132 sub->position.y = y;
2133 sub->position.set = 1;
2134}
2135
2136static struct weston_subsurface *
2137subsurface_from_surface(struct weston_surface *surface)
2138{
2139 struct weston_subsurface *sub;
2140
2141 sub = weston_surface_to_subsurface(surface);
2142 if (sub)
2143 return sub;
2144
2145 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2146 if (sub->surface == surface)
2147 return sub;
2148
2149 return NULL;
2150}
2151
2152static struct weston_subsurface *
2153subsurface_sibling_check(struct weston_subsurface *sub,
2154 struct weston_surface *surface,
2155 const char *request)
2156{
2157 struct weston_subsurface *sibling;
2158
2159 sibling = subsurface_from_surface(surface);
2160
2161 if (!sibling) {
2162 wl_resource_post_error(sub->resource,
2163 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2164 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002165 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002166 return NULL;
2167 }
2168
2169 if (sibling->parent != sub->parent) {
2170 wl_resource_post_error(sub->resource,
2171 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2172 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002173 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002174 return NULL;
2175 }
2176
2177 return sibling;
2178}
2179
2180static void
2181subsurface_place_above(struct wl_client *client,
2182 struct wl_resource *resource,
2183 struct wl_resource *sibling_resource)
2184{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002185 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002186 struct weston_surface *surface =
2187 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002188 struct weston_subsurface *sibling;
2189
2190 if (!sub)
2191 return;
2192
2193 sibling = subsurface_sibling_check(sub, surface, "place_above");
2194 if (!sibling)
2195 return;
2196
2197 wl_list_remove(&sub->parent_link_pending);
2198 wl_list_insert(sibling->parent_link_pending.prev,
2199 &sub->parent_link_pending);
2200}
2201
2202static void
2203subsurface_place_below(struct wl_client *client,
2204 struct wl_resource *resource,
2205 struct wl_resource *sibling_resource)
2206{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002207 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002208 struct weston_surface *surface =
2209 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210 struct weston_subsurface *sibling;
2211
2212 if (!sub)
2213 return;
2214
2215 sibling = subsurface_sibling_check(sub, surface, "place_below");
2216 if (!sibling)
2217 return;
2218
2219 wl_list_remove(&sub->parent_link_pending);
2220 wl_list_insert(&sibling->parent_link_pending,
2221 &sub->parent_link_pending);
2222}
2223
2224static void
2225subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2226{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002227 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002228
2229 if (sub)
2230 sub->synchronized = 1;
2231}
2232
2233static void
2234subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2235{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002236 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002237
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002238 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002239 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002240
2241 /* If sub became effectively desynchronized, flush. */
2242 if (!weston_subsurface_is_synchronized(sub))
2243 weston_subsurface_synchronized_commit(sub);
2244 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002245}
2246
2247static void
2248weston_subsurface_cache_init(struct weston_subsurface *sub)
2249{
2250 pixman_region32_init(&sub->cached.damage);
2251 pixman_region32_init(&sub->cached.opaque);
2252 pixman_region32_init(&sub->cached.input);
2253 wl_list_init(&sub->cached.frame_callback_list);
2254 sub->cached.buffer_ref.buffer = NULL;
2255}
2256
2257static void
2258weston_subsurface_cache_fini(struct weston_subsurface *sub)
2259{
2260 struct weston_frame_callback *cb, *tmp;
2261
2262 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002263 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002264
2265 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2266 pixman_region32_fini(&sub->cached.damage);
2267 pixman_region32_fini(&sub->cached.opaque);
2268 pixman_region32_fini(&sub->cached.input);
2269}
2270
2271static void
2272weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2273{
2274 wl_list_remove(&sub->parent_link);
2275 wl_list_remove(&sub->parent_link_pending);
2276 wl_list_remove(&sub->parent_destroy_listener.link);
2277 sub->parent = NULL;
2278}
2279
2280static void
2281weston_subsurface_destroy(struct weston_subsurface *sub);
2282
2283static void
2284subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2285{
2286 struct weston_subsurface *sub =
2287 container_of(listener, struct weston_subsurface,
2288 surface_destroy_listener);
2289 assert(data == &sub->surface->resource);
2290
2291 /* The protocol object (wl_resource) is left inert. */
2292 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002293 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002294
2295 weston_subsurface_destroy(sub);
2296}
2297
2298static void
2299subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2300{
2301 struct weston_subsurface *sub =
2302 container_of(listener, struct weston_subsurface,
2303 parent_destroy_listener);
2304 assert(data == &sub->parent->resource);
2305 assert(sub->surface != sub->parent);
2306
2307 if (weston_surface_is_mapped(sub->surface))
2308 weston_surface_unmap(sub->surface);
2309
2310 weston_subsurface_unlink_parent(sub);
2311}
2312
2313static void
2314subsurface_resource_destroy(struct wl_resource *resource)
2315{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002316 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002317
2318 if (sub)
2319 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002320}
2321
2322static void
2323subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2324{
2325 wl_resource_destroy(resource);
2326}
2327
2328static void
2329weston_subsurface_link_parent(struct weston_subsurface *sub,
2330 struct weston_surface *parent)
2331{
2332 sub->parent = parent;
2333 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002334 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002335 &sub->parent_destroy_listener);
2336
2337 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2338 wl_list_insert(&parent->subsurface_list_pending,
2339 &sub->parent_link_pending);
2340}
2341
2342static void
2343weston_subsurface_link_surface(struct weston_subsurface *sub,
2344 struct weston_surface *surface)
2345{
2346 sub->surface = surface;
2347 sub->surface_destroy_listener.notify =
2348 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002349 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002350 &sub->surface_destroy_listener);
2351}
2352
2353static void
2354weston_subsurface_destroy(struct weston_subsurface *sub)
2355{
2356 assert(sub->surface);
2357
2358 if (sub->resource) {
2359 assert(weston_surface_to_subsurface(sub->surface) == sub);
2360 assert(sub->parent_destroy_listener.notify ==
2361 subsurface_handle_parent_destroy);
2362
2363 weston_surface_set_transform_parent(sub->surface, NULL);
2364 if (sub->parent)
2365 weston_subsurface_unlink_parent(sub);
2366
2367 weston_subsurface_cache_fini(sub);
2368
2369 sub->surface->configure = NULL;
2370 sub->surface->configure_private = NULL;
2371 } else {
2372 /* the dummy weston_subsurface for the parent itself */
2373 assert(sub->parent_destroy_listener.notify == NULL);
2374 wl_list_remove(&sub->parent_link);
2375 wl_list_remove(&sub->parent_link_pending);
2376 }
2377
2378 wl_list_remove(&sub->surface_destroy_listener.link);
2379 free(sub);
2380}
2381
2382static const struct wl_subsurface_interface subsurface_implementation = {
2383 subsurface_destroy,
2384 subsurface_set_position,
2385 subsurface_place_above,
2386 subsurface_place_below,
2387 subsurface_set_sync,
2388 subsurface_set_desync
2389};
2390
2391static struct weston_subsurface *
2392weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2393 struct weston_surface *parent)
2394{
2395 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002396 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002397
2398 sub = calloc(1, sizeof *sub);
2399 if (!sub)
2400 return NULL;
2401
Jason Ekstranda85118c2013-06-27 20:17:02 -05002402 sub->resource =
2403 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002404 if (!sub->resource) {
2405 free(sub);
2406 return NULL;
2407 }
2408
Jason Ekstranda85118c2013-06-27 20:17:02 -05002409 wl_resource_set_implementation(sub->resource,
2410 &subsurface_implementation,
2411 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002412 weston_subsurface_link_surface(sub, surface);
2413 weston_subsurface_link_parent(sub, parent);
2414 weston_subsurface_cache_init(sub);
2415 sub->synchronized = 1;
2416 weston_surface_set_transform_parent(surface, parent);
2417
2418 return sub;
2419}
2420
2421/* Create a dummy subsurface for having the parent itself in its
2422 * sub-surface lists. Makes stacking order manipulation easy.
2423 */
2424static struct weston_subsurface *
2425weston_subsurface_create_for_parent(struct weston_surface *parent)
2426{
2427 struct weston_subsurface *sub;
2428
2429 sub = calloc(1, sizeof *sub);
2430 if (!sub)
2431 return NULL;
2432
2433 weston_subsurface_link_surface(sub, parent);
2434 sub->parent = parent;
2435 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2436 wl_list_insert(&parent->subsurface_list_pending,
2437 &sub->parent_link_pending);
2438
2439 return sub;
2440}
2441
2442static void
2443subcompositor_get_subsurface(struct wl_client *client,
2444 struct wl_resource *resource,
2445 uint32_t id,
2446 struct wl_resource *surface_resource,
2447 struct wl_resource *parent_resource)
2448{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002449 struct weston_surface *surface =
2450 wl_resource_get_user_data(surface_resource);
2451 struct weston_surface *parent =
2452 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453 struct weston_subsurface *sub;
2454 static const char where[] = "get_subsurface: wl_subsurface@";
2455
2456 if (surface == parent) {
2457 wl_resource_post_error(resource,
2458 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2459 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002460 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002461 return;
2462 }
2463
2464 if (weston_surface_to_subsurface(surface)) {
2465 wl_resource_post_error(resource,
2466 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2467 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002468 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002469 return;
2470 }
2471
2472 if (surface->configure) {
2473 wl_resource_post_error(resource,
2474 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2475 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002476 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002477 return;
2478 }
2479
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002480 if (weston_surface_get_main_surface(parent) == surface) {
2481 wl_resource_post_error(resource,
2482 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2483 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002484 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002485 return;
2486 }
2487
Pekka Paalanene67858b2013-04-25 13:57:42 +03002488 /* make sure the parent is in its own list */
2489 if (wl_list_empty(&parent->subsurface_list)) {
2490 if (!weston_subsurface_create_for_parent(parent)) {
2491 wl_resource_post_no_memory(resource);
2492 return;
2493 }
2494 }
2495
2496 sub = weston_subsurface_create(id, surface, parent);
2497 if (!sub) {
2498 wl_resource_post_no_memory(resource);
2499 return;
2500 }
2501
2502 surface->configure = subsurface_configure;
2503 surface->configure_private = sub;
2504}
2505
2506static void
2507subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2508{
2509 wl_resource_destroy(resource);
2510}
2511
2512static const struct wl_subcompositor_interface subcompositor_interface = {
2513 subcompositor_destroy,
2514 subcompositor_get_subsurface
2515};
2516
2517static void
2518bind_subcompositor(struct wl_client *client,
2519 void *data, uint32_t version, uint32_t id)
2520{
2521 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002522 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002523
Jason Ekstranda85118c2013-06-27 20:17:02 -05002524 resource =
2525 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002526 if (resource == NULL) {
2527 wl_client_post_no_memory(client);
2528 return;
2529 }
2530 wl_resource_set_implementation(resource, &subcompositor_interface,
2531 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002532}
2533
2534static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002535weston_compositor_dpms(struct weston_compositor *compositor,
2536 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002537{
2538 struct weston_output *output;
2539
2540 wl_list_for_each(output, &compositor->output_list, link)
2541 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002542 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002543}
2544
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002545WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002546weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002547{
Neil Roberts8b62e202013-09-30 13:14:47 +01002548 uint32_t old_state = compositor->state;
2549
2550 /* The state needs to be changed before emitting the wake
2551 * signal because that may try to schedule a repaint which
2552 * will not work if the compositor is still sleeping */
2553 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2554
2555 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002556 case WESTON_COMPOSITOR_SLEEPING:
2557 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2558 /* fall through */
2559 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002560 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002561 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002562 /* fall through */
2563 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002564 wl_event_source_timer_update(compositor->idle_source,
2565 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002566 }
2567}
2568
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002569WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002570weston_compositor_offscreen(struct weston_compositor *compositor)
2571{
2572 switch (compositor->state) {
2573 case WESTON_COMPOSITOR_OFFSCREEN:
2574 return;
2575 case WESTON_COMPOSITOR_SLEEPING:
2576 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2577 /* fall through */
2578 default:
2579 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2580 wl_event_source_timer_update(compositor->idle_source, 0);
2581 }
2582}
2583
2584WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002585weston_compositor_sleep(struct weston_compositor *compositor)
2586{
2587 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2588 return;
2589
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002590 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002591 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2592 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2593}
2594
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002595static int
2596idle_handler(void *data)
2597{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002598 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002599
2600 if (compositor->idle_inhibit)
2601 return 1;
2602
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002603 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002604 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002605
2606 return 1;
2607}
2608
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002609WL_EXPORT void
2610weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2611{
2612 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002613 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002614 plane->x = x;
2615 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002616
2617 /* Init the link so that the call to wl_list_remove() when releasing
2618 * the plane without ever stacking doesn't lead to a crash */
2619 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002620}
2621
2622WL_EXPORT void
2623weston_plane_release(struct weston_plane *plane)
2624{
2625 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002626 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002627
2628 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002629}
2630
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002631WL_EXPORT void
2632weston_compositor_stack_plane(struct weston_compositor *ec,
2633 struct weston_plane *plane,
2634 struct weston_plane *above)
2635{
2636 if (above)
2637 wl_list_insert(above->link.prev, &plane->link);
2638 else
2639 wl_list_insert(&ec->plane_list, &plane->link);
2640}
2641
Casey Dahlin9074db52012-04-19 22:50:09 -04002642static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002643{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002644 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002645}
2646
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002647static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002648bind_output(struct wl_client *client,
2649 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002650{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002651 struct weston_output *output = data;
2652 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002653 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002654
Jason Ekstranda85118c2013-06-27 20:17:02 -05002655 resource = wl_resource_create(client, &wl_output_interface,
2656 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002657 if (resource == NULL) {
2658 wl_client_post_no_memory(client);
2659 return;
2660 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002661
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002662 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002663 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002664
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002665 wl_output_send_geometry(resource,
2666 output->x,
2667 output->y,
2668 output->mm_width,
2669 output->mm_height,
2670 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002671 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002672 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002673 if (version >= 2)
2674 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002675 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002676
2677 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002678 wl_output_send_mode(resource,
2679 mode->flags,
2680 mode->width,
2681 mode->height,
2682 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002683 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002684
2685 if (version >= 2)
2686 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002687}
2688
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002689WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002690weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002691{
Richard Hughes64ddde12013-05-01 21:52:10 +01002692 wl_signal_emit(&output->destroy_signal, output);
2693
Richard Hughesafe690c2013-05-02 10:10:04 +01002694 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002695 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002696 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002697 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002698
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002699 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002700}
2701
Scott Moreau1bad5db2012-08-18 01:04:05 -06002702static void
2703weston_output_compute_transform(struct weston_output *output)
2704{
2705 struct weston_matrix transform;
2706 int flip;
2707
2708 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002709 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002710
2711 switch(output->transform) {
2712 case WL_OUTPUT_TRANSFORM_FLIPPED:
2713 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2714 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2715 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002716 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002717 flip = -1;
2718 break;
2719 default:
2720 flip = 1;
2721 break;
2722 }
2723
2724 switch(output->transform) {
2725 case WL_OUTPUT_TRANSFORM_NORMAL:
2726 case WL_OUTPUT_TRANSFORM_FLIPPED:
2727 transform.d[0] = flip;
2728 transform.d[1] = 0;
2729 transform.d[4] = 0;
2730 transform.d[5] = 1;
2731 break;
2732 case WL_OUTPUT_TRANSFORM_90:
2733 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2734 transform.d[0] = 0;
2735 transform.d[1] = -flip;
2736 transform.d[4] = 1;
2737 transform.d[5] = 0;
2738 break;
2739 case WL_OUTPUT_TRANSFORM_180:
2740 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2741 transform.d[0] = -flip;
2742 transform.d[1] = 0;
2743 transform.d[4] = 0;
2744 transform.d[5] = -1;
2745 break;
2746 case WL_OUTPUT_TRANSFORM_270:
2747 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2748 transform.d[0] = 0;
2749 transform.d[1] = flip;
2750 transform.d[4] = -1;
2751 transform.d[5] = 0;
2752 break;
2753 default:
2754 break;
2755 }
2756
2757 weston_matrix_multiply(&output->matrix, &transform);
2758}
2759
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002760WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002761weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002762{
Scott Moreau850ca422012-05-21 15:21:25 -06002763 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002764 struct weston_matrix camera;
2765 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002766
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002767 weston_matrix_init(&output->matrix);
2768 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002769 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2770 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002771
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002772 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002773 2.0 / (output->width + output->border.left + output->border.right),
2774 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2775
2776 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002777
Scott Moreauccbf29d2012-02-22 14:21:41 -07002778 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002779 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002780 weston_matrix_init(&camera);
2781 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002782 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002783 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002784 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002785 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002786 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002787 weston_matrix_multiply(&output->matrix, &modelview);
2788 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002789
Scott Moreauccbf29d2012-02-22 14:21:41 -07002790 output->dirty = 0;
2791}
2792
Scott Moreau1bad5db2012-08-18 01:04:05 -06002793static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002794weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002795{
2796 output->transform = transform;
2797
2798 switch (transform) {
2799 case WL_OUTPUT_TRANSFORM_90:
2800 case WL_OUTPUT_TRANSFORM_270:
2801 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2802 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2803 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02002804 output->width = output->current_mode->height;
2805 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002806 break;
2807 case WL_OUTPUT_TRANSFORM_NORMAL:
2808 case WL_OUTPUT_TRANSFORM_180:
2809 case WL_OUTPUT_TRANSFORM_FLIPPED:
2810 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02002811 output->width = output->current_mode->width;
2812 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002813 break;
2814 default:
2815 break;
2816 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002817
Hardening57388e42013-09-18 23:56:36 +02002818 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002819 output->width /= scale;
2820 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002821}
2822
Scott Moreauccbf29d2012-02-22 14:21:41 -07002823WL_EXPORT void
2824weston_output_move(struct weston_output *output, int x, int y)
2825{
2826 output->x = x;
2827 output->y = y;
2828
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002829 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002830 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002831 output->width,
2832 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002833}
2834
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002835WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002836weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002837 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002838 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002839{
2840 output->compositor = c;
2841 output->x = x;
2842 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002843 output->border.top = 0;
2844 output->border.bottom = 0;
2845 output->border.left = 0;
2846 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002847 output->mm_width = mm_width;
2848 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002849 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02002850 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002851
Alexander Larsson0b135062013-05-28 16:23:36 +02002852 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002853 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002854
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002855 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002856 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002857
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002858 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002859 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002860 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002861 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002862
Casey Dahlin58ba1372012-04-19 22:50:08 -04002863 output->id = ffs(~output->compositor->output_id_pool) - 1;
2864 output->compositor->output_id_pool |= 1 << output->id;
2865
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002866 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002867 wl_global_create(c->wl_display, &wl_output_interface, 2,
2868 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002869 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002870}
2871
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002872WL_EXPORT void
2873weston_output_transform_coordinate(struct weston_output *output,
2874 int device_x, int device_y,
2875 wl_fixed_t *x, wl_fixed_t *y)
2876{
2877 wl_fixed_t tx, ty;
2878 wl_fixed_t width, height;
2879
Hardeningff39efa2013-09-18 23:56:35 +02002880 width = wl_fixed_from_int(output->width * output->current_scale - 1);
2881 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002882
2883 switch(output->transform) {
2884 case WL_OUTPUT_TRANSFORM_NORMAL:
2885 default:
2886 tx = wl_fixed_from_int(device_x);
2887 ty = wl_fixed_from_int(device_y);
2888 break;
2889 case WL_OUTPUT_TRANSFORM_90:
2890 tx = wl_fixed_from_int(device_y);
2891 ty = height - wl_fixed_from_int(device_x);
2892 break;
2893 case WL_OUTPUT_TRANSFORM_180:
2894 tx = width - wl_fixed_from_int(device_x);
2895 ty = height - wl_fixed_from_int(device_y);
2896 break;
2897 case WL_OUTPUT_TRANSFORM_270:
2898 tx = width - wl_fixed_from_int(device_y);
2899 ty = wl_fixed_from_int(device_x);
2900 break;
2901 case WL_OUTPUT_TRANSFORM_FLIPPED:
2902 tx = width - wl_fixed_from_int(device_x);
2903 ty = wl_fixed_from_int(device_y);
2904 break;
2905 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2906 tx = width - wl_fixed_from_int(device_y);
2907 ty = height - wl_fixed_from_int(device_x);
2908 break;
2909 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2910 tx = wl_fixed_from_int(device_x);
2911 ty = height - wl_fixed_from_int(device_y);
2912 break;
2913 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2914 tx = wl_fixed_from_int(device_y);
2915 ty = wl_fixed_from_int(device_x);
2916 break;
2917 }
2918
Hardeningff39efa2013-09-18 23:56:35 +02002919 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
2920 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002921}
2922
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002923static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002924compositor_bind(struct wl_client *client,
2925 void *data, uint32_t version, uint32_t id)
2926{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002927 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002928 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002929
Jason Ekstranda85118c2013-06-27 20:17:02 -05002930 resource = wl_resource_create(client, &wl_compositor_interface,
2931 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002932 if (resource == NULL) {
2933 wl_client_post_no_memory(client);
2934 return;
2935 }
2936
2937 wl_resource_set_implementation(resource, &compositor_interface,
2938 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002939}
2940
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002941static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002942log_uname(void)
2943{
2944 struct utsname usys;
2945
2946 uname(&usys);
2947
2948 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2949 usys.version, usys.machine);
2950}
2951
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002952WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002953weston_environment_get_fd(const char *env)
2954{
2955 char *e, *end;
2956 int fd, flags;
2957
2958 e = getenv(env);
2959 if (!e)
2960 return -1;
2961 fd = strtol(e, &end, 0);
2962 if (*end != '\0')
2963 return -1;
2964
2965 flags = fcntl(fd, F_GETFD);
2966 if (flags == -1)
2967 return -1;
2968
2969 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2970 unsetenv(env);
2971
2972 return fd;
2973}
2974
2975WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002976weston_compositor_init(struct weston_compositor *ec,
2977 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002978 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002979 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002980{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002981 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002982 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002983 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002984
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002985 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002986 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002987 wl_signal_init(&ec->destroy_signal);
2988 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002989 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002990 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002991 wl_signal_init(&ec->idle_signal);
2992 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002993 wl_signal_init(&ec->show_input_panel_signal);
2994 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002995 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002996 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002997 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002998 wl_signal_init(&ec->session_signal);
2999 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003000
Casey Dahlin58ba1372012-04-19 22:50:08 -04003001 ec->output_id_pool = 0;
3002
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003003 if (!wl_global_create(display, &wl_compositor_interface, 3,
3004 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003005 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003006
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003007 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3008 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003009 return -1;
3010
Daniel Stone725c2c32012-06-22 14:04:36 +01003011 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003012 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003013 wl_list_init(&ec->layer_list);
3014 wl_list_init(&ec->seat_list);
3015 wl_list_init(&ec->output_list);
3016 wl_list_init(&ec->key_binding_list);
3017 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003018 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003019 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003020 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003021
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003022 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003023 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003024
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003025 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3026 weston_config_section_get_string(s, "keymap_rules",
3027 (char **) &xkb_names.rules, NULL);
3028 weston_config_section_get_string(s, "keymap_model",
3029 (char **) &xkb_names.model, NULL);
3030 weston_config_section_get_string(s, "keymap_layout",
3031 (char **) &xkb_names.layout, NULL);
3032 weston_config_section_get_string(s, "keymap_variant",
3033 (char **) &xkb_names.variant, NULL);
3034 weston_config_section_get_string(s, "keymap_options",
3035 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003036
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003037 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3038 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003039
3040 ec->ping_handler = NULL;
3041
3042 screenshooter_create(ec);
3043 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003044 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003045
3046 wl_data_device_manager_init(ec->wl_display);
3047
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003048 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003049
Daniel Stone725c2c32012-06-22 14:04:36 +01003050 loop = wl_display_get_event_loop(ec->wl_display);
3051 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3052 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3053
3054 ec->input_loop = wl_event_loop_create();
3055
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003056 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3057 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3058
3059 weston_compositor_schedule_repaint(ec);
3060
Daniel Stone725c2c32012-06-22 14:04:36 +01003061 return 0;
3062}
3063
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003064WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003065weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003066{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003067 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003068
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003069 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003070 if (ec->input_loop_source)
3071 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003072
Matt Roper361d2ad2011-08-29 13:52:23 -07003073 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003074 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003075 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003076
Daniel Stone325fc2d2012-05-30 16:31:58 +01003077 weston_binding_list_destroy_all(&ec->key_binding_list);
3078 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003079 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003080 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003081 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003082
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003083 weston_plane_release(&ec->primary_plane);
3084
Jonas Ådahlc97af922012-03-28 22:36:09 +02003085 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003086
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003087 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003088}
3089
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003090WL_EXPORT void
3091weston_version(int *major, int *minor, int *micro)
3092{
3093 *major = WESTON_VERSION_MAJOR;
3094 *minor = WESTON_VERSION_MINOR;
3095 *micro = WESTON_VERSION_MICRO;
3096}
3097
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003098static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003099 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003100 const char *desc;
3101} capability_strings[] = {
3102 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003103 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003104};
3105
3106static void
3107weston_compositor_log_capabilities(struct weston_compositor *compositor)
3108{
3109 unsigned i;
3110 int yes;
3111
3112 weston_log("Compositor capabilities:\n");
3113 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3114 yes = compositor->capabilities & capability_strings[i].bit;
3115 weston_log_continue(STAMP_SPACE "%s %s\n",
3116 capability_strings[i].desc,
3117 yes ? "yes" : "no");
3118 }
3119}
3120
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003121static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003122{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003123 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003124
Martin Minarik6d118362012-06-07 18:01:59 +02003125 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003126 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003127
3128 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003129}
3130
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003131#ifdef HAVE_LIBUNWIND
3132
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003133static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003134print_backtrace(void)
3135{
3136 unw_cursor_t cursor;
3137 unw_context_t context;
3138 unw_word_t off;
3139 unw_proc_info_t pip;
3140 int ret, i = 0;
3141 char procname[256];
3142 const char *filename;
3143 Dl_info dlinfo;
3144
3145 pip.unwind_info = NULL;
3146 ret = unw_getcontext(&context);
3147 if (ret) {
3148 weston_log("unw_getcontext: %d\n", ret);
3149 return;
3150 }
3151
3152 ret = unw_init_local(&cursor, &context);
3153 if (ret) {
3154 weston_log("unw_init_local: %d\n", ret);
3155 return;
3156 }
3157
3158 ret = unw_step(&cursor);
3159 while (ret > 0) {
3160 ret = unw_get_proc_info(&cursor, &pip);
3161 if (ret) {
3162 weston_log("unw_get_proc_info: %d\n", ret);
3163 break;
3164 }
3165
3166 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3167 if (ret && ret != -UNW_ENOMEM) {
3168 if (ret != -UNW_EUNSPEC)
3169 weston_log("unw_get_proc_name: %d\n", ret);
3170 procname[0] = '?';
3171 procname[1] = 0;
3172 }
3173
3174 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3175 *dlinfo.dli_fname)
3176 filename = dlinfo.dli_fname;
3177 else
3178 filename = "?";
3179
3180 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3181 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3182
3183 ret = unw_step(&cursor);
3184 if (ret < 0)
3185 weston_log("unw_step: %d\n", ret);
3186 }
3187}
3188
3189#else
3190
3191static void
3192print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003193{
3194 void *buffer[32];
3195 int i, count;
3196 Dl_info info;
3197
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003198 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3199 for (i = 0; i < count; i++) {
3200 dladdr(buffer[i], &info);
3201 weston_log(" [%016lx] %s (%s)\n",
3202 (long) buffer[i],
3203 info.dli_sname ? info.dli_sname : "--",
3204 info.dli_fname);
3205 }
3206}
3207
3208#endif
3209
3210static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003211on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003212{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003213 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003214 * then call the backend restore function, which will switch
3215 * back to the vt we launched from or ungrab X etc and then
3216 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003217 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003218 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003219 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003220
Peter Maatmane5b42e42013-03-27 22:38:53 +01003221 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003222
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003223 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003224
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003225 segv_compositor->restore(segv_compositor);
3226
3227 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003228}
3229
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003230WL_EXPORT void *
3231weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003232{
3233 char path[PATH_MAX];
3234 void *module, *init;
3235
3236 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003237 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003238 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003239 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003240
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003241 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3242 if (module) {
3243 weston_log("Module '%s' already loaded\n", path);
3244 dlclose(module);
3245 return NULL;
3246 }
3247
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003248 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003249 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003250 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003251 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003252 return NULL;
3253 }
3254
3255 init = dlsym(module, entrypoint);
3256 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003257 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003258 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003259 return NULL;
3260 }
3261
3262 return init;
3263}
3264
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003265static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003266load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003267 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003268{
3269 const char *p, *end;
3270 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003271 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003272 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003273
3274 if (modules == NULL)
3275 return 0;
3276
3277 p = modules;
3278 while (*p) {
3279 end = strchrnul(p, ',');
3280 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003281 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003282 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003283 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003284 p = end;
3285 while (*p == ',')
3286 p++;
3287
3288 }
3289
3290 return 0;
3291}
3292
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003293static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003294 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3295
3296static const char xdg_wrong_message[] =
3297 "fatal: environment variable XDG_RUNTIME_DIR\n"
3298 "is set to \"%s\", which is not a directory.\n";
3299
3300static const char xdg_wrong_mode_message[] =
3301 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3302 "correctly. Unix access mode must be 0700 but is %o,\n"
3303 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003304 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003305
3306static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003307 "Refer to your distribution on how to get it, or\n"
3308 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3309 "on how to implement it.\n";
3310
Martin Minarik37032f82012-06-18 20:15:18 +02003311static void
3312verify_xdg_runtime_dir(void)
3313{
3314 char *dir = getenv("XDG_RUNTIME_DIR");
3315 struct stat s;
3316
3317 if (!dir) {
3318 weston_log(xdg_error_message);
3319 weston_log_continue(xdg_detail_message);
3320 exit(EXIT_FAILURE);
3321 }
3322
3323 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3324 weston_log(xdg_wrong_message, dir);
3325 weston_log_continue(xdg_detail_message);
3326 exit(EXIT_FAILURE);
3327 }
3328
3329 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3330 weston_log(xdg_wrong_mode_message,
3331 dir, s.st_mode & 0777, s.st_uid);
3332 weston_log_continue(xdg_detail_message);
3333 }
3334}
3335
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003336static int
3337usage(int error_code)
3338{
3339 fprintf(stderr,
3340 "Usage: weston [OPTIONS]\n\n"
3341 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3342 "Weston supports multiple backends, and depending on which backend is in use\n"
3343 "different options will be accepted.\n\n"
3344
3345
3346 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003347 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003348 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003349 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3350 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003351 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003352 " -S, --socket=NAME\tName of socket to listen on\n"
3353 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003354 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003355 " --log==FILE\t\tLog to the given file\n"
3356 " -h, --help\t\tThis help message\n\n");
3357
3358 fprintf(stderr,
3359 "Options for drm-backend.so:\n\n"
3360 " --connector=ID\tBring up only this connector\n"
3361 " --seat=SEAT\t\tThe seat that weston should run on\n"
3362 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003363 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003364 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3365
3366 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003367 "Options for fbdev-backend.so:\n\n"
3368 " --tty=TTY\t\tThe tty to use\n"
3369 " --device=DEVICE\tThe framebuffer device to use\n\n");
3370
3371 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003372 "Options for x11-backend.so:\n\n"
3373 " --width=WIDTH\t\tWidth of X window\n"
3374 " --height=HEIGHT\tHeight of X window\n"
3375 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003376 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003377 " --output-count=COUNT\tCreate multiple outputs\n"
3378 " --no-input\t\tDont create input devices\n\n");
3379
3380 fprintf(stderr,
3381 "Options for wayland-backend.so:\n\n"
3382 " --width=WIDTH\t\tWidth of Wayland surface\n"
3383 " --height=HEIGHT\tHeight of Wayland surface\n"
3384 " --display=DISPLAY\tWayland display to connect to\n\n");
3385
Pekka Paalanene31e0532013-05-22 18:03:07 +03003386#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3387 fprintf(stderr,
3388 "Options for rpi-backend.so:\n\n"
3389 " --tty=TTY\t\tThe tty to use\n"
3390 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3391 " --transform=TR\tThe output transformation, TR is one of:\n"
3392 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3393 "\n");
3394#endif
3395
Hardeningc077a842013-07-08 00:51:35 +02003396#if defined(BUILD_RDP_COMPOSITOR)
3397 fprintf(stderr,
3398 "Options for rdp-backend.so:\n\n"
3399 " --width=WIDTH\t\tWidth of desktop\n"
3400 " --height=HEIGHT\tHeight of desktop\n"
3401 " --extra-modes=MODES\t\n"
3402 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3403 " --address=ADDR\tThe address to bind\n"
3404 " --port=PORT\tThe port to listen on\n"
3405 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3406 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3407 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3408 "\n");
3409#endif
3410
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003411 exit(error_code);
3412}
3413
Peter Maatmane5b42e42013-03-27 22:38:53 +01003414static void
3415catch_signals(void)
3416{
3417 struct sigaction action;
3418
3419 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3420 action.sa_sigaction = on_caught_signal;
3421 sigemptyset(&action.sa_mask);
3422 sigaction(SIGSEGV, &action, NULL);
3423 sigaction(SIGABRT, &action, NULL);
3424}
3425
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003426int main(int argc, char *argv[])
3427{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003428 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003429 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003430 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003431 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003432 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003433 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003434 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003435 int *argc, char *argv[],
3436 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003437 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003438 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003439 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003440 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003441 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003442 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003443 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003444 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003445 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003446 struct weston_config *config;
3447 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003448
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003449 const struct weston_option core_options[] = {
3450 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003451 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003452 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3453 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003454 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003455 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003456 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003457 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003458 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003459
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003460 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003461
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003462 if (help)
3463 usage(EXIT_SUCCESS);
3464
Scott Moreau12245142012-08-29 15:15:58 -06003465 if (version) {
3466 printf(PACKAGE_STRING "\n");
3467 return EXIT_SUCCESS;
3468 }
3469
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003470 weston_log_file_open(log);
3471
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003472 weston_log("%s\n"
3473 STAMP_SPACE "%s\n"
3474 STAMP_SPACE "Bug reports to: %s\n"
3475 STAMP_SPACE "Build: %s\n",
3476 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003477 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003478 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003479
Martin Minarik37032f82012-06-18 20:15:18 +02003480 verify_xdg_runtime_dir();
3481
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003482 display = wl_display_create();
3483
Tiago Vignatti2116b892011-08-08 05:52:59 -07003484 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003485 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3486 display);
3487 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3488 display);
3489 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3490 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003491
3492 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003493 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3494 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003495
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003496 if (!backend) {
3497 if (getenv("WAYLAND_DISPLAY"))
3498 backend = "wayland-backend.so";
3499 else if (getenv("DISPLAY"))
3500 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003501 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003502 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003503 }
3504
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003505 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003506 if (config != NULL) {
3507 weston_log("Using config file '%s'\n",
3508 weston_config_get_full_path(config));
3509 } else {
3510 weston_log("Starting with no config file.\n");
3511 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003512 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003513 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003514
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003515 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003516 if (!backend_init)
3517 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003518
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003519 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003520 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003521 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003522 exit(EXIT_FAILURE);
3523 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003524
Peter Maatmane5b42e42013-03-27 22:38:53 +01003525 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003526 segv_compositor = ec;
3527
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003528 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003529
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003530 setenv("WAYLAND_DISPLAY", socket_name, 1);
3531
Jason Ekstranda985da42013-08-22 17:28:03 -05003532 if (!shell)
3533 weston_config_section_get_string(section, "shell", &shell,
3534 "desktop-shell.so");
3535 if (load_modules(ec, shell, &argc, argv) < 0)
3536 goto out;
3537
Ossama Othmana50e6e42013-05-14 09:48:26 -07003538 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003539 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003540 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003541 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003542
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003543 for (i = 1; i < argc; i++)
3544 weston_log("fatal: unhandled option: %s\n", argv[i]);
3545 if (argc > 1) {
3546 ret = EXIT_FAILURE;
3547 goto out;
3548 }
3549
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003550 weston_compositor_log_capabilities(ec);
3551
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003552 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003553 weston_log("fatal: failed to add socket: %m\n");
3554 ret = EXIT_FAILURE;
3555 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003556 }
3557
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003558 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003559
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003560 wl_display_run(display);
3561
3562 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003563 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003564 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003565
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003566 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003567
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003568 for (i = ARRAY_LENGTH(signals); i;)
3569 wl_event_source_remove(signals[--i]);
3570
Daniel Stone855028f2012-05-01 20:37:10 +01003571 weston_compositor_xkb_destroy(ec);
3572
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003573 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003574 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003575
Martin Minarik19e6f262012-06-07 13:08:46 +02003576 weston_log_file_close();
3577
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003578 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003579}