blob: c4ffb5e95ed8199e4424cd72125771f01a31d968 [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 Ekstrand918f2dd2013-12-02 21:01:53 -06001222weston_surface_set_size(struct weston_surface *surface,
1223 int32_t width, int32_t height)
1224{
1225 struct weston_view *view;
1226
1227 if (surface->width == width && surface->height == height)
1228 return;
1229
1230 surface->width = width;
1231 surface->height = height;
1232
1233 wl_list_for_each(view, &surface->views, surface_link)
1234 weston_view_geometry_dirty(view);
1235}
1236
1237static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001238weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001239{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001240 int32_t width, height;
1241
1242 if (!surface->buffer_ref.buffer) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001243 weston_surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001244 return;
1245 }
1246
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001247 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001248 case WL_OUTPUT_TRANSFORM_90:
1249 case WL_OUTPUT_TRANSFORM_270:
1250 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1251 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001252 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001253 height = surface->buffer_ref.buffer->width;
1254 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001255 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001256 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001257 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001258 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001259 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001260
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001261 width = width / surface->buffer_viewport.scale;
1262 height = height / surface->buffer_viewport.scale;
1263 weston_surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001264}
1265
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001266WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001267weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001268{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001269 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001270
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001271 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001272
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001273 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001274}
1275
Jason Ekstranda7af7042013-10-12 22:38:11 -05001276WL_EXPORT struct weston_view *
1277weston_compositor_pick_view(struct weston_compositor *compositor,
1278 wl_fixed_t x, wl_fixed_t y,
1279 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001280{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 wl_list_for_each(view, &compositor->view_list, link) {
1284 weston_view_from_global_fixed(view, x, y, vx, vy);
1285 if (pixman_region32_contains_point(&view->surface->input,
1286 wl_fixed_to_int(*vx),
1287 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001288 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001290 }
1291
1292 return NULL;
1293}
1294
1295static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001296weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001297{
Daniel Stone37816df2012-05-16 18:45:18 +01001298 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001299
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001300 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001301 return;
1302
Daniel Stone37816df2012-05-16 18:45:18 +01001303 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001304 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001305}
1306
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001307WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001308weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001309{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001310 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001311
Jason Ekstranda7af7042013-10-12 22:38:11 -05001312 if (!weston_view_is_mapped(view))
1313 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001314
Jason Ekstranda7af7042013-10-12 22:38:11 -05001315 weston_view_damage_below(view);
1316 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001317 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318 wl_list_remove(&view->layer_link);
1319 wl_list_init(&view->layer_link);
1320 wl_list_remove(&view->link);
1321 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001322 wl_list_remove(&view->output_move_listener.link);
1323 wl_list_init(&view->output_move_listener.link);
Zhang, Xiong Y010d0b12013-12-13 22:10:54 +02001324 wl_list_remove(&view->output_destroy_listener.link);
1325 wl_list_init(&view->output_destroy_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001326 view->output_mask = 0;
1327 weston_surface_assign_output(view->surface);
1328
1329 if (weston_surface_is_mapped(view->surface))
1330 return;
1331
1332 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1333 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001334 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001335 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001336 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001337 NULL,
1338 wl_fixed_from_int(0),
1339 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001341 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001342 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001343}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001344
Jason Ekstranda7af7042013-10-12 22:38:11 -05001345WL_EXPORT void
1346weston_surface_unmap(struct weston_surface *surface)
1347{
1348 struct weston_view *view;
1349
1350 wl_list_for_each(view, &surface->views, surface_link)
1351 weston_view_unmap(view);
1352 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001353}
1354
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001355struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001356 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001357 struct wl_list link;
1358};
1359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360WL_EXPORT void
1361weston_view_destroy(struct weston_view *view)
1362{
1363 wl_signal_emit(&view->destroy_signal, view);
1364
1365 assert(wl_list_empty(&view->geometry.child_list));
1366
1367 if (weston_view_is_mapped(view)) {
1368 weston_view_unmap(view);
1369 weston_compositor_build_view_list(view->surface->compositor);
1370 }
1371
1372 wl_list_remove(&view->link);
1373 wl_list_remove(&view->layer_link);
1374
1375 pixman_region32_fini(&view->clip);
1376 pixman_region32_fini(&view->transform.boundingbox);
1377
1378 weston_view_set_transform_parent(view, NULL);
1379
Jason Ekstranda7af7042013-10-12 22:38:11 -05001380 wl_list_remove(&view->surface_link);
1381
1382 free(view);
1383}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001384
1385WL_EXPORT void
1386weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001387{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001388 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001390
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001391 if (--surface->ref_count > 0)
1392 return;
1393
1394 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1395
Pekka Paalanene67858b2013-04-25 13:57:42 +03001396 assert(wl_list_empty(&surface->subsurface_list_pending));
1397 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001398
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1400 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001401
Pekka Paalanenbc106382012-10-10 12:49:31 +03001402 wl_list_for_each_safe(cb, next,
1403 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001404 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001405
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001406 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001407 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001408 pixman_region32_fini(&surface->pending.damage);
1409
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001410 if (surface->pending.buffer)
1411 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1412
Pekka Paalanende685b82012-12-04 15:58:12 +02001413 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001414
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001415 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001416 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001417 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001418
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001419 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001420 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001421
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001422 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001423}
1424
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001425static void
1426destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001427{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001428 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001429
Giulio Camuffo0d379742013-11-15 22:06:15 +01001430 /* Set the resource to NULL, since we don't want to leave a
1431 * dangling pointer if the surface was refcounted and survives
1432 * the weston_surface_destroy() call. */
1433 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001434 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001435}
1436
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001437static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001438weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1439{
1440 struct weston_buffer *buffer =
1441 container_of(listener, struct weston_buffer, destroy_listener);
1442
1443 wl_signal_emit(&buffer->destroy_signal, buffer);
1444 free(buffer);
1445}
1446
1447struct weston_buffer *
1448weston_buffer_from_resource(struct wl_resource *resource)
1449{
1450 struct weston_buffer *buffer;
1451 struct wl_listener *listener;
1452
1453 listener = wl_resource_get_destroy_listener(resource,
1454 weston_buffer_destroy_handler);
1455
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001456 if (listener)
1457 return container_of(listener, struct weston_buffer,
1458 destroy_listener);
1459
1460 buffer = zalloc(sizeof *buffer);
1461 if (buffer == NULL)
1462 return NULL;
1463
1464 buffer->resource = resource;
1465 wl_signal_init(&buffer->destroy_signal);
1466 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001467 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001468 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001469
1470 return buffer;
1471}
1472
1473static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001474weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1475 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001476{
Pekka Paalanende685b82012-12-04 15:58:12 +02001477 struct weston_buffer_reference *ref =
1478 container_of(listener, struct weston_buffer_reference,
1479 destroy_listener);
1480
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001481 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001482 ref->buffer = NULL;
1483}
1484
1485WL_EXPORT void
1486weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001487 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001488{
1489 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001490 ref->buffer->busy_count--;
1491 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001492 assert(wl_resource_get_client(ref->buffer->resource));
1493 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001494 WL_BUFFER_RELEASE);
1495 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001496 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001497 }
1498
Pekka Paalanende685b82012-12-04 15:58:12 +02001499 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001500 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001501 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001502 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001503 }
1504
Pekka Paalanende685b82012-12-04 15:58:12 +02001505 ref->buffer = buffer;
1506 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1507}
1508
1509static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001510weston_surface_attach(struct weston_surface *surface,
1511 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001512{
1513 weston_buffer_reference(&surface->buffer_ref, buffer);
1514
Pekka Paalanena6421c42012-12-04 15:58:10 +02001515 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001516 if (weston_surface_is_mapped(surface))
1517 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001518 }
1519
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001520 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001521}
1522
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001523WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001524weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001525{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001526 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001527
1528 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001530}
1531
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001532WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001533weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001534{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001535 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001536
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001537 pixman_region32_union(&compositor->primary_plane.damage,
1538 &compositor->primary_plane.damage,
1539 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001540 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001541}
1542
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001543static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001545{
Pekka Paalanende685b82012-12-04 15:58:12 +02001546 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001547 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001548 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001549
Jason Ekstranda7af7042013-10-12 22:38:11 -05001550 empty_region(&surface->damage);
1551}
1552
1553static void
1554view_accumulate_damage(struct weston_view *view,
1555 pixman_region32_t *opaque)
1556{
1557 pixman_region32_t damage;
1558
1559 pixman_region32_init(&damage);
1560 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001561 pixman_box32_t *extents;
1562
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 extents = pixman_region32_extents(&view->surface->damage);
1564 view_compute_bbox(view, extents->x1, extents->y1,
1565 extents->x2 - extents->x1,
1566 extents->y2 - extents->y1,
1567 &damage);
1568 pixman_region32_translate(&damage,
1569 -view->plane->x,
1570 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001571 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001572 pixman_region32_copy(&damage, &view->surface->damage);
1573 pixman_region32_translate(&damage,
1574 view->geometry.x - view->plane->x,
1575 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001576 }
1577
Jason Ekstranda7af7042013-10-12 22:38:11 -05001578 pixman_region32_subtract(&damage, &damage, opaque);
1579 pixman_region32_union(&view->plane->damage,
1580 &view->plane->damage, &damage);
1581 pixman_region32_fini(&damage);
1582 pixman_region32_copy(&view->clip, opaque);
1583 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001584}
1585
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001586static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001587compositor_accumulate_damage(struct weston_compositor *ec)
1588{
1589 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001590 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001591 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001592
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001593 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001594
1595 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001596 pixman_region32_copy(&plane->clip, &clip);
1597
1598 pixman_region32_init(&opaque);
1599
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 wl_list_for_each(ev, &ec->view_list, link) {
1601 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001602 continue;
1603
Jason Ekstranda7af7042013-10-12 22:38:11 -05001604 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001605 }
1606
1607 pixman_region32_union(&clip, &clip, &opaque);
1608 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001609 }
1610
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001611 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001612
Jason Ekstranda7af7042013-10-12 22:38:11 -05001613 wl_list_for_each(ev, &ec->view_list, link)
1614 ev->surface->touched = 0;
1615
1616 wl_list_for_each(ev, &ec->view_list, link) {
1617 if (ev->surface->touched)
1618 continue;
1619 ev->surface->touched = 1;
1620
1621 surface_flush_damage(ev->surface);
1622
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001623 /* Both the renderer and the backend have seen the buffer
1624 * by now. If renderer needs the buffer, it has its own
1625 * reference set. If the backend wants to keep the buffer
1626 * around for migrating the surface into a non-primary plane
1627 * later, keep_buffer is true. Otherwise, drop the core
1628 * reference now, and allow early buffer release. This enables
1629 * clients to use single-buffering.
1630 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001631 if (!ev->surface->keep_buffer)
1632 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001633 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001634}
1635
1636static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001637surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001638{
1639 struct weston_subsurface *sub;
1640
Pekka Paalanene67858b2013-04-25 13:57:42 +03001641 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001642 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001643 continue;
1644
Jason Ekstranda7af7042013-10-12 22:38:11 -05001645 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1646 wl_list_init(&sub->surface->views);
1647
1648 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001649 }
1650}
1651
1652static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001654{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001655 struct weston_subsurface *sub;
1656 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001657
Jason Ekstranda7af7042013-10-12 22:38:11 -05001658 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1659 if (sub->surface == surface)
1660 continue;
1661
1662 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1663 weston_view_destroy(view);
1664
1665 surface_free_unused_subsurface_views(sub->surface);
1666 }
1667}
1668
1669static void
1670view_list_add_subsurface_view(struct weston_compositor *compositor,
1671 struct weston_subsurface *sub,
1672 struct weston_view *parent)
1673{
1674 struct weston_subsurface *child;
1675 struct weston_view *view = NULL, *iv;
1676
1677 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1678 if (iv->geometry.parent == parent) {
1679 view = iv;
1680 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001681 }
1682 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001683
1684 if (view) {
1685 /* Put it back in the surface's list of views */
1686 wl_list_remove(&view->surface_link);
1687 wl_list_insert(&sub->surface->views, &view->surface_link);
1688 } else {
1689 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001690 weston_view_set_position(view,
1691 sub->position.x,
1692 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001693 weston_view_set_transform_parent(view, parent);
1694 }
1695
1696 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001697
Pekka Paalanenb188e912013-11-19 14:03:35 +02001698 if (wl_list_empty(&sub->surface->subsurface_list)) {
1699 wl_list_insert(compositor->view_list.prev, &view->link);
1700 return;
1701 }
1702
1703 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1704 if (child->surface == sub->surface)
1705 wl_list_insert(compositor->view_list.prev, &view->link);
1706 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001707 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001708 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709}
1710
1711static void
1712view_list_add(struct weston_compositor *compositor,
1713 struct weston_view *view)
1714{
1715 struct weston_subsurface *sub;
1716
1717 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001718
Pekka Paalanenb188e912013-11-19 14:03:35 +02001719 if (wl_list_empty(&view->surface->subsurface_list)) {
1720 wl_list_insert(compositor->view_list.prev, &view->link);
1721 return;
1722 }
1723
1724 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1725 if (sub->surface == view->surface)
1726 wl_list_insert(compositor->view_list.prev, &view->link);
1727 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001729 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730}
1731
1732static void
1733weston_compositor_build_view_list(struct weston_compositor *compositor)
1734{
1735 struct weston_view *view;
1736 struct weston_layer *layer;
1737
1738 wl_list_for_each(layer, &compositor->layer_list, link)
1739 wl_list_for_each(view, &layer->view_list, layer_link)
1740 surface_stash_subsurface_views(view->surface);
1741
1742 wl_list_init(&compositor->view_list);
1743 wl_list_for_each(layer, &compositor->layer_list, link) {
1744 wl_list_for_each(view, &layer->view_list, layer_link) {
1745 view_list_add(compositor, view);
1746 }
1747 }
1748
1749 wl_list_for_each(layer, &compositor->layer_list, link)
1750 wl_list_for_each(view, &layer->view_list, layer_link)
1751 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001752}
1753
David Herrmann1edf44c2013-10-22 17:11:26 +02001754static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001755weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001756{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001757 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001758 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001759 struct weston_animation *animation, *next;
1760 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001761 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001762 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001763 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001764
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001765 if (output->destroying)
1766 return 0;
1767
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001768 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001770
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001771 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001772 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001773 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 wl_list_for_each(ev, &ec->view_list, link)
1775 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001776
Pekka Paalanene67858b2013-04-25 13:57:42 +03001777 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001778 wl_list_for_each(ev, &ec->view_list, link) {
1779 /* Note: This operation is safe to do multiple times on the
1780 * same surface.
1781 */
1782 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001783 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001784 &ev->surface->frame_callback_list);
1785 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001786 }
1787 }
1788
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001789 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001790
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001791 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001792 pixman_region32_intersect(&output_damage,
1793 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001794 pixman_region32_subtract(&output_damage,
1795 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001796
Scott Moreauccbf29d2012-02-22 14:21:41 -07001797 if (output->dirty)
1798 weston_output_update_matrix(output);
1799
David Herrmann1edf44c2013-10-22 17:11:26 +02001800 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001801
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001802 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001803
Kristian Høgsbergef044142011-06-21 15:02:12 -04001804 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001805
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001806 weston_compositor_repick(ec);
1807 wl_event_loop_dispatch(ec->input_loop, 0);
1808
Jonas Ådahldb773762012-06-13 00:01:21 +02001809 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001810 wl_callback_send_done(cb->resource, msecs);
1811 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001812 }
1813
Scott Moreaud64cf212012-06-08 19:40:54 -06001814 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001815 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001816 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001817 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001818
1819 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001820}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001821
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001822static int
1823weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001824{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001825 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001826
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001827 wl_event_loop_dispatch(compositor->input_loop, 0);
1828
1829 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001830}
1831
1832WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001833weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001834{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001835 struct weston_compositor *compositor = output->compositor;
1836 struct wl_event_loop *loop =
1837 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001838 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001839
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001840 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001841
1842 if (output->repaint_needed &&
1843 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1844 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001845 r = weston_output_repaint(output, msecs);
1846 if (!r)
1847 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001848 }
1849
1850 output->repaint_scheduled = 0;
1851 if (compositor->input_loop_source)
1852 return;
1853
1854 fd = wl_event_loop_get_fd(compositor->input_loop);
1855 compositor->input_loop_source =
1856 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1857 weston_compositor_read_input, compositor);
1858}
1859
1860static void
1861idle_repaint(void *data)
1862{
1863 struct weston_output *output = data;
1864
Jonas Ådahle5a12252013-04-05 23:07:11 +02001865 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001866}
1867
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001868WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001869weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1870{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001872 if (below != NULL)
1873 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001874}
1875
1876WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001877weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001878{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001879 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001880 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001881
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001882 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1883 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001884 return;
1885
Kristian Høgsbergef044142011-06-21 15:02:12 -04001886 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001887 output->repaint_needed = 1;
1888 if (output->repaint_scheduled)
1889 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001890
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001891 wl_event_loop_add_idle(loop, idle_repaint, output);
1892 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001893
1894 if (compositor->input_loop_source) {
1895 wl_event_source_remove(compositor->input_loop_source);
1896 compositor->input_loop_source = NULL;
1897 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001898}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001899
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001900WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001901weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1902{
1903 struct weston_output *output;
1904
1905 wl_list_for_each(output, &compositor->output_list, link)
1906 weston_output_schedule_repaint(output);
1907}
1908
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001909static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001910surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001911{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001912 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001913}
1914
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001915static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001916surface_attach(struct wl_client *client,
1917 struct wl_resource *resource,
1918 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1919{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001920 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001921 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001922
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001923 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001924 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001925 if (buffer == NULL) {
1926 wl_client_post_no_memory(client);
1927 return;
1928 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001929 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001930
Pekka Paalanende685b82012-12-04 15:58:12 +02001931 /* Attach, attach, without commit in between does not send
1932 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001933 if (surface->pending.buffer)
1934 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001935
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001936 surface->pending.sx = sx;
1937 surface->pending.sy = sy;
1938 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001939 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001940 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001941 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001942 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001943 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001944}
1945
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001946static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001947surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001948 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001949 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001950{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001951 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001952
Pekka Paalanen8e159182012-10-10 12:49:25 +03001953 pixman_region32_union_rect(&surface->pending.damage,
1954 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001955 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001956}
1957
Kristian Høgsberg33418202011-08-16 23:01:28 -04001958static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001959destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001960{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001961 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001962
1963 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001964 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001965}
1966
1967static void
1968surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001969 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001970{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001971 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001972 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001973
1974 cb = malloc(sizeof *cb);
1975 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001976 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001977 return;
1978 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001979
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001980 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1981 callback);
1982 if (cb->resource == NULL) {
1983 free(cb);
1984 wl_resource_post_no_memory(resource);
1985 return;
1986 }
1987
Jason Ekstranda85118c2013-06-27 20:17:02 -05001988 wl_resource_set_implementation(cb->resource, NULL, cb,
1989 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001990
Pekka Paalanenbc106382012-10-10 12:49:31 +03001991 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001992}
1993
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001994static void
1995surface_set_opaque_region(struct wl_client *client,
1996 struct wl_resource *resource,
1997 struct wl_resource *region_resource)
1998{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001999 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002000 struct weston_region *region;
2001
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002002 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002003 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002004 pixman_region32_copy(&surface->pending.opaque,
2005 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002006 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03002007 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002008 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002009}
2010
2011static void
2012surface_set_input_region(struct wl_client *client,
2013 struct wl_resource *resource,
2014 struct wl_resource *region_resource)
2015{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002016 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002017 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002018
2019 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002020 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002021 pixman_region32_copy(&surface->pending.input,
2022 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002023 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002024 pixman_region32_fini(&surface->pending.input);
2025 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002026 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002027}
2028
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002029static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002030weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002031{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002032 struct weston_subsurface *sub;
2033
2034 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2035 parent_link_pending) {
2036 wl_list_remove(&sub->parent_link);
2037 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2038 }
2039}
2040
2041static void
2042weston_surface_commit(struct weston_surface *surface)
2043{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002044 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002045 pixman_region32_t opaque;
2046
Alexander Larsson4ea95522013-05-22 14:41:37 +02002047 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002048 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002049 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002050
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002051 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01002052 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002053 weston_surface_attach(surface, surface->pending.buffer);
2054
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002055 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01002056
2057 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02002058 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002059 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01002060
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002061 if (surface->pending.buffer)
2062 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2063 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002064 surface->pending.sx = 0;
2065 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002066 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002067
2068 /* wl_surface.damage */
2069 pixman_region32_union(&surface->damage, &surface->damage,
2070 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002071 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2072 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002073 surface->width,
2074 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002075 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002076
2077 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002078 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002079 surface->width,
2080 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002081 pixman_region32_intersect(&opaque,
2082 &opaque, &surface->pending.opaque);
2083
2084 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2085 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002086 wl_list_for_each(view, &surface->views, surface_link)
2087 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002088 }
2089
2090 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002091
2092 /* wl_surface.set_input_region */
2093 pixman_region32_fini(&surface->input);
2094 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002095 surface->width,
2096 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002097 pixman_region32_intersect(&surface->input,
2098 &surface->input, &surface->pending.input);
2099
Pekka Paalanenbc106382012-10-10 12:49:31 +03002100 /* wl_surface.frame */
2101 wl_list_insert_list(&surface->frame_callback_list,
2102 &surface->pending.frame_callback_list);
2103 wl_list_init(&surface->pending.frame_callback_list);
2104
Pekka Paalanene67858b2013-04-25 13:57:42 +03002105 weston_surface_commit_subsurface_order(surface);
2106
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002107 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002108}
2109
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002110static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002111weston_subsurface_commit(struct weston_subsurface *sub);
2112
2113static void
2114weston_subsurface_parent_commit(struct weston_subsurface *sub,
2115 int parent_is_synchronized);
2116
2117static void
2118surface_commit(struct wl_client *client, struct wl_resource *resource)
2119{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002120 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002121 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2122
2123 if (sub) {
2124 weston_subsurface_commit(sub);
2125 return;
2126 }
2127
2128 weston_surface_commit(surface);
2129
2130 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2131 if (sub->surface != surface)
2132 weston_subsurface_parent_commit(sub, 0);
2133 }
2134}
2135
2136static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002137surface_set_buffer_transform(struct wl_client *client,
2138 struct wl_resource *resource, int transform)
2139{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002140 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002141
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002142 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002143}
2144
Alexander Larsson4ea95522013-05-22 14:41:37 +02002145static void
2146surface_set_buffer_scale(struct wl_client *client,
2147 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002148 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002149{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002150 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002151
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002152 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002153}
2154
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002155static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002156 surface_destroy,
2157 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002158 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002159 surface_frame,
2160 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002161 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002162 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002163 surface_set_buffer_transform,
2164 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002165};
2166
2167static void
2168compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002169 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002170{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002171 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002172 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002173
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002174 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002175 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002176 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002177 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002178 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002179
Jason Ekstranda85118c2013-06-27 20:17:02 -05002180 surface->resource =
2181 wl_resource_create(client, &wl_surface_interface,
2182 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002183 if (surface->resource == NULL) {
2184 weston_surface_destroy(surface);
2185 wl_resource_post_no_memory(resource);
2186 return;
2187 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002188 wl_resource_set_implementation(surface->resource, &surface_interface,
2189 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002190}
2191
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002192static void
2193destroy_region(struct wl_resource *resource)
2194{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002195 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002196
2197 pixman_region32_fini(&region->region);
2198 free(region);
2199}
2200
2201static void
2202region_destroy(struct wl_client *client, struct wl_resource *resource)
2203{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002204 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002205}
2206
2207static void
2208region_add(struct wl_client *client, struct wl_resource *resource,
2209 int32_t x, int32_t y, int32_t width, int32_t height)
2210{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002211 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002212
2213 pixman_region32_union_rect(&region->region, &region->region,
2214 x, y, width, height);
2215}
2216
2217static void
2218region_subtract(struct wl_client *client, struct wl_resource *resource,
2219 int32_t x, int32_t y, int32_t width, int32_t height)
2220{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002221 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002222 pixman_region32_t rect;
2223
2224 pixman_region32_init_rect(&rect, x, y, width, height);
2225 pixman_region32_subtract(&region->region, &region->region, &rect);
2226 pixman_region32_fini(&rect);
2227}
2228
2229static const struct wl_region_interface region_interface = {
2230 region_destroy,
2231 region_add,
2232 region_subtract
2233};
2234
2235static void
2236compositor_create_region(struct wl_client *client,
2237 struct wl_resource *resource, uint32_t id)
2238{
2239 struct weston_region *region;
2240
2241 region = malloc(sizeof *region);
2242 if (region == NULL) {
2243 wl_resource_post_no_memory(resource);
2244 return;
2245 }
2246
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002247 pixman_region32_init(&region->region);
2248
Jason Ekstranda85118c2013-06-27 20:17:02 -05002249 region->resource =
2250 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002251 if (region->resource == NULL) {
2252 free(region);
2253 wl_resource_post_no_memory(resource);
2254 return;
2255 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002256 wl_resource_set_implementation(region->resource, &region_interface,
2257 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002258}
2259
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002260static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002261 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002262 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002263};
2264
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002265static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002266weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2267{
2268 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002269 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002270 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002271
Alexander Larsson4ea95522013-05-22 14:41:37 +02002272 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002273 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002274 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002275
Pekka Paalanene67858b2013-04-25 13:57:42 +03002276 /* wl_surface.attach */
2277 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2278 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2279 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2280
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002281 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002282
2283 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002284 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002285 sub->cached.sx = 0;
2286 sub->cached.sy = 0;
2287 sub->cached.newly_attached = 0;
2288
2289 /* wl_surface.damage */
2290 pixman_region32_union(&surface->damage, &surface->damage,
2291 &sub->cached.damage);
2292 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2293 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002294 surface->width,
2295 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002296 empty_region(&sub->cached.damage);
2297
2298 /* wl_surface.set_opaque_region */
2299 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002300 surface->width,
2301 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002302 pixman_region32_intersect(&opaque,
2303 &opaque, &sub->cached.opaque);
2304
2305 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2306 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002307 wl_list_for_each(view, &surface->views, surface_link)
2308 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002309 }
2310
2311 pixman_region32_fini(&opaque);
2312
2313 /* wl_surface.set_input_region */
2314 pixman_region32_fini(&surface->input);
2315 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002316 surface->width,
2317 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002318 pixman_region32_intersect(&surface->input,
2319 &surface->input, &sub->cached.input);
2320
2321 /* wl_surface.frame */
2322 wl_list_insert_list(&surface->frame_callback_list,
2323 &sub->cached.frame_callback_list);
2324 wl_list_init(&sub->cached.frame_callback_list);
2325
2326 weston_surface_commit_subsurface_order(surface);
2327
2328 weston_surface_schedule_repaint(surface);
2329
2330 sub->cached.has_data = 0;
2331}
2332
2333static void
2334weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2335{
2336 struct weston_surface *surface = sub->surface;
2337
2338 /*
2339 * If this commit would cause the surface to move by the
2340 * attach(dx, dy) parameters, the old damage region must be
2341 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002342 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002343 */
2344 pixman_region32_translate(&sub->cached.damage,
2345 -surface->pending.sx, -surface->pending.sy);
2346 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2347 &surface->pending.damage);
2348 empty_region(&surface->pending.damage);
2349
2350 if (surface->pending.newly_attached) {
2351 sub->cached.newly_attached = 1;
2352 weston_buffer_reference(&sub->cached.buffer_ref,
2353 surface->pending.buffer);
2354 }
2355 sub->cached.sx += surface->pending.sx;
2356 sub->cached.sy += surface->pending.sy;
2357 surface->pending.sx = 0;
2358 surface->pending.sy = 0;
2359 surface->pending.newly_attached = 0;
2360
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002361 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002362
2363 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2364
2365 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2366
2367 wl_list_insert_list(&sub->cached.frame_callback_list,
2368 &surface->pending.frame_callback_list);
2369 wl_list_init(&surface->pending.frame_callback_list);
2370
2371 sub->cached.has_data = 1;
2372}
2373
2374static int
2375weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2376{
2377 while (sub) {
2378 if (sub->synchronized)
2379 return 1;
2380
2381 if (!sub->parent)
2382 return 0;
2383
2384 sub = weston_surface_to_subsurface(sub->parent);
2385 }
2386
2387 return 0;
2388}
2389
2390static void
2391weston_subsurface_commit(struct weston_subsurface *sub)
2392{
2393 struct weston_surface *surface = sub->surface;
2394 struct weston_subsurface *tmp;
2395
2396 /* Recursive check for effectively synchronized. */
2397 if (weston_subsurface_is_synchronized(sub)) {
2398 weston_subsurface_commit_to_cache(sub);
2399 } else {
2400 if (sub->cached.has_data) {
2401 /* flush accumulated state from cache */
2402 weston_subsurface_commit_to_cache(sub);
2403 weston_subsurface_commit_from_cache(sub);
2404 } else {
2405 weston_surface_commit(surface);
2406 }
2407
2408 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2409 if (tmp->surface != surface)
2410 weston_subsurface_parent_commit(tmp, 0);
2411 }
2412 }
2413}
2414
2415static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002416weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002417{
2418 struct weston_surface *surface = sub->surface;
2419 struct weston_subsurface *tmp;
2420
Pekka Paalanene67858b2013-04-25 13:57:42 +03002421 /* From now on, commit_from_cache the whole sub-tree, regardless of
2422 * the synchronized mode of each child. This sub-surface or some
2423 * of its ancestors were synchronized, so we are synchronized
2424 * all the way down.
2425 */
2426
2427 if (sub->cached.has_data)
2428 weston_subsurface_commit_from_cache(sub);
2429
2430 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2431 if (tmp->surface != surface)
2432 weston_subsurface_parent_commit(tmp, 1);
2433 }
2434}
2435
2436static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002437weston_subsurface_parent_commit(struct weston_subsurface *sub,
2438 int parent_is_synchronized)
2439{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002440 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002441 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002442 wl_list_for_each(view, &sub->surface->views, surface_link)
2443 weston_view_set_position(view,
2444 sub->position.x,
2445 sub->position.y);
2446
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002447 sub->position.set = 0;
2448 }
2449
2450 if (parent_is_synchronized || sub->synchronized)
2451 weston_subsurface_synchronized_commit(sub);
2452}
2453
2454static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002455subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002456{
2457 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002458 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002459
Jason Ekstranda7af7042013-10-12 22:38:11 -05002460 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002461 weston_view_set_position(view,
2462 view->geometry.x + dx,
2463 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002464
2465 /* No need to check parent mappedness, because if parent is not
2466 * mapped, parent is not in a visible layer, so this sub-surface
2467 * will not be drawn either.
2468 */
2469 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002470 /* Cannot call weston_surface_update_transform(),
2471 * because that would call it also for the parent surface,
2472 * which might not be mapped yet. That would lead to
2473 * inconsistent state, where the window could never be
2474 * mapped.
2475 *
2476 * Instead just assing any output, to make
2477 * weston_surface_is_mapped() return true, so that when the
2478 * parent surface does get mapped, this one will get
2479 * included, too. See surface_list_add().
2480 */
2481 assert(!wl_list_empty(&compositor->output_list));
2482 surface->output = container_of(compositor->output_list.next,
2483 struct weston_output, link);
2484 }
2485}
2486
2487static struct weston_subsurface *
2488weston_surface_to_subsurface(struct weston_surface *surface)
2489{
2490 if (surface->configure == subsurface_configure)
2491 return surface->configure_private;
2492
2493 return NULL;
2494}
2495
Pekka Paalanen01388e22013-04-25 13:57:44 +03002496WL_EXPORT struct weston_surface *
2497weston_surface_get_main_surface(struct weston_surface *surface)
2498{
2499 struct weston_subsurface *sub;
2500
2501 while (surface && (sub = weston_surface_to_subsurface(surface)))
2502 surface = sub->parent;
2503
2504 return surface;
2505}
2506
Pekka Paalanene67858b2013-04-25 13:57:42 +03002507static void
2508subsurface_set_position(struct wl_client *client,
2509 struct wl_resource *resource, int32_t x, int32_t y)
2510{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002511 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512
2513 if (!sub)
2514 return;
2515
2516 sub->position.x = x;
2517 sub->position.y = y;
2518 sub->position.set = 1;
2519}
2520
2521static struct weston_subsurface *
2522subsurface_from_surface(struct weston_surface *surface)
2523{
2524 struct weston_subsurface *sub;
2525
2526 sub = weston_surface_to_subsurface(surface);
2527 if (sub)
2528 return sub;
2529
2530 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2531 if (sub->surface == surface)
2532 return sub;
2533
2534 return NULL;
2535}
2536
2537static struct weston_subsurface *
2538subsurface_sibling_check(struct weston_subsurface *sub,
2539 struct weston_surface *surface,
2540 const char *request)
2541{
2542 struct weston_subsurface *sibling;
2543
2544 sibling = subsurface_from_surface(surface);
2545
2546 if (!sibling) {
2547 wl_resource_post_error(sub->resource,
2548 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2549 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002550 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551 return NULL;
2552 }
2553
2554 if (sibling->parent != sub->parent) {
2555 wl_resource_post_error(sub->resource,
2556 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2557 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002558 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002559 return NULL;
2560 }
2561
2562 return sibling;
2563}
2564
2565static void
2566subsurface_place_above(struct wl_client *client,
2567 struct wl_resource *resource,
2568 struct wl_resource *sibling_resource)
2569{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002570 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002571 struct weston_surface *surface =
2572 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002573 struct weston_subsurface *sibling;
2574
2575 if (!sub)
2576 return;
2577
2578 sibling = subsurface_sibling_check(sub, surface, "place_above");
2579 if (!sibling)
2580 return;
2581
2582 wl_list_remove(&sub->parent_link_pending);
2583 wl_list_insert(sibling->parent_link_pending.prev,
2584 &sub->parent_link_pending);
2585}
2586
2587static void
2588subsurface_place_below(struct wl_client *client,
2589 struct wl_resource *resource,
2590 struct wl_resource *sibling_resource)
2591{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002592 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002593 struct weston_surface *surface =
2594 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002595 struct weston_subsurface *sibling;
2596
2597 if (!sub)
2598 return;
2599
2600 sibling = subsurface_sibling_check(sub, surface, "place_below");
2601 if (!sibling)
2602 return;
2603
2604 wl_list_remove(&sub->parent_link_pending);
2605 wl_list_insert(&sibling->parent_link_pending,
2606 &sub->parent_link_pending);
2607}
2608
2609static void
2610subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2611{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002612 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613
2614 if (sub)
2615 sub->synchronized = 1;
2616}
2617
2618static void
2619subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2620{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002621 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002622
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002623 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002624 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002625
2626 /* If sub became effectively desynchronized, flush. */
2627 if (!weston_subsurface_is_synchronized(sub))
2628 weston_subsurface_synchronized_commit(sub);
2629 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002630}
2631
2632static void
2633weston_subsurface_cache_init(struct weston_subsurface *sub)
2634{
2635 pixman_region32_init(&sub->cached.damage);
2636 pixman_region32_init(&sub->cached.opaque);
2637 pixman_region32_init(&sub->cached.input);
2638 wl_list_init(&sub->cached.frame_callback_list);
2639 sub->cached.buffer_ref.buffer = NULL;
2640}
2641
2642static void
2643weston_subsurface_cache_fini(struct weston_subsurface *sub)
2644{
2645 struct weston_frame_callback *cb, *tmp;
2646
2647 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002648 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002649
2650 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2651 pixman_region32_fini(&sub->cached.damage);
2652 pixman_region32_fini(&sub->cached.opaque);
2653 pixman_region32_fini(&sub->cached.input);
2654}
2655
2656static void
2657weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2658{
2659 wl_list_remove(&sub->parent_link);
2660 wl_list_remove(&sub->parent_link_pending);
2661 wl_list_remove(&sub->parent_destroy_listener.link);
2662 sub->parent = NULL;
2663}
2664
2665static void
2666weston_subsurface_destroy(struct weston_subsurface *sub);
2667
2668static void
2669subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2670{
2671 struct weston_subsurface *sub =
2672 container_of(listener, struct weston_subsurface,
2673 surface_destroy_listener);
2674 assert(data == &sub->surface->resource);
2675
2676 /* The protocol object (wl_resource) is left inert. */
2677 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002678 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002679
2680 weston_subsurface_destroy(sub);
2681}
2682
2683static void
2684subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2685{
2686 struct weston_subsurface *sub =
2687 container_of(listener, struct weston_subsurface,
2688 parent_destroy_listener);
2689 assert(data == &sub->parent->resource);
2690 assert(sub->surface != sub->parent);
2691
2692 if (weston_surface_is_mapped(sub->surface))
2693 weston_surface_unmap(sub->surface);
2694
2695 weston_subsurface_unlink_parent(sub);
2696}
2697
2698static void
2699subsurface_resource_destroy(struct wl_resource *resource)
2700{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002701 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002702
2703 if (sub)
2704 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002705}
2706
2707static void
2708subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2709{
2710 wl_resource_destroy(resource);
2711}
2712
2713static void
2714weston_subsurface_link_parent(struct weston_subsurface *sub,
2715 struct weston_surface *parent)
2716{
2717 sub->parent = parent;
2718 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002719 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002720 &sub->parent_destroy_listener);
2721
2722 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2723 wl_list_insert(&parent->subsurface_list_pending,
2724 &sub->parent_link_pending);
2725}
2726
2727static void
2728weston_subsurface_link_surface(struct weston_subsurface *sub,
2729 struct weston_surface *surface)
2730{
2731 sub->surface = surface;
2732 sub->surface_destroy_listener.notify =
2733 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002734 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002735 &sub->surface_destroy_listener);
2736}
2737
2738static void
2739weston_subsurface_destroy(struct weston_subsurface *sub)
2740{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002741 struct weston_view *view, *next;
2742
Pekka Paalanene67858b2013-04-25 13:57:42 +03002743 assert(sub->surface);
2744
2745 if (sub->resource) {
2746 assert(weston_surface_to_subsurface(sub->surface) == sub);
2747 assert(sub->parent_destroy_listener.notify ==
2748 subsurface_handle_parent_destroy);
2749
Jason Ekstranda7af7042013-10-12 22:38:11 -05002750 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2751 weston_view_destroy(view);
2752
Pekka Paalanene67858b2013-04-25 13:57:42 +03002753 if (sub->parent)
2754 weston_subsurface_unlink_parent(sub);
2755
2756 weston_subsurface_cache_fini(sub);
2757
2758 sub->surface->configure = NULL;
2759 sub->surface->configure_private = NULL;
2760 } else {
2761 /* the dummy weston_subsurface for the parent itself */
2762 assert(sub->parent_destroy_listener.notify == NULL);
2763 wl_list_remove(&sub->parent_link);
2764 wl_list_remove(&sub->parent_link_pending);
2765 }
2766
2767 wl_list_remove(&sub->surface_destroy_listener.link);
2768 free(sub);
2769}
2770
2771static const struct wl_subsurface_interface subsurface_implementation = {
2772 subsurface_destroy,
2773 subsurface_set_position,
2774 subsurface_place_above,
2775 subsurface_place_below,
2776 subsurface_set_sync,
2777 subsurface_set_desync
2778};
2779
2780static struct weston_subsurface *
2781weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2782 struct weston_surface *parent)
2783{
2784 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002785 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002786
2787 sub = calloc(1, sizeof *sub);
2788 if (!sub)
2789 return NULL;
2790
Jason Ekstranda7af7042013-10-12 22:38:11 -05002791 wl_list_init(&sub->unused_views);
2792
Jason Ekstranda85118c2013-06-27 20:17:02 -05002793 sub->resource =
2794 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002795 if (!sub->resource) {
2796 free(sub);
2797 return NULL;
2798 }
2799
Jason Ekstranda85118c2013-06-27 20:17:02 -05002800 wl_resource_set_implementation(sub->resource,
2801 &subsurface_implementation,
2802 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002803 weston_subsurface_link_surface(sub, surface);
2804 weston_subsurface_link_parent(sub, parent);
2805 weston_subsurface_cache_init(sub);
2806 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002807
2808 return sub;
2809}
2810
2811/* Create a dummy subsurface for having the parent itself in its
2812 * sub-surface lists. Makes stacking order manipulation easy.
2813 */
2814static struct weston_subsurface *
2815weston_subsurface_create_for_parent(struct weston_surface *parent)
2816{
2817 struct weston_subsurface *sub;
2818
2819 sub = calloc(1, sizeof *sub);
2820 if (!sub)
2821 return NULL;
2822
2823 weston_subsurface_link_surface(sub, parent);
2824 sub->parent = parent;
2825 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2826 wl_list_insert(&parent->subsurface_list_pending,
2827 &sub->parent_link_pending);
2828
2829 return sub;
2830}
2831
2832static void
2833subcompositor_get_subsurface(struct wl_client *client,
2834 struct wl_resource *resource,
2835 uint32_t id,
2836 struct wl_resource *surface_resource,
2837 struct wl_resource *parent_resource)
2838{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002839 struct weston_surface *surface =
2840 wl_resource_get_user_data(surface_resource);
2841 struct weston_surface *parent =
2842 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002843 struct weston_subsurface *sub;
2844 static const char where[] = "get_subsurface: wl_subsurface@";
2845
2846 if (surface == parent) {
2847 wl_resource_post_error(resource,
2848 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2849 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002850 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002851 return;
2852 }
2853
2854 if (weston_surface_to_subsurface(surface)) {
2855 wl_resource_post_error(resource,
2856 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2857 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002858 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002859 return;
2860 }
2861
2862 if (surface->configure) {
2863 wl_resource_post_error(resource,
2864 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2865 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002866 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002867 return;
2868 }
2869
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002870 if (weston_surface_get_main_surface(parent) == surface) {
2871 wl_resource_post_error(resource,
2872 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2873 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002874 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002875 return;
2876 }
2877
Pekka Paalanene67858b2013-04-25 13:57:42 +03002878 /* make sure the parent is in its own list */
2879 if (wl_list_empty(&parent->subsurface_list)) {
2880 if (!weston_subsurface_create_for_parent(parent)) {
2881 wl_resource_post_no_memory(resource);
2882 return;
2883 }
2884 }
2885
2886 sub = weston_subsurface_create(id, surface, parent);
2887 if (!sub) {
2888 wl_resource_post_no_memory(resource);
2889 return;
2890 }
2891
2892 surface->configure = subsurface_configure;
2893 surface->configure_private = sub;
2894}
2895
2896static void
2897subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2898{
2899 wl_resource_destroy(resource);
2900}
2901
2902static const struct wl_subcompositor_interface subcompositor_interface = {
2903 subcompositor_destroy,
2904 subcompositor_get_subsurface
2905};
2906
2907static void
2908bind_subcompositor(struct wl_client *client,
2909 void *data, uint32_t version, uint32_t id)
2910{
2911 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002912 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002913
Jason Ekstranda85118c2013-06-27 20:17:02 -05002914 resource =
2915 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002916 if (resource == NULL) {
2917 wl_client_post_no_memory(client);
2918 return;
2919 }
2920 wl_resource_set_implementation(resource, &subcompositor_interface,
2921 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002922}
2923
2924static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002925weston_compositor_dpms(struct weston_compositor *compositor,
2926 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002927{
2928 struct weston_output *output;
2929
2930 wl_list_for_each(output, &compositor->output_list, link)
2931 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002932 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002933}
2934
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002935WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002936weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002937{
Neil Roberts8b62e202013-09-30 13:14:47 +01002938 uint32_t old_state = compositor->state;
2939
2940 /* The state needs to be changed before emitting the wake
2941 * signal because that may try to schedule a repaint which
2942 * will not work if the compositor is still sleeping */
2943 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2944
2945 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002946 case WESTON_COMPOSITOR_SLEEPING:
2947 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2948 /* fall through */
2949 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002950 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002951 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002952 /* fall through */
2953 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002954 wl_event_source_timer_update(compositor->idle_source,
2955 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002956 }
2957}
2958
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002959WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002960weston_compositor_offscreen(struct weston_compositor *compositor)
2961{
2962 switch (compositor->state) {
2963 case WESTON_COMPOSITOR_OFFSCREEN:
2964 return;
2965 case WESTON_COMPOSITOR_SLEEPING:
2966 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2967 /* fall through */
2968 default:
2969 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2970 wl_event_source_timer_update(compositor->idle_source, 0);
2971 }
2972}
2973
2974WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002975weston_compositor_sleep(struct weston_compositor *compositor)
2976{
2977 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2978 return;
2979
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002980 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002981 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2982 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2983}
2984
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002985static int
2986idle_handler(void *data)
2987{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002988 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002989
2990 if (compositor->idle_inhibit)
2991 return 1;
2992
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002993 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002994 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002995
2996 return 1;
2997}
2998
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002999WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003000weston_plane_init(struct weston_plane *plane,
3001 struct weston_compositor *ec,
3002 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003003{
3004 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003005 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003006 plane->x = x;
3007 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003008 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003009
3010 /* Init the link so that the call to wl_list_remove() when releasing
3011 * the plane without ever stacking doesn't lead to a crash */
3012 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003013}
3014
3015WL_EXPORT void
3016weston_plane_release(struct weston_plane *plane)
3017{
Xiong Zhang97116532013-10-23 13:58:31 +08003018 struct weston_view *view;
3019
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003020 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003021 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003022
Xiong Zhang97116532013-10-23 13:58:31 +08003023 wl_list_for_each(view, &plane->compositor->view_list, link) {
3024 if (view->plane == plane)
3025 view->plane = NULL;
3026 }
3027
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003028 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003029}
3030
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003031WL_EXPORT void
3032weston_compositor_stack_plane(struct weston_compositor *ec,
3033 struct weston_plane *plane,
3034 struct weston_plane *above)
3035{
3036 if (above)
3037 wl_list_insert(above->link.prev, &plane->link);
3038 else
3039 wl_list_insert(&ec->plane_list, &plane->link);
3040}
3041
Casey Dahlin9074db52012-04-19 22:50:09 -04003042static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003043{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003044 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003045}
3046
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003047static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003048bind_output(struct wl_client *client,
3049 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003050{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003051 struct weston_output *output = data;
3052 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003053 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003054
Jason Ekstranda85118c2013-06-27 20:17:02 -05003055 resource = wl_resource_create(client, &wl_output_interface,
3056 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003057 if (resource == NULL) {
3058 wl_client_post_no_memory(client);
3059 return;
3060 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003061
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003062 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003063 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003064
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003065 wl_output_send_geometry(resource,
3066 output->x,
3067 output->y,
3068 output->mm_width,
3069 output->mm_height,
3070 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003071 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003072 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003073 if (version >= 2)
3074 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003075 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003076
3077 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003078 wl_output_send_mode(resource,
3079 mode->flags,
3080 mode->width,
3081 mode->height,
3082 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003083 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003084
3085 if (version >= 2)
3086 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003087}
3088
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003089/* Move other outputs when one is removed so the space remains contiguos. */
3090static void
3091weston_compositor_remove_output(struct weston_compositor *compositor,
3092 struct weston_output *remove_output)
3093{
3094 struct weston_output *output;
3095 int offset = 0;
3096
3097 wl_list_for_each(output, &compositor->output_list, link) {
3098 if (output == remove_output) {
3099 offset = output->width;
3100 continue;
3101 }
3102
3103 if (offset > 0) {
3104 weston_output_move(output,
3105 output->x - offset, output->y);
3106 output->dirty = 1;
3107 }
3108 }
3109}
3110
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003111static void
3112weston_compositor_verify_pointers(struct weston_compositor *ec)
3113{
3114 struct weston_seat *seat;
3115
3116 wl_list_for_each(seat, &ec->seat_list, link) {
3117 if (!seat->pointer)
3118 continue;
3119
3120 weston_pointer_verify(seat->pointer);
3121 }
3122}
3123
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003124WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003125weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003126{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003127 output->destroying = 1;
3128
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003129 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003130 wl_list_remove(&output->link);
3131
Ander Conselvan de Oliveira54e90c72013-12-13 22:10:56 +02003132 weston_compositor_verify_pointers(output->compositor);
3133
Richard Hughes64ddde12013-05-01 21:52:10 +01003134 wl_signal_emit(&output->destroy_signal, output);
3135
Richard Hughesafe690c2013-05-02 10:10:04 +01003136 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003137 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003138 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003139 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003140
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003141 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003142}
3143
Scott Moreau1bad5db2012-08-18 01:04:05 -06003144static void
3145weston_output_compute_transform(struct weston_output *output)
3146{
3147 struct weston_matrix transform;
3148 int flip;
3149
3150 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003151 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003152
3153 switch(output->transform) {
3154 case WL_OUTPUT_TRANSFORM_FLIPPED:
3155 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3156 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3157 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003158 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003159 flip = -1;
3160 break;
3161 default:
3162 flip = 1;
3163 break;
3164 }
3165
3166 switch(output->transform) {
3167 case WL_OUTPUT_TRANSFORM_NORMAL:
3168 case WL_OUTPUT_TRANSFORM_FLIPPED:
3169 transform.d[0] = flip;
3170 transform.d[1] = 0;
3171 transform.d[4] = 0;
3172 transform.d[5] = 1;
3173 break;
3174 case WL_OUTPUT_TRANSFORM_90:
3175 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3176 transform.d[0] = 0;
3177 transform.d[1] = -flip;
3178 transform.d[4] = 1;
3179 transform.d[5] = 0;
3180 break;
3181 case WL_OUTPUT_TRANSFORM_180:
3182 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3183 transform.d[0] = -flip;
3184 transform.d[1] = 0;
3185 transform.d[4] = 0;
3186 transform.d[5] = -1;
3187 break;
3188 case WL_OUTPUT_TRANSFORM_270:
3189 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3190 transform.d[0] = 0;
3191 transform.d[1] = flip;
3192 transform.d[4] = -1;
3193 transform.d[5] = 0;
3194 break;
3195 default:
3196 break;
3197 }
3198
3199 weston_matrix_multiply(&output->matrix, &transform);
3200}
3201
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003202WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003203weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003204{
Scott Moreau850ca422012-05-21 15:21:25 -06003205 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003206 struct weston_matrix camera;
3207 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003208
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003209 weston_matrix_init(&output->matrix);
3210 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003211 -(output->x + output->width / 2.0),
3212 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003213
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003214 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003215 2.0 / output->width,
3216 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003217
3218 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003219
Scott Moreauccbf29d2012-02-22 14:21:41 -07003220 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003221 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003222 weston_matrix_init(&camera);
3223 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003224 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003225 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003226 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003227 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003228 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003229 weston_matrix_multiply(&output->matrix, &modelview);
3230 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003231
Scott Moreauccbf29d2012-02-22 14:21:41 -07003232 output->dirty = 0;
3233}
3234
Scott Moreau1bad5db2012-08-18 01:04:05 -06003235static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003236weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003237{
3238 output->transform = transform;
3239
3240 switch (transform) {
3241 case WL_OUTPUT_TRANSFORM_90:
3242 case WL_OUTPUT_TRANSFORM_270:
3243 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3244 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3245 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003246 output->width = output->current_mode->height;
3247 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003248 break;
3249 case WL_OUTPUT_TRANSFORM_NORMAL:
3250 case WL_OUTPUT_TRANSFORM_180:
3251 case WL_OUTPUT_TRANSFORM_FLIPPED:
3252 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003253 output->width = output->current_mode->width;
3254 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003255 break;
3256 default:
3257 break;
3258 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003259
Hardening57388e42013-09-18 23:56:36 +02003260 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003261 output->width /= scale;
3262 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003263}
3264
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003265static void
3266weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003267{
3268 output->x = x;
3269 output->y = y;
3270
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003271 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003272 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003273 output->width,
3274 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003275}
3276
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003277WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003278weston_output_move(struct weston_output *output, int x, int y)
3279{
3280 pixman_region32_t old_region;
3281 struct wl_resource *resource;
3282
3283 output->move_x = x - output->x;
3284 output->move_y = y - output->y;
3285
3286 if (output->move_x == 0 && output->move_y == 0)
3287 return;
3288
3289 pixman_region32_init(&old_region);
3290 pixman_region32_copy(&old_region, &output->region);
3291
3292 weston_output_init_geometry(output, x, y);
3293
3294 output->dirty = 1;
3295
3296 /* Move views on this output. */
3297 wl_signal_emit(&output->move_signal, output);
3298
3299 /* Notify clients of the change for output position. */
3300 wl_resource_for_each(resource, &output->resource_list)
3301 wl_output_send_geometry(resource,
3302 output->x,
3303 output->y,
3304 output->mm_width,
3305 output->mm_height,
3306 output->subpixel,
3307 output->make,
3308 output->model,
3309 output->transform);
3310}
3311
3312WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003313weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003314 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003315 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003316{
3317 output->compositor = c;
3318 output->x = x;
3319 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003320 output->mm_width = mm_width;
3321 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003322 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003323 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003324
Alexander Larsson0b135062013-05-28 16:23:36 +02003325 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003326 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003327
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003328 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003329 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003330
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003331 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003332 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003333 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003334 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003335 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003336
Casey Dahlin58ba1372012-04-19 22:50:08 -04003337 output->id = ffs(~output->compositor->output_id_pool) - 1;
3338 output->compositor->output_id_pool |= 1 << output->id;
3339
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003340 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003341 wl_global_create(c->wl_display, &wl_output_interface, 2,
3342 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003343 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003344}
3345
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003346WL_EXPORT void
3347weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003348 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003349 wl_fixed_t *x, wl_fixed_t *y)
3350{
3351 wl_fixed_t tx, ty;
3352 wl_fixed_t width, height;
3353
Hardeningff39efa2013-09-18 23:56:35 +02003354 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3355 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003356
3357 switch(output->transform) {
3358 case WL_OUTPUT_TRANSFORM_NORMAL:
3359 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003360 tx = device_x;
3361 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003362 break;
3363 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003364 tx = device_y;
3365 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003366 break;
3367 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003368 tx = width - device_x;
3369 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003370 break;
3371 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003372 tx = width - device_y;
3373 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003374 break;
3375 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003376 tx = width - device_x;
3377 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003378 break;
3379 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003380 tx = width - device_y;
3381 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003382 break;
3383 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003384 tx = device_x;
3385 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003386 break;
3387 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003388 tx = device_y;
3389 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003390 break;
3391 }
3392
Hardeningff39efa2013-09-18 23:56:35 +02003393 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3394 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003395}
3396
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003397static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003398compositor_bind(struct wl_client *client,
3399 void *data, uint32_t version, uint32_t id)
3400{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003401 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003402 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003403
Jason Ekstranda85118c2013-06-27 20:17:02 -05003404 resource = wl_resource_create(client, &wl_compositor_interface,
3405 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003406 if (resource == NULL) {
3407 wl_client_post_no_memory(client);
3408 return;
3409 }
3410
3411 wl_resource_set_implementation(resource, &compositor_interface,
3412 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003413}
3414
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003415static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003416log_uname(void)
3417{
3418 struct utsname usys;
3419
3420 uname(&usys);
3421
3422 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3423 usys.version, usys.machine);
3424}
3425
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003426WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003427weston_environment_get_fd(const char *env)
3428{
3429 char *e, *end;
3430 int fd, flags;
3431
3432 e = getenv(env);
3433 if (!e)
3434 return -1;
3435 fd = strtol(e, &end, 0);
3436 if (*end != '\0')
3437 return -1;
3438
3439 flags = fcntl(fd, F_GETFD);
3440 if (flags == -1)
3441 return -1;
3442
3443 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3444 unsetenv(env);
3445
3446 return fd;
3447}
3448
3449WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003450weston_compositor_init(struct weston_compositor *ec,
3451 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003452 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003453 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003454{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003455 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003456 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003457 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003458
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003459 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003460 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003461 wl_signal_init(&ec->destroy_signal);
3462 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003463 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003464 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003465 wl_signal_init(&ec->idle_signal);
3466 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003467 wl_signal_init(&ec->show_input_panel_signal);
3468 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003469 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003470 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003471 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003472 wl_signal_init(&ec->session_signal);
3473 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003474
Casey Dahlin58ba1372012-04-19 22:50:08 -04003475 ec->output_id_pool = 0;
3476
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003477 if (!wl_global_create(display, &wl_compositor_interface, 3,
3478 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003479 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003480
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003481 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3482 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003483 return -1;
3484
Jason Ekstranda7af7042013-10-12 22:38:11 -05003485 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003486 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003487 wl_list_init(&ec->layer_list);
3488 wl_list_init(&ec->seat_list);
3489 wl_list_init(&ec->output_list);
3490 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003491 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003492 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003493 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003494 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003495 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003496
Xiong Zhang97116532013-10-23 13:58:31 +08003497 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003498 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003499
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003500 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3501 weston_config_section_get_string(s, "keymap_rules",
3502 (char **) &xkb_names.rules, NULL);
3503 weston_config_section_get_string(s, "keymap_model",
3504 (char **) &xkb_names.model, NULL);
3505 weston_config_section_get_string(s, "keymap_layout",
3506 (char **) &xkb_names.layout, NULL);
3507 weston_config_section_get_string(s, "keymap_variant",
3508 (char **) &xkb_names.variant, NULL);
3509 weston_config_section_get_string(s, "keymap_options",
3510 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003511
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003512 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3513 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003514
3515 ec->ping_handler = NULL;
3516
3517 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003518 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003519
3520 wl_data_device_manager_init(ec->wl_display);
3521
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003522 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003523
Daniel Stone725c2c32012-06-22 14:04:36 +01003524 loop = wl_display_get_event_loop(ec->wl_display);
3525 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3526 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3527
3528 ec->input_loop = wl_event_loop_create();
3529
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003530 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3531 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3532
3533 weston_compositor_schedule_repaint(ec);
3534
Daniel Stone725c2c32012-06-22 14:04:36 +01003535 return 0;
3536}
3537
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003538WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003539weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003540{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003541 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003542
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003543 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003544 if (ec->input_loop_source)
3545 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003546
Matt Roper361d2ad2011-08-29 13:52:23 -07003547 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003548 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003549 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003550
Daniel Stone325fc2d2012-05-30 16:31:58 +01003551 weston_binding_list_destroy_all(&ec->key_binding_list);
3552 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003553 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003554 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003555 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003556
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003557 weston_plane_release(&ec->primary_plane);
3558
Jonas Ådahlc97af922012-03-28 22:36:09 +02003559 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003560
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003561 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003562}
3563
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003564WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003565weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3566 const struct weston_pointer_grab_interface *interface)
3567{
3568 struct weston_seat *seat;
3569
3570 ec->default_pointer_grab = interface;
3571 wl_list_for_each(seat, &ec->seat_list, link) {
3572 if (seat->pointer) {
3573 weston_pointer_set_default_grab(seat->pointer,
3574 interface);
3575 }
3576 }
3577}
3578
3579WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003580weston_version(int *major, int *minor, int *micro)
3581{
3582 *major = WESTON_VERSION_MAJOR;
3583 *minor = WESTON_VERSION_MINOR;
3584 *micro = WESTON_VERSION_MICRO;
3585}
3586
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003587static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003588 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003589 const char *desc;
3590} capability_strings[] = {
3591 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003592 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003593};
3594
3595static void
3596weston_compositor_log_capabilities(struct weston_compositor *compositor)
3597{
3598 unsigned i;
3599 int yes;
3600
3601 weston_log("Compositor capabilities:\n");
3602 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3603 yes = compositor->capabilities & capability_strings[i].bit;
3604 weston_log_continue(STAMP_SPACE "%s %s\n",
3605 capability_strings[i].desc,
3606 yes ? "yes" : "no");
3607 }
3608}
3609
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003610static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003611{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003612 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003613
Martin Minarik6d118362012-06-07 18:01:59 +02003614 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003615 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003616
3617 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003618}
3619
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003620#ifdef HAVE_LIBUNWIND
3621
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003622static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003623print_backtrace(void)
3624{
3625 unw_cursor_t cursor;
3626 unw_context_t context;
3627 unw_word_t off;
3628 unw_proc_info_t pip;
3629 int ret, i = 0;
3630 char procname[256];
3631 const char *filename;
3632 Dl_info dlinfo;
3633
3634 pip.unwind_info = NULL;
3635 ret = unw_getcontext(&context);
3636 if (ret) {
3637 weston_log("unw_getcontext: %d\n", ret);
3638 return;
3639 }
3640
3641 ret = unw_init_local(&cursor, &context);
3642 if (ret) {
3643 weston_log("unw_init_local: %d\n", ret);
3644 return;
3645 }
3646
3647 ret = unw_step(&cursor);
3648 while (ret > 0) {
3649 ret = unw_get_proc_info(&cursor, &pip);
3650 if (ret) {
3651 weston_log("unw_get_proc_info: %d\n", ret);
3652 break;
3653 }
3654
3655 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3656 if (ret && ret != -UNW_ENOMEM) {
3657 if (ret != -UNW_EUNSPEC)
3658 weston_log("unw_get_proc_name: %d\n", ret);
3659 procname[0] = '?';
3660 procname[1] = 0;
3661 }
3662
3663 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3664 *dlinfo.dli_fname)
3665 filename = dlinfo.dli_fname;
3666 else
3667 filename = "?";
3668
3669 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3670 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3671
3672 ret = unw_step(&cursor);
3673 if (ret < 0)
3674 weston_log("unw_step: %d\n", ret);
3675 }
3676}
3677
3678#else
3679
3680static void
3681print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003682{
3683 void *buffer[32];
3684 int i, count;
3685 Dl_info info;
3686
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003687 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3688 for (i = 0; i < count; i++) {
3689 dladdr(buffer[i], &info);
3690 weston_log(" [%016lx] %s (%s)\n",
3691 (long) buffer[i],
3692 info.dli_sname ? info.dli_sname : "--",
3693 info.dli_fname);
3694 }
3695}
3696
3697#endif
3698
3699static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003700on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003701{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003702 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003703 * then call the backend restore function, which will switch
3704 * back to the vt we launched from or ungrab X etc and then
3705 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003706 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003707 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003708 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003709
Peter Maatmane5b42e42013-03-27 22:38:53 +01003710 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003711
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003712 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003713
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003714 segv_compositor->restore(segv_compositor);
3715
3716 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003717}
3718
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003719WL_EXPORT void *
3720weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003721{
3722 char path[PATH_MAX];
3723 void *module, *init;
3724
3725 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003726 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003727 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003728 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003729
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003730 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3731 if (module) {
3732 weston_log("Module '%s' already loaded\n", path);
3733 dlclose(module);
3734 return NULL;
3735 }
3736
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003737 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003738 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003739 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003740 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003741 return NULL;
3742 }
3743
3744 init = dlsym(module, entrypoint);
3745 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003746 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003747 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003748 return NULL;
3749 }
3750
3751 return init;
3752}
3753
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003754static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003755load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003756 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003757{
3758 const char *p, *end;
3759 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003760 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003761 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003762
3763 if (modules == NULL)
3764 return 0;
3765
3766 p = modules;
3767 while (*p) {
3768 end = strchrnul(p, ',');
3769 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003770 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003771 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003772 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003773 p = end;
3774 while (*p == ',')
3775 p++;
3776
3777 }
3778
3779 return 0;
3780}
3781
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003782static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003783 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3784
3785static const char xdg_wrong_message[] =
3786 "fatal: environment variable XDG_RUNTIME_DIR\n"
3787 "is set to \"%s\", which is not a directory.\n";
3788
3789static const char xdg_wrong_mode_message[] =
3790 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3791 "correctly. Unix access mode must be 0700 but is %o,\n"
3792 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003793 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003794
3795static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003796 "Refer to your distribution on how to get it, or\n"
3797 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3798 "on how to implement it.\n";
3799
Martin Minarik37032f82012-06-18 20:15:18 +02003800static void
3801verify_xdg_runtime_dir(void)
3802{
3803 char *dir = getenv("XDG_RUNTIME_DIR");
3804 struct stat s;
3805
3806 if (!dir) {
3807 weston_log(xdg_error_message);
3808 weston_log_continue(xdg_detail_message);
3809 exit(EXIT_FAILURE);
3810 }
3811
3812 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3813 weston_log(xdg_wrong_message, dir);
3814 weston_log_continue(xdg_detail_message);
3815 exit(EXIT_FAILURE);
3816 }
3817
3818 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3819 weston_log(xdg_wrong_mode_message,
3820 dir, s.st_mode & 0777, s.st_uid);
3821 weston_log_continue(xdg_detail_message);
3822 }
3823}
3824
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003825static int
3826usage(int error_code)
3827{
3828 fprintf(stderr,
3829 "Usage: weston [OPTIONS]\n\n"
3830 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3831 "Weston supports multiple backends, and depending on which backend is in use\n"
3832 "different options will be accepted.\n\n"
3833
3834
3835 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003836 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003837 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003838 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3839 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003840 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003841 " -S, --socket=NAME\tName of socket to listen on\n"
3842 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003843 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003844 " --log==FILE\t\tLog to the given file\n"
3845 " -h, --help\t\tThis help message\n\n");
3846
3847 fprintf(stderr,
3848 "Options for drm-backend.so:\n\n"
3849 " --connector=ID\tBring up only this connector\n"
3850 " --seat=SEAT\t\tThe seat that weston should run on\n"
3851 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003852 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003853 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3854
3855 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003856 "Options for fbdev-backend.so:\n\n"
3857 " --tty=TTY\t\tThe tty to use\n"
3858 " --device=DEVICE\tThe framebuffer device to use\n\n");
3859
3860 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003861 "Options for x11-backend.so:\n\n"
3862 " --width=WIDTH\t\tWidth of X window\n"
3863 " --height=HEIGHT\tHeight of X window\n"
3864 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003865 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003866 " --output-count=COUNT\tCreate multiple outputs\n"
3867 " --no-input\t\tDont create input devices\n\n");
3868
3869 fprintf(stderr,
3870 "Options for wayland-backend.so:\n\n"
3871 " --width=WIDTH\t\tWidth of Wayland surface\n"
3872 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003873 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003874 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003875 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003876 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003877 " --display=DISPLAY\tWayland display to connect to\n\n");
3878
Pekka Paalanene31e0532013-05-22 18:03:07 +03003879#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3880 fprintf(stderr,
3881 "Options for rpi-backend.so:\n\n"
3882 " --tty=TTY\t\tThe tty to use\n"
3883 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3884 " --transform=TR\tThe output transformation, TR is one of:\n"
3885 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003886 " --opaque-regions\tEnable support for opaque regions, can be "
3887 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003888 "\n");
3889#endif
3890
Hardeningc077a842013-07-08 00:51:35 +02003891#if defined(BUILD_RDP_COMPOSITOR)
3892 fprintf(stderr,
3893 "Options for rdp-backend.so:\n\n"
3894 " --width=WIDTH\t\tWidth of desktop\n"
3895 " --height=HEIGHT\tHeight of desktop\n"
3896 " --extra-modes=MODES\t\n"
3897 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3898 " --address=ADDR\tThe address to bind\n"
3899 " --port=PORT\tThe port to listen on\n"
3900 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3901 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3902 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3903 "\n");
3904#endif
3905
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003906 exit(error_code);
3907}
3908
Peter Maatmane5b42e42013-03-27 22:38:53 +01003909static void
3910catch_signals(void)
3911{
3912 struct sigaction action;
3913
3914 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3915 action.sa_sigaction = on_caught_signal;
3916 sigemptyset(&action.sa_mask);
3917 sigaction(SIGSEGV, &action, NULL);
3918 sigaction(SIGABRT, &action, NULL);
3919}
3920
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003921int main(int argc, char *argv[])
3922{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003923 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003924 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003925 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003926 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003927 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003928 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003929 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003930 int *argc, char *argv[],
3931 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003932 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003933 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003934 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003935 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003936 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003937 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003938 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003939 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003940 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003941 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003942 struct weston_config *config;
3943 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003944
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003945 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003946 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003947 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003948 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3949 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003950 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003951 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003952 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003953 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003954 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003955
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003956 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003957
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003958 if (help)
3959 usage(EXIT_SUCCESS);
3960
Scott Moreau12245142012-08-29 15:15:58 -06003961 if (version) {
3962 printf(PACKAGE_STRING "\n");
3963 return EXIT_SUCCESS;
3964 }
3965
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003966 weston_log_file_open(log);
3967
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003968 weston_log("%s\n"
3969 STAMP_SPACE "%s\n"
3970 STAMP_SPACE "Bug reports to: %s\n"
3971 STAMP_SPACE "Build: %s\n",
3972 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003973 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003974 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003975
Martin Minarik37032f82012-06-18 20:15:18 +02003976 verify_xdg_runtime_dir();
3977
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003978 display = wl_display_create();
3979
Tiago Vignatti2116b892011-08-08 05:52:59 -07003980 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003981 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3982 display);
3983 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3984 display);
3985 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3986 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003987
3988 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003989 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3990 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003991
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003992 config = weston_config_parse("weston.ini");
3993 if (config != NULL) {
3994 weston_log("Using config file '%s'\n",
3995 weston_config_get_full_path(config));
3996 } else {
3997 weston_log("Starting with no config file.\n");
3998 }
3999 section = weston_config_get_section(config, "core", NULL, NULL);
4000
4001 weston_config_section_get_string(section, "backend", &backend, NULL);
4002 if (option_backend) {
4003 backend = option_backend;
4004 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004005 if (!backend) {
4006 if (getenv("WAYLAND_DISPLAY"))
4007 backend = "wayland-backend.so";
4008 else if (getenv("DISPLAY"))
4009 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004010 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02004011 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004012 }
4013
Jason Ekstranda985da42013-08-22 17:28:03 -05004014 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004015
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004016 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004017 if (!backend_init)
4018 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004019
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004020 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004021 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004022 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004023 exit(EXIT_FAILURE);
4024 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004025
Peter Maatmane5b42e42013-03-27 22:38:53 +01004026 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004027 segv_compositor = ec;
4028
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004029 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004030 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004031
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004032 setenv("WAYLAND_DISPLAY", socket_name, 1);
4033
Jason Ekstranda985da42013-08-22 17:28:03 -05004034 if (!shell)
4035 weston_config_section_get_string(section, "shell", &shell,
4036 "desktop-shell.so");
4037 if (load_modules(ec, shell, &argc, argv) < 0)
4038 goto out;
4039
Ossama Othmana50e6e42013-05-14 09:48:26 -07004040 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004041 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004042 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004043 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004044
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004045 for (i = 1; i < argc; i++)
4046 weston_log("fatal: unhandled option: %s\n", argv[i]);
4047 if (argc > 1) {
4048 ret = EXIT_FAILURE;
4049 goto out;
4050 }
4051
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004052 weston_compositor_log_capabilities(ec);
4053
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004054 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004055 weston_log("fatal: failed to add socket: %m\n");
4056 ret = EXIT_FAILURE;
4057 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004058 }
4059
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004060 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004061
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004062 wl_display_run(display);
4063
4064 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004065 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004066 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004067
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004068 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004069
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004070 for (i = ARRAY_LENGTH(signals); i;)
4071 wl_event_source_remove(signals[--i]);
4072
Daniel Stone855028f2012-05-01 20:37:10 +01004073 weston_compositor_xkb_destroy(ec);
4074
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004075 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004076 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004077
Martin Minarik19e6f262012-06-07 13:08:46 +02004078 weston_log_file_close();
4079
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004080 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004081}