blob: 59a5abd12600c034a149af3d586f62b3318d8050 [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
David Herrmann1edf44c2013-10-22 17:11:26 +02001368static int
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;
David Herrmann1edf44c2013-10-22 17:11:26 +02001377 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001378
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001379 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001380 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001381
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001382 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001383 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001384 else
1385 wl_list_for_each(es, &ec->surface_list, link)
1386 weston_surface_move_to_plane(es, &ec->primary_plane);
1387
Pekka Paalanene67858b2013-04-25 13:57:42 +03001388 wl_list_init(&frame_callback_list);
1389 wl_list_for_each(es, &ec->surface_list, link) {
1390 if (es->output == output) {
1391 wl_list_insert_list(&frame_callback_list,
1392 &es->frame_callback_list);
1393 wl_list_init(&es->frame_callback_list);
1394 }
1395 }
1396
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001397 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001398
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001399 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001400 pixman_region32_intersect(&output_damage,
1401 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001402 pixman_region32_subtract(&output_damage,
1403 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001404
Scott Moreauccbf29d2012-02-22 14:21:41 -07001405 if (output->dirty)
1406 weston_output_update_matrix(output);
1407
David Herrmann1edf44c2013-10-22 17:11:26 +02001408 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001409
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001410 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001411
Kristian Høgsbergef044142011-06-21 15:02:12 -04001412 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001413
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001414 weston_compositor_repick(ec);
1415 wl_event_loop_dispatch(ec->input_loop, 0);
1416
Jonas Ådahldb773762012-06-13 00:01:21 +02001417 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001418 wl_callback_send_done(cb->resource, msecs);
1419 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001420 }
1421
Scott Moreaud64cf212012-06-08 19:40:54 -06001422 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001423 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001424 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001425 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001426
1427 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001428}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001429
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001430static int
1431weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001432{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001433 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001434
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001435 wl_event_loop_dispatch(compositor->input_loop, 0);
1436
1437 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001438}
1439
1440WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001441weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001442{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001443 struct weston_compositor *compositor = output->compositor;
1444 struct wl_event_loop *loop =
1445 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001446 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001447
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001448 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001449
1450 if (output->repaint_needed &&
1451 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1452 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001453 r = weston_output_repaint(output, msecs);
1454 if (!r)
1455 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001456 }
1457
1458 output->repaint_scheduled = 0;
1459 if (compositor->input_loop_source)
1460 return;
1461
1462 fd = wl_event_loop_get_fd(compositor->input_loop);
1463 compositor->input_loop_source =
1464 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1465 weston_compositor_read_input, compositor);
1466}
1467
1468static void
1469idle_repaint(void *data)
1470{
1471 struct weston_output *output = data;
1472
Jonas Ådahle5a12252013-04-05 23:07:11 +02001473 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001474}
1475
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001476WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001477weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1478{
1479 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001480 if (below != NULL)
1481 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001482}
1483
1484WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001485weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001486{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001487 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001488 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001489
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001490 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1491 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001492 return;
1493
Kristian Høgsbergef044142011-06-21 15:02:12 -04001494 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001495 output->repaint_needed = 1;
1496 if (output->repaint_scheduled)
1497 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001498
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001499 wl_event_loop_add_idle(loop, idle_repaint, output);
1500 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001501
1502 if (compositor->input_loop_source) {
1503 wl_event_source_remove(compositor->input_loop_source);
1504 compositor->input_loop_source = NULL;
1505 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001506}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001507
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001508WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001509weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1510{
1511 struct weston_output *output;
1512
1513 wl_list_for_each(output, &compositor->output_list, link)
1514 weston_output_schedule_repaint(output);
1515}
1516
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001517static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001518surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001519{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001520 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001521}
1522
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001523static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001524surface_attach(struct wl_client *client,
1525 struct wl_resource *resource,
1526 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1527{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001528 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001529 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001530
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001531 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001532 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001533 if (buffer == NULL) {
1534 wl_client_post_no_memory(client);
1535 return;
1536 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001537 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001538
Pekka Paalanende685b82012-12-04 15:58:12 +02001539 /* Attach, attach, without commit in between does not send
1540 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001541 if (surface->pending.buffer)
1542 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001543
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001544 surface->pending.sx = sx;
1545 surface->pending.sy = sy;
1546 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001547 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001548 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001549 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001550 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001551 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001552}
1553
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001554static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001555surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001556 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001557 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001558{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001559 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001560
Pekka Paalanen8e159182012-10-10 12:49:25 +03001561 pixman_region32_union_rect(&surface->pending.damage,
1562 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001563 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001564}
1565
Kristian Høgsberg33418202011-08-16 23:01:28 -04001566static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001567destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001568{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001569 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001570
1571 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001572 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001573}
1574
1575static void
1576surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001577 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001578{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001579 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001580 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001581
1582 cb = malloc(sizeof *cb);
1583 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001584 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001585 return;
1586 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001587
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001588 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1589 callback);
1590 if (cb->resource == NULL) {
1591 free(cb);
1592 wl_resource_post_no_memory(resource);
1593 return;
1594 }
1595
Jason Ekstranda85118c2013-06-27 20:17:02 -05001596 wl_resource_set_implementation(cb->resource, NULL, cb,
1597 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001598
Pekka Paalanenbc106382012-10-10 12:49:31 +03001599 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001600}
1601
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001602static void
1603surface_set_opaque_region(struct wl_client *client,
1604 struct wl_resource *resource,
1605 struct wl_resource *region_resource)
1606{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001607 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001608 struct weston_region *region;
1609
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001610 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001611 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001612 pixman_region32_copy(&surface->pending.opaque,
1613 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001614 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001615 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001616 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001617}
1618
1619static void
1620surface_set_input_region(struct wl_client *client,
1621 struct wl_resource *resource,
1622 struct wl_resource *region_resource)
1623{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001624 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001625 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001626
1627 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001628 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001629 pixman_region32_copy(&surface->pending.input,
1630 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001631 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001632 pixman_region32_fini(&surface->pending.input);
1633 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001634 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001635}
1636
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001637static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001638weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001639{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001640 struct weston_subsurface *sub;
1641
1642 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1643 parent_link_pending) {
1644 wl_list_remove(&sub->parent_link);
1645 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1646 }
1647}
1648
1649static void
1650weston_surface_commit(struct weston_surface *surface)
1651{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001652 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001653 int surface_width = 0;
1654 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001655
Alexander Larsson4ea95522013-05-22 14:41:37 +02001656 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001657 surface->buffer_transform = surface->pending.buffer_transform;
1658
Alexander Larsson4ea95522013-05-22 14:41:37 +02001659 /* wl_surface.set_buffer_scale */
1660 surface->buffer_scale = surface->pending.buffer_scale;
1661
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001662 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001663 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001664 weston_surface_attach(surface, surface->pending.buffer);
1665
Giulio Camuffo184df502013-02-21 11:29:21 +01001666 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001667 surface_width = weston_surface_buffer_width(surface);
1668 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001669 }
1670
1671 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001672 surface->configure(surface,
1673 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001674 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001675
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001676 if (surface->pending.buffer)
1677 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1678 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001679 surface->pending.sx = 0;
1680 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001681 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001682
1683 /* wl_surface.damage */
1684 pixman_region32_union(&surface->damage, &surface->damage,
1685 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001686 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1687 0, 0,
1688 surface->geometry.width,
1689 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001690 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001691
1692 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001693 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001694 surface->geometry.width,
1695 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001696 pixman_region32_intersect(&opaque,
1697 &opaque, &surface->pending.opaque);
1698
1699 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1700 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001701 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001702 }
1703
1704 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001705
1706 /* wl_surface.set_input_region */
1707 pixman_region32_fini(&surface->input);
1708 pixman_region32_init_rect(&surface->input, 0, 0,
1709 surface->geometry.width,
1710 surface->geometry.height);
1711 pixman_region32_intersect(&surface->input,
1712 &surface->input, &surface->pending.input);
1713
Pekka Paalanenbc106382012-10-10 12:49:31 +03001714 /* wl_surface.frame */
1715 wl_list_insert_list(&surface->frame_callback_list,
1716 &surface->pending.frame_callback_list);
1717 wl_list_init(&surface->pending.frame_callback_list);
1718
Pekka Paalanene67858b2013-04-25 13:57:42 +03001719 weston_surface_commit_subsurface_order(surface);
1720
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001721 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001722}
1723
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001724static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001725weston_subsurface_commit(struct weston_subsurface *sub);
1726
1727static void
1728weston_subsurface_parent_commit(struct weston_subsurface *sub,
1729 int parent_is_synchronized);
1730
1731static void
1732surface_commit(struct wl_client *client, struct wl_resource *resource)
1733{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001734 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001735 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1736
1737 if (sub) {
1738 weston_subsurface_commit(sub);
1739 return;
1740 }
1741
1742 weston_surface_commit(surface);
1743
1744 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1745 if (sub->surface != surface)
1746 weston_subsurface_parent_commit(sub, 0);
1747 }
1748}
1749
1750static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001751surface_set_buffer_transform(struct wl_client *client,
1752 struct wl_resource *resource, int transform)
1753{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001754 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001755
1756 surface->pending.buffer_transform = transform;
1757}
1758
Alexander Larsson4ea95522013-05-22 14:41:37 +02001759static void
1760surface_set_buffer_scale(struct wl_client *client,
1761 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001762 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001763{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001764 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001765
1766 surface->pending.buffer_scale = scale;
1767}
1768
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001769static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001770 surface_destroy,
1771 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001772 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001773 surface_frame,
1774 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001775 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001776 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001777 surface_set_buffer_transform,
1778 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001779};
1780
1781static void
1782compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001783 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001784{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001785 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001786 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001787
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001788 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001789 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001790 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001791 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001792 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001793
Jason Ekstranda85118c2013-06-27 20:17:02 -05001794 surface->resource =
1795 wl_resource_create(client, &wl_surface_interface,
1796 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001797 if (surface->resource == NULL) {
1798 weston_surface_destroy(surface);
1799 wl_resource_post_no_memory(resource);
1800 return;
1801 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001802 wl_resource_set_implementation(surface->resource, &surface_interface,
1803 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001804}
1805
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001806static void
1807destroy_region(struct wl_resource *resource)
1808{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001809 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001810
1811 pixman_region32_fini(&region->region);
1812 free(region);
1813}
1814
1815static void
1816region_destroy(struct wl_client *client, struct wl_resource *resource)
1817{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001818 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001819}
1820
1821static void
1822region_add(struct wl_client *client, struct wl_resource *resource,
1823 int32_t x, int32_t y, int32_t width, int32_t height)
1824{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001825 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001826
1827 pixman_region32_union_rect(&region->region, &region->region,
1828 x, y, width, height);
1829}
1830
1831static void
1832region_subtract(struct wl_client *client, struct wl_resource *resource,
1833 int32_t x, int32_t y, int32_t width, int32_t height)
1834{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001835 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001836 pixman_region32_t rect;
1837
1838 pixman_region32_init_rect(&rect, x, y, width, height);
1839 pixman_region32_subtract(&region->region, &region->region, &rect);
1840 pixman_region32_fini(&rect);
1841}
1842
1843static const struct wl_region_interface region_interface = {
1844 region_destroy,
1845 region_add,
1846 region_subtract
1847};
1848
1849static void
1850compositor_create_region(struct wl_client *client,
1851 struct wl_resource *resource, uint32_t id)
1852{
1853 struct weston_region *region;
1854
1855 region = malloc(sizeof *region);
1856 if (region == NULL) {
1857 wl_resource_post_no_memory(resource);
1858 return;
1859 }
1860
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001861 pixman_region32_init(&region->region);
1862
Jason Ekstranda85118c2013-06-27 20:17:02 -05001863 region->resource =
1864 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001865 if (region->resource == NULL) {
1866 free(region);
1867 wl_resource_post_no_memory(resource);
1868 return;
1869 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001870 wl_resource_set_implementation(region->resource, &region_interface,
1871 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001872}
1873
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001874static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001875 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001876 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001877};
1878
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001879static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001880weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1881{
1882 struct weston_surface *surface = sub->surface;
1883 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001884 int surface_width = 0;
1885 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001886
Alexander Larsson4ea95522013-05-22 14:41:37 +02001887 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001888 surface->buffer_transform = sub->cached.buffer_transform;
1889
Alexander Larsson4ea95522013-05-22 14:41:37 +02001890 /* wl_surface.set_buffer_scale */
1891 surface->buffer_scale = sub->cached.buffer_scale;
1892
Pekka Paalanene67858b2013-04-25 13:57:42 +03001893 /* wl_surface.attach */
1894 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1895 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1896 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1897
1898 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001899 surface_width = weston_surface_buffer_width(surface);
1900 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001901 }
1902
1903 if (surface->configure && sub->cached.newly_attached)
1904 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001905 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001906 sub->cached.sx = 0;
1907 sub->cached.sy = 0;
1908 sub->cached.newly_attached = 0;
1909
1910 /* wl_surface.damage */
1911 pixman_region32_union(&surface->damage, &surface->damage,
1912 &sub->cached.damage);
1913 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1914 0, 0,
1915 surface->geometry.width,
1916 surface->geometry.height);
1917 empty_region(&sub->cached.damage);
1918
1919 /* wl_surface.set_opaque_region */
1920 pixman_region32_init_rect(&opaque, 0, 0,
1921 surface->geometry.width,
1922 surface->geometry.height);
1923 pixman_region32_intersect(&opaque,
1924 &opaque, &sub->cached.opaque);
1925
1926 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1927 pixman_region32_copy(&surface->opaque, &opaque);
1928 weston_surface_geometry_dirty(surface);
1929 }
1930
1931 pixman_region32_fini(&opaque);
1932
1933 /* wl_surface.set_input_region */
1934 pixman_region32_fini(&surface->input);
1935 pixman_region32_init_rect(&surface->input, 0, 0,
1936 surface->geometry.width,
1937 surface->geometry.height);
1938 pixman_region32_intersect(&surface->input,
1939 &surface->input, &sub->cached.input);
1940
1941 /* wl_surface.frame */
1942 wl_list_insert_list(&surface->frame_callback_list,
1943 &sub->cached.frame_callback_list);
1944 wl_list_init(&sub->cached.frame_callback_list);
1945
1946 weston_surface_commit_subsurface_order(surface);
1947
1948 weston_surface_schedule_repaint(surface);
1949
1950 sub->cached.has_data = 0;
1951}
1952
1953static void
1954weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1955{
1956 struct weston_surface *surface = sub->surface;
1957
1958 /*
1959 * If this commit would cause the surface to move by the
1960 * attach(dx, dy) parameters, the old damage region must be
1961 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02001962 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03001963 */
1964 pixman_region32_translate(&sub->cached.damage,
1965 -surface->pending.sx, -surface->pending.sy);
1966 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1967 &surface->pending.damage);
1968 empty_region(&surface->pending.damage);
1969
1970 if (surface->pending.newly_attached) {
1971 sub->cached.newly_attached = 1;
1972 weston_buffer_reference(&sub->cached.buffer_ref,
1973 surface->pending.buffer);
1974 }
1975 sub->cached.sx += surface->pending.sx;
1976 sub->cached.sy += surface->pending.sy;
1977 surface->pending.sx = 0;
1978 surface->pending.sy = 0;
1979 surface->pending.newly_attached = 0;
1980
1981 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001982 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001983
1984 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1985
1986 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1987
1988 wl_list_insert_list(&sub->cached.frame_callback_list,
1989 &surface->pending.frame_callback_list);
1990 wl_list_init(&surface->pending.frame_callback_list);
1991
1992 sub->cached.has_data = 1;
1993}
1994
1995static int
1996weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1997{
1998 while (sub) {
1999 if (sub->synchronized)
2000 return 1;
2001
2002 if (!sub->parent)
2003 return 0;
2004
2005 sub = weston_surface_to_subsurface(sub->parent);
2006 }
2007
2008 return 0;
2009}
2010
2011static void
2012weston_subsurface_commit(struct weston_subsurface *sub)
2013{
2014 struct weston_surface *surface = sub->surface;
2015 struct weston_subsurface *tmp;
2016
2017 /* Recursive check for effectively synchronized. */
2018 if (weston_subsurface_is_synchronized(sub)) {
2019 weston_subsurface_commit_to_cache(sub);
2020 } else {
2021 if (sub->cached.has_data) {
2022 /* flush accumulated state from cache */
2023 weston_subsurface_commit_to_cache(sub);
2024 weston_subsurface_commit_from_cache(sub);
2025 } else {
2026 weston_surface_commit(surface);
2027 }
2028
2029 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2030 if (tmp->surface != surface)
2031 weston_subsurface_parent_commit(tmp, 0);
2032 }
2033 }
2034}
2035
2036static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002037weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002038{
2039 struct weston_surface *surface = sub->surface;
2040 struct weston_subsurface *tmp;
2041
Pekka Paalanene67858b2013-04-25 13:57:42 +03002042 /* From now on, commit_from_cache the whole sub-tree, regardless of
2043 * the synchronized mode of each child. This sub-surface or some
2044 * of its ancestors were synchronized, so we are synchronized
2045 * all the way down.
2046 */
2047
2048 if (sub->cached.has_data)
2049 weston_subsurface_commit_from_cache(sub);
2050
2051 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2052 if (tmp->surface != surface)
2053 weston_subsurface_parent_commit(tmp, 1);
2054 }
2055}
2056
2057static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002058weston_subsurface_parent_commit(struct weston_subsurface *sub,
2059 int parent_is_synchronized)
2060{
2061 if (sub->position.set) {
2062 weston_surface_set_position(sub->surface,
2063 sub->position.x, sub->position.y);
2064 sub->position.set = 0;
2065 }
2066
2067 if (parent_is_synchronized || sub->synchronized)
2068 weston_subsurface_synchronized_commit(sub);
2069}
2070
2071static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002072subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2073 int32_t width, int32_t height)
2074{
2075 struct weston_compositor *compositor = surface->compositor;
2076
2077 weston_surface_configure(surface,
2078 surface->geometry.x + dx,
2079 surface->geometry.y + dy,
2080 width, height);
2081
2082 /* No need to check parent mappedness, because if parent is not
2083 * mapped, parent is not in a visible layer, so this sub-surface
2084 * will not be drawn either.
2085 */
2086 if (!weston_surface_is_mapped(surface)) {
2087 wl_list_init(&surface->layer_link);
2088
2089 /* Cannot call weston_surface_update_transform(),
2090 * because that would call it also for the parent surface,
2091 * which might not be mapped yet. That would lead to
2092 * inconsistent state, where the window could never be
2093 * mapped.
2094 *
2095 * Instead just assing any output, to make
2096 * weston_surface_is_mapped() return true, so that when the
2097 * parent surface does get mapped, this one will get
2098 * included, too. See surface_list_add().
2099 */
2100 assert(!wl_list_empty(&compositor->output_list));
2101 surface->output = container_of(compositor->output_list.next,
2102 struct weston_output, link);
2103 }
2104}
2105
2106static struct weston_subsurface *
2107weston_surface_to_subsurface(struct weston_surface *surface)
2108{
2109 if (surface->configure == subsurface_configure)
2110 return surface->configure_private;
2111
2112 return NULL;
2113}
2114
Pekka Paalanen01388e22013-04-25 13:57:44 +03002115WL_EXPORT struct weston_surface *
2116weston_surface_get_main_surface(struct weston_surface *surface)
2117{
2118 struct weston_subsurface *sub;
2119
2120 while (surface && (sub = weston_surface_to_subsurface(surface)))
2121 surface = sub->parent;
2122
2123 return surface;
2124}
2125
Pekka Paalanene67858b2013-04-25 13:57:42 +03002126static void
2127subsurface_set_position(struct wl_client *client,
2128 struct wl_resource *resource, int32_t x, int32_t y)
2129{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002130 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002131
2132 if (!sub)
2133 return;
2134
2135 sub->position.x = x;
2136 sub->position.y = y;
2137 sub->position.set = 1;
2138}
2139
2140static struct weston_subsurface *
2141subsurface_from_surface(struct weston_surface *surface)
2142{
2143 struct weston_subsurface *sub;
2144
2145 sub = weston_surface_to_subsurface(surface);
2146 if (sub)
2147 return sub;
2148
2149 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2150 if (sub->surface == surface)
2151 return sub;
2152
2153 return NULL;
2154}
2155
2156static struct weston_subsurface *
2157subsurface_sibling_check(struct weston_subsurface *sub,
2158 struct weston_surface *surface,
2159 const char *request)
2160{
2161 struct weston_subsurface *sibling;
2162
2163 sibling = subsurface_from_surface(surface);
2164
2165 if (!sibling) {
2166 wl_resource_post_error(sub->resource,
2167 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2168 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002169 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002170 return NULL;
2171 }
2172
2173 if (sibling->parent != sub->parent) {
2174 wl_resource_post_error(sub->resource,
2175 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2176 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002177 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002178 return NULL;
2179 }
2180
2181 return sibling;
2182}
2183
2184static void
2185subsurface_place_above(struct wl_client *client,
2186 struct wl_resource *resource,
2187 struct wl_resource *sibling_resource)
2188{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002189 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002190 struct weston_surface *surface =
2191 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002192 struct weston_subsurface *sibling;
2193
2194 if (!sub)
2195 return;
2196
2197 sibling = subsurface_sibling_check(sub, surface, "place_above");
2198 if (!sibling)
2199 return;
2200
2201 wl_list_remove(&sub->parent_link_pending);
2202 wl_list_insert(sibling->parent_link_pending.prev,
2203 &sub->parent_link_pending);
2204}
2205
2206static void
2207subsurface_place_below(struct wl_client *client,
2208 struct wl_resource *resource,
2209 struct wl_resource *sibling_resource)
2210{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002211 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002212 struct weston_surface *surface =
2213 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002214 struct weston_subsurface *sibling;
2215
2216 if (!sub)
2217 return;
2218
2219 sibling = subsurface_sibling_check(sub, surface, "place_below");
2220 if (!sibling)
2221 return;
2222
2223 wl_list_remove(&sub->parent_link_pending);
2224 wl_list_insert(&sibling->parent_link_pending,
2225 &sub->parent_link_pending);
2226}
2227
2228static void
2229subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2230{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002231 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002232
2233 if (sub)
2234 sub->synchronized = 1;
2235}
2236
2237static void
2238subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2239{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002240 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002241
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002242 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002243 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002244
2245 /* If sub became effectively desynchronized, flush. */
2246 if (!weston_subsurface_is_synchronized(sub))
2247 weston_subsurface_synchronized_commit(sub);
2248 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002249}
2250
2251static void
2252weston_subsurface_cache_init(struct weston_subsurface *sub)
2253{
2254 pixman_region32_init(&sub->cached.damage);
2255 pixman_region32_init(&sub->cached.opaque);
2256 pixman_region32_init(&sub->cached.input);
2257 wl_list_init(&sub->cached.frame_callback_list);
2258 sub->cached.buffer_ref.buffer = NULL;
2259}
2260
2261static void
2262weston_subsurface_cache_fini(struct weston_subsurface *sub)
2263{
2264 struct weston_frame_callback *cb, *tmp;
2265
2266 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002267 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002268
2269 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2270 pixman_region32_fini(&sub->cached.damage);
2271 pixman_region32_fini(&sub->cached.opaque);
2272 pixman_region32_fini(&sub->cached.input);
2273}
2274
2275static void
2276weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2277{
2278 wl_list_remove(&sub->parent_link);
2279 wl_list_remove(&sub->parent_link_pending);
2280 wl_list_remove(&sub->parent_destroy_listener.link);
2281 sub->parent = NULL;
2282}
2283
2284static void
2285weston_subsurface_destroy(struct weston_subsurface *sub);
2286
2287static void
2288subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2289{
2290 struct weston_subsurface *sub =
2291 container_of(listener, struct weston_subsurface,
2292 surface_destroy_listener);
2293 assert(data == &sub->surface->resource);
2294
2295 /* The protocol object (wl_resource) is left inert. */
2296 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002297 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298
2299 weston_subsurface_destroy(sub);
2300}
2301
2302static void
2303subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2304{
2305 struct weston_subsurface *sub =
2306 container_of(listener, struct weston_subsurface,
2307 parent_destroy_listener);
2308 assert(data == &sub->parent->resource);
2309 assert(sub->surface != sub->parent);
2310
2311 if (weston_surface_is_mapped(sub->surface))
2312 weston_surface_unmap(sub->surface);
2313
2314 weston_subsurface_unlink_parent(sub);
2315}
2316
2317static void
2318subsurface_resource_destroy(struct wl_resource *resource)
2319{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002320 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002321
2322 if (sub)
2323 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002324}
2325
2326static void
2327subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2328{
2329 wl_resource_destroy(resource);
2330}
2331
2332static void
2333weston_subsurface_link_parent(struct weston_subsurface *sub,
2334 struct weston_surface *parent)
2335{
2336 sub->parent = parent;
2337 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002338 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002339 &sub->parent_destroy_listener);
2340
2341 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2342 wl_list_insert(&parent->subsurface_list_pending,
2343 &sub->parent_link_pending);
2344}
2345
2346static void
2347weston_subsurface_link_surface(struct weston_subsurface *sub,
2348 struct weston_surface *surface)
2349{
2350 sub->surface = surface;
2351 sub->surface_destroy_listener.notify =
2352 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002353 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002354 &sub->surface_destroy_listener);
2355}
2356
2357static void
2358weston_subsurface_destroy(struct weston_subsurface *sub)
2359{
2360 assert(sub->surface);
2361
2362 if (sub->resource) {
2363 assert(weston_surface_to_subsurface(sub->surface) == sub);
2364 assert(sub->parent_destroy_listener.notify ==
2365 subsurface_handle_parent_destroy);
2366
2367 weston_surface_set_transform_parent(sub->surface, NULL);
2368 if (sub->parent)
2369 weston_subsurface_unlink_parent(sub);
2370
2371 weston_subsurface_cache_fini(sub);
2372
2373 sub->surface->configure = NULL;
2374 sub->surface->configure_private = NULL;
2375 } else {
2376 /* the dummy weston_subsurface for the parent itself */
2377 assert(sub->parent_destroy_listener.notify == NULL);
2378 wl_list_remove(&sub->parent_link);
2379 wl_list_remove(&sub->parent_link_pending);
2380 }
2381
2382 wl_list_remove(&sub->surface_destroy_listener.link);
2383 free(sub);
2384}
2385
2386static const struct wl_subsurface_interface subsurface_implementation = {
2387 subsurface_destroy,
2388 subsurface_set_position,
2389 subsurface_place_above,
2390 subsurface_place_below,
2391 subsurface_set_sync,
2392 subsurface_set_desync
2393};
2394
2395static struct weston_subsurface *
2396weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2397 struct weston_surface *parent)
2398{
2399 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002400 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002401
2402 sub = calloc(1, sizeof *sub);
2403 if (!sub)
2404 return NULL;
2405
Jason Ekstranda85118c2013-06-27 20:17:02 -05002406 sub->resource =
2407 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002408 if (!sub->resource) {
2409 free(sub);
2410 return NULL;
2411 }
2412
Jason Ekstranda85118c2013-06-27 20:17:02 -05002413 wl_resource_set_implementation(sub->resource,
2414 &subsurface_implementation,
2415 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002416 weston_subsurface_link_surface(sub, surface);
2417 weston_subsurface_link_parent(sub, parent);
2418 weston_subsurface_cache_init(sub);
2419 sub->synchronized = 1;
2420 weston_surface_set_transform_parent(surface, parent);
2421
2422 return sub;
2423}
2424
2425/* Create a dummy subsurface for having the parent itself in its
2426 * sub-surface lists. Makes stacking order manipulation easy.
2427 */
2428static struct weston_subsurface *
2429weston_subsurface_create_for_parent(struct weston_surface *parent)
2430{
2431 struct weston_subsurface *sub;
2432
2433 sub = calloc(1, sizeof *sub);
2434 if (!sub)
2435 return NULL;
2436
2437 weston_subsurface_link_surface(sub, parent);
2438 sub->parent = parent;
2439 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2440 wl_list_insert(&parent->subsurface_list_pending,
2441 &sub->parent_link_pending);
2442
2443 return sub;
2444}
2445
2446static void
2447subcompositor_get_subsurface(struct wl_client *client,
2448 struct wl_resource *resource,
2449 uint32_t id,
2450 struct wl_resource *surface_resource,
2451 struct wl_resource *parent_resource)
2452{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002453 struct weston_surface *surface =
2454 wl_resource_get_user_data(surface_resource);
2455 struct weston_surface *parent =
2456 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002457 struct weston_subsurface *sub;
2458 static const char where[] = "get_subsurface: wl_subsurface@";
2459
2460 if (surface == parent) {
2461 wl_resource_post_error(resource,
2462 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2463 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002464 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002465 return;
2466 }
2467
2468 if (weston_surface_to_subsurface(surface)) {
2469 wl_resource_post_error(resource,
2470 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2471 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002472 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473 return;
2474 }
2475
2476 if (surface->configure) {
2477 wl_resource_post_error(resource,
2478 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2479 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002480 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002481 return;
2482 }
2483
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002484 if (weston_surface_get_main_surface(parent) == surface) {
2485 wl_resource_post_error(resource,
2486 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2487 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002488 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002489 return;
2490 }
2491
Pekka Paalanene67858b2013-04-25 13:57:42 +03002492 /* make sure the parent is in its own list */
2493 if (wl_list_empty(&parent->subsurface_list)) {
2494 if (!weston_subsurface_create_for_parent(parent)) {
2495 wl_resource_post_no_memory(resource);
2496 return;
2497 }
2498 }
2499
2500 sub = weston_subsurface_create(id, surface, parent);
2501 if (!sub) {
2502 wl_resource_post_no_memory(resource);
2503 return;
2504 }
2505
2506 surface->configure = subsurface_configure;
2507 surface->configure_private = sub;
2508}
2509
2510static void
2511subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2512{
2513 wl_resource_destroy(resource);
2514}
2515
2516static const struct wl_subcompositor_interface subcompositor_interface = {
2517 subcompositor_destroy,
2518 subcompositor_get_subsurface
2519};
2520
2521static void
2522bind_subcompositor(struct wl_client *client,
2523 void *data, uint32_t version, uint32_t id)
2524{
2525 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002526 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002527
Jason Ekstranda85118c2013-06-27 20:17:02 -05002528 resource =
2529 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002530 if (resource == NULL) {
2531 wl_client_post_no_memory(client);
2532 return;
2533 }
2534 wl_resource_set_implementation(resource, &subcompositor_interface,
2535 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002536}
2537
2538static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002539weston_compositor_dpms(struct weston_compositor *compositor,
2540 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002541{
2542 struct weston_output *output;
2543
2544 wl_list_for_each(output, &compositor->output_list, link)
2545 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002546 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002547}
2548
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002549WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002550weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002551{
Neil Roberts8b62e202013-09-30 13:14:47 +01002552 uint32_t old_state = compositor->state;
2553
2554 /* The state needs to be changed before emitting the wake
2555 * signal because that may try to schedule a repaint which
2556 * will not work if the compositor is still sleeping */
2557 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2558
2559 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002560 case WESTON_COMPOSITOR_SLEEPING:
2561 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2562 /* fall through */
2563 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002564 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002565 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002566 /* fall through */
2567 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002568 wl_event_source_timer_update(compositor->idle_source,
2569 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002570 }
2571}
2572
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002573WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002574weston_compositor_offscreen(struct weston_compositor *compositor)
2575{
2576 switch (compositor->state) {
2577 case WESTON_COMPOSITOR_OFFSCREEN:
2578 return;
2579 case WESTON_COMPOSITOR_SLEEPING:
2580 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2581 /* fall through */
2582 default:
2583 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2584 wl_event_source_timer_update(compositor->idle_source, 0);
2585 }
2586}
2587
2588WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002589weston_compositor_sleep(struct weston_compositor *compositor)
2590{
2591 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2592 return;
2593
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002594 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002595 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2596 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2597}
2598
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002599static int
2600idle_handler(void *data)
2601{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002602 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002603
2604 if (compositor->idle_inhibit)
2605 return 1;
2606
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002607 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002608 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002609
2610 return 1;
2611}
2612
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002613WL_EXPORT void
2614weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2615{
2616 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002617 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002618 plane->x = x;
2619 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002620
2621 /* Init the link so that the call to wl_list_remove() when releasing
2622 * the plane without ever stacking doesn't lead to a crash */
2623 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002624}
2625
2626WL_EXPORT void
2627weston_plane_release(struct weston_plane *plane)
2628{
2629 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002630 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002631
2632 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002633}
2634
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002635WL_EXPORT void
2636weston_compositor_stack_plane(struct weston_compositor *ec,
2637 struct weston_plane *plane,
2638 struct weston_plane *above)
2639{
2640 if (above)
2641 wl_list_insert(above->link.prev, &plane->link);
2642 else
2643 wl_list_insert(&ec->plane_list, &plane->link);
2644}
2645
Casey Dahlin9074db52012-04-19 22:50:09 -04002646static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002647{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002648 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002649}
2650
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002651static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002652bind_output(struct wl_client *client,
2653 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002654{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002655 struct weston_output *output = data;
2656 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002657 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002658
Jason Ekstranda85118c2013-06-27 20:17:02 -05002659 resource = wl_resource_create(client, &wl_output_interface,
2660 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002661 if (resource == NULL) {
2662 wl_client_post_no_memory(client);
2663 return;
2664 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002665
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002666 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002667 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002668
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002669 wl_output_send_geometry(resource,
2670 output->x,
2671 output->y,
2672 output->mm_width,
2673 output->mm_height,
2674 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002675 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002676 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002677 if (version >= 2)
2678 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002679 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002680
2681 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002682 wl_output_send_mode(resource,
2683 mode->flags,
2684 mode->width,
2685 mode->height,
2686 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002687 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002688
2689 if (version >= 2)
2690 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002691}
2692
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002693WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002694weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002695{
Richard Hughes64ddde12013-05-01 21:52:10 +01002696 wl_signal_emit(&output->destroy_signal, output);
2697
Richard Hughesafe690c2013-05-02 10:10:04 +01002698 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002699 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002700 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002701 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002702
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002703 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002704}
2705
Scott Moreau1bad5db2012-08-18 01:04:05 -06002706static void
2707weston_output_compute_transform(struct weston_output *output)
2708{
2709 struct weston_matrix transform;
2710 int flip;
2711
2712 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002713 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002714
2715 switch(output->transform) {
2716 case WL_OUTPUT_TRANSFORM_FLIPPED:
2717 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2718 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2719 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002720 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002721 flip = -1;
2722 break;
2723 default:
2724 flip = 1;
2725 break;
2726 }
2727
2728 switch(output->transform) {
2729 case WL_OUTPUT_TRANSFORM_NORMAL:
2730 case WL_OUTPUT_TRANSFORM_FLIPPED:
2731 transform.d[0] = flip;
2732 transform.d[1] = 0;
2733 transform.d[4] = 0;
2734 transform.d[5] = 1;
2735 break;
2736 case WL_OUTPUT_TRANSFORM_90:
2737 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2738 transform.d[0] = 0;
2739 transform.d[1] = -flip;
2740 transform.d[4] = 1;
2741 transform.d[5] = 0;
2742 break;
2743 case WL_OUTPUT_TRANSFORM_180:
2744 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2745 transform.d[0] = -flip;
2746 transform.d[1] = 0;
2747 transform.d[4] = 0;
2748 transform.d[5] = -1;
2749 break;
2750 case WL_OUTPUT_TRANSFORM_270:
2751 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2752 transform.d[0] = 0;
2753 transform.d[1] = flip;
2754 transform.d[4] = -1;
2755 transform.d[5] = 0;
2756 break;
2757 default:
2758 break;
2759 }
2760
2761 weston_matrix_multiply(&output->matrix, &transform);
2762}
2763
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002764WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002765weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002766{
Scott Moreau850ca422012-05-21 15:21:25 -06002767 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002768 struct weston_matrix camera;
2769 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002770
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002771 weston_matrix_init(&output->matrix);
2772 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002773 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2774 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002775
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002776 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002777 2.0 / (output->width + output->border.left + output->border.right),
2778 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2779
2780 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002781
Scott Moreauccbf29d2012-02-22 14:21:41 -07002782 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002783 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002784 weston_matrix_init(&camera);
2785 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002786 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002787 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002788 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002789 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002790 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002791 weston_matrix_multiply(&output->matrix, &modelview);
2792 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002793
Scott Moreauccbf29d2012-02-22 14:21:41 -07002794 output->dirty = 0;
2795}
2796
Scott Moreau1bad5db2012-08-18 01:04:05 -06002797static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002798weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002799{
2800 output->transform = transform;
2801
2802 switch (transform) {
2803 case WL_OUTPUT_TRANSFORM_90:
2804 case WL_OUTPUT_TRANSFORM_270:
2805 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2806 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2807 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02002808 output->width = output->current_mode->height;
2809 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002810 break;
2811 case WL_OUTPUT_TRANSFORM_NORMAL:
2812 case WL_OUTPUT_TRANSFORM_180:
2813 case WL_OUTPUT_TRANSFORM_FLIPPED:
2814 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02002815 output->width = output->current_mode->width;
2816 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002817 break;
2818 default:
2819 break;
2820 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002821
Hardening57388e42013-09-18 23:56:36 +02002822 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002823 output->width /= scale;
2824 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002825}
2826
Scott Moreauccbf29d2012-02-22 14:21:41 -07002827WL_EXPORT void
2828weston_output_move(struct weston_output *output, int x, int y)
2829{
2830 output->x = x;
2831 output->y = y;
2832
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002833 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002834 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002835 output->width,
2836 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002837}
2838
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002839WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002840weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002841 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002842 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002843{
2844 output->compositor = c;
2845 output->x = x;
2846 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002847 output->border.top = 0;
2848 output->border.bottom = 0;
2849 output->border.left = 0;
2850 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002851 output->mm_width = mm_width;
2852 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002853 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02002854 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002855
Alexander Larsson0b135062013-05-28 16:23:36 +02002856 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002857 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002858
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002859 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002860 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002861
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002862 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002863 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002864 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002865 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002866
Casey Dahlin58ba1372012-04-19 22:50:08 -04002867 output->id = ffs(~output->compositor->output_id_pool) - 1;
2868 output->compositor->output_id_pool |= 1 << output->id;
2869
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002870 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002871 wl_global_create(c->wl_display, &wl_output_interface, 2,
2872 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002873 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002874}
2875
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002876WL_EXPORT void
2877weston_output_transform_coordinate(struct weston_output *output,
2878 int device_x, int device_y,
2879 wl_fixed_t *x, wl_fixed_t *y)
2880{
2881 wl_fixed_t tx, ty;
2882 wl_fixed_t width, height;
2883
Hardeningff39efa2013-09-18 23:56:35 +02002884 width = wl_fixed_from_int(output->width * output->current_scale - 1);
2885 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002886
2887 switch(output->transform) {
2888 case WL_OUTPUT_TRANSFORM_NORMAL:
2889 default:
2890 tx = wl_fixed_from_int(device_x);
2891 ty = wl_fixed_from_int(device_y);
2892 break;
2893 case WL_OUTPUT_TRANSFORM_90:
2894 tx = wl_fixed_from_int(device_y);
2895 ty = height - wl_fixed_from_int(device_x);
2896 break;
2897 case WL_OUTPUT_TRANSFORM_180:
2898 tx = width - wl_fixed_from_int(device_x);
2899 ty = height - wl_fixed_from_int(device_y);
2900 break;
2901 case WL_OUTPUT_TRANSFORM_270:
2902 tx = width - wl_fixed_from_int(device_y);
2903 ty = wl_fixed_from_int(device_x);
2904 break;
2905 case WL_OUTPUT_TRANSFORM_FLIPPED:
2906 tx = width - wl_fixed_from_int(device_x);
2907 ty = wl_fixed_from_int(device_y);
2908 break;
2909 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2910 tx = width - wl_fixed_from_int(device_y);
2911 ty = height - wl_fixed_from_int(device_x);
2912 break;
2913 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2914 tx = wl_fixed_from_int(device_x);
2915 ty = height - wl_fixed_from_int(device_y);
2916 break;
2917 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2918 tx = wl_fixed_from_int(device_y);
2919 ty = wl_fixed_from_int(device_x);
2920 break;
2921 }
2922
Hardeningff39efa2013-09-18 23:56:35 +02002923 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
2924 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002925}
2926
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002927static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002928compositor_bind(struct wl_client *client,
2929 void *data, uint32_t version, uint32_t id)
2930{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002931 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002932 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002933
Jason Ekstranda85118c2013-06-27 20:17:02 -05002934 resource = wl_resource_create(client, &wl_compositor_interface,
2935 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002936 if (resource == NULL) {
2937 wl_client_post_no_memory(client);
2938 return;
2939 }
2940
2941 wl_resource_set_implementation(resource, &compositor_interface,
2942 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002943}
2944
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002945static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002946log_uname(void)
2947{
2948 struct utsname usys;
2949
2950 uname(&usys);
2951
2952 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2953 usys.version, usys.machine);
2954}
2955
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002956WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002957weston_environment_get_fd(const char *env)
2958{
2959 char *e, *end;
2960 int fd, flags;
2961
2962 e = getenv(env);
2963 if (!e)
2964 return -1;
2965 fd = strtol(e, &end, 0);
2966 if (*end != '\0')
2967 return -1;
2968
2969 flags = fcntl(fd, F_GETFD);
2970 if (flags == -1)
2971 return -1;
2972
2973 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2974 unsetenv(env);
2975
2976 return fd;
2977}
2978
2979WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002980weston_compositor_init(struct weston_compositor *ec,
2981 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002982 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002983 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002984{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002985 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002986 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002987 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002988
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002989 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002990 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002991 wl_signal_init(&ec->destroy_signal);
2992 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002993 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002994 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002995 wl_signal_init(&ec->idle_signal);
2996 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002997 wl_signal_init(&ec->show_input_panel_signal);
2998 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002999 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003000 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003001 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003002 wl_signal_init(&ec->session_signal);
3003 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003004
Casey Dahlin58ba1372012-04-19 22:50:08 -04003005 ec->output_id_pool = 0;
3006
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003007 if (!wl_global_create(display, &wl_compositor_interface, 3,
3008 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003009 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003010
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003011 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3012 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013 return -1;
3014
Daniel Stone725c2c32012-06-22 14:04:36 +01003015 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003016 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003017 wl_list_init(&ec->layer_list);
3018 wl_list_init(&ec->seat_list);
3019 wl_list_init(&ec->output_list);
3020 wl_list_init(&ec->key_binding_list);
3021 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003022 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003023 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003024 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003025
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003026 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003027 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003028
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003029 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3030 weston_config_section_get_string(s, "keymap_rules",
3031 (char **) &xkb_names.rules, NULL);
3032 weston_config_section_get_string(s, "keymap_model",
3033 (char **) &xkb_names.model, NULL);
3034 weston_config_section_get_string(s, "keymap_layout",
3035 (char **) &xkb_names.layout, NULL);
3036 weston_config_section_get_string(s, "keymap_variant",
3037 (char **) &xkb_names.variant, NULL);
3038 weston_config_section_get_string(s, "keymap_options",
3039 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003040
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003041 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3042 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003043
3044 ec->ping_handler = NULL;
3045
3046 screenshooter_create(ec);
3047 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003048 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003049
3050 wl_data_device_manager_init(ec->wl_display);
3051
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003052 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003053
Daniel Stone725c2c32012-06-22 14:04:36 +01003054 loop = wl_display_get_event_loop(ec->wl_display);
3055 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3056 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3057
3058 ec->input_loop = wl_event_loop_create();
3059
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003060 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3061 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3062
3063 weston_compositor_schedule_repaint(ec);
3064
Daniel Stone725c2c32012-06-22 14:04:36 +01003065 return 0;
3066}
3067
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003068WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003069weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003070{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003071 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003072
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003073 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003074 if (ec->input_loop_source)
3075 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003076
Matt Roper361d2ad2011-08-29 13:52:23 -07003077 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003078 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003079 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003080
Daniel Stone325fc2d2012-05-30 16:31:58 +01003081 weston_binding_list_destroy_all(&ec->key_binding_list);
3082 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003083 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003084 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003085 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003086
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003087 weston_plane_release(&ec->primary_plane);
3088
Jonas Ådahlc97af922012-03-28 22:36:09 +02003089 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003090
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003091 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003092}
3093
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003094WL_EXPORT void
3095weston_version(int *major, int *minor, int *micro)
3096{
3097 *major = WESTON_VERSION_MAJOR;
3098 *minor = WESTON_VERSION_MINOR;
3099 *micro = WESTON_VERSION_MICRO;
3100}
3101
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003102static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003103 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003104 const char *desc;
3105} capability_strings[] = {
3106 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003107 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003108};
3109
3110static void
3111weston_compositor_log_capabilities(struct weston_compositor *compositor)
3112{
3113 unsigned i;
3114 int yes;
3115
3116 weston_log("Compositor capabilities:\n");
3117 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3118 yes = compositor->capabilities & capability_strings[i].bit;
3119 weston_log_continue(STAMP_SPACE "%s %s\n",
3120 capability_strings[i].desc,
3121 yes ? "yes" : "no");
3122 }
3123}
3124
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003125static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003126{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003127 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003128
Martin Minarik6d118362012-06-07 18:01:59 +02003129 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003130 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003131
3132 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003133}
3134
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003135#ifdef HAVE_LIBUNWIND
3136
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003137static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003138print_backtrace(void)
3139{
3140 unw_cursor_t cursor;
3141 unw_context_t context;
3142 unw_word_t off;
3143 unw_proc_info_t pip;
3144 int ret, i = 0;
3145 char procname[256];
3146 const char *filename;
3147 Dl_info dlinfo;
3148
3149 pip.unwind_info = NULL;
3150 ret = unw_getcontext(&context);
3151 if (ret) {
3152 weston_log("unw_getcontext: %d\n", ret);
3153 return;
3154 }
3155
3156 ret = unw_init_local(&cursor, &context);
3157 if (ret) {
3158 weston_log("unw_init_local: %d\n", ret);
3159 return;
3160 }
3161
3162 ret = unw_step(&cursor);
3163 while (ret > 0) {
3164 ret = unw_get_proc_info(&cursor, &pip);
3165 if (ret) {
3166 weston_log("unw_get_proc_info: %d\n", ret);
3167 break;
3168 }
3169
3170 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3171 if (ret && ret != -UNW_ENOMEM) {
3172 if (ret != -UNW_EUNSPEC)
3173 weston_log("unw_get_proc_name: %d\n", ret);
3174 procname[0] = '?';
3175 procname[1] = 0;
3176 }
3177
3178 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3179 *dlinfo.dli_fname)
3180 filename = dlinfo.dli_fname;
3181 else
3182 filename = "?";
3183
3184 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3185 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3186
3187 ret = unw_step(&cursor);
3188 if (ret < 0)
3189 weston_log("unw_step: %d\n", ret);
3190 }
3191}
3192
3193#else
3194
3195static void
3196print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003197{
3198 void *buffer[32];
3199 int i, count;
3200 Dl_info info;
3201
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003202 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3203 for (i = 0; i < count; i++) {
3204 dladdr(buffer[i], &info);
3205 weston_log(" [%016lx] %s (%s)\n",
3206 (long) buffer[i],
3207 info.dli_sname ? info.dli_sname : "--",
3208 info.dli_fname);
3209 }
3210}
3211
3212#endif
3213
3214static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003215on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003216{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003217 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003218 * then call the backend restore function, which will switch
3219 * back to the vt we launched from or ungrab X etc and then
3220 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003221 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003222 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003223 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003224
Peter Maatmane5b42e42013-03-27 22:38:53 +01003225 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003226
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003227 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003228
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003229 segv_compositor->restore(segv_compositor);
3230
3231 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003232}
3233
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003234WL_EXPORT void *
3235weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003236{
3237 char path[PATH_MAX];
3238 void *module, *init;
3239
3240 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003241 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003242 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003243 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003244
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003245 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3246 if (module) {
3247 weston_log("Module '%s' already loaded\n", path);
3248 dlclose(module);
3249 return NULL;
3250 }
3251
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003252 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003253 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003254 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003255 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003256 return NULL;
3257 }
3258
3259 init = dlsym(module, entrypoint);
3260 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003261 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003262 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003263 return NULL;
3264 }
3265
3266 return init;
3267}
3268
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003269static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003270load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003271 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003272{
3273 const char *p, *end;
3274 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003275 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003276 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003277
3278 if (modules == NULL)
3279 return 0;
3280
3281 p = modules;
3282 while (*p) {
3283 end = strchrnul(p, ',');
3284 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003285 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003286 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003287 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003288 p = end;
3289 while (*p == ',')
3290 p++;
3291
3292 }
3293
3294 return 0;
3295}
3296
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003297static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003298 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3299
3300static const char xdg_wrong_message[] =
3301 "fatal: environment variable XDG_RUNTIME_DIR\n"
3302 "is set to \"%s\", which is not a directory.\n";
3303
3304static const char xdg_wrong_mode_message[] =
3305 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3306 "correctly. Unix access mode must be 0700 but is %o,\n"
3307 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003308 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003309
3310static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003311 "Refer to your distribution on how to get it, or\n"
3312 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3313 "on how to implement it.\n";
3314
Martin Minarik37032f82012-06-18 20:15:18 +02003315static void
3316verify_xdg_runtime_dir(void)
3317{
3318 char *dir = getenv("XDG_RUNTIME_DIR");
3319 struct stat s;
3320
3321 if (!dir) {
3322 weston_log(xdg_error_message);
3323 weston_log_continue(xdg_detail_message);
3324 exit(EXIT_FAILURE);
3325 }
3326
3327 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3328 weston_log(xdg_wrong_message, dir);
3329 weston_log_continue(xdg_detail_message);
3330 exit(EXIT_FAILURE);
3331 }
3332
3333 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3334 weston_log(xdg_wrong_mode_message,
3335 dir, s.st_mode & 0777, s.st_uid);
3336 weston_log_continue(xdg_detail_message);
3337 }
3338}
3339
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003340static int
3341usage(int error_code)
3342{
3343 fprintf(stderr,
3344 "Usage: weston [OPTIONS]\n\n"
3345 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3346 "Weston supports multiple backends, and depending on which backend is in use\n"
3347 "different options will be accepted.\n\n"
3348
3349
3350 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003351 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003352 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003353 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3354 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003355 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003356 " -S, --socket=NAME\tName of socket to listen on\n"
3357 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003358 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003359 " --log==FILE\t\tLog to the given file\n"
3360 " -h, --help\t\tThis help message\n\n");
3361
3362 fprintf(stderr,
3363 "Options for drm-backend.so:\n\n"
3364 " --connector=ID\tBring up only this connector\n"
3365 " --seat=SEAT\t\tThe seat that weston should run on\n"
3366 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003367 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003368 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3369
3370 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003371 "Options for fbdev-backend.so:\n\n"
3372 " --tty=TTY\t\tThe tty to use\n"
3373 " --device=DEVICE\tThe framebuffer device to use\n\n");
3374
3375 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003376 "Options for x11-backend.so:\n\n"
3377 " --width=WIDTH\t\tWidth of X window\n"
3378 " --height=HEIGHT\tHeight of X window\n"
3379 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003380 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003381 " --output-count=COUNT\tCreate multiple outputs\n"
3382 " --no-input\t\tDont create input devices\n\n");
3383
3384 fprintf(stderr,
3385 "Options for wayland-backend.so:\n\n"
3386 " --width=WIDTH\t\tWidth of Wayland surface\n"
3387 " --height=HEIGHT\tHeight of Wayland surface\n"
3388 " --display=DISPLAY\tWayland display to connect to\n\n");
3389
Pekka Paalanene31e0532013-05-22 18:03:07 +03003390#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3391 fprintf(stderr,
3392 "Options for rpi-backend.so:\n\n"
3393 " --tty=TTY\t\tThe tty to use\n"
3394 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3395 " --transform=TR\tThe output transformation, TR is one of:\n"
3396 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3397 "\n");
3398#endif
3399
Hardeningc077a842013-07-08 00:51:35 +02003400#if defined(BUILD_RDP_COMPOSITOR)
3401 fprintf(stderr,
3402 "Options for rdp-backend.so:\n\n"
3403 " --width=WIDTH\t\tWidth of desktop\n"
3404 " --height=HEIGHT\tHeight of desktop\n"
3405 " --extra-modes=MODES\t\n"
3406 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3407 " --address=ADDR\tThe address to bind\n"
3408 " --port=PORT\tThe port to listen on\n"
3409 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3410 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3411 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3412 "\n");
3413#endif
3414
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003415 exit(error_code);
3416}
3417
Peter Maatmane5b42e42013-03-27 22:38:53 +01003418static void
3419catch_signals(void)
3420{
3421 struct sigaction action;
3422
3423 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3424 action.sa_sigaction = on_caught_signal;
3425 sigemptyset(&action.sa_mask);
3426 sigaction(SIGSEGV, &action, NULL);
3427 sigaction(SIGABRT, &action, NULL);
3428}
3429
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003430int main(int argc, char *argv[])
3431{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003432 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003433 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003434 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003435 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003436 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003437 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003438 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003439 int *argc, char *argv[],
3440 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003441 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003442 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003443 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003444 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003445 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003446 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003447 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003448 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003449 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003450 struct weston_config *config;
3451 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003452
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003453 const struct weston_option core_options[] = {
3454 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003455 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003456 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3457 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003458 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003459 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003460 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003461 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003462 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003463
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003464 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003465
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003466 if (help)
3467 usage(EXIT_SUCCESS);
3468
Scott Moreau12245142012-08-29 15:15:58 -06003469 if (version) {
3470 printf(PACKAGE_STRING "\n");
3471 return EXIT_SUCCESS;
3472 }
3473
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003474 weston_log_file_open(log);
3475
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003476 weston_log("%s\n"
3477 STAMP_SPACE "%s\n"
3478 STAMP_SPACE "Bug reports to: %s\n"
3479 STAMP_SPACE "Build: %s\n",
3480 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003481 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003482 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003483
Martin Minarik37032f82012-06-18 20:15:18 +02003484 verify_xdg_runtime_dir();
3485
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003486 display = wl_display_create();
3487
Tiago Vignatti2116b892011-08-08 05:52:59 -07003488 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003489 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3490 display);
3491 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3492 display);
3493 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3494 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003495
3496 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003497 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3498 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003499
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003500 if (!backend) {
3501 if (getenv("WAYLAND_DISPLAY"))
3502 backend = "wayland-backend.so";
3503 else if (getenv("DISPLAY"))
3504 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003505 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003506 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003507 }
3508
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003509 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003510 if (config != NULL) {
3511 weston_log("Using config file '%s'\n",
3512 weston_config_get_full_path(config));
3513 } else {
3514 weston_log("Starting with no config file.\n");
3515 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003516 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003517 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003518
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003519 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003520 if (!backend_init)
3521 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003522
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003523 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003524 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003525 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003526 exit(EXIT_FAILURE);
3527 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003528
Peter Maatmane5b42e42013-03-27 22:38:53 +01003529 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003530 segv_compositor = ec;
3531
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003532 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003533
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003534 setenv("WAYLAND_DISPLAY", socket_name, 1);
3535
Jason Ekstranda985da42013-08-22 17:28:03 -05003536 if (!shell)
3537 weston_config_section_get_string(section, "shell", &shell,
3538 "desktop-shell.so");
3539 if (load_modules(ec, shell, &argc, argv) < 0)
3540 goto out;
3541
Ossama Othmana50e6e42013-05-14 09:48:26 -07003542 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003543 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003544 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003545 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003546
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003547 for (i = 1; i < argc; i++)
3548 weston_log("fatal: unhandled option: %s\n", argv[i]);
3549 if (argc > 1) {
3550 ret = EXIT_FAILURE;
3551 goto out;
3552 }
3553
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003554 weston_compositor_log_capabilities(ec);
3555
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003556 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003557 weston_log("fatal: failed to add socket: %m\n");
3558 ret = EXIT_FAILURE;
3559 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003560 }
3561
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003562 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003563
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003564 wl_display_run(display);
3565
3566 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003567 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003568 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003569
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003570 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003571
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003572 for (i = ARRAY_LENGTH(signals); i;)
3573 wl_event_source_remove(signals[--i]);
3574
Daniel Stone855028f2012-05-01 20:37:10 +01003575 weston_compositor_xkb_destroy(ec);
3576
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003577 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003578 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003579
Martin Minarik19e6f262012-06-07 13:08:46 +02003580 weston_log_file_close();
3581
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003582 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003583}