blob: 34d9024bc388f2646dff9348d710539f3b50fc7f [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberga9410222011-01-14 17:22:35 -050025#include "config.h"
26
Daniel Stoneb7452fe2012-06-01 12:14:06 +010027#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040028#include <stdio.h>
29#include <string.h>
30#include <stdlib.h>
31#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010032#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050033#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020034#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040035#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010036#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040037#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020038#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020039#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020040#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040041#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050042#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040044#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040045#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050046#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040047#include <sys/time.h>
48#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050049
Marcin Slusarz554a0da2013-02-18 13:27:22 -050050#ifdef HAVE_LIBUNWIND
51#define UNW_LOCAL_ONLY
52#include <libunwind.h>
53#endif
54
Kristian Høgsberg82863022010-06-04 21:52:02 -040055#include "compositor.h"
U. Artie Eoffec08f332013-05-13 15:55:47 -070056#include "subsurface-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
96weston_compositor_build_surface_list(struct weston_compositor *compositor);
97
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
141 output->current_mode = mode;
142 output->current_scale = scale;
143 break;
144 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
145 if (!temporary_mode) {
146 weston_log("already in the native mode\n");
147 return -1;
148 }
149
150 notify_mode_changed = (output->original_mode != output->native_mode);
151
152 ret = output->switch_mode(output, mode);
153 if (ret < 0)
154 return ret;
155
156 if (output->original_scale != output->native_scale) {
157 notify_scale_changed = 1;
158 scale = output->native_scale;
159 output->original_scale = scale;
160 }
161 output->original_mode = 0;
162
163 output->current_scale = output->native_scale;
164 break;
165 default:
166 weston_log("unknown weston_switch_mode_op %d\n", op);
167 break;
168 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200169
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200170 pixman_region32_init(&old_output_region);
171 pixman_region32_copy(&old_output_region, &output->region);
172
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200173 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200174 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200175
176 pixman_region32_init(&output->previous_damage);
177 pixman_region32_init_rect(&output->region, output->x, output->y,
178 output->width, output->height);
179
180 weston_output_update_matrix(output);
181
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200182 /* If a pointer falls outside the outputs new geometry, move it to its
183 * lower-right corner */
184 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400185 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200186 int32_t x, y;
187
188 if (!pointer)
189 continue;
190
191 x = wl_fixed_to_int(pointer->x);
192 y = wl_fixed_to_int(pointer->y);
193
194 if (!pixman_region32_contains_point(&old_output_region,
195 x, y, NULL) ||
196 pixman_region32_contains_point(&output->region,
197 x, y, NULL))
198 continue;
199
200 if (x >= output->x + output->width)
201 x = output->x + output->width - 1;
202 if (y >= output->y + output->height)
203 y = output->y + output->height - 1;
204
205 pointer->x = wl_fixed_from_int(x);
206 pointer->y = wl_fixed_from_int(y);
207 }
208
209 pixman_region32_fini(&old_output_region);
210
Hardening57388e42013-09-18 23:56:36 +0200211 /* notify clients of the changes */
212 if (notify_mode_changed || notify_scale_changed) {
213 wl_resource_for_each(resource, &output->resource_list) {
214 if(notify_mode_changed) {
215 wl_output_send_mode(resource,
216 mode->flags | WL_OUTPUT_MODE_CURRENT,
217 mode->width,
218 mode->height,
219 mode->refresh);
220 }
221
222 if (notify_scale_changed)
223 wl_output_send_scale(resource, scale);
224
225 if (wl_resource_get_version(resource) >= 2)
226 wl_output_send_done(resource);
227 }
228 }
229
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200230 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800231}
232
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400233WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500234weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400235{
236 wl_list_insert(&child_process_list, &process->link);
237}
238
Benjamin Franzke06286262011-05-06 19:12:33 +0200239static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200240child_client_exec(int sockfd, const char *path)
241{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500242 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200243 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200244 sigset_t allsigs;
245
246 /* do not give our signal mask to the new process */
247 sigfillset(&allsigs);
248 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200249
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100250 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
251 if (seteuid(getuid()) == -1) {
252 weston_log("compositor: failed seteuid\n");
253 return;
254 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500255
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500256 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
257 * non-CLOEXEC fd to pass through exec. */
258 clientfd = dup(sockfd);
259 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200260 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500261 return;
262 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200263
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500264 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200265 setenv("WAYLAND_SOCKET", s, 1);
266
267 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200268 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200269 path);
270}
271
272WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500273weston_client_launch(struct weston_compositor *compositor,
274 struct weston_process *proc,
275 const char *path,
276 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200277{
278 int sv[2];
279 pid_t pid;
280 struct wl_client *client;
281
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300282 weston_log("launching '%s'\n", path);
283
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300284 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200285 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200286 "socketpair failed while launching '%s': %m\n",
287 path);
288 return NULL;
289 }
290
291 pid = fork();
292 if (pid == -1) {
293 close(sv[0]);
294 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200296 "fork failed while launching '%s': %m\n", path);
297 return NULL;
298 }
299
300 if (pid == 0) {
301 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700302 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200303 }
304
305 close(sv[1]);
306
307 client = wl_client_create(compositor->wl_display, sv[0]);
308 if (!client) {
309 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200310 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200311 "wl_client_create failed while launching '%s'.\n",
312 path);
313 return NULL;
314 }
315
316 proc->pid = pid;
317 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500318 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200319
320 return client;
321}
322
323static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300324surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
325{
326 struct weston_surface *surface =
327 container_of(listener, struct weston_surface,
328 pending.buffer_destroy_listener);
329
330 surface->pending.buffer = NULL;
331}
332
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200333static void
334empty_region(pixman_region32_t *region)
335{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300336 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200337 pixman_region32_init(region);
338}
339
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300340static void
341region_init_infinite(pixman_region32_t *region)
342{
343 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
344 UINT32_MAX, UINT32_MAX);
345}
346
Pekka Paalanene67858b2013-04-25 13:57:42 +0300347static struct weston_subsurface *
348weston_surface_to_subsurface(struct weston_surface *surface);
349
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500350WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500351weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500352{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500353 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400354
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200355 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400356 if (surface == NULL)
357 return NULL;
358
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500359 wl_signal_init(&surface->destroy_signal);
360
361 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500362
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500363 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500364 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500365
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500366 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400367 surface->alpha = 1.0;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200368 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400369
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100370 if (compositor->renderer->create_surface(surface) < 0) {
371 free(surface);
372 return NULL;
373 }
374
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200375 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200376 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200377 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200378 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400379 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400380 surface->plane = &compositor->primary_plane;
Giulio Camuffo184df502013-02-21 11:29:21 +0100381 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200382
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400383 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500384 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500385 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300386 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200387 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500388 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400389
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200390 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200391 wl_list_insert(&surface->geometry.transformation_list,
392 &surface->transform.position.link);
393 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200394 wl_list_init(&surface->geometry.child_list);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200395 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200396 surface->transform.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500397
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300398 surface->pending.buffer_destroy_listener.notify =
399 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300400 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300401 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300402 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300403 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300404
Pekka Paalanene67858b2013-04-25 13:57:42 +0300405 wl_list_init(&surface->subsurface_list);
406 wl_list_init(&surface->subsurface_list_pending);
407
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400408 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500409}
410
Alex Wu8811bf92012-02-28 18:07:54 +0800411WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500412weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200413 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500414{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100415 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500416}
417
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400418WL_EXPORT void
419weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200420 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200421{
422 if (surface->transform.enabled) {
423 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
424
425 weston_matrix_transform(&surface->transform.matrix, &v);
426
427 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200428 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700429 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200430 v.f[3]);
431 *x = 0;
432 *y = 0;
433 return;
434 }
435
436 *x = v.f[0] / v.f[3];
437 *y = v.f[1] / v.f[3];
438 } else {
439 *x = sx + surface->geometry.x;
440 *y = sy + surface->geometry.y;
441 }
442}
443
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500444WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200445weston_transformed_coord(int width, int height,
446 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200447 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200448 float sx, float sy, float *bx, float *by)
449{
450 switch (transform) {
451 case WL_OUTPUT_TRANSFORM_NORMAL:
452 default:
453 *bx = sx;
454 *by = sy;
455 break;
456 case WL_OUTPUT_TRANSFORM_FLIPPED:
457 *bx = width - sx;
458 *by = sy;
459 break;
460 case WL_OUTPUT_TRANSFORM_90:
461 *bx = height - sy;
462 *by = sx;
463 break;
464 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
465 *bx = height - sy;
466 *by = width - sx;
467 break;
468 case WL_OUTPUT_TRANSFORM_180:
469 *bx = width - sx;
470 *by = height - sy;
471 break;
472 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
473 *bx = sx;
474 *by = height - sy;
475 break;
476 case WL_OUTPUT_TRANSFORM_270:
477 *bx = sy;
478 *by = width - sx;
479 break;
480 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
481 *bx = sy;
482 *by = sx;
483 break;
484 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200485
486 *bx *= scale;
487 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200488}
489
490WL_EXPORT pixman_box32_t
491weston_transformed_rect(int width, int height,
492 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200493 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200494 pixman_box32_t rect)
495{
496 float x1, x2, y1, y2;
497
498 pixman_box32_t ret;
499
Alexander Larsson4ea95522013-05-22 14:41:37 +0200500 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200501 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200502 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200503 rect.x2, rect.y2, &x2, &y2);
504
505 if (x1 <= x2) {
506 ret.x1 = x1;
507 ret.x2 = x2;
508 } else {
509 ret.x1 = x2;
510 ret.x2 = x1;
511 }
512
513 if (y1 <= y2) {
514 ret.y1 = y1;
515 ret.y2 = y2;
516 } else {
517 ret.y1 = y2;
518 ret.y2 = y1;
519 }
520
521 return ret;
522}
523
524WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200525weston_surface_to_buffer_float(struct weston_surface *surface,
526 float sx, float sy, float *bx, float *by)
527{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200528 weston_transformed_coord(surface->geometry.width,
529 surface->geometry.height,
530 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200531 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200532 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200533}
534
Alexander Larsson4ea95522013-05-22 14:41:37 +0200535WL_EXPORT void
536weston_surface_to_buffer(struct weston_surface *surface,
537 int sx, int sy, int *bx, int *by)
538{
539 float bxf, byf;
540
541 weston_transformed_coord(surface->geometry.width,
542 surface->geometry.height,
543 surface->buffer_transform,
544 surface->buffer_scale,
545 sx, sy, &bxf, &byf);
546 *bx = floorf(bxf);
547 *by = floorf(byf);
548}
549
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200550WL_EXPORT pixman_box32_t
551weston_surface_to_buffer_rect(struct weston_surface *surface,
552 pixman_box32_t rect)
553{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200554 return weston_transformed_rect(surface->geometry.width,
555 surface->geometry.height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200556 surface->buffer_transform,
557 surface->buffer_scale,
558 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200559}
560
561WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400562weston_surface_move_to_plane(struct weston_surface *surface,
563 struct weston_plane *plane)
564{
565 if (surface->plane == plane)
566 return;
567
568 weston_surface_damage_below(surface);
569 surface->plane = plane;
570 weston_surface_damage(surface);
571}
572
573WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500574weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200575{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500576 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200577
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500578 pixman_region32_init(&damage);
579 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
580 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400581 pixman_region32_union(&surface->plane->damage,
582 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500583 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200584}
585
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400586static void
587weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
588{
589 uint32_t different = es->output_mask ^ mask;
590 uint32_t entered = mask & different;
591 uint32_t left = es->output_mask & different;
592 struct weston_output *output;
593 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500594 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400595
596 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500597 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400598 return;
599 if (different == 0)
600 return;
601
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500602 client = wl_resource_get_client(es->resource);
603
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400604 wl_list_for_each(output, &es->compositor->output_list, link) {
605 if (1 << output->id & different)
606 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500607 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400608 client);
609 if (resource == NULL)
610 continue;
611 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500612 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400613 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500614 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400615 }
616}
617
618static void
619weston_surface_assign_output(struct weston_surface *es)
620{
621 struct weston_compositor *ec = es->compositor;
622 struct weston_output *output, *new_output;
623 pixman_region32_t region;
624 uint32_t max, area, mask;
625 pixman_box32_t *e;
626
627 new_output = NULL;
628 max = 0;
629 mask = 0;
630 pixman_region32_init(&region);
631 wl_list_for_each(output, &ec->output_list, link) {
632 pixman_region32_intersect(&region, &es->transform.boundingbox,
633 &output->region);
634
635 e = pixman_region32_extents(&region);
636 area = (e->x2 - e->x1) * (e->y2 - e->y1);
637
638 if (area > 0)
639 mask |= 1 << output->id;
640
641 if (area >= max) {
642 new_output = output;
643 max = area;
644 }
645 }
646 pixman_region32_fini(&region);
647
648 es->output = new_output;
649 weston_surface_update_output_mask(es, mask);
650}
651
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200652static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200653surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
654 int32_t width, int32_t height,
655 pixman_region32_t *bbox)
656{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200657 float min_x = HUGE_VALF, min_y = HUGE_VALF;
658 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200659 int32_t s[4][2] = {
660 { sx, sy },
661 { sx, sy + height },
662 { sx + width, sy },
663 { sx + width, sy + height }
664 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200665 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200666 int i;
667
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300668 if (width == 0 || height == 0) {
669 /* avoid rounding empty bbox to 1x1 */
670 pixman_region32_init(bbox);
671 return;
672 }
673
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200674 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200675 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400676 weston_surface_to_global_float(surface,
677 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200678 if (x < min_x)
679 min_x = x;
680 if (x > max_x)
681 max_x = x;
682 if (y < min_y)
683 min_y = y;
684 if (y > max_y)
685 max_y = y;
686 }
687
Pekka Paalanen219b9822012-02-08 15:38:37 +0200688 int_x = floorf(min_x);
689 int_y = floorf(min_y);
690 pixman_region32_init_rect(bbox, int_x, int_y,
691 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200692}
693
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200694static void
695weston_surface_update_transform_disable(struct weston_surface *surface)
696{
697 surface->transform.enabled = 0;
698
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200699 /* round off fractions when not transformed */
700 surface->geometry.x = roundf(surface->geometry.x);
701 surface->geometry.y = roundf(surface->geometry.y);
702
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500703 /* Otherwise identity matrix, but with x and y translation. */
704 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
705 surface->transform.position.matrix.d[12] = surface->geometry.x;
706 surface->transform.position.matrix.d[13] = surface->geometry.y;
707
708 surface->transform.matrix = surface->transform.position.matrix;
709
Kristian Høgsberg9bcaaeb2013-02-28 14:56:43 -0500710 surface->transform.inverse = surface->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500711 surface->transform.inverse.d[12] = -surface->geometry.x;
712 surface->transform.inverse.d[13] = -surface->geometry.y;
713
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200714 pixman_region32_init_rect(&surface->transform.boundingbox,
715 surface->geometry.x,
716 surface->geometry.y,
717 surface->geometry.width,
718 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500719
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400720 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500721 pixman_region32_copy(&surface->transform.opaque,
722 &surface->opaque);
723 pixman_region32_translate(&surface->transform.opaque,
724 surface->geometry.x,
725 surface->geometry.y);
726 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200727}
728
729static int
730weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200731{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200732 struct weston_surface *parent = surface->geometry.parent;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200733 struct weston_matrix *matrix = &surface->transform.matrix;
734 struct weston_matrix *inverse = &surface->transform.inverse;
735 struct weston_transform *tform;
736
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200737 surface->transform.enabled = 1;
738
739 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300740 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200741 surface->transform.position.matrix.d[12] = surface->geometry.x;
742 surface->transform.position.matrix.d[13] = surface->geometry.y;
743
744 weston_matrix_init(matrix);
745 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
746 weston_matrix_multiply(matrix, &tform->matrix);
747
Pekka Paalanen483243f2013-03-08 14:56:50 +0200748 if (parent)
749 weston_matrix_multiply(matrix, &parent->transform.matrix);
750
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200751 if (weston_matrix_invert(inverse, matrix) < 0) {
752 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200753 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200754 " transformation not invertible.\n", surface);
755 return -1;
756 }
757
758 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
759 surface->geometry.height,
760 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500761
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200762 return 0;
763}
764
765WL_EXPORT void
766weston_surface_update_transform(struct weston_surface *surface)
767{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200768 struct weston_surface *parent = surface->geometry.parent;
769
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200770 if (!surface->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200771 return;
772
Pekka Paalanen483243f2013-03-08 14:56:50 +0200773 if (parent)
774 weston_surface_update_transform(parent);
775
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200776 surface->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200777
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500778 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200779
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200780 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500781 pixman_region32_fini(&surface->transform.opaque);
782 pixman_region32_init(&surface->transform.opaque);
783
Pekka Paalanencd403622012-01-25 13:37:39 +0200784 /* transform.position is always in transformation_list */
785 if (surface->geometry.transformation_list.next ==
786 &surface->transform.position.link &&
787 surface->geometry.transformation_list.prev ==
Pekka Paalanen483243f2013-03-08 14:56:50 +0200788 &surface->transform.position.link &&
789 !parent) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200790 weston_surface_update_transform_disable(surface);
791 } else {
792 if (weston_surface_update_transform_enable(surface) < 0)
793 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200794 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200795
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400796 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200797
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300798 weston_surface_assign_output(surface);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300799
800 wl_signal_emit(&surface->compositor->transform_signal, surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200801}
802
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200803WL_EXPORT void
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200804weston_surface_geometry_dirty(struct weston_surface *surface)
805{
Pekka Paalanen483243f2013-03-08 14:56:50 +0200806 struct weston_surface *child;
807
808 /*
809 * The invariant: if surface->geometry.dirty, then all surfaces
810 * in surface->geometry.child_list have geometry.dirty too.
811 * Corollary: if not parent->geometry.dirty, then all ancestors
812 * are not dirty.
813 */
814
815 if (surface->transform.dirty)
816 return;
817
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200818 surface->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200819
820 wl_list_for_each(child, &surface->geometry.child_list,
821 geometry.parent_link)
822 weston_surface_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200823}
824
825WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100826weston_surface_to_global_fixed(struct weston_surface *surface,
827 wl_fixed_t sx, wl_fixed_t sy,
828 wl_fixed_t *x, wl_fixed_t *y)
829{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200830 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100831
832 weston_surface_to_global_float(surface,
833 wl_fixed_to_double(sx),
834 wl_fixed_to_double(sy),
835 &xf, &yf);
836 *x = wl_fixed_from_double(xf);
837 *y = wl_fixed_from_double(yf);
838}
839
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400840WL_EXPORT void
841weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200842 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200843{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200844 if (surface->transform.enabled) {
845 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
846
847 weston_matrix_transform(&surface->transform.inverse, &v);
848
849 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200850 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200851 "weston_surface_from_global(), divisor = %g\n",
852 v.f[3]);
853 *sx = 0;
854 *sy = 0;
855 return;
856 }
857
Pekka Paalanencd403622012-01-25 13:37:39 +0200858 *sx = v.f[0] / v.f[3];
859 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200860 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200861 *sx = x - surface->geometry.x;
862 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200863 }
864}
865
866WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100867weston_surface_from_global_fixed(struct weston_surface *surface,
868 wl_fixed_t x, wl_fixed_t y,
869 wl_fixed_t *sx, wl_fixed_t *sy)
870{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200871 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100872
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400873 weston_surface_from_global_float(surface,
874 wl_fixed_to_double(x),
875 wl_fixed_to_double(y),
876 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100877 *sx = wl_fixed_from_double(sxf);
878 *sy = wl_fixed_from_double(syf);
879}
880
881WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200882weston_surface_from_global(struct weston_surface *surface,
883 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
884{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200885 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200886
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400887 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200888 *sx = floorf(sxf);
889 *sy = floorf(syf);
890}
891
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400892WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400893weston_surface_schedule_repaint(struct weston_surface *surface)
894{
895 struct weston_output *output;
896
897 wl_list_for_each(output, &surface->compositor->output_list, link)
898 if (surface->output_mask & (1 << output->id))
899 weston_output_schedule_repaint(output);
900}
901
902WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500903weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500904{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400905 pixman_region32_union_rect(&surface->damage, &surface->damage,
906 0, 0, surface->geometry.width,
907 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200908
Kristian Høgsberg98238702012-08-03 16:29:12 -0400909 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500910}
911
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400912WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200914 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400915{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200916 surface->geometry.x = x;
917 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200918 surface->geometry.width = width;
919 surface->geometry.height = height;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200920 weston_surface_geometry_dirty(surface);
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400921}
922
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200923WL_EXPORT void
924weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200925 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200926{
927 surface->geometry.x = x;
928 surface->geometry.y = y;
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200929 weston_surface_geometry_dirty(surface);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200930}
931
Pekka Paalanen483243f2013-03-08 14:56:50 +0200932static void
933transform_parent_handle_parent_destroy(struct wl_listener *listener,
934 void *data)
935{
936 struct weston_surface *surface =
937 container_of(listener, struct weston_surface,
938 geometry.parent_destroy_listener);
939
940 weston_surface_set_transform_parent(surface, NULL);
941}
942
943WL_EXPORT void
944weston_surface_set_transform_parent(struct weston_surface *surface,
945 struct weston_surface *parent)
946{
947 if (surface->geometry.parent) {
948 wl_list_remove(&surface->geometry.parent_destroy_listener.link);
949 wl_list_remove(&surface->geometry.parent_link);
950 }
951
952 surface->geometry.parent = parent;
953
954 surface->geometry.parent_destroy_listener.notify =
955 transform_parent_handle_parent_destroy;
956 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500957 wl_signal_add(&parent->destroy_signal,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200958 &surface->geometry.parent_destroy_listener);
959 wl_list_insert(&parent->geometry.child_list,
960 &surface->geometry.parent_link);
961 }
962
963 weston_surface_geometry_dirty(surface);
964}
965
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300966WL_EXPORT int
967weston_surface_is_mapped(struct weston_surface *surface)
968{
969 if (surface->output)
970 return 1;
971 else
972 return 0;
973}
974
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200975WL_EXPORT int32_t
976weston_surface_buffer_width(struct weston_surface *surface)
977{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200978 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200979 switch (surface->buffer_transform) {
980 case WL_OUTPUT_TRANSFORM_90:
981 case WL_OUTPUT_TRANSFORM_270:
982 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
983 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200984 width = surface->buffer_ref.buffer->height;
985 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200986 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +0200987 width = surface->buffer_ref.buffer->width;
988 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200989 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200990 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200991}
992
993WL_EXPORT int32_t
994weston_surface_buffer_height(struct weston_surface *surface)
995{
Alexander Larsson4ea95522013-05-22 14:41:37 +0200996 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200997 switch (surface->buffer_transform) {
998 case WL_OUTPUT_TRANSFORM_90:
999 case WL_OUTPUT_TRANSFORM_270:
1000 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1001 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001002 height = surface->buffer_ref.buffer->width;
1003 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001004 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001005 height = surface->buffer_ref.buffer->height;
1006 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001007 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001008 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001009}
1010
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001011WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001012weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001013{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001014 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001015
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001016 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001017
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001018 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001019}
1020
Kristian Høgsberg6848c252013-05-08 22:02:59 -04001021WL_EXPORT struct weston_surface *
Tiago Vignatti9d393522012-02-10 16:26:19 +02001022weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +01001023 wl_fixed_t x, wl_fixed_t y,
1024 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001025{
1026 struct weston_surface *surface;
1027
1028 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +01001029 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001030 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +01001031 wl_fixed_to_int(*sx),
1032 wl_fixed_to_int(*sy),
1033 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +02001034 return surface;
1035 }
1036
1037 return NULL;
1038}
1039
1040static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001041weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001042{
Daniel Stone37816df2012-05-16 18:45:18 +01001043 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001044
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001045 if (!compositor->focus)
1046 return;
1047
Daniel Stone37816df2012-05-16 18:45:18 +01001048 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001049 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001050}
1051
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001052WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001053weston_surface_unmap(struct weston_surface *surface)
1054{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001055 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001056
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001057 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001058 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001059 wl_list_remove(&surface->layer_link);
Giulio Camuffo4df790e2013-09-21 18:08:28 +02001060 wl_list_remove(&surface->link);
1061 wl_list_init(&surface->link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001062
Daniel Stone4dab5db2012-05-30 16:31:53 +01001063 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001064 if (seat->keyboard && seat->keyboard->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001065 weston_keyboard_set_focus(seat->keyboard, NULL);
Kristian Høgsbergfe7aa902013-05-08 09:54:37 -04001066 if (seat->pointer && seat->pointer->focus == surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001067 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001068 NULL,
1069 wl_fixed_from_int(0),
1070 wl_fixed_from_int(0));
Michael Fua2bb7912013-07-23 15:51:06 +08001071 if (seat->touch && seat->touch->focus == surface)
1072 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001073 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001074
Kristian Høgsberg98238702012-08-03 16:29:12 -04001075 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001076}
1077
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001078struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001079 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001080 struct wl_list link;
1081};
1082
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001083
1084WL_EXPORT void
1085weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001086{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001087 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001088 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001089
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001090 if (--surface->ref_count > 0)
1091 return;
1092
1093 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1094
Pekka Paalanen483243f2013-03-08 14:56:50 +02001095 assert(wl_list_empty(&surface->geometry.child_list));
Pekka Paalanene67858b2013-04-25 13:57:42 +03001096 assert(wl_list_empty(&surface->subsurface_list_pending));
1097 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001098
Kristian Høgsbergb12e3562013-09-21 21:26:05 -07001099 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001100 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001101
Pekka Paalanenbc106382012-10-10 12:49:31 +03001102 wl_list_for_each_safe(cb, next,
1103 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001104 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001105
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001106 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001107 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001108 pixman_region32_fini(&surface->pending.damage);
1109
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001110 if (surface->pending.buffer)
1111 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1112
Pekka Paalanende685b82012-12-04 15:58:12 +02001113 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001114
Kristian Høgsberg42263852012-09-06 21:59:29 -04001115 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001116
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001117 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001118 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001119 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001120 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001121 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001122
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001123 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001124 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001125
Pekka Paalanen483243f2013-03-08 14:56:50 +02001126 weston_surface_set_transform_parent(surface, NULL);
1127
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001128 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001129}
1130
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001131static void
1132destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001133{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001134 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001135
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001136 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001137}
1138
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001139static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001140weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1141{
1142 struct weston_buffer *buffer =
1143 container_of(listener, struct weston_buffer, destroy_listener);
1144
1145 wl_signal_emit(&buffer->destroy_signal, buffer);
1146 free(buffer);
1147}
1148
1149struct weston_buffer *
1150weston_buffer_from_resource(struct wl_resource *resource)
1151{
1152 struct weston_buffer *buffer;
1153 struct wl_listener *listener;
1154
1155 listener = wl_resource_get_destroy_listener(resource,
1156 weston_buffer_destroy_handler);
1157
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001158 if (listener)
1159 return container_of(listener, struct weston_buffer,
1160 destroy_listener);
1161
1162 buffer = zalloc(sizeof *buffer);
1163 if (buffer == NULL)
1164 return NULL;
1165
1166 buffer->resource = resource;
1167 wl_signal_init(&buffer->destroy_signal);
1168 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001169 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001170 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001171
1172 return buffer;
1173}
1174
1175static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001176weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1177 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001178{
Pekka Paalanende685b82012-12-04 15:58:12 +02001179 struct weston_buffer_reference *ref =
1180 container_of(listener, struct weston_buffer_reference,
1181 destroy_listener);
1182
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001183 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001184 ref->buffer = NULL;
1185}
1186
1187WL_EXPORT void
1188weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001189 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001190{
1191 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001192 ref->buffer->busy_count--;
1193 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001194 assert(wl_resource_get_client(ref->buffer->resource));
1195 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001196 WL_BUFFER_RELEASE);
1197 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001198 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001199 }
1200
Pekka Paalanende685b82012-12-04 15:58:12 +02001201 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001202 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001203 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001204 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001205 }
1206
Pekka Paalanende685b82012-12-04 15:58:12 +02001207 ref->buffer = buffer;
1208 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1209}
1210
1211static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001212weston_surface_attach(struct weston_surface *surface,
1213 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001214{
1215 weston_buffer_reference(&surface->buffer_ref, buffer);
1216
Pekka Paalanena6421c42012-12-04 15:58:10 +02001217 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001218 if (weston_surface_is_mapped(surface))
1219 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001220 }
1221
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001222 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001223}
1224
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001225WL_EXPORT void
1226weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001227{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001228 wl_list_remove(&surface->layer_link);
1229 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001230 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001231 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001232}
1233
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001234WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001235weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001236{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001237 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001238
1239 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001240 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001241}
1242
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001243WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001244weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001245{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001247
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001248 pixman_region32_union(&compositor->primary_plane.damage,
1249 &compositor->primary_plane.damage,
1250 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001251 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001252}
1253
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001254static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001255surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001256 pixman_region32_t *opaque)
1257{
Pekka Paalanende685b82012-12-04 15:58:12 +02001258 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001259 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001260 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001261
1262 if (surface->transform.enabled) {
1263 pixman_box32_t *extents;
1264
1265 extents = pixman_region32_extents(&surface->damage);
1266 surface_compute_bbox(surface, extents->x1, extents->y1,
1267 extents->x2 - extents->x1,
1268 extents->y2 - extents->y1,
1269 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001270 pixman_region32_translate(&surface->damage,
1271 -surface->plane->x,
1272 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001273 } else {
1274 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001275 surface->geometry.x - surface->plane->x,
1276 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001277 }
1278
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001279 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001280 pixman_region32_union(&surface->plane->damage,
1281 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001282 empty_region(&surface->damage);
1283 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001284 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001285}
1286
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001287static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001288compositor_accumulate_damage(struct weston_compositor *ec)
1289{
1290 struct weston_plane *plane;
1291 struct weston_surface *es;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001292 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001293
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001294 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001295
1296 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001297 pixman_region32_copy(&plane->clip, &clip);
1298
1299 pixman_region32_init(&opaque);
1300
1301 wl_list_for_each(es, &ec->surface_list, link) {
1302 if (es->plane != plane)
1303 continue;
1304
1305 surface_accumulate_damage(es, &opaque);
1306 }
1307
1308 pixman_region32_union(&clip, &clip, &opaque);
1309 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001310 }
1311
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001312 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001313
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001314 wl_list_for_each(es, &ec->surface_list, link) {
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001315 /* Both the renderer and the backend have seen the buffer
1316 * by now. If renderer needs the buffer, it has its own
1317 * reference set. If the backend wants to keep the buffer
1318 * around for migrating the surface into a non-primary plane
1319 * later, keep_buffer is true. Otherwise, drop the core
1320 * reference now, and allow early buffer release. This enables
1321 * clients to use single-buffering.
1322 */
1323 if (!es->keep_buffer)
1324 weston_buffer_reference(&es->buffer_ref, NULL);
1325 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001326}
1327
1328static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001329surface_list_add(struct weston_compositor *compositor,
1330 struct weston_surface *surface)
1331{
1332 struct weston_subsurface *sub;
1333
1334 if (wl_list_empty(&surface->subsurface_list)) {
1335 weston_surface_update_transform(surface);
1336 wl_list_insert(compositor->surface_list.prev, &surface->link);
1337 return;
1338 }
1339
1340 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1341 if (!weston_surface_is_mapped(sub->surface))
1342 continue;
1343
1344 if (sub->surface == surface) {
1345 weston_surface_update_transform(sub->surface);
1346 wl_list_insert(compositor->surface_list.prev,
1347 &sub->surface->link);
1348 } else {
1349 surface_list_add(compositor, sub->surface);
1350 }
1351 }
1352}
1353
1354static void
1355weston_compositor_build_surface_list(struct weston_compositor *compositor)
1356{
1357 struct weston_surface *surface;
1358 struct weston_layer *layer;
1359
1360 wl_list_init(&compositor->surface_list);
1361 wl_list_for_each(layer, &compositor->layer_list, link) {
1362 wl_list_for_each(surface, &layer->surface_list, layer_link) {
1363 surface_list_add(compositor, surface);
1364 }
1365 }
1366}
1367
1368static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001369weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001370{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001371 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001372 struct weston_surface *es;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001373 struct weston_animation *animation, *next;
1374 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001375 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001376 pixman_region32_t output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001377
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001378 /* Rebuild the surface list and update surface transforms up front. */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001379 weston_compositor_build_surface_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001380
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001381 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001382 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001383 else
1384 wl_list_for_each(es, &ec->surface_list, link)
1385 weston_surface_move_to_plane(es, &ec->primary_plane);
1386
Pekka Paalanene67858b2013-04-25 13:57:42 +03001387 wl_list_init(&frame_callback_list);
1388 wl_list_for_each(es, &ec->surface_list, link) {
1389 if (es->output == output) {
1390 wl_list_insert_list(&frame_callback_list,
1391 &es->frame_callback_list);
1392 wl_list_init(&es->frame_callback_list);
1393 }
1394 }
1395
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001396 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001397
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001398 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001399 pixman_region32_intersect(&output_damage,
1400 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001401 pixman_region32_subtract(&output_damage,
1402 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001403
Scott Moreauccbf29d2012-02-22 14:21:41 -07001404 if (output->dirty)
1405 weston_output_update_matrix(output);
1406
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001407 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001408
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001409 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001410
Kristian Høgsbergef044142011-06-21 15:02:12 -04001411 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001412
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001413 weston_compositor_repick(ec);
1414 wl_event_loop_dispatch(ec->input_loop, 0);
1415
Jonas Ådahldb773762012-06-13 00:01:21 +02001416 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001417 wl_callback_send_done(cb->resource, msecs);
1418 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001419 }
1420
Scott Moreaud64cf212012-06-08 19:40:54 -06001421 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001422 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001423 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001424 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001425}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001426
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001427static int
1428weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001429{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001430 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001431
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001432 wl_event_loop_dispatch(compositor->input_loop, 0);
1433
1434 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001435}
1436
1437WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001438weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001439{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001440 struct weston_compositor *compositor = output->compositor;
1441 struct wl_event_loop *loop =
1442 wl_display_get_event_loop(compositor->wl_display);
1443 int fd;
1444
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001445 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001446 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001447 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001448 return;
1449 }
1450
1451 output->repaint_scheduled = 0;
1452 if (compositor->input_loop_source)
1453 return;
1454
1455 fd = wl_event_loop_get_fd(compositor->input_loop);
1456 compositor->input_loop_source =
1457 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1458 weston_compositor_read_input, compositor);
1459}
1460
1461static void
1462idle_repaint(void *data)
1463{
1464 struct weston_output *output = data;
1465
Jonas Ådahle5a12252013-04-05 23:07:11 +02001466 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001467}
1468
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001469WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001470weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1471{
1472 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001473 if (below != NULL)
1474 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001475}
1476
1477WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001478weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001479{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001480 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001481 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001482
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001483 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1484 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001485 return;
1486
Kristian Høgsbergef044142011-06-21 15:02:12 -04001487 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001488 output->repaint_needed = 1;
1489 if (output->repaint_scheduled)
1490 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001491
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001492 wl_event_loop_add_idle(loop, idle_repaint, output);
1493 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001494
1495 if (compositor->input_loop_source) {
1496 wl_event_source_remove(compositor->input_loop_source);
1497 compositor->input_loop_source = NULL;
1498 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001499}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001500
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001501WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001502weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1503{
1504 struct weston_output *output;
1505
1506 wl_list_for_each(output, &compositor->output_list, link)
1507 weston_output_schedule_repaint(output);
1508}
1509
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001510static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001511surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001512{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001513 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001514}
1515
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001516static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001517surface_attach(struct wl_client *client,
1518 struct wl_resource *resource,
1519 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1520{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001521 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001522 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001523
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001524 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001525 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001526 if (buffer == NULL) {
1527 wl_client_post_no_memory(client);
1528 return;
1529 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001530 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001531
Pekka Paalanende685b82012-12-04 15:58:12 +02001532 /* Attach, attach, without commit in between does not send
1533 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001534 if (surface->pending.buffer)
1535 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001536
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001537 surface->pending.sx = sx;
1538 surface->pending.sy = sy;
1539 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001540 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001541 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001542 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001543 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001544 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001545}
1546
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001547static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001548surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001549 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001550 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001551{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001552 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001553
Pekka Paalanen8e159182012-10-10 12:49:25 +03001554 pixman_region32_union_rect(&surface->pending.damage,
1555 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001556 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001557}
1558
Kristian Høgsberg33418202011-08-16 23:01:28 -04001559static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001560destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001561{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001562 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001563
1564 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001565 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001566}
1567
1568static void
1569surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001570 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001572 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001573 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001574
1575 cb = malloc(sizeof *cb);
1576 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001577 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001578 return;
1579 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001580
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001581 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1582 callback);
1583 if (cb->resource == NULL) {
1584 free(cb);
1585 wl_resource_post_no_memory(resource);
1586 return;
1587 }
1588
Jason Ekstranda85118c2013-06-27 20:17:02 -05001589 wl_resource_set_implementation(cb->resource, NULL, cb,
1590 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001591
Pekka Paalanenbc106382012-10-10 12:49:31 +03001592 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001593}
1594
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001595static void
1596surface_set_opaque_region(struct wl_client *client,
1597 struct wl_resource *resource,
1598 struct wl_resource *region_resource)
1599{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001600 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001601 struct weston_region *region;
1602
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001603 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001604 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001605 pixman_region32_copy(&surface->pending.opaque,
1606 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001607 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001608 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001609 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001610}
1611
1612static void
1613surface_set_input_region(struct wl_client *client,
1614 struct wl_resource *resource,
1615 struct wl_resource *region_resource)
1616{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001617 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001618 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001619
1620 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001621 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001622 pixman_region32_copy(&surface->pending.input,
1623 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001624 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001625 pixman_region32_fini(&surface->pending.input);
1626 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001627 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001628}
1629
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001630static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001631weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001632{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001633 struct weston_subsurface *sub;
1634
1635 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1636 parent_link_pending) {
1637 wl_list_remove(&sub->parent_link);
1638 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1639 }
1640}
1641
1642static void
1643weston_surface_commit(struct weston_surface *surface)
1644{
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001645 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001646 int surface_width = 0;
1647 int surface_height = 0;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001648
Alexander Larsson4ea95522013-05-22 14:41:37 +02001649 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001650 surface->buffer_transform = surface->pending.buffer_transform;
1651
Alexander Larsson4ea95522013-05-22 14:41:37 +02001652 /* wl_surface.set_buffer_scale */
1653 surface->buffer_scale = surface->pending.buffer_scale;
1654
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001655 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001656 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001657 weston_surface_attach(surface, surface->pending.buffer);
1658
Giulio Camuffo184df502013-02-21 11:29:21 +01001659 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001660 surface_width = weston_surface_buffer_width(surface);
1661 surface_height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001662 }
1663
1664 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001665 surface->configure(surface,
1666 surface->pending.sx, surface->pending.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001667 surface_width, surface_height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001668
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001669 if (surface->pending.buffer)
1670 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1671 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001672 surface->pending.sx = 0;
1673 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001674 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001675
1676 /* wl_surface.damage */
1677 pixman_region32_union(&surface->damage, &surface->damage,
1678 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001679 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1680 0, 0,
1681 surface->geometry.width,
1682 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001683 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001684
1685 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001686 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001687 surface->geometry.width,
1688 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001689 pixman_region32_intersect(&opaque,
1690 &opaque, &surface->pending.opaque);
1691
1692 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1693 pixman_region32_copy(&surface->opaque, &opaque);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001694 weston_surface_geometry_dirty(surface);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001695 }
1696
1697 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001698
1699 /* wl_surface.set_input_region */
1700 pixman_region32_fini(&surface->input);
1701 pixman_region32_init_rect(&surface->input, 0, 0,
1702 surface->geometry.width,
1703 surface->geometry.height);
1704 pixman_region32_intersect(&surface->input,
1705 &surface->input, &surface->pending.input);
1706
Pekka Paalanenbc106382012-10-10 12:49:31 +03001707 /* wl_surface.frame */
1708 wl_list_insert_list(&surface->frame_callback_list,
1709 &surface->pending.frame_callback_list);
1710 wl_list_init(&surface->pending.frame_callback_list);
1711
Pekka Paalanene67858b2013-04-25 13:57:42 +03001712 weston_surface_commit_subsurface_order(surface);
1713
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001714 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001715}
1716
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001717static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001718weston_subsurface_commit(struct weston_subsurface *sub);
1719
1720static void
1721weston_subsurface_parent_commit(struct weston_subsurface *sub,
1722 int parent_is_synchronized);
1723
1724static void
1725surface_commit(struct wl_client *client, struct wl_resource *resource)
1726{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001727 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001728 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1729
1730 if (sub) {
1731 weston_subsurface_commit(sub);
1732 return;
1733 }
1734
1735 weston_surface_commit(surface);
1736
1737 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1738 if (sub->surface != surface)
1739 weston_subsurface_parent_commit(sub, 0);
1740 }
1741}
1742
1743static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001744surface_set_buffer_transform(struct wl_client *client,
1745 struct wl_resource *resource, int transform)
1746{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001747 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001748
1749 surface->pending.buffer_transform = transform;
1750}
1751
Alexander Larsson4ea95522013-05-22 14:41:37 +02001752static void
1753surface_set_buffer_scale(struct wl_client *client,
1754 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001755 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001756{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001757 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001758
1759 surface->pending.buffer_scale = scale;
1760}
1761
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001762static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001763 surface_destroy,
1764 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001765 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001766 surface_frame,
1767 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001768 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001769 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001770 surface_set_buffer_transform,
1771 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001772};
1773
1774static void
1775compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001776 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001777{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04001778 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001779 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001780
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001781 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001782 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001783 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001784 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001785 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001786
Jason Ekstranda85118c2013-06-27 20:17:02 -05001787 surface->resource =
1788 wl_resource_create(client, &wl_surface_interface,
1789 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001790 if (surface->resource == NULL) {
1791 weston_surface_destroy(surface);
1792 wl_resource_post_no_memory(resource);
1793 return;
1794 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001795 wl_resource_set_implementation(surface->resource, &surface_interface,
1796 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001797}
1798
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001799static void
1800destroy_region(struct wl_resource *resource)
1801{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001802 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001803
1804 pixman_region32_fini(&region->region);
1805 free(region);
1806}
1807
1808static void
1809region_destroy(struct wl_client *client, struct wl_resource *resource)
1810{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001811 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001812}
1813
1814static void
1815region_add(struct wl_client *client, struct wl_resource *resource,
1816 int32_t x, int32_t y, int32_t width, int32_t height)
1817{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001818 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001819
1820 pixman_region32_union_rect(&region->region, &region->region,
1821 x, y, width, height);
1822}
1823
1824static void
1825region_subtract(struct wl_client *client, struct wl_resource *resource,
1826 int32_t x, int32_t y, int32_t width, int32_t height)
1827{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001828 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001829 pixman_region32_t rect;
1830
1831 pixman_region32_init_rect(&rect, x, y, width, height);
1832 pixman_region32_subtract(&region->region, &region->region, &rect);
1833 pixman_region32_fini(&rect);
1834}
1835
1836static const struct wl_region_interface region_interface = {
1837 region_destroy,
1838 region_add,
1839 region_subtract
1840};
1841
1842static void
1843compositor_create_region(struct wl_client *client,
1844 struct wl_resource *resource, uint32_t id)
1845{
1846 struct weston_region *region;
1847
1848 region = malloc(sizeof *region);
1849 if (region == NULL) {
1850 wl_resource_post_no_memory(resource);
1851 return;
1852 }
1853
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001854 pixman_region32_init(&region->region);
1855
Jason Ekstranda85118c2013-06-27 20:17:02 -05001856 region->resource =
1857 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001858 if (region->resource == NULL) {
1859 free(region);
1860 wl_resource_post_no_memory(resource);
1861 return;
1862 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05001863 wl_resource_set_implementation(region->resource, &region_interface,
1864 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001865}
1866
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001867static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001868 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001869 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001870};
1871
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001872static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001873weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
1874{
1875 struct weston_surface *surface = sub->surface;
1876 pixman_region32_t opaque;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001877 int surface_width = 0;
1878 int surface_height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001879
Alexander Larsson4ea95522013-05-22 14:41:37 +02001880 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881 surface->buffer_transform = sub->cached.buffer_transform;
1882
Alexander Larsson4ea95522013-05-22 14:41:37 +02001883 /* wl_surface.set_buffer_scale */
1884 surface->buffer_scale = sub->cached.buffer_scale;
1885
Pekka Paalanene67858b2013-04-25 13:57:42 +03001886 /* wl_surface.attach */
1887 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
1888 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
1889 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
1890
1891 if (surface->buffer_ref.buffer) {
Alexander Larsson4ea95522013-05-22 14:41:37 +02001892 surface_width = weston_surface_buffer_width(surface);
1893 surface_height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001894 }
1895
1896 if (surface->configure && sub->cached.newly_attached)
1897 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001898 surface_width, surface_height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001899 sub->cached.sx = 0;
1900 sub->cached.sy = 0;
1901 sub->cached.newly_attached = 0;
1902
1903 /* wl_surface.damage */
1904 pixman_region32_union(&surface->damage, &surface->damage,
1905 &sub->cached.damage);
1906 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1907 0, 0,
1908 surface->geometry.width,
1909 surface->geometry.height);
1910 empty_region(&sub->cached.damage);
1911
1912 /* wl_surface.set_opaque_region */
1913 pixman_region32_init_rect(&opaque, 0, 0,
1914 surface->geometry.width,
1915 surface->geometry.height);
1916 pixman_region32_intersect(&opaque,
1917 &opaque, &sub->cached.opaque);
1918
1919 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1920 pixman_region32_copy(&surface->opaque, &opaque);
1921 weston_surface_geometry_dirty(surface);
1922 }
1923
1924 pixman_region32_fini(&opaque);
1925
1926 /* wl_surface.set_input_region */
1927 pixman_region32_fini(&surface->input);
1928 pixman_region32_init_rect(&surface->input, 0, 0,
1929 surface->geometry.width,
1930 surface->geometry.height);
1931 pixman_region32_intersect(&surface->input,
1932 &surface->input, &sub->cached.input);
1933
1934 /* wl_surface.frame */
1935 wl_list_insert_list(&surface->frame_callback_list,
1936 &sub->cached.frame_callback_list);
1937 wl_list_init(&sub->cached.frame_callback_list);
1938
1939 weston_surface_commit_subsurface_order(surface);
1940
1941 weston_surface_schedule_repaint(surface);
1942
1943 sub->cached.has_data = 0;
1944}
1945
1946static void
1947weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
1948{
1949 struct weston_surface *surface = sub->surface;
1950
1951 /*
1952 * If this commit would cause the surface to move by the
1953 * attach(dx, dy) parameters, the old damage region must be
1954 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02001955 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03001956 */
1957 pixman_region32_translate(&sub->cached.damage,
1958 -surface->pending.sx, -surface->pending.sy);
1959 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
1960 &surface->pending.damage);
1961 empty_region(&surface->pending.damage);
1962
1963 if (surface->pending.newly_attached) {
1964 sub->cached.newly_attached = 1;
1965 weston_buffer_reference(&sub->cached.buffer_ref,
1966 surface->pending.buffer);
1967 }
1968 sub->cached.sx += surface->pending.sx;
1969 sub->cached.sy += surface->pending.sy;
1970 surface->pending.sx = 0;
1971 surface->pending.sy = 0;
1972 surface->pending.newly_attached = 0;
1973
1974 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001975 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001976
1977 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
1978
1979 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
1980
1981 wl_list_insert_list(&sub->cached.frame_callback_list,
1982 &surface->pending.frame_callback_list);
1983 wl_list_init(&surface->pending.frame_callback_list);
1984
1985 sub->cached.has_data = 1;
1986}
1987
1988static int
1989weston_subsurface_is_synchronized(struct weston_subsurface *sub)
1990{
1991 while (sub) {
1992 if (sub->synchronized)
1993 return 1;
1994
1995 if (!sub->parent)
1996 return 0;
1997
1998 sub = weston_surface_to_subsurface(sub->parent);
1999 }
2000
2001 return 0;
2002}
2003
2004static void
2005weston_subsurface_commit(struct weston_subsurface *sub)
2006{
2007 struct weston_surface *surface = sub->surface;
2008 struct weston_subsurface *tmp;
2009
2010 /* Recursive check for effectively synchronized. */
2011 if (weston_subsurface_is_synchronized(sub)) {
2012 weston_subsurface_commit_to_cache(sub);
2013 } else {
2014 if (sub->cached.has_data) {
2015 /* flush accumulated state from cache */
2016 weston_subsurface_commit_to_cache(sub);
2017 weston_subsurface_commit_from_cache(sub);
2018 } else {
2019 weston_surface_commit(surface);
2020 }
2021
2022 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2023 if (tmp->surface != surface)
2024 weston_subsurface_parent_commit(tmp, 0);
2025 }
2026 }
2027}
2028
2029static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002030weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002031{
2032 struct weston_surface *surface = sub->surface;
2033 struct weston_subsurface *tmp;
2034
Pekka Paalanene67858b2013-04-25 13:57:42 +03002035 /* From now on, commit_from_cache the whole sub-tree, regardless of
2036 * the synchronized mode of each child. This sub-surface or some
2037 * of its ancestors were synchronized, so we are synchronized
2038 * all the way down.
2039 */
2040
2041 if (sub->cached.has_data)
2042 weston_subsurface_commit_from_cache(sub);
2043
2044 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2045 if (tmp->surface != surface)
2046 weston_subsurface_parent_commit(tmp, 1);
2047 }
2048}
2049
2050static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002051weston_subsurface_parent_commit(struct weston_subsurface *sub,
2052 int parent_is_synchronized)
2053{
2054 if (sub->position.set) {
2055 weston_surface_set_position(sub->surface,
2056 sub->position.x, sub->position.y);
2057 sub->position.set = 0;
2058 }
2059
2060 if (parent_is_synchronized || sub->synchronized)
2061 weston_subsurface_synchronized_commit(sub);
2062}
2063
2064static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002065subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2066 int32_t width, int32_t height)
2067{
2068 struct weston_compositor *compositor = surface->compositor;
2069
2070 weston_surface_configure(surface,
2071 surface->geometry.x + dx,
2072 surface->geometry.y + dy,
2073 width, height);
2074
2075 /* No need to check parent mappedness, because if parent is not
2076 * mapped, parent is not in a visible layer, so this sub-surface
2077 * will not be drawn either.
2078 */
2079 if (!weston_surface_is_mapped(surface)) {
2080 wl_list_init(&surface->layer_link);
2081
2082 /* Cannot call weston_surface_update_transform(),
2083 * because that would call it also for the parent surface,
2084 * which might not be mapped yet. That would lead to
2085 * inconsistent state, where the window could never be
2086 * mapped.
2087 *
2088 * Instead just assing any output, to make
2089 * weston_surface_is_mapped() return true, so that when the
2090 * parent surface does get mapped, this one will get
2091 * included, too. See surface_list_add().
2092 */
2093 assert(!wl_list_empty(&compositor->output_list));
2094 surface->output = container_of(compositor->output_list.next,
2095 struct weston_output, link);
2096 }
2097}
2098
2099static struct weston_subsurface *
2100weston_surface_to_subsurface(struct weston_surface *surface)
2101{
2102 if (surface->configure == subsurface_configure)
2103 return surface->configure_private;
2104
2105 return NULL;
2106}
2107
Pekka Paalanen01388e22013-04-25 13:57:44 +03002108WL_EXPORT struct weston_surface *
2109weston_surface_get_main_surface(struct weston_surface *surface)
2110{
2111 struct weston_subsurface *sub;
2112
2113 while (surface && (sub = weston_surface_to_subsurface(surface)))
2114 surface = sub->parent;
2115
2116 return surface;
2117}
2118
Pekka Paalanene67858b2013-04-25 13:57:42 +03002119static void
2120subsurface_set_position(struct wl_client *client,
2121 struct wl_resource *resource, int32_t x, int32_t y)
2122{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002123 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002124
2125 if (!sub)
2126 return;
2127
2128 sub->position.x = x;
2129 sub->position.y = y;
2130 sub->position.set = 1;
2131}
2132
2133static struct weston_subsurface *
2134subsurface_from_surface(struct weston_surface *surface)
2135{
2136 struct weston_subsurface *sub;
2137
2138 sub = weston_surface_to_subsurface(surface);
2139 if (sub)
2140 return sub;
2141
2142 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2143 if (sub->surface == surface)
2144 return sub;
2145
2146 return NULL;
2147}
2148
2149static struct weston_subsurface *
2150subsurface_sibling_check(struct weston_subsurface *sub,
2151 struct weston_surface *surface,
2152 const char *request)
2153{
2154 struct weston_subsurface *sibling;
2155
2156 sibling = subsurface_from_surface(surface);
2157
2158 if (!sibling) {
2159 wl_resource_post_error(sub->resource,
2160 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2161 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002162 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002163 return NULL;
2164 }
2165
2166 if (sibling->parent != sub->parent) {
2167 wl_resource_post_error(sub->resource,
2168 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2169 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002170 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002171 return NULL;
2172 }
2173
2174 return sibling;
2175}
2176
2177static void
2178subsurface_place_above(struct wl_client *client,
2179 struct wl_resource *resource,
2180 struct wl_resource *sibling_resource)
2181{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002182 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002183 struct weston_surface *surface =
2184 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002185 struct weston_subsurface *sibling;
2186
2187 if (!sub)
2188 return;
2189
2190 sibling = subsurface_sibling_check(sub, surface, "place_above");
2191 if (!sibling)
2192 return;
2193
2194 wl_list_remove(&sub->parent_link_pending);
2195 wl_list_insert(sibling->parent_link_pending.prev,
2196 &sub->parent_link_pending);
2197}
2198
2199static void
2200subsurface_place_below(struct wl_client *client,
2201 struct wl_resource *resource,
2202 struct wl_resource *sibling_resource)
2203{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002204 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002205 struct weston_surface *surface =
2206 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002207 struct weston_subsurface *sibling;
2208
2209 if (!sub)
2210 return;
2211
2212 sibling = subsurface_sibling_check(sub, surface, "place_below");
2213 if (!sibling)
2214 return;
2215
2216 wl_list_remove(&sub->parent_link_pending);
2217 wl_list_insert(&sibling->parent_link_pending,
2218 &sub->parent_link_pending);
2219}
2220
2221static void
2222subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2223{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002224 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002225
2226 if (sub)
2227 sub->synchronized = 1;
2228}
2229
2230static void
2231subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2232{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002233 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002234
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002235 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002236 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002237
2238 /* If sub became effectively desynchronized, flush. */
2239 if (!weston_subsurface_is_synchronized(sub))
2240 weston_subsurface_synchronized_commit(sub);
2241 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002242}
2243
2244static void
2245weston_subsurface_cache_init(struct weston_subsurface *sub)
2246{
2247 pixman_region32_init(&sub->cached.damage);
2248 pixman_region32_init(&sub->cached.opaque);
2249 pixman_region32_init(&sub->cached.input);
2250 wl_list_init(&sub->cached.frame_callback_list);
2251 sub->cached.buffer_ref.buffer = NULL;
2252}
2253
2254static void
2255weston_subsurface_cache_fini(struct weston_subsurface *sub)
2256{
2257 struct weston_frame_callback *cb, *tmp;
2258
2259 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002260 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002261
2262 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2263 pixman_region32_fini(&sub->cached.damage);
2264 pixman_region32_fini(&sub->cached.opaque);
2265 pixman_region32_fini(&sub->cached.input);
2266}
2267
2268static void
2269weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2270{
2271 wl_list_remove(&sub->parent_link);
2272 wl_list_remove(&sub->parent_link_pending);
2273 wl_list_remove(&sub->parent_destroy_listener.link);
2274 sub->parent = NULL;
2275}
2276
2277static void
2278weston_subsurface_destroy(struct weston_subsurface *sub);
2279
2280static void
2281subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2282{
2283 struct weston_subsurface *sub =
2284 container_of(listener, struct weston_subsurface,
2285 surface_destroy_listener);
2286 assert(data == &sub->surface->resource);
2287
2288 /* The protocol object (wl_resource) is left inert. */
2289 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002290 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002291
2292 weston_subsurface_destroy(sub);
2293}
2294
2295static void
2296subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2297{
2298 struct weston_subsurface *sub =
2299 container_of(listener, struct weston_subsurface,
2300 parent_destroy_listener);
2301 assert(data == &sub->parent->resource);
2302 assert(sub->surface != sub->parent);
2303
2304 if (weston_surface_is_mapped(sub->surface))
2305 weston_surface_unmap(sub->surface);
2306
2307 weston_subsurface_unlink_parent(sub);
2308}
2309
2310static void
2311subsurface_resource_destroy(struct wl_resource *resource)
2312{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002313 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002314
2315 if (sub)
2316 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002317}
2318
2319static void
2320subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2321{
2322 wl_resource_destroy(resource);
2323}
2324
2325static void
2326weston_subsurface_link_parent(struct weston_subsurface *sub,
2327 struct weston_surface *parent)
2328{
2329 sub->parent = parent;
2330 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002331 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002332 &sub->parent_destroy_listener);
2333
2334 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2335 wl_list_insert(&parent->subsurface_list_pending,
2336 &sub->parent_link_pending);
2337}
2338
2339static void
2340weston_subsurface_link_surface(struct weston_subsurface *sub,
2341 struct weston_surface *surface)
2342{
2343 sub->surface = surface;
2344 sub->surface_destroy_listener.notify =
2345 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002346 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002347 &sub->surface_destroy_listener);
2348}
2349
2350static void
2351weston_subsurface_destroy(struct weston_subsurface *sub)
2352{
2353 assert(sub->surface);
2354
2355 if (sub->resource) {
2356 assert(weston_surface_to_subsurface(sub->surface) == sub);
2357 assert(sub->parent_destroy_listener.notify ==
2358 subsurface_handle_parent_destroy);
2359
2360 weston_surface_set_transform_parent(sub->surface, NULL);
2361 if (sub->parent)
2362 weston_subsurface_unlink_parent(sub);
2363
2364 weston_subsurface_cache_fini(sub);
2365
2366 sub->surface->configure = NULL;
2367 sub->surface->configure_private = NULL;
2368 } else {
2369 /* the dummy weston_subsurface for the parent itself */
2370 assert(sub->parent_destroy_listener.notify == NULL);
2371 wl_list_remove(&sub->parent_link);
2372 wl_list_remove(&sub->parent_link_pending);
2373 }
2374
2375 wl_list_remove(&sub->surface_destroy_listener.link);
2376 free(sub);
2377}
2378
2379static const struct wl_subsurface_interface subsurface_implementation = {
2380 subsurface_destroy,
2381 subsurface_set_position,
2382 subsurface_place_above,
2383 subsurface_place_below,
2384 subsurface_set_sync,
2385 subsurface_set_desync
2386};
2387
2388static struct weston_subsurface *
2389weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2390 struct weston_surface *parent)
2391{
2392 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002393 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002394
2395 sub = calloc(1, sizeof *sub);
2396 if (!sub)
2397 return NULL;
2398
Jason Ekstranda85118c2013-06-27 20:17:02 -05002399 sub->resource =
2400 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002401 if (!sub->resource) {
2402 free(sub);
2403 return NULL;
2404 }
2405
Jason Ekstranda85118c2013-06-27 20:17:02 -05002406 wl_resource_set_implementation(sub->resource,
2407 &subsurface_implementation,
2408 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002409 weston_subsurface_link_surface(sub, surface);
2410 weston_subsurface_link_parent(sub, parent);
2411 weston_subsurface_cache_init(sub);
2412 sub->synchronized = 1;
2413 weston_surface_set_transform_parent(surface, parent);
2414
2415 return sub;
2416}
2417
2418/* Create a dummy subsurface for having the parent itself in its
2419 * sub-surface lists. Makes stacking order manipulation easy.
2420 */
2421static struct weston_subsurface *
2422weston_subsurface_create_for_parent(struct weston_surface *parent)
2423{
2424 struct weston_subsurface *sub;
2425
2426 sub = calloc(1, sizeof *sub);
2427 if (!sub)
2428 return NULL;
2429
2430 weston_subsurface_link_surface(sub, parent);
2431 sub->parent = parent;
2432 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2433 wl_list_insert(&parent->subsurface_list_pending,
2434 &sub->parent_link_pending);
2435
2436 return sub;
2437}
2438
2439static void
2440subcompositor_get_subsurface(struct wl_client *client,
2441 struct wl_resource *resource,
2442 uint32_t id,
2443 struct wl_resource *surface_resource,
2444 struct wl_resource *parent_resource)
2445{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002446 struct weston_surface *surface =
2447 wl_resource_get_user_data(surface_resource);
2448 struct weston_surface *parent =
2449 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002450 struct weston_subsurface *sub;
2451 static const char where[] = "get_subsurface: wl_subsurface@";
2452
2453 if (surface == parent) {
2454 wl_resource_post_error(resource,
2455 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2456 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002457 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002458 return;
2459 }
2460
2461 if (weston_surface_to_subsurface(surface)) {
2462 wl_resource_post_error(resource,
2463 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2464 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002465 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002466 return;
2467 }
2468
2469 if (surface->configure) {
2470 wl_resource_post_error(resource,
2471 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2472 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002473 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002474 return;
2475 }
2476
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002477 if (weston_surface_get_main_surface(parent) == surface) {
2478 wl_resource_post_error(resource,
2479 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2480 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002481 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002482 return;
2483 }
2484
Pekka Paalanene67858b2013-04-25 13:57:42 +03002485 /* make sure the parent is in its own list */
2486 if (wl_list_empty(&parent->subsurface_list)) {
2487 if (!weston_subsurface_create_for_parent(parent)) {
2488 wl_resource_post_no_memory(resource);
2489 return;
2490 }
2491 }
2492
2493 sub = weston_subsurface_create(id, surface, parent);
2494 if (!sub) {
2495 wl_resource_post_no_memory(resource);
2496 return;
2497 }
2498
2499 surface->configure = subsurface_configure;
2500 surface->configure_private = sub;
2501}
2502
2503static void
2504subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2505{
2506 wl_resource_destroy(resource);
2507}
2508
2509static const struct wl_subcompositor_interface subcompositor_interface = {
2510 subcompositor_destroy,
2511 subcompositor_get_subsurface
2512};
2513
2514static void
2515bind_subcompositor(struct wl_client *client,
2516 void *data, uint32_t version, uint32_t id)
2517{
2518 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002519 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002520
Jason Ekstranda85118c2013-06-27 20:17:02 -05002521 resource =
2522 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002523 if (resource == NULL) {
2524 wl_client_post_no_memory(client);
2525 return;
2526 }
2527 wl_resource_set_implementation(resource, &subcompositor_interface,
2528 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002529}
2530
2531static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002532weston_compositor_dpms(struct weston_compositor *compositor,
2533 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002534{
2535 struct weston_output *output;
2536
2537 wl_list_for_each(output, &compositor->output_list, link)
2538 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002539 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002540}
2541
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002542WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002543weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002544{
Neil Roberts8b62e202013-09-30 13:14:47 +01002545 uint32_t old_state = compositor->state;
2546
2547 /* The state needs to be changed before emitting the wake
2548 * signal because that may try to schedule a repaint which
2549 * will not work if the compositor is still sleeping */
2550 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2551
2552 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002553 case WESTON_COMPOSITOR_SLEEPING:
2554 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2555 /* fall through */
2556 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002557 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002558 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002559 /* fall through */
2560 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002561 wl_event_source_timer_update(compositor->idle_source,
2562 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002563 }
2564}
2565
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002566WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002567weston_compositor_offscreen(struct weston_compositor *compositor)
2568{
2569 switch (compositor->state) {
2570 case WESTON_COMPOSITOR_OFFSCREEN:
2571 return;
2572 case WESTON_COMPOSITOR_SLEEPING:
2573 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2574 /* fall through */
2575 default:
2576 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2577 wl_event_source_timer_update(compositor->idle_source, 0);
2578 }
2579}
2580
2581WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002582weston_compositor_sleep(struct weston_compositor *compositor)
2583{
2584 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2585 return;
2586
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002587 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002588 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2589 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2590}
2591
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002592static int
2593idle_handler(void *data)
2594{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002595 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002596
2597 if (compositor->idle_inhibit)
2598 return 1;
2599
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002600 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002601 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002602
2603 return 1;
2604}
2605
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002606WL_EXPORT void
2607weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2608{
2609 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002610 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002611 plane->x = x;
2612 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002613
2614 /* Init the link so that the call to wl_list_remove() when releasing
2615 * the plane without ever stacking doesn't lead to a crash */
2616 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002617}
2618
2619WL_EXPORT void
2620weston_plane_release(struct weston_plane *plane)
2621{
2622 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002623 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002624
2625 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002626}
2627
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002628WL_EXPORT void
2629weston_compositor_stack_plane(struct weston_compositor *ec,
2630 struct weston_plane *plane,
2631 struct weston_plane *above)
2632{
2633 if (above)
2634 wl_list_insert(above->link.prev, &plane->link);
2635 else
2636 wl_list_insert(&ec->plane_list, &plane->link);
2637}
2638
Casey Dahlin9074db52012-04-19 22:50:09 -04002639static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002640{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002641 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002642}
2643
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002644static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002645bind_output(struct wl_client *client,
2646 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002647{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002648 struct weston_output *output = data;
2649 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002650 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002651
Jason Ekstranda85118c2013-06-27 20:17:02 -05002652 resource = wl_resource_create(client, &wl_output_interface,
2653 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002654 if (resource == NULL) {
2655 wl_client_post_no_memory(client);
2656 return;
2657 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002658
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002659 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002660 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002661
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002662 wl_output_send_geometry(resource,
2663 output->x,
2664 output->y,
2665 output->mm_width,
2666 output->mm_height,
2667 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002668 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002669 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002670 if (version >= 2)
2671 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002672 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002673
2674 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002675 wl_output_send_mode(resource,
2676 mode->flags,
2677 mode->width,
2678 mode->height,
2679 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002680 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002681
2682 if (version >= 2)
2683 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002684}
2685
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002686WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002687weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002688{
Richard Hughes64ddde12013-05-01 21:52:10 +01002689 wl_signal_emit(&output->destroy_signal, output);
2690
Richard Hughesafe690c2013-05-02 10:10:04 +01002691 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002692 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002693 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002694 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002695
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002696 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002697}
2698
Scott Moreau1bad5db2012-08-18 01:04:05 -06002699static void
2700weston_output_compute_transform(struct weston_output *output)
2701{
2702 struct weston_matrix transform;
2703 int flip;
2704
2705 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002706 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002707
2708 switch(output->transform) {
2709 case WL_OUTPUT_TRANSFORM_FLIPPED:
2710 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2711 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2712 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002713 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002714 flip = -1;
2715 break;
2716 default:
2717 flip = 1;
2718 break;
2719 }
2720
2721 switch(output->transform) {
2722 case WL_OUTPUT_TRANSFORM_NORMAL:
2723 case WL_OUTPUT_TRANSFORM_FLIPPED:
2724 transform.d[0] = flip;
2725 transform.d[1] = 0;
2726 transform.d[4] = 0;
2727 transform.d[5] = 1;
2728 break;
2729 case WL_OUTPUT_TRANSFORM_90:
2730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2731 transform.d[0] = 0;
2732 transform.d[1] = -flip;
2733 transform.d[4] = 1;
2734 transform.d[5] = 0;
2735 break;
2736 case WL_OUTPUT_TRANSFORM_180:
2737 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2738 transform.d[0] = -flip;
2739 transform.d[1] = 0;
2740 transform.d[4] = 0;
2741 transform.d[5] = -1;
2742 break;
2743 case WL_OUTPUT_TRANSFORM_270:
2744 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2745 transform.d[0] = 0;
2746 transform.d[1] = flip;
2747 transform.d[4] = -1;
2748 transform.d[5] = 0;
2749 break;
2750 default:
2751 break;
2752 }
2753
2754 weston_matrix_multiply(&output->matrix, &transform);
2755}
2756
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002757WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002758weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002759{
Scott Moreau850ca422012-05-21 15:21:25 -06002760 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002761 struct weston_matrix camera;
2762 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002763
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002764 weston_matrix_init(&output->matrix);
2765 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002766 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2767 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002768
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002769 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002770 2.0 / (output->width + output->border.left + output->border.right),
2771 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2772
2773 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002774
Scott Moreauccbf29d2012-02-22 14:21:41 -07002775 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002776 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002777 weston_matrix_init(&camera);
2778 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002779 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002780 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002781 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002782 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002783 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002784 weston_matrix_multiply(&output->matrix, &modelview);
2785 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002786
Scott Moreauccbf29d2012-02-22 14:21:41 -07002787 output->dirty = 0;
2788}
2789
Scott Moreau1bad5db2012-08-18 01:04:05 -06002790static void
Alexander Larsson0b135062013-05-28 16:23:36 +02002791weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06002792{
2793 output->transform = transform;
2794
2795 switch (transform) {
2796 case WL_OUTPUT_TRANSFORM_90:
2797 case WL_OUTPUT_TRANSFORM_270:
2798 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2799 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2800 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02002801 output->width = output->current_mode->height;
2802 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002803 break;
2804 case WL_OUTPUT_TRANSFORM_NORMAL:
2805 case WL_OUTPUT_TRANSFORM_180:
2806 case WL_OUTPUT_TRANSFORM_FLIPPED:
2807 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02002808 output->width = output->current_mode->width;
2809 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002810 break;
2811 default:
2812 break;
2813 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06002814
Hardening57388e42013-09-18 23:56:36 +02002815 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02002816 output->width /= scale;
2817 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002818}
2819
Scott Moreauccbf29d2012-02-22 14:21:41 -07002820WL_EXPORT void
2821weston_output_move(struct weston_output *output, int x, int y)
2822{
2823 output->x = x;
2824 output->y = y;
2825
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002826 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002827 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002828 output->width,
2829 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002830}
2831
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002832WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002833weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02002834 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002835 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002836{
2837 output->compositor = c;
2838 output->x = x;
2839 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002840 output->border.top = 0;
2841 output->border.bottom = 0;
2842 output->border.left = 0;
2843 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02002844 output->mm_width = mm_width;
2845 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002846 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02002847 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002848
Alexander Larsson0b135062013-05-28 16:23:36 +02002849 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06002850 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002851
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002852 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002853 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002854
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002855 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01002856 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002857 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002858 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002859
Casey Dahlin58ba1372012-04-19 22:50:08 -04002860 output->id = ffs(~output->compositor->output_id_pool) - 1;
2861 output->compositor->output_id_pool |= 1 << output->id;
2862
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002863 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002864 wl_global_create(c->wl_display, &wl_output_interface, 2,
2865 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01002866 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002867}
2868
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002869WL_EXPORT void
2870weston_output_transform_coordinate(struct weston_output *output,
2871 int device_x, int device_y,
2872 wl_fixed_t *x, wl_fixed_t *y)
2873{
2874 wl_fixed_t tx, ty;
2875 wl_fixed_t width, height;
2876
Hardeningff39efa2013-09-18 23:56:35 +02002877 width = wl_fixed_from_int(output->width * output->current_scale - 1);
2878 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002879
2880 switch(output->transform) {
2881 case WL_OUTPUT_TRANSFORM_NORMAL:
2882 default:
2883 tx = wl_fixed_from_int(device_x);
2884 ty = wl_fixed_from_int(device_y);
2885 break;
2886 case WL_OUTPUT_TRANSFORM_90:
2887 tx = wl_fixed_from_int(device_y);
2888 ty = height - wl_fixed_from_int(device_x);
2889 break;
2890 case WL_OUTPUT_TRANSFORM_180:
2891 tx = width - wl_fixed_from_int(device_x);
2892 ty = height - wl_fixed_from_int(device_y);
2893 break;
2894 case WL_OUTPUT_TRANSFORM_270:
2895 tx = width - wl_fixed_from_int(device_y);
2896 ty = wl_fixed_from_int(device_x);
2897 break;
2898 case WL_OUTPUT_TRANSFORM_FLIPPED:
2899 tx = width - wl_fixed_from_int(device_x);
2900 ty = wl_fixed_from_int(device_y);
2901 break;
2902 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2903 tx = width - wl_fixed_from_int(device_y);
2904 ty = height - wl_fixed_from_int(device_x);
2905 break;
2906 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2907 tx = wl_fixed_from_int(device_x);
2908 ty = height - wl_fixed_from_int(device_y);
2909 break;
2910 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2911 tx = wl_fixed_from_int(device_y);
2912 ty = wl_fixed_from_int(device_x);
2913 break;
2914 }
2915
Hardeningff39efa2013-09-18 23:56:35 +02002916 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
2917 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07002918}
2919
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002920static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002921compositor_bind(struct wl_client *client,
2922 void *data, uint32_t version, uint32_t id)
2923{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002924 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002925 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002926
Jason Ekstranda85118c2013-06-27 20:17:02 -05002927 resource = wl_resource_create(client, &wl_compositor_interface,
2928 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002929 if (resource == NULL) {
2930 wl_client_post_no_memory(client);
2931 return;
2932 }
2933
2934 wl_resource_set_implementation(resource, &compositor_interface,
2935 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05002936}
2937
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002938static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002939log_uname(void)
2940{
2941 struct utsname usys;
2942
2943 uname(&usys);
2944
2945 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2946 usys.version, usys.machine);
2947}
2948
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002949WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02002950weston_environment_get_fd(const char *env)
2951{
2952 char *e, *end;
2953 int fd, flags;
2954
2955 e = getenv(env);
2956 if (!e)
2957 return -1;
2958 fd = strtol(e, &end, 0);
2959 if (*end != '\0')
2960 return -1;
2961
2962 flags = fcntl(fd, F_GETFD);
2963 if (flags == -1)
2964 return -1;
2965
2966 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
2967 unsetenv(env);
2968
2969 return fd;
2970}
2971
2972WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002973weston_compositor_init(struct weston_compositor *ec,
2974 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05002975 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002976 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002977{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002978 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002979 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04002980 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07002981
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04002982 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002983 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002984 wl_signal_init(&ec->destroy_signal);
2985 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03002986 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002987 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002988 wl_signal_init(&ec->idle_signal);
2989 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002990 wl_signal_init(&ec->show_input_panel_signal);
2991 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02002992 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002993 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01002994 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07002995 wl_signal_init(&ec->session_signal);
2996 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002997
Casey Dahlin58ba1372012-04-19 22:50:08 -04002998 ec->output_id_pool = 0;
2999
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003000 if (!wl_global_create(display, &wl_compositor_interface, 3,
3001 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003002 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003003
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003004 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3005 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003006 return -1;
3007
Daniel Stone725c2c32012-06-22 14:04:36 +01003008 wl_list_init(&ec->surface_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003009 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003010 wl_list_init(&ec->layer_list);
3011 wl_list_init(&ec->seat_list);
3012 wl_list_init(&ec->output_list);
3013 wl_list_init(&ec->key_binding_list);
3014 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003015 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003016 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003017 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003018
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003019 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003020 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003021
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003022 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3023 weston_config_section_get_string(s, "keymap_rules",
3024 (char **) &xkb_names.rules, NULL);
3025 weston_config_section_get_string(s, "keymap_model",
3026 (char **) &xkb_names.model, NULL);
3027 weston_config_section_get_string(s, "keymap_layout",
3028 (char **) &xkb_names.layout, NULL);
3029 weston_config_section_get_string(s, "keymap_variant",
3030 (char **) &xkb_names.variant, NULL);
3031 weston_config_section_get_string(s, "keymap_options",
3032 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003033
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003034 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3035 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003036
3037 ec->ping_handler = NULL;
3038
3039 screenshooter_create(ec);
3040 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003041 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003042
3043 wl_data_device_manager_init(ec->wl_display);
3044
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003045 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003046
Daniel Stone725c2c32012-06-22 14:04:36 +01003047 loop = wl_display_get_event_loop(ec->wl_display);
3048 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3049 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3050
3051 ec->input_loop = wl_event_loop_create();
3052
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003053 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3054 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3055
3056 weston_compositor_schedule_repaint(ec);
3057
Daniel Stone725c2c32012-06-22 14:04:36 +01003058 return 0;
3059}
3060
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003061WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003062weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003063{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003064 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003065
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003066 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003067 if (ec->input_loop_source)
3068 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003069
Matt Roper361d2ad2011-08-29 13:52:23 -07003070 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003071 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003072 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003073
Daniel Stone325fc2d2012-05-30 16:31:58 +01003074 weston_binding_list_destroy_all(&ec->key_binding_list);
3075 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003076 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003077 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003078 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003079
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003080 weston_plane_release(&ec->primary_plane);
3081
Jonas Ådahlc97af922012-03-28 22:36:09 +02003082 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003083
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003084 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003085}
3086
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003087WL_EXPORT void
3088weston_version(int *major, int *minor, int *micro)
3089{
3090 *major = WESTON_VERSION_MAJOR;
3091 *minor = WESTON_VERSION_MINOR;
3092 *micro = WESTON_VERSION_MICRO;
3093}
3094
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003095static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003096 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003097 const char *desc;
3098} capability_strings[] = {
3099 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003100 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003101};
3102
3103static void
3104weston_compositor_log_capabilities(struct weston_compositor *compositor)
3105{
3106 unsigned i;
3107 int yes;
3108
3109 weston_log("Compositor capabilities:\n");
3110 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3111 yes = compositor->capabilities & capability_strings[i].bit;
3112 weston_log_continue(STAMP_SPACE "%s %s\n",
3113 capability_strings[i].desc,
3114 yes ? "yes" : "no");
3115 }
3116}
3117
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003118static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003119{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003120 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003121
Martin Minarik6d118362012-06-07 18:01:59 +02003122 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003123 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003124
3125 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003126}
3127
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003128#ifdef HAVE_LIBUNWIND
3129
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003130static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003131print_backtrace(void)
3132{
3133 unw_cursor_t cursor;
3134 unw_context_t context;
3135 unw_word_t off;
3136 unw_proc_info_t pip;
3137 int ret, i = 0;
3138 char procname[256];
3139 const char *filename;
3140 Dl_info dlinfo;
3141
3142 pip.unwind_info = NULL;
3143 ret = unw_getcontext(&context);
3144 if (ret) {
3145 weston_log("unw_getcontext: %d\n", ret);
3146 return;
3147 }
3148
3149 ret = unw_init_local(&cursor, &context);
3150 if (ret) {
3151 weston_log("unw_init_local: %d\n", ret);
3152 return;
3153 }
3154
3155 ret = unw_step(&cursor);
3156 while (ret > 0) {
3157 ret = unw_get_proc_info(&cursor, &pip);
3158 if (ret) {
3159 weston_log("unw_get_proc_info: %d\n", ret);
3160 break;
3161 }
3162
3163 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3164 if (ret && ret != -UNW_ENOMEM) {
3165 if (ret != -UNW_EUNSPEC)
3166 weston_log("unw_get_proc_name: %d\n", ret);
3167 procname[0] = '?';
3168 procname[1] = 0;
3169 }
3170
3171 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3172 *dlinfo.dli_fname)
3173 filename = dlinfo.dli_fname;
3174 else
3175 filename = "?";
3176
3177 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3178 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3179
3180 ret = unw_step(&cursor);
3181 if (ret < 0)
3182 weston_log("unw_step: %d\n", ret);
3183 }
3184}
3185
3186#else
3187
3188static void
3189print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003190{
3191 void *buffer[32];
3192 int i, count;
3193 Dl_info info;
3194
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003195 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3196 for (i = 0; i < count; i++) {
3197 dladdr(buffer[i], &info);
3198 weston_log(" [%016lx] %s (%s)\n",
3199 (long) buffer[i],
3200 info.dli_sname ? info.dli_sname : "--",
3201 info.dli_fname);
3202 }
3203}
3204
3205#endif
3206
3207static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003208on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003209{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003210 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003211 * then call the backend restore function, which will switch
3212 * back to the vt we launched from or ungrab X etc and then
3213 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003214 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003215 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003216 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003217
Peter Maatmane5b42e42013-03-27 22:38:53 +01003218 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003219
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003220 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003221
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003222 segv_compositor->restore(segv_compositor);
3223
3224 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003225}
3226
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003227WL_EXPORT void *
3228weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003229{
3230 char path[PATH_MAX];
3231 void *module, *init;
3232
3233 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003234 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003235 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003236 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003237
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003238 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3239 if (module) {
3240 weston_log("Module '%s' already loaded\n", path);
3241 dlclose(module);
3242 return NULL;
3243 }
3244
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003245 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003246 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003247 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003248 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003249 return NULL;
3250 }
3251
3252 init = dlsym(module, entrypoint);
3253 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003254 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003255 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003256 return NULL;
3257 }
3258
3259 return init;
3260}
3261
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003262static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003263load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003264 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003265{
3266 const char *p, *end;
3267 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003268 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003269 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003270
3271 if (modules == NULL)
3272 return 0;
3273
3274 p = modules;
3275 while (*p) {
3276 end = strchrnul(p, ',');
3277 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003278 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003279 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003280 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003281 p = end;
3282 while (*p == ',')
3283 p++;
3284
3285 }
3286
3287 return 0;
3288}
3289
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003290static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003291 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3292
3293static const char xdg_wrong_message[] =
3294 "fatal: environment variable XDG_RUNTIME_DIR\n"
3295 "is set to \"%s\", which is not a directory.\n";
3296
3297static const char xdg_wrong_mode_message[] =
3298 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3299 "correctly. Unix access mode must be 0700 but is %o,\n"
3300 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003301 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003302
3303static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003304 "Refer to your distribution on how to get it, or\n"
3305 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3306 "on how to implement it.\n";
3307
Martin Minarik37032f82012-06-18 20:15:18 +02003308static void
3309verify_xdg_runtime_dir(void)
3310{
3311 char *dir = getenv("XDG_RUNTIME_DIR");
3312 struct stat s;
3313
3314 if (!dir) {
3315 weston_log(xdg_error_message);
3316 weston_log_continue(xdg_detail_message);
3317 exit(EXIT_FAILURE);
3318 }
3319
3320 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3321 weston_log(xdg_wrong_message, dir);
3322 weston_log_continue(xdg_detail_message);
3323 exit(EXIT_FAILURE);
3324 }
3325
3326 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3327 weston_log(xdg_wrong_mode_message,
3328 dir, s.st_mode & 0777, s.st_uid);
3329 weston_log_continue(xdg_detail_message);
3330 }
3331}
3332
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003333static int
3334usage(int error_code)
3335{
3336 fprintf(stderr,
3337 "Usage: weston [OPTIONS]\n\n"
3338 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3339 "Weston supports multiple backends, and depending on which backend is in use\n"
3340 "different options will be accepted.\n\n"
3341
3342
3343 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003344 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003345 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003346 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3347 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003348 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003349 " -S, --socket=NAME\tName of socket to listen on\n"
3350 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003351 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003352 " --log==FILE\t\tLog to the given file\n"
3353 " -h, --help\t\tThis help message\n\n");
3354
3355 fprintf(stderr,
3356 "Options for drm-backend.so:\n\n"
3357 " --connector=ID\tBring up only this connector\n"
3358 " --seat=SEAT\t\tThe seat that weston should run on\n"
3359 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003360 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003361 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3362
3363 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003364 "Options for fbdev-backend.so:\n\n"
3365 " --tty=TTY\t\tThe tty to use\n"
3366 " --device=DEVICE\tThe framebuffer device to use\n\n");
3367
3368 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003369 "Options for x11-backend.so:\n\n"
3370 " --width=WIDTH\t\tWidth of X window\n"
3371 " --height=HEIGHT\tHeight of X window\n"
3372 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003373 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003374 " --output-count=COUNT\tCreate multiple outputs\n"
3375 " --no-input\t\tDont create input devices\n\n");
3376
3377 fprintf(stderr,
3378 "Options for wayland-backend.so:\n\n"
3379 " --width=WIDTH\t\tWidth of Wayland surface\n"
3380 " --height=HEIGHT\tHeight of Wayland surface\n"
3381 " --display=DISPLAY\tWayland display to connect to\n\n");
3382
Pekka Paalanene31e0532013-05-22 18:03:07 +03003383#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3384 fprintf(stderr,
3385 "Options for rpi-backend.so:\n\n"
3386 " --tty=TTY\t\tThe tty to use\n"
3387 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3388 " --transform=TR\tThe output transformation, TR is one of:\n"
3389 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3390 "\n");
3391#endif
3392
Hardeningc077a842013-07-08 00:51:35 +02003393#if defined(BUILD_RDP_COMPOSITOR)
3394 fprintf(stderr,
3395 "Options for rdp-backend.so:\n\n"
3396 " --width=WIDTH\t\tWidth of desktop\n"
3397 " --height=HEIGHT\tHeight of desktop\n"
3398 " --extra-modes=MODES\t\n"
3399 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3400 " --address=ADDR\tThe address to bind\n"
3401 " --port=PORT\tThe port to listen on\n"
3402 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3403 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3404 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3405 "\n");
3406#endif
3407
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003408 exit(error_code);
3409}
3410
Peter Maatmane5b42e42013-03-27 22:38:53 +01003411static void
3412catch_signals(void)
3413{
3414 struct sigaction action;
3415
3416 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3417 action.sa_sigaction = on_caught_signal;
3418 sigemptyset(&action.sa_mask);
3419 sigaction(SIGSEGV, &action, NULL);
3420 sigaction(SIGABRT, &action, NULL);
3421}
3422
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003423int main(int argc, char *argv[])
3424{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003425 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003426 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003427 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003428 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003429 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003430 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003431 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003432 int *argc, char *argv[],
3433 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003434 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003435 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003436 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003437 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003438 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003439 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003440 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003441 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003442 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003443 struct weston_config *config;
3444 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003445
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003446 const struct weston_option core_options[] = {
3447 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003448 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003449 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3450 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003451 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003452 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003453 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003454 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003455 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003456
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003457 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003458
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003459 if (help)
3460 usage(EXIT_SUCCESS);
3461
Scott Moreau12245142012-08-29 15:15:58 -06003462 if (version) {
3463 printf(PACKAGE_STRING "\n");
3464 return EXIT_SUCCESS;
3465 }
3466
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003467 weston_log_file_open(log);
3468
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003469 weston_log("%s\n"
3470 STAMP_SPACE "%s\n"
3471 STAMP_SPACE "Bug reports to: %s\n"
3472 STAMP_SPACE "Build: %s\n",
3473 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003474 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003475 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003476
Martin Minarik37032f82012-06-18 20:15:18 +02003477 verify_xdg_runtime_dir();
3478
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003479 display = wl_display_create();
3480
Tiago Vignatti2116b892011-08-08 05:52:59 -07003481 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003482 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3483 display);
3484 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3485 display);
3486 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3487 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003488
3489 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003490 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3491 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003492
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003493 if (!backend) {
3494 if (getenv("WAYLAND_DISPLAY"))
3495 backend = "wayland-backend.so";
3496 else if (getenv("DISPLAY"))
3497 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003498 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003499 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003500 }
3501
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003502 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003503 if (config != NULL) {
3504 weston_log("Using config file '%s'\n",
3505 weston_config_get_full_path(config));
3506 } else {
3507 weston_log("Starting with no config file.\n");
3508 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003509 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003510 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003511
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003512 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003513 if (!backend_init)
3514 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003515
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003516 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003517 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003518 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003519 exit(EXIT_FAILURE);
3520 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003521
Peter Maatmane5b42e42013-03-27 22:38:53 +01003522 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003523 segv_compositor = ec;
3524
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003525 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003526
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003527 setenv("WAYLAND_DISPLAY", socket_name, 1);
3528
Jason Ekstranda985da42013-08-22 17:28:03 -05003529 if (!shell)
3530 weston_config_section_get_string(section, "shell", &shell,
3531 "desktop-shell.so");
3532 if (load_modules(ec, shell, &argc, argv) < 0)
3533 goto out;
3534
Ossama Othmana50e6e42013-05-14 09:48:26 -07003535 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003536 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003537 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003538 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003539
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003540 for (i = 1; i < argc; i++)
3541 weston_log("fatal: unhandled option: %s\n", argv[i]);
3542 if (argc > 1) {
3543 ret = EXIT_FAILURE;
3544 goto out;
3545 }
3546
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003547 weston_compositor_log_capabilities(ec);
3548
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003549 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003550 weston_log("fatal: failed to add socket: %m\n");
3551 ret = EXIT_FAILURE;
3552 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003553 }
3554
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003555 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003556
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003557 wl_display_run(display);
3558
3559 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003560 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003561 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003562
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003563 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003564
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003565 for (i = ARRAY_LENGTH(signals); i;)
3566 wl_event_source_remove(signals[--i]);
3567
Daniel Stone855028f2012-05-01 20:37:10 +01003568 weston_compositor_xkb_destroy(ec);
3569
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003570 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003571 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003572
Martin Minarik19e6f262012-06-07 13:08:46 +02003573 weston_log_file_close();
3574
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003575 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003576}