blob: e4eb56dcb07722dbdf935be00959bd743b74833f [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
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001762 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001763 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001764
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001765 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001766 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001767 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 wl_list_for_each(ev, &ec->view_list, link)
1769 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001770
Pekka Paalanene67858b2013-04-25 13:57:42 +03001771 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001772 wl_list_for_each(ev, &ec->view_list, link) {
1773 /* Note: This operation is safe to do multiple times on the
1774 * same surface.
1775 */
1776 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001777 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 &ev->surface->frame_callback_list);
1779 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001780 }
1781 }
1782
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001783 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001784
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001785 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001786 pixman_region32_intersect(&output_damage,
1787 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001788 pixman_region32_subtract(&output_damage,
1789 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001790
Scott Moreauccbf29d2012-02-22 14:21:41 -07001791 if (output->dirty)
1792 weston_output_update_matrix(output);
1793
David Herrmann1edf44c2013-10-22 17:11:26 +02001794 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001795
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001796 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001797
Kristian Høgsbergef044142011-06-21 15:02:12 -04001798 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001799
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001800 weston_compositor_repick(ec);
1801 wl_event_loop_dispatch(ec->input_loop, 0);
1802
Jonas Ådahldb773762012-06-13 00:01:21 +02001803 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001804 wl_callback_send_done(cb->resource, msecs);
1805 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001806 }
1807
Scott Moreaud64cf212012-06-08 19:40:54 -06001808 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001809 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001810 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001811 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001812
1813 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001814}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001815
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001816static int
1817weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001818{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001819 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001820
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001821 wl_event_loop_dispatch(compositor->input_loop, 0);
1822
1823 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001824}
1825
1826WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001827weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001828{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001829 struct weston_compositor *compositor = output->compositor;
1830 struct wl_event_loop *loop =
1831 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001832 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001833
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001834 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001835
1836 if (output->repaint_needed &&
1837 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1838 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001839 r = weston_output_repaint(output, msecs);
1840 if (!r)
1841 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001842 }
1843
1844 output->repaint_scheduled = 0;
1845 if (compositor->input_loop_source)
1846 return;
1847
1848 fd = wl_event_loop_get_fd(compositor->input_loop);
1849 compositor->input_loop_source =
1850 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1851 weston_compositor_read_input, compositor);
1852}
1853
1854static void
1855idle_repaint(void *data)
1856{
1857 struct weston_output *output = data;
1858
Jonas Ådahle5a12252013-04-05 23:07:11 +02001859 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001860}
1861
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001862WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001863weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1864{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001865 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001866 if (below != NULL)
1867 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001868}
1869
1870WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001871weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001872{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001873 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001874 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001875
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001876 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1877 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001878 return;
1879
Kristian Høgsbergef044142011-06-21 15:02:12 -04001880 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001881 output->repaint_needed = 1;
1882 if (output->repaint_scheduled)
1883 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001884
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001885 wl_event_loop_add_idle(loop, idle_repaint, output);
1886 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001887
1888 if (compositor->input_loop_source) {
1889 wl_event_source_remove(compositor->input_loop_source);
1890 compositor->input_loop_source = NULL;
1891 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001892}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001893
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001894WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001895weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1896{
1897 struct weston_output *output;
1898
1899 wl_list_for_each(output, &compositor->output_list, link)
1900 weston_output_schedule_repaint(output);
1901}
1902
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001903static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001904surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001905{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001906 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001907}
1908
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001909static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001910surface_attach(struct wl_client *client,
1911 struct wl_resource *resource,
1912 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1913{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001914 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001915 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001916
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001917 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001918 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001919 if (buffer == NULL) {
1920 wl_client_post_no_memory(client);
1921 return;
1922 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001923 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001924
Pekka Paalanende685b82012-12-04 15:58:12 +02001925 /* Attach, attach, without commit in between does not send
1926 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001927 if (surface->pending.buffer)
1928 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001929
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001930 surface->pending.sx = sx;
1931 surface->pending.sy = sy;
1932 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001933 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001934 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001935 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001936 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001937 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001938}
1939
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001940static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001941surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001942 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001943 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001944{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001945 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001946
Pekka Paalanen8e159182012-10-10 12:49:25 +03001947 pixman_region32_union_rect(&surface->pending.damage,
1948 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001949 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001950}
1951
Kristian Høgsberg33418202011-08-16 23:01:28 -04001952static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001953destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001954{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001955 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001956
1957 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001958 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001959}
1960
1961static void
1962surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001963 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001964{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001965 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001966 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001967
1968 cb = malloc(sizeof *cb);
1969 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001970 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001971 return;
1972 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001973
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001974 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1975 callback);
1976 if (cb->resource == NULL) {
1977 free(cb);
1978 wl_resource_post_no_memory(resource);
1979 return;
1980 }
1981
Jason Ekstranda85118c2013-06-27 20:17:02 -05001982 wl_resource_set_implementation(cb->resource, NULL, cb,
1983 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001984
Pekka Paalanenbc106382012-10-10 12:49:31 +03001985 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001986}
1987
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001988static void
1989surface_set_opaque_region(struct wl_client *client,
1990 struct wl_resource *resource,
1991 struct wl_resource *region_resource)
1992{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001993 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001994 struct weston_region *region;
1995
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001996 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001997 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001998 pixman_region32_copy(&surface->pending.opaque,
1999 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002000 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002001 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002002 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002003}
2004
2005static void
2006surface_set_input_region(struct wl_client *client,
2007 struct wl_resource *resource,
2008 struct wl_resource *region_resource)
2009{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002010 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002011 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002012
2013 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002014 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002015 pixman_region32_copy(&surface->pending.input,
2016 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002017 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002018 pixman_region32_fini(&surface->pending.input);
2019 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002020 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002021}
2022
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002023static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002024weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002025{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002026 struct weston_subsurface *sub;
2027
2028 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2029 parent_link_pending) {
2030 wl_list_remove(&sub->parent_link);
2031 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2032 }
2033}
2034
2035static void
2036weston_surface_commit(struct weston_surface *surface)
2037{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002038 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002039 pixman_region32_t opaque;
2040
Alexander Larsson4ea95522013-05-22 14:41:37 +02002041 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002042 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002043 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002044
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002045 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01002046 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002047 weston_surface_attach(surface, surface->pending.buffer);
2048
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002049 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01002050
2051 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002052 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002053 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002054
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002055 if (surface->pending.buffer)
2056 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2057 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002058 surface->pending.sx = 0;
2059 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002060 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002061
2062 /* wl_surface.damage */
2063 pixman_region32_union(&surface->damage, &surface->damage,
2064 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002065 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2066 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002067 surface->width,
2068 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002069 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002070
2071 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002072 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 surface->width,
2074 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002075 pixman_region32_intersect(&opaque,
2076 &opaque, &surface->pending.opaque);
2077
2078 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2079 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 wl_list_for_each(view, &surface->views, surface_link)
2081 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002082 }
2083
2084 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002085
2086 /* wl_surface.set_input_region */
2087 pixman_region32_fini(&surface->input);
2088 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002089 surface->width,
2090 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002091 pixman_region32_intersect(&surface->input,
2092 &surface->input, &surface->pending.input);
2093
Pekka Paalanenbc106382012-10-10 12:49:31 +03002094 /* wl_surface.frame */
2095 wl_list_insert_list(&surface->frame_callback_list,
2096 &surface->pending.frame_callback_list);
2097 wl_list_init(&surface->pending.frame_callback_list);
2098
Pekka Paalanene67858b2013-04-25 13:57:42 +03002099 weston_surface_commit_subsurface_order(surface);
2100
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002101 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002102}
2103
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002104static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105weston_subsurface_commit(struct weston_subsurface *sub);
2106
2107static void
2108weston_subsurface_parent_commit(struct weston_subsurface *sub,
2109 int parent_is_synchronized);
2110
2111static void
2112surface_commit(struct wl_client *client, struct wl_resource *resource)
2113{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002114 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002115 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2116
2117 if (sub) {
2118 weston_subsurface_commit(sub);
2119 return;
2120 }
2121
2122 weston_surface_commit(surface);
2123
2124 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2125 if (sub->surface != surface)
2126 weston_subsurface_parent_commit(sub, 0);
2127 }
2128}
2129
2130static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002131surface_set_buffer_transform(struct wl_client *client,
2132 struct wl_resource *resource, int transform)
2133{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002134 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002135
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002136 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002137}
2138
Alexander Larsson4ea95522013-05-22 14:41:37 +02002139static void
2140surface_set_buffer_scale(struct wl_client *client,
2141 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002142 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002143{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002144 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002145
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002146 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002147}
2148
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002149static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002150 surface_destroy,
2151 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002152 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002153 surface_frame,
2154 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002155 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002156 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002157 surface_set_buffer_transform,
2158 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002159};
2160
2161static void
2162compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002163 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002164{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002165 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002166 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002167
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002168 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002169 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002170 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002171 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002172 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002173
Jason Ekstranda85118c2013-06-27 20:17:02 -05002174 surface->resource =
2175 wl_resource_create(client, &wl_surface_interface,
2176 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002177 if (surface->resource == NULL) {
2178 weston_surface_destroy(surface);
2179 wl_resource_post_no_memory(resource);
2180 return;
2181 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002182 wl_resource_set_implementation(surface->resource, &surface_interface,
2183 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002184}
2185
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002186static void
2187destroy_region(struct wl_resource *resource)
2188{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002189 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002190
2191 pixman_region32_fini(&region->region);
2192 free(region);
2193}
2194
2195static void
2196region_destroy(struct wl_client *client, struct wl_resource *resource)
2197{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002198 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002199}
2200
2201static void
2202region_add(struct wl_client *client, struct wl_resource *resource,
2203 int32_t x, int32_t y, int32_t width, int32_t height)
2204{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002205 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002206
2207 pixman_region32_union_rect(&region->region, &region->region,
2208 x, y, width, height);
2209}
2210
2211static void
2212region_subtract(struct wl_client *client, struct wl_resource *resource,
2213 int32_t x, int32_t y, int32_t width, int32_t height)
2214{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002215 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002216 pixman_region32_t rect;
2217
2218 pixman_region32_init_rect(&rect, x, y, width, height);
2219 pixman_region32_subtract(&region->region, &region->region, &rect);
2220 pixman_region32_fini(&rect);
2221}
2222
2223static const struct wl_region_interface region_interface = {
2224 region_destroy,
2225 region_add,
2226 region_subtract
2227};
2228
2229static void
2230compositor_create_region(struct wl_client *client,
2231 struct wl_resource *resource, uint32_t id)
2232{
2233 struct weston_region *region;
2234
2235 region = malloc(sizeof *region);
2236 if (region == NULL) {
2237 wl_resource_post_no_memory(resource);
2238 return;
2239 }
2240
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002241 pixman_region32_init(&region->region);
2242
Jason Ekstranda85118c2013-06-27 20:17:02 -05002243 region->resource =
2244 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002245 if (region->resource == NULL) {
2246 free(region);
2247 wl_resource_post_no_memory(resource);
2248 return;
2249 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002250 wl_resource_set_implementation(region->resource, &region_interface,
2251 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002252}
2253
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002254static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002255 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002256 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002257};
2258
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002259static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002260weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2261{
2262 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002264 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002265
Alexander Larsson4ea95522013-05-22 14:41:37 +02002266 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002267 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002268 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002269
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270 /* wl_surface.attach */
2271 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2272 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2273 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2274
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002275 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002276
2277 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002278 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002279 sub->cached.sx = 0;
2280 sub->cached.sy = 0;
2281 sub->cached.newly_attached = 0;
2282
2283 /* wl_surface.damage */
2284 pixman_region32_union(&surface->damage, &surface->damage,
2285 &sub->cached.damage);
2286 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2287 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002288 surface->width,
2289 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002290 empty_region(&sub->cached.damage);
2291
2292 /* wl_surface.set_opaque_region */
2293 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 surface->width,
2295 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002296 pixman_region32_intersect(&opaque,
2297 &opaque, &sub->cached.opaque);
2298
2299 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2300 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 wl_list_for_each(view, &surface->views, surface_link)
2302 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 }
2304
2305 pixman_region32_fini(&opaque);
2306
2307 /* wl_surface.set_input_region */
2308 pixman_region32_fini(&surface->input);
2309 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002310 surface->width,
2311 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002312 pixman_region32_intersect(&surface->input,
2313 &surface->input, &sub->cached.input);
2314
2315 /* wl_surface.frame */
2316 wl_list_insert_list(&surface->frame_callback_list,
2317 &sub->cached.frame_callback_list);
2318 wl_list_init(&sub->cached.frame_callback_list);
2319
2320 weston_surface_commit_subsurface_order(surface);
2321
2322 weston_surface_schedule_repaint(surface);
2323
2324 sub->cached.has_data = 0;
2325}
2326
2327static void
2328weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2329{
2330 struct weston_surface *surface = sub->surface;
2331
2332 /*
2333 * If this commit would cause the surface to move by the
2334 * attach(dx, dy) parameters, the old damage region must be
2335 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002336 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002337 */
2338 pixman_region32_translate(&sub->cached.damage,
2339 -surface->pending.sx, -surface->pending.sy);
2340 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2341 &surface->pending.damage);
2342 empty_region(&surface->pending.damage);
2343
2344 if (surface->pending.newly_attached) {
2345 sub->cached.newly_attached = 1;
2346 weston_buffer_reference(&sub->cached.buffer_ref,
2347 surface->pending.buffer);
2348 }
2349 sub->cached.sx += surface->pending.sx;
2350 sub->cached.sy += surface->pending.sy;
2351 surface->pending.sx = 0;
2352 surface->pending.sy = 0;
2353 surface->pending.newly_attached = 0;
2354
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002355 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002356
2357 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2358
2359 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2360
2361 wl_list_insert_list(&sub->cached.frame_callback_list,
2362 &surface->pending.frame_callback_list);
2363 wl_list_init(&surface->pending.frame_callback_list);
2364
2365 sub->cached.has_data = 1;
2366}
2367
2368static int
2369weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2370{
2371 while (sub) {
2372 if (sub->synchronized)
2373 return 1;
2374
2375 if (!sub->parent)
2376 return 0;
2377
2378 sub = weston_surface_to_subsurface(sub->parent);
2379 }
2380
2381 return 0;
2382}
2383
2384static void
2385weston_subsurface_commit(struct weston_subsurface *sub)
2386{
2387 struct weston_surface *surface = sub->surface;
2388 struct weston_subsurface *tmp;
2389
2390 /* Recursive check for effectively synchronized. */
2391 if (weston_subsurface_is_synchronized(sub)) {
2392 weston_subsurface_commit_to_cache(sub);
2393 } else {
2394 if (sub->cached.has_data) {
2395 /* flush accumulated state from cache */
2396 weston_subsurface_commit_to_cache(sub);
2397 weston_subsurface_commit_from_cache(sub);
2398 } else {
2399 weston_surface_commit(surface);
2400 }
2401
2402 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2403 if (tmp->surface != surface)
2404 weston_subsurface_parent_commit(tmp, 0);
2405 }
2406 }
2407}
2408
2409static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002410weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002411{
2412 struct weston_surface *surface = sub->surface;
2413 struct weston_subsurface *tmp;
2414
Pekka Paalanene67858b2013-04-25 13:57:42 +03002415 /* From now on, commit_from_cache the whole sub-tree, regardless of
2416 * the synchronized mode of each child. This sub-surface or some
2417 * of its ancestors were synchronized, so we are synchronized
2418 * all the way down.
2419 */
2420
2421 if (sub->cached.has_data)
2422 weston_subsurface_commit_from_cache(sub);
2423
2424 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2425 if (tmp->surface != surface)
2426 weston_subsurface_parent_commit(tmp, 1);
2427 }
2428}
2429
2430static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002431weston_subsurface_parent_commit(struct weston_subsurface *sub,
2432 int parent_is_synchronized)
2433{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002434 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002435 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002436 wl_list_for_each(view, &sub->surface->views, surface_link)
2437 weston_view_set_position(view,
2438 sub->position.x,
2439 sub->position.y);
2440
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002441 sub->position.set = 0;
2442 }
2443
2444 if (parent_is_synchronized || sub->synchronized)
2445 weston_subsurface_synchronized_commit(sub);
2446}
2447
2448static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002449subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002450{
2451 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002452 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002453
Jason Ekstranda7af7042013-10-12 22:38:11 -05002454 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002455 weston_view_set_position(view,
2456 view->geometry.x + dx,
2457 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002458
2459 /* No need to check parent mappedness, because if parent is not
2460 * mapped, parent is not in a visible layer, so this sub-surface
2461 * will not be drawn either.
2462 */
2463 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002464 /* Cannot call weston_surface_update_transform(),
2465 * because that would call it also for the parent surface,
2466 * which might not be mapped yet. That would lead to
2467 * inconsistent state, where the window could never be
2468 * mapped.
2469 *
2470 * Instead just assing any output, to make
2471 * weston_surface_is_mapped() return true, so that when the
2472 * parent surface does get mapped, this one will get
2473 * included, too. See surface_list_add().
2474 */
2475 assert(!wl_list_empty(&compositor->output_list));
2476 surface->output = container_of(compositor->output_list.next,
2477 struct weston_output, link);
2478 }
2479}
2480
2481static struct weston_subsurface *
2482weston_surface_to_subsurface(struct weston_surface *surface)
2483{
2484 if (surface->configure == subsurface_configure)
2485 return surface->configure_private;
2486
2487 return NULL;
2488}
2489
Pekka Paalanen01388e22013-04-25 13:57:44 +03002490WL_EXPORT struct weston_surface *
2491weston_surface_get_main_surface(struct weston_surface *surface)
2492{
2493 struct weston_subsurface *sub;
2494
2495 while (surface && (sub = weston_surface_to_subsurface(surface)))
2496 surface = sub->parent;
2497
2498 return surface;
2499}
2500
Pekka Paalanene67858b2013-04-25 13:57:42 +03002501static void
2502subsurface_set_position(struct wl_client *client,
2503 struct wl_resource *resource, int32_t x, int32_t y)
2504{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002505 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002506
2507 if (!sub)
2508 return;
2509
2510 sub->position.x = x;
2511 sub->position.y = y;
2512 sub->position.set = 1;
2513}
2514
2515static struct weston_subsurface *
2516subsurface_from_surface(struct weston_surface *surface)
2517{
2518 struct weston_subsurface *sub;
2519
2520 sub = weston_surface_to_subsurface(surface);
2521 if (sub)
2522 return sub;
2523
2524 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2525 if (sub->surface == surface)
2526 return sub;
2527
2528 return NULL;
2529}
2530
2531static struct weston_subsurface *
2532subsurface_sibling_check(struct weston_subsurface *sub,
2533 struct weston_surface *surface,
2534 const char *request)
2535{
2536 struct weston_subsurface *sibling;
2537
2538 sibling = subsurface_from_surface(surface);
2539
2540 if (!sibling) {
2541 wl_resource_post_error(sub->resource,
2542 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2543 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002544 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002545 return NULL;
2546 }
2547
2548 if (sibling->parent != sub->parent) {
2549 wl_resource_post_error(sub->resource,
2550 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2551 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002552 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553 return NULL;
2554 }
2555
2556 return sibling;
2557}
2558
2559static void
2560subsurface_place_above(struct wl_client *client,
2561 struct wl_resource *resource,
2562 struct wl_resource *sibling_resource)
2563{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002564 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002565 struct weston_surface *surface =
2566 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002567 struct weston_subsurface *sibling;
2568
2569 if (!sub)
2570 return;
2571
2572 sibling = subsurface_sibling_check(sub, surface, "place_above");
2573 if (!sibling)
2574 return;
2575
2576 wl_list_remove(&sub->parent_link_pending);
2577 wl_list_insert(sibling->parent_link_pending.prev,
2578 &sub->parent_link_pending);
2579}
2580
2581static void
2582subsurface_place_below(struct wl_client *client,
2583 struct wl_resource *resource,
2584 struct wl_resource *sibling_resource)
2585{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002586 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002587 struct weston_surface *surface =
2588 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002589 struct weston_subsurface *sibling;
2590
2591 if (!sub)
2592 return;
2593
2594 sibling = subsurface_sibling_check(sub, surface, "place_below");
2595 if (!sibling)
2596 return;
2597
2598 wl_list_remove(&sub->parent_link_pending);
2599 wl_list_insert(&sibling->parent_link_pending,
2600 &sub->parent_link_pending);
2601}
2602
2603static void
2604subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2605{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002606 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002607
2608 if (sub)
2609 sub->synchronized = 1;
2610}
2611
2612static void
2613subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2614{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002615 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002616
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002617 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002619
2620 /* If sub became effectively desynchronized, flush. */
2621 if (!weston_subsurface_is_synchronized(sub))
2622 weston_subsurface_synchronized_commit(sub);
2623 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002624}
2625
2626static void
2627weston_subsurface_cache_init(struct weston_subsurface *sub)
2628{
2629 pixman_region32_init(&sub->cached.damage);
2630 pixman_region32_init(&sub->cached.opaque);
2631 pixman_region32_init(&sub->cached.input);
2632 wl_list_init(&sub->cached.frame_callback_list);
2633 sub->cached.buffer_ref.buffer = NULL;
2634}
2635
2636static void
2637weston_subsurface_cache_fini(struct weston_subsurface *sub)
2638{
2639 struct weston_frame_callback *cb, *tmp;
2640
2641 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002642 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643
2644 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2645 pixman_region32_fini(&sub->cached.damage);
2646 pixman_region32_fini(&sub->cached.opaque);
2647 pixman_region32_fini(&sub->cached.input);
2648}
2649
2650static void
2651weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2652{
2653 wl_list_remove(&sub->parent_link);
2654 wl_list_remove(&sub->parent_link_pending);
2655 wl_list_remove(&sub->parent_destroy_listener.link);
2656 sub->parent = NULL;
2657}
2658
2659static void
2660weston_subsurface_destroy(struct weston_subsurface *sub);
2661
2662static void
2663subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2664{
2665 struct weston_subsurface *sub =
2666 container_of(listener, struct weston_subsurface,
2667 surface_destroy_listener);
2668 assert(data == &sub->surface->resource);
2669
2670 /* The protocol object (wl_resource) is left inert. */
2671 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002672 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002673
2674 weston_subsurface_destroy(sub);
2675}
2676
2677static void
2678subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2679{
2680 struct weston_subsurface *sub =
2681 container_of(listener, struct weston_subsurface,
2682 parent_destroy_listener);
2683 assert(data == &sub->parent->resource);
2684 assert(sub->surface != sub->parent);
2685
2686 if (weston_surface_is_mapped(sub->surface))
2687 weston_surface_unmap(sub->surface);
2688
2689 weston_subsurface_unlink_parent(sub);
2690}
2691
2692static void
2693subsurface_resource_destroy(struct wl_resource *resource)
2694{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002695 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002696
2697 if (sub)
2698 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002699}
2700
2701static void
2702subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2703{
2704 wl_resource_destroy(resource);
2705}
2706
2707static void
2708weston_subsurface_link_parent(struct weston_subsurface *sub,
2709 struct weston_surface *parent)
2710{
2711 sub->parent = parent;
2712 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002713 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002714 &sub->parent_destroy_listener);
2715
2716 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2717 wl_list_insert(&parent->subsurface_list_pending,
2718 &sub->parent_link_pending);
2719}
2720
2721static void
2722weston_subsurface_link_surface(struct weston_subsurface *sub,
2723 struct weston_surface *surface)
2724{
2725 sub->surface = surface;
2726 sub->surface_destroy_listener.notify =
2727 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002728 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729 &sub->surface_destroy_listener);
2730}
2731
2732static void
2733weston_subsurface_destroy(struct weston_subsurface *sub)
2734{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002735 struct weston_view *view, *next;
2736
Pekka Paalanene67858b2013-04-25 13:57:42 +03002737 assert(sub->surface);
2738
2739 if (sub->resource) {
2740 assert(weston_surface_to_subsurface(sub->surface) == sub);
2741 assert(sub->parent_destroy_listener.notify ==
2742 subsurface_handle_parent_destroy);
2743
Jason Ekstranda7af7042013-10-12 22:38:11 -05002744 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2745 weston_view_destroy(view);
2746
Pekka Paalanene67858b2013-04-25 13:57:42 +03002747 if (sub->parent)
2748 weston_subsurface_unlink_parent(sub);
2749
2750 weston_subsurface_cache_fini(sub);
2751
2752 sub->surface->configure = NULL;
2753 sub->surface->configure_private = NULL;
2754 } else {
2755 /* the dummy weston_subsurface for the parent itself */
2756 assert(sub->parent_destroy_listener.notify == NULL);
2757 wl_list_remove(&sub->parent_link);
2758 wl_list_remove(&sub->parent_link_pending);
2759 }
2760
2761 wl_list_remove(&sub->surface_destroy_listener.link);
2762 free(sub);
2763}
2764
2765static const struct wl_subsurface_interface subsurface_implementation = {
2766 subsurface_destroy,
2767 subsurface_set_position,
2768 subsurface_place_above,
2769 subsurface_place_below,
2770 subsurface_set_sync,
2771 subsurface_set_desync
2772};
2773
2774static struct weston_subsurface *
2775weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2776 struct weston_surface *parent)
2777{
2778 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002779 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002780
2781 sub = calloc(1, sizeof *sub);
2782 if (!sub)
2783 return NULL;
2784
Jason Ekstranda7af7042013-10-12 22:38:11 -05002785 wl_list_init(&sub->unused_views);
2786
Jason Ekstranda85118c2013-06-27 20:17:02 -05002787 sub->resource =
2788 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002789 if (!sub->resource) {
2790 free(sub);
2791 return NULL;
2792 }
2793
Jason Ekstranda85118c2013-06-27 20:17:02 -05002794 wl_resource_set_implementation(sub->resource,
2795 &subsurface_implementation,
2796 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002797 weston_subsurface_link_surface(sub, surface);
2798 weston_subsurface_link_parent(sub, parent);
2799 weston_subsurface_cache_init(sub);
2800 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002801
2802 return sub;
2803}
2804
2805/* Create a dummy subsurface for having the parent itself in its
2806 * sub-surface lists. Makes stacking order manipulation easy.
2807 */
2808static struct weston_subsurface *
2809weston_subsurface_create_for_parent(struct weston_surface *parent)
2810{
2811 struct weston_subsurface *sub;
2812
2813 sub = calloc(1, sizeof *sub);
2814 if (!sub)
2815 return NULL;
2816
2817 weston_subsurface_link_surface(sub, parent);
2818 sub->parent = parent;
2819 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2820 wl_list_insert(&parent->subsurface_list_pending,
2821 &sub->parent_link_pending);
2822
2823 return sub;
2824}
2825
2826static void
2827subcompositor_get_subsurface(struct wl_client *client,
2828 struct wl_resource *resource,
2829 uint32_t id,
2830 struct wl_resource *surface_resource,
2831 struct wl_resource *parent_resource)
2832{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002833 struct weston_surface *surface =
2834 wl_resource_get_user_data(surface_resource);
2835 struct weston_surface *parent =
2836 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002837 struct weston_subsurface *sub;
2838 static const char where[] = "get_subsurface: wl_subsurface@";
2839
2840 if (surface == parent) {
2841 wl_resource_post_error(resource,
2842 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2843 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002844 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002845 return;
2846 }
2847
2848 if (weston_surface_to_subsurface(surface)) {
2849 wl_resource_post_error(resource,
2850 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2851 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002852 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002853 return;
2854 }
2855
2856 if (surface->configure) {
2857 wl_resource_post_error(resource,
2858 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2859 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002860 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861 return;
2862 }
2863
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002864 if (weston_surface_get_main_surface(parent) == surface) {
2865 wl_resource_post_error(resource,
2866 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2867 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002868 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002869 return;
2870 }
2871
Pekka Paalanene67858b2013-04-25 13:57:42 +03002872 /* make sure the parent is in its own list */
2873 if (wl_list_empty(&parent->subsurface_list)) {
2874 if (!weston_subsurface_create_for_parent(parent)) {
2875 wl_resource_post_no_memory(resource);
2876 return;
2877 }
2878 }
2879
2880 sub = weston_subsurface_create(id, surface, parent);
2881 if (!sub) {
2882 wl_resource_post_no_memory(resource);
2883 return;
2884 }
2885
2886 surface->configure = subsurface_configure;
2887 surface->configure_private = sub;
2888}
2889
2890static void
2891subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2892{
2893 wl_resource_destroy(resource);
2894}
2895
2896static const struct wl_subcompositor_interface subcompositor_interface = {
2897 subcompositor_destroy,
2898 subcompositor_get_subsurface
2899};
2900
2901static void
2902bind_subcompositor(struct wl_client *client,
2903 void *data, uint32_t version, uint32_t id)
2904{
2905 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002906 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002907
Jason Ekstranda85118c2013-06-27 20:17:02 -05002908 resource =
2909 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002910 if (resource == NULL) {
2911 wl_client_post_no_memory(client);
2912 return;
2913 }
2914 wl_resource_set_implementation(resource, &subcompositor_interface,
2915 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002916}
2917
2918static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002919weston_compositor_dpms(struct weston_compositor *compositor,
2920 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002921{
2922 struct weston_output *output;
2923
2924 wl_list_for_each(output, &compositor->output_list, link)
2925 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002926 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002927}
2928
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002929WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002930weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002931{
Neil Roberts8b62e202013-09-30 13:14:47 +01002932 uint32_t old_state = compositor->state;
2933
2934 /* The state needs to be changed before emitting the wake
2935 * signal because that may try to schedule a repaint which
2936 * will not work if the compositor is still sleeping */
2937 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2938
2939 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002940 case WESTON_COMPOSITOR_SLEEPING:
2941 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2942 /* fall through */
2943 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002944 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002945 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002946 /* fall through */
2947 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002948 wl_event_source_timer_update(compositor->idle_source,
2949 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002950 }
2951}
2952
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002953WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002954weston_compositor_offscreen(struct weston_compositor *compositor)
2955{
2956 switch (compositor->state) {
2957 case WESTON_COMPOSITOR_OFFSCREEN:
2958 return;
2959 case WESTON_COMPOSITOR_SLEEPING:
2960 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2961 /* fall through */
2962 default:
2963 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2964 wl_event_source_timer_update(compositor->idle_source, 0);
2965 }
2966}
2967
2968WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002969weston_compositor_sleep(struct weston_compositor *compositor)
2970{
2971 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2972 return;
2973
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002974 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002975 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2976 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2977}
2978
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002979static int
2980idle_handler(void *data)
2981{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002982 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002983
2984 if (compositor->idle_inhibit)
2985 return 1;
2986
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002987 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002988 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002989
2990 return 1;
2991}
2992
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002993WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002994weston_plane_init(struct weston_plane *plane,
2995 struct weston_compositor *ec,
2996 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002997{
2998 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002999 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003000 plane->x = x;
3001 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003002 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003003
3004 /* Init the link so that the call to wl_list_remove() when releasing
3005 * the plane without ever stacking doesn't lead to a crash */
3006 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003007}
3008
3009WL_EXPORT void
3010weston_plane_release(struct weston_plane *plane)
3011{
Xiong Zhang97116532013-10-23 13:58:31 +08003012 struct weston_view *view;
3013
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003014 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003015 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003016
Xiong Zhang97116532013-10-23 13:58:31 +08003017 wl_list_for_each(view, &plane->compositor->view_list, link) {
3018 if (view->plane == plane)
3019 view->plane = NULL;
3020 }
3021
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003022 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003023}
3024
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003025WL_EXPORT void
3026weston_compositor_stack_plane(struct weston_compositor *ec,
3027 struct weston_plane *plane,
3028 struct weston_plane *above)
3029{
3030 if (above)
3031 wl_list_insert(above->link.prev, &plane->link);
3032 else
3033 wl_list_insert(&ec->plane_list, &plane->link);
3034}
3035
Casey Dahlin9074db52012-04-19 22:50:09 -04003036static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003037{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003038 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003039}
3040
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003041static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003042bind_output(struct wl_client *client,
3043 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003044{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003045 struct weston_output *output = data;
3046 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003047 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003048
Jason Ekstranda85118c2013-06-27 20:17:02 -05003049 resource = wl_resource_create(client, &wl_output_interface,
3050 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003051 if (resource == NULL) {
3052 wl_client_post_no_memory(client);
3053 return;
3054 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003055
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003056 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003057 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003058
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003059 wl_output_send_geometry(resource,
3060 output->x,
3061 output->y,
3062 output->mm_width,
3063 output->mm_height,
3064 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003065 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003066 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003067 if (version >= 2)
3068 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003069 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003070
3071 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003072 wl_output_send_mode(resource,
3073 mode->flags,
3074 mode->width,
3075 mode->height,
3076 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003077 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003078
3079 if (version >= 2)
3080 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003081}
3082
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003083/* Move other outputs when one is removed so the space remains contiguos. */
3084static void
3085weston_compositor_remove_output(struct weston_compositor *compositor,
3086 struct weston_output *remove_output)
3087{
3088 struct weston_output *output;
3089 int offset = 0;
3090
3091 wl_list_for_each(output, &compositor->output_list, link) {
3092 if (output == remove_output) {
3093 offset = output->width;
3094 continue;
3095 }
3096
3097 if (offset > 0) {
3098 weston_output_move(output,
3099 output->x - offset, output->y);
3100 output->dirty = 1;
3101 }
3102 }
3103}
3104
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003105WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003106weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003107{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003108 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003109 wl_list_remove(&output->link);
3110
Richard Hughes64ddde12013-05-01 21:52:10 +01003111 wl_signal_emit(&output->destroy_signal, output);
3112
Richard Hughesafe690c2013-05-02 10:10:04 +01003113 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003114 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003115 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003116 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003117
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003118 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003119}
3120
Scott Moreau1bad5db2012-08-18 01:04:05 -06003121static void
3122weston_output_compute_transform(struct weston_output *output)
3123{
3124 struct weston_matrix transform;
3125 int flip;
3126
3127 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003128 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003129
3130 switch(output->transform) {
3131 case WL_OUTPUT_TRANSFORM_FLIPPED:
3132 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3133 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3134 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003135 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003136 flip = -1;
3137 break;
3138 default:
3139 flip = 1;
3140 break;
3141 }
3142
3143 switch(output->transform) {
3144 case WL_OUTPUT_TRANSFORM_NORMAL:
3145 case WL_OUTPUT_TRANSFORM_FLIPPED:
3146 transform.d[0] = flip;
3147 transform.d[1] = 0;
3148 transform.d[4] = 0;
3149 transform.d[5] = 1;
3150 break;
3151 case WL_OUTPUT_TRANSFORM_90:
3152 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3153 transform.d[0] = 0;
3154 transform.d[1] = -flip;
3155 transform.d[4] = 1;
3156 transform.d[5] = 0;
3157 break;
3158 case WL_OUTPUT_TRANSFORM_180:
3159 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3160 transform.d[0] = -flip;
3161 transform.d[1] = 0;
3162 transform.d[4] = 0;
3163 transform.d[5] = -1;
3164 break;
3165 case WL_OUTPUT_TRANSFORM_270:
3166 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3167 transform.d[0] = 0;
3168 transform.d[1] = flip;
3169 transform.d[4] = -1;
3170 transform.d[5] = 0;
3171 break;
3172 default:
3173 break;
3174 }
3175
3176 weston_matrix_multiply(&output->matrix, &transform);
3177}
3178
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003179WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003180weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003181{
Scott Moreau850ca422012-05-21 15:21:25 -06003182 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003183 struct weston_matrix camera;
3184 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003185
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003186 weston_matrix_init(&output->matrix);
3187 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003188 -(output->x + output->width / 2.0),
3189 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003190
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003191 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003192 2.0 / output->width,
3193 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003194
3195 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003196
Scott Moreauccbf29d2012-02-22 14:21:41 -07003197 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003198 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003199 weston_matrix_init(&camera);
3200 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003201 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003202 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003203 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003204 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003205 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003206 weston_matrix_multiply(&output->matrix, &modelview);
3207 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003208
Scott Moreauccbf29d2012-02-22 14:21:41 -07003209 output->dirty = 0;
3210}
3211
Scott Moreau1bad5db2012-08-18 01:04:05 -06003212static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003213weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003214{
3215 output->transform = transform;
3216
3217 switch (transform) {
3218 case WL_OUTPUT_TRANSFORM_90:
3219 case WL_OUTPUT_TRANSFORM_270:
3220 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3221 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3222 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003223 output->width = output->current_mode->height;
3224 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003225 break;
3226 case WL_OUTPUT_TRANSFORM_NORMAL:
3227 case WL_OUTPUT_TRANSFORM_180:
3228 case WL_OUTPUT_TRANSFORM_FLIPPED:
3229 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003230 output->width = output->current_mode->width;
3231 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003232 break;
3233 default:
3234 break;
3235 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003236
Hardening57388e42013-09-18 23:56:36 +02003237 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003238 output->width /= scale;
3239 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003240}
3241
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003242static void
3243weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003244{
3245 output->x = x;
3246 output->y = y;
3247
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003248 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003249 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003250 output->width,
3251 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003252}
3253
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003254WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003255weston_output_move(struct weston_output *output, int x, int y)
3256{
3257 pixman_region32_t old_region;
3258 struct wl_resource *resource;
3259
3260 output->move_x = x - output->x;
3261 output->move_y = y - output->y;
3262
3263 if (output->move_x == 0 && output->move_y == 0)
3264 return;
3265
3266 pixman_region32_init(&old_region);
3267 pixman_region32_copy(&old_region, &output->region);
3268
3269 weston_output_init_geometry(output, x, y);
3270
3271 output->dirty = 1;
3272
3273 /* Move views on this output. */
3274 wl_signal_emit(&output->move_signal, output);
3275
3276 /* Notify clients of the change for output position. */
3277 wl_resource_for_each(resource, &output->resource_list)
3278 wl_output_send_geometry(resource,
3279 output->x,
3280 output->y,
3281 output->mm_width,
3282 output->mm_height,
3283 output->subpixel,
3284 output->make,
3285 output->model,
3286 output->transform);
3287}
3288
3289WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003290weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003291 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003292 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003293{
3294 output->compositor = c;
3295 output->x = x;
3296 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003297 output->mm_width = mm_width;
3298 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003299 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003300 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003301
Alexander Larsson0b135062013-05-28 16:23:36 +02003302 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003303 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003304
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003305 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003306 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003307
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003308 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003309 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003310 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003311 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003312 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003313
Casey Dahlin58ba1372012-04-19 22:50:08 -04003314 output->id = ffs(~output->compositor->output_id_pool) - 1;
3315 output->compositor->output_id_pool |= 1 << output->id;
3316
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003317 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003318 wl_global_create(c->wl_display, &wl_output_interface, 2,
3319 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003320 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003321}
3322
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003323WL_EXPORT void
3324weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003325 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003326 wl_fixed_t *x, wl_fixed_t *y)
3327{
3328 wl_fixed_t tx, ty;
3329 wl_fixed_t width, height;
3330
Hardeningff39efa2013-09-18 23:56:35 +02003331 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3332 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003333
3334 switch(output->transform) {
3335 case WL_OUTPUT_TRANSFORM_NORMAL:
3336 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003337 tx = device_x;
3338 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003339 break;
3340 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003341 tx = device_y;
3342 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003343 break;
3344 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003345 tx = width - device_x;
3346 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003347 break;
3348 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003349 tx = width - device_y;
3350 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003351 break;
3352 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003353 tx = width - device_x;
3354 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003355 break;
3356 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003357 tx = width - device_y;
3358 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003359 break;
3360 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003361 tx = device_x;
3362 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003363 break;
3364 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003365 tx = device_y;
3366 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003367 break;
3368 }
3369
Hardeningff39efa2013-09-18 23:56:35 +02003370 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3371 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003372}
3373
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003374static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003375compositor_bind(struct wl_client *client,
3376 void *data, uint32_t version, uint32_t id)
3377{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003378 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003379 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003380
Jason Ekstranda85118c2013-06-27 20:17:02 -05003381 resource = wl_resource_create(client, &wl_compositor_interface,
3382 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003383 if (resource == NULL) {
3384 wl_client_post_no_memory(client);
3385 return;
3386 }
3387
3388 wl_resource_set_implementation(resource, &compositor_interface,
3389 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003390}
3391
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003392static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003393log_uname(void)
3394{
3395 struct utsname usys;
3396
3397 uname(&usys);
3398
3399 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3400 usys.version, usys.machine);
3401}
3402
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003403WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003404weston_environment_get_fd(const char *env)
3405{
3406 char *e, *end;
3407 int fd, flags;
3408
3409 e = getenv(env);
3410 if (!e)
3411 return -1;
3412 fd = strtol(e, &end, 0);
3413 if (*end != '\0')
3414 return -1;
3415
3416 flags = fcntl(fd, F_GETFD);
3417 if (flags == -1)
3418 return -1;
3419
3420 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3421 unsetenv(env);
3422
3423 return fd;
3424}
3425
3426WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003427weston_compositor_init(struct weston_compositor *ec,
3428 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003429 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003430 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003431{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003432 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003433 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003434 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003435
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003436 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003437 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003438 wl_signal_init(&ec->destroy_signal);
3439 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003440 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003441 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003442 wl_signal_init(&ec->idle_signal);
3443 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003444 wl_signal_init(&ec->show_input_panel_signal);
3445 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003446 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003447 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003448 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003449 wl_signal_init(&ec->session_signal);
3450 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003451
Casey Dahlin58ba1372012-04-19 22:50:08 -04003452 ec->output_id_pool = 0;
3453
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003454 if (!wl_global_create(display, &wl_compositor_interface, 3,
3455 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003456 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003457
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003458 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3459 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003460 return -1;
3461
Jason Ekstranda7af7042013-10-12 22:38:11 -05003462 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003463 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003464 wl_list_init(&ec->layer_list);
3465 wl_list_init(&ec->seat_list);
3466 wl_list_init(&ec->output_list);
3467 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003468 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003469 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003470 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003471 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003472 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003473
Xiong Zhang97116532013-10-23 13:58:31 +08003474 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003475 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003476
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003477 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3478 weston_config_section_get_string(s, "keymap_rules",
3479 (char **) &xkb_names.rules, NULL);
3480 weston_config_section_get_string(s, "keymap_model",
3481 (char **) &xkb_names.model, NULL);
3482 weston_config_section_get_string(s, "keymap_layout",
3483 (char **) &xkb_names.layout, NULL);
3484 weston_config_section_get_string(s, "keymap_variant",
3485 (char **) &xkb_names.variant, NULL);
3486 weston_config_section_get_string(s, "keymap_options",
3487 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003488
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003489 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3490 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003491
3492 ec->ping_handler = NULL;
3493
3494 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003495 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003496
3497 wl_data_device_manager_init(ec->wl_display);
3498
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003499 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003500
Daniel Stone725c2c32012-06-22 14:04:36 +01003501 loop = wl_display_get_event_loop(ec->wl_display);
3502 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3503 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3504
3505 ec->input_loop = wl_event_loop_create();
3506
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003507 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3508 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3509
3510 weston_compositor_schedule_repaint(ec);
3511
Daniel Stone725c2c32012-06-22 14:04:36 +01003512 return 0;
3513}
3514
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003515WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003516weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003517{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003518 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003519
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003520 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003521 if (ec->input_loop_source)
3522 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003523
Matt Roper361d2ad2011-08-29 13:52:23 -07003524 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003525 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003526 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003527
Daniel Stone325fc2d2012-05-30 16:31:58 +01003528 weston_binding_list_destroy_all(&ec->key_binding_list);
3529 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003530 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003531 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003532 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003533
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003534 weston_plane_release(&ec->primary_plane);
3535
Jonas Ådahlc97af922012-03-28 22:36:09 +02003536 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003537
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003538 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003539}
3540
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003541WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003542weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3543 const struct weston_pointer_grab_interface *interface)
3544{
3545 struct weston_seat *seat;
3546
3547 ec->default_pointer_grab = interface;
3548 wl_list_for_each(seat, &ec->seat_list, link) {
3549 if (seat->pointer) {
3550 weston_pointer_set_default_grab(seat->pointer,
3551 interface);
3552 }
3553 }
3554}
3555
3556WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003557weston_version(int *major, int *minor, int *micro)
3558{
3559 *major = WESTON_VERSION_MAJOR;
3560 *minor = WESTON_VERSION_MINOR;
3561 *micro = WESTON_VERSION_MICRO;
3562}
3563
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003564static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003565 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003566 const char *desc;
3567} capability_strings[] = {
3568 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003569 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003570};
3571
3572static void
3573weston_compositor_log_capabilities(struct weston_compositor *compositor)
3574{
3575 unsigned i;
3576 int yes;
3577
3578 weston_log("Compositor capabilities:\n");
3579 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3580 yes = compositor->capabilities & capability_strings[i].bit;
3581 weston_log_continue(STAMP_SPACE "%s %s\n",
3582 capability_strings[i].desc,
3583 yes ? "yes" : "no");
3584 }
3585}
3586
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003587static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003588{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003589 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003590
Martin Minarik6d118362012-06-07 18:01:59 +02003591 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003592 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003593
3594 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003595}
3596
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003597#ifdef HAVE_LIBUNWIND
3598
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003599static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003600print_backtrace(void)
3601{
3602 unw_cursor_t cursor;
3603 unw_context_t context;
3604 unw_word_t off;
3605 unw_proc_info_t pip;
3606 int ret, i = 0;
3607 char procname[256];
3608 const char *filename;
3609 Dl_info dlinfo;
3610
3611 pip.unwind_info = NULL;
3612 ret = unw_getcontext(&context);
3613 if (ret) {
3614 weston_log("unw_getcontext: %d\n", ret);
3615 return;
3616 }
3617
3618 ret = unw_init_local(&cursor, &context);
3619 if (ret) {
3620 weston_log("unw_init_local: %d\n", ret);
3621 return;
3622 }
3623
3624 ret = unw_step(&cursor);
3625 while (ret > 0) {
3626 ret = unw_get_proc_info(&cursor, &pip);
3627 if (ret) {
3628 weston_log("unw_get_proc_info: %d\n", ret);
3629 break;
3630 }
3631
3632 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3633 if (ret && ret != -UNW_ENOMEM) {
3634 if (ret != -UNW_EUNSPEC)
3635 weston_log("unw_get_proc_name: %d\n", ret);
3636 procname[0] = '?';
3637 procname[1] = 0;
3638 }
3639
3640 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3641 *dlinfo.dli_fname)
3642 filename = dlinfo.dli_fname;
3643 else
3644 filename = "?";
3645
3646 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3647 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3648
3649 ret = unw_step(&cursor);
3650 if (ret < 0)
3651 weston_log("unw_step: %d\n", ret);
3652 }
3653}
3654
3655#else
3656
3657static void
3658print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003659{
3660 void *buffer[32];
3661 int i, count;
3662 Dl_info info;
3663
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003664 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3665 for (i = 0; i < count; i++) {
3666 dladdr(buffer[i], &info);
3667 weston_log(" [%016lx] %s (%s)\n",
3668 (long) buffer[i],
3669 info.dli_sname ? info.dli_sname : "--",
3670 info.dli_fname);
3671 }
3672}
3673
3674#endif
3675
3676static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003677on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003678{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003679 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003680 * then call the backend restore function, which will switch
3681 * back to the vt we launched from or ungrab X etc and then
3682 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003683 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003684 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003685 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003686
Peter Maatmane5b42e42013-03-27 22:38:53 +01003687 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003688
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003689 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003690
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003691 segv_compositor->restore(segv_compositor);
3692
3693 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003694}
3695
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003696WL_EXPORT void *
3697weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003698{
3699 char path[PATH_MAX];
3700 void *module, *init;
3701
3702 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003703 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003704 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003705 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003706
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003707 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3708 if (module) {
3709 weston_log("Module '%s' already loaded\n", path);
3710 dlclose(module);
3711 return NULL;
3712 }
3713
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003714 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003715 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003716 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003717 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003718 return NULL;
3719 }
3720
3721 init = dlsym(module, entrypoint);
3722 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003723 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003724 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003725 return NULL;
3726 }
3727
3728 return init;
3729}
3730
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003731static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003732load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003733 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003734{
3735 const char *p, *end;
3736 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003737 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003738 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003739
3740 if (modules == NULL)
3741 return 0;
3742
3743 p = modules;
3744 while (*p) {
3745 end = strchrnul(p, ',');
3746 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003747 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003748 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003749 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003750 p = end;
3751 while (*p == ',')
3752 p++;
3753
3754 }
3755
3756 return 0;
3757}
3758
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003759static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003760 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3761
3762static const char xdg_wrong_message[] =
3763 "fatal: environment variable XDG_RUNTIME_DIR\n"
3764 "is set to \"%s\", which is not a directory.\n";
3765
3766static const char xdg_wrong_mode_message[] =
3767 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3768 "correctly. Unix access mode must be 0700 but is %o,\n"
3769 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003770 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003771
3772static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003773 "Refer to your distribution on how to get it, or\n"
3774 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3775 "on how to implement it.\n";
3776
Martin Minarik37032f82012-06-18 20:15:18 +02003777static void
3778verify_xdg_runtime_dir(void)
3779{
3780 char *dir = getenv("XDG_RUNTIME_DIR");
3781 struct stat s;
3782
3783 if (!dir) {
3784 weston_log(xdg_error_message);
3785 weston_log_continue(xdg_detail_message);
3786 exit(EXIT_FAILURE);
3787 }
3788
3789 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3790 weston_log(xdg_wrong_message, dir);
3791 weston_log_continue(xdg_detail_message);
3792 exit(EXIT_FAILURE);
3793 }
3794
3795 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3796 weston_log(xdg_wrong_mode_message,
3797 dir, s.st_mode & 0777, s.st_uid);
3798 weston_log_continue(xdg_detail_message);
3799 }
3800}
3801
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003802static int
3803usage(int error_code)
3804{
3805 fprintf(stderr,
3806 "Usage: weston [OPTIONS]\n\n"
3807 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3808 "Weston supports multiple backends, and depending on which backend is in use\n"
3809 "different options will be accepted.\n\n"
3810
3811
3812 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003813 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003814 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003815 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3816 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003817 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003818 " -S, --socket=NAME\tName of socket to listen on\n"
3819 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003820 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003821 " --log==FILE\t\tLog to the given file\n"
3822 " -h, --help\t\tThis help message\n\n");
3823
3824 fprintf(stderr,
3825 "Options for drm-backend.so:\n\n"
3826 " --connector=ID\tBring up only this connector\n"
3827 " --seat=SEAT\t\tThe seat that weston should run on\n"
3828 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003829 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003830 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3831
3832 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003833 "Options for fbdev-backend.so:\n\n"
3834 " --tty=TTY\t\tThe tty to use\n"
3835 " --device=DEVICE\tThe framebuffer device to use\n\n");
3836
3837 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003838 "Options for x11-backend.so:\n\n"
3839 " --width=WIDTH\t\tWidth of X window\n"
3840 " --height=HEIGHT\tHeight of X window\n"
3841 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003842 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003843 " --output-count=COUNT\tCreate multiple outputs\n"
3844 " --no-input\t\tDont create input devices\n\n");
3845
3846 fprintf(stderr,
3847 "Options for wayland-backend.so:\n\n"
3848 " --width=WIDTH\t\tWidth of Wayland surface\n"
3849 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003850 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003851 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003852 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003853 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003854 " --display=DISPLAY\tWayland display to connect to\n\n");
3855
Pekka Paalanene31e0532013-05-22 18:03:07 +03003856#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3857 fprintf(stderr,
3858 "Options for rpi-backend.so:\n\n"
3859 " --tty=TTY\t\tThe tty to use\n"
3860 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3861 " --transform=TR\tThe output transformation, TR is one of:\n"
3862 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003863 " --opaque-regions\tEnable support for opaque regions, can be "
3864 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003865 "\n");
3866#endif
3867
Hardeningc077a842013-07-08 00:51:35 +02003868#if defined(BUILD_RDP_COMPOSITOR)
3869 fprintf(stderr,
3870 "Options for rdp-backend.so:\n\n"
3871 " --width=WIDTH\t\tWidth of desktop\n"
3872 " --height=HEIGHT\tHeight of desktop\n"
3873 " --extra-modes=MODES\t\n"
3874 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3875 " --address=ADDR\tThe address to bind\n"
3876 " --port=PORT\tThe port to listen on\n"
3877 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3878 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3879 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3880 "\n");
3881#endif
3882
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003883 exit(error_code);
3884}
3885
Peter Maatmane5b42e42013-03-27 22:38:53 +01003886static void
3887catch_signals(void)
3888{
3889 struct sigaction action;
3890
3891 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3892 action.sa_sigaction = on_caught_signal;
3893 sigemptyset(&action.sa_mask);
3894 sigaction(SIGSEGV, &action, NULL);
3895 sigaction(SIGABRT, &action, NULL);
3896}
3897
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003898int main(int argc, char *argv[])
3899{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003900 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003901 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003902 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003903 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003904 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003905 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003906 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003907 int *argc, char *argv[],
3908 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003909 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003910 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003911 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003912 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003913 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003914 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003915 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003916 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003917 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003918 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003919 struct weston_config *config;
3920 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003921
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003922 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003923 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003924 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003925 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3926 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003927 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003928 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003929 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003930 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003931 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003932
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003933 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003934
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003935 if (help)
3936 usage(EXIT_SUCCESS);
3937
Scott Moreau12245142012-08-29 15:15:58 -06003938 if (version) {
3939 printf(PACKAGE_STRING "\n");
3940 return EXIT_SUCCESS;
3941 }
3942
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003943 weston_log_file_open(log);
3944
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003945 weston_log("%s\n"
3946 STAMP_SPACE "%s\n"
3947 STAMP_SPACE "Bug reports to: %s\n"
3948 STAMP_SPACE "Build: %s\n",
3949 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003950 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003951 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003952
Martin Minarik37032f82012-06-18 20:15:18 +02003953 verify_xdg_runtime_dir();
3954
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003955 display = wl_display_create();
3956
Tiago Vignatti2116b892011-08-08 05:52:59 -07003957 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003958 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3959 display);
3960 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3961 display);
3962 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3963 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003964
3965 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003966 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3967 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003968
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003969 config = weston_config_parse("weston.ini");
3970 if (config != NULL) {
3971 weston_log("Using config file '%s'\n",
3972 weston_config_get_full_path(config));
3973 } else {
3974 weston_log("Starting with no config file.\n");
3975 }
3976 section = weston_config_get_section(config, "core", NULL, NULL);
3977
3978 weston_config_section_get_string(section, "backend", &backend, NULL);
3979 if (option_backend) {
3980 backend = option_backend;
3981 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003982 if (!backend) {
3983 if (getenv("WAYLAND_DISPLAY"))
3984 backend = "wayland-backend.so";
3985 else if (getenv("DISPLAY"))
3986 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003987 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003988 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003989 }
3990
Jason Ekstranda985da42013-08-22 17:28:03 -05003991 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003992
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003993 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003994 if (!backend_init)
3995 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003996
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003997 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003998 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003999 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004000 exit(EXIT_FAILURE);
4001 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004002
Peter Maatmane5b42e42013-03-27 22:38:53 +01004003 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004004 segv_compositor = ec;
4005
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004006 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004007 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004008
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004009 setenv("WAYLAND_DISPLAY", socket_name, 1);
4010
Jason Ekstranda985da42013-08-22 17:28:03 -05004011 if (!shell)
4012 weston_config_section_get_string(section, "shell", &shell,
4013 "desktop-shell.so");
4014 if (load_modules(ec, shell, &argc, argv) < 0)
4015 goto out;
4016
Ossama Othmana50e6e42013-05-14 09:48:26 -07004017 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004018 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004019 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004020 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004021
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004022 for (i = 1; i < argc; i++)
4023 weston_log("fatal: unhandled option: %s\n", argv[i]);
4024 if (argc > 1) {
4025 ret = EXIT_FAILURE;
4026 goto out;
4027 }
4028
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004029 weston_compositor_log_capabilities(ec);
4030
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004031 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004032 weston_log("fatal: failed to add socket: %m\n");
4033 ret = EXIT_FAILURE;
4034 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004035 }
4036
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004037 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004038
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004039 wl_display_run(display);
4040
4041 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004042 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004043 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004044
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004045 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004046
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004047 for (i = ARRAY_LENGTH(signals); i;)
4048 wl_event_source_remove(signals[--i]);
4049
Daniel Stone855028f2012-05-01 20:37:10 +01004050 weston_compositor_xkb_destroy(ec);
4051
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004052 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004053 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004054
Martin Minarik19e6f262012-06-07 13:08:46 +02004055 weston_log_file_close();
4056
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004057 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004058}