blob: af33fdae8299cd29e17ac7208f2eb21762b7c761 [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"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030056#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040057#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050058#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050059
Kristian Høgsberg27da5382011-06-21 17:32:25 -040060static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040061static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062
63static int
64sigchld_handler(int signal_number, void *data)
65{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050066 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040067 int status;
68 pid_t pid;
69
Bill Spitzak027b9622012-03-17 15:22:03 -070070 pid = waitpid(-1, &status, WNOHANG);
71 if (!pid)
72 return 1;
73
Kristian Høgsberg27da5382011-06-21 17:32:25 -040074 wl_list_for_each(p, &child_process_list, link) {
75 if (p->pid == pid)
76 break;
77 }
78
79 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020080 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040081 return 1;
82 }
83
84 wl_list_remove(&p->link);
85 p->cleanup(p, status);
86
87 return 1;
88}
89
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020090static void
Alexander Larsson0b135062013-05-28 16:23:36 +020091weston_output_transform_scale_init(struct weston_output *output,
92 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093
Rob Bradford27b17932013-06-26 18:08:46 +010094static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050095weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010096
Alex Wu2dda6042012-04-17 17:20:47 +080097WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020098weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
99 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800100{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200101 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200102 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200103 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200104 int ret, notify_mode_changed, notify_scale_changed;
105 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200106
Alex Wu2dda6042012-04-17 17:20:47 +0800107 if (!output->switch_mode)
108 return -1;
109
Hardening57388e42013-09-18 23:56:36 +0200110 temporary_mode = (output->original_mode != 0);
111 temporary_scale = (output->current_scale != output->original_scale);
112 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113
Hardening57388e42013-09-18 23:56:36 +0200114 notify_mode_changed = 0;
115 notify_scale_changed = 0;
116 switch(op) {
117 case WESTON_MODE_SWITCH_SET_NATIVE:
118 output->native_mode = mode;
119 if (!temporary_mode) {
120 notify_mode_changed = 1;
121 ret = output->switch_mode(output, mode);
122 if (ret < 0)
123 return ret;
124 }
125
126 output->native_scale = scale;
127 if(!temporary_scale)
128 notify_scale_changed = 1;
129 break;
130 case WESTON_MODE_SWITCH_SET_TEMPORARY:
131 if (!temporary_mode)
132 output->original_mode = output->native_mode;
133 if (!temporary_scale)
134 output->original_scale = output->native_scale;
135
136 ret = output->switch_mode(output, mode);
137 if (ret < 0)
138 return ret;
139
Hardening57388e42013-09-18 23:56:36 +0200140 output->current_scale = scale;
141 break;
142 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
143 if (!temporary_mode) {
144 weston_log("already in the native mode\n");
145 return -1;
146 }
147
148 notify_mode_changed = (output->original_mode != output->native_mode);
149
150 ret = output->switch_mode(output, mode);
151 if (ret < 0)
152 return ret;
153
154 if (output->original_scale != output->native_scale) {
155 notify_scale_changed = 1;
156 scale = output->native_scale;
157 output->original_scale = scale;
158 }
159 output->original_mode = 0;
160
161 output->current_scale = output->native_scale;
162 break;
163 default:
164 weston_log("unknown weston_switch_mode_op %d\n", op);
165 break;
166 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200167
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200168 pixman_region32_init(&old_output_region);
169 pixman_region32_copy(&old_output_region, &output->region);
170
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200171 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200172 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200173
174 pixman_region32_init(&output->previous_damage);
175 pixman_region32_init_rect(&output->region, output->x, output->y,
176 output->width, output->height);
177
178 weston_output_update_matrix(output);
179
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200180 /* If a pointer falls outside the outputs new geometry, move it to its
181 * lower-right corner */
182 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400183 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200184 int32_t x, y;
185
186 if (!pointer)
187 continue;
188
189 x = wl_fixed_to_int(pointer->x);
190 y = wl_fixed_to_int(pointer->y);
191
192 if (!pixman_region32_contains_point(&old_output_region,
193 x, y, NULL) ||
194 pixman_region32_contains_point(&output->region,
195 x, y, NULL))
196 continue;
197
198 if (x >= output->x + output->width)
199 x = output->x + output->width - 1;
200 if (y >= output->y + output->height)
201 y = output->y + output->height - 1;
202
203 pointer->x = wl_fixed_from_int(x);
204 pointer->y = wl_fixed_from_int(y);
205 }
206
207 pixman_region32_fini(&old_output_region);
208
Hardening57388e42013-09-18 23:56:36 +0200209 /* notify clients of the changes */
210 if (notify_mode_changed || notify_scale_changed) {
211 wl_resource_for_each(resource, &output->resource_list) {
212 if(notify_mode_changed) {
213 wl_output_send_mode(resource,
214 mode->flags | WL_OUTPUT_MODE_CURRENT,
215 mode->width,
216 mode->height,
217 mode->refresh);
218 }
219
220 if (notify_scale_changed)
221 wl_output_send_scale(resource, scale);
222
223 if (wl_resource_get_version(resource) >= 2)
224 wl_output_send_done(resource);
225 }
226 }
227
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200228 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800229}
230
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400231WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500232weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400233{
234 wl_list_insert(&child_process_list, &process->link);
235}
236
Benjamin Franzke06286262011-05-06 19:12:33 +0200237static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200238child_client_exec(int sockfd, const char *path)
239{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500240 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200241 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200242 sigset_t allsigs;
243
244 /* do not give our signal mask to the new process */
245 sigfillset(&allsigs);
246 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200247
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100248 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
249 if (seteuid(getuid()) == -1) {
250 weston_log("compositor: failed seteuid\n");
251 return;
252 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500253
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500254 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
255 * non-CLOEXEC fd to pass through exec. */
256 clientfd = dup(sockfd);
257 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200258 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500259 return;
260 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200261
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500262 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200263 setenv("WAYLAND_SOCKET", s, 1);
264
265 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200266 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200267 path);
268}
269
270WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500271weston_client_launch(struct weston_compositor *compositor,
272 struct weston_process *proc,
273 const char *path,
274 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200275{
276 int sv[2];
277 pid_t pid;
278 struct wl_client *client;
279
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300280 weston_log("launching '%s'\n", path);
281
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300282 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200283 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200284 "socketpair failed while launching '%s': %m\n",
285 path);
286 return NULL;
287 }
288
289 pid = fork();
290 if (pid == -1) {
291 close(sv[0]);
292 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200293 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200294 "fork failed while launching '%s': %m\n", path);
295 return NULL;
296 }
297
298 if (pid == 0) {
299 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700300 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200301 }
302
303 close(sv[1]);
304
305 client = wl_client_create(compositor->wl_display, sv[0]);
306 if (!client) {
307 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200308 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200309 "wl_client_create failed while launching '%s'.\n",
310 path);
311 return NULL;
312 }
313
314 proc->pid = pid;
315 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500316 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200317
318 return client;
319}
320
321static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300322surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
323{
324 struct weston_surface *surface =
325 container_of(listener, struct weston_surface,
326 pending.buffer_destroy_listener);
327
328 surface->pending.buffer = NULL;
329}
330
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200331static void
332empty_region(pixman_region32_t *region)
333{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300334 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200335 pixman_region32_init(region);
336}
337
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300338static void
339region_init_infinite(pixman_region32_t *region)
340{
341 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
342 UINT32_MAX, UINT32_MAX);
343}
344
Pekka Paalanene67858b2013-04-25 13:57:42 +0300345static struct weston_subsurface *
346weston_surface_to_subsurface(struct weston_surface *surface);
347
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200348static void
349weston_view_output_move_handler(struct wl_listener *listener,
350 void *data)
351{
352 struct weston_view *ev;
353 struct weston_output *output = data;
354
355 ev = container_of(listener, struct weston_view,
356 output_move_listener);
357
358 /* the child window's view->geometry is a relative coordinate to
359 * parent view, no need to move child_view. */
360 if (ev->geometry.parent)
361 return;
362
363 weston_view_set_position(ev,
364 ev->geometry.x + output->move_x,
365 ev->geometry.y + output->move_y);
366}
367
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200368static void
369weston_view_output_destroy_handler(struct wl_listener *listener,
370 void *data)
371{
372 struct weston_view *ev;
373 struct weston_compositor *ec;
374 struct weston_output *output, *first_output;
375 float x, y;
376 int visible;
377
378 ev = container_of(listener, struct weston_view,
379 output_destroy_listener);
380
381 ec = ev->surface->compositor;
382
383 /* the child window's view->geometry is a relative coordinate to
384 * parent view, no need to move child_view. */
385 if (ev->geometry.parent)
386 return;
387
388 x = ev->geometry.x;
389 y = ev->geometry.y;
390
391 /* At this point the destroyed output is not in the list anymore.
392 * If the view is still visible somewhere, we leave where it is,
393 * otherwise, move it to the first output. */
394 visible = 0;
395 wl_list_for_each(output, &ec->output_list, link) {
396 if (pixman_region32_contains_point(&output->region,
397 x, y, NULL)) {
398 visible = 1;
399 break;
400 }
401 }
402
403 if (!visible) {
404 first_output = container_of(ec->output_list.next,
405 struct weston_output, link);
406
407 x = first_output->x + first_output->width / 4;
408 y = first_output->y + first_output->height / 4;
409 }
410
411 weston_view_set_position(ev, x, y);
412}
413
Jason Ekstranda7af7042013-10-12 22:38:11 -0500414WL_EXPORT struct weston_view *
415weston_view_create(struct weston_surface *surface)
416{
417 struct weston_view *view;
418
419 view = calloc(1, sizeof *view);
420 if (view == NULL)
421 return NULL;
422
423 view->surface = surface;
424
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425 /* Assign to surface */
426 wl_list_insert(&surface->views, &view->surface_link);
427
428 wl_signal_init(&view->destroy_signal);
429 wl_list_init(&view->link);
430 wl_list_init(&view->layer_link);
431
Xiong Zhang97116532013-10-23 13:58:31 +0800432 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500433
434 pixman_region32_init(&view->clip);
435
436 view->alpha = 1.0;
437 pixman_region32_init(&view->transform.opaque);
438
439 wl_list_init(&view->geometry.transformation_list);
440 wl_list_insert(&view->geometry.transformation_list,
441 &view->transform.position.link);
442 weston_matrix_init(&view->transform.position.matrix);
443 wl_list_init(&view->geometry.child_list);
444 pixman_region32_init(&view->transform.boundingbox);
445 view->transform.dirty = 1;
446
447 view->output = NULL;
448
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200449 view->output_move_listener.notify = weston_view_output_move_handler;
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200450 view->output_destroy_listener.notify =
451 weston_view_output_destroy_handler;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200452
Jason Ekstranda7af7042013-10-12 22:38:11 -0500453 return view;
454}
455
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500456WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500457weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500458{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500459 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400460
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200461 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400462 if (surface == NULL)
463 return NULL;
464
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500465 wl_signal_init(&surface->destroy_signal);
466
467 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500468
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500469 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200470 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400471
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100472 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
473 surface->buffer_viewport.scale = 1;
474 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400475 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100476 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200477
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400478 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500479 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300480 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400481
Jason Ekstranda7af7042013-10-12 22:38:11 -0500482 wl_list_init(&surface->views);
483
484 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500485
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300486 surface->pending.buffer_destroy_listener.notify =
487 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300488 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300489 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300490 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300491 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300492
Pekka Paalanene67858b2013-04-25 13:57:42 +0300493 wl_list_init(&surface->subsurface_list);
494 wl_list_init(&surface->subsurface_list_pending);
495
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400496 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500497}
498
Alex Wu8811bf92012-02-28 18:07:54 +0800499WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500500weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200501 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500502{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100503 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500504}
505
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400506WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500507weston_view_to_global_float(struct weston_view *view,
508 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200509{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200511 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
512
Jason Ekstranda7af7042013-10-12 22:38:11 -0500513 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200514
515 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200516 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700517 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200518 v.f[3]);
519 *x = 0;
520 *y = 0;
521 return;
522 }
523
524 *x = v.f[0] / v.f[3];
525 *y = v.f[1] / v.f[3];
526 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500527 *x = sx + view->geometry.x;
528 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200529 }
530}
531
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500532WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200533weston_transformed_coord(int width, int height,
534 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200535 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200536 float sx, float sy, float *bx, float *by)
537{
538 switch (transform) {
539 case WL_OUTPUT_TRANSFORM_NORMAL:
540 default:
541 *bx = sx;
542 *by = sy;
543 break;
544 case WL_OUTPUT_TRANSFORM_FLIPPED:
545 *bx = width - sx;
546 *by = sy;
547 break;
548 case WL_OUTPUT_TRANSFORM_90:
549 *bx = height - sy;
550 *by = sx;
551 break;
552 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
553 *bx = height - sy;
554 *by = width - sx;
555 break;
556 case WL_OUTPUT_TRANSFORM_180:
557 *bx = width - sx;
558 *by = height - sy;
559 break;
560 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
561 *bx = sx;
562 *by = height - sy;
563 break;
564 case WL_OUTPUT_TRANSFORM_270:
565 *bx = sy;
566 *by = width - sx;
567 break;
568 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
569 *bx = sy;
570 *by = sx;
571 break;
572 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200573
574 *bx *= scale;
575 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200576}
577
578WL_EXPORT pixman_box32_t
579weston_transformed_rect(int width, int height,
580 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200581 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200582 pixman_box32_t rect)
583{
584 float x1, x2, y1, y2;
585
586 pixman_box32_t ret;
587
Alexander Larsson4ea95522013-05-22 14:41:37 +0200588 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200589 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200590 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200591 rect.x2, rect.y2, &x2, &y2);
592
593 if (x1 <= x2) {
594 ret.x1 = x1;
595 ret.x2 = x2;
596 } else {
597 ret.x1 = x2;
598 ret.x2 = x1;
599 }
600
601 if (y1 <= y2) {
602 ret.y1 = y1;
603 ret.y2 = y2;
604 } else {
605 ret.y1 = y2;
606 ret.y2 = y1;
607 }
608
609 return ret;
610}
611
612WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500613weston_transformed_region(int width, int height,
614 enum wl_output_transform transform,
615 int32_t scale,
616 pixman_region32_t *src, pixman_region32_t *dest)
617{
618 pixman_box32_t *src_rects, *dest_rects;
619 int nrects, i;
620
621 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
622 if (src != dest)
623 pixman_region32_copy(dest, src);
624 return;
625 }
626
627 src_rects = pixman_region32_rectangles(src, &nrects);
628 dest_rects = malloc(nrects * sizeof(*dest_rects));
629 if (!dest_rects)
630 return;
631
632 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
633 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
634 } else {
635 for (i = 0; i < nrects; i++) {
636 switch (transform) {
637 default:
638 case WL_OUTPUT_TRANSFORM_NORMAL:
639 dest_rects[i].x1 = src_rects[i].x1;
640 dest_rects[i].y1 = src_rects[i].y1;
641 dest_rects[i].x2 = src_rects[i].x2;
642 dest_rects[i].y2 = src_rects[i].y2;
643 break;
644 case WL_OUTPUT_TRANSFORM_90:
645 dest_rects[i].x1 = height - src_rects[i].y2;
646 dest_rects[i].y1 = src_rects[i].x1;
647 dest_rects[i].x2 = height - src_rects[i].y1;
648 dest_rects[i].y2 = src_rects[i].x2;
649 break;
650 case WL_OUTPUT_TRANSFORM_180:
651 dest_rects[i].x1 = width - src_rects[i].x2;
652 dest_rects[i].y1 = height - src_rects[i].y2;
653 dest_rects[i].x2 = width - src_rects[i].x1;
654 dest_rects[i].y2 = height - src_rects[i].y1;
655 break;
656 case WL_OUTPUT_TRANSFORM_270:
657 dest_rects[i].x1 = src_rects[i].y1;
658 dest_rects[i].y1 = width - src_rects[i].x2;
659 dest_rects[i].x2 = src_rects[i].y2;
660 dest_rects[i].y2 = width - src_rects[i].x1;
661 break;
662 case WL_OUTPUT_TRANSFORM_FLIPPED:
663 dest_rects[i].x1 = width - src_rects[i].x2;
664 dest_rects[i].y1 = src_rects[i].y1;
665 dest_rects[i].x2 = width - src_rects[i].x1;
666 dest_rects[i].y2 = src_rects[i].y2;
667 break;
668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
669 dest_rects[i].x1 = height - src_rects[i].y2;
670 dest_rects[i].y1 = width - src_rects[i].x2;
671 dest_rects[i].x2 = height - src_rects[i].y1;
672 dest_rects[i].y2 = width - src_rects[i].x1;
673 break;
674 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
675 dest_rects[i].x1 = src_rects[i].x1;
676 dest_rects[i].y1 = height - src_rects[i].y2;
677 dest_rects[i].x2 = src_rects[i].x2;
678 dest_rects[i].y2 = height - src_rects[i].y1;
679 break;
680 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
681 dest_rects[i].x1 = src_rects[i].y1;
682 dest_rects[i].y1 = src_rects[i].x1;
683 dest_rects[i].x2 = src_rects[i].y2;
684 dest_rects[i].y2 = src_rects[i].x2;
685 break;
686 }
687 }
688 }
689
690 if (scale != 1) {
691 for (i = 0; i < nrects; i++) {
692 dest_rects[i].x1 *= scale;
693 dest_rects[i].x2 *= scale;
694 dest_rects[i].y1 *= scale;
695 dest_rects[i].y2 *= scale;
696 }
697 }
698
699 pixman_region32_clear(dest);
700 pixman_region32_init_rects(dest, dest_rects, nrects);
701 free(dest_rects);
702}
703
704WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200705weston_surface_to_buffer_float(struct weston_surface *surface,
706 float sx, float sy, float *bx, float *by)
707{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500708 weston_transformed_coord(surface->width,
709 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100710 surface->buffer_viewport.transform,
711 surface->buffer_viewport.scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200712 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200713}
714
Alexander Larsson4ea95522013-05-22 14:41:37 +0200715WL_EXPORT void
716weston_surface_to_buffer(struct weston_surface *surface,
717 int sx, int sy, int *bx, int *by)
718{
719 float bxf, byf;
720
Jason Ekstranda7af7042013-10-12 22:38:11 -0500721 weston_transformed_coord(surface->width,
722 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100723 surface->buffer_viewport.transform,
724 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200725 sx, sy, &bxf, &byf);
726 *bx = floorf(bxf);
727 *by = floorf(byf);
728}
729
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200730WL_EXPORT pixman_box32_t
731weston_surface_to_buffer_rect(struct weston_surface *surface,
732 pixman_box32_t rect)
733{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500734 return weston_transformed_rect(surface->width,
735 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100736 surface->buffer_viewport.transform,
737 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200738 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200739}
740
741WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500742weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400743 struct weston_plane *plane)
744{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500745 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400746 return;
747
Jason Ekstranda7af7042013-10-12 22:38:11 -0500748 weston_view_damage_below(view);
749 view->plane = plane;
750 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400751}
752
753WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500754weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200755{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500756 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200757
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500758 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500759 pixman_region32_subtract(&damage, &view->transform.boundingbox,
760 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800761 if (view->plane)
762 pixman_region32_union(&view->plane->damage,
763 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500764 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800765 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200766}
767
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400768static void
769weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
770{
771 uint32_t different = es->output_mask ^ mask;
772 uint32_t entered = mask & different;
773 uint32_t left = es->output_mask & different;
774 struct weston_output *output;
775 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500776 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400777
778 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500779 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400780 return;
781 if (different == 0)
782 return;
783
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500784 client = wl_resource_get_client(es->resource);
785
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400786 wl_list_for_each(output, &es->compositor->output_list, link) {
787 if (1 << output->id & different)
788 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500789 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400790 client);
791 if (resource == NULL)
792 continue;
793 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500794 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400795 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500796 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400797 }
798}
799
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200800
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400801static void
802weston_surface_assign_output(struct weston_surface *es)
803{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500804 struct weston_output *new_output;
805 struct weston_view *view;
806 pixman_region32_t region;
807 uint32_t max, area, mask;
808 pixman_box32_t *e;
809
810 new_output = NULL;
811 max = 0;
812 mask = 0;
813 pixman_region32_init(&region);
814 wl_list_for_each(view, &es->views, surface_link) {
815 if (!view->output)
816 continue;
817
818 pixman_region32_intersect(&region, &view->transform.boundingbox,
819 &view->output->region);
820
821 e = pixman_region32_extents(&region);
822 area = (e->x2 - e->x1) * (e->y2 - e->y1);
823
824 mask |= view->output_mask;
825
826 if (area >= max) {
827 new_output = view->output;
828 max = area;
829 }
830 }
831 pixman_region32_fini(&region);
832
833 es->output = new_output;
834 weston_surface_update_output_mask(es, mask);
835}
836
837static void
838weston_view_assign_output(struct weston_view *ev)
839{
840 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400841 struct weston_output *output, *new_output;
842 pixman_region32_t region;
843 uint32_t max, area, mask;
844 pixman_box32_t *e;
845
846 new_output = NULL;
847 max = 0;
848 mask = 0;
849 pixman_region32_init(&region);
850 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500851 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400852 &output->region);
853
854 e = pixman_region32_extents(&region);
855 area = (e->x2 - e->x1) * (e->y2 - e->y1);
856
857 if (area > 0)
858 mask |= 1 << output->id;
859
860 if (area >= max) {
861 new_output = output;
862 max = area;
863 }
864 }
865 pixman_region32_fini(&region);
866
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200867 if (ev->output_mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200868 wl_list_remove(&ev->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200869 wl_list_remove(&ev->output_destroy_listener.link);
870 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200871
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200872 if (mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200873 wl_signal_add(&new_output->move_signal,
874 &ev->output_move_listener);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200875 wl_signal_add(&new_output->destroy_signal,
876 &ev->output_destroy_listener);
877 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200878
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 ev->output = new_output;
880 ev->output_mask = mask;
881
882 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400883}
884
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200885static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500886view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
887 int32_t width, int32_t height,
888 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200889{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200890 float min_x = HUGE_VALF, min_y = HUGE_VALF;
891 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200892 int32_t s[4][2] = {
893 { sx, sy },
894 { sx, sy + height },
895 { sx + width, sy },
896 { sx + width, sy + height }
897 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200898 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200899 int i;
900
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300901 if (width == 0 || height == 0) {
902 /* avoid rounding empty bbox to 1x1 */
903 pixman_region32_init(bbox);
904 return;
905 }
906
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200907 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200908 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200910 if (x < min_x)
911 min_x = x;
912 if (x > max_x)
913 max_x = x;
914 if (y < min_y)
915 min_y = y;
916 if (y > max_y)
917 max_y = y;
918 }
919
Pekka Paalanen219b9822012-02-08 15:38:37 +0200920 int_x = floorf(min_x);
921 int_y = floorf(min_y);
922 pixman_region32_init_rect(bbox, int_x, int_y,
923 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200924}
925
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200926static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200928{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500929 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200930
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200931 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 view->geometry.x = roundf(view->geometry.x);
933 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200934
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500935 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500936 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
937 view->transform.position.matrix.d[12] = view->geometry.x;
938 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500939
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500941
Jason Ekstranda7af7042013-10-12 22:38:11 -0500942 view->transform.inverse = view->transform.position.matrix;
943 view->transform.inverse.d[12] = -view->geometry.x;
944 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500945
Jason Ekstranda7af7042013-10-12 22:38:11 -0500946 pixman_region32_init_rect(&view->transform.boundingbox,
947 view->geometry.x,
948 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600949 view->surface->width,
950 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500951
Jason Ekstranda7af7042013-10-12 22:38:11 -0500952 if (view->alpha == 1.0) {
953 pixman_region32_copy(&view->transform.opaque,
954 &view->surface->opaque);
955 pixman_region32_translate(&view->transform.opaque,
956 view->geometry.x,
957 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500958 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200959}
960
961static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500962weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200963{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 struct weston_view *parent = view->geometry.parent;
965 struct weston_matrix *matrix = &view->transform.matrix;
966 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200967 struct weston_transform *tform;
968
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200970
971 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
973 view->transform.position.matrix.d[12] = view->geometry.x;
974 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200975
976 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200978 weston_matrix_multiply(matrix, &tform->matrix);
979
Pekka Paalanen483243f2013-03-08 14:56:50 +0200980 if (parent)
981 weston_matrix_multiply(matrix, &parent->transform.matrix);
982
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200983 if (weston_matrix_invert(inverse, matrix) < 0) {
984 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500985 weston_log("error: weston_view %p"
986 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200987 return -1;
988 }
989
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600990 view_compute_bbox(view, 0, 0,
991 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500992 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500993
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200994 return 0;
995}
996
997WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200999{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001001
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001003 return;
1004
Pekka Paalanen483243f2013-03-08 14:56:50 +02001005 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001007
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001009
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001011
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 pixman_region32_fini(&view->transform.boundingbox);
1013 pixman_region32_fini(&view->transform.opaque);
1014 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001015
Pekka Paalanencd403622012-01-25 13:37:39 +02001016 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 if (view->geometry.transformation_list.next ==
1018 &view->transform.position.link &&
1019 view->geometry.transformation_list.prev ==
1020 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001021 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001023 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 if (weston_view_update_transform_enable(view) < 0)
1025 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001026 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001027
Jason Ekstranda7af7042013-10-12 22:38:11 -05001028 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001029
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 wl_signal_emit(&view->surface->compositor->transform_signal,
1033 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001034}
1035
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001036WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001037weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001038{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001039 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001040
1041 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 * The invariant: if view->geometry.dirty, then all views
1043 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001044 * Corollary: if not parent->geometry.dirty, then all ancestors
1045 * are not dirty.
1046 */
1047
Jason Ekstranda7af7042013-10-12 22:38:11 -05001048 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001049 return;
1050
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001054 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001055 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001056}
1057
1058WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001059weston_view_to_global_fixed(struct weston_view *view,
1060 wl_fixed_t vx, wl_fixed_t vy,
1061 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001062{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001063 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001064
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 weston_view_to_global_float(view,
1066 wl_fixed_to_double(vx),
1067 wl_fixed_to_double(vy),
1068 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001069 *x = wl_fixed_from_double(xf);
1070 *y = wl_fixed_from_double(yf);
1071}
1072
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001073WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001074weston_view_from_global_float(struct weston_view *view,
1075 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001078 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1079
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001081
1082 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001083 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001084 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001085 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 *vx = 0;
1087 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001088 return;
1089 }
1090
Jason Ekstranda7af7042013-10-12 22:38:11 -05001091 *vx = v.f[0] / v.f[3];
1092 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001093 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 *vx = x - view->geometry.x;
1095 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001096 }
1097}
1098
1099WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100weston_view_from_global_fixed(struct weston_view *view,
1101 wl_fixed_t x, wl_fixed_t y,
1102 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001103{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001104 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001105
Jason Ekstranda7af7042013-10-12 22:38:11 -05001106 weston_view_from_global_float(view,
1107 wl_fixed_to_double(x),
1108 wl_fixed_to_double(y),
1109 &vxf, &vyf);
1110 *vx = wl_fixed_from_double(vxf);
1111 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001112}
1113
1114WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115weston_view_from_global(struct weston_view *view,
1116 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001117{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001119
Jason Ekstranda7af7042013-10-12 22:38:11 -05001120 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1121 *vx = floorf(vxf);
1122 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001123}
1124
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001125WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001126weston_surface_schedule_repaint(struct weston_surface *surface)
1127{
1128 struct weston_output *output;
1129
1130 wl_list_for_each(output, &surface->compositor->output_list, link)
1131 if (surface->output_mask & (1 << output->id))
1132 weston_output_schedule_repaint(output);
1133}
1134
1135WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001136weston_view_schedule_repaint(struct weston_view *view)
1137{
1138 struct weston_output *output;
1139
1140 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1141 if (view->output_mask & (1 << output->id))
1142 weston_output_schedule_repaint(output);
1143}
1144
1145WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001146weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001147{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001148 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 0, 0, surface->width,
1150 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001151
Kristian Høgsberg98238702012-08-03 16:29:12 -04001152 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001153}
1154
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001155WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001157{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001158 if (view->geometry.x == x && view->geometry.y == y)
1159 return;
1160
Jason Ekstranda7af7042013-10-12 22:38:11 -05001161 view->geometry.x = x;
1162 view->geometry.y = y;
1163 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001164}
1165
Pekka Paalanen483243f2013-03-08 14:56:50 +02001166static void
1167transform_parent_handle_parent_destroy(struct wl_listener *listener,
1168 void *data)
1169{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001170 struct weston_view *view =
1171 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001172 geometry.parent_destroy_listener);
1173
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001175}
1176
1177WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178weston_view_set_transform_parent(struct weston_view *view,
1179 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001180{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 if (view->geometry.parent) {
1182 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1183 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001184 }
1185
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001187
Jason Ekstranda7af7042013-10-12 22:38:11 -05001188 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001189 transform_parent_handle_parent_destroy;
1190 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001191 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001193 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001195 }
1196
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 weston_view_geometry_dirty(view);
1198}
1199
1200WL_EXPORT int
1201weston_view_is_mapped(struct weston_view *view)
1202{
1203 if (view->output)
1204 return 1;
1205 else
1206 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001207}
1208
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001209WL_EXPORT int
1210weston_surface_is_mapped(struct weston_surface *surface)
1211{
1212 if (surface->output)
1213 return 1;
1214 else
1215 return 0;
1216}
1217
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001218static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001219weston_surface_set_size(struct weston_surface *surface,
1220 int32_t width, int32_t height)
1221{
1222 struct weston_view *view;
1223
1224 if (surface->width == width && surface->height == height)
1225 return;
1226
1227 surface->width = width;
1228 surface->height = height;
1229
1230 wl_list_for_each(view, &surface->views, surface_link)
1231 weston_view_geometry_dirty(view);
1232}
1233
1234static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001235weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001236{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001237 int32_t width, height;
1238
1239 if (!surface->buffer_ref.buffer) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001240 weston_surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001241 return;
1242 }
1243
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001244 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001245 case WL_OUTPUT_TRANSFORM_90:
1246 case WL_OUTPUT_TRANSFORM_270:
1247 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1248 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001249 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001250 height = surface->buffer_ref.buffer->width;
1251 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001252 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001253 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001254 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001255 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001256 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001257
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001258 width = width / surface->buffer_viewport.scale;
1259 height = height / surface->buffer_viewport.scale;
1260 weston_surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001261}
1262
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001263WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001264weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001265{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001266 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001267
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001268 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001269
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001270 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001271}
1272
Jason Ekstranda7af7042013-10-12 22:38:11 -05001273WL_EXPORT struct weston_view *
1274weston_compositor_pick_view(struct weston_compositor *compositor,
1275 wl_fixed_t x, wl_fixed_t y,
1276 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001277{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001279
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 wl_list_for_each(view, &compositor->view_list, link) {
1281 weston_view_from_global_fixed(view, x, y, vx, vy);
1282 if (pixman_region32_contains_point(&view->surface->input,
1283 wl_fixed_to_int(*vx),
1284 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001285 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001287 }
1288
1289 return NULL;
1290}
1291
1292static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001294{
Daniel Stone37816df2012-05-16 18:45:18 +01001295 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001296
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001297 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001298 return;
1299
Daniel Stone37816df2012-05-16 18:45:18 +01001300 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001301 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001302}
1303
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001304WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001305weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001306{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001307 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 if (!weston_view_is_mapped(view))
1310 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 weston_view_damage_below(view);
1313 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001314 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 wl_list_remove(&view->layer_link);
1316 wl_list_init(&view->layer_link);
1317 wl_list_remove(&view->link);
1318 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001319 wl_list_remove(&view->output_move_listener.link);
1320 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001321 wl_list_remove(&view->output_destroy_listener.link);
1322 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 view->output_mask = 0;
1324 weston_surface_assign_output(view->surface);
1325
1326 if (weston_surface_is_mapped(view->surface))
1327 return;
1328
1329 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1330 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001331 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001332 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001333 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001334 NULL,
1335 wl_fixed_from_int(0),
1336 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001337 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001338 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001339 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001341
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342WL_EXPORT void
1343weston_surface_unmap(struct weston_surface *surface)
1344{
1345 struct weston_view *view;
1346
1347 wl_list_for_each(view, &surface->views, surface_link)
1348 weston_view_unmap(view);
1349 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001350}
1351
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001352struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001353 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001354 struct wl_list link;
1355};
1356
Jason Ekstranda7af7042013-10-12 22:38:11 -05001357WL_EXPORT void
1358weston_view_destroy(struct weston_view *view)
1359{
1360 wl_signal_emit(&view->destroy_signal, view);
1361
1362 assert(wl_list_empty(&view->geometry.child_list));
1363
1364 if (weston_view_is_mapped(view)) {
1365 weston_view_unmap(view);
1366 weston_compositor_build_view_list(view->surface->compositor);
1367 }
1368
1369 wl_list_remove(&view->link);
1370 wl_list_remove(&view->layer_link);
1371
1372 pixman_region32_fini(&view->clip);
1373 pixman_region32_fini(&view->transform.boundingbox);
1374
1375 weston_view_set_transform_parent(view, NULL);
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 wl_list_remove(&view->surface_link);
1378
1379 free(view);
1380}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001381
1382WL_EXPORT void
1383weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001384{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001385 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001386 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001387
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001388 if (--surface->ref_count > 0)
1389 return;
1390
1391 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1392
Pekka Paalanene67858b2013-04-25 13:57:42 +03001393 assert(wl_list_empty(&surface->subsurface_list_pending));
1394 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001395
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1397 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001398
Pekka Paalanenbc106382012-10-10 12:49:31 +03001399 wl_list_for_each_safe(cb, next,
1400 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001401 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001402
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001403 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001404 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001405 pixman_region32_fini(&surface->pending.damage);
1406
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001407 if (surface->pending.buffer)
1408 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1409
Pekka Paalanende685b82012-12-04 15:58:12 +02001410 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001411
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001412 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001413 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001414 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001415
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001416 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001417 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001418
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001419 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001420}
1421
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001422static void
1423destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001424{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001425 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001426
Giulio Camuffo0d379742013-11-15 22:06:15 +01001427 /* Set the resource to NULL, since we don't want to leave a
1428 * dangling pointer if the surface was refcounted and survives
1429 * the weston_surface_destroy() call. */
1430 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001431 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001432}
1433
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001434static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001435weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1436{
1437 struct weston_buffer *buffer =
1438 container_of(listener, struct weston_buffer, destroy_listener);
1439
1440 wl_signal_emit(&buffer->destroy_signal, buffer);
1441 free(buffer);
1442}
1443
1444struct weston_buffer *
1445weston_buffer_from_resource(struct wl_resource *resource)
1446{
1447 struct weston_buffer *buffer;
1448 struct wl_listener *listener;
1449
1450 listener = wl_resource_get_destroy_listener(resource,
1451 weston_buffer_destroy_handler);
1452
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001453 if (listener)
1454 return container_of(listener, struct weston_buffer,
1455 destroy_listener);
1456
1457 buffer = zalloc(sizeof *buffer);
1458 if (buffer == NULL)
1459 return NULL;
1460
1461 buffer->resource = resource;
1462 wl_signal_init(&buffer->destroy_signal);
1463 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001464 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001465 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001466
1467 return buffer;
1468}
1469
1470static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001471weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1472 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001473{
Pekka Paalanende685b82012-12-04 15:58:12 +02001474 struct weston_buffer_reference *ref =
1475 container_of(listener, struct weston_buffer_reference,
1476 destroy_listener);
1477
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001478 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001479 ref->buffer = NULL;
1480}
1481
1482WL_EXPORT void
1483weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001484 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001485{
1486 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001487 ref->buffer->busy_count--;
1488 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001489 assert(wl_resource_get_client(ref->buffer->resource));
1490 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001491 WL_BUFFER_RELEASE);
1492 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001493 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001494 }
1495
Pekka Paalanende685b82012-12-04 15:58:12 +02001496 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001497 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001498 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001499 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001500 }
1501
Pekka Paalanende685b82012-12-04 15:58:12 +02001502 ref->buffer = buffer;
1503 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1504}
1505
1506static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001507weston_surface_attach(struct weston_surface *surface,
1508 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001509{
1510 weston_buffer_reference(&surface->buffer_ref, buffer);
1511
Pekka Paalanena6421c42012-12-04 15:58:10 +02001512 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001513 if (weston_surface_is_mapped(surface))
1514 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001515 }
1516
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001517 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001518}
1519
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001520WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001521weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001522{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001523 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001524
1525 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001527}
1528
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001529WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001530weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001532 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001533
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001534 pixman_region32_union(&compositor->primary_plane.damage,
1535 &compositor->primary_plane.damage,
1536 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001537 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001538}
1539
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001540static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001542{
Pekka Paalanende685b82012-12-04 15:58:12 +02001543 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001544 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001545 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001546
Jason Ekstranda7af7042013-10-12 22:38:11 -05001547 empty_region(&surface->damage);
1548}
1549
1550static void
1551view_accumulate_damage(struct weston_view *view,
1552 pixman_region32_t *opaque)
1553{
1554 pixman_region32_t damage;
1555
1556 pixman_region32_init(&damage);
1557 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001558 pixman_box32_t *extents;
1559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 extents = pixman_region32_extents(&view->surface->damage);
1561 view_compute_bbox(view, extents->x1, extents->y1,
1562 extents->x2 - extents->x1,
1563 extents->y2 - extents->y1,
1564 &damage);
1565 pixman_region32_translate(&damage,
1566 -view->plane->x,
1567 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001568 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001569 pixman_region32_copy(&damage, &view->surface->damage);
1570 pixman_region32_translate(&damage,
1571 view->geometry.x - view->plane->x,
1572 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001573 }
1574
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 pixman_region32_subtract(&damage, &damage, opaque);
1576 pixman_region32_union(&view->plane->damage,
1577 &view->plane->damage, &damage);
1578 pixman_region32_fini(&damage);
1579 pixman_region32_copy(&view->clip, opaque);
1580 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001581}
1582
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001583static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001584compositor_accumulate_damage(struct weston_compositor *ec)
1585{
1586 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001588 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001589
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001590 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001591
1592 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001593 pixman_region32_copy(&plane->clip, &clip);
1594
1595 pixman_region32_init(&opaque);
1596
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 wl_list_for_each(ev, &ec->view_list, link) {
1598 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001599 continue;
1600
Jason Ekstranda7af7042013-10-12 22:38:11 -05001601 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001602 }
1603
1604 pixman_region32_union(&clip, &clip, &opaque);
1605 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001606 }
1607
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001608 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001609
Jason Ekstranda7af7042013-10-12 22:38:11 -05001610 wl_list_for_each(ev, &ec->view_list, link)
1611 ev->surface->touched = 0;
1612
1613 wl_list_for_each(ev, &ec->view_list, link) {
1614 if (ev->surface->touched)
1615 continue;
1616 ev->surface->touched = 1;
1617
1618 surface_flush_damage(ev->surface);
1619
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001620 /* Both the renderer and the backend have seen the buffer
1621 * by now. If renderer needs the buffer, it has its own
1622 * reference set. If the backend wants to keep the buffer
1623 * around for migrating the surface into a non-primary plane
1624 * later, keep_buffer is true. Otherwise, drop the core
1625 * reference now, and allow early buffer release. This enables
1626 * clients to use single-buffering.
1627 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001628 if (!ev->surface->keep_buffer)
1629 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001630 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001631}
1632
1633static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001635{
1636 struct weston_subsurface *sub;
1637
Pekka Paalanene67858b2013-04-25 13:57:42 +03001638 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001640 continue;
1641
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1643 wl_list_init(&sub->surface->views);
1644
1645 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001646 }
1647}
1648
1649static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001650surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001651{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 struct weston_subsurface *sub;
1653 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001654
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1656 if (sub->surface == surface)
1657 continue;
1658
1659 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1660 weston_view_destroy(view);
1661
1662 surface_free_unused_subsurface_views(sub->surface);
1663 }
1664}
1665
1666static void
1667view_list_add_subsurface_view(struct weston_compositor *compositor,
1668 struct weston_subsurface *sub,
1669 struct weston_view *parent)
1670{
1671 struct weston_subsurface *child;
1672 struct weston_view *view = NULL, *iv;
1673
1674 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1675 if (iv->geometry.parent == parent) {
1676 view = iv;
1677 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001678 }
1679 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001680
1681 if (view) {
1682 /* Put it back in the surface's list of views */
1683 wl_list_remove(&view->surface_link);
1684 wl_list_insert(&sub->surface->views, &view->surface_link);
1685 } else {
1686 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001687 weston_view_set_position(view,
1688 sub->position.x,
1689 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690 weston_view_set_transform_parent(view, parent);
1691 }
1692
1693 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001694
Pekka Paalanenb188e912013-11-19 14:03:35 +02001695 if (wl_list_empty(&sub->surface->subsurface_list)) {
1696 wl_list_insert(compositor->view_list.prev, &view->link);
1697 return;
1698 }
1699
1700 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1701 if (child->surface == sub->surface)
1702 wl_list_insert(compositor->view_list.prev, &view->link);
1703 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001705 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706}
1707
1708static void
1709view_list_add(struct weston_compositor *compositor,
1710 struct weston_view *view)
1711{
1712 struct weston_subsurface *sub;
1713
1714 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715
Pekka Paalanenb188e912013-11-19 14:03:35 +02001716 if (wl_list_empty(&view->surface->subsurface_list)) {
1717 wl_list_insert(compositor->view_list.prev, &view->link);
1718 return;
1719 }
1720
1721 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1722 if (sub->surface == view->surface)
1723 wl_list_insert(compositor->view_list.prev, &view->link);
1724 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001726 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727}
1728
1729static void
1730weston_compositor_build_view_list(struct weston_compositor *compositor)
1731{
1732 struct weston_view *view;
1733 struct weston_layer *layer;
1734
1735 wl_list_for_each(layer, &compositor->layer_list, link)
1736 wl_list_for_each(view, &layer->view_list, layer_link)
1737 surface_stash_subsurface_views(view->surface);
1738
1739 wl_list_init(&compositor->view_list);
1740 wl_list_for_each(layer, &compositor->layer_list, link) {
1741 wl_list_for_each(view, &layer->view_list, layer_link) {
1742 view_list_add(compositor, view);
1743 }
1744 }
1745
1746 wl_list_for_each(layer, &compositor->layer_list, link)
1747 wl_list_for_each(view, &layer->view_list, layer_link)
1748 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001749}
1750
David Herrmann1edf44c2013-10-22 17:11:26 +02001751static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001752weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001753{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001754 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001755 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001756 struct weston_animation *animation, *next;
1757 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001758 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001759 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001760 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001761
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001762 if (output->destroying)
1763 return 0;
1764
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001765 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001766 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001767
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001768 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001769 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001770 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001771 wl_list_for_each(ev, &ec->view_list, link)
1772 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001773
Pekka Paalanene67858b2013-04-25 13:57:42 +03001774 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775 wl_list_for_each(ev, &ec->view_list, link) {
1776 /* Note: This operation is safe to do multiple times on the
1777 * same surface.
1778 */
1779 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001780 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 &ev->surface->frame_callback_list);
1782 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001783 }
1784 }
1785
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001786 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001787
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001788 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001789 pixman_region32_intersect(&output_damage,
1790 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001791 pixman_region32_subtract(&output_damage,
1792 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001793
Scott Moreauccbf29d2012-02-22 14:21:41 -07001794 if (output->dirty)
1795 weston_output_update_matrix(output);
1796
David Herrmann1edf44c2013-10-22 17:11:26 +02001797 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001798
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001799 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001800
Kristian Høgsbergef044142011-06-21 15:02:12 -04001801 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001802
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001803 weston_compositor_repick(ec);
1804 wl_event_loop_dispatch(ec->input_loop, 0);
1805
Jonas Ådahldb773762012-06-13 00:01:21 +02001806 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001807 wl_callback_send_done(cb->resource, msecs);
1808 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001809 }
1810
Scott Moreaud64cf212012-06-08 19:40:54 -06001811 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001812 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001813 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001814 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001815
1816 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001817}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001818
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001819static int
1820weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001821{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001822 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001823
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001824 wl_event_loop_dispatch(compositor->input_loop, 0);
1825
1826 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001827}
1828
1829WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001830weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001831{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001832 struct weston_compositor *compositor = output->compositor;
1833 struct wl_event_loop *loop =
1834 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001835 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001836
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001837 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001838
1839 if (output->repaint_needed &&
1840 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1841 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001842 r = weston_output_repaint(output, msecs);
1843 if (!r)
1844 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001845 }
1846
1847 output->repaint_scheduled = 0;
1848 if (compositor->input_loop_source)
1849 return;
1850
1851 fd = wl_event_loop_get_fd(compositor->input_loop);
1852 compositor->input_loop_source =
1853 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1854 weston_compositor_read_input, compositor);
1855}
1856
1857static void
1858idle_repaint(void *data)
1859{
1860 struct weston_output *output = data;
1861
Jonas Ådahle5a12252013-04-05 23:07:11 +02001862 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001863}
1864
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001865WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001866weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1867{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001869 if (below != NULL)
1870 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001871}
1872
1873WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001874weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001875{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001876 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001877 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001878
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001879 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1880 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001881 return;
1882
Kristian Høgsbergef044142011-06-21 15:02:12 -04001883 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001884 output->repaint_needed = 1;
1885 if (output->repaint_scheduled)
1886 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001887
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001888 wl_event_loop_add_idle(loop, idle_repaint, output);
1889 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001890
1891 if (compositor->input_loop_source) {
1892 wl_event_source_remove(compositor->input_loop_source);
1893 compositor->input_loop_source = NULL;
1894 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001895}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001896
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001897WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001898weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1899{
1900 struct weston_output *output;
1901
1902 wl_list_for_each(output, &compositor->output_list, link)
1903 weston_output_schedule_repaint(output);
1904}
1905
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001906static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001907surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001908{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001909 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001910}
1911
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001912static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001913surface_attach(struct wl_client *client,
1914 struct wl_resource *resource,
1915 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1916{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001917 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001918 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001919
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001920 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001921 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001922 if (buffer == NULL) {
1923 wl_client_post_no_memory(client);
1924 return;
1925 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001926 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001927
Pekka Paalanende685b82012-12-04 15:58:12 +02001928 /* Attach, attach, without commit in between does not send
1929 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001930 if (surface->pending.buffer)
1931 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001932
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001933 surface->pending.sx = sx;
1934 surface->pending.sy = sy;
1935 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001936 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001937 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001938 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001939 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001940 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001941}
1942
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001943static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001944surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001945 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001946 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001947{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001948 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001949
Pekka Paalanen8e159182012-10-10 12:49:25 +03001950 pixman_region32_union_rect(&surface->pending.damage,
1951 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001952 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001953}
1954
Kristian Høgsberg33418202011-08-16 23:01:28 -04001955static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001956destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001957{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001958 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001959
1960 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001961 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001962}
1963
1964static void
1965surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001966 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001967{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001968 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001969 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001970
1971 cb = malloc(sizeof *cb);
1972 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001973 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001974 return;
1975 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001976
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001977 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1978 callback);
1979 if (cb->resource == NULL) {
1980 free(cb);
1981 wl_resource_post_no_memory(resource);
1982 return;
1983 }
1984
Jason Ekstranda85118c2013-06-27 20:17:02 -05001985 wl_resource_set_implementation(cb->resource, NULL, cb,
1986 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001987
Pekka Paalanenbc106382012-10-10 12:49:31 +03001988 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001989}
1990
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001991static void
1992surface_set_opaque_region(struct wl_client *client,
1993 struct wl_resource *resource,
1994 struct wl_resource *region_resource)
1995{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001996 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001997 struct weston_region *region;
1998
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001999 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002000 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002001 pixman_region32_copy(&surface->pending.opaque,
2002 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002003 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002004 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002005 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002006}
2007
2008static void
2009surface_set_input_region(struct wl_client *client,
2010 struct wl_resource *resource,
2011 struct wl_resource *region_resource)
2012{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002013 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002014 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002015
2016 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002017 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002018 pixman_region32_copy(&surface->pending.input,
2019 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002020 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002021 pixman_region32_fini(&surface->pending.input);
2022 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002023 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002024}
2025
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002026static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002027weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002028{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002029 struct weston_subsurface *sub;
2030
2031 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2032 parent_link_pending) {
2033 wl_list_remove(&sub->parent_link);
2034 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2035 }
2036}
2037
2038static void
2039weston_surface_commit(struct weston_surface *surface)
2040{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002041 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002042 pixman_region32_t opaque;
2043
Alexander Larsson4ea95522013-05-22 14:41:37 +02002044 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002045 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002046 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002047
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002048 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01002049 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002050 weston_surface_attach(surface, surface->pending.buffer);
2051
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002052 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01002053
2054 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002055 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002056 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002057
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002058 if (surface->pending.buffer)
2059 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2060 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002061 surface->pending.sx = 0;
2062 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002063 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002064
2065 /* wl_surface.damage */
2066 pixman_region32_union(&surface->damage, &surface->damage,
2067 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002068 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2069 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002070 surface->width,
2071 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002072 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002073
2074 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002075 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002076 surface->width,
2077 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002078 pixman_region32_intersect(&opaque,
2079 &opaque, &surface->pending.opaque);
2080
2081 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2082 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002083 wl_list_for_each(view, &surface->views, surface_link)
2084 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002085 }
2086
2087 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002088
2089 /* wl_surface.set_input_region */
2090 pixman_region32_fini(&surface->input);
2091 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002092 surface->width,
2093 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002094 pixman_region32_intersect(&surface->input,
2095 &surface->input, &surface->pending.input);
2096
Pekka Paalanenbc106382012-10-10 12:49:31 +03002097 /* wl_surface.frame */
2098 wl_list_insert_list(&surface->frame_callback_list,
2099 &surface->pending.frame_callback_list);
2100 wl_list_init(&surface->pending.frame_callback_list);
2101
Pekka Paalanene67858b2013-04-25 13:57:42 +03002102 weston_surface_commit_subsurface_order(surface);
2103
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002104 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002105}
2106
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002107static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002108weston_subsurface_commit(struct weston_subsurface *sub);
2109
2110static void
2111weston_subsurface_parent_commit(struct weston_subsurface *sub,
2112 int parent_is_synchronized);
2113
2114static void
2115surface_commit(struct wl_client *client, struct wl_resource *resource)
2116{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002117 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002118 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2119
2120 if (sub) {
2121 weston_subsurface_commit(sub);
2122 return;
2123 }
2124
2125 weston_surface_commit(surface);
2126
2127 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2128 if (sub->surface != surface)
2129 weston_subsurface_parent_commit(sub, 0);
2130 }
2131}
2132
2133static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002134surface_set_buffer_transform(struct wl_client *client,
2135 struct wl_resource *resource, int transform)
2136{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002137 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002138
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002139 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002140}
2141
Alexander Larsson4ea95522013-05-22 14:41:37 +02002142static void
2143surface_set_buffer_scale(struct wl_client *client,
2144 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002145 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002146{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002147 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002148
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002149 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002150}
2151
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002152static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002153 surface_destroy,
2154 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002155 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002156 surface_frame,
2157 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002158 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002159 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002160 surface_set_buffer_transform,
2161 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002162};
2163
2164static void
2165compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002166 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002167{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002168 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002169 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002170
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002171 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002172 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002173 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002174 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002175 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002176
Jason Ekstranda85118c2013-06-27 20:17:02 -05002177 surface->resource =
2178 wl_resource_create(client, &wl_surface_interface,
2179 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002180 if (surface->resource == NULL) {
2181 weston_surface_destroy(surface);
2182 wl_resource_post_no_memory(resource);
2183 return;
2184 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002185 wl_resource_set_implementation(surface->resource, &surface_interface,
2186 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002187}
2188
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002189static void
2190destroy_region(struct wl_resource *resource)
2191{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002192 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002193
2194 pixman_region32_fini(&region->region);
2195 free(region);
2196}
2197
2198static void
2199region_destroy(struct wl_client *client, struct wl_resource *resource)
2200{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002201 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002202}
2203
2204static void
2205region_add(struct wl_client *client, struct wl_resource *resource,
2206 int32_t x, int32_t y, int32_t width, int32_t height)
2207{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002208 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002209
2210 pixman_region32_union_rect(&region->region, &region->region,
2211 x, y, width, height);
2212}
2213
2214static void
2215region_subtract(struct wl_client *client, struct wl_resource *resource,
2216 int32_t x, int32_t y, int32_t width, int32_t height)
2217{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002218 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002219 pixman_region32_t rect;
2220
2221 pixman_region32_init_rect(&rect, x, y, width, height);
2222 pixman_region32_subtract(&region->region, &region->region, &rect);
2223 pixman_region32_fini(&rect);
2224}
2225
2226static const struct wl_region_interface region_interface = {
2227 region_destroy,
2228 region_add,
2229 region_subtract
2230};
2231
2232static void
2233compositor_create_region(struct wl_client *client,
2234 struct wl_resource *resource, uint32_t id)
2235{
2236 struct weston_region *region;
2237
2238 region = malloc(sizeof *region);
2239 if (region == NULL) {
2240 wl_resource_post_no_memory(resource);
2241 return;
2242 }
2243
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002244 pixman_region32_init(&region->region);
2245
Jason Ekstranda85118c2013-06-27 20:17:02 -05002246 region->resource =
2247 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002248 if (region->resource == NULL) {
2249 free(region);
2250 wl_resource_post_no_memory(resource);
2251 return;
2252 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002253 wl_resource_set_implementation(region->resource, &region_interface,
2254 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002255}
2256
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002257static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002258 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002259 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002260};
2261
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002262static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002263weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2264{
2265 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002266 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002267 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002268
Alexander Larsson4ea95522013-05-22 14:41:37 +02002269 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002270 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002271 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002272
Pekka Paalanene67858b2013-04-25 13:57:42 +03002273 /* wl_surface.attach */
2274 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2275 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2276 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2277
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002278 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279
2280 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002281 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002282 sub->cached.sx = 0;
2283 sub->cached.sy = 0;
2284 sub->cached.newly_attached = 0;
2285
2286 /* wl_surface.damage */
2287 pixman_region32_union(&surface->damage, &surface->damage,
2288 &sub->cached.damage);
2289 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2290 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002291 surface->width,
2292 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002293 empty_region(&sub->cached.damage);
2294
2295 /* wl_surface.set_opaque_region */
2296 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002297 surface->width,
2298 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299 pixman_region32_intersect(&opaque,
2300 &opaque, &sub->cached.opaque);
2301
2302 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2303 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002304 wl_list_for_each(view, &surface->views, surface_link)
2305 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002306 }
2307
2308 pixman_region32_fini(&opaque);
2309
2310 /* wl_surface.set_input_region */
2311 pixman_region32_fini(&surface->input);
2312 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002313 surface->width,
2314 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002315 pixman_region32_intersect(&surface->input,
2316 &surface->input, &sub->cached.input);
2317
2318 /* wl_surface.frame */
2319 wl_list_insert_list(&surface->frame_callback_list,
2320 &sub->cached.frame_callback_list);
2321 wl_list_init(&sub->cached.frame_callback_list);
2322
2323 weston_surface_commit_subsurface_order(surface);
2324
2325 weston_surface_schedule_repaint(surface);
2326
2327 sub->cached.has_data = 0;
2328}
2329
2330static void
2331weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2332{
2333 struct weston_surface *surface = sub->surface;
2334
2335 /*
2336 * If this commit would cause the surface to move by the
2337 * attach(dx, dy) parameters, the old damage region must be
2338 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002339 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002340 */
2341 pixman_region32_translate(&sub->cached.damage,
2342 -surface->pending.sx, -surface->pending.sy);
2343 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2344 &surface->pending.damage);
2345 empty_region(&surface->pending.damage);
2346
2347 if (surface->pending.newly_attached) {
2348 sub->cached.newly_attached = 1;
2349 weston_buffer_reference(&sub->cached.buffer_ref,
2350 surface->pending.buffer);
2351 }
2352 sub->cached.sx += surface->pending.sx;
2353 sub->cached.sy += surface->pending.sy;
2354 surface->pending.sx = 0;
2355 surface->pending.sy = 0;
2356 surface->pending.newly_attached = 0;
2357
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002358 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002359
2360 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2361
2362 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2363
2364 wl_list_insert_list(&sub->cached.frame_callback_list,
2365 &surface->pending.frame_callback_list);
2366 wl_list_init(&surface->pending.frame_callback_list);
2367
2368 sub->cached.has_data = 1;
2369}
2370
2371static int
2372weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2373{
2374 while (sub) {
2375 if (sub->synchronized)
2376 return 1;
2377
2378 if (!sub->parent)
2379 return 0;
2380
2381 sub = weston_surface_to_subsurface(sub->parent);
2382 }
2383
2384 return 0;
2385}
2386
2387static void
2388weston_subsurface_commit(struct weston_subsurface *sub)
2389{
2390 struct weston_surface *surface = sub->surface;
2391 struct weston_subsurface *tmp;
2392
2393 /* Recursive check for effectively synchronized. */
2394 if (weston_subsurface_is_synchronized(sub)) {
2395 weston_subsurface_commit_to_cache(sub);
2396 } else {
2397 if (sub->cached.has_data) {
2398 /* flush accumulated state from cache */
2399 weston_subsurface_commit_to_cache(sub);
2400 weston_subsurface_commit_from_cache(sub);
2401 } else {
2402 weston_surface_commit(surface);
2403 }
2404
2405 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2406 if (tmp->surface != surface)
2407 weston_subsurface_parent_commit(tmp, 0);
2408 }
2409 }
2410}
2411
2412static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002413weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002414{
2415 struct weston_surface *surface = sub->surface;
2416 struct weston_subsurface *tmp;
2417
Pekka Paalanene67858b2013-04-25 13:57:42 +03002418 /* From now on, commit_from_cache the whole sub-tree, regardless of
2419 * the synchronized mode of each child. This sub-surface or some
2420 * of its ancestors were synchronized, so we are synchronized
2421 * all the way down.
2422 */
2423
2424 if (sub->cached.has_data)
2425 weston_subsurface_commit_from_cache(sub);
2426
2427 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2428 if (tmp->surface != surface)
2429 weston_subsurface_parent_commit(tmp, 1);
2430 }
2431}
2432
2433static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002434weston_subsurface_parent_commit(struct weston_subsurface *sub,
2435 int parent_is_synchronized)
2436{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002437 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002438 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002439 wl_list_for_each(view, &sub->surface->views, surface_link)
2440 weston_view_set_position(view,
2441 sub->position.x,
2442 sub->position.y);
2443
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002444 sub->position.set = 0;
2445 }
2446
2447 if (parent_is_synchronized || sub->synchronized)
2448 weston_subsurface_synchronized_commit(sub);
2449}
2450
2451static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002452subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453{
2454 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002455 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002456
Jason Ekstranda7af7042013-10-12 22:38:11 -05002457 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002458 weston_view_set_position(view,
2459 view->geometry.x + dx,
2460 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002461
2462 /* No need to check parent mappedness, because if parent is not
2463 * mapped, parent is not in a visible layer, so this sub-surface
2464 * will not be drawn either.
2465 */
2466 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002467 /* Cannot call weston_surface_update_transform(),
2468 * because that would call it also for the parent surface,
2469 * which might not be mapped yet. That would lead to
2470 * inconsistent state, where the window could never be
2471 * mapped.
2472 *
2473 * Instead just assing any output, to make
2474 * weston_surface_is_mapped() return true, so that when the
2475 * parent surface does get mapped, this one will get
2476 * included, too. See surface_list_add().
2477 */
2478 assert(!wl_list_empty(&compositor->output_list));
2479 surface->output = container_of(compositor->output_list.next,
2480 struct weston_output, link);
2481 }
2482}
2483
2484static struct weston_subsurface *
2485weston_surface_to_subsurface(struct weston_surface *surface)
2486{
2487 if (surface->configure == subsurface_configure)
2488 return surface->configure_private;
2489
2490 return NULL;
2491}
2492
Pekka Paalanen01388e22013-04-25 13:57:44 +03002493WL_EXPORT struct weston_surface *
2494weston_surface_get_main_surface(struct weston_surface *surface)
2495{
2496 struct weston_subsurface *sub;
2497
2498 while (surface && (sub = weston_surface_to_subsurface(surface)))
2499 surface = sub->parent;
2500
2501 return surface;
2502}
2503
Pekka Paalanene67858b2013-04-25 13:57:42 +03002504static void
2505subsurface_set_position(struct wl_client *client,
2506 struct wl_resource *resource, int32_t x, int32_t y)
2507{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002508 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002509
2510 if (!sub)
2511 return;
2512
2513 sub->position.x = x;
2514 sub->position.y = y;
2515 sub->position.set = 1;
2516}
2517
2518static struct weston_subsurface *
2519subsurface_from_surface(struct weston_surface *surface)
2520{
2521 struct weston_subsurface *sub;
2522
2523 sub = weston_surface_to_subsurface(surface);
2524 if (sub)
2525 return sub;
2526
2527 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2528 if (sub->surface == surface)
2529 return sub;
2530
2531 return NULL;
2532}
2533
2534static struct weston_subsurface *
2535subsurface_sibling_check(struct weston_subsurface *sub,
2536 struct weston_surface *surface,
2537 const char *request)
2538{
2539 struct weston_subsurface *sibling;
2540
2541 sibling = subsurface_from_surface(surface);
2542
2543 if (!sibling) {
2544 wl_resource_post_error(sub->resource,
2545 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2546 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002547 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002548 return NULL;
2549 }
2550
2551 if (sibling->parent != sub->parent) {
2552 wl_resource_post_error(sub->resource,
2553 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2554 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002555 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002556 return NULL;
2557 }
2558
2559 return sibling;
2560}
2561
2562static void
2563subsurface_place_above(struct wl_client *client,
2564 struct wl_resource *resource,
2565 struct wl_resource *sibling_resource)
2566{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002567 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002568 struct weston_surface *surface =
2569 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570 struct weston_subsurface *sibling;
2571
2572 if (!sub)
2573 return;
2574
2575 sibling = subsurface_sibling_check(sub, surface, "place_above");
2576 if (!sibling)
2577 return;
2578
2579 wl_list_remove(&sub->parent_link_pending);
2580 wl_list_insert(sibling->parent_link_pending.prev,
2581 &sub->parent_link_pending);
2582}
2583
2584static void
2585subsurface_place_below(struct wl_client *client,
2586 struct wl_resource *resource,
2587 struct wl_resource *sibling_resource)
2588{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002589 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002590 struct weston_surface *surface =
2591 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592 struct weston_subsurface *sibling;
2593
2594 if (!sub)
2595 return;
2596
2597 sibling = subsurface_sibling_check(sub, surface, "place_below");
2598 if (!sibling)
2599 return;
2600
2601 wl_list_remove(&sub->parent_link_pending);
2602 wl_list_insert(&sibling->parent_link_pending,
2603 &sub->parent_link_pending);
2604}
2605
2606static void
2607subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2608{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002609 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610
2611 if (sub)
2612 sub->synchronized = 1;
2613}
2614
2615static void
2616subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2617{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002618 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002619
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002620 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002621 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002622
2623 /* If sub became effectively desynchronized, flush. */
2624 if (!weston_subsurface_is_synchronized(sub))
2625 weston_subsurface_synchronized_commit(sub);
2626 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002627}
2628
2629static void
2630weston_subsurface_cache_init(struct weston_subsurface *sub)
2631{
2632 pixman_region32_init(&sub->cached.damage);
2633 pixman_region32_init(&sub->cached.opaque);
2634 pixman_region32_init(&sub->cached.input);
2635 wl_list_init(&sub->cached.frame_callback_list);
2636 sub->cached.buffer_ref.buffer = NULL;
2637}
2638
2639static void
2640weston_subsurface_cache_fini(struct weston_subsurface *sub)
2641{
2642 struct weston_frame_callback *cb, *tmp;
2643
2644 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002645 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002646
2647 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2648 pixman_region32_fini(&sub->cached.damage);
2649 pixman_region32_fini(&sub->cached.opaque);
2650 pixman_region32_fini(&sub->cached.input);
2651}
2652
2653static void
2654weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2655{
2656 wl_list_remove(&sub->parent_link);
2657 wl_list_remove(&sub->parent_link_pending);
2658 wl_list_remove(&sub->parent_destroy_listener.link);
2659 sub->parent = NULL;
2660}
2661
2662static void
2663weston_subsurface_destroy(struct weston_subsurface *sub);
2664
2665static void
2666subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2667{
2668 struct weston_subsurface *sub =
2669 container_of(listener, struct weston_subsurface,
2670 surface_destroy_listener);
2671 assert(data == &sub->surface->resource);
2672
2673 /* The protocol object (wl_resource) is left inert. */
2674 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002675 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002676
2677 weston_subsurface_destroy(sub);
2678}
2679
2680static void
2681subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2682{
2683 struct weston_subsurface *sub =
2684 container_of(listener, struct weston_subsurface,
2685 parent_destroy_listener);
2686 assert(data == &sub->parent->resource);
2687 assert(sub->surface != sub->parent);
2688
2689 if (weston_surface_is_mapped(sub->surface))
2690 weston_surface_unmap(sub->surface);
2691
2692 weston_subsurface_unlink_parent(sub);
2693}
2694
2695static void
2696subsurface_resource_destroy(struct wl_resource *resource)
2697{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002698 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002699
2700 if (sub)
2701 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002702}
2703
2704static void
2705subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2706{
2707 wl_resource_destroy(resource);
2708}
2709
2710static void
2711weston_subsurface_link_parent(struct weston_subsurface *sub,
2712 struct weston_surface *parent)
2713{
2714 sub->parent = parent;
2715 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002716 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002717 &sub->parent_destroy_listener);
2718
2719 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2720 wl_list_insert(&parent->subsurface_list_pending,
2721 &sub->parent_link_pending);
2722}
2723
2724static void
2725weston_subsurface_link_surface(struct weston_subsurface *sub,
2726 struct weston_surface *surface)
2727{
2728 sub->surface = surface;
2729 sub->surface_destroy_listener.notify =
2730 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002731 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732 &sub->surface_destroy_listener);
2733}
2734
2735static void
2736weston_subsurface_destroy(struct weston_subsurface *sub)
2737{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002738 struct weston_view *view, *next;
2739
Pekka Paalanene67858b2013-04-25 13:57:42 +03002740 assert(sub->surface);
2741
2742 if (sub->resource) {
2743 assert(weston_surface_to_subsurface(sub->surface) == sub);
2744 assert(sub->parent_destroy_listener.notify ==
2745 subsurface_handle_parent_destroy);
2746
Jason Ekstranda7af7042013-10-12 22:38:11 -05002747 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2748 weston_view_destroy(view);
2749
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750 if (sub->parent)
2751 weston_subsurface_unlink_parent(sub);
2752
2753 weston_subsurface_cache_fini(sub);
2754
2755 sub->surface->configure = NULL;
2756 sub->surface->configure_private = NULL;
2757 } else {
2758 /* the dummy weston_subsurface for the parent itself */
2759 assert(sub->parent_destroy_listener.notify == NULL);
2760 wl_list_remove(&sub->parent_link);
2761 wl_list_remove(&sub->parent_link_pending);
2762 }
2763
2764 wl_list_remove(&sub->surface_destroy_listener.link);
2765 free(sub);
2766}
2767
2768static const struct wl_subsurface_interface subsurface_implementation = {
2769 subsurface_destroy,
2770 subsurface_set_position,
2771 subsurface_place_above,
2772 subsurface_place_below,
2773 subsurface_set_sync,
2774 subsurface_set_desync
2775};
2776
2777static struct weston_subsurface *
2778weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2779 struct weston_surface *parent)
2780{
2781 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002782 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002783
2784 sub = calloc(1, sizeof *sub);
2785 if (!sub)
2786 return NULL;
2787
Jason Ekstranda7af7042013-10-12 22:38:11 -05002788 wl_list_init(&sub->unused_views);
2789
Jason Ekstranda85118c2013-06-27 20:17:02 -05002790 sub->resource =
2791 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792 if (!sub->resource) {
2793 free(sub);
2794 return NULL;
2795 }
2796
Jason Ekstranda85118c2013-06-27 20:17:02 -05002797 wl_resource_set_implementation(sub->resource,
2798 &subsurface_implementation,
2799 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002800 weston_subsurface_link_surface(sub, surface);
2801 weston_subsurface_link_parent(sub, parent);
2802 weston_subsurface_cache_init(sub);
2803 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002804
2805 return sub;
2806}
2807
2808/* Create a dummy subsurface for having the parent itself in its
2809 * sub-surface lists. Makes stacking order manipulation easy.
2810 */
2811static struct weston_subsurface *
2812weston_subsurface_create_for_parent(struct weston_surface *parent)
2813{
2814 struct weston_subsurface *sub;
2815
2816 sub = calloc(1, sizeof *sub);
2817 if (!sub)
2818 return NULL;
2819
2820 weston_subsurface_link_surface(sub, parent);
2821 sub->parent = parent;
2822 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2823 wl_list_insert(&parent->subsurface_list_pending,
2824 &sub->parent_link_pending);
2825
2826 return sub;
2827}
2828
2829static void
2830subcompositor_get_subsurface(struct wl_client *client,
2831 struct wl_resource *resource,
2832 uint32_t id,
2833 struct wl_resource *surface_resource,
2834 struct wl_resource *parent_resource)
2835{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002836 struct weston_surface *surface =
2837 wl_resource_get_user_data(surface_resource);
2838 struct weston_surface *parent =
2839 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002840 struct weston_subsurface *sub;
2841 static const char where[] = "get_subsurface: wl_subsurface@";
2842
2843 if (surface == parent) {
2844 wl_resource_post_error(resource,
2845 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2846 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002847 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002848 return;
2849 }
2850
2851 if (weston_surface_to_subsurface(surface)) {
2852 wl_resource_post_error(resource,
2853 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2854 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002855 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002856 return;
2857 }
2858
2859 if (surface->configure) {
2860 wl_resource_post_error(resource,
2861 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2862 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002863 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002864 return;
2865 }
2866
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002867 if (weston_surface_get_main_surface(parent) == surface) {
2868 wl_resource_post_error(resource,
2869 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2870 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002871 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002872 return;
2873 }
2874
Pekka Paalanene67858b2013-04-25 13:57:42 +03002875 /* make sure the parent is in its own list */
2876 if (wl_list_empty(&parent->subsurface_list)) {
2877 if (!weston_subsurface_create_for_parent(parent)) {
2878 wl_resource_post_no_memory(resource);
2879 return;
2880 }
2881 }
2882
2883 sub = weston_subsurface_create(id, surface, parent);
2884 if (!sub) {
2885 wl_resource_post_no_memory(resource);
2886 return;
2887 }
2888
2889 surface->configure = subsurface_configure;
2890 surface->configure_private = sub;
2891}
2892
2893static void
2894subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2895{
2896 wl_resource_destroy(resource);
2897}
2898
2899static const struct wl_subcompositor_interface subcompositor_interface = {
2900 subcompositor_destroy,
2901 subcompositor_get_subsurface
2902};
2903
2904static void
2905bind_subcompositor(struct wl_client *client,
2906 void *data, uint32_t version, uint32_t id)
2907{
2908 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002909 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002910
Jason Ekstranda85118c2013-06-27 20:17:02 -05002911 resource =
2912 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002913 if (resource == NULL) {
2914 wl_client_post_no_memory(client);
2915 return;
2916 }
2917 wl_resource_set_implementation(resource, &subcompositor_interface,
2918 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002919}
2920
2921static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002922weston_compositor_dpms(struct weston_compositor *compositor,
2923 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002924{
2925 struct weston_output *output;
2926
2927 wl_list_for_each(output, &compositor->output_list, link)
2928 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002929 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002930}
2931
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002932WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002933weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002934{
Neil Roberts8b62e202013-09-30 13:14:47 +01002935 uint32_t old_state = compositor->state;
2936
2937 /* The state needs to be changed before emitting the wake
2938 * signal because that may try to schedule a repaint which
2939 * will not work if the compositor is still sleeping */
2940 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2941
2942 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002943 case WESTON_COMPOSITOR_SLEEPING:
2944 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2945 /* fall through */
2946 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002947 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002948 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002949 /* fall through */
2950 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002951 wl_event_source_timer_update(compositor->idle_source,
2952 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002953 }
2954}
2955
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002956WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002957weston_compositor_offscreen(struct weston_compositor *compositor)
2958{
2959 switch (compositor->state) {
2960 case WESTON_COMPOSITOR_OFFSCREEN:
2961 return;
2962 case WESTON_COMPOSITOR_SLEEPING:
2963 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2964 /* fall through */
2965 default:
2966 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2967 wl_event_source_timer_update(compositor->idle_source, 0);
2968 }
2969}
2970
2971WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002972weston_compositor_sleep(struct weston_compositor *compositor)
2973{
2974 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2975 return;
2976
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002977 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002978 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2979 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2980}
2981
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002982static int
2983idle_handler(void *data)
2984{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002985 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002986
2987 if (compositor->idle_inhibit)
2988 return 1;
2989
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002990 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002991 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002992
2993 return 1;
2994}
2995
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002996WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002997weston_plane_init(struct weston_plane *plane,
2998 struct weston_compositor *ec,
2999 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003000{
3001 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003002 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003003 plane->x = x;
3004 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003005 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003006
3007 /* Init the link so that the call to wl_list_remove() when releasing
3008 * the plane without ever stacking doesn't lead to a crash */
3009 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003010}
3011
3012WL_EXPORT void
3013weston_plane_release(struct weston_plane *plane)
3014{
Xiong Zhang97116532013-10-23 13:58:31 +08003015 struct weston_view *view;
3016
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003017 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003018 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003019
Xiong Zhang97116532013-10-23 13:58:31 +08003020 wl_list_for_each(view, &plane->compositor->view_list, link) {
3021 if (view->plane == plane)
3022 view->plane = NULL;
3023 }
3024
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003025 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003026}
3027
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003028WL_EXPORT void
3029weston_compositor_stack_plane(struct weston_compositor *ec,
3030 struct weston_plane *plane,
3031 struct weston_plane *above)
3032{
3033 if (above)
3034 wl_list_insert(above->link.prev, &plane->link);
3035 else
3036 wl_list_insert(&ec->plane_list, &plane->link);
3037}
3038
Casey Dahlin9074db52012-04-19 22:50:09 -04003039static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003040{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003041 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003042}
3043
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003044static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003045bind_output(struct wl_client *client,
3046 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003047{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003048 struct weston_output *output = data;
3049 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003050 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003051
Jason Ekstranda85118c2013-06-27 20:17:02 -05003052 resource = wl_resource_create(client, &wl_output_interface,
3053 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003054 if (resource == NULL) {
3055 wl_client_post_no_memory(client);
3056 return;
3057 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003058
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003059 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003060 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003061
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003062 wl_output_send_geometry(resource,
3063 output->x,
3064 output->y,
3065 output->mm_width,
3066 output->mm_height,
3067 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003068 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003069 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003070 if (version >= 2)
3071 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003072 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003073
3074 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003075 wl_output_send_mode(resource,
3076 mode->flags,
3077 mode->width,
3078 mode->height,
3079 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003080 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003081
3082 if (version >= 2)
3083 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003084}
3085
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003086/* Move other outputs when one is removed so the space remains contiguos. */
3087static void
3088weston_compositor_remove_output(struct weston_compositor *compositor,
3089 struct weston_output *remove_output)
3090{
3091 struct weston_output *output;
3092 int offset = 0;
3093
3094 wl_list_for_each(output, &compositor->output_list, link) {
3095 if (output == remove_output) {
3096 offset = output->width;
3097 continue;
3098 }
3099
3100 if (offset > 0) {
3101 weston_output_move(output,
3102 output->x - offset, output->y);
3103 output->dirty = 1;
3104 }
3105 }
3106}
3107
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003108static void
3109weston_compositor_verify_pointers(struct weston_compositor *ec)
3110{
3111 struct weston_seat *seat;
3112
3113 wl_list_for_each(seat, &ec->seat_list, link) {
3114 if (!seat->pointer)
3115 continue;
3116
3117 weston_pointer_verify(seat->pointer);
3118 }
3119}
3120
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003121WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003122weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003123{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003124 output->destroying = 1;
3125
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003126 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003127 wl_list_remove(&output->link);
3128
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003129 weston_compositor_verify_pointers(output->compositor);
3130
Richard Hughes64ddde12013-05-01 21:52:10 +01003131 wl_signal_emit(&output->destroy_signal, output);
3132
Richard Hughesafe690c2013-05-02 10:10:04 +01003133 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003134 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003135 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003136 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003137
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003138 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003139}
3140
Scott Moreau1bad5db2012-08-18 01:04:05 -06003141static void
3142weston_output_compute_transform(struct weston_output *output)
3143{
3144 struct weston_matrix transform;
3145 int flip;
3146
3147 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003148 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003149
3150 switch(output->transform) {
3151 case WL_OUTPUT_TRANSFORM_FLIPPED:
3152 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3153 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3154 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003155 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003156 flip = -1;
3157 break;
3158 default:
3159 flip = 1;
3160 break;
3161 }
3162
3163 switch(output->transform) {
3164 case WL_OUTPUT_TRANSFORM_NORMAL:
3165 case WL_OUTPUT_TRANSFORM_FLIPPED:
3166 transform.d[0] = flip;
3167 transform.d[1] = 0;
3168 transform.d[4] = 0;
3169 transform.d[5] = 1;
3170 break;
3171 case WL_OUTPUT_TRANSFORM_90:
3172 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3173 transform.d[0] = 0;
3174 transform.d[1] = -flip;
3175 transform.d[4] = 1;
3176 transform.d[5] = 0;
3177 break;
3178 case WL_OUTPUT_TRANSFORM_180:
3179 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3180 transform.d[0] = -flip;
3181 transform.d[1] = 0;
3182 transform.d[4] = 0;
3183 transform.d[5] = -1;
3184 break;
3185 case WL_OUTPUT_TRANSFORM_270:
3186 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3187 transform.d[0] = 0;
3188 transform.d[1] = flip;
3189 transform.d[4] = -1;
3190 transform.d[5] = 0;
3191 break;
3192 default:
3193 break;
3194 }
3195
3196 weston_matrix_multiply(&output->matrix, &transform);
3197}
3198
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003199WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003200weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003201{
Scott Moreau850ca422012-05-21 15:21:25 -06003202 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003203 struct weston_matrix camera;
3204 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003205
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003206 weston_matrix_init(&output->matrix);
3207 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003208 -(output->x + output->width / 2.0),
3209 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003210
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003211 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003212 2.0 / output->width,
3213 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003214
3215 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003216
Scott Moreauccbf29d2012-02-22 14:21:41 -07003217 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003218 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003219 weston_matrix_init(&camera);
3220 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003221 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003222 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003223 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003224 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003225 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003226 weston_matrix_multiply(&output->matrix, &modelview);
3227 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003228
Scott Moreauccbf29d2012-02-22 14:21:41 -07003229 output->dirty = 0;
3230}
3231
Scott Moreau1bad5db2012-08-18 01:04:05 -06003232static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003233weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003234{
3235 output->transform = transform;
3236
3237 switch (transform) {
3238 case WL_OUTPUT_TRANSFORM_90:
3239 case WL_OUTPUT_TRANSFORM_270:
3240 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3241 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3242 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003243 output->width = output->current_mode->height;
3244 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003245 break;
3246 case WL_OUTPUT_TRANSFORM_NORMAL:
3247 case WL_OUTPUT_TRANSFORM_180:
3248 case WL_OUTPUT_TRANSFORM_FLIPPED:
3249 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003250 output->width = output->current_mode->width;
3251 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003252 break;
3253 default:
3254 break;
3255 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003256
Hardening57388e42013-09-18 23:56:36 +02003257 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003258 output->width /= scale;
3259 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003260}
3261
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003262static void
3263weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003264{
3265 output->x = x;
3266 output->y = y;
3267
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003268 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003269 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003270 output->width,
3271 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003272}
3273
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003274WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003275weston_output_move(struct weston_output *output, int x, int y)
3276{
3277 pixman_region32_t old_region;
3278 struct wl_resource *resource;
3279
3280 output->move_x = x - output->x;
3281 output->move_y = y - output->y;
3282
3283 if (output->move_x == 0 && output->move_y == 0)
3284 return;
3285
3286 pixman_region32_init(&old_region);
3287 pixman_region32_copy(&old_region, &output->region);
3288
3289 weston_output_init_geometry(output, x, y);
3290
3291 output->dirty = 1;
3292
3293 /* Move views on this output. */
3294 wl_signal_emit(&output->move_signal, output);
3295
3296 /* Notify clients of the change for output position. */
3297 wl_resource_for_each(resource, &output->resource_list)
3298 wl_output_send_geometry(resource,
3299 output->x,
3300 output->y,
3301 output->mm_width,
3302 output->mm_height,
3303 output->subpixel,
3304 output->make,
3305 output->model,
3306 output->transform);
3307}
3308
3309WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003310weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003311 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003312 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003313{
3314 output->compositor = c;
3315 output->x = x;
3316 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003317 output->mm_width = mm_width;
3318 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003319 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003320 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003321
Alexander Larsson0b135062013-05-28 16:23:36 +02003322 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003323 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003324
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003325 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003326 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003327
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003328 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003329 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003330 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003331 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003332 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003333
Casey Dahlin58ba1372012-04-19 22:50:08 -04003334 output->id = ffs(~output->compositor->output_id_pool) - 1;
3335 output->compositor->output_id_pool |= 1 << output->id;
3336
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003337 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003338 wl_global_create(c->wl_display, &wl_output_interface, 2,
3339 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003340 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003341}
3342
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003343WL_EXPORT void
3344weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003345 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003346 wl_fixed_t *x, wl_fixed_t *y)
3347{
3348 wl_fixed_t tx, ty;
3349 wl_fixed_t width, height;
3350
Hardeningff39efa2013-09-18 23:56:35 +02003351 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3352 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003353
3354 switch(output->transform) {
3355 case WL_OUTPUT_TRANSFORM_NORMAL:
3356 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003357 tx = device_x;
3358 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003359 break;
3360 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003361 tx = device_y;
3362 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003363 break;
3364 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003365 tx = width - device_x;
3366 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003367 break;
3368 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003369 tx = width - device_y;
3370 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003371 break;
3372 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003373 tx = width - device_x;
3374 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003375 break;
3376 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003377 tx = width - device_y;
3378 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003379 break;
3380 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003381 tx = device_x;
3382 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003383 break;
3384 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003385 tx = device_y;
3386 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003387 break;
3388 }
3389
Hardeningff39efa2013-09-18 23:56:35 +02003390 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3391 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003392}
3393
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003394static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003395compositor_bind(struct wl_client *client,
3396 void *data, uint32_t version, uint32_t id)
3397{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003398 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003399 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003400
Jason Ekstranda85118c2013-06-27 20:17:02 -05003401 resource = wl_resource_create(client, &wl_compositor_interface,
3402 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003403 if (resource == NULL) {
3404 wl_client_post_no_memory(client);
3405 return;
3406 }
3407
3408 wl_resource_set_implementation(resource, &compositor_interface,
3409 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003410}
3411
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003412static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003413log_uname(void)
3414{
3415 struct utsname usys;
3416
3417 uname(&usys);
3418
3419 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3420 usys.version, usys.machine);
3421}
3422
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003423WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003424weston_environment_get_fd(const char *env)
3425{
3426 char *e, *end;
3427 int fd, flags;
3428
3429 e = getenv(env);
3430 if (!e)
3431 return -1;
3432 fd = strtol(e, &end, 0);
3433 if (*end != '\0')
3434 return -1;
3435
3436 flags = fcntl(fd, F_GETFD);
3437 if (flags == -1)
3438 return -1;
3439
3440 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3441 unsetenv(env);
3442
3443 return fd;
3444}
3445
3446WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003447weston_compositor_init(struct weston_compositor *ec,
3448 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003449 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003450 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003451{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003452 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003453 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003454 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003455
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003456 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003457 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003458 wl_signal_init(&ec->destroy_signal);
3459 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003460 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003461 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003462 wl_signal_init(&ec->idle_signal);
3463 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003464 wl_signal_init(&ec->show_input_panel_signal);
3465 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003466 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003467 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003468 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003469 wl_signal_init(&ec->session_signal);
3470 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003471
Casey Dahlin58ba1372012-04-19 22:50:08 -04003472 ec->output_id_pool = 0;
3473
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003474 if (!wl_global_create(display, &wl_compositor_interface, 3,
3475 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003476 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003477
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003478 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3479 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003480 return -1;
3481
Jason Ekstranda7af7042013-10-12 22:38:11 -05003482 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003483 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003484 wl_list_init(&ec->layer_list);
3485 wl_list_init(&ec->seat_list);
3486 wl_list_init(&ec->output_list);
3487 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003488 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003489 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003490 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003491 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003492 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003493
Xiong Zhang97116532013-10-23 13:58:31 +08003494 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003495 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003496
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003497 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3498 weston_config_section_get_string(s, "keymap_rules",
3499 (char **) &xkb_names.rules, NULL);
3500 weston_config_section_get_string(s, "keymap_model",
3501 (char **) &xkb_names.model, NULL);
3502 weston_config_section_get_string(s, "keymap_layout",
3503 (char **) &xkb_names.layout, NULL);
3504 weston_config_section_get_string(s, "keymap_variant",
3505 (char **) &xkb_names.variant, NULL);
3506 weston_config_section_get_string(s, "keymap_options",
3507 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003508
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003509 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3510 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003511
3512 ec->ping_handler = NULL;
3513
3514 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003515 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003516
3517 wl_data_device_manager_init(ec->wl_display);
3518
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003519 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003520
Daniel Stone725c2c32012-06-22 14:04:36 +01003521 loop = wl_display_get_event_loop(ec->wl_display);
3522 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3523 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3524
3525 ec->input_loop = wl_event_loop_create();
3526
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003527 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3528 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3529
3530 weston_compositor_schedule_repaint(ec);
3531
Daniel Stone725c2c32012-06-22 14:04:36 +01003532 return 0;
3533}
3534
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003535WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003536weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003537{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003538 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003539
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003540 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003541 if (ec->input_loop_source)
3542 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003543
Matt Roper361d2ad2011-08-29 13:52:23 -07003544 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003545 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003546 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003547
Daniel Stone325fc2d2012-05-30 16:31:58 +01003548 weston_binding_list_destroy_all(&ec->key_binding_list);
3549 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003550 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003551 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003552 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003553
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003554 weston_plane_release(&ec->primary_plane);
3555
Jonas Ådahlc97af922012-03-28 22:36:09 +02003556 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003557
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003558 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003559}
3560
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003561WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003562weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3563 const struct weston_pointer_grab_interface *interface)
3564{
3565 struct weston_seat *seat;
3566
3567 ec->default_pointer_grab = interface;
3568 wl_list_for_each(seat, &ec->seat_list, link) {
3569 if (seat->pointer) {
3570 weston_pointer_set_default_grab(seat->pointer,
3571 interface);
3572 }
3573 }
3574}
3575
3576WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003577weston_version(int *major, int *minor, int *micro)
3578{
3579 *major = WESTON_VERSION_MAJOR;
3580 *minor = WESTON_VERSION_MINOR;
3581 *micro = WESTON_VERSION_MICRO;
3582}
3583
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003584static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003585 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003586 const char *desc;
3587} capability_strings[] = {
3588 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003589 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003590};
3591
3592static void
3593weston_compositor_log_capabilities(struct weston_compositor *compositor)
3594{
3595 unsigned i;
3596 int yes;
3597
3598 weston_log("Compositor capabilities:\n");
3599 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3600 yes = compositor->capabilities & capability_strings[i].bit;
3601 weston_log_continue(STAMP_SPACE "%s %s\n",
3602 capability_strings[i].desc,
3603 yes ? "yes" : "no");
3604 }
3605}
3606
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003607static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003608{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003609 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003610
Martin Minarik6d118362012-06-07 18:01:59 +02003611 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003612 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003613
3614 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003615}
3616
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003617#ifdef HAVE_LIBUNWIND
3618
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003619static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003620print_backtrace(void)
3621{
3622 unw_cursor_t cursor;
3623 unw_context_t context;
3624 unw_word_t off;
3625 unw_proc_info_t pip;
3626 int ret, i = 0;
3627 char procname[256];
3628 const char *filename;
3629 Dl_info dlinfo;
3630
3631 pip.unwind_info = NULL;
3632 ret = unw_getcontext(&context);
3633 if (ret) {
3634 weston_log("unw_getcontext: %d\n", ret);
3635 return;
3636 }
3637
3638 ret = unw_init_local(&cursor, &context);
3639 if (ret) {
3640 weston_log("unw_init_local: %d\n", ret);
3641 return;
3642 }
3643
3644 ret = unw_step(&cursor);
3645 while (ret > 0) {
3646 ret = unw_get_proc_info(&cursor, &pip);
3647 if (ret) {
3648 weston_log("unw_get_proc_info: %d\n", ret);
3649 break;
3650 }
3651
3652 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3653 if (ret && ret != -UNW_ENOMEM) {
3654 if (ret != -UNW_EUNSPEC)
3655 weston_log("unw_get_proc_name: %d\n", ret);
3656 procname[0] = '?';
3657 procname[1] = 0;
3658 }
3659
3660 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3661 *dlinfo.dli_fname)
3662 filename = dlinfo.dli_fname;
3663 else
3664 filename = "?";
3665
3666 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3667 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3668
3669 ret = unw_step(&cursor);
3670 if (ret < 0)
3671 weston_log("unw_step: %d\n", ret);
3672 }
3673}
3674
3675#else
3676
3677static void
3678print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003679{
3680 void *buffer[32];
3681 int i, count;
3682 Dl_info info;
3683
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003684 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3685 for (i = 0; i < count; i++) {
3686 dladdr(buffer[i], &info);
3687 weston_log(" [%016lx] %s (%s)\n",
3688 (long) buffer[i],
3689 info.dli_sname ? info.dli_sname : "--",
3690 info.dli_fname);
3691 }
3692}
3693
3694#endif
3695
3696static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003697on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003698{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003699 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003700 * then call the backend restore function, which will switch
3701 * back to the vt we launched from or ungrab X etc and then
3702 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003703 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003704 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003705 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003706
Peter Maatmane5b42e42013-03-27 22:38:53 +01003707 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003708
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003709 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003710
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003711 segv_compositor->restore(segv_compositor);
3712
3713 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003714}
3715
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003716WL_EXPORT void *
3717weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003718{
3719 char path[PATH_MAX];
3720 void *module, *init;
3721
3722 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003723 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003724 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003725 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003726
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003727 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3728 if (module) {
3729 weston_log("Module '%s' already loaded\n", path);
3730 dlclose(module);
3731 return NULL;
3732 }
3733
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003734 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003735 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003736 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003737 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003738 return NULL;
3739 }
3740
3741 init = dlsym(module, entrypoint);
3742 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003743 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003744 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003745 return NULL;
3746 }
3747
3748 return init;
3749}
3750
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003751static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003752load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003753 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003754{
3755 const char *p, *end;
3756 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003757 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003758 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003759
3760 if (modules == NULL)
3761 return 0;
3762
3763 p = modules;
3764 while (*p) {
3765 end = strchrnul(p, ',');
3766 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003767 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003768 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003769 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003770 p = end;
3771 while (*p == ',')
3772 p++;
3773
3774 }
3775
3776 return 0;
3777}
3778
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003779static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003780 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3781
3782static const char xdg_wrong_message[] =
3783 "fatal: environment variable XDG_RUNTIME_DIR\n"
3784 "is set to \"%s\", which is not a directory.\n";
3785
3786static const char xdg_wrong_mode_message[] =
3787 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3788 "correctly. Unix access mode must be 0700 but is %o,\n"
3789 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003790 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003791
3792static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003793 "Refer to your distribution on how to get it, or\n"
3794 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3795 "on how to implement it.\n";
3796
Martin Minarik37032f82012-06-18 20:15:18 +02003797static void
3798verify_xdg_runtime_dir(void)
3799{
3800 char *dir = getenv("XDG_RUNTIME_DIR");
3801 struct stat s;
3802
3803 if (!dir) {
3804 weston_log(xdg_error_message);
3805 weston_log_continue(xdg_detail_message);
3806 exit(EXIT_FAILURE);
3807 }
3808
3809 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3810 weston_log(xdg_wrong_message, dir);
3811 weston_log_continue(xdg_detail_message);
3812 exit(EXIT_FAILURE);
3813 }
3814
3815 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3816 weston_log(xdg_wrong_mode_message,
3817 dir, s.st_mode & 0777, s.st_uid);
3818 weston_log_continue(xdg_detail_message);
3819 }
3820}
3821
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003822static int
3823usage(int error_code)
3824{
3825 fprintf(stderr,
3826 "Usage: weston [OPTIONS]\n\n"
3827 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3828 "Weston supports multiple backends, and depending on which backend is in use\n"
3829 "different options will be accepted.\n\n"
3830
3831
3832 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003833 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003834 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003835 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3836 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003837 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003838 " -S, --socket=NAME\tName of socket to listen on\n"
3839 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003840 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003841 " --log==FILE\t\tLog to the given file\n"
3842 " -h, --help\t\tThis help message\n\n");
3843
3844 fprintf(stderr,
3845 "Options for drm-backend.so:\n\n"
3846 " --connector=ID\tBring up only this connector\n"
3847 " --seat=SEAT\t\tThe seat that weston should run on\n"
3848 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003849 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003850 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3851
3852 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003853 "Options for fbdev-backend.so:\n\n"
3854 " --tty=TTY\t\tThe tty to use\n"
3855 " --device=DEVICE\tThe framebuffer device to use\n\n");
3856
3857 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003858 "Options for x11-backend.so:\n\n"
3859 " --width=WIDTH\t\tWidth of X window\n"
3860 " --height=HEIGHT\tHeight of X window\n"
3861 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003862 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003863 " --output-count=COUNT\tCreate multiple outputs\n"
3864 " --no-input\t\tDont create input devices\n\n");
3865
3866 fprintf(stderr,
3867 "Options for wayland-backend.so:\n\n"
3868 " --width=WIDTH\t\tWidth of Wayland surface\n"
3869 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003870 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003871 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003872 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003873 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003874 " --display=DISPLAY\tWayland display to connect to\n\n");
3875
Pekka Paalanene31e0532013-05-22 18:03:07 +03003876#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3877 fprintf(stderr,
3878 "Options for rpi-backend.so:\n\n"
3879 " --tty=TTY\t\tThe tty to use\n"
3880 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3881 " --transform=TR\tThe output transformation, TR is one of:\n"
3882 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003883 " --opaque-regions\tEnable support for opaque regions, can be "
3884 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003885 "\n");
3886#endif
3887
Hardeningc077a842013-07-08 00:51:35 +02003888#if defined(BUILD_RDP_COMPOSITOR)
3889 fprintf(stderr,
3890 "Options for rdp-backend.so:\n\n"
3891 " --width=WIDTH\t\tWidth of desktop\n"
3892 " --height=HEIGHT\tHeight of desktop\n"
3893 " --extra-modes=MODES\t\n"
3894 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3895 " --address=ADDR\tThe address to bind\n"
3896 " --port=PORT\tThe port to listen on\n"
3897 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3898 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3899 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3900 "\n");
3901#endif
3902
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003903 exit(error_code);
3904}
3905
Peter Maatmane5b42e42013-03-27 22:38:53 +01003906static void
3907catch_signals(void)
3908{
3909 struct sigaction action;
3910
3911 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3912 action.sa_sigaction = on_caught_signal;
3913 sigemptyset(&action.sa_mask);
3914 sigaction(SIGSEGV, &action, NULL);
3915 sigaction(SIGABRT, &action, NULL);
3916}
3917
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003918int main(int argc, char *argv[])
3919{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003920 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003921 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003922 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003923 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003924 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003925 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003926 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003927 int *argc, char *argv[],
3928 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003929 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003930 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003931 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003932 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003933 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003934 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003935 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003936 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003937 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003938 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003939 struct weston_config *config;
3940 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003941
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003942 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003943 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003944 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003945 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3946 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003947 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003948 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003949 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003950 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003951 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003952
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003953 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003954
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003955 if (help)
3956 usage(EXIT_SUCCESS);
3957
Scott Moreau12245142012-08-29 15:15:58 -06003958 if (version) {
3959 printf(PACKAGE_STRING "\n");
3960 return EXIT_SUCCESS;
3961 }
3962
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003963 weston_log_file_open(log);
3964
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003965 weston_log("%s\n"
3966 STAMP_SPACE "%s\n"
3967 STAMP_SPACE "Bug reports to: %s\n"
3968 STAMP_SPACE "Build: %s\n",
3969 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003970 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003971 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003972
Martin Minarik37032f82012-06-18 20:15:18 +02003973 verify_xdg_runtime_dir();
3974
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003975 display = wl_display_create();
3976
Tiago Vignatti2116b892011-08-08 05:52:59 -07003977 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003978 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3979 display);
3980 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3981 display);
3982 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3983 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003984
3985 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003986 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3987 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003988
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003989 config = weston_config_parse("weston.ini");
3990 if (config != NULL) {
3991 weston_log("Using config file '%s'\n",
3992 weston_config_get_full_path(config));
3993 } else {
3994 weston_log("Starting with no config file.\n");
3995 }
3996 section = weston_config_get_section(config, "core", NULL, NULL);
3997
3998 weston_config_section_get_string(section, "backend", &backend, NULL);
3999 if (option_backend) {
4000 backend = option_backend;
4001 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004002 if (!backend) {
4003 if (getenv("WAYLAND_DISPLAY"))
4004 backend = "wayland-backend.so";
4005 else if (getenv("DISPLAY"))
4006 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004007 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02004008 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004009 }
4010
Jason Ekstranda985da42013-08-22 17:28:03 -05004011 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004012
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004013 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004014 if (!backend_init)
4015 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004016
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004017 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004018 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004019 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004020 exit(EXIT_FAILURE);
4021 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004022
Peter Maatmane5b42e42013-03-27 22:38:53 +01004023 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004024 segv_compositor = ec;
4025
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004026 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004027 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004028
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004029 setenv("WAYLAND_DISPLAY", socket_name, 1);
4030
Jason Ekstranda985da42013-08-22 17:28:03 -05004031 if (!shell)
4032 weston_config_section_get_string(section, "shell", &shell,
4033 "desktop-shell.so");
4034 if (load_modules(ec, shell, &argc, argv) < 0)
4035 goto out;
4036
Ossama Othmana50e6e42013-05-14 09:48:26 -07004037 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004038 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004039 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004040 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004041
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004042 for (i = 1; i < argc; i++)
4043 weston_log("fatal: unhandled option: %s\n", argv[i]);
4044 if (argc > 1) {
4045 ret = EXIT_FAILURE;
4046 goto out;
4047 }
4048
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004049 weston_compositor_log_capabilities(ec);
4050
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004051 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004052 weston_log("fatal: failed to add socket: %m\n");
4053 ret = EXIT_FAILURE;
4054 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004055 }
4056
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004057 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004058
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004059 wl_display_run(display);
4060
4061 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004062 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004063 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004064
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004065 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004066
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004067 for (i = ARRAY_LENGTH(signals); i;)
4068 wl_event_source_remove(signals[--i]);
4069
Daniel Stone855028f2012-05-01 20:37:10 +01004070 weston_compositor_xkb_destroy(ec);
4071
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004072 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004073 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004074
Martin Minarik19e6f262012-06-07 13:08:46 +02004075 weston_log_file_close();
4076
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004077 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004078}