blob: 11f582e7113b291f83a073cdd441d27d23249d7c [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
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
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
Jason Ekstranda7af7042013-10-12 22:38:11 -0500350WL_EXPORT struct weston_view *
351weston_view_create(struct weston_surface *surface)
352{
353 struct weston_view *view;
354
355 view = calloc(1, sizeof *view);
356 if (view == NULL)
357 return NULL;
358
359 view->surface = surface;
360
361 if (surface->compositor->renderer->create_view &&
362 surface->compositor->renderer->create_view(view) < 0) {
363 free(view);
364 return NULL;
365 }
366
367 /* Assign to surface */
368 wl_list_insert(&surface->views, &view->surface_link);
369
370 wl_signal_init(&view->destroy_signal);
371 wl_list_init(&view->link);
372 wl_list_init(&view->layer_link);
373
374 view->plane = &surface->compositor->primary_plane;
375
376 pixman_region32_init(&view->clip);
377
378 view->alpha = 1.0;
379 pixman_region32_init(&view->transform.opaque);
380
381 wl_list_init(&view->geometry.transformation_list);
382 wl_list_insert(&view->geometry.transformation_list,
383 &view->transform.position.link);
384 weston_matrix_init(&view->transform.position.matrix);
385 wl_list_init(&view->geometry.child_list);
386 pixman_region32_init(&view->transform.boundingbox);
387 view->transform.dirty = 1;
388
389 view->output = NULL;
390
391 return view;
392}
393
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500394WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500395weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500396{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500397 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400398
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200399 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400400 if (surface == NULL)
401 return NULL;
402
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500403 wl_signal_init(&surface->destroy_signal);
404
405 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500406
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500407 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200408 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400409
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100410 if (compositor->renderer->create_surface(surface) < 0) {
411 free(surface);
412 return NULL;
413 }
414
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200415 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200416 surface->buffer_scale = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200417 surface->pending.buffer_transform = surface->buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +0200418 surface->pending.buffer_scale = surface->buffer_scale;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400419 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100420 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200421
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400422 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500423 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300424 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400425
Jason Ekstranda7af7042013-10-12 22:38:11 -0500426 wl_list_init(&surface->views);
427
428 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500429
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300430 surface->pending.buffer_destroy_listener.notify =
431 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300432 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300433 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300434 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300435 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300436
Pekka Paalanene67858b2013-04-25 13:57:42 +0300437 wl_list_init(&surface->subsurface_list);
438 wl_list_init(&surface->subsurface_list_pending);
439
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400440 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500441}
442
Alex Wu8811bf92012-02-28 18:07:54 +0800443WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500444weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200445 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500446{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100447 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500448}
449
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400450WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500451weston_view_to_global_float(struct weston_view *view,
452 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200453{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500454 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200455 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
456
Jason Ekstranda7af7042013-10-12 22:38:11 -0500457 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200458
459 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200460 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700461 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200462 v.f[3]);
463 *x = 0;
464 *y = 0;
465 return;
466 }
467
468 *x = v.f[0] / v.f[3];
469 *y = v.f[1] / v.f[3];
470 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500471 *x = sx + view->geometry.x;
472 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200473 }
474}
475
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500476WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200477weston_transformed_coord(int width, int height,
478 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200479 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200480 float sx, float sy, float *bx, float *by)
481{
482 switch (transform) {
483 case WL_OUTPUT_TRANSFORM_NORMAL:
484 default:
485 *bx = sx;
486 *by = sy;
487 break;
488 case WL_OUTPUT_TRANSFORM_FLIPPED:
489 *bx = width - sx;
490 *by = sy;
491 break;
492 case WL_OUTPUT_TRANSFORM_90:
493 *bx = height - sy;
494 *by = sx;
495 break;
496 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
497 *bx = height - sy;
498 *by = width - sx;
499 break;
500 case WL_OUTPUT_TRANSFORM_180:
501 *bx = width - sx;
502 *by = height - sy;
503 break;
504 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
505 *bx = sx;
506 *by = height - sy;
507 break;
508 case WL_OUTPUT_TRANSFORM_270:
509 *bx = sy;
510 *by = width - sx;
511 break;
512 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
513 *bx = sy;
514 *by = sx;
515 break;
516 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200517
518 *bx *= scale;
519 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520}
521
522WL_EXPORT pixman_box32_t
523weston_transformed_rect(int width, int height,
524 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200525 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200526 pixman_box32_t rect)
527{
528 float x1, x2, y1, y2;
529
530 pixman_box32_t ret;
531
Alexander Larsson4ea95522013-05-22 14:41:37 +0200532 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200533 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200534 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200535 rect.x2, rect.y2, &x2, &y2);
536
537 if (x1 <= x2) {
538 ret.x1 = x1;
539 ret.x2 = x2;
540 } else {
541 ret.x1 = x2;
542 ret.x2 = x1;
543 }
544
545 if (y1 <= y2) {
546 ret.y1 = y1;
547 ret.y2 = y2;
548 } else {
549 ret.y1 = y2;
550 ret.y2 = y1;
551 }
552
553 return ret;
554}
555
556WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200557weston_surface_to_buffer_float(struct weston_surface *surface,
558 float sx, float sy, float *bx, float *by)
559{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 weston_transformed_coord(surface->width,
561 surface->height,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200562 surface->buffer_transform,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200563 surface->buffer_scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200564 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200565}
566
Alexander Larsson4ea95522013-05-22 14:41:37 +0200567WL_EXPORT void
568weston_surface_to_buffer(struct weston_surface *surface,
569 int sx, int sy, int *bx, int *by)
570{
571 float bxf, byf;
572
Jason Ekstranda7af7042013-10-12 22:38:11 -0500573 weston_transformed_coord(surface->width,
574 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200575 surface->buffer_transform,
576 surface->buffer_scale,
577 sx, sy, &bxf, &byf);
578 *bx = floorf(bxf);
579 *by = floorf(byf);
580}
581
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200582WL_EXPORT pixman_box32_t
583weston_surface_to_buffer_rect(struct weston_surface *surface,
584 pixman_box32_t rect)
585{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500586 return weston_transformed_rect(surface->width,
587 surface->height,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200588 surface->buffer_transform,
589 surface->buffer_scale,
590 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200591}
592
593WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500594weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400595 struct weston_plane *plane)
596{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500597 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400598 return;
599
Jason Ekstranda7af7042013-10-12 22:38:11 -0500600 weston_view_damage_below(view);
601 view->plane = plane;
602 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400603}
604
605WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500606weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200607{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500608 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200609
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500610 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500611 pixman_region32_subtract(&damage, &view->transform.boundingbox,
612 &view->clip);
613 pixman_region32_union(&view->plane->damage,
614 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500615 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200616}
617
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400618static void
619weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
620{
621 uint32_t different = es->output_mask ^ mask;
622 uint32_t entered = mask & different;
623 uint32_t left = es->output_mask & different;
624 struct weston_output *output;
625 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500626 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400627
628 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500629 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400630 return;
631 if (different == 0)
632 return;
633
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500634 client = wl_resource_get_client(es->resource);
635
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400636 wl_list_for_each(output, &es->compositor->output_list, link) {
637 if (1 << output->id & different)
638 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500639 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400640 client);
641 if (resource == NULL)
642 continue;
643 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500644 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400645 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500646 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400647 }
648}
649
650static void
651weston_surface_assign_output(struct weston_surface *es)
652{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 struct weston_output *new_output;
654 struct weston_view *view;
655 pixman_region32_t region;
656 uint32_t max, area, mask;
657 pixman_box32_t *e;
658
659 new_output = NULL;
660 max = 0;
661 mask = 0;
662 pixman_region32_init(&region);
663 wl_list_for_each(view, &es->views, surface_link) {
664 if (!view->output)
665 continue;
666
667 pixman_region32_intersect(&region, &view->transform.boundingbox,
668 &view->output->region);
669
670 e = pixman_region32_extents(&region);
671 area = (e->x2 - e->x1) * (e->y2 - e->y1);
672
673 mask |= view->output_mask;
674
675 if (area >= max) {
676 new_output = view->output;
677 max = area;
678 }
679 }
680 pixman_region32_fini(&region);
681
682 es->output = new_output;
683 weston_surface_update_output_mask(es, mask);
684}
685
686static void
687weston_view_assign_output(struct weston_view *ev)
688{
689 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400690 struct weston_output *output, *new_output;
691 pixman_region32_t region;
692 uint32_t max, area, mask;
693 pixman_box32_t *e;
694
695 new_output = NULL;
696 max = 0;
697 mask = 0;
698 pixman_region32_init(&region);
699 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500700 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400701 &output->region);
702
703 e = pixman_region32_extents(&region);
704 area = (e->x2 - e->x1) * (e->y2 - e->y1);
705
706 if (area > 0)
707 mask |= 1 << output->id;
708
709 if (area >= max) {
710 new_output = output;
711 max = area;
712 }
713 }
714 pixman_region32_fini(&region);
715
Jason Ekstranda7af7042013-10-12 22:38:11 -0500716 ev->output = new_output;
717 ev->output_mask = mask;
718
719 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400720}
721
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200722static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500723view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
724 int32_t width, int32_t height,
725 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200726{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200727 float min_x = HUGE_VALF, min_y = HUGE_VALF;
728 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200729 int32_t s[4][2] = {
730 { sx, sy },
731 { sx, sy + height },
732 { sx + width, sy },
733 { sx + width, sy + height }
734 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200735 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200736 int i;
737
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300738 if (width == 0 || height == 0) {
739 /* avoid rounding empty bbox to 1x1 */
740 pixman_region32_init(bbox);
741 return;
742 }
743
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200744 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200745 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500746 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200747 if (x < min_x)
748 min_x = x;
749 if (x > max_x)
750 max_x = x;
751 if (y < min_y)
752 min_y = y;
753 if (y > max_y)
754 max_y = y;
755 }
756
Pekka Paalanen219b9822012-02-08 15:38:37 +0200757 int_x = floorf(min_x);
758 int_y = floorf(min_y);
759 pixman_region32_init_rect(bbox, int_x, int_y,
760 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200761}
762
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200763static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500764weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200765{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500766 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200767
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200768 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500769 view->geometry.x = roundf(view->geometry.x);
770 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200771
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500772 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500773 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
774 view->transform.position.matrix.d[12] = view->geometry.x;
775 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500776
Jason Ekstranda7af7042013-10-12 22:38:11 -0500777 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500778
Jason Ekstranda7af7042013-10-12 22:38:11 -0500779 view->transform.inverse = view->transform.position.matrix;
780 view->transform.inverse.d[12] = -view->geometry.x;
781 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500782
Jason Ekstranda7af7042013-10-12 22:38:11 -0500783 pixman_region32_init_rect(&view->transform.boundingbox,
784 view->geometry.x,
785 view->geometry.y,
786 view->geometry.width,
787 view->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500788
Jason Ekstranda7af7042013-10-12 22:38:11 -0500789 if (view->alpha == 1.0) {
790 pixman_region32_copy(&view->transform.opaque,
791 &view->surface->opaque);
792 pixman_region32_translate(&view->transform.opaque,
793 view->geometry.x,
794 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500795 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200796}
797
798static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500799weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200800{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500801 struct weston_view *parent = view->geometry.parent;
802 struct weston_matrix *matrix = &view->transform.matrix;
803 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200804 struct weston_transform *tform;
805
Jason Ekstranda7af7042013-10-12 22:38:11 -0500806 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200807
808 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500809 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
810 view->transform.position.matrix.d[12] = view->geometry.x;
811 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200812
813 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500814 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200815 weston_matrix_multiply(matrix, &tform->matrix);
816
Pekka Paalanen483243f2013-03-08 14:56:50 +0200817 if (parent)
818 weston_matrix_multiply(matrix, &parent->transform.matrix);
819
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200820 if (weston_matrix_invert(inverse, matrix) < 0) {
821 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500822 weston_log("error: weston_view %p"
823 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200824 return -1;
825 }
826
Jason Ekstranda7af7042013-10-12 22:38:11 -0500827 view_compute_bbox(view, 0, 0, view->geometry.width,
828 view->geometry.height,
829 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500830
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200831 return 0;
832}
833
834WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500835weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200836{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500837 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200838
Jason Ekstranda7af7042013-10-12 22:38:11 -0500839 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200840 return;
841
Pekka Paalanen483243f2013-03-08 14:56:50 +0200842 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500843 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200844
Jason Ekstranda7af7042013-10-12 22:38:11 -0500845 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200846
Jason Ekstranda7af7042013-10-12 22:38:11 -0500847 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200848
Jason Ekstranda7af7042013-10-12 22:38:11 -0500849 pixman_region32_fini(&view->transform.boundingbox);
850 pixman_region32_fini(&view->transform.opaque);
851 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500852
Pekka Paalanencd403622012-01-25 13:37:39 +0200853 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500854 if (view->geometry.transformation_list.next ==
855 &view->transform.position.link &&
856 view->geometry.transformation_list.prev ==
857 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200858 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500859 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200860 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500861 if (weston_view_update_transform_enable(view) < 0)
862 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200863 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200864
Jason Ekstranda7af7042013-10-12 22:38:11 -0500865 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200866
Jason Ekstranda7af7042013-10-12 22:38:11 -0500867 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300868
Jason Ekstranda7af7042013-10-12 22:38:11 -0500869 wl_signal_emit(&view->surface->compositor->transform_signal,
870 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200871}
872
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200873WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200875{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200877
878 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 * The invariant: if view->geometry.dirty, then all views
880 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200881 * Corollary: if not parent->geometry.dirty, then all ancestors
882 * are not dirty.
883 */
884
Jason Ekstranda7af7042013-10-12 22:38:11 -0500885 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200886 return;
887
Jason Ekstranda7af7042013-10-12 22:38:11 -0500888 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200889
Jason Ekstranda7af7042013-10-12 22:38:11 -0500890 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +0200891 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500892 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200893}
894
895WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896weston_view_to_global_fixed(struct weston_view *view,
897 wl_fixed_t vx, wl_fixed_t vy,
898 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100899{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200900 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100901
Jason Ekstranda7af7042013-10-12 22:38:11 -0500902 weston_view_to_global_float(view,
903 wl_fixed_to_double(vx),
904 wl_fixed_to_double(vy),
905 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100906 *x = wl_fixed_from_double(xf);
907 *y = wl_fixed_from_double(yf);
908}
909
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400910WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911weston_view_from_global_float(struct weston_view *view,
912 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200913{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500914 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200915 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
916
Jason Ekstranda7af7042013-10-12 22:38:11 -0500917 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200918
919 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200920 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -0500921 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200922 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500923 *vx = 0;
924 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200925 return;
926 }
927
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 *vx = v.f[0] / v.f[3];
929 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200930 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 *vx = x - view->geometry.x;
932 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200933 }
934}
935
936WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937weston_view_from_global_fixed(struct weston_view *view,
938 wl_fixed_t x, wl_fixed_t y,
939 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +0100940{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500941 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100942
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 weston_view_from_global_float(view,
944 wl_fixed_to_double(x),
945 wl_fixed_to_double(y),
946 &vxf, &vyf);
947 *vx = wl_fixed_from_double(vxf);
948 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100949}
950
951WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952weston_view_from_global(struct weston_view *view,
953 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200954{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200956
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 weston_view_from_global_float(view, x, y, &vxf, &vyf);
958 *vx = floorf(vxf);
959 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200960}
961
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400962WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400963weston_surface_schedule_repaint(struct weston_surface *surface)
964{
965 struct weston_output *output;
966
967 wl_list_for_each(output, &surface->compositor->output_list, link)
968 if (surface->output_mask & (1 << output->id))
969 weston_output_schedule_repaint(output);
970}
971
972WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500973weston_view_schedule_repaint(struct weston_view *view)
974{
975 struct weston_output *output;
976
977 wl_list_for_each(output, &view->surface->compositor->output_list, link)
978 if (view->output_mask & (1 << output->id))
979 weston_output_schedule_repaint(output);
980}
981
982WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500983weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500984{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400985 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 0, 0, surface->width,
987 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200988
Kristian Høgsberg98238702012-08-03 16:29:12 -0400989 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500990}
991
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400992WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500993weston_view_configure(struct weston_view *view,
994 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400995{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500996 view->geometry.x = x;
997 view->geometry.y = y;
998 view->geometry.width = width;
999 view->geometry.height = height;
1000 weston_view_geometry_dirty(view);
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001001}
1002
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001003WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001004weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001005{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 view->geometry.x = x;
1007 view->geometry.y = y;
1008 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001009}
1010
Pekka Paalanen483243f2013-03-08 14:56:50 +02001011static void
1012transform_parent_handle_parent_destroy(struct wl_listener *listener,
1013 void *data)
1014{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 struct weston_view *view =
1016 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001017 geometry.parent_destroy_listener);
1018
Jason Ekstranda7af7042013-10-12 22:38:11 -05001019 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001020}
1021
1022WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023weston_view_set_transform_parent(struct weston_view *view,
1024 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001025{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 if (view->geometry.parent) {
1027 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1028 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001029 }
1030
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001032
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001034 transform_parent_handle_parent_destroy;
1035 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001036 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001038 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001040 }
1041
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 weston_view_geometry_dirty(view);
1043}
1044
1045WL_EXPORT int
1046weston_view_is_mapped(struct weston_view *view)
1047{
1048 if (view->output)
1049 return 1;
1050 else
1051 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001052}
1053
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001054WL_EXPORT int
1055weston_surface_is_mapped(struct weston_surface *surface)
1056{
1057 if (surface->output)
1058 return 1;
1059 else
1060 return 0;
1061}
1062
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001063WL_EXPORT int32_t
1064weston_surface_buffer_width(struct weston_surface *surface)
1065{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001066 int32_t width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001067 switch (surface->buffer_transform) {
1068 case WL_OUTPUT_TRANSFORM_90:
1069 case WL_OUTPUT_TRANSFORM_270:
1070 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1071 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001072 width = surface->buffer_ref.buffer->height;
1073 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001074 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001075 width = surface->buffer_ref.buffer->width;
1076 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001077 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001078 return width / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001079}
1080
1081WL_EXPORT int32_t
1082weston_surface_buffer_height(struct weston_surface *surface)
1083{
Alexander Larsson4ea95522013-05-22 14:41:37 +02001084 int32_t height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001085 switch (surface->buffer_transform) {
1086 case WL_OUTPUT_TRANSFORM_90:
1087 case WL_OUTPUT_TRANSFORM_270:
1088 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1089 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001090 height = surface->buffer_ref.buffer->width;
1091 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001092 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001093 height = surface->buffer_ref.buffer->height;
1094 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001095 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02001096 return height / surface->buffer_scale;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001097}
1098
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001099WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001100weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001101{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001102 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001103
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001104 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001105
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001106 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001107}
1108
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109WL_EXPORT struct weston_view *
1110weston_compositor_pick_view(struct weston_compositor *compositor,
1111 wl_fixed_t x, wl_fixed_t y,
1112 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001113{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001115
Jason Ekstranda7af7042013-10-12 22:38:11 -05001116 wl_list_for_each(view, &compositor->view_list, link) {
1117 weston_view_from_global_fixed(view, x, y, vx, vy);
1118 if (pixman_region32_contains_point(&view->surface->input,
1119 wl_fixed_to_int(*vx),
1120 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001121 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001123 }
1124
1125 return NULL;
1126}
1127
1128static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001130{
Daniel Stone37816df2012-05-16 18:45:18 +01001131 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001132
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001133 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001134 return;
1135
Daniel Stone37816df2012-05-16 18:45:18 +01001136 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001137 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001138}
1139
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001140WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001142{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001143 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001144
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145 if (!weston_view_is_mapped(view))
1146 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001147
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 weston_view_damage_below(view);
1149 view->output = NULL;
1150 wl_list_remove(&view->layer_link);
1151 wl_list_init(&view->layer_link);
1152 wl_list_remove(&view->link);
1153 wl_list_init(&view->link);
1154 /* We need to do this before torching the output mask */
1155 weston_view_schedule_repaint(view);
1156 view->output_mask = 0;
1157 weston_surface_assign_output(view->surface);
1158
1159 if (weston_surface_is_mapped(view->surface))
1160 return;
1161
1162 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1163 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001164 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001166 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001167 NULL,
1168 wl_fixed_from_int(0),
1169 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001171 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001172 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001174
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175WL_EXPORT void
1176weston_surface_unmap(struct weston_surface *surface)
1177{
1178 struct weston_view *view;
1179
1180 wl_list_for_each(view, &surface->views, surface_link)
1181 weston_view_unmap(view);
1182 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001183}
1184
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001185struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001186 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001187 struct wl_list link;
1188};
1189
Jason Ekstranda7af7042013-10-12 22:38:11 -05001190WL_EXPORT void
1191weston_view_destroy(struct weston_view *view)
1192{
1193 wl_signal_emit(&view->destroy_signal, view);
1194
1195 assert(wl_list_empty(&view->geometry.child_list));
1196
1197 if (weston_view_is_mapped(view)) {
1198 weston_view_unmap(view);
1199 weston_compositor_build_view_list(view->surface->compositor);
1200 }
1201
1202 wl_list_remove(&view->link);
1203 wl_list_remove(&view->layer_link);
1204
1205 pixman_region32_fini(&view->clip);
1206 pixman_region32_fini(&view->transform.boundingbox);
1207
1208 weston_view_set_transform_parent(view, NULL);
1209
1210 if (view->surface->compositor->renderer->destroy_view)
1211 view->surface->compositor->renderer->destroy_view(view);
1212
1213 wl_list_remove(&view->surface_link);
1214
1215 free(view);
1216}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001217
1218WL_EXPORT void
1219weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001220{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001221 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001222 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001224
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001225 if (--surface->ref_count > 0)
1226 return;
1227
1228 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1229
Pekka Paalanene67858b2013-04-25 13:57:42 +03001230 assert(wl_list_empty(&surface->subsurface_list_pending));
1231 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1234 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001235
Pekka Paalanenbc106382012-10-10 12:49:31 +03001236 wl_list_for_each_safe(cb, next,
1237 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001238 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001239
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001240 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001241 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001242 pixman_region32_fini(&surface->pending.damage);
1243
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001244 if (surface->pending.buffer)
1245 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1246
Pekka Paalanende685b82012-12-04 15:58:12 +02001247 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001248
Kristian Høgsberg42263852012-09-06 21:59:29 -04001249 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +02001250
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001251 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001252 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001253 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001254
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001255 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001256 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001257
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001258 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001259}
1260
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001261static void
1262destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001263{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001264 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001265
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001266 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001267}
1268
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001269static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001270weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1271{
1272 struct weston_buffer *buffer =
1273 container_of(listener, struct weston_buffer, destroy_listener);
1274
1275 wl_signal_emit(&buffer->destroy_signal, buffer);
1276 free(buffer);
1277}
1278
1279struct weston_buffer *
1280weston_buffer_from_resource(struct wl_resource *resource)
1281{
1282 struct weston_buffer *buffer;
1283 struct wl_listener *listener;
1284
1285 listener = wl_resource_get_destroy_listener(resource,
1286 weston_buffer_destroy_handler);
1287
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001288 if (listener)
1289 return container_of(listener, struct weston_buffer,
1290 destroy_listener);
1291
1292 buffer = zalloc(sizeof *buffer);
1293 if (buffer == NULL)
1294 return NULL;
1295
1296 buffer->resource = resource;
1297 wl_signal_init(&buffer->destroy_signal);
1298 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001299 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001300 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001301
1302 return buffer;
1303}
1304
1305static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001306weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1307 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001308{
Pekka Paalanende685b82012-12-04 15:58:12 +02001309 struct weston_buffer_reference *ref =
1310 container_of(listener, struct weston_buffer_reference,
1311 destroy_listener);
1312
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001313 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001314 ref->buffer = NULL;
1315}
1316
1317WL_EXPORT void
1318weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001319 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001320{
1321 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001322 ref->buffer->busy_count--;
1323 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001324 assert(wl_resource_get_client(ref->buffer->resource));
1325 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001326 WL_BUFFER_RELEASE);
1327 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001328 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001329 }
1330
Pekka Paalanende685b82012-12-04 15:58:12 +02001331 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001332 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001333 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001334 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001335 }
1336
Pekka Paalanende685b82012-12-04 15:58:12 +02001337 ref->buffer = buffer;
1338 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1339}
1340
1341static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001342weston_surface_attach(struct weston_surface *surface,
1343 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001344{
1345 weston_buffer_reference(&surface->buffer_ref, buffer);
1346
Pekka Paalanena6421c42012-12-04 15:58:10 +02001347 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001348 if (weston_surface_is_mapped(surface))
1349 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001350 }
1351
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001352 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001353}
1354
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001355WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356weston_view_restack(struct weston_view *view, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001357{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001358 wl_list_remove(&view->layer_link);
1359 wl_list_insert(below, &view->layer_link);
1360 weston_view_damage_below(view);
1361 weston_surface_damage(view->surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001362}
1363
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001364WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001365weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001366{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001367 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001368
1369 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001370 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001371}
1372
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001373WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001374weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001375{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001376 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001377
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001378 pixman_region32_union(&compositor->primary_plane.damage,
1379 &compositor->primary_plane.damage,
1380 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001381 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001382}
1383
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001384static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001386{
Pekka Paalanende685b82012-12-04 15:58:12 +02001387 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001388 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001389 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001390
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 empty_region(&surface->damage);
1392}
1393
1394static void
1395view_accumulate_damage(struct weston_view *view,
1396 pixman_region32_t *opaque)
1397{
1398 pixman_region32_t damage;
1399
1400 pixman_region32_init(&damage);
1401 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001402 pixman_box32_t *extents;
1403
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404 extents = pixman_region32_extents(&view->surface->damage);
1405 view_compute_bbox(view, extents->x1, extents->y1,
1406 extents->x2 - extents->x1,
1407 extents->y2 - extents->y1,
1408 &damage);
1409 pixman_region32_translate(&damage,
1410 -view->plane->x,
1411 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001412 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413 pixman_region32_copy(&damage, &view->surface->damage);
1414 pixman_region32_translate(&damage,
1415 view->geometry.x - view->plane->x,
1416 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001417 }
1418
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 pixman_region32_subtract(&damage, &damage, opaque);
1420 pixman_region32_union(&view->plane->damage,
1421 &view->plane->damage, &damage);
1422 pixman_region32_fini(&damage);
1423 pixman_region32_copy(&view->clip, opaque);
1424 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001425}
1426
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001427static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001428compositor_accumulate_damage(struct weston_compositor *ec)
1429{
1430 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001432 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001433
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001434 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001435
1436 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001437 pixman_region32_copy(&plane->clip, &clip);
1438
1439 pixman_region32_init(&opaque);
1440
Jason Ekstranda7af7042013-10-12 22:38:11 -05001441 wl_list_for_each(ev, &ec->view_list, link) {
1442 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001443 continue;
1444
Jason Ekstranda7af7042013-10-12 22:38:11 -05001445 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001446 }
1447
1448 pixman_region32_union(&clip, &clip, &opaque);
1449 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001450 }
1451
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001452 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001453
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 wl_list_for_each(ev, &ec->view_list, link)
1455 ev->surface->touched = 0;
1456
1457 wl_list_for_each(ev, &ec->view_list, link) {
1458 if (ev->surface->touched)
1459 continue;
1460 ev->surface->touched = 1;
1461
1462 surface_flush_damage(ev->surface);
1463
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001464 /* Both the renderer and the backend have seen the buffer
1465 * by now. If renderer needs the buffer, it has its own
1466 * reference set. If the backend wants to keep the buffer
1467 * around for migrating the surface into a non-primary plane
1468 * later, keep_buffer is true. Otherwise, drop the core
1469 * reference now, and allow early buffer release. This enables
1470 * clients to use single-buffering.
1471 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 if (!ev->surface->keep_buffer)
1473 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001474 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001475}
1476
1477static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001479{
1480 struct weston_subsurface *sub;
1481
Pekka Paalanene67858b2013-04-25 13:57:42 +03001482 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001484 continue;
1485
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1487 wl_list_init(&sub->surface->views);
1488
1489 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001490 }
1491}
1492
1493static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001494surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001495{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001496 struct weston_subsurface *sub;
1497 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001498
Jason Ekstranda7af7042013-10-12 22:38:11 -05001499 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1500 if (sub->surface == surface)
1501 continue;
1502
1503 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1504 weston_view_destroy(view);
1505
1506 surface_free_unused_subsurface_views(sub->surface);
1507 }
1508}
1509
1510static void
1511view_list_add_subsurface_view(struct weston_compositor *compositor,
1512 struct weston_subsurface *sub,
1513 struct weston_view *parent)
1514{
1515 struct weston_subsurface *child;
1516 struct weston_view *view = NULL, *iv;
1517
1518 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1519 if (iv->geometry.parent == parent) {
1520 view = iv;
1521 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001522 }
1523 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524
1525 if (view) {
1526 /* Put it back in the surface's list of views */
1527 wl_list_remove(&view->surface_link);
1528 wl_list_insert(&sub->surface->views, &view->surface_link);
1529 } else {
1530 view = weston_view_create(sub->surface);
1531 weston_view_configure(view,
1532 sub->position.x,
1533 sub->position.y,
1534 sub->surface->width,
1535 sub->surface->height);
1536 weston_view_set_transform_parent(view, parent);
1537 }
1538
1539 weston_view_update_transform(view);
1540 wl_list_insert(compositor->view_list.next, &view->link);
1541
1542 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link)
1543 if (child->surface != sub->surface)
1544 view_list_add_subsurface_view(compositor, child, view);
1545}
1546
1547static void
1548view_list_add(struct weston_compositor *compositor,
1549 struct weston_view *view)
1550{
1551 struct weston_subsurface *sub;
1552
1553 weston_view_update_transform(view);
1554 wl_list_insert(compositor->view_list.prev, &view->link);
1555
1556 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link)
1557 if (sub->surface != view->surface)
1558 view_list_add_subsurface_view(compositor, sub, view);
1559}
1560
1561static void
1562weston_compositor_build_view_list(struct weston_compositor *compositor)
1563{
1564 struct weston_view *view;
1565 struct weston_layer *layer;
1566
1567 wl_list_for_each(layer, &compositor->layer_list, link)
1568 wl_list_for_each(view, &layer->view_list, layer_link)
1569 surface_stash_subsurface_views(view->surface);
1570
1571 wl_list_init(&compositor->view_list);
1572 wl_list_for_each(layer, &compositor->layer_list, link) {
1573 wl_list_for_each(view, &layer->view_list, layer_link) {
1574 view_list_add(compositor, view);
1575 }
1576 }
1577
1578 wl_list_for_each(layer, &compositor->layer_list, link)
1579 wl_list_for_each(view, &layer->view_list, layer_link)
1580 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001581}
1582
David Herrmann1edf44c2013-10-22 17:11:26 +02001583static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001584weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001586 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001588 struct weston_animation *animation, *next;
1589 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001590 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001591 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001592 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001593
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001594 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001596
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001597 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001598 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001599 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 wl_list_for_each(ev, &ec->view_list, link)
1601 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001602
Pekka Paalanene67858b2013-04-25 13:57:42 +03001603 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604 wl_list_for_each(ev, &ec->view_list, link) {
1605 /* Note: This operation is safe to do multiple times on the
1606 * same surface.
1607 */
1608 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001609 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 &ev->surface->frame_callback_list);
1611 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001612 }
1613 }
1614
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001615 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001616
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001617 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001618 pixman_region32_intersect(&output_damage,
1619 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001620 pixman_region32_subtract(&output_damage,
1621 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001622
Scott Moreauccbf29d2012-02-22 14:21:41 -07001623 if (output->dirty)
1624 weston_output_update_matrix(output);
1625
David Herrmann1edf44c2013-10-22 17:11:26 +02001626 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001627
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001628 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001629
Kristian Høgsbergef044142011-06-21 15:02:12 -04001630 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001631
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001632 weston_compositor_repick(ec);
1633 wl_event_loop_dispatch(ec->input_loop, 0);
1634
Jonas Ådahldb773762012-06-13 00:01:21 +02001635 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001636 wl_callback_send_done(cb->resource, msecs);
1637 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001638 }
1639
Scott Moreaud64cf212012-06-08 19:40:54 -06001640 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001641 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001642 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001643 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001644
1645 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001646}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001647
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001648static int
1649weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001650{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001651 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001652
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001653 wl_event_loop_dispatch(compositor->input_loop, 0);
1654
1655 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001656}
1657
1658WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001659weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001660{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001661 struct weston_compositor *compositor = output->compositor;
1662 struct wl_event_loop *loop =
1663 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001664 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001665
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001666 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001667
1668 if (output->repaint_needed &&
1669 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1670 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001671 r = weston_output_repaint(output, msecs);
1672 if (!r)
1673 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001674 }
1675
1676 output->repaint_scheduled = 0;
1677 if (compositor->input_loop_source)
1678 return;
1679
1680 fd = wl_event_loop_get_fd(compositor->input_loop);
1681 compositor->input_loop_source =
1682 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1683 weston_compositor_read_input, compositor);
1684}
1685
1686static void
1687idle_repaint(void *data)
1688{
1689 struct weston_output *output = data;
1690
Jonas Ådahle5a12252013-04-05 23:07:11 +02001691 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001692}
1693
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001694WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001695weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1696{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001698 if (below != NULL)
1699 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001700}
1701
1702WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001703weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001704{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001705 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001706 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001707
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001708 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1709 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001710 return;
1711
Kristian Høgsbergef044142011-06-21 15:02:12 -04001712 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001713 output->repaint_needed = 1;
1714 if (output->repaint_scheduled)
1715 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001716
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001717 wl_event_loop_add_idle(loop, idle_repaint, output);
1718 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001719
1720 if (compositor->input_loop_source) {
1721 wl_event_source_remove(compositor->input_loop_source);
1722 compositor->input_loop_source = NULL;
1723 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001724}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001725
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001726WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001727weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1728{
1729 struct weston_output *output;
1730
1731 wl_list_for_each(output, &compositor->output_list, link)
1732 weston_output_schedule_repaint(output);
1733}
1734
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001735static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001736surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001737{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001738 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001739}
1740
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001741static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001742surface_attach(struct wl_client *client,
1743 struct wl_resource *resource,
1744 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1745{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001746 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001747 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001748
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001749 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001750 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001751 if (buffer == NULL) {
1752 wl_client_post_no_memory(client);
1753 return;
1754 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001755 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001756
Pekka Paalanende685b82012-12-04 15:58:12 +02001757 /* Attach, attach, without commit in between does not send
1758 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001759 if (surface->pending.buffer)
1760 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001761
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001762 surface->pending.sx = sx;
1763 surface->pending.sy = sy;
1764 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001765 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001766 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001767 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001768 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001769 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001770}
1771
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001772static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001773surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001774 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001775 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001776{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001777 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001778
Pekka Paalanen8e159182012-10-10 12:49:25 +03001779 pixman_region32_union_rect(&surface->pending.damage,
1780 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001781 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001782}
1783
Kristian Høgsberg33418202011-08-16 23:01:28 -04001784static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001785destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001786{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001787 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001788
1789 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001790 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001791}
1792
1793static void
1794surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001795 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001796{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001797 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001798 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001799
1800 cb = malloc(sizeof *cb);
1801 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001802 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001803 return;
1804 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001805
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001806 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1807 callback);
1808 if (cb->resource == NULL) {
1809 free(cb);
1810 wl_resource_post_no_memory(resource);
1811 return;
1812 }
1813
Jason Ekstranda85118c2013-06-27 20:17:02 -05001814 wl_resource_set_implementation(cb->resource, NULL, cb,
1815 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001816
Pekka Paalanenbc106382012-10-10 12:49:31 +03001817 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001818}
1819
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001820static void
1821surface_set_opaque_region(struct wl_client *client,
1822 struct wl_resource *resource,
1823 struct wl_resource *region_resource)
1824{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001825 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001826 struct weston_region *region;
1827
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001828 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001829 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001830 pixman_region32_copy(&surface->pending.opaque,
1831 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001832 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001833 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001834 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001835}
1836
1837static void
1838surface_set_input_region(struct wl_client *client,
1839 struct wl_resource *resource,
1840 struct wl_resource *region_resource)
1841{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001842 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001843 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001844
1845 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001846 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001847 pixman_region32_copy(&surface->pending.input,
1848 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001849 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001850 pixman_region32_fini(&surface->pending.input);
1851 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001852 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001853}
1854
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001855static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001856weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001857{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001858 struct weston_subsurface *sub;
1859
1860 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1861 parent_link_pending) {
1862 wl_list_remove(&sub->parent_link);
1863 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1864 }
1865}
1866
1867static void
1868weston_surface_commit(struct weston_surface *surface)
1869{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001870 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001871 pixman_region32_t opaque;
1872
Alexander Larsson4ea95522013-05-22 14:41:37 +02001873 /* wl_surface.set_buffer_transform */
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001874 surface->buffer_transform = surface->pending.buffer_transform;
1875
Alexander Larsson4ea95522013-05-22 14:41:37 +02001876 /* wl_surface.set_buffer_scale */
1877 surface->buffer_scale = surface->pending.buffer_scale;
1878
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001879 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001880 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001881 weston_surface_attach(surface, surface->pending.buffer);
1882
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 surface->width = 0;
1884 surface->height = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001885 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 /* This already includes the buffer scale */
1887 surface->width = weston_surface_buffer_width(surface);
1888 surface->height = weston_surface_buffer_height(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001889 }
1890
1891 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001892 surface->configure(surface,
1893 surface->pending.sx, surface->pending.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001894 surface->width, surface->height);
Giulio Camuffo184df502013-02-21 11:29:21 +01001895
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05001896 if (surface->pending.buffer)
1897 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1898 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001899 surface->pending.sx = 0;
1900 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01001901 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001902
1903 /* wl_surface.damage */
1904 pixman_region32_union(&surface->damage, &surface->damage,
1905 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001906 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1907 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001908 surface->width,
1909 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001910 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001911
1912 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001913 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001914 surface->width,
1915 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001916 pixman_region32_intersect(&opaque,
1917 &opaque, &surface->pending.opaque);
1918
1919 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1920 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 wl_list_for_each(view, &surface->views, surface_link)
1922 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001923 }
1924
1925 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001926
1927 /* wl_surface.set_input_region */
1928 pixman_region32_fini(&surface->input);
1929 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001930 surface->width,
1931 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001932 pixman_region32_intersect(&surface->input,
1933 &surface->input, &surface->pending.input);
1934
Pekka Paalanenbc106382012-10-10 12:49:31 +03001935 /* wl_surface.frame */
1936 wl_list_insert_list(&surface->frame_callback_list,
1937 &surface->pending.frame_callback_list);
1938 wl_list_init(&surface->pending.frame_callback_list);
1939
Pekka Paalanene67858b2013-04-25 13:57:42 +03001940 weston_surface_commit_subsurface_order(surface);
1941
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001942 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001943}
1944
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001945static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001946weston_subsurface_commit(struct weston_subsurface *sub);
1947
1948static void
1949weston_subsurface_parent_commit(struct weston_subsurface *sub,
1950 int parent_is_synchronized);
1951
1952static void
1953surface_commit(struct wl_client *client, struct wl_resource *resource)
1954{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001955 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001956 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
1957
1958 if (sub) {
1959 weston_subsurface_commit(sub);
1960 return;
1961 }
1962
1963 weston_surface_commit(surface);
1964
1965 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1966 if (sub->surface != surface)
1967 weston_subsurface_parent_commit(sub, 0);
1968 }
1969}
1970
1971static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001972surface_set_buffer_transform(struct wl_client *client,
1973 struct wl_resource *resource, int transform)
1974{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001975 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001976
1977 surface->pending.buffer_transform = transform;
1978}
1979
Alexander Larsson4ea95522013-05-22 14:41:37 +02001980static void
1981surface_set_buffer_scale(struct wl_client *client,
1982 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02001983 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02001984{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001985 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02001986
1987 surface->pending.buffer_scale = scale;
1988}
1989
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001990static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001991 surface_destroy,
1992 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001993 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001994 surface_frame,
1995 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001996 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001997 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02001998 surface_set_buffer_transform,
1999 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002000};
2001
2002static void
2003compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002004 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002005{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002006 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002007 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002008
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002009 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002010 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002011 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002012 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002013 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002014
Jason Ekstranda85118c2013-06-27 20:17:02 -05002015 surface->resource =
2016 wl_resource_create(client, &wl_surface_interface,
2017 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002018 if (surface->resource == NULL) {
2019 weston_surface_destroy(surface);
2020 wl_resource_post_no_memory(resource);
2021 return;
2022 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002023 wl_resource_set_implementation(surface->resource, &surface_interface,
2024 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002025}
2026
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002027static void
2028destroy_region(struct wl_resource *resource)
2029{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002030 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002031
2032 pixman_region32_fini(&region->region);
2033 free(region);
2034}
2035
2036static void
2037region_destroy(struct wl_client *client, struct wl_resource *resource)
2038{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002039 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002040}
2041
2042static void
2043region_add(struct wl_client *client, struct wl_resource *resource,
2044 int32_t x, int32_t y, int32_t width, int32_t height)
2045{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002046 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002047
2048 pixman_region32_union_rect(&region->region, &region->region,
2049 x, y, width, height);
2050}
2051
2052static void
2053region_subtract(struct wl_client *client, struct wl_resource *resource,
2054 int32_t x, int32_t y, int32_t width, int32_t height)
2055{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002056 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002057 pixman_region32_t rect;
2058
2059 pixman_region32_init_rect(&rect, x, y, width, height);
2060 pixman_region32_subtract(&region->region, &region->region, &rect);
2061 pixman_region32_fini(&rect);
2062}
2063
2064static const struct wl_region_interface region_interface = {
2065 region_destroy,
2066 region_add,
2067 region_subtract
2068};
2069
2070static void
2071compositor_create_region(struct wl_client *client,
2072 struct wl_resource *resource, uint32_t id)
2073{
2074 struct weston_region *region;
2075
2076 region = malloc(sizeof *region);
2077 if (region == NULL) {
2078 wl_resource_post_no_memory(resource);
2079 return;
2080 }
2081
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002082 pixman_region32_init(&region->region);
2083
Jason Ekstranda85118c2013-06-27 20:17:02 -05002084 region->resource =
2085 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002086 if (region->resource == NULL) {
2087 free(region);
2088 wl_resource_post_no_memory(resource);
2089 return;
2090 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002091 wl_resource_set_implementation(region->resource, &region_interface,
2092 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002093}
2094
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002095static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002096 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002097 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002098};
2099
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002100static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002101weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2102{
2103 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002104 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002106
Alexander Larsson4ea95522013-05-22 14:41:37 +02002107 /* wl_surface.set_buffer_transform */
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108 surface->buffer_transform = sub->cached.buffer_transform;
2109
Alexander Larsson4ea95522013-05-22 14:41:37 +02002110 /* wl_surface.set_buffer_scale */
2111 surface->buffer_scale = sub->cached.buffer_scale;
2112
Pekka Paalanene67858b2013-04-25 13:57:42 +03002113 /* wl_surface.attach */
2114 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2115 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2116 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2117
Jason Ekstranda7af7042013-10-12 22:38:11 -05002118 surface->width = 0;
2119 surface->height = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002120 if (surface->buffer_ref.buffer) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002121 surface->width = weston_surface_buffer_width(surface);
2122 surface->height = weston_surface_buffer_height(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002123 }
2124
2125 if (surface->configure && sub->cached.newly_attached)
2126 surface->configure(surface, sub->cached.sx, sub->cached.sy,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002127 surface->width, surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 sub->cached.sx = 0;
2129 sub->cached.sy = 0;
2130 sub->cached.newly_attached = 0;
2131
2132 /* wl_surface.damage */
2133 pixman_region32_union(&surface->damage, &surface->damage,
2134 &sub->cached.damage);
2135 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2136 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002137 surface->width,
2138 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002139 empty_region(&sub->cached.damage);
2140
2141 /* wl_surface.set_opaque_region */
2142 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002143 surface->width,
2144 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002145 pixman_region32_intersect(&opaque,
2146 &opaque, &sub->cached.opaque);
2147
2148 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2149 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002150 wl_list_for_each(view, &surface->views, surface_link)
2151 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002152 }
2153
2154 pixman_region32_fini(&opaque);
2155
2156 /* wl_surface.set_input_region */
2157 pixman_region32_fini(&surface->input);
2158 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002159 surface->width,
2160 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002161 pixman_region32_intersect(&surface->input,
2162 &surface->input, &sub->cached.input);
2163
2164 /* wl_surface.frame */
2165 wl_list_insert_list(&surface->frame_callback_list,
2166 &sub->cached.frame_callback_list);
2167 wl_list_init(&sub->cached.frame_callback_list);
2168
2169 weston_surface_commit_subsurface_order(surface);
2170
2171 weston_surface_schedule_repaint(surface);
2172
2173 sub->cached.has_data = 0;
2174}
2175
2176static void
2177weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2178{
2179 struct weston_surface *surface = sub->surface;
2180
2181 /*
2182 * If this commit would cause the surface to move by the
2183 * attach(dx, dy) parameters, the old damage region must be
2184 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002185 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002186 */
2187 pixman_region32_translate(&sub->cached.damage,
2188 -surface->pending.sx, -surface->pending.sy);
2189 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2190 &surface->pending.damage);
2191 empty_region(&surface->pending.damage);
2192
2193 if (surface->pending.newly_attached) {
2194 sub->cached.newly_attached = 1;
2195 weston_buffer_reference(&sub->cached.buffer_ref,
2196 surface->pending.buffer);
2197 }
2198 sub->cached.sx += surface->pending.sx;
2199 sub->cached.sy += surface->pending.sy;
2200 surface->pending.sx = 0;
2201 surface->pending.sy = 0;
2202 surface->pending.newly_attached = 0;
2203
2204 sub->cached.buffer_transform = surface->pending.buffer_transform;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002205 sub->cached.buffer_scale = surface->pending.buffer_scale;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002206
2207 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2208
2209 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2210
2211 wl_list_insert_list(&sub->cached.frame_callback_list,
2212 &surface->pending.frame_callback_list);
2213 wl_list_init(&surface->pending.frame_callback_list);
2214
2215 sub->cached.has_data = 1;
2216}
2217
2218static int
2219weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2220{
2221 while (sub) {
2222 if (sub->synchronized)
2223 return 1;
2224
2225 if (!sub->parent)
2226 return 0;
2227
2228 sub = weston_surface_to_subsurface(sub->parent);
2229 }
2230
2231 return 0;
2232}
2233
2234static void
2235weston_subsurface_commit(struct weston_subsurface *sub)
2236{
2237 struct weston_surface *surface = sub->surface;
2238 struct weston_subsurface *tmp;
2239
2240 /* Recursive check for effectively synchronized. */
2241 if (weston_subsurface_is_synchronized(sub)) {
2242 weston_subsurface_commit_to_cache(sub);
2243 } else {
2244 if (sub->cached.has_data) {
2245 /* flush accumulated state from cache */
2246 weston_subsurface_commit_to_cache(sub);
2247 weston_subsurface_commit_from_cache(sub);
2248 } else {
2249 weston_surface_commit(surface);
2250 }
2251
2252 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2253 if (tmp->surface != surface)
2254 weston_subsurface_parent_commit(tmp, 0);
2255 }
2256 }
2257}
2258
2259static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002260weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002261{
2262 struct weston_surface *surface = sub->surface;
2263 struct weston_subsurface *tmp;
2264
Pekka Paalanene67858b2013-04-25 13:57:42 +03002265 /* From now on, commit_from_cache the whole sub-tree, regardless of
2266 * the synchronized mode of each child. This sub-surface or some
2267 * of its ancestors were synchronized, so we are synchronized
2268 * all the way down.
2269 */
2270
2271 if (sub->cached.has_data)
2272 weston_subsurface_commit_from_cache(sub);
2273
2274 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2275 if (tmp->surface != surface)
2276 weston_subsurface_parent_commit(tmp, 1);
2277 }
2278}
2279
2280static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002281weston_subsurface_parent_commit(struct weston_subsurface *sub,
2282 int parent_is_synchronized)
2283{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002284 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002285 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002286 wl_list_for_each(view, &sub->surface->views, surface_link)
2287 weston_view_set_position(view,
2288 sub->position.x,
2289 sub->position.y);
2290
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002291 sub->position.set = 0;
2292 }
2293
2294 if (parent_is_synchronized || sub->synchronized)
2295 weston_subsurface_synchronized_commit(sub);
2296}
2297
2298static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy,
2300 int32_t width, int32_t height)
2301{
2302 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002303 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002304
Jason Ekstranda7af7042013-10-12 22:38:11 -05002305 wl_list_for_each(view, &surface->views, surface_link)
2306 weston_view_configure(view,
2307 view->geometry.x + dx,
2308 view->geometry.y + dy,
2309 width, height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002310
2311 /* No need to check parent mappedness, because if parent is not
2312 * mapped, parent is not in a visible layer, so this sub-surface
2313 * will not be drawn either.
2314 */
2315 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 /* Cannot call weston_surface_update_transform(),
2317 * because that would call it also for the parent surface,
2318 * which might not be mapped yet. That would lead to
2319 * inconsistent state, where the window could never be
2320 * mapped.
2321 *
2322 * Instead just assing any output, to make
2323 * weston_surface_is_mapped() return true, so that when the
2324 * parent surface does get mapped, this one will get
2325 * included, too. See surface_list_add().
2326 */
2327 assert(!wl_list_empty(&compositor->output_list));
2328 surface->output = container_of(compositor->output_list.next,
2329 struct weston_output, link);
2330 }
2331}
2332
2333static struct weston_subsurface *
2334weston_surface_to_subsurface(struct weston_surface *surface)
2335{
2336 if (surface->configure == subsurface_configure)
2337 return surface->configure_private;
2338
2339 return NULL;
2340}
2341
Pekka Paalanen01388e22013-04-25 13:57:44 +03002342WL_EXPORT struct weston_surface *
2343weston_surface_get_main_surface(struct weston_surface *surface)
2344{
2345 struct weston_subsurface *sub;
2346
2347 while (surface && (sub = weston_surface_to_subsurface(surface)))
2348 surface = sub->parent;
2349
2350 return surface;
2351}
2352
Pekka Paalanene67858b2013-04-25 13:57:42 +03002353static void
2354subsurface_set_position(struct wl_client *client,
2355 struct wl_resource *resource, int32_t x, int32_t y)
2356{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002357 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002358
2359 if (!sub)
2360 return;
2361
2362 sub->position.x = x;
2363 sub->position.y = y;
2364 sub->position.set = 1;
2365}
2366
2367static struct weston_subsurface *
2368subsurface_from_surface(struct weston_surface *surface)
2369{
2370 struct weston_subsurface *sub;
2371
2372 sub = weston_surface_to_subsurface(surface);
2373 if (sub)
2374 return sub;
2375
2376 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2377 if (sub->surface == surface)
2378 return sub;
2379
2380 return NULL;
2381}
2382
2383static struct weston_subsurface *
2384subsurface_sibling_check(struct weston_subsurface *sub,
2385 struct weston_surface *surface,
2386 const char *request)
2387{
2388 struct weston_subsurface *sibling;
2389
2390 sibling = subsurface_from_surface(surface);
2391
2392 if (!sibling) {
2393 wl_resource_post_error(sub->resource,
2394 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2395 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002396 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002397 return NULL;
2398 }
2399
2400 if (sibling->parent != sub->parent) {
2401 wl_resource_post_error(sub->resource,
2402 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2403 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002404 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002405 return NULL;
2406 }
2407
2408 return sibling;
2409}
2410
2411static void
2412subsurface_place_above(struct wl_client *client,
2413 struct wl_resource *resource,
2414 struct wl_resource *sibling_resource)
2415{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002416 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002417 struct weston_surface *surface =
2418 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002419 struct weston_subsurface *sibling;
2420
2421 if (!sub)
2422 return;
2423
2424 sibling = subsurface_sibling_check(sub, surface, "place_above");
2425 if (!sibling)
2426 return;
2427
2428 wl_list_remove(&sub->parent_link_pending);
2429 wl_list_insert(sibling->parent_link_pending.prev,
2430 &sub->parent_link_pending);
2431}
2432
2433static void
2434subsurface_place_below(struct wl_client *client,
2435 struct wl_resource *resource,
2436 struct wl_resource *sibling_resource)
2437{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002438 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002439 struct weston_surface *surface =
2440 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002441 struct weston_subsurface *sibling;
2442
2443 if (!sub)
2444 return;
2445
2446 sibling = subsurface_sibling_check(sub, surface, "place_below");
2447 if (!sibling)
2448 return;
2449
2450 wl_list_remove(&sub->parent_link_pending);
2451 wl_list_insert(&sibling->parent_link_pending,
2452 &sub->parent_link_pending);
2453}
2454
2455static void
2456subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2457{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002458 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002459
2460 if (sub)
2461 sub->synchronized = 1;
2462}
2463
2464static void
2465subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2466{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002467 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002468
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002469 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002470 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002471
2472 /* If sub became effectively desynchronized, flush. */
2473 if (!weston_subsurface_is_synchronized(sub))
2474 weston_subsurface_synchronized_commit(sub);
2475 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002476}
2477
2478static void
2479weston_subsurface_cache_init(struct weston_subsurface *sub)
2480{
2481 pixman_region32_init(&sub->cached.damage);
2482 pixman_region32_init(&sub->cached.opaque);
2483 pixman_region32_init(&sub->cached.input);
2484 wl_list_init(&sub->cached.frame_callback_list);
2485 sub->cached.buffer_ref.buffer = NULL;
2486}
2487
2488static void
2489weston_subsurface_cache_fini(struct weston_subsurface *sub)
2490{
2491 struct weston_frame_callback *cb, *tmp;
2492
2493 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002494 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002495
2496 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2497 pixman_region32_fini(&sub->cached.damage);
2498 pixman_region32_fini(&sub->cached.opaque);
2499 pixman_region32_fini(&sub->cached.input);
2500}
2501
2502static void
2503weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2504{
2505 wl_list_remove(&sub->parent_link);
2506 wl_list_remove(&sub->parent_link_pending);
2507 wl_list_remove(&sub->parent_destroy_listener.link);
2508 sub->parent = NULL;
2509}
2510
2511static void
2512weston_subsurface_destroy(struct weston_subsurface *sub);
2513
2514static void
2515subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2516{
2517 struct weston_subsurface *sub =
2518 container_of(listener, struct weston_subsurface,
2519 surface_destroy_listener);
2520 assert(data == &sub->surface->resource);
2521
2522 /* The protocol object (wl_resource) is left inert. */
2523 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002524 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002525
2526 weston_subsurface_destroy(sub);
2527}
2528
2529static void
2530subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2531{
2532 struct weston_subsurface *sub =
2533 container_of(listener, struct weston_subsurface,
2534 parent_destroy_listener);
2535 assert(data == &sub->parent->resource);
2536 assert(sub->surface != sub->parent);
2537
2538 if (weston_surface_is_mapped(sub->surface))
2539 weston_surface_unmap(sub->surface);
2540
2541 weston_subsurface_unlink_parent(sub);
2542}
2543
2544static void
2545subsurface_resource_destroy(struct wl_resource *resource)
2546{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002547 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002548
2549 if (sub)
2550 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551}
2552
2553static void
2554subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2555{
2556 wl_resource_destroy(resource);
2557}
2558
2559static void
2560weston_subsurface_link_parent(struct weston_subsurface *sub,
2561 struct weston_surface *parent)
2562{
2563 sub->parent = parent;
2564 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002565 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002566 &sub->parent_destroy_listener);
2567
2568 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2569 wl_list_insert(&parent->subsurface_list_pending,
2570 &sub->parent_link_pending);
2571}
2572
2573static void
2574weston_subsurface_link_surface(struct weston_subsurface *sub,
2575 struct weston_surface *surface)
2576{
2577 sub->surface = surface;
2578 sub->surface_destroy_listener.notify =
2579 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002580 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002581 &sub->surface_destroy_listener);
2582}
2583
2584static void
2585weston_subsurface_destroy(struct weston_subsurface *sub)
2586{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002587 struct weston_view *view, *next;
2588
Pekka Paalanene67858b2013-04-25 13:57:42 +03002589 assert(sub->surface);
2590
2591 if (sub->resource) {
2592 assert(weston_surface_to_subsurface(sub->surface) == sub);
2593 assert(sub->parent_destroy_listener.notify ==
2594 subsurface_handle_parent_destroy);
2595
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2597 weston_view_destroy(view);
2598
Pekka Paalanene67858b2013-04-25 13:57:42 +03002599 if (sub->parent)
2600 weston_subsurface_unlink_parent(sub);
2601
2602 weston_subsurface_cache_fini(sub);
2603
2604 sub->surface->configure = NULL;
2605 sub->surface->configure_private = NULL;
2606 } else {
2607 /* the dummy weston_subsurface for the parent itself */
2608 assert(sub->parent_destroy_listener.notify == NULL);
2609 wl_list_remove(&sub->parent_link);
2610 wl_list_remove(&sub->parent_link_pending);
2611 }
2612
2613 wl_list_remove(&sub->surface_destroy_listener.link);
2614 free(sub);
2615}
2616
2617static const struct wl_subsurface_interface subsurface_implementation = {
2618 subsurface_destroy,
2619 subsurface_set_position,
2620 subsurface_place_above,
2621 subsurface_place_below,
2622 subsurface_set_sync,
2623 subsurface_set_desync
2624};
2625
2626static struct weston_subsurface *
2627weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2628 struct weston_surface *parent)
2629{
2630 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002631 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632
2633 sub = calloc(1, sizeof *sub);
2634 if (!sub)
2635 return NULL;
2636
Jason Ekstranda7af7042013-10-12 22:38:11 -05002637 wl_list_init(&sub->unused_views);
2638
Jason Ekstranda85118c2013-06-27 20:17:02 -05002639 sub->resource =
2640 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641 if (!sub->resource) {
2642 free(sub);
2643 return NULL;
2644 }
2645
Jason Ekstranda85118c2013-06-27 20:17:02 -05002646 wl_resource_set_implementation(sub->resource,
2647 &subsurface_implementation,
2648 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002649 weston_subsurface_link_surface(sub, surface);
2650 weston_subsurface_link_parent(sub, parent);
2651 weston_subsurface_cache_init(sub);
2652 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002653
2654 return sub;
2655}
2656
2657/* Create a dummy subsurface for having the parent itself in its
2658 * sub-surface lists. Makes stacking order manipulation easy.
2659 */
2660static struct weston_subsurface *
2661weston_subsurface_create_for_parent(struct weston_surface *parent)
2662{
2663 struct weston_subsurface *sub;
2664
2665 sub = calloc(1, sizeof *sub);
2666 if (!sub)
2667 return NULL;
2668
2669 weston_subsurface_link_surface(sub, parent);
2670 sub->parent = parent;
2671 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2672 wl_list_insert(&parent->subsurface_list_pending,
2673 &sub->parent_link_pending);
2674
2675 return sub;
2676}
2677
2678static void
2679subcompositor_get_subsurface(struct wl_client *client,
2680 struct wl_resource *resource,
2681 uint32_t id,
2682 struct wl_resource *surface_resource,
2683 struct wl_resource *parent_resource)
2684{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002685 struct weston_surface *surface =
2686 wl_resource_get_user_data(surface_resource);
2687 struct weston_surface *parent =
2688 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002689 struct weston_subsurface *sub;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002690 struct weston_view *view, *subview;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002691 static const char where[] = "get_subsurface: wl_subsurface@";
2692
2693 if (surface == parent) {
2694 wl_resource_post_error(resource,
2695 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2696 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002697 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002698 return;
2699 }
2700
2701 if (weston_surface_to_subsurface(surface)) {
2702 wl_resource_post_error(resource,
2703 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2704 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002705 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002706 return;
2707 }
2708
2709 if (surface->configure) {
2710 wl_resource_post_error(resource,
2711 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2712 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002713 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002714 return;
2715 }
2716
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002717 if (weston_surface_get_main_surface(parent) == surface) {
2718 wl_resource_post_error(resource,
2719 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2720 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002721 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002722 return;
2723 }
2724
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725 /* make sure the parent is in its own list */
2726 if (wl_list_empty(&parent->subsurface_list)) {
2727 if (!weston_subsurface_create_for_parent(parent)) {
2728 wl_resource_post_no_memory(resource);
2729 return;
2730 }
2731 }
2732
2733 sub = weston_subsurface_create(id, surface, parent);
2734 if (!sub) {
2735 wl_resource_post_no_memory(resource);
2736 return;
2737 }
2738
2739 surface->configure = subsurface_configure;
2740 surface->configure_private = sub;
2741}
2742
2743static void
2744subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2745{
2746 wl_resource_destroy(resource);
2747}
2748
2749static const struct wl_subcompositor_interface subcompositor_interface = {
2750 subcompositor_destroy,
2751 subcompositor_get_subsurface
2752};
2753
2754static void
2755bind_subcompositor(struct wl_client *client,
2756 void *data, uint32_t version, uint32_t id)
2757{
2758 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002759 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002760
Jason Ekstranda85118c2013-06-27 20:17:02 -05002761 resource =
2762 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002763 if (resource == NULL) {
2764 wl_client_post_no_memory(client);
2765 return;
2766 }
2767 wl_resource_set_implementation(resource, &subcompositor_interface,
2768 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002769}
2770
2771static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002772weston_compositor_dpms(struct weston_compositor *compositor,
2773 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002774{
2775 struct weston_output *output;
2776
2777 wl_list_for_each(output, &compositor->output_list, link)
2778 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002779 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002780}
2781
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002782WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002783weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002784{
Neil Roberts8b62e202013-09-30 13:14:47 +01002785 uint32_t old_state = compositor->state;
2786
2787 /* The state needs to be changed before emitting the wake
2788 * signal because that may try to schedule a repaint which
2789 * will not work if the compositor is still sleeping */
2790 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2791
2792 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002793 case WESTON_COMPOSITOR_SLEEPING:
2794 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2795 /* fall through */
2796 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002797 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002798 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002799 /* fall through */
2800 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002801 wl_event_source_timer_update(compositor->idle_source,
2802 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002803 }
2804}
2805
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002806WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002807weston_compositor_offscreen(struct weston_compositor *compositor)
2808{
2809 switch (compositor->state) {
2810 case WESTON_COMPOSITOR_OFFSCREEN:
2811 return;
2812 case WESTON_COMPOSITOR_SLEEPING:
2813 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2814 /* fall through */
2815 default:
2816 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2817 wl_event_source_timer_update(compositor->idle_source, 0);
2818 }
2819}
2820
2821WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002822weston_compositor_sleep(struct weston_compositor *compositor)
2823{
2824 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2825 return;
2826
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002827 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002828 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2829 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2830}
2831
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002832static int
2833idle_handler(void *data)
2834{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002835 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002836
2837 if (compositor->idle_inhibit)
2838 return 1;
2839
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002840 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002841 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002842
2843 return 1;
2844}
2845
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002846WL_EXPORT void
2847weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2848{
2849 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002850 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002851 plane->x = x;
2852 plane->y = y;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002853
2854 /* Init the link so that the call to wl_list_remove() when releasing
2855 * the plane without ever stacking doesn't lead to a crash */
2856 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002857}
2858
2859WL_EXPORT void
2860weston_plane_release(struct weston_plane *plane)
2861{
2862 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002863 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002864
2865 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002866}
2867
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002868WL_EXPORT void
2869weston_compositor_stack_plane(struct weston_compositor *ec,
2870 struct weston_plane *plane,
2871 struct weston_plane *above)
2872{
2873 if (above)
2874 wl_list_insert(above->link.prev, &plane->link);
2875 else
2876 wl_list_insert(&ec->plane_list, &plane->link);
2877}
2878
Casey Dahlin9074db52012-04-19 22:50:09 -04002879static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002880{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002881 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002882}
2883
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002884static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002885bind_output(struct wl_client *client,
2886 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002887{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002888 struct weston_output *output = data;
2889 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002890 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002891
Jason Ekstranda85118c2013-06-27 20:17:02 -05002892 resource = wl_resource_create(client, &wl_output_interface,
2893 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002894 if (resource == NULL) {
2895 wl_client_post_no_memory(client);
2896 return;
2897 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002898
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002899 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05002900 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04002901
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002902 wl_output_send_geometry(resource,
2903 output->x,
2904 output->y,
2905 output->mm_width,
2906 output->mm_height,
2907 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002908 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002909 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002910 if (version >= 2)
2911 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02002912 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002913
2914 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002915 wl_output_send_mode(resource,
2916 mode->flags,
2917 mode->width,
2918 mode->height,
2919 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002920 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02002921
2922 if (version >= 2)
2923 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002924}
2925
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002926WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002927weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002928{
Richard Hughes64ddde12013-05-01 21:52:10 +01002929 wl_signal_emit(&output->destroy_signal, output);
2930
Richard Hughesafe690c2013-05-02 10:10:04 +01002931 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002932 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002933 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002934 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002935
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04002936 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002937}
2938
Scott Moreau1bad5db2012-08-18 01:04:05 -06002939static void
2940weston_output_compute_transform(struct weston_output *output)
2941{
2942 struct weston_matrix transform;
2943 int flip;
2944
2945 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002946 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002947
2948 switch(output->transform) {
2949 case WL_OUTPUT_TRANSFORM_FLIPPED:
2950 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2951 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2952 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002953 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002954 flip = -1;
2955 break;
2956 default:
2957 flip = 1;
2958 break;
2959 }
2960
2961 switch(output->transform) {
2962 case WL_OUTPUT_TRANSFORM_NORMAL:
2963 case WL_OUTPUT_TRANSFORM_FLIPPED:
2964 transform.d[0] = flip;
2965 transform.d[1] = 0;
2966 transform.d[4] = 0;
2967 transform.d[5] = 1;
2968 break;
2969 case WL_OUTPUT_TRANSFORM_90:
2970 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2971 transform.d[0] = 0;
2972 transform.d[1] = -flip;
2973 transform.d[4] = 1;
2974 transform.d[5] = 0;
2975 break;
2976 case WL_OUTPUT_TRANSFORM_180:
2977 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2978 transform.d[0] = -flip;
2979 transform.d[1] = 0;
2980 transform.d[4] = 0;
2981 transform.d[5] = -1;
2982 break;
2983 case WL_OUTPUT_TRANSFORM_270:
2984 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2985 transform.d[0] = 0;
2986 transform.d[1] = flip;
2987 transform.d[4] = -1;
2988 transform.d[5] = 0;
2989 break;
2990 default:
2991 break;
2992 }
2993
2994 weston_matrix_multiply(&output->matrix, &transform);
2995}
2996
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002997WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002998weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002999{
Scott Moreau850ca422012-05-21 15:21:25 -06003000 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003001 struct weston_matrix camera;
3002 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003003
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003004 weston_matrix_init(&output->matrix);
3005 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003006 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3007 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003008
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003009 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003010 2.0 / (output->width + output->border.left + output->border.right),
3011 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3012
3013 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003014
Scott Moreauccbf29d2012-02-22 14:21:41 -07003015 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003016 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003017 weston_matrix_init(&camera);
3018 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003019 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003020 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003021 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003022 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003023 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003024 weston_matrix_multiply(&output->matrix, &modelview);
3025 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003026
Scott Moreauccbf29d2012-02-22 14:21:41 -07003027 output->dirty = 0;
3028}
3029
Scott Moreau1bad5db2012-08-18 01:04:05 -06003030static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003031weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003032{
3033 output->transform = transform;
3034
3035 switch (transform) {
3036 case WL_OUTPUT_TRANSFORM_90:
3037 case WL_OUTPUT_TRANSFORM_270:
3038 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3039 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3040 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003041 output->width = output->current_mode->height;
3042 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003043 break;
3044 case WL_OUTPUT_TRANSFORM_NORMAL:
3045 case WL_OUTPUT_TRANSFORM_180:
3046 case WL_OUTPUT_TRANSFORM_FLIPPED:
3047 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003048 output->width = output->current_mode->width;
3049 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003050 break;
3051 default:
3052 break;
3053 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003054
Hardening57388e42013-09-18 23:56:36 +02003055 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003056 output->width /= scale;
3057 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003058}
3059
Scott Moreauccbf29d2012-02-22 14:21:41 -07003060WL_EXPORT void
3061weston_output_move(struct weston_output *output, int x, int y)
3062{
3063 output->x = x;
3064 output->y = y;
3065
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003066 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003067 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003068 output->width,
3069 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003070}
3071
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003072WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003073weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003074 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003075 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003076{
3077 output->compositor = c;
3078 output->x = x;
3079 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003080 output->border.top = 0;
3081 output->border.bottom = 0;
3082 output->border.left = 0;
3083 output->border.right = 0;
Alexander Larsson0b135062013-05-28 16:23:36 +02003084 output->mm_width = mm_width;
3085 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003086 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003087 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003088
Alexander Larsson0b135062013-05-28 16:23:36 +02003089 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003090 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003091
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003092 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003093 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003094
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003095 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003096 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003097 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003098 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003099
Casey Dahlin58ba1372012-04-19 22:50:08 -04003100 output->id = ffs(~output->compositor->output_id_pool) - 1;
3101 output->compositor->output_id_pool |= 1 << output->id;
3102
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003103 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003104 wl_global_create(c->wl_display, &wl_output_interface, 2,
3105 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003106 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003107}
3108
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003109WL_EXPORT void
3110weston_output_transform_coordinate(struct weston_output *output,
3111 int device_x, int device_y,
3112 wl_fixed_t *x, wl_fixed_t *y)
3113{
3114 wl_fixed_t tx, ty;
3115 wl_fixed_t width, height;
3116
Hardeningff39efa2013-09-18 23:56:35 +02003117 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3118 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003119
3120 switch(output->transform) {
3121 case WL_OUTPUT_TRANSFORM_NORMAL:
3122 default:
3123 tx = wl_fixed_from_int(device_x);
3124 ty = wl_fixed_from_int(device_y);
3125 break;
3126 case WL_OUTPUT_TRANSFORM_90:
3127 tx = wl_fixed_from_int(device_y);
3128 ty = height - wl_fixed_from_int(device_x);
3129 break;
3130 case WL_OUTPUT_TRANSFORM_180:
3131 tx = width - wl_fixed_from_int(device_x);
3132 ty = height - wl_fixed_from_int(device_y);
3133 break;
3134 case WL_OUTPUT_TRANSFORM_270:
3135 tx = width - wl_fixed_from_int(device_y);
3136 ty = wl_fixed_from_int(device_x);
3137 break;
3138 case WL_OUTPUT_TRANSFORM_FLIPPED:
3139 tx = width - wl_fixed_from_int(device_x);
3140 ty = wl_fixed_from_int(device_y);
3141 break;
3142 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3143 tx = width - wl_fixed_from_int(device_y);
3144 ty = height - wl_fixed_from_int(device_x);
3145 break;
3146 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3147 tx = wl_fixed_from_int(device_x);
3148 ty = height - wl_fixed_from_int(device_y);
3149 break;
3150 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3151 tx = wl_fixed_from_int(device_y);
3152 ty = wl_fixed_from_int(device_x);
3153 break;
3154 }
3155
Hardeningff39efa2013-09-18 23:56:35 +02003156 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3157 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003158}
3159
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003160static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003161compositor_bind(struct wl_client *client,
3162 void *data, uint32_t version, uint32_t id)
3163{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003164 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003165 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003166
Jason Ekstranda85118c2013-06-27 20:17:02 -05003167 resource = wl_resource_create(client, &wl_compositor_interface,
3168 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003169 if (resource == NULL) {
3170 wl_client_post_no_memory(client);
3171 return;
3172 }
3173
3174 wl_resource_set_implementation(resource, &compositor_interface,
3175 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003176}
3177
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003178static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003179log_uname(void)
3180{
3181 struct utsname usys;
3182
3183 uname(&usys);
3184
3185 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3186 usys.version, usys.machine);
3187}
3188
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003189WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003190weston_environment_get_fd(const char *env)
3191{
3192 char *e, *end;
3193 int fd, flags;
3194
3195 e = getenv(env);
3196 if (!e)
3197 return -1;
3198 fd = strtol(e, &end, 0);
3199 if (*end != '\0')
3200 return -1;
3201
3202 flags = fcntl(fd, F_GETFD);
3203 if (flags == -1)
3204 return -1;
3205
3206 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3207 unsetenv(env);
3208
3209 return fd;
3210}
3211
3212WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003213weston_compositor_init(struct weston_compositor *ec,
3214 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003215 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003216 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003217{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003218 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003219 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003220 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003221
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003222 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003223 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003224 wl_signal_init(&ec->destroy_signal);
3225 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003226 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003227 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003228 wl_signal_init(&ec->idle_signal);
3229 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003230 wl_signal_init(&ec->show_input_panel_signal);
3231 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003232 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003233 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003234 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003235 wl_signal_init(&ec->session_signal);
3236 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003237
Casey Dahlin58ba1372012-04-19 22:50:08 -04003238 ec->output_id_pool = 0;
3239
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003240 if (!wl_global_create(display, &wl_compositor_interface, 3,
3241 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003242 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003243
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003244 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3245 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003246 return -1;
3247
Jason Ekstranda7af7042013-10-12 22:38:11 -05003248 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003249 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003250 wl_list_init(&ec->layer_list);
3251 wl_list_init(&ec->seat_list);
3252 wl_list_init(&ec->output_list);
3253 wl_list_init(&ec->key_binding_list);
3254 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003255 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003256 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003257 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003258
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003259 weston_plane_init(&ec->primary_plane, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003260 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003261
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003262 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3263 weston_config_section_get_string(s, "keymap_rules",
3264 (char **) &xkb_names.rules, NULL);
3265 weston_config_section_get_string(s, "keymap_model",
3266 (char **) &xkb_names.model, NULL);
3267 weston_config_section_get_string(s, "keymap_layout",
3268 (char **) &xkb_names.layout, NULL);
3269 weston_config_section_get_string(s, "keymap_variant",
3270 (char **) &xkb_names.variant, NULL);
3271 weston_config_section_get_string(s, "keymap_options",
3272 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003273
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003274 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3275 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003276
3277 ec->ping_handler = NULL;
3278
3279 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003280 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003281
3282 wl_data_device_manager_init(ec->wl_display);
3283
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003284 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003285
Daniel Stone725c2c32012-06-22 14:04:36 +01003286 loop = wl_display_get_event_loop(ec->wl_display);
3287 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3288 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3289
3290 ec->input_loop = wl_event_loop_create();
3291
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003292 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3293 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3294
3295 weston_compositor_schedule_repaint(ec);
3296
Daniel Stone725c2c32012-06-22 14:04:36 +01003297 return 0;
3298}
3299
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003300WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003301weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003302{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003303 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003304
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003305 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003306 if (ec->input_loop_source)
3307 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003308
Matt Roper361d2ad2011-08-29 13:52:23 -07003309 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003310 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003311 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003312
Daniel Stone325fc2d2012-05-30 16:31:58 +01003313 weston_binding_list_destroy_all(&ec->key_binding_list);
3314 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003315 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003316 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003317 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003318
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003319 weston_plane_release(&ec->primary_plane);
3320
Jonas Ådahlc97af922012-03-28 22:36:09 +02003321 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003322
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003323 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003324}
3325
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003326WL_EXPORT void
3327weston_version(int *major, int *minor, int *micro)
3328{
3329 *major = WESTON_VERSION_MAJOR;
3330 *minor = WESTON_VERSION_MINOR;
3331 *micro = WESTON_VERSION_MICRO;
3332}
3333
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003334static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003335 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003336 const char *desc;
3337} capability_strings[] = {
3338 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003339 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003340};
3341
3342static void
3343weston_compositor_log_capabilities(struct weston_compositor *compositor)
3344{
3345 unsigned i;
3346 int yes;
3347
3348 weston_log("Compositor capabilities:\n");
3349 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3350 yes = compositor->capabilities & capability_strings[i].bit;
3351 weston_log_continue(STAMP_SPACE "%s %s\n",
3352 capability_strings[i].desc,
3353 yes ? "yes" : "no");
3354 }
3355}
3356
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003357static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003358{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003359 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003360
Martin Minarik6d118362012-06-07 18:01:59 +02003361 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003362 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003363
3364 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003365}
3366
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003367#ifdef HAVE_LIBUNWIND
3368
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003369static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003370print_backtrace(void)
3371{
3372 unw_cursor_t cursor;
3373 unw_context_t context;
3374 unw_word_t off;
3375 unw_proc_info_t pip;
3376 int ret, i = 0;
3377 char procname[256];
3378 const char *filename;
3379 Dl_info dlinfo;
3380
3381 pip.unwind_info = NULL;
3382 ret = unw_getcontext(&context);
3383 if (ret) {
3384 weston_log("unw_getcontext: %d\n", ret);
3385 return;
3386 }
3387
3388 ret = unw_init_local(&cursor, &context);
3389 if (ret) {
3390 weston_log("unw_init_local: %d\n", ret);
3391 return;
3392 }
3393
3394 ret = unw_step(&cursor);
3395 while (ret > 0) {
3396 ret = unw_get_proc_info(&cursor, &pip);
3397 if (ret) {
3398 weston_log("unw_get_proc_info: %d\n", ret);
3399 break;
3400 }
3401
3402 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3403 if (ret && ret != -UNW_ENOMEM) {
3404 if (ret != -UNW_EUNSPEC)
3405 weston_log("unw_get_proc_name: %d\n", ret);
3406 procname[0] = '?';
3407 procname[1] = 0;
3408 }
3409
3410 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3411 *dlinfo.dli_fname)
3412 filename = dlinfo.dli_fname;
3413 else
3414 filename = "?";
3415
3416 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3417 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3418
3419 ret = unw_step(&cursor);
3420 if (ret < 0)
3421 weston_log("unw_step: %d\n", ret);
3422 }
3423}
3424
3425#else
3426
3427static void
3428print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003429{
3430 void *buffer[32];
3431 int i, count;
3432 Dl_info info;
3433
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003434 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3435 for (i = 0; i < count; i++) {
3436 dladdr(buffer[i], &info);
3437 weston_log(" [%016lx] %s (%s)\n",
3438 (long) buffer[i],
3439 info.dli_sname ? info.dli_sname : "--",
3440 info.dli_fname);
3441 }
3442}
3443
3444#endif
3445
3446static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003447on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003448{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003449 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003450 * then call the backend restore function, which will switch
3451 * back to the vt we launched from or ungrab X etc and then
3452 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003453 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003454 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003455 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003456
Peter Maatmane5b42e42013-03-27 22:38:53 +01003457 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003458
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003459 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003460
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003461 segv_compositor->restore(segv_compositor);
3462
3463 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003464}
3465
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003466WL_EXPORT void *
3467weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003468{
3469 char path[PATH_MAX];
3470 void *module, *init;
3471
3472 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003473 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003474 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003475 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003476
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003477 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3478 if (module) {
3479 weston_log("Module '%s' already loaded\n", path);
3480 dlclose(module);
3481 return NULL;
3482 }
3483
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003484 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003485 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003486 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003487 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003488 return NULL;
3489 }
3490
3491 init = dlsym(module, entrypoint);
3492 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003493 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003494 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003495 return NULL;
3496 }
3497
3498 return init;
3499}
3500
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003501static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003502load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003503 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003504{
3505 const char *p, *end;
3506 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003507 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003508 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003509
3510 if (modules == NULL)
3511 return 0;
3512
3513 p = modules;
3514 while (*p) {
3515 end = strchrnul(p, ',');
3516 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003517 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003518 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003519 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003520 p = end;
3521 while (*p == ',')
3522 p++;
3523
3524 }
3525
3526 return 0;
3527}
3528
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003529static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003530 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3531
3532static const char xdg_wrong_message[] =
3533 "fatal: environment variable XDG_RUNTIME_DIR\n"
3534 "is set to \"%s\", which is not a directory.\n";
3535
3536static const char xdg_wrong_mode_message[] =
3537 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3538 "correctly. Unix access mode must be 0700 but is %o,\n"
3539 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003540 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003541
3542static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003543 "Refer to your distribution on how to get it, or\n"
3544 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3545 "on how to implement it.\n";
3546
Martin Minarik37032f82012-06-18 20:15:18 +02003547static void
3548verify_xdg_runtime_dir(void)
3549{
3550 char *dir = getenv("XDG_RUNTIME_DIR");
3551 struct stat s;
3552
3553 if (!dir) {
3554 weston_log(xdg_error_message);
3555 weston_log_continue(xdg_detail_message);
3556 exit(EXIT_FAILURE);
3557 }
3558
3559 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3560 weston_log(xdg_wrong_message, dir);
3561 weston_log_continue(xdg_detail_message);
3562 exit(EXIT_FAILURE);
3563 }
3564
3565 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3566 weston_log(xdg_wrong_mode_message,
3567 dir, s.st_mode & 0777, s.st_uid);
3568 weston_log_continue(xdg_detail_message);
3569 }
3570}
3571
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003572static int
3573usage(int error_code)
3574{
3575 fprintf(stderr,
3576 "Usage: weston [OPTIONS]\n\n"
3577 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3578 "Weston supports multiple backends, and depending on which backend is in use\n"
3579 "different options will be accepted.\n\n"
3580
3581
3582 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003583 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003584 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003585 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3586 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003587 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003588 " -S, --socket=NAME\tName of socket to listen on\n"
3589 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003590 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003591 " --log==FILE\t\tLog to the given file\n"
3592 " -h, --help\t\tThis help message\n\n");
3593
3594 fprintf(stderr,
3595 "Options for drm-backend.so:\n\n"
3596 " --connector=ID\tBring up only this connector\n"
3597 " --seat=SEAT\t\tThe seat that weston should run on\n"
3598 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003599 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003600 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3601
3602 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003603 "Options for fbdev-backend.so:\n\n"
3604 " --tty=TTY\t\tThe tty to use\n"
3605 " --device=DEVICE\tThe framebuffer device to use\n\n");
3606
3607 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003608 "Options for x11-backend.so:\n\n"
3609 " --width=WIDTH\t\tWidth of X window\n"
3610 " --height=HEIGHT\tHeight of X window\n"
3611 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003612 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003613 " --output-count=COUNT\tCreate multiple outputs\n"
3614 " --no-input\t\tDont create input devices\n\n");
3615
3616 fprintf(stderr,
3617 "Options for wayland-backend.so:\n\n"
3618 " --width=WIDTH\t\tWidth of Wayland surface\n"
3619 " --height=HEIGHT\tHeight of Wayland surface\n"
3620 " --display=DISPLAY\tWayland display to connect to\n\n");
3621
Pekka Paalanene31e0532013-05-22 18:03:07 +03003622#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3623 fprintf(stderr,
3624 "Options for rpi-backend.so:\n\n"
3625 " --tty=TTY\t\tThe tty to use\n"
3626 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3627 " --transform=TR\tThe output transformation, TR is one of:\n"
3628 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
3629 "\n");
3630#endif
3631
Hardeningc077a842013-07-08 00:51:35 +02003632#if defined(BUILD_RDP_COMPOSITOR)
3633 fprintf(stderr,
3634 "Options for rdp-backend.so:\n\n"
3635 " --width=WIDTH\t\tWidth of desktop\n"
3636 " --height=HEIGHT\tHeight of desktop\n"
3637 " --extra-modes=MODES\t\n"
3638 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3639 " --address=ADDR\tThe address to bind\n"
3640 " --port=PORT\tThe port to listen on\n"
3641 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3642 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3643 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3644 "\n");
3645#endif
3646
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003647 exit(error_code);
3648}
3649
Peter Maatmane5b42e42013-03-27 22:38:53 +01003650static void
3651catch_signals(void)
3652{
3653 struct sigaction action;
3654
3655 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3656 action.sa_sigaction = on_caught_signal;
3657 sigemptyset(&action.sa_mask);
3658 sigaction(SIGSEGV, &action, NULL);
3659 sigaction(SIGABRT, &action, NULL);
3660}
3661
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003662int main(int argc, char *argv[])
3663{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003664 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003665 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003666 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003667 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003668 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003669 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003670 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003671 int *argc, char *argv[],
3672 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003673 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003674 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003675 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003676 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003677 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003678 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003679 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003680 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003681 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003682 struct weston_config *config;
3683 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003684
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003685 const struct weston_option core_options[] = {
3686 { WESTON_OPTION_STRING, "backend", 'B', &backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003687 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003688 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3689 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003690 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003691 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003692 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003693 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003694 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003695
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003696 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003697
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003698 if (help)
3699 usage(EXIT_SUCCESS);
3700
Scott Moreau12245142012-08-29 15:15:58 -06003701 if (version) {
3702 printf(PACKAGE_STRING "\n");
3703 return EXIT_SUCCESS;
3704 }
3705
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003706 weston_log_file_open(log);
3707
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003708 weston_log("%s\n"
3709 STAMP_SPACE "%s\n"
3710 STAMP_SPACE "Bug reports to: %s\n"
3711 STAMP_SPACE "Build: %s\n",
3712 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003713 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003714 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003715
Martin Minarik37032f82012-06-18 20:15:18 +02003716 verify_xdg_runtime_dir();
3717
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003718 display = wl_display_create();
3719
Tiago Vignatti2116b892011-08-08 05:52:59 -07003720 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003721 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3722 display);
3723 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3724 display);
3725 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3726 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003727
3728 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003729 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3730 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003731
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003732 if (!backend) {
3733 if (getenv("WAYLAND_DISPLAY"))
3734 backend = "wayland-backend.so";
3735 else if (getenv("DISPLAY"))
3736 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003737 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003738 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003739 }
3740
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003741 config = weston_config_parse("weston.ini");
Alexandru DAMIAN5f429302013-09-26 10:27:16 +01003742 if (config != NULL) {
3743 weston_log("Using config file '%s'\n",
3744 weston_config_get_full_path(config));
3745 } else {
3746 weston_log("Starting with no config file.\n");
3747 }
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003748 section = weston_config_get_section(config, "core", NULL, NULL);
Jason Ekstranda985da42013-08-22 17:28:03 -05003749 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003750
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003751 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003752 if (!backend_init)
3753 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003754
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003755 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003756 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003757 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003758 exit(EXIT_FAILURE);
3759 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003760
Peter Maatmane5b42e42013-03-27 22:38:53 +01003761 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003762 segv_compositor = ec;
3763
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003764 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003765
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003766 setenv("WAYLAND_DISPLAY", socket_name, 1);
3767
Jason Ekstranda985da42013-08-22 17:28:03 -05003768 if (!shell)
3769 weston_config_section_get_string(section, "shell", &shell,
3770 "desktop-shell.so");
3771 if (load_modules(ec, shell, &argc, argv) < 0)
3772 goto out;
3773
Ossama Othmana50e6e42013-05-14 09:48:26 -07003774 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003775 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003776 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003777 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003778
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003779 for (i = 1; i < argc; i++)
3780 weston_log("fatal: unhandled option: %s\n", argv[i]);
3781 if (argc > 1) {
3782 ret = EXIT_FAILURE;
3783 goto out;
3784 }
3785
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003786 weston_compositor_log_capabilities(ec);
3787
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003788 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003789 weston_log("fatal: failed to add socket: %m\n");
3790 ret = EXIT_FAILURE;
3791 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003792 }
3793
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003794 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003795
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003796 wl_display_run(display);
3797
3798 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003799 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003800 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003801
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003802 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003803
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003804 for (i = ARRAY_LENGTH(signals); i;)
3805 wl_event_source_remove(signals[--i]);
3806
Daniel Stone855028f2012-05-01 20:37:10 +01003807 weston_compositor_xkb_destroy(ec);
3808
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003809 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003810 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003811
Martin Minarik19e6f262012-06-07 13:08:46 +02003812 weston_log_file_close();
3813
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003814 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003815}