blob: d273e3fab099fa5d2d3e03e704f0c177aa1c00ca [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);
Zhang, Xiong Y31236932013-12-13 22:10:57 +0200412
413 if (ev->surface->output_destroyed)
414 ev->surface->output_destroyed(ev->surface);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200415}
416
Jason Ekstranda7af7042013-10-12 22:38:11 -0500417WL_EXPORT struct weston_view *
418weston_view_create(struct weston_surface *surface)
419{
420 struct weston_view *view;
421
422 view = calloc(1, sizeof *view);
423 if (view == NULL)
424 return NULL;
425
426 view->surface = surface;
427
Jason Ekstranda7af7042013-10-12 22:38:11 -0500428 /* Assign to surface */
429 wl_list_insert(&surface->views, &view->surface_link);
430
431 wl_signal_init(&view->destroy_signal);
432 wl_list_init(&view->link);
433 wl_list_init(&view->layer_link);
434
Xiong Zhang97116532013-10-23 13:58:31 +0800435 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500436
437 pixman_region32_init(&view->clip);
438
439 view->alpha = 1.0;
440 pixman_region32_init(&view->transform.opaque);
441
442 wl_list_init(&view->geometry.transformation_list);
443 wl_list_insert(&view->geometry.transformation_list,
444 &view->transform.position.link);
445 weston_matrix_init(&view->transform.position.matrix);
446 wl_list_init(&view->geometry.child_list);
447 pixman_region32_init(&view->transform.boundingbox);
448 view->transform.dirty = 1;
449
450 view->output = NULL;
451
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200452 view->output_move_listener.notify = weston_view_output_move_handler;
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200453 view->output_destroy_listener.notify =
454 weston_view_output_destroy_handler;
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200455
Jason Ekstranda7af7042013-10-12 22:38:11 -0500456 return view;
457}
458
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500459WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500460weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500461{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500462 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400463
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200464 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400465 if (surface == NULL)
466 return NULL;
467
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500468 wl_signal_init(&surface->destroy_signal);
469
470 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500471
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500472 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200473 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400474
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100475 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
476 surface->buffer_viewport.scale = 1;
477 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400478 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100479 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200480
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400481 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500482 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300483 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400484
Jason Ekstranda7af7042013-10-12 22:38:11 -0500485 wl_list_init(&surface->views);
486
487 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500488
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300489 surface->pending.buffer_destroy_listener.notify =
490 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300491 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300492 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300493 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300494 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300495
Pekka Paalanene67858b2013-04-25 13:57:42 +0300496 wl_list_init(&surface->subsurface_list);
497 wl_list_init(&surface->subsurface_list_pending);
498
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400499 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500500}
501
Alex Wu8811bf92012-02-28 18:07:54 +0800502WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500503weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200504 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500505{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100506 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500507}
508
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400509WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500510weston_view_to_global_float(struct weston_view *view,
511 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200512{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500513 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200514 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
515
Jason Ekstranda7af7042013-10-12 22:38:11 -0500516 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200517
518 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200519 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700520 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200521 v.f[3]);
522 *x = 0;
523 *y = 0;
524 return;
525 }
526
527 *x = v.f[0] / v.f[3];
528 *y = v.f[1] / v.f[3];
529 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500530 *x = sx + view->geometry.x;
531 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200532 }
533}
534
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500535WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200536weston_transformed_coord(int width, int height,
537 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200538 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200539 float sx, float sy, float *bx, float *by)
540{
541 switch (transform) {
542 case WL_OUTPUT_TRANSFORM_NORMAL:
543 default:
544 *bx = sx;
545 *by = sy;
546 break;
547 case WL_OUTPUT_TRANSFORM_FLIPPED:
548 *bx = width - sx;
549 *by = sy;
550 break;
551 case WL_OUTPUT_TRANSFORM_90:
552 *bx = height - sy;
553 *by = sx;
554 break;
555 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
556 *bx = height - sy;
557 *by = width - sx;
558 break;
559 case WL_OUTPUT_TRANSFORM_180:
560 *bx = width - sx;
561 *by = height - sy;
562 break;
563 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
564 *bx = sx;
565 *by = height - sy;
566 break;
567 case WL_OUTPUT_TRANSFORM_270:
568 *bx = sy;
569 *by = width - sx;
570 break;
571 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
572 *bx = sy;
573 *by = sx;
574 break;
575 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200576
577 *bx *= scale;
578 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200579}
580
581WL_EXPORT pixman_box32_t
582weston_transformed_rect(int width, int height,
583 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200584 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585 pixman_box32_t rect)
586{
587 float x1, x2, y1, y2;
588
589 pixman_box32_t ret;
590
Alexander Larsson4ea95522013-05-22 14:41:37 +0200591 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200592 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200593 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200594 rect.x2, rect.y2, &x2, &y2);
595
596 if (x1 <= x2) {
597 ret.x1 = x1;
598 ret.x2 = x2;
599 } else {
600 ret.x1 = x2;
601 ret.x2 = x1;
602 }
603
604 if (y1 <= y2) {
605 ret.y1 = y1;
606 ret.y2 = y2;
607 } else {
608 ret.y1 = y2;
609 ret.y2 = y1;
610 }
611
612 return ret;
613}
614
615WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500616weston_transformed_region(int width, int height,
617 enum wl_output_transform transform,
618 int32_t scale,
619 pixman_region32_t *src, pixman_region32_t *dest)
620{
621 pixman_box32_t *src_rects, *dest_rects;
622 int nrects, i;
623
624 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
625 if (src != dest)
626 pixman_region32_copy(dest, src);
627 return;
628 }
629
630 src_rects = pixman_region32_rectangles(src, &nrects);
631 dest_rects = malloc(nrects * sizeof(*dest_rects));
632 if (!dest_rects)
633 return;
634
635 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
636 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
637 } else {
638 for (i = 0; i < nrects; i++) {
639 switch (transform) {
640 default:
641 case WL_OUTPUT_TRANSFORM_NORMAL:
642 dest_rects[i].x1 = src_rects[i].x1;
643 dest_rects[i].y1 = src_rects[i].y1;
644 dest_rects[i].x2 = src_rects[i].x2;
645 dest_rects[i].y2 = src_rects[i].y2;
646 break;
647 case WL_OUTPUT_TRANSFORM_90:
648 dest_rects[i].x1 = height - src_rects[i].y2;
649 dest_rects[i].y1 = src_rects[i].x1;
650 dest_rects[i].x2 = height - src_rects[i].y1;
651 dest_rects[i].y2 = src_rects[i].x2;
652 break;
653 case WL_OUTPUT_TRANSFORM_180:
654 dest_rects[i].x1 = width - src_rects[i].x2;
655 dest_rects[i].y1 = height - src_rects[i].y2;
656 dest_rects[i].x2 = width - src_rects[i].x1;
657 dest_rects[i].y2 = height - src_rects[i].y1;
658 break;
659 case WL_OUTPUT_TRANSFORM_270:
660 dest_rects[i].x1 = src_rects[i].y1;
661 dest_rects[i].y1 = width - src_rects[i].x2;
662 dest_rects[i].x2 = src_rects[i].y2;
663 dest_rects[i].y2 = width - src_rects[i].x1;
664 break;
665 case WL_OUTPUT_TRANSFORM_FLIPPED:
666 dest_rects[i].x1 = width - src_rects[i].x2;
667 dest_rects[i].y1 = src_rects[i].y1;
668 dest_rects[i].x2 = width - src_rects[i].x1;
669 dest_rects[i].y2 = src_rects[i].y2;
670 break;
671 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
672 dest_rects[i].x1 = height - src_rects[i].y2;
673 dest_rects[i].y1 = width - src_rects[i].x2;
674 dest_rects[i].x2 = height - src_rects[i].y1;
675 dest_rects[i].y2 = width - src_rects[i].x1;
676 break;
677 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
678 dest_rects[i].x1 = src_rects[i].x1;
679 dest_rects[i].y1 = height - src_rects[i].y2;
680 dest_rects[i].x2 = src_rects[i].x2;
681 dest_rects[i].y2 = height - src_rects[i].y1;
682 break;
683 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
684 dest_rects[i].x1 = src_rects[i].y1;
685 dest_rects[i].y1 = src_rects[i].x1;
686 dest_rects[i].x2 = src_rects[i].y2;
687 dest_rects[i].y2 = src_rects[i].x2;
688 break;
689 }
690 }
691 }
692
693 if (scale != 1) {
694 for (i = 0; i < nrects; i++) {
695 dest_rects[i].x1 *= scale;
696 dest_rects[i].x2 *= scale;
697 dest_rects[i].y1 *= scale;
698 dest_rects[i].y2 *= scale;
699 }
700 }
701
702 pixman_region32_clear(dest);
703 pixman_region32_init_rects(dest, dest_rects, nrects);
704 free(dest_rects);
705}
706
707WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200708weston_surface_to_buffer_float(struct weston_surface *surface,
709 float sx, float sy, float *bx, float *by)
710{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 weston_transformed_coord(surface->width,
712 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100713 surface->buffer_viewport.transform,
714 surface->buffer_viewport.scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200715 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200716}
717
Alexander Larsson4ea95522013-05-22 14:41:37 +0200718WL_EXPORT void
719weston_surface_to_buffer(struct weston_surface *surface,
720 int sx, int sy, int *bx, int *by)
721{
722 float bxf, byf;
723
Jason Ekstranda7af7042013-10-12 22:38:11 -0500724 weston_transformed_coord(surface->width,
725 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100726 surface->buffer_viewport.transform,
727 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200728 sx, sy, &bxf, &byf);
729 *bx = floorf(bxf);
730 *by = floorf(byf);
731}
732
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200733WL_EXPORT pixman_box32_t
734weston_surface_to_buffer_rect(struct weston_surface *surface,
735 pixman_box32_t rect)
736{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500737 return weston_transformed_rect(surface->width,
738 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100739 surface->buffer_viewport.transform,
740 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200741 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200742}
743
744WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500745weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400746 struct weston_plane *plane)
747{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500748 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400749 return;
750
Jason Ekstranda7af7042013-10-12 22:38:11 -0500751 weston_view_damage_below(view);
752 view->plane = plane;
753 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400754}
755
756WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500757weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200758{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500759 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200760
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500761 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500762 pixman_region32_subtract(&damage, &view->transform.boundingbox,
763 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800764 if (view->plane)
765 pixman_region32_union(&view->plane->damage,
766 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500767 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800768 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200769}
770
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400771static void
772weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
773{
774 uint32_t different = es->output_mask ^ mask;
775 uint32_t entered = mask & different;
776 uint32_t left = es->output_mask & different;
777 struct weston_output *output;
778 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500779 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400780
781 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500782 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400783 return;
784 if (different == 0)
785 return;
786
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500787 client = wl_resource_get_client(es->resource);
788
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400789 wl_list_for_each(output, &es->compositor->output_list, link) {
790 if (1 << output->id & different)
791 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500792 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400793 client);
794 if (resource == NULL)
795 continue;
796 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500797 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400798 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500799 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400800 }
801}
802
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200803
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400804static void
805weston_surface_assign_output(struct weston_surface *es)
806{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500807 struct weston_output *new_output;
808 struct weston_view *view;
809 pixman_region32_t region;
810 uint32_t max, area, mask;
811 pixman_box32_t *e;
812
813 new_output = NULL;
814 max = 0;
815 mask = 0;
816 pixman_region32_init(&region);
817 wl_list_for_each(view, &es->views, surface_link) {
818 if (!view->output)
819 continue;
820
821 pixman_region32_intersect(&region, &view->transform.boundingbox,
822 &view->output->region);
823
824 e = pixman_region32_extents(&region);
825 area = (e->x2 - e->x1) * (e->y2 - e->y1);
826
827 mask |= view->output_mask;
828
829 if (area >= max) {
830 new_output = view->output;
831 max = area;
832 }
833 }
834 pixman_region32_fini(&region);
835
836 es->output = new_output;
837 weston_surface_update_output_mask(es, mask);
838}
839
840static void
841weston_view_assign_output(struct weston_view *ev)
842{
843 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400844 struct weston_output *output, *new_output;
845 pixman_region32_t region;
846 uint32_t max, area, mask;
847 pixman_box32_t *e;
848
849 new_output = NULL;
850 max = 0;
851 mask = 0;
852 pixman_region32_init(&region);
853 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500854 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400855 &output->region);
856
857 e = pixman_region32_extents(&region);
858 area = (e->x2 - e->x1) * (e->y2 - e->y1);
859
860 if (area > 0)
861 mask |= 1 << output->id;
862
863 if (area >= max) {
864 new_output = output;
865 max = area;
866 }
867 }
868 pixman_region32_fini(&region);
869
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200870 if (ev->output_mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200871 wl_list_remove(&ev->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200872 wl_list_remove(&ev->output_destroy_listener.link);
873 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200874
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200875 if (mask != 0) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200876 wl_signal_add(&new_output->move_signal,
877 &ev->output_move_listener);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +0200878 wl_signal_add(&new_output->destroy_signal,
879 &ev->output_destroy_listener);
880 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200881
Jason Ekstranda7af7042013-10-12 22:38:11 -0500882 ev->output = new_output;
883 ev->output_mask = mask;
884
885 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400886}
887
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200888static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
890 int32_t width, int32_t height,
891 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200892{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200893 float min_x = HUGE_VALF, min_y = HUGE_VALF;
894 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200895 int32_t s[4][2] = {
896 { sx, sy },
897 { sx, sy + height },
898 { sx + width, sy },
899 { sx + width, sy + height }
900 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200901 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200902 int i;
903
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300904 if (width == 0 || height == 0) {
905 /* avoid rounding empty bbox to 1x1 */
906 pixman_region32_init(bbox);
907 return;
908 }
909
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200910 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200911 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500912 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200913 if (x < min_x)
914 min_x = x;
915 if (x > max_x)
916 max_x = x;
917 if (y < min_y)
918 min_y = y;
919 if (y > max_y)
920 max_y = y;
921 }
922
Pekka Paalanen219b9822012-02-08 15:38:37 +0200923 int_x = floorf(min_x);
924 int_y = floorf(min_y);
925 pixman_region32_init_rect(bbox, int_x, int_y,
926 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200927}
928
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200929static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500930weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200931{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200933
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200934 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500935 view->geometry.x = roundf(view->geometry.x);
936 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200937
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500938 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
940 view->transform.position.matrix.d[12] = view->geometry.x;
941 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500942
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500944
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945 view->transform.inverse = view->transform.position.matrix;
946 view->transform.inverse.d[12] = -view->geometry.x;
947 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 pixman_region32_init_rect(&view->transform.boundingbox,
950 view->geometry.x,
951 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600952 view->surface->width,
953 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 if (view->alpha == 1.0) {
956 pixman_region32_copy(&view->transform.opaque,
957 &view->surface->opaque);
958 pixman_region32_translate(&view->transform.opaque,
959 view->geometry.x,
960 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500961 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200962}
963
964static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500965weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200966{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967 struct weston_view *parent = view->geometry.parent;
968 struct weston_matrix *matrix = &view->transform.matrix;
969 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200970 struct weston_transform *tform;
971
Jason Ekstranda7af7042013-10-12 22:38:11 -0500972 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200973
974 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
976 view->transform.position.matrix.d[12] = view->geometry.x;
977 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200978
979 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200981 weston_matrix_multiply(matrix, &tform->matrix);
982
Pekka Paalanen483243f2013-03-08 14:56:50 +0200983 if (parent)
984 weston_matrix_multiply(matrix, &parent->transform.matrix);
985
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200986 if (weston_matrix_invert(inverse, matrix) < 0) {
987 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500988 weston_log("error: weston_view %p"
989 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200990 return -1;
991 }
992
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600993 view_compute_bbox(view, 0, 0,
994 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500996
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200997 return 0;
998}
999
1000WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001001weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001002{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001004
Jason Ekstranda7af7042013-10-12 22:38:11 -05001005 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001006 return;
1007
Pekka Paalanen483243f2013-03-08 14:56:50 +02001008 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001010
Jason Ekstranda7af7042013-10-12 22:38:11 -05001011 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001012
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001014
Jason Ekstranda7af7042013-10-12 22:38:11 -05001015 pixman_region32_fini(&view->transform.boundingbox);
1016 pixman_region32_fini(&view->transform.opaque);
1017 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001018
Pekka Paalanencd403622012-01-25 13:37:39 +02001019 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 if (view->geometry.transformation_list.next ==
1021 &view->transform.position.link &&
1022 view->geometry.transformation_list.prev ==
1023 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001024 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001025 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001026 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 if (weston_view_update_transform_enable(view) < 0)
1028 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001029 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001030
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001032
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001034
Jason Ekstranda7af7042013-10-12 22:38:11 -05001035 wl_signal_emit(&view->surface->compositor->transform_signal,
1036 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001037}
1038
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001039WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001041{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001043
1044 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 * The invariant: if view->geometry.dirty, then all views
1046 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001047 * Corollary: if not parent->geometry.dirty, then all ancestors
1048 * are not dirty.
1049 */
1050
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001052 return;
1053
Jason Ekstranda7af7042013-10-12 22:38:11 -05001054 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001055
Jason Ekstranda7af7042013-10-12 22:38:11 -05001056 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001057 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001059}
1060
1061WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062weston_view_to_global_fixed(struct weston_view *view,
1063 wl_fixed_t vx, wl_fixed_t vy,
1064 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001065{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001066 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001067
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_view_to_global_float(view,
1069 wl_fixed_to_double(vx),
1070 wl_fixed_to_double(vy),
1071 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001072 *x = wl_fixed_from_double(xf);
1073 *y = wl_fixed_from_double(yf);
1074}
1075
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001076WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077weston_view_from_global_float(struct weston_view *view,
1078 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001079{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001081 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1082
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001084
1085 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001086 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001088 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 *vx = 0;
1090 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001091 return;
1092 }
1093
Jason Ekstranda7af7042013-10-12 22:38:11 -05001094 *vx = v.f[0] / v.f[3];
1095 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001096 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097 *vx = x - view->geometry.x;
1098 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001099 }
1100}
1101
1102WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103weston_view_from_global_fixed(struct weston_view *view,
1104 wl_fixed_t x, wl_fixed_t y,
1105 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001106{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001108
Jason Ekstranda7af7042013-10-12 22:38:11 -05001109 weston_view_from_global_float(view,
1110 wl_fixed_to_double(x),
1111 wl_fixed_to_double(y),
1112 &vxf, &vyf);
1113 *vx = wl_fixed_from_double(vxf);
1114 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001115}
1116
1117WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118weston_view_from_global(struct weston_view *view,
1119 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001120{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001122
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1124 *vx = floorf(vxf);
1125 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001126}
1127
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001128WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001129weston_surface_schedule_repaint(struct weston_surface *surface)
1130{
1131 struct weston_output *output;
1132
1133 wl_list_for_each(output, &surface->compositor->output_list, link)
1134 if (surface->output_mask & (1 << output->id))
1135 weston_output_schedule_repaint(output);
1136}
1137
1138WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139weston_view_schedule_repaint(struct weston_view *view)
1140{
1141 struct weston_output *output;
1142
1143 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1144 if (view->output_mask & (1 << output->id))
1145 weston_output_schedule_repaint(output);
1146}
1147
1148WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001149weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001150{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001151 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 0, 0, surface->width,
1153 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001154
Kristian Høgsberg98238702012-08-03 16:29:12 -04001155 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001156}
1157
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001158WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001160{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001161 if (view->geometry.x == x && view->geometry.y == y)
1162 return;
1163
Jason Ekstranda7af7042013-10-12 22:38:11 -05001164 view->geometry.x = x;
1165 view->geometry.y = y;
1166 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001167}
1168
Pekka Paalanen483243f2013-03-08 14:56:50 +02001169static void
1170transform_parent_handle_parent_destroy(struct wl_listener *listener,
1171 void *data)
1172{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001173 struct weston_view *view =
1174 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001175 geometry.parent_destroy_listener);
1176
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001178}
1179
1180WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181weston_view_set_transform_parent(struct weston_view *view,
1182 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001183{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 if (view->geometry.parent) {
1185 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1186 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001187 }
1188
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001192 transform_parent_handle_parent_destroy;
1193 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001194 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001196 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001197 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001198 }
1199
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200 weston_view_geometry_dirty(view);
1201}
1202
1203WL_EXPORT int
1204weston_view_is_mapped(struct weston_view *view)
1205{
1206 if (view->output)
1207 return 1;
1208 else
1209 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001210}
1211
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001212WL_EXPORT int
1213weston_surface_is_mapped(struct weston_surface *surface)
1214{
1215 if (surface->output)
1216 return 1;
1217 else
1218 return 0;
1219}
1220
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001221static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001222surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001223{
1224 struct weston_view *view;
1225
1226 if (surface->width == width && surface->height == height)
1227 return;
1228
1229 surface->width = width;
1230 surface->height = height;
1231
1232 wl_list_for_each(view, &surface->views, surface_link)
1233 weston_view_geometry_dirty(view);
1234}
1235
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001236WL_EXPORT void
1237weston_surface_set_size(struct weston_surface *surface,
1238 int32_t width, int32_t height)
1239{
1240 assert(!surface->resource);
1241 surface_set_size(surface, width, height);
1242}
1243
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001244static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001245weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001246{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001247 int32_t width, height;
1248
1249 if (!surface->buffer_ref.buffer) {
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001250 surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001251 return;
1252 }
1253
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001254 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001255 case WL_OUTPUT_TRANSFORM_90:
1256 case WL_OUTPUT_TRANSFORM_270:
1257 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1258 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001259 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001260 height = surface->buffer_ref.buffer->width;
1261 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001262 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001263 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001264 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001265 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001266 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001267
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001268 width = width / surface->buffer_viewport.scale;
1269 height = height / surface->buffer_viewport.scale;
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001270 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001271}
1272
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001273WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001274weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001275{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001276 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001277
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001278 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001279
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001280 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001281}
1282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283WL_EXPORT struct weston_view *
1284weston_compositor_pick_view(struct weston_compositor *compositor,
1285 wl_fixed_t x, wl_fixed_t y,
1286 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001287{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001289
Jason Ekstranda7af7042013-10-12 22:38:11 -05001290 wl_list_for_each(view, &compositor->view_list, link) {
1291 weston_view_from_global_fixed(view, x, y, vx, vy);
1292 if (pixman_region32_contains_point(&view->surface->input,
1293 wl_fixed_to_int(*vx),
1294 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001295 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001296 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001297 }
1298
1299 return NULL;
1300}
1301
1302static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001303weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001304{
Daniel Stone37816df2012-05-16 18:45:18 +01001305 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001306
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001307 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001308 return;
1309
Daniel Stone37816df2012-05-16 18:45:18 +01001310 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001311 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001312}
1313
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001314WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001316{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001317 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001318
Jason Ekstranda7af7042013-10-12 22:38:11 -05001319 if (!weston_view_is_mapped(view))
1320 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 weston_view_damage_below(view);
1323 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001324 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001325 wl_list_remove(&view->layer_link);
1326 wl_list_init(&view->layer_link);
1327 wl_list_remove(&view->link);
1328 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001329 wl_list_remove(&view->output_move_listener.link);
1330 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001331 wl_list_remove(&view->output_destroy_listener.link);
1332 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001333 view->output_mask = 0;
1334 weston_surface_assign_output(view->surface);
1335
1336 if (weston_surface_is_mapped(view->surface))
1337 return;
1338
1339 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1340 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001341 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001342 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001343 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001344 NULL,
1345 wl_fixed_from_int(0),
1346 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001348 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001349 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001350}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001351
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352WL_EXPORT void
1353weston_surface_unmap(struct weston_surface *surface)
1354{
1355 struct weston_view *view;
1356
1357 wl_list_for_each(view, &surface->views, surface_link)
1358 weston_view_unmap(view);
1359 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001360}
1361
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001362struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001363 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001364 struct wl_list link;
1365};
1366
Jason Ekstranda7af7042013-10-12 22:38:11 -05001367WL_EXPORT void
1368weston_view_destroy(struct weston_view *view)
1369{
1370 wl_signal_emit(&view->destroy_signal, view);
1371
1372 assert(wl_list_empty(&view->geometry.child_list));
1373
1374 if (weston_view_is_mapped(view)) {
1375 weston_view_unmap(view);
1376 weston_compositor_build_view_list(view->surface->compositor);
1377 }
1378
1379 wl_list_remove(&view->link);
1380 wl_list_remove(&view->layer_link);
1381
1382 pixman_region32_fini(&view->clip);
1383 pixman_region32_fini(&view->transform.boundingbox);
1384
1385 weston_view_set_transform_parent(view, NULL);
1386
Jason Ekstranda7af7042013-10-12 22:38:11 -05001387 wl_list_remove(&view->surface_link);
1388
1389 free(view);
1390}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001391
1392WL_EXPORT void
1393weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001394{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001395 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001397
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001398 if (--surface->ref_count > 0)
1399 return;
1400
1401 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1402
Pekka Paalanene67858b2013-04-25 13:57:42 +03001403 assert(wl_list_empty(&surface->subsurface_list_pending));
1404 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001405
Jason Ekstranda7af7042013-10-12 22:38:11 -05001406 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1407 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001408
Pekka Paalanenbc106382012-10-10 12:49:31 +03001409 wl_list_for_each_safe(cb, next,
1410 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001411 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001412
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001413 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001414 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001415 pixman_region32_fini(&surface->pending.damage);
1416
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001417 if (surface->pending.buffer)
1418 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1419
Pekka Paalanende685b82012-12-04 15:58:12 +02001420 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001421
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001422 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001423 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001424 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001425
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001426 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001427 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001428
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001429 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001430}
1431
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001432static void
1433destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001434{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001435 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001436
Giulio Camuffo0d379742013-11-15 22:06:15 +01001437 /* Set the resource to NULL, since we don't want to leave a
1438 * dangling pointer if the surface was refcounted and survives
1439 * the weston_surface_destroy() call. */
1440 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001441 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001442}
1443
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001444static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001445weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1446{
1447 struct weston_buffer *buffer =
1448 container_of(listener, struct weston_buffer, destroy_listener);
1449
1450 wl_signal_emit(&buffer->destroy_signal, buffer);
1451 free(buffer);
1452}
1453
1454struct weston_buffer *
1455weston_buffer_from_resource(struct wl_resource *resource)
1456{
1457 struct weston_buffer *buffer;
1458 struct wl_listener *listener;
1459
1460 listener = wl_resource_get_destroy_listener(resource,
1461 weston_buffer_destroy_handler);
1462
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001463 if (listener)
1464 return container_of(listener, struct weston_buffer,
1465 destroy_listener);
1466
1467 buffer = zalloc(sizeof *buffer);
1468 if (buffer == NULL)
1469 return NULL;
1470
1471 buffer->resource = resource;
1472 wl_signal_init(&buffer->destroy_signal);
1473 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001474 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001475 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001476
1477 return buffer;
1478}
1479
1480static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001481weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1482 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001483{
Pekka Paalanende685b82012-12-04 15:58:12 +02001484 struct weston_buffer_reference *ref =
1485 container_of(listener, struct weston_buffer_reference,
1486 destroy_listener);
1487
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001488 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001489 ref->buffer = NULL;
1490}
1491
1492WL_EXPORT void
1493weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001494 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001495{
1496 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001497 ref->buffer->busy_count--;
1498 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001499 assert(wl_resource_get_client(ref->buffer->resource));
1500 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001501 WL_BUFFER_RELEASE);
1502 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001503 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001504 }
1505
Pekka Paalanende685b82012-12-04 15:58:12 +02001506 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001507 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001508 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001509 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001510 }
1511
Pekka Paalanende685b82012-12-04 15:58:12 +02001512 ref->buffer = buffer;
1513 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1514}
1515
1516static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001517weston_surface_attach(struct weston_surface *surface,
1518 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001519{
1520 weston_buffer_reference(&surface->buffer_ref, buffer);
1521
Pekka Paalanena6421c42012-12-04 15:58:10 +02001522 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001523 if (weston_surface_is_mapped(surface))
1524 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001525 }
1526
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001527 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001528}
1529
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001530WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001531weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001532{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001533 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001534
1535 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001536 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001537}
1538
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001539WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001540weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001541{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001542 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001543
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001544 pixman_region32_union(&compositor->primary_plane.damage,
1545 &compositor->primary_plane.damage,
1546 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001547 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001548}
1549
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001550static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001551surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001552{
Pekka Paalanende685b82012-12-04 15:58:12 +02001553 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001554 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001555 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 empty_region(&surface->damage);
1558}
1559
1560static void
1561view_accumulate_damage(struct weston_view *view,
1562 pixman_region32_t *opaque)
1563{
1564 pixman_region32_t damage;
1565
1566 pixman_region32_init(&damage);
1567 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001568 pixman_box32_t *extents;
1569
Jason Ekstranda7af7042013-10-12 22:38:11 -05001570 extents = pixman_region32_extents(&view->surface->damage);
1571 view_compute_bbox(view, extents->x1, extents->y1,
1572 extents->x2 - extents->x1,
1573 extents->y2 - extents->y1,
1574 &damage);
1575 pixman_region32_translate(&damage,
1576 -view->plane->x,
1577 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001578 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579 pixman_region32_copy(&damage, &view->surface->damage);
1580 pixman_region32_translate(&damage,
1581 view->geometry.x - view->plane->x,
1582 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001583 }
1584
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585 pixman_region32_subtract(&damage, &damage, opaque);
1586 pixman_region32_union(&view->plane->damage,
1587 &view->plane->damage, &damage);
1588 pixman_region32_fini(&damage);
1589 pixman_region32_copy(&view->clip, opaque);
1590 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001591}
1592
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001593static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001594compositor_accumulate_damage(struct weston_compositor *ec)
1595{
1596 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001598 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001599
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001600 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001601
1602 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001603 pixman_region32_copy(&plane->clip, &clip);
1604
1605 pixman_region32_init(&opaque);
1606
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607 wl_list_for_each(ev, &ec->view_list, link) {
1608 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001609 continue;
1610
Jason Ekstranda7af7042013-10-12 22:38:11 -05001611 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001612 }
1613
1614 pixman_region32_union(&clip, &clip, &opaque);
1615 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001616 }
1617
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001618 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001619
Jason Ekstranda7af7042013-10-12 22:38:11 -05001620 wl_list_for_each(ev, &ec->view_list, link)
1621 ev->surface->touched = 0;
1622
1623 wl_list_for_each(ev, &ec->view_list, link) {
1624 if (ev->surface->touched)
1625 continue;
1626 ev->surface->touched = 1;
1627
1628 surface_flush_damage(ev->surface);
1629
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001630 /* Both the renderer and the backend have seen the buffer
1631 * by now. If renderer needs the buffer, it has its own
1632 * reference set. If the backend wants to keep the buffer
1633 * around for migrating the surface into a non-primary plane
1634 * later, keep_buffer is true. Otherwise, drop the core
1635 * reference now, and allow early buffer release. This enables
1636 * clients to use single-buffering.
1637 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 if (!ev->surface->keep_buffer)
1639 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001640 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001641}
1642
1643static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001644surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001645{
1646 struct weston_subsurface *sub;
1647
Pekka Paalanene67858b2013-04-25 13:57:42 +03001648 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001650 continue;
1651
Jason Ekstranda7af7042013-10-12 22:38:11 -05001652 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1653 wl_list_init(&sub->surface->views);
1654
1655 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001656 }
1657}
1658
1659static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001661{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001662 struct weston_subsurface *sub;
1663 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001664
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1666 if (sub->surface == surface)
1667 continue;
1668
1669 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1670 weston_view_destroy(view);
1671
1672 surface_free_unused_subsurface_views(sub->surface);
1673 }
1674}
1675
1676static void
1677view_list_add_subsurface_view(struct weston_compositor *compositor,
1678 struct weston_subsurface *sub,
1679 struct weston_view *parent)
1680{
1681 struct weston_subsurface *child;
1682 struct weston_view *view = NULL, *iv;
1683
1684 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1685 if (iv->geometry.parent == parent) {
1686 view = iv;
1687 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001688 }
1689 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001690
1691 if (view) {
1692 /* Put it back in the surface's list of views */
1693 wl_list_remove(&view->surface_link);
1694 wl_list_insert(&sub->surface->views, &view->surface_link);
1695 } else {
1696 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001697 weston_view_set_position(view,
1698 sub->position.x,
1699 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 weston_view_set_transform_parent(view, parent);
1701 }
1702
1703 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001704
Pekka Paalanenb188e912013-11-19 14:03:35 +02001705 if (wl_list_empty(&sub->surface->subsurface_list)) {
1706 wl_list_insert(compositor->view_list.prev, &view->link);
1707 return;
1708 }
1709
1710 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1711 if (child->surface == sub->surface)
1712 wl_list_insert(compositor->view_list.prev, &view->link);
1713 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001714 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001715 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716}
1717
1718static void
1719view_list_add(struct weston_compositor *compositor,
1720 struct weston_view *view)
1721{
1722 struct weston_subsurface *sub;
1723
1724 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001725
Pekka Paalanenb188e912013-11-19 14:03:35 +02001726 if (wl_list_empty(&view->surface->subsurface_list)) {
1727 wl_list_insert(compositor->view_list.prev, &view->link);
1728 return;
1729 }
1730
1731 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1732 if (sub->surface == view->surface)
1733 wl_list_insert(compositor->view_list.prev, &view->link);
1734 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001735 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001736 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737}
1738
1739static void
1740weston_compositor_build_view_list(struct weston_compositor *compositor)
1741{
1742 struct weston_view *view;
1743 struct weston_layer *layer;
1744
1745 wl_list_for_each(layer, &compositor->layer_list, link)
1746 wl_list_for_each(view, &layer->view_list, layer_link)
1747 surface_stash_subsurface_views(view->surface);
1748
1749 wl_list_init(&compositor->view_list);
1750 wl_list_for_each(layer, &compositor->layer_list, link) {
1751 wl_list_for_each(view, &layer->view_list, layer_link) {
1752 view_list_add(compositor, view);
1753 }
1754 }
1755
1756 wl_list_for_each(layer, &compositor->layer_list, link)
1757 wl_list_for_each(view, &layer->view_list, layer_link)
1758 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001759}
1760
David Herrmann1edf44c2013-10-22 17:11:26 +02001761static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001762weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001763{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001765 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001766 struct weston_animation *animation, *next;
1767 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001768 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001769 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001770 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001771
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001772 if (output->destroying)
1773 return 0;
1774
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001775 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001776 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001777
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001778 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001779 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001780 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001781 wl_list_for_each(ev, &ec->view_list, link)
1782 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001783
Pekka Paalanene67858b2013-04-25 13:57:42 +03001784 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 wl_list_for_each(ev, &ec->view_list, link) {
1786 /* Note: This operation is safe to do multiple times on the
1787 * same surface.
1788 */
1789 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001790 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791 &ev->surface->frame_callback_list);
1792 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001793 }
1794 }
1795
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001796 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001797
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001798 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001799 pixman_region32_intersect(&output_damage,
1800 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001801 pixman_region32_subtract(&output_damage,
1802 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001803
Scott Moreauccbf29d2012-02-22 14:21:41 -07001804 if (output->dirty)
1805 weston_output_update_matrix(output);
1806
David Herrmann1edf44c2013-10-22 17:11:26 +02001807 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001808
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001809 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001810
Kristian Høgsbergef044142011-06-21 15:02:12 -04001811 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001812
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001813 weston_compositor_repick(ec);
1814 wl_event_loop_dispatch(ec->input_loop, 0);
1815
Jonas Ådahldb773762012-06-13 00:01:21 +02001816 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001817 wl_callback_send_done(cb->resource, msecs);
1818 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001819 }
1820
Scott Moreaud64cf212012-06-08 19:40:54 -06001821 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001822 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001823 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001824 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001825
1826 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001827}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001828
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001829static int
1830weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001831{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001832 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001833
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001834 wl_event_loop_dispatch(compositor->input_loop, 0);
1835
1836 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001837}
1838
1839WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001840weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001841{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001842 struct weston_compositor *compositor = output->compositor;
1843 struct wl_event_loop *loop =
1844 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001845 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001846
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001847 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001848
1849 if (output->repaint_needed &&
1850 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1851 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001852 r = weston_output_repaint(output, msecs);
1853 if (!r)
1854 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001855 }
1856
1857 output->repaint_scheduled = 0;
1858 if (compositor->input_loop_source)
1859 return;
1860
1861 fd = wl_event_loop_get_fd(compositor->input_loop);
1862 compositor->input_loop_source =
1863 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1864 weston_compositor_read_input, compositor);
1865}
1866
1867static void
1868idle_repaint(void *data)
1869{
1870 struct weston_output *output = data;
1871
Jonas Ådahle5a12252013-04-05 23:07:11 +02001872 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001873}
1874
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001875WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001876weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1877{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001878 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001879 if (below != NULL)
1880 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001881}
1882
1883WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001884weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001885{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001886 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001887 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001888
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001889 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1890 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001891 return;
1892
Kristian Høgsbergef044142011-06-21 15:02:12 -04001893 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001894 output->repaint_needed = 1;
1895 if (output->repaint_scheduled)
1896 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001897
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001898 wl_event_loop_add_idle(loop, idle_repaint, output);
1899 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001900
1901 if (compositor->input_loop_source) {
1902 wl_event_source_remove(compositor->input_loop_source);
1903 compositor->input_loop_source = NULL;
1904 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001905}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001906
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001907WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001908weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1909{
1910 struct weston_output *output;
1911
1912 wl_list_for_each(output, &compositor->output_list, link)
1913 weston_output_schedule_repaint(output);
1914}
1915
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001916static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001917surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001918{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001919 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001920}
1921
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001922static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001923surface_attach(struct wl_client *client,
1924 struct wl_resource *resource,
1925 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1926{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001927 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001928 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001929
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001930 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001931 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001932 if (buffer == NULL) {
1933 wl_client_post_no_memory(client);
1934 return;
1935 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001936 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001937
Pekka Paalanende685b82012-12-04 15:58:12 +02001938 /* Attach, attach, without commit in between does not send
1939 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001940 if (surface->pending.buffer)
1941 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001942
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001943 surface->pending.sx = sx;
1944 surface->pending.sy = sy;
1945 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001946 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001947 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001948 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001949 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001950 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001951}
1952
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001953static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001954surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001955 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001956 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001957{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001958 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001959
Pekka Paalanen8e159182012-10-10 12:49:25 +03001960 pixman_region32_union_rect(&surface->pending.damage,
1961 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001962 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001963}
1964
Kristian Høgsberg33418202011-08-16 23:01:28 -04001965static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001966destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001967{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001968 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001969
1970 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001971 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001972}
1973
1974static void
1975surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001976 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001977{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001978 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001979 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001980
1981 cb = malloc(sizeof *cb);
1982 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001983 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001984 return;
1985 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001986
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001987 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1988 callback);
1989 if (cb->resource == NULL) {
1990 free(cb);
1991 wl_resource_post_no_memory(resource);
1992 return;
1993 }
1994
Jason Ekstranda85118c2013-06-27 20:17:02 -05001995 wl_resource_set_implementation(cb->resource, NULL, cb,
1996 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001997
Pekka Paalanenbc106382012-10-10 12:49:31 +03001998 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001999}
2000
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002001static void
2002surface_set_opaque_region(struct wl_client *client,
2003 struct wl_resource *resource,
2004 struct wl_resource *region_resource)
2005{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002006 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002007 struct weston_region *region;
2008
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002009 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002010 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002011 pixman_region32_copy(&surface->pending.opaque,
2012 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002013 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002014 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002015 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002016}
2017
2018static void
2019surface_set_input_region(struct wl_client *client,
2020 struct wl_resource *resource,
2021 struct wl_resource *region_resource)
2022{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002023 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002024 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002025
2026 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002027 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002028 pixman_region32_copy(&surface->pending.input,
2029 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002030 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002031 pixman_region32_fini(&surface->pending.input);
2032 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002033 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002034}
2035
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002036static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002037weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002038{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002039 struct weston_subsurface *sub;
2040
2041 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2042 parent_link_pending) {
2043 wl_list_remove(&sub->parent_link);
2044 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2045 }
2046}
2047
2048static void
2049weston_surface_commit(struct weston_surface *surface)
2050{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002051 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002052 pixman_region32_t opaque;
2053
Alexander Larsson4ea95522013-05-22 14:41:37 +02002054 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002055 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002056 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002057
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002058 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01002059 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002060 weston_surface_attach(surface, surface->pending.buffer);
2061
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002062 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01002063
2064 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002065 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002066 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002067
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002068 if (surface->pending.buffer)
2069 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2070 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002071 surface->pending.sx = 0;
2072 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002073 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002074
2075 /* wl_surface.damage */
2076 pixman_region32_union(&surface->damage, &surface->damage,
2077 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002078 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2079 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002080 surface->width,
2081 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002082 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002083
2084 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002085 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 surface->width,
2087 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002088 pixman_region32_intersect(&opaque,
2089 &opaque, &surface->pending.opaque);
2090
2091 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2092 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002093 wl_list_for_each(view, &surface->views, surface_link)
2094 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002095 }
2096
2097 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002098
2099 /* wl_surface.set_input_region */
2100 pixman_region32_fini(&surface->input);
2101 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002102 surface->width,
2103 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002104 pixman_region32_intersect(&surface->input,
2105 &surface->input, &surface->pending.input);
2106
Pekka Paalanenbc106382012-10-10 12:49:31 +03002107 /* wl_surface.frame */
2108 wl_list_insert_list(&surface->frame_callback_list,
2109 &surface->pending.frame_callback_list);
2110 wl_list_init(&surface->pending.frame_callback_list);
2111
Pekka Paalanene67858b2013-04-25 13:57:42 +03002112 weston_surface_commit_subsurface_order(surface);
2113
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002114 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002115}
2116
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002117static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002118weston_subsurface_commit(struct weston_subsurface *sub);
2119
2120static void
2121weston_subsurface_parent_commit(struct weston_subsurface *sub,
2122 int parent_is_synchronized);
2123
2124static void
2125surface_commit(struct wl_client *client, struct wl_resource *resource)
2126{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002127 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002128 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2129
2130 if (sub) {
2131 weston_subsurface_commit(sub);
2132 return;
2133 }
2134
2135 weston_surface_commit(surface);
2136
2137 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2138 if (sub->surface != surface)
2139 weston_subsurface_parent_commit(sub, 0);
2140 }
2141}
2142
2143static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002144surface_set_buffer_transform(struct wl_client *client,
2145 struct wl_resource *resource, int transform)
2146{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002147 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002148
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002149 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002150}
2151
Alexander Larsson4ea95522013-05-22 14:41:37 +02002152static void
2153surface_set_buffer_scale(struct wl_client *client,
2154 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002155 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002156{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002157 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002158
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002159 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002160}
2161
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002162static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002163 surface_destroy,
2164 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002165 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002166 surface_frame,
2167 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002168 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002169 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002170 surface_set_buffer_transform,
2171 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002172};
2173
2174static void
2175compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002176 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002177{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002178 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002179 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002180
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002181 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002182 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002183 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002184 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002185 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002186
Jason Ekstranda85118c2013-06-27 20:17:02 -05002187 surface->resource =
2188 wl_resource_create(client, &wl_surface_interface,
2189 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002190 if (surface->resource == NULL) {
2191 weston_surface_destroy(surface);
2192 wl_resource_post_no_memory(resource);
2193 return;
2194 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002195 wl_resource_set_implementation(surface->resource, &surface_interface,
2196 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002197}
2198
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002199static void
2200destroy_region(struct wl_resource *resource)
2201{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002202 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002203
2204 pixman_region32_fini(&region->region);
2205 free(region);
2206}
2207
2208static void
2209region_destroy(struct wl_client *client, struct wl_resource *resource)
2210{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002211 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002212}
2213
2214static void
2215region_add(struct wl_client *client, struct wl_resource *resource,
2216 int32_t x, int32_t y, int32_t width, int32_t height)
2217{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002218 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002219
2220 pixman_region32_union_rect(&region->region, &region->region,
2221 x, y, width, height);
2222}
2223
2224static void
2225region_subtract(struct wl_client *client, struct wl_resource *resource,
2226 int32_t x, int32_t y, int32_t width, int32_t height)
2227{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002228 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002229 pixman_region32_t rect;
2230
2231 pixman_region32_init_rect(&rect, x, y, width, height);
2232 pixman_region32_subtract(&region->region, &region->region, &rect);
2233 pixman_region32_fini(&rect);
2234}
2235
2236static const struct wl_region_interface region_interface = {
2237 region_destroy,
2238 region_add,
2239 region_subtract
2240};
2241
2242static void
2243compositor_create_region(struct wl_client *client,
2244 struct wl_resource *resource, uint32_t id)
2245{
2246 struct weston_region *region;
2247
2248 region = malloc(sizeof *region);
2249 if (region == NULL) {
2250 wl_resource_post_no_memory(resource);
2251 return;
2252 }
2253
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002254 pixman_region32_init(&region->region);
2255
Jason Ekstranda85118c2013-06-27 20:17:02 -05002256 region->resource =
2257 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002258 if (region->resource == NULL) {
2259 free(region);
2260 wl_resource_post_no_memory(resource);
2261 return;
2262 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002263 wl_resource_set_implementation(region->resource, &region_interface,
2264 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002265}
2266
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002267static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002268 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002269 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002270};
2271
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002272static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002273weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2274{
2275 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002276 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002277 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002278
Alexander Larsson4ea95522013-05-22 14:41:37 +02002279 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002280 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002281 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002282
Pekka Paalanene67858b2013-04-25 13:57:42 +03002283 /* wl_surface.attach */
2284 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2285 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2286 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2287
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002288 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002289
2290 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002291 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002292 sub->cached.sx = 0;
2293 sub->cached.sy = 0;
2294 sub->cached.newly_attached = 0;
2295
2296 /* wl_surface.damage */
2297 pixman_region32_union(&surface->damage, &surface->damage,
2298 &sub->cached.damage);
2299 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2300 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002301 surface->width,
2302 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002303 empty_region(&sub->cached.damage);
2304
2305 /* wl_surface.set_opaque_region */
2306 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 surface->width,
2308 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002309 pixman_region32_intersect(&opaque,
2310 &opaque, &sub->cached.opaque);
2311
2312 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2313 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002314 wl_list_for_each(view, &surface->views, surface_link)
2315 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002316 }
2317
2318 pixman_region32_fini(&opaque);
2319
2320 /* wl_surface.set_input_region */
2321 pixman_region32_fini(&surface->input);
2322 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002323 surface->width,
2324 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002325 pixman_region32_intersect(&surface->input,
2326 &surface->input, &sub->cached.input);
2327
2328 /* wl_surface.frame */
2329 wl_list_insert_list(&surface->frame_callback_list,
2330 &sub->cached.frame_callback_list);
2331 wl_list_init(&sub->cached.frame_callback_list);
2332
2333 weston_surface_commit_subsurface_order(surface);
2334
2335 weston_surface_schedule_repaint(surface);
2336
2337 sub->cached.has_data = 0;
2338}
2339
2340static void
2341weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2342{
2343 struct weston_surface *surface = sub->surface;
2344
2345 /*
2346 * If this commit would cause the surface to move by the
2347 * attach(dx, dy) parameters, the old damage region must be
2348 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002349 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002350 */
2351 pixman_region32_translate(&sub->cached.damage,
2352 -surface->pending.sx, -surface->pending.sy);
2353 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2354 &surface->pending.damage);
2355 empty_region(&surface->pending.damage);
2356
2357 if (surface->pending.newly_attached) {
2358 sub->cached.newly_attached = 1;
2359 weston_buffer_reference(&sub->cached.buffer_ref,
2360 surface->pending.buffer);
2361 }
2362 sub->cached.sx += surface->pending.sx;
2363 sub->cached.sy += surface->pending.sy;
2364 surface->pending.sx = 0;
2365 surface->pending.sy = 0;
2366 surface->pending.newly_attached = 0;
2367
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002368 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002369
2370 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2371
2372 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2373
2374 wl_list_insert_list(&sub->cached.frame_callback_list,
2375 &surface->pending.frame_callback_list);
2376 wl_list_init(&surface->pending.frame_callback_list);
2377
2378 sub->cached.has_data = 1;
2379}
2380
2381static int
2382weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2383{
2384 while (sub) {
2385 if (sub->synchronized)
2386 return 1;
2387
2388 if (!sub->parent)
2389 return 0;
2390
2391 sub = weston_surface_to_subsurface(sub->parent);
2392 }
2393
2394 return 0;
2395}
2396
2397static void
2398weston_subsurface_commit(struct weston_subsurface *sub)
2399{
2400 struct weston_surface *surface = sub->surface;
2401 struct weston_subsurface *tmp;
2402
2403 /* Recursive check for effectively synchronized. */
2404 if (weston_subsurface_is_synchronized(sub)) {
2405 weston_subsurface_commit_to_cache(sub);
2406 } else {
2407 if (sub->cached.has_data) {
2408 /* flush accumulated state from cache */
2409 weston_subsurface_commit_to_cache(sub);
2410 weston_subsurface_commit_from_cache(sub);
2411 } else {
2412 weston_surface_commit(surface);
2413 }
2414
2415 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2416 if (tmp->surface != surface)
2417 weston_subsurface_parent_commit(tmp, 0);
2418 }
2419 }
2420}
2421
2422static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002423weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002424{
2425 struct weston_surface *surface = sub->surface;
2426 struct weston_subsurface *tmp;
2427
Pekka Paalanene67858b2013-04-25 13:57:42 +03002428 /* From now on, commit_from_cache the whole sub-tree, regardless of
2429 * the synchronized mode of each child. This sub-surface or some
2430 * of its ancestors were synchronized, so we are synchronized
2431 * all the way down.
2432 */
2433
2434 if (sub->cached.has_data)
2435 weston_subsurface_commit_from_cache(sub);
2436
2437 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2438 if (tmp->surface != surface)
2439 weston_subsurface_parent_commit(tmp, 1);
2440 }
2441}
2442
2443static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002444weston_subsurface_parent_commit(struct weston_subsurface *sub,
2445 int parent_is_synchronized)
2446{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002447 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002448 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002449 wl_list_for_each(view, &sub->surface->views, surface_link)
2450 weston_view_set_position(view,
2451 sub->position.x,
2452 sub->position.y);
2453
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002454 sub->position.set = 0;
2455 }
2456
2457 if (parent_is_synchronized || sub->synchronized)
2458 weston_subsurface_synchronized_commit(sub);
2459}
2460
2461static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002462subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002463{
2464 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002465 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002466
Jason Ekstranda7af7042013-10-12 22:38:11 -05002467 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002468 weston_view_set_position(view,
2469 view->geometry.x + dx,
2470 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002471
2472 /* No need to check parent mappedness, because if parent is not
2473 * mapped, parent is not in a visible layer, so this sub-surface
2474 * will not be drawn either.
2475 */
2476 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002477 /* Cannot call weston_surface_update_transform(),
2478 * because that would call it also for the parent surface,
2479 * which might not be mapped yet. That would lead to
2480 * inconsistent state, where the window could never be
2481 * mapped.
2482 *
2483 * Instead just assing any output, to make
2484 * weston_surface_is_mapped() return true, so that when the
2485 * parent surface does get mapped, this one will get
2486 * included, too. See surface_list_add().
2487 */
2488 assert(!wl_list_empty(&compositor->output_list));
2489 surface->output = container_of(compositor->output_list.next,
2490 struct weston_output, link);
2491 }
2492}
2493
2494static struct weston_subsurface *
2495weston_surface_to_subsurface(struct weston_surface *surface)
2496{
2497 if (surface->configure == subsurface_configure)
2498 return surface->configure_private;
2499
2500 return NULL;
2501}
2502
Pekka Paalanen01388e22013-04-25 13:57:44 +03002503WL_EXPORT struct weston_surface *
2504weston_surface_get_main_surface(struct weston_surface *surface)
2505{
2506 struct weston_subsurface *sub;
2507
2508 while (surface && (sub = weston_surface_to_subsurface(surface)))
2509 surface = sub->parent;
2510
2511 return surface;
2512}
2513
Pekka Paalanene67858b2013-04-25 13:57:42 +03002514static void
2515subsurface_set_position(struct wl_client *client,
2516 struct wl_resource *resource, int32_t x, int32_t y)
2517{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002518 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002519
2520 if (!sub)
2521 return;
2522
2523 sub->position.x = x;
2524 sub->position.y = y;
2525 sub->position.set = 1;
2526}
2527
2528static struct weston_subsurface *
2529subsurface_from_surface(struct weston_surface *surface)
2530{
2531 struct weston_subsurface *sub;
2532
2533 sub = weston_surface_to_subsurface(surface);
2534 if (sub)
2535 return sub;
2536
2537 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2538 if (sub->surface == surface)
2539 return sub;
2540
2541 return NULL;
2542}
2543
2544static struct weston_subsurface *
2545subsurface_sibling_check(struct weston_subsurface *sub,
2546 struct weston_surface *surface,
2547 const char *request)
2548{
2549 struct weston_subsurface *sibling;
2550
2551 sibling = subsurface_from_surface(surface);
2552
2553 if (!sibling) {
2554 wl_resource_post_error(sub->resource,
2555 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2556 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002557 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002558 return NULL;
2559 }
2560
2561 if (sibling->parent != sub->parent) {
2562 wl_resource_post_error(sub->resource,
2563 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2564 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002565 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002566 return NULL;
2567 }
2568
2569 return sibling;
2570}
2571
2572static void
2573subsurface_place_above(struct wl_client *client,
2574 struct wl_resource *resource,
2575 struct wl_resource *sibling_resource)
2576{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002577 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002578 struct weston_surface *surface =
2579 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002580 struct weston_subsurface *sibling;
2581
2582 if (!sub)
2583 return;
2584
2585 sibling = subsurface_sibling_check(sub, surface, "place_above");
2586 if (!sibling)
2587 return;
2588
2589 wl_list_remove(&sub->parent_link_pending);
2590 wl_list_insert(sibling->parent_link_pending.prev,
2591 &sub->parent_link_pending);
2592}
2593
2594static void
2595subsurface_place_below(struct wl_client *client,
2596 struct wl_resource *resource,
2597 struct wl_resource *sibling_resource)
2598{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002599 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002600 struct weston_surface *surface =
2601 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002602 struct weston_subsurface *sibling;
2603
2604 if (!sub)
2605 return;
2606
2607 sibling = subsurface_sibling_check(sub, surface, "place_below");
2608 if (!sibling)
2609 return;
2610
2611 wl_list_remove(&sub->parent_link_pending);
2612 wl_list_insert(&sibling->parent_link_pending,
2613 &sub->parent_link_pending);
2614}
2615
2616static void
2617subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2618{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002619 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002620
2621 if (sub)
2622 sub->synchronized = 1;
2623}
2624
2625static void
2626subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2627{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002628 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002629
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002630 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002631 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002632
2633 /* If sub became effectively desynchronized, flush. */
2634 if (!weston_subsurface_is_synchronized(sub))
2635 weston_subsurface_synchronized_commit(sub);
2636 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002637}
2638
2639static void
2640weston_subsurface_cache_init(struct weston_subsurface *sub)
2641{
2642 pixman_region32_init(&sub->cached.damage);
2643 pixman_region32_init(&sub->cached.opaque);
2644 pixman_region32_init(&sub->cached.input);
2645 wl_list_init(&sub->cached.frame_callback_list);
2646 sub->cached.buffer_ref.buffer = NULL;
2647}
2648
2649static void
2650weston_subsurface_cache_fini(struct weston_subsurface *sub)
2651{
2652 struct weston_frame_callback *cb, *tmp;
2653
2654 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002655 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656
2657 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2658 pixman_region32_fini(&sub->cached.damage);
2659 pixman_region32_fini(&sub->cached.opaque);
2660 pixman_region32_fini(&sub->cached.input);
2661}
2662
2663static void
2664weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2665{
2666 wl_list_remove(&sub->parent_link);
2667 wl_list_remove(&sub->parent_link_pending);
2668 wl_list_remove(&sub->parent_destroy_listener.link);
2669 sub->parent = NULL;
2670}
2671
2672static void
2673weston_subsurface_destroy(struct weston_subsurface *sub);
2674
2675static void
2676subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2677{
2678 struct weston_subsurface *sub =
2679 container_of(listener, struct weston_subsurface,
2680 surface_destroy_listener);
2681 assert(data == &sub->surface->resource);
2682
2683 /* The protocol object (wl_resource) is left inert. */
2684 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002685 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002686
2687 weston_subsurface_destroy(sub);
2688}
2689
2690static void
2691subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2692{
2693 struct weston_subsurface *sub =
2694 container_of(listener, struct weston_subsurface,
2695 parent_destroy_listener);
2696 assert(data == &sub->parent->resource);
2697 assert(sub->surface != sub->parent);
2698
2699 if (weston_surface_is_mapped(sub->surface))
2700 weston_surface_unmap(sub->surface);
2701
2702 weston_subsurface_unlink_parent(sub);
2703}
2704
2705static void
2706subsurface_resource_destroy(struct wl_resource *resource)
2707{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002708 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709
2710 if (sub)
2711 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002712}
2713
2714static void
2715subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2716{
2717 wl_resource_destroy(resource);
2718}
2719
2720static void
2721weston_subsurface_link_parent(struct weston_subsurface *sub,
2722 struct weston_surface *parent)
2723{
2724 sub->parent = parent;
2725 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002726 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002727 &sub->parent_destroy_listener);
2728
2729 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2730 wl_list_insert(&parent->subsurface_list_pending,
2731 &sub->parent_link_pending);
2732}
2733
2734static void
2735weston_subsurface_link_surface(struct weston_subsurface *sub,
2736 struct weston_surface *surface)
2737{
2738 sub->surface = surface;
2739 sub->surface_destroy_listener.notify =
2740 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002741 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002742 &sub->surface_destroy_listener);
2743}
2744
2745static void
2746weston_subsurface_destroy(struct weston_subsurface *sub)
2747{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002748 struct weston_view *view, *next;
2749
Pekka Paalanene67858b2013-04-25 13:57:42 +03002750 assert(sub->surface);
2751
2752 if (sub->resource) {
2753 assert(weston_surface_to_subsurface(sub->surface) == sub);
2754 assert(sub->parent_destroy_listener.notify ==
2755 subsurface_handle_parent_destroy);
2756
Jason Ekstranda7af7042013-10-12 22:38:11 -05002757 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2758 weston_view_destroy(view);
2759
Pekka Paalanene67858b2013-04-25 13:57:42 +03002760 if (sub->parent)
2761 weston_subsurface_unlink_parent(sub);
2762
2763 weston_subsurface_cache_fini(sub);
2764
2765 sub->surface->configure = NULL;
2766 sub->surface->configure_private = NULL;
2767 } else {
2768 /* the dummy weston_subsurface for the parent itself */
2769 assert(sub->parent_destroy_listener.notify == NULL);
2770 wl_list_remove(&sub->parent_link);
2771 wl_list_remove(&sub->parent_link_pending);
2772 }
2773
2774 wl_list_remove(&sub->surface_destroy_listener.link);
2775 free(sub);
2776}
2777
2778static const struct wl_subsurface_interface subsurface_implementation = {
2779 subsurface_destroy,
2780 subsurface_set_position,
2781 subsurface_place_above,
2782 subsurface_place_below,
2783 subsurface_set_sync,
2784 subsurface_set_desync
2785};
2786
2787static struct weston_subsurface *
2788weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2789 struct weston_surface *parent)
2790{
2791 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002792 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002793
2794 sub = calloc(1, sizeof *sub);
2795 if (!sub)
2796 return NULL;
2797
Jason Ekstranda7af7042013-10-12 22:38:11 -05002798 wl_list_init(&sub->unused_views);
2799
Jason Ekstranda85118c2013-06-27 20:17:02 -05002800 sub->resource =
2801 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002802 if (!sub->resource) {
2803 free(sub);
2804 return NULL;
2805 }
2806
Jason Ekstranda85118c2013-06-27 20:17:02 -05002807 wl_resource_set_implementation(sub->resource,
2808 &subsurface_implementation,
2809 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002810 weston_subsurface_link_surface(sub, surface);
2811 weston_subsurface_link_parent(sub, parent);
2812 weston_subsurface_cache_init(sub);
2813 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002814
2815 return sub;
2816}
2817
2818/* Create a dummy subsurface for having the parent itself in its
2819 * sub-surface lists. Makes stacking order manipulation easy.
2820 */
2821static struct weston_subsurface *
2822weston_subsurface_create_for_parent(struct weston_surface *parent)
2823{
2824 struct weston_subsurface *sub;
2825
2826 sub = calloc(1, sizeof *sub);
2827 if (!sub)
2828 return NULL;
2829
2830 weston_subsurface_link_surface(sub, parent);
2831 sub->parent = parent;
2832 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2833 wl_list_insert(&parent->subsurface_list_pending,
2834 &sub->parent_link_pending);
2835
2836 return sub;
2837}
2838
2839static void
2840subcompositor_get_subsurface(struct wl_client *client,
2841 struct wl_resource *resource,
2842 uint32_t id,
2843 struct wl_resource *surface_resource,
2844 struct wl_resource *parent_resource)
2845{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002846 struct weston_surface *surface =
2847 wl_resource_get_user_data(surface_resource);
2848 struct weston_surface *parent =
2849 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002850 struct weston_subsurface *sub;
2851 static const char where[] = "get_subsurface: wl_subsurface@";
2852
2853 if (surface == parent) {
2854 wl_resource_post_error(resource,
2855 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2856 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002857 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002858 return;
2859 }
2860
2861 if (weston_surface_to_subsurface(surface)) {
2862 wl_resource_post_error(resource,
2863 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2864 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002865 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002866 return;
2867 }
2868
2869 if (surface->configure) {
2870 wl_resource_post_error(resource,
2871 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2872 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002873 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002874 return;
2875 }
2876
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002877 if (weston_surface_get_main_surface(parent) == surface) {
2878 wl_resource_post_error(resource,
2879 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2880 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002881 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002882 return;
2883 }
2884
Pekka Paalanene67858b2013-04-25 13:57:42 +03002885 /* make sure the parent is in its own list */
2886 if (wl_list_empty(&parent->subsurface_list)) {
2887 if (!weston_subsurface_create_for_parent(parent)) {
2888 wl_resource_post_no_memory(resource);
2889 return;
2890 }
2891 }
2892
2893 sub = weston_subsurface_create(id, surface, parent);
2894 if (!sub) {
2895 wl_resource_post_no_memory(resource);
2896 return;
2897 }
2898
2899 surface->configure = subsurface_configure;
2900 surface->configure_private = sub;
2901}
2902
2903static void
2904subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2905{
2906 wl_resource_destroy(resource);
2907}
2908
2909static const struct wl_subcompositor_interface subcompositor_interface = {
2910 subcompositor_destroy,
2911 subcompositor_get_subsurface
2912};
2913
2914static void
2915bind_subcompositor(struct wl_client *client,
2916 void *data, uint32_t version, uint32_t id)
2917{
2918 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002919 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002920
Jason Ekstranda85118c2013-06-27 20:17:02 -05002921 resource =
2922 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002923 if (resource == NULL) {
2924 wl_client_post_no_memory(client);
2925 return;
2926 }
2927 wl_resource_set_implementation(resource, &subcompositor_interface,
2928 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002929}
2930
2931static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002932weston_compositor_dpms(struct weston_compositor *compositor,
2933 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002934{
2935 struct weston_output *output;
2936
2937 wl_list_for_each(output, &compositor->output_list, link)
2938 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002939 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002940}
2941
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002942WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002943weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002944{
Neil Roberts8b62e202013-09-30 13:14:47 +01002945 uint32_t old_state = compositor->state;
2946
2947 /* The state needs to be changed before emitting the wake
2948 * signal because that may try to schedule a repaint which
2949 * will not work if the compositor is still sleeping */
2950 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2951
2952 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002953 case WESTON_COMPOSITOR_SLEEPING:
2954 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2955 /* fall through */
2956 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002957 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002958 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002959 /* fall through */
2960 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002961 wl_event_source_timer_update(compositor->idle_source,
2962 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002963 }
2964}
2965
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002966WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002967weston_compositor_offscreen(struct weston_compositor *compositor)
2968{
2969 switch (compositor->state) {
2970 case WESTON_COMPOSITOR_OFFSCREEN:
2971 return;
2972 case WESTON_COMPOSITOR_SLEEPING:
2973 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2974 /* fall through */
2975 default:
2976 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2977 wl_event_source_timer_update(compositor->idle_source, 0);
2978 }
2979}
2980
2981WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002982weston_compositor_sleep(struct weston_compositor *compositor)
2983{
2984 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2985 return;
2986
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002987 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002988 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2989 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2990}
2991
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002992static int
2993idle_handler(void *data)
2994{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002995 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002996
2997 if (compositor->idle_inhibit)
2998 return 1;
2999
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003000 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003001 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003002
3003 return 1;
3004}
3005
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003006WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003007weston_plane_init(struct weston_plane *plane,
3008 struct weston_compositor *ec,
3009 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003010{
3011 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003012 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003013 plane->x = x;
3014 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003015 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003016
3017 /* Init the link so that the call to wl_list_remove() when releasing
3018 * the plane without ever stacking doesn't lead to a crash */
3019 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003020}
3021
3022WL_EXPORT void
3023weston_plane_release(struct weston_plane *plane)
3024{
Xiong Zhang97116532013-10-23 13:58:31 +08003025 struct weston_view *view;
3026
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003027 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003028 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003029
Xiong Zhang97116532013-10-23 13:58:31 +08003030 wl_list_for_each(view, &plane->compositor->view_list, link) {
3031 if (view->plane == plane)
3032 view->plane = NULL;
3033 }
3034
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003035 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003036}
3037
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003038WL_EXPORT void
3039weston_compositor_stack_plane(struct weston_compositor *ec,
3040 struct weston_plane *plane,
3041 struct weston_plane *above)
3042{
3043 if (above)
3044 wl_list_insert(above->link.prev, &plane->link);
3045 else
3046 wl_list_insert(&ec->plane_list, &plane->link);
3047}
3048
Casey Dahlin9074db52012-04-19 22:50:09 -04003049static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003050{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003051 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003052}
3053
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003054static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003055bind_output(struct wl_client *client,
3056 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003057{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003058 struct weston_output *output = data;
3059 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003060 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003061
Jason Ekstranda85118c2013-06-27 20:17:02 -05003062 resource = wl_resource_create(client, &wl_output_interface,
3063 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003064 if (resource == NULL) {
3065 wl_client_post_no_memory(client);
3066 return;
3067 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003068
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003069 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003070 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003071
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003072 wl_output_send_geometry(resource,
3073 output->x,
3074 output->y,
3075 output->mm_width,
3076 output->mm_height,
3077 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003078 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003079 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003080 if (version >= 2)
3081 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003082 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003083
3084 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003085 wl_output_send_mode(resource,
3086 mode->flags,
3087 mode->width,
3088 mode->height,
3089 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003090 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003091
3092 if (version >= 2)
3093 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003094}
3095
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003096/* Move other outputs when one is removed so the space remains contiguos. */
3097static void
3098weston_compositor_remove_output(struct weston_compositor *compositor,
3099 struct weston_output *remove_output)
3100{
3101 struct weston_output *output;
3102 int offset = 0;
3103
3104 wl_list_for_each(output, &compositor->output_list, link) {
3105 if (output == remove_output) {
3106 offset = output->width;
3107 continue;
3108 }
3109
3110 if (offset > 0) {
3111 weston_output_move(output,
3112 output->x - offset, output->y);
3113 output->dirty = 1;
3114 }
3115 }
3116}
3117
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003118static void
3119weston_compositor_verify_pointers(struct weston_compositor *ec)
3120{
3121 struct weston_seat *seat;
3122
3123 wl_list_for_each(seat, &ec->seat_list, link) {
3124 if (!seat->pointer)
3125 continue;
3126
3127 weston_pointer_verify(seat->pointer);
3128 }
3129}
3130
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003131WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003132weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003133{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003134 output->destroying = 1;
3135
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003136 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003137 wl_list_remove(&output->link);
3138
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003139 weston_compositor_verify_pointers(output->compositor);
3140
Richard Hughes64ddde12013-05-01 21:52:10 +01003141 wl_signal_emit(&output->destroy_signal, output);
3142
Richard Hughesafe690c2013-05-02 10:10:04 +01003143 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003144 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003145 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003146 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003147
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003148 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003149}
3150
Scott Moreau1bad5db2012-08-18 01:04:05 -06003151static void
3152weston_output_compute_transform(struct weston_output *output)
3153{
3154 struct weston_matrix transform;
3155 int flip;
3156
3157 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003158 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003159
3160 switch(output->transform) {
3161 case WL_OUTPUT_TRANSFORM_FLIPPED:
3162 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3163 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3164 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003165 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003166 flip = -1;
3167 break;
3168 default:
3169 flip = 1;
3170 break;
3171 }
3172
3173 switch(output->transform) {
3174 case WL_OUTPUT_TRANSFORM_NORMAL:
3175 case WL_OUTPUT_TRANSFORM_FLIPPED:
3176 transform.d[0] = flip;
3177 transform.d[1] = 0;
3178 transform.d[4] = 0;
3179 transform.d[5] = 1;
3180 break;
3181 case WL_OUTPUT_TRANSFORM_90:
3182 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3183 transform.d[0] = 0;
3184 transform.d[1] = -flip;
3185 transform.d[4] = 1;
3186 transform.d[5] = 0;
3187 break;
3188 case WL_OUTPUT_TRANSFORM_180:
3189 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3190 transform.d[0] = -flip;
3191 transform.d[1] = 0;
3192 transform.d[4] = 0;
3193 transform.d[5] = -1;
3194 break;
3195 case WL_OUTPUT_TRANSFORM_270:
3196 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3197 transform.d[0] = 0;
3198 transform.d[1] = flip;
3199 transform.d[4] = -1;
3200 transform.d[5] = 0;
3201 break;
3202 default:
3203 break;
3204 }
3205
3206 weston_matrix_multiply(&output->matrix, &transform);
3207}
3208
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003209WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003210weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003211{
Scott Moreau850ca422012-05-21 15:21:25 -06003212 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003213 struct weston_matrix camera;
3214 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003215
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003216 weston_matrix_init(&output->matrix);
3217 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003218 -(output->x + output->width / 2.0),
3219 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003220
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003221 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003222 2.0 / output->width,
3223 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003224
3225 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003226
Scott Moreauccbf29d2012-02-22 14:21:41 -07003227 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003228 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003229 weston_matrix_init(&camera);
3230 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003231 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003232 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003233 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003234 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003235 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003236 weston_matrix_multiply(&output->matrix, &modelview);
3237 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003238
Scott Moreauccbf29d2012-02-22 14:21:41 -07003239 output->dirty = 0;
3240}
3241
Scott Moreau1bad5db2012-08-18 01:04:05 -06003242static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003243weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003244{
3245 output->transform = transform;
3246
3247 switch (transform) {
3248 case WL_OUTPUT_TRANSFORM_90:
3249 case WL_OUTPUT_TRANSFORM_270:
3250 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3251 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3252 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003253 output->width = output->current_mode->height;
3254 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003255 break;
3256 case WL_OUTPUT_TRANSFORM_NORMAL:
3257 case WL_OUTPUT_TRANSFORM_180:
3258 case WL_OUTPUT_TRANSFORM_FLIPPED:
3259 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003260 output->width = output->current_mode->width;
3261 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003262 break;
3263 default:
3264 break;
3265 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003266
Hardening57388e42013-09-18 23:56:36 +02003267 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003268 output->width /= scale;
3269 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003270}
3271
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003272static void
3273weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003274{
3275 output->x = x;
3276 output->y = y;
3277
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003278 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003279 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003280 output->width,
3281 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003282}
3283
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003284WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003285weston_output_move(struct weston_output *output, int x, int y)
3286{
3287 pixman_region32_t old_region;
3288 struct wl_resource *resource;
3289
3290 output->move_x = x - output->x;
3291 output->move_y = y - output->y;
3292
3293 if (output->move_x == 0 && output->move_y == 0)
3294 return;
3295
3296 pixman_region32_init(&old_region);
3297 pixman_region32_copy(&old_region, &output->region);
3298
3299 weston_output_init_geometry(output, x, y);
3300
3301 output->dirty = 1;
3302
3303 /* Move views on this output. */
3304 wl_signal_emit(&output->move_signal, output);
3305
3306 /* Notify clients of the change for output position. */
3307 wl_resource_for_each(resource, &output->resource_list)
3308 wl_output_send_geometry(resource,
3309 output->x,
3310 output->y,
3311 output->mm_width,
3312 output->mm_height,
3313 output->subpixel,
3314 output->make,
3315 output->model,
3316 output->transform);
3317}
3318
3319WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003320weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003321 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003322 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003323{
3324 output->compositor = c;
3325 output->x = x;
3326 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003327 output->mm_width = mm_width;
3328 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003329 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003330 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003331
Alexander Larsson0b135062013-05-28 16:23:36 +02003332 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003333 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003334
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003335 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003336 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003337
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003338 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003339 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003340 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003341 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003342 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003343
Casey Dahlin58ba1372012-04-19 22:50:08 -04003344 output->id = ffs(~output->compositor->output_id_pool) - 1;
3345 output->compositor->output_id_pool |= 1 << output->id;
3346
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003347 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003348 wl_global_create(c->wl_display, &wl_output_interface, 2,
3349 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003350 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003351}
3352
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003353WL_EXPORT void
3354weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003355 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003356 wl_fixed_t *x, wl_fixed_t *y)
3357{
3358 wl_fixed_t tx, ty;
3359 wl_fixed_t width, height;
3360
Hardeningff39efa2013-09-18 23:56:35 +02003361 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3362 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003363
3364 switch(output->transform) {
3365 case WL_OUTPUT_TRANSFORM_NORMAL:
3366 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003367 tx = device_x;
3368 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003369 break;
3370 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003371 tx = device_y;
3372 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003373 break;
3374 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003375 tx = width - device_x;
3376 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003377 break;
3378 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003379 tx = width - device_y;
3380 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003381 break;
3382 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003383 tx = width - device_x;
3384 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003385 break;
3386 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003387 tx = width - device_y;
3388 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003389 break;
3390 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003391 tx = device_x;
3392 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003393 break;
3394 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003395 tx = device_y;
3396 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003397 break;
3398 }
3399
Hardeningff39efa2013-09-18 23:56:35 +02003400 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3401 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003402}
3403
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003404static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003405compositor_bind(struct wl_client *client,
3406 void *data, uint32_t version, uint32_t id)
3407{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003408 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003409 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003410
Jason Ekstranda85118c2013-06-27 20:17:02 -05003411 resource = wl_resource_create(client, &wl_compositor_interface,
3412 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003413 if (resource == NULL) {
3414 wl_client_post_no_memory(client);
3415 return;
3416 }
3417
3418 wl_resource_set_implementation(resource, &compositor_interface,
3419 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003420}
3421
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003422static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003423log_uname(void)
3424{
3425 struct utsname usys;
3426
3427 uname(&usys);
3428
3429 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3430 usys.version, usys.machine);
3431}
3432
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003433WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003434weston_environment_get_fd(const char *env)
3435{
3436 char *e, *end;
3437 int fd, flags;
3438
3439 e = getenv(env);
3440 if (!e)
3441 return -1;
3442 fd = strtol(e, &end, 0);
3443 if (*end != '\0')
3444 return -1;
3445
3446 flags = fcntl(fd, F_GETFD);
3447 if (flags == -1)
3448 return -1;
3449
3450 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3451 unsetenv(env);
3452
3453 return fd;
3454}
3455
3456WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003457weston_compositor_init(struct weston_compositor *ec,
3458 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003459 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003460 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003461{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003462 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003463 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003464 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003465
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003466 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003467 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003468 wl_signal_init(&ec->destroy_signal);
3469 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003470 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003471 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003472 wl_signal_init(&ec->idle_signal);
3473 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003474 wl_signal_init(&ec->show_input_panel_signal);
3475 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003476 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003477 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003478 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003479 wl_signal_init(&ec->session_signal);
3480 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003481
Casey Dahlin58ba1372012-04-19 22:50:08 -04003482 ec->output_id_pool = 0;
3483
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003484 if (!wl_global_create(display, &wl_compositor_interface, 3,
3485 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003486 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003487
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003488 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3489 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003490 return -1;
3491
Jason Ekstranda7af7042013-10-12 22:38:11 -05003492 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003493 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003494 wl_list_init(&ec->layer_list);
3495 wl_list_init(&ec->seat_list);
3496 wl_list_init(&ec->output_list);
3497 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003498 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003499 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003500 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003501 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003502 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003503
Xiong Zhang97116532013-10-23 13:58:31 +08003504 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003505 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003506
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003507 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3508 weston_config_section_get_string(s, "keymap_rules",
3509 (char **) &xkb_names.rules, NULL);
3510 weston_config_section_get_string(s, "keymap_model",
3511 (char **) &xkb_names.model, NULL);
3512 weston_config_section_get_string(s, "keymap_layout",
3513 (char **) &xkb_names.layout, NULL);
3514 weston_config_section_get_string(s, "keymap_variant",
3515 (char **) &xkb_names.variant, NULL);
3516 weston_config_section_get_string(s, "keymap_options",
3517 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003518
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003519 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3520 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003521
3522 ec->ping_handler = NULL;
3523
3524 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003525 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003526
3527 wl_data_device_manager_init(ec->wl_display);
3528
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003529 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003530
Daniel Stone725c2c32012-06-22 14:04:36 +01003531 loop = wl_display_get_event_loop(ec->wl_display);
3532 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3533 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3534
3535 ec->input_loop = wl_event_loop_create();
3536
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003537 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3538 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3539
3540 weston_compositor_schedule_repaint(ec);
3541
Daniel Stone725c2c32012-06-22 14:04:36 +01003542 return 0;
3543}
3544
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003545WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003546weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003547{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003548 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003549
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003550 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003551 if (ec->input_loop_source)
3552 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003553
Matt Roper361d2ad2011-08-29 13:52:23 -07003554 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003555 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003556 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003557
Daniel Stone325fc2d2012-05-30 16:31:58 +01003558 weston_binding_list_destroy_all(&ec->key_binding_list);
3559 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003560 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003561 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003562 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003563
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003564 weston_plane_release(&ec->primary_plane);
3565
Jonas Ådahlc97af922012-03-28 22:36:09 +02003566 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003567
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003568 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003569}
3570
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003571WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003572weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3573 const struct weston_pointer_grab_interface *interface)
3574{
3575 struct weston_seat *seat;
3576
3577 ec->default_pointer_grab = interface;
3578 wl_list_for_each(seat, &ec->seat_list, link) {
3579 if (seat->pointer) {
3580 weston_pointer_set_default_grab(seat->pointer,
3581 interface);
3582 }
3583 }
3584}
3585
3586WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003587weston_version(int *major, int *minor, int *micro)
3588{
3589 *major = WESTON_VERSION_MAJOR;
3590 *minor = WESTON_VERSION_MINOR;
3591 *micro = WESTON_VERSION_MICRO;
3592}
3593
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003594static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003595 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003596 const char *desc;
3597} capability_strings[] = {
3598 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003599 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003600};
3601
3602static void
3603weston_compositor_log_capabilities(struct weston_compositor *compositor)
3604{
3605 unsigned i;
3606 int yes;
3607
3608 weston_log("Compositor capabilities:\n");
3609 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3610 yes = compositor->capabilities & capability_strings[i].bit;
3611 weston_log_continue(STAMP_SPACE "%s %s\n",
3612 capability_strings[i].desc,
3613 yes ? "yes" : "no");
3614 }
3615}
3616
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003617static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003618{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003619 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003620
Martin Minarik6d118362012-06-07 18:01:59 +02003621 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003622 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003623
3624 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003625}
3626
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003627#ifdef HAVE_LIBUNWIND
3628
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003629static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003630print_backtrace(void)
3631{
3632 unw_cursor_t cursor;
3633 unw_context_t context;
3634 unw_word_t off;
3635 unw_proc_info_t pip;
3636 int ret, i = 0;
3637 char procname[256];
3638 const char *filename;
3639 Dl_info dlinfo;
3640
3641 pip.unwind_info = NULL;
3642 ret = unw_getcontext(&context);
3643 if (ret) {
3644 weston_log("unw_getcontext: %d\n", ret);
3645 return;
3646 }
3647
3648 ret = unw_init_local(&cursor, &context);
3649 if (ret) {
3650 weston_log("unw_init_local: %d\n", ret);
3651 return;
3652 }
3653
3654 ret = unw_step(&cursor);
3655 while (ret > 0) {
3656 ret = unw_get_proc_info(&cursor, &pip);
3657 if (ret) {
3658 weston_log("unw_get_proc_info: %d\n", ret);
3659 break;
3660 }
3661
3662 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3663 if (ret && ret != -UNW_ENOMEM) {
3664 if (ret != -UNW_EUNSPEC)
3665 weston_log("unw_get_proc_name: %d\n", ret);
3666 procname[0] = '?';
3667 procname[1] = 0;
3668 }
3669
3670 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3671 *dlinfo.dli_fname)
3672 filename = dlinfo.dli_fname;
3673 else
3674 filename = "?";
3675
3676 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3677 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3678
3679 ret = unw_step(&cursor);
3680 if (ret < 0)
3681 weston_log("unw_step: %d\n", ret);
3682 }
3683}
3684
3685#else
3686
3687static void
3688print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003689{
3690 void *buffer[32];
3691 int i, count;
3692 Dl_info info;
3693
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003694 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3695 for (i = 0; i < count; i++) {
3696 dladdr(buffer[i], &info);
3697 weston_log(" [%016lx] %s (%s)\n",
3698 (long) buffer[i],
3699 info.dli_sname ? info.dli_sname : "--",
3700 info.dli_fname);
3701 }
3702}
3703
3704#endif
3705
3706static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003707on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003708{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003709 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003710 * then call the backend restore function, which will switch
3711 * back to the vt we launched from or ungrab X etc and then
3712 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003713 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003714 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003715 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003716
Peter Maatmane5b42e42013-03-27 22:38:53 +01003717 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003718
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003719 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003720
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003721 segv_compositor->restore(segv_compositor);
3722
3723 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003724}
3725
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003726WL_EXPORT void *
3727weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003728{
3729 char path[PATH_MAX];
3730 void *module, *init;
3731
3732 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003733 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003734 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003735 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003736
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003737 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3738 if (module) {
3739 weston_log("Module '%s' already loaded\n", path);
3740 dlclose(module);
3741 return NULL;
3742 }
3743
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003744 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003745 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003746 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003747 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003748 return NULL;
3749 }
3750
3751 init = dlsym(module, entrypoint);
3752 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003753 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003754 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003755 return NULL;
3756 }
3757
3758 return init;
3759}
3760
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003761static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003762load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003763 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003764{
3765 const char *p, *end;
3766 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003767 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003768 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003769
3770 if (modules == NULL)
3771 return 0;
3772
3773 p = modules;
3774 while (*p) {
3775 end = strchrnul(p, ',');
3776 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003777 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003778 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003779 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003780 p = end;
3781 while (*p == ',')
3782 p++;
3783
3784 }
3785
3786 return 0;
3787}
3788
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003789static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003790 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3791
3792static const char xdg_wrong_message[] =
3793 "fatal: environment variable XDG_RUNTIME_DIR\n"
3794 "is set to \"%s\", which is not a directory.\n";
3795
3796static const char xdg_wrong_mode_message[] =
3797 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3798 "correctly. Unix access mode must be 0700 but is %o,\n"
3799 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003800 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003801
3802static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003803 "Refer to your distribution on how to get it, or\n"
3804 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3805 "on how to implement it.\n";
3806
Martin Minarik37032f82012-06-18 20:15:18 +02003807static void
3808verify_xdg_runtime_dir(void)
3809{
3810 char *dir = getenv("XDG_RUNTIME_DIR");
3811 struct stat s;
3812
3813 if (!dir) {
3814 weston_log(xdg_error_message);
3815 weston_log_continue(xdg_detail_message);
3816 exit(EXIT_FAILURE);
3817 }
3818
3819 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3820 weston_log(xdg_wrong_message, dir);
3821 weston_log_continue(xdg_detail_message);
3822 exit(EXIT_FAILURE);
3823 }
3824
3825 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3826 weston_log(xdg_wrong_mode_message,
3827 dir, s.st_mode & 0777, s.st_uid);
3828 weston_log_continue(xdg_detail_message);
3829 }
3830}
3831
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003832static int
3833usage(int error_code)
3834{
3835 fprintf(stderr,
3836 "Usage: weston [OPTIONS]\n\n"
3837 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3838 "Weston supports multiple backends, and depending on which backend is in use\n"
3839 "different options will be accepted.\n\n"
3840
3841
3842 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003843 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003844 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003845 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3846 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003847 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003848 " -S, --socket=NAME\tName of socket to listen on\n"
3849 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003850 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003851 " --log==FILE\t\tLog to the given file\n"
3852 " -h, --help\t\tThis help message\n\n");
3853
3854 fprintf(stderr,
3855 "Options for drm-backend.so:\n\n"
3856 " --connector=ID\tBring up only this connector\n"
3857 " --seat=SEAT\t\tThe seat that weston should run on\n"
3858 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003859 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003860 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3861
3862 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003863 "Options for fbdev-backend.so:\n\n"
3864 " --tty=TTY\t\tThe tty to use\n"
3865 " --device=DEVICE\tThe framebuffer device to use\n\n");
3866
3867 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003868 "Options for x11-backend.so:\n\n"
3869 " --width=WIDTH\t\tWidth of X window\n"
3870 " --height=HEIGHT\tHeight of X window\n"
3871 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003872 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003873 " --output-count=COUNT\tCreate multiple outputs\n"
3874 " --no-input\t\tDont create input devices\n\n");
3875
3876 fprintf(stderr,
3877 "Options for wayland-backend.so:\n\n"
3878 " --width=WIDTH\t\tWidth of Wayland surface\n"
3879 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003880 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003881 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003882 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003883 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003884 " --display=DISPLAY\tWayland display to connect to\n\n");
3885
Pekka Paalanene31e0532013-05-22 18:03:07 +03003886#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3887 fprintf(stderr,
3888 "Options for rpi-backend.so:\n\n"
3889 " --tty=TTY\t\tThe tty to use\n"
3890 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3891 " --transform=TR\tThe output transformation, TR is one of:\n"
3892 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003893 " --opaque-regions\tEnable support for opaque regions, can be "
3894 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003895 "\n");
3896#endif
3897
Hardeningc077a842013-07-08 00:51:35 +02003898#if defined(BUILD_RDP_COMPOSITOR)
3899 fprintf(stderr,
3900 "Options for rdp-backend.so:\n\n"
3901 " --width=WIDTH\t\tWidth of desktop\n"
3902 " --height=HEIGHT\tHeight of desktop\n"
3903 " --extra-modes=MODES\t\n"
3904 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3905 " --address=ADDR\tThe address to bind\n"
3906 " --port=PORT\tThe port to listen on\n"
3907 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3908 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3909 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3910 "\n");
3911#endif
3912
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003913 exit(error_code);
3914}
3915
Peter Maatmane5b42e42013-03-27 22:38:53 +01003916static void
3917catch_signals(void)
3918{
3919 struct sigaction action;
3920
3921 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3922 action.sa_sigaction = on_caught_signal;
3923 sigemptyset(&action.sa_mask);
3924 sigaction(SIGSEGV, &action, NULL);
3925 sigaction(SIGABRT, &action, NULL);
3926}
3927
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003928int main(int argc, char *argv[])
3929{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003930 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003931 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003932 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003933 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003934 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003935 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003936 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003937 int *argc, char *argv[],
3938 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003939 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003940 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003941 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003942 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003943 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003944 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003945 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003946 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003947 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003948 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003949 struct weston_config *config;
3950 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003951
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003952 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003953 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003954 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003955 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3956 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003957 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003958 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003959 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003960 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003961 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003962
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003963 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003964
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003965 if (help)
3966 usage(EXIT_SUCCESS);
3967
Scott Moreau12245142012-08-29 15:15:58 -06003968 if (version) {
3969 printf(PACKAGE_STRING "\n");
3970 return EXIT_SUCCESS;
3971 }
3972
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003973 weston_log_file_open(log);
3974
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003975 weston_log("%s\n"
3976 STAMP_SPACE "%s\n"
3977 STAMP_SPACE "Bug reports to: %s\n"
3978 STAMP_SPACE "Build: %s\n",
3979 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003980 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003981 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003982
Martin Minarik37032f82012-06-18 20:15:18 +02003983 verify_xdg_runtime_dir();
3984
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003985 display = wl_display_create();
3986
Tiago Vignatti2116b892011-08-08 05:52:59 -07003987 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003988 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3989 display);
3990 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3991 display);
3992 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3993 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003994
3995 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003996 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3997 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003998
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003999 config = weston_config_parse("weston.ini");
4000 if (config != NULL) {
4001 weston_log("Using config file '%s'\n",
4002 weston_config_get_full_path(config));
4003 } else {
4004 weston_log("Starting with no config file.\n");
4005 }
4006 section = weston_config_get_section(config, "core", NULL, NULL);
4007
4008 weston_config_section_get_string(section, "backend", &backend, NULL);
4009 if (option_backend) {
4010 backend = option_backend;
4011 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004012 if (!backend) {
4013 if (getenv("WAYLAND_DISPLAY"))
4014 backend = "wayland-backend.so";
4015 else if (getenv("DISPLAY"))
4016 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004017 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02004018 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004019 }
4020
Jason Ekstranda985da42013-08-22 17:28:03 -05004021 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004022
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004023 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004024 if (!backend_init)
4025 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004026
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004027 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004028 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004029 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004030 exit(EXIT_FAILURE);
4031 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004032
Peter Maatmane5b42e42013-03-27 22:38:53 +01004033 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004034 segv_compositor = ec;
4035
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004036 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004037 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004038
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004039 setenv("WAYLAND_DISPLAY", socket_name, 1);
4040
Jason Ekstranda985da42013-08-22 17:28:03 -05004041 if (!shell)
4042 weston_config_section_get_string(section, "shell", &shell,
4043 "desktop-shell.so");
4044 if (load_modules(ec, shell, &argc, argv) < 0)
4045 goto out;
4046
Ossama Othmana50e6e42013-05-14 09:48:26 -07004047 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004048 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004049 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004050 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004051
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004052 for (i = 1; i < argc; i++)
4053 weston_log("fatal: unhandled option: %s\n", argv[i]);
4054 if (argc > 1) {
4055 ret = EXIT_FAILURE;
4056 goto out;
4057 }
4058
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004059 weston_compositor_log_capabilities(ec);
4060
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004061 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004062 weston_log("fatal: failed to add socket: %m\n");
4063 ret = EXIT_FAILURE;
4064 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004065 }
4066
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004067 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004068
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004069 wl_display_run(display);
4070
4071 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004072 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004073 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004074
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004075 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004076
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004077 for (i = ARRAY_LENGTH(signals); i;)
4078 wl_event_source_remove(signals[--i]);
4079
Daniel Stone855028f2012-05-01 20:37:10 +01004080 weston_compositor_xkb_destroy(ec);
4081
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004082 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004083 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004084
Martin Minarik19e6f262012-06-07 13:08:46 +02004085 weston_log_file_close();
4086
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004087 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004088}