blob: 2f2c8389727504b80fa881106f3e487d88a6fd45 [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
Jason Ekstranda7af7042013-10-12 22:38:11 -0500368WL_EXPORT struct weston_view *
369weston_view_create(struct weston_surface *surface)
370{
371 struct weston_view *view;
372
373 view = calloc(1, sizeof *view);
374 if (view == NULL)
375 return NULL;
376
377 view->surface = surface;
378
Jason Ekstranda7af7042013-10-12 22:38:11 -0500379 /* Assign to surface */
380 wl_list_insert(&surface->views, &view->surface_link);
381
382 wl_signal_init(&view->destroy_signal);
383 wl_list_init(&view->link);
384 wl_list_init(&view->layer_link);
385
Xiong Zhang97116532013-10-23 13:58:31 +0800386 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500387
388 pixman_region32_init(&view->clip);
389
390 view->alpha = 1.0;
391 pixman_region32_init(&view->transform.opaque);
392
393 wl_list_init(&view->geometry.transformation_list);
394 wl_list_insert(&view->geometry.transformation_list,
395 &view->transform.position.link);
396 weston_matrix_init(&view->transform.position.matrix);
397 wl_list_init(&view->geometry.child_list);
398 pixman_region32_init(&view->transform.boundingbox);
399 view->transform.dirty = 1;
400
401 view->output = NULL;
402
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200403 view->output_move_listener.notify = weston_view_output_move_handler;
404
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 return view;
406}
407
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500408WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500409weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500410{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500411 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400412
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200413 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400414 if (surface == NULL)
415 return NULL;
416
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500417 wl_signal_init(&surface->destroy_signal);
418
419 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500420
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500421 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200422 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400423
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100424 surface->buffer_viewport.transform = WL_OUTPUT_TRANSFORM_NORMAL;
425 surface->buffer_viewport.scale = 1;
426 surface->pending.buffer_viewport = surface->buffer_viewport;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400427 surface->output = NULL;
Giulio Camuffo184df502013-02-21 11:29:21 +0100428 surface->pending.newly_attached = 0;
Benjamin Franzke06286262011-05-06 19:12:33 +0200429
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400430 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500431 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300432 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400433
Jason Ekstranda7af7042013-10-12 22:38:11 -0500434 wl_list_init(&surface->views);
435
436 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500437
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300438 surface->pending.buffer_destroy_listener.notify =
439 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300440 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300441 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300442 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300443 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300444
Pekka Paalanene67858b2013-04-25 13:57:42 +0300445 wl_list_init(&surface->subsurface_list);
446 wl_list_init(&surface->subsurface_list_pending);
447
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400448 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500449}
450
Alex Wu8811bf92012-02-28 18:07:54 +0800451WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500452weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200453 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500454{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100455 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500456}
457
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400458WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500459weston_view_to_global_float(struct weston_view *view,
460 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200461{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500462 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200463 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
464
Jason Ekstranda7af7042013-10-12 22:38:11 -0500465 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200466
467 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200468 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700469 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200470 v.f[3]);
471 *x = 0;
472 *y = 0;
473 return;
474 }
475
476 *x = v.f[0] / v.f[3];
477 *y = v.f[1] / v.f[3];
478 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500479 *x = sx + view->geometry.x;
480 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200481 }
482}
483
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500484WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200485weston_transformed_coord(int width, int height,
486 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200487 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200488 float sx, float sy, float *bx, float *by)
489{
490 switch (transform) {
491 case WL_OUTPUT_TRANSFORM_NORMAL:
492 default:
493 *bx = sx;
494 *by = sy;
495 break;
496 case WL_OUTPUT_TRANSFORM_FLIPPED:
497 *bx = width - sx;
498 *by = sy;
499 break;
500 case WL_OUTPUT_TRANSFORM_90:
501 *bx = height - sy;
502 *by = sx;
503 break;
504 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
505 *bx = height - sy;
506 *by = width - sx;
507 break;
508 case WL_OUTPUT_TRANSFORM_180:
509 *bx = width - sx;
510 *by = height - sy;
511 break;
512 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
513 *bx = sx;
514 *by = height - sy;
515 break;
516 case WL_OUTPUT_TRANSFORM_270:
517 *bx = sy;
518 *by = width - sx;
519 break;
520 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
521 *bx = sy;
522 *by = sx;
523 break;
524 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200525
526 *bx *= scale;
527 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200528}
529
530WL_EXPORT pixman_box32_t
531weston_transformed_rect(int width, int height,
532 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200533 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200534 pixman_box32_t rect)
535{
536 float x1, x2, y1, y2;
537
538 pixman_box32_t ret;
539
Alexander Larsson4ea95522013-05-22 14:41:37 +0200540 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200541 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200542 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200543 rect.x2, rect.y2, &x2, &y2);
544
545 if (x1 <= x2) {
546 ret.x1 = x1;
547 ret.x2 = x2;
548 } else {
549 ret.x1 = x2;
550 ret.x2 = x1;
551 }
552
553 if (y1 <= y2) {
554 ret.y1 = y1;
555 ret.y2 = y2;
556 } else {
557 ret.y1 = y2;
558 ret.y2 = y1;
559 }
560
561 return ret;
562}
563
564WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500565weston_transformed_region(int width, int height,
566 enum wl_output_transform transform,
567 int32_t scale,
568 pixman_region32_t *src, pixman_region32_t *dest)
569{
570 pixman_box32_t *src_rects, *dest_rects;
571 int nrects, i;
572
573 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
574 if (src != dest)
575 pixman_region32_copy(dest, src);
576 return;
577 }
578
579 src_rects = pixman_region32_rectangles(src, &nrects);
580 dest_rects = malloc(nrects * sizeof(*dest_rects));
581 if (!dest_rects)
582 return;
583
584 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
585 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
586 } else {
587 for (i = 0; i < nrects; i++) {
588 switch (transform) {
589 default:
590 case WL_OUTPUT_TRANSFORM_NORMAL:
591 dest_rects[i].x1 = src_rects[i].x1;
592 dest_rects[i].y1 = src_rects[i].y1;
593 dest_rects[i].x2 = src_rects[i].x2;
594 dest_rects[i].y2 = src_rects[i].y2;
595 break;
596 case WL_OUTPUT_TRANSFORM_90:
597 dest_rects[i].x1 = height - src_rects[i].y2;
598 dest_rects[i].y1 = src_rects[i].x1;
599 dest_rects[i].x2 = height - src_rects[i].y1;
600 dest_rects[i].y2 = src_rects[i].x2;
601 break;
602 case WL_OUTPUT_TRANSFORM_180:
603 dest_rects[i].x1 = width - src_rects[i].x2;
604 dest_rects[i].y1 = height - src_rects[i].y2;
605 dest_rects[i].x2 = width - src_rects[i].x1;
606 dest_rects[i].y2 = height - src_rects[i].y1;
607 break;
608 case WL_OUTPUT_TRANSFORM_270:
609 dest_rects[i].x1 = src_rects[i].y1;
610 dest_rects[i].y1 = width - src_rects[i].x2;
611 dest_rects[i].x2 = src_rects[i].y2;
612 dest_rects[i].y2 = width - src_rects[i].x1;
613 break;
614 case WL_OUTPUT_TRANSFORM_FLIPPED:
615 dest_rects[i].x1 = width - src_rects[i].x2;
616 dest_rects[i].y1 = src_rects[i].y1;
617 dest_rects[i].x2 = width - src_rects[i].x1;
618 dest_rects[i].y2 = src_rects[i].y2;
619 break;
620 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
621 dest_rects[i].x1 = height - src_rects[i].y2;
622 dest_rects[i].y1 = width - src_rects[i].x2;
623 dest_rects[i].x2 = height - src_rects[i].y1;
624 dest_rects[i].y2 = width - src_rects[i].x1;
625 break;
626 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
627 dest_rects[i].x1 = src_rects[i].x1;
628 dest_rects[i].y1 = height - src_rects[i].y2;
629 dest_rects[i].x2 = src_rects[i].x2;
630 dest_rects[i].y2 = height - src_rects[i].y1;
631 break;
632 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
633 dest_rects[i].x1 = src_rects[i].y1;
634 dest_rects[i].y1 = src_rects[i].x1;
635 dest_rects[i].x2 = src_rects[i].y2;
636 dest_rects[i].y2 = src_rects[i].x2;
637 break;
638 }
639 }
640 }
641
642 if (scale != 1) {
643 for (i = 0; i < nrects; i++) {
644 dest_rects[i].x1 *= scale;
645 dest_rects[i].x2 *= scale;
646 dest_rects[i].y1 *= scale;
647 dest_rects[i].y2 *= scale;
648 }
649 }
650
651 pixman_region32_clear(dest);
652 pixman_region32_init_rects(dest, dest_rects, nrects);
653 free(dest_rects);
654}
655
656WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200657weston_surface_to_buffer_float(struct weston_surface *surface,
658 float sx, float sy, float *bx, float *by)
659{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500660 weston_transformed_coord(surface->width,
661 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100662 surface->buffer_viewport.transform,
663 surface->buffer_viewport.scale,
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200664 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200665}
666
Alexander Larsson4ea95522013-05-22 14:41:37 +0200667WL_EXPORT void
668weston_surface_to_buffer(struct weston_surface *surface,
669 int sx, int sy, int *bx, int *by)
670{
671 float bxf, byf;
672
Jason Ekstranda7af7042013-10-12 22:38:11 -0500673 weston_transformed_coord(surface->width,
674 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100675 surface->buffer_viewport.transform,
676 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200677 sx, sy, &bxf, &byf);
678 *bx = floorf(bxf);
679 *by = floorf(byf);
680}
681
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200682WL_EXPORT pixman_box32_t
683weston_surface_to_buffer_rect(struct weston_surface *surface,
684 pixman_box32_t rect)
685{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500686 return weston_transformed_rect(surface->width,
687 surface->height,
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +0100688 surface->buffer_viewport.transform,
689 surface->buffer_viewport.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200690 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200691}
692
693WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500694weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400695 struct weston_plane *plane)
696{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500697 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400698 return;
699
Jason Ekstranda7af7042013-10-12 22:38:11 -0500700 weston_view_damage_below(view);
701 view->plane = plane;
702 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400703}
704
705WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500706weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200707{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500708 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200709
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500710 pixman_region32_init(&damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500711 pixman_region32_subtract(&damage, &view->transform.boundingbox,
712 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800713 if (view->plane)
714 pixman_region32_union(&view->plane->damage,
715 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500716 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800717 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200718}
719
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400720static void
721weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
722{
723 uint32_t different = es->output_mask ^ mask;
724 uint32_t entered = mask & different;
725 uint32_t left = es->output_mask & different;
726 struct weston_output *output;
727 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500728 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400729
730 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500731 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400732 return;
733 if (different == 0)
734 return;
735
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500736 client = wl_resource_get_client(es->resource);
737
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400738 wl_list_for_each(output, &es->compositor->output_list, link) {
739 if (1 << output->id & different)
740 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500741 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400742 client);
743 if (resource == NULL)
744 continue;
745 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500746 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400747 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500748 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400749 }
750}
751
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200752
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400753static void
754weston_surface_assign_output(struct weston_surface *es)
755{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500756 struct weston_output *new_output;
757 struct weston_view *view;
758 pixman_region32_t region;
759 uint32_t max, area, mask;
760 pixman_box32_t *e;
761
762 new_output = NULL;
763 max = 0;
764 mask = 0;
765 pixman_region32_init(&region);
766 wl_list_for_each(view, &es->views, surface_link) {
767 if (!view->output)
768 continue;
769
770 pixman_region32_intersect(&region, &view->transform.boundingbox,
771 &view->output->region);
772
773 e = pixman_region32_extents(&region);
774 area = (e->x2 - e->x1) * (e->y2 - e->y1);
775
776 mask |= view->output_mask;
777
778 if (area >= max) {
779 new_output = view->output;
780 max = area;
781 }
782 }
783 pixman_region32_fini(&region);
784
785 es->output = new_output;
786 weston_surface_update_output_mask(es, mask);
787}
788
789static void
790weston_view_assign_output(struct weston_view *ev)
791{
792 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400793 struct weston_output *output, *new_output;
794 pixman_region32_t region;
795 uint32_t max, area, mask;
796 pixman_box32_t *e;
797
798 new_output = NULL;
799 max = 0;
800 mask = 0;
801 pixman_region32_init(&region);
802 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500803 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400804 &output->region);
805
806 e = pixman_region32_extents(&region);
807 area = (e->x2 - e->x1) * (e->y2 - e->y1);
808
809 if (area > 0)
810 mask |= 1 << output->id;
811
812 if (area >= max) {
813 new_output = output;
814 max = area;
815 }
816 }
817 pixman_region32_fini(&region);
818
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200819 if (ev->output_mask != 0)
820 wl_list_remove(&ev->output_move_listener.link);
821
822 if (mask != 0)
823 wl_signal_add(&new_output->move_signal,
824 &ev->output_move_listener);
825
Jason Ekstranda7af7042013-10-12 22:38:11 -0500826 ev->output = new_output;
827 ev->output_mask = mask;
828
829 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400830}
831
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200832static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500833view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
834 int32_t width, int32_t height,
835 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200836{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200837 float min_x = HUGE_VALF, min_y = HUGE_VALF;
838 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200839 int32_t s[4][2] = {
840 { sx, sy },
841 { sx, sy + height },
842 { sx + width, sy },
843 { sx + width, sy + height }
844 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200845 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200846 int i;
847
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300848 if (width == 0 || height == 0) {
849 /* avoid rounding empty bbox to 1x1 */
850 pixman_region32_init(bbox);
851 return;
852 }
853
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200854 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200855 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500856 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200857 if (x < min_x)
858 min_x = x;
859 if (x > max_x)
860 max_x = x;
861 if (y < min_y)
862 min_y = y;
863 if (y > max_y)
864 max_y = y;
865 }
866
Pekka Paalanen219b9822012-02-08 15:38:37 +0200867 int_x = floorf(min_x);
868 int_y = floorf(min_y);
869 pixman_region32_init_rect(bbox, int_x, int_y,
870 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200871}
872
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200873static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500874weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200875{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500876 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200877
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200878 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500879 view->geometry.x = roundf(view->geometry.x);
880 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200881
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500882 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500883 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
884 view->transform.position.matrix.d[12] = view->geometry.x;
885 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500886
Jason Ekstranda7af7042013-10-12 22:38:11 -0500887 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500888
Jason Ekstranda7af7042013-10-12 22:38:11 -0500889 view->transform.inverse = view->transform.position.matrix;
890 view->transform.inverse.d[12] = -view->geometry.x;
891 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500892
Jason Ekstranda7af7042013-10-12 22:38:11 -0500893 pixman_region32_init_rect(&view->transform.boundingbox,
894 view->geometry.x,
895 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600896 view->surface->width,
897 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500898
Jason Ekstranda7af7042013-10-12 22:38:11 -0500899 if (view->alpha == 1.0) {
900 pixman_region32_copy(&view->transform.opaque,
901 &view->surface->opaque);
902 pixman_region32_translate(&view->transform.opaque,
903 view->geometry.x,
904 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500905 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200906}
907
908static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500909weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200910{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500911 struct weston_view *parent = view->geometry.parent;
912 struct weston_matrix *matrix = &view->transform.matrix;
913 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200914 struct weston_transform *tform;
915
Jason Ekstranda7af7042013-10-12 22:38:11 -0500916 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200917
918 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500919 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
920 view->transform.position.matrix.d[12] = view->geometry.x;
921 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200922
923 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500924 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200925 weston_matrix_multiply(matrix, &tform->matrix);
926
Pekka Paalanen483243f2013-03-08 14:56:50 +0200927 if (parent)
928 weston_matrix_multiply(matrix, &parent->transform.matrix);
929
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200930 if (weston_matrix_invert(inverse, matrix) < 0) {
931 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500932 weston_log("error: weston_view %p"
933 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200934 return -1;
935 }
936
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600937 view_compute_bbox(view, 0, 0,
938 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500939 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500940
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200941 return 0;
942}
943
944WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200946{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 struct weston_view *parent = view->geometry.parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200948
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200950 return;
951
Pekka Paalanen483243f2013-03-08 14:56:50 +0200952 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -0500953 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +0200954
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200956
Jason Ekstranda7af7042013-10-12 22:38:11 -0500957 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200958
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 pixman_region32_fini(&view->transform.boundingbox);
960 pixman_region32_fini(&view->transform.opaque);
961 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500962
Pekka Paalanencd403622012-01-25 13:37:39 +0200963 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 if (view->geometry.transformation_list.next ==
965 &view->transform.position.link &&
966 view->geometry.transformation_list.prev ==
967 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +0200968 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500969 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200970 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500971 if (weston_view_update_transform_enable(view) < 0)
972 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200973 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200974
Jason Ekstranda7af7042013-10-12 22:38:11 -0500975 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +0200976
Jason Ekstranda7af7042013-10-12 22:38:11 -0500977 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -0300978
Jason Ekstranda7af7042013-10-12 22:38:11 -0500979 wl_signal_emit(&view->surface->compositor->transform_signal,
980 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200981}
982
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200983WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500984weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +0200985{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500986 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200987
988 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 * The invariant: if view->geometry.dirty, then all views
990 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +0200991 * Corollary: if not parent->geometry.dirty, then all ancestors
992 * are not dirty.
993 */
994
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +0200996 return;
997
Jason Ekstranda7af7042013-10-12 22:38:11 -0500998 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +0200999
Jason Ekstranda7af7042013-10-12 22:38:11 -05001000 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001001 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001002 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001003}
1004
1005WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001006weston_view_to_global_fixed(struct weston_view *view,
1007 wl_fixed_t vx, wl_fixed_t vy,
1008 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001009{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001010 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001011
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 weston_view_to_global_float(view,
1013 wl_fixed_to_double(vx),
1014 wl_fixed_to_double(vy),
1015 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001016 *x = wl_fixed_from_double(xf);
1017 *y = wl_fixed_from_double(yf);
1018}
1019
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001020WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021weston_view_from_global_float(struct weston_view *view,
1022 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001023{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001025 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001028
1029 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001030 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001031 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001032 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 *vx = 0;
1034 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001035 return;
1036 }
1037
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 *vx = v.f[0] / v.f[3];
1039 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001040 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001041 *vx = x - view->geometry.x;
1042 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001043 }
1044}
1045
1046WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001047weston_view_from_global_fixed(struct weston_view *view,
1048 wl_fixed_t x, wl_fixed_t y,
1049 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001050{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001051 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001052
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 weston_view_from_global_float(view,
1054 wl_fixed_to_double(x),
1055 wl_fixed_to_double(y),
1056 &vxf, &vyf);
1057 *vx = wl_fixed_from_double(vxf);
1058 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001059}
1060
1061WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062weston_view_from_global(struct weston_view *view,
1063 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001064{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001066
Jason Ekstranda7af7042013-10-12 22:38:11 -05001067 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1068 *vx = floorf(vxf);
1069 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001070}
1071
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001072WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001073weston_surface_schedule_repaint(struct weston_surface *surface)
1074{
1075 struct weston_output *output;
1076
1077 wl_list_for_each(output, &surface->compositor->output_list, link)
1078 if (surface->output_mask & (1 << output->id))
1079 weston_output_schedule_repaint(output);
1080}
1081
1082WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001083weston_view_schedule_repaint(struct weston_view *view)
1084{
1085 struct weston_output *output;
1086
1087 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1088 if (view->output_mask & (1 << output->id))
1089 weston_output_schedule_repaint(output);
1090}
1091
1092WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001093weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001094{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001095 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 0, 0, surface->width,
1097 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001098
Kristian Høgsberg98238702012-08-03 16:29:12 -04001099 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001100}
1101
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001102WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001104{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001105 if (view->geometry.x == x && view->geometry.y == y)
1106 return;
1107
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 view->geometry.x = x;
1109 view->geometry.y = y;
1110 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001111}
1112
Pekka Paalanen483243f2013-03-08 14:56:50 +02001113static void
1114transform_parent_handle_parent_destroy(struct wl_listener *listener,
1115 void *data)
1116{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 struct weston_view *view =
1118 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001119 geometry.parent_destroy_listener);
1120
Jason Ekstranda7af7042013-10-12 22:38:11 -05001121 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001122}
1123
1124WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125weston_view_set_transform_parent(struct weston_view *view,
1126 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001127{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 if (view->geometry.parent) {
1129 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1130 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001131 }
1132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001136 transform_parent_handle_parent_destroy;
1137 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001138 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001140 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001141 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001142 }
1143
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 weston_view_geometry_dirty(view);
1145}
1146
1147WL_EXPORT int
1148weston_view_is_mapped(struct weston_view *view)
1149{
1150 if (view->output)
1151 return 1;
1152 else
1153 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001154}
1155
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001156WL_EXPORT int
1157weston_surface_is_mapped(struct weston_surface *surface)
1158{
1159 if (surface->output)
1160 return 1;
1161 else
1162 return 0;
1163}
1164
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001165static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001166weston_surface_set_size(struct weston_surface *surface,
1167 int32_t width, int32_t height)
1168{
1169 struct weston_view *view;
1170
1171 if (surface->width == width && surface->height == height)
1172 return;
1173
1174 surface->width = width;
1175 surface->height = height;
1176
1177 wl_list_for_each(view, &surface->views, surface_link)
1178 weston_view_geometry_dirty(view);
1179}
1180
1181static void
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001182weston_surface_set_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001183{
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001184 int32_t width, height;
1185
1186 if (!surface->buffer_ref.buffer) {
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001187 weston_surface_set_size(surface, 0, 0);
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001188 return;
1189 }
1190
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001191 switch (surface->buffer_viewport.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001192 case WL_OUTPUT_TRANSFORM_90:
1193 case WL_OUTPUT_TRANSFORM_270:
1194 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1195 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001196 width = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001197 height = surface->buffer_ref.buffer->width;
1198 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001199 default:
Alexander Larsson4ea95522013-05-22 14:41:37 +02001200 width = surface->buffer_ref.buffer->width;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001201 height = surface->buffer_ref.buffer->height;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001202 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001203 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001204
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001205 width = width / surface->buffer_viewport.scale;
1206 height = height / surface->buffer_viewport.scale;
1207 weston_surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001208}
1209
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001210WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001212{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001213 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001214
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001215 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001216
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001217 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001218}
1219
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220WL_EXPORT struct weston_view *
1221weston_compositor_pick_view(struct weston_compositor *compositor,
1222 wl_fixed_t x, wl_fixed_t y,
1223 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001224{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001225 struct weston_view *view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001226
Jason Ekstranda7af7042013-10-12 22:38:11 -05001227 wl_list_for_each(view, &compositor->view_list, link) {
1228 weston_view_from_global_fixed(view, x, y, vx, vy);
1229 if (pixman_region32_contains_point(&view->surface->input,
1230 wl_fixed_to_int(*vx),
1231 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001232 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001234 }
1235
1236 return NULL;
1237}
1238
1239static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001240weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001241{
Daniel Stone37816df2012-05-16 18:45:18 +01001242 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001243
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001244 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001245 return;
1246
Daniel Stone37816df2012-05-16 18:45:18 +01001247 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001248 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001249}
1250
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001251WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001252weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001253{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001254 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 if (!weston_view_is_mapped(view))
1257 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001258
Jason Ekstranda7af7042013-10-12 22:38:11 -05001259 weston_view_damage_below(view);
1260 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001261 view->plane = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 wl_list_remove(&view->layer_link);
1263 wl_list_init(&view->layer_link);
1264 wl_list_remove(&view->link);
1265 wl_list_init(&view->link);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02001266 wl_list_remove(&view->output_move_listener.link);
1267 wl_list_init(&view->output_move_listener.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 view->output_mask = 0;
1269 weston_surface_assign_output(view->surface);
1270
1271 if (weston_surface_is_mapped(view->surface))
1272 return;
1273
1274 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1275 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001276 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001278 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001279 NULL,
1280 wl_fixed_from_int(0),
1281 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001283 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001284 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287WL_EXPORT void
1288weston_surface_unmap(struct weston_surface *surface)
1289{
1290 struct weston_view *view;
1291
1292 wl_list_for_each(view, &surface->views, surface_link)
1293 weston_view_unmap(view);
1294 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001295}
1296
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001297struct weston_frame_callback {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001298 struct wl_resource *resource;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001299 struct wl_list link;
1300};
1301
Jason Ekstranda7af7042013-10-12 22:38:11 -05001302WL_EXPORT void
1303weston_view_destroy(struct weston_view *view)
1304{
1305 wl_signal_emit(&view->destroy_signal, view);
1306
1307 assert(wl_list_empty(&view->geometry.child_list));
1308
1309 if (weston_view_is_mapped(view)) {
1310 weston_view_unmap(view);
1311 weston_compositor_build_view_list(view->surface->compositor);
1312 }
1313
1314 wl_list_remove(&view->link);
1315 wl_list_remove(&view->layer_link);
1316
1317 pixman_region32_fini(&view->clip);
1318 pixman_region32_fini(&view->transform.boundingbox);
1319
1320 weston_view_set_transform_parent(view, NULL);
1321
Jason Ekstranda7af7042013-10-12 22:38:11 -05001322 wl_list_remove(&view->surface_link);
1323
1324 free(view);
1325}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001326
1327WL_EXPORT void
1328weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001329{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001330 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001331 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001332
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001333 if (--surface->ref_count > 0)
1334 return;
1335
1336 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1337
Pekka Paalanene67858b2013-04-25 13:57:42 +03001338 assert(wl_list_empty(&surface->subsurface_list_pending));
1339 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001340
Jason Ekstranda7af7042013-10-12 22:38:11 -05001341 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1342 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001343
Pekka Paalanenbc106382012-10-10 12:49:31 +03001344 wl_list_for_each_safe(cb, next,
1345 &surface->pending.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001346 wl_resource_destroy(cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001347
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001348 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001349 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001350 pixman_region32_fini(&surface->pending.damage);
1351
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001352 if (surface->pending.buffer)
1353 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
1354
Pekka Paalanende685b82012-12-04 15:58:12 +02001355 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001356
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001357 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001358 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001359 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001360
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001361 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001362 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001363
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001364 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001365}
1366
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001367static void
1368destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001369{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001370 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001371
Giulio Camuffo0d379742013-11-15 22:06:15 +01001372 /* Set the resource to NULL, since we don't want to leave a
1373 * dangling pointer if the surface was refcounted and survives
1374 * the weston_surface_destroy() call. */
1375 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001376 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001377}
1378
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001379static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001380weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1381{
1382 struct weston_buffer *buffer =
1383 container_of(listener, struct weston_buffer, destroy_listener);
1384
1385 wl_signal_emit(&buffer->destroy_signal, buffer);
1386 free(buffer);
1387}
1388
1389struct weston_buffer *
1390weston_buffer_from_resource(struct wl_resource *resource)
1391{
1392 struct weston_buffer *buffer;
1393 struct wl_listener *listener;
1394
1395 listener = wl_resource_get_destroy_listener(resource,
1396 weston_buffer_destroy_handler);
1397
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001398 if (listener)
1399 return container_of(listener, struct weston_buffer,
1400 destroy_listener);
1401
1402 buffer = zalloc(sizeof *buffer);
1403 if (buffer == NULL)
1404 return NULL;
1405
1406 buffer->resource = resource;
1407 wl_signal_init(&buffer->destroy_signal);
1408 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001409 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001410 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001411
1412 return buffer;
1413}
1414
1415static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001416weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1417 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001418{
Pekka Paalanende685b82012-12-04 15:58:12 +02001419 struct weston_buffer_reference *ref =
1420 container_of(listener, struct weston_buffer_reference,
1421 destroy_listener);
1422
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001423 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001424 ref->buffer = NULL;
1425}
1426
1427WL_EXPORT void
1428weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001429 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001430{
1431 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001432 ref->buffer->busy_count--;
1433 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001434 assert(wl_resource_get_client(ref->buffer->resource));
1435 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001436 WL_BUFFER_RELEASE);
1437 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001438 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001439 }
1440
Pekka Paalanende685b82012-12-04 15:58:12 +02001441 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001442 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001443 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001444 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001445 }
1446
Pekka Paalanende685b82012-12-04 15:58:12 +02001447 ref->buffer = buffer;
1448 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1449}
1450
1451static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001452weston_surface_attach(struct weston_surface *surface,
1453 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001454{
1455 weston_buffer_reference(&surface->buffer_ref, buffer);
1456
Pekka Paalanena6421c42012-12-04 15:58:10 +02001457 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001458 if (weston_surface_is_mapped(surface))
1459 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001460 }
1461
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001462 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001463}
1464
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001465WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001466weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001467{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001468 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001469
1470 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001471 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001472}
1473
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001474WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001475weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001476{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001477 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001478
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001479 pixman_region32_union(&compositor->primary_plane.damage,
1480 &compositor->primary_plane.damage,
1481 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001482 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001483}
1484
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001485static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001486surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001487{
Pekka Paalanende685b82012-12-04 15:58:12 +02001488 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001489 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001490 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001491
Jason Ekstranda7af7042013-10-12 22:38:11 -05001492 empty_region(&surface->damage);
1493}
1494
1495static void
1496view_accumulate_damage(struct weston_view *view,
1497 pixman_region32_t *opaque)
1498{
1499 pixman_region32_t damage;
1500
1501 pixman_region32_init(&damage);
1502 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001503 pixman_box32_t *extents;
1504
Jason Ekstranda7af7042013-10-12 22:38:11 -05001505 extents = pixman_region32_extents(&view->surface->damage);
1506 view_compute_bbox(view, extents->x1, extents->y1,
1507 extents->x2 - extents->x1,
1508 extents->y2 - extents->y1,
1509 &damage);
1510 pixman_region32_translate(&damage,
1511 -view->plane->x,
1512 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001513 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001514 pixman_region32_copy(&damage, &view->surface->damage);
1515 pixman_region32_translate(&damage,
1516 view->geometry.x - view->plane->x,
1517 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001518 }
1519
Jason Ekstranda7af7042013-10-12 22:38:11 -05001520 pixman_region32_subtract(&damage, &damage, opaque);
1521 pixman_region32_union(&view->plane->damage,
1522 &view->plane->damage, &damage);
1523 pixman_region32_fini(&damage);
1524 pixman_region32_copy(&view->clip, opaque);
1525 pixman_region32_union(opaque, opaque, &view->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001526}
1527
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001528static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001529compositor_accumulate_damage(struct weston_compositor *ec)
1530{
1531 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001532 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001533 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001534
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001535 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001536
1537 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001538 pixman_region32_copy(&plane->clip, &clip);
1539
1540 pixman_region32_init(&opaque);
1541
Jason Ekstranda7af7042013-10-12 22:38:11 -05001542 wl_list_for_each(ev, &ec->view_list, link) {
1543 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001544 continue;
1545
Jason Ekstranda7af7042013-10-12 22:38:11 -05001546 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001547 }
1548
1549 pixman_region32_union(&clip, &clip, &opaque);
1550 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001551 }
1552
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001553 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001554
Jason Ekstranda7af7042013-10-12 22:38:11 -05001555 wl_list_for_each(ev, &ec->view_list, link)
1556 ev->surface->touched = 0;
1557
1558 wl_list_for_each(ev, &ec->view_list, link) {
1559 if (ev->surface->touched)
1560 continue;
1561 ev->surface->touched = 1;
1562
1563 surface_flush_damage(ev->surface);
1564
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001565 /* Both the renderer and the backend have seen the buffer
1566 * by now. If renderer needs the buffer, it has its own
1567 * reference set. If the backend wants to keep the buffer
1568 * around for migrating the surface into a non-primary plane
1569 * later, keep_buffer is true. Otherwise, drop the core
1570 * reference now, and allow early buffer release. This enables
1571 * clients to use single-buffering.
1572 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573 if (!ev->surface->keep_buffer)
1574 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001575 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001576}
1577
1578static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001579surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001580{
1581 struct weston_subsurface *sub;
1582
Pekka Paalanene67858b2013-04-25 13:57:42 +03001583 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001584 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001585 continue;
1586
Jason Ekstranda7af7042013-10-12 22:38:11 -05001587 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1588 wl_list_init(&sub->surface->views);
1589
1590 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001591 }
1592}
1593
1594static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001595surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001596{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001597 struct weston_subsurface *sub;
1598 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001599
Jason Ekstranda7af7042013-10-12 22:38:11 -05001600 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1601 if (sub->surface == surface)
1602 continue;
1603
1604 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link)
1605 weston_view_destroy(view);
1606
1607 surface_free_unused_subsurface_views(sub->surface);
1608 }
1609}
1610
1611static void
1612view_list_add_subsurface_view(struct weston_compositor *compositor,
1613 struct weston_subsurface *sub,
1614 struct weston_view *parent)
1615{
1616 struct weston_subsurface *child;
1617 struct weston_view *view = NULL, *iv;
1618
1619 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1620 if (iv->geometry.parent == parent) {
1621 view = iv;
1622 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001623 }
1624 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001625
1626 if (view) {
1627 /* Put it back in the surface's list of views */
1628 wl_list_remove(&view->surface_link);
1629 wl_list_insert(&sub->surface->views, &view->surface_link);
1630 } else {
1631 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001632 weston_view_set_position(view,
1633 sub->position.x,
1634 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001635 weston_view_set_transform_parent(view, parent);
1636 }
1637
1638 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001639
Pekka Paalanenb188e912013-11-19 14:03:35 +02001640 if (wl_list_empty(&sub->surface->subsurface_list)) {
1641 wl_list_insert(compositor->view_list.prev, &view->link);
1642 return;
1643 }
1644
1645 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1646 if (child->surface == sub->surface)
1647 wl_list_insert(compositor->view_list.prev, &view->link);
1648 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001649 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001650 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001651}
1652
1653static void
1654view_list_add(struct weston_compositor *compositor,
1655 struct weston_view *view)
1656{
1657 struct weston_subsurface *sub;
1658
1659 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001660
Pekka Paalanenb188e912013-11-19 14:03:35 +02001661 if (wl_list_empty(&view->surface->subsurface_list)) {
1662 wl_list_insert(compositor->view_list.prev, &view->link);
1663 return;
1664 }
1665
1666 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1667 if (sub->surface == view->surface)
1668 wl_list_insert(compositor->view_list.prev, &view->link);
1669 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001670 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001671 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001672}
1673
1674static void
1675weston_compositor_build_view_list(struct weston_compositor *compositor)
1676{
1677 struct weston_view *view;
1678 struct weston_layer *layer;
1679
1680 wl_list_for_each(layer, &compositor->layer_list, link)
1681 wl_list_for_each(view, &layer->view_list, layer_link)
1682 surface_stash_subsurface_views(view->surface);
1683
1684 wl_list_init(&compositor->view_list);
1685 wl_list_for_each(layer, &compositor->layer_list, link) {
1686 wl_list_for_each(view, &layer->view_list, layer_link) {
1687 view_list_add(compositor, view);
1688 }
1689 }
1690
1691 wl_list_for_each(layer, &compositor->layer_list, link)
1692 wl_list_for_each(view, &layer->view_list, layer_link)
1693 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001694}
1695
David Herrmann1edf44c2013-10-22 17:11:26 +02001696static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001697weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001698{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001699 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001701 struct weston_animation *animation, *next;
1702 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001703 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001704 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001705 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001706
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001707 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001708 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001709
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001710 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001711 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001712 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001713 wl_list_for_each(ev, &ec->view_list, link)
1714 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001715
Pekka Paalanene67858b2013-04-25 13:57:42 +03001716 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 wl_list_for_each(ev, &ec->view_list, link) {
1718 /* Note: This operation is safe to do multiple times on the
1719 * same surface.
1720 */
1721 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001722 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001723 &ev->surface->frame_callback_list);
1724 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001725 }
1726 }
1727
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001728 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001729
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001730 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001731 pixman_region32_intersect(&output_damage,
1732 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001733 pixman_region32_subtract(&output_damage,
1734 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001735
Scott Moreauccbf29d2012-02-22 14:21:41 -07001736 if (output->dirty)
1737 weston_output_update_matrix(output);
1738
David Herrmann1edf44c2013-10-22 17:11:26 +02001739 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001740
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001741 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001742
Kristian Høgsbergef044142011-06-21 15:02:12 -04001743 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001744
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001745 weston_compositor_repick(ec);
1746 wl_event_loop_dispatch(ec->input_loop, 0);
1747
Jonas Ådahldb773762012-06-13 00:01:21 +02001748 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001749 wl_callback_send_done(cb->resource, msecs);
1750 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001751 }
1752
Scott Moreaud64cf212012-06-08 19:40:54 -06001753 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001754 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001755 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001756 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001757
1758 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001759}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001760
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001761static int
1762weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001763{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001764 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001765
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001766 wl_event_loop_dispatch(compositor->input_loop, 0);
1767
1768 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001769}
1770
1771WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001772weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001773{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001774 struct weston_compositor *compositor = output->compositor;
1775 struct wl_event_loop *loop =
1776 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001777 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001778
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001779 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001780
1781 if (output->repaint_needed &&
1782 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1783 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001784 r = weston_output_repaint(output, msecs);
1785 if (!r)
1786 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001787 }
1788
1789 output->repaint_scheduled = 0;
1790 if (compositor->input_loop_source)
1791 return;
1792
1793 fd = wl_event_loop_get_fd(compositor->input_loop);
1794 compositor->input_loop_source =
1795 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1796 weston_compositor_read_input, compositor);
1797}
1798
1799static void
1800idle_repaint(void *data)
1801{
1802 struct weston_output *output = data;
1803
Jonas Ådahle5a12252013-04-05 23:07:11 +02001804 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001805}
1806
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001807WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001808weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1809{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001810 wl_list_init(&layer->view_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001811 if (below != NULL)
1812 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001813}
1814
1815WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001816weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001817{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001818 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001819 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001820
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01001821 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
1822 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001823 return;
1824
Kristian Høgsbergef044142011-06-21 15:02:12 -04001825 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001826 output->repaint_needed = 1;
1827 if (output->repaint_scheduled)
1828 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001829
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001830 wl_event_loop_add_idle(loop, idle_repaint, output);
1831 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001832
1833 if (compositor->input_loop_source) {
1834 wl_event_source_remove(compositor->input_loop_source);
1835 compositor->input_loop_source = NULL;
1836 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001837}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001838
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001839WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001840weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1841{
1842 struct weston_output *output;
1843
1844 wl_list_for_each(output, &compositor->output_list, link)
1845 weston_output_schedule_repaint(output);
1846}
1847
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001848static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001849surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001850{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001851 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001852}
1853
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001854static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001855surface_attach(struct wl_client *client,
1856 struct wl_resource *resource,
1857 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1858{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001859 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001860 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001861
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001862 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001863 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07001864 if (buffer == NULL) {
1865 wl_client_post_no_memory(client);
1866 return;
1867 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001868 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001869
Pekka Paalanende685b82012-12-04 15:58:12 +02001870 /* Attach, attach, without commit in between does not send
1871 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001872 if (surface->pending.buffer)
1873 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001874
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001875 surface->pending.sx = sx;
1876 surface->pending.sy = sy;
1877 surface->pending.buffer = buffer;
Giulio Camuffo184df502013-02-21 11:29:21 +01001878 surface->pending.newly_attached = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001879 if (buffer) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001880 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001881 &surface->pending.buffer_destroy_listener);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001882 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001883}
1884
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001885static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001886surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001887 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001888 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001889{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001890 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001891
Pekka Paalanen8e159182012-10-10 12:49:25 +03001892 pixman_region32_union_rect(&surface->pending.damage,
1893 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001894 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001895}
1896
Kristian Høgsberg33418202011-08-16 23:01:28 -04001897static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001898destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001899{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001900 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001901
1902 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001903 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001904}
1905
1906static void
1907surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001908 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001909{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001910 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001911 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001912
1913 cb = malloc(sizeof *cb);
1914 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001915 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001916 return;
1917 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001918
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07001919 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
1920 callback);
1921 if (cb->resource == NULL) {
1922 free(cb);
1923 wl_resource_post_no_memory(resource);
1924 return;
1925 }
1926
Jason Ekstranda85118c2013-06-27 20:17:02 -05001927 wl_resource_set_implementation(cb->resource, NULL, cb,
1928 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001929
Pekka Paalanenbc106382012-10-10 12:49:31 +03001930 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001931}
1932
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001933static void
1934surface_set_opaque_region(struct wl_client *client,
1935 struct wl_resource *resource,
1936 struct wl_resource *region_resource)
1937{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001938 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001939 struct weston_region *region;
1940
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001941 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001942 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001943 pixman_region32_copy(&surface->pending.opaque,
1944 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001945 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001946 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001947 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001948}
1949
1950static void
1951surface_set_input_region(struct wl_client *client,
1952 struct wl_resource *resource,
1953 struct wl_resource *region_resource)
1954{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05001955 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001956 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001957
1958 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05001959 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001960 pixman_region32_copy(&surface->pending.input,
1961 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001962 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001963 pixman_region32_fini(&surface->pending.input);
1964 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001965 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001966}
1967
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001968static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03001969weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001970{
Pekka Paalanene67858b2013-04-25 13:57:42 +03001971 struct weston_subsurface *sub;
1972
1973 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
1974 parent_link_pending) {
1975 wl_list_remove(&sub->parent_link);
1976 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
1977 }
1978}
1979
1980static void
1981weston_surface_commit(struct weston_surface *surface)
1982{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001983 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001984 pixman_region32_t opaque;
1985
Alexander Larsson4ea95522013-05-22 14:41:37 +02001986 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02001987 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01001988 surface->buffer_viewport = surface->pending.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02001989
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001990 /* wl_surface.attach */
Giulio Camuffo184df502013-02-21 11:29:21 +01001991 if (surface->pending.buffer || surface->pending.newly_attached)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001992 weston_surface_attach(surface, surface->pending.buffer);
1993
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001994 weston_surface_set_size_from_buffer(surface);
Giulio Camuffo184df502013-02-21 11:29:21 +01001995
1996 if (surface->configure && surface->pending.newly_attached)
Pekka Paalanenf1f48cf2013-03-08 14:56:48 +02001997 surface->configure(surface,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001998 surface->pending.sx, surface->pending.sy);
Giulio Camuffo184df502013-02-21 11:29:21 +01001999
Kristian Høgsberge7144fd2013-03-04 12:11:41 -05002000 if (surface->pending.buffer)
2001 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
2002 surface->pending.buffer = NULL;
Daniel Stoneb4f4a592012-11-07 17:51:44 +11002003 surface->pending.sx = 0;
2004 surface->pending.sy = 0;
Giulio Camuffo184df502013-02-21 11:29:21 +01002005 surface->pending.newly_attached = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002006
2007 /* wl_surface.damage */
2008 pixman_region32_union(&surface->damage, &surface->damage,
2009 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002010 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2011 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002012 surface->width,
2013 surface->height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03002014 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002015
2016 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002017 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 surface->width,
2019 surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002020 pixman_region32_intersect(&opaque,
2021 &opaque, &surface->pending.opaque);
2022
2023 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2024 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002025 wl_list_for_each(view, &surface->views, surface_link)
2026 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002027 }
2028
2029 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002030
2031 /* wl_surface.set_input_region */
2032 pixman_region32_fini(&surface->input);
2033 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002034 surface->width,
2035 surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002036 pixman_region32_intersect(&surface->input,
2037 &surface->input, &surface->pending.input);
2038
Pekka Paalanenbc106382012-10-10 12:49:31 +03002039 /* wl_surface.frame */
2040 wl_list_insert_list(&surface->frame_callback_list,
2041 &surface->pending.frame_callback_list);
2042 wl_list_init(&surface->pending.frame_callback_list);
2043
Pekka Paalanene67858b2013-04-25 13:57:42 +03002044 weston_surface_commit_subsurface_order(surface);
2045
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002046 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002047}
2048
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002049static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002050weston_subsurface_commit(struct weston_subsurface *sub);
2051
2052static void
2053weston_subsurface_parent_commit(struct weston_subsurface *sub,
2054 int parent_is_synchronized);
2055
2056static void
2057surface_commit(struct wl_client *client, struct wl_resource *resource)
2058{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002059 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002060 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2061
2062 if (sub) {
2063 weston_subsurface_commit(sub);
2064 return;
2065 }
2066
2067 weston_surface_commit(surface);
2068
2069 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2070 if (sub->surface != surface)
2071 weston_subsurface_parent_commit(sub, 0);
2072 }
2073}
2074
2075static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002076surface_set_buffer_transform(struct wl_client *client,
2077 struct wl_resource *resource, int transform)
2078{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002079 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002080
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002081 surface->pending.buffer_viewport.transform = transform;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002082}
2083
Alexander Larsson4ea95522013-05-22 14:41:37 +02002084static void
2085surface_set_buffer_scale(struct wl_client *client,
2086 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002087 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002088{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002089 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002090
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002091 surface->pending.buffer_viewport.scale = scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002092}
2093
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002094static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002095 surface_destroy,
2096 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002097 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002098 surface_frame,
2099 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002100 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002101 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002102 surface_set_buffer_transform,
2103 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002104};
2105
2106static void
2107compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002108 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002109{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002110 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002111 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002112
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002113 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002114 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002115 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002116 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002117 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002118
Jason Ekstranda85118c2013-06-27 20:17:02 -05002119 surface->resource =
2120 wl_resource_create(client, &wl_surface_interface,
2121 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002122 if (surface->resource == NULL) {
2123 weston_surface_destroy(surface);
2124 wl_resource_post_no_memory(resource);
2125 return;
2126 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002127 wl_resource_set_implementation(surface->resource, &surface_interface,
2128 surface, destroy_surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002129}
2130
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002131static void
2132destroy_region(struct wl_resource *resource)
2133{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002134 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002135
2136 pixman_region32_fini(&region->region);
2137 free(region);
2138}
2139
2140static void
2141region_destroy(struct wl_client *client, struct wl_resource *resource)
2142{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002143 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002144}
2145
2146static void
2147region_add(struct wl_client *client, struct wl_resource *resource,
2148 int32_t x, int32_t y, int32_t width, int32_t height)
2149{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002150 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002151
2152 pixman_region32_union_rect(&region->region, &region->region,
2153 x, y, width, height);
2154}
2155
2156static void
2157region_subtract(struct wl_client *client, struct wl_resource *resource,
2158 int32_t x, int32_t y, int32_t width, int32_t height)
2159{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002160 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002161 pixman_region32_t rect;
2162
2163 pixman_region32_init_rect(&rect, x, y, width, height);
2164 pixman_region32_subtract(&region->region, &region->region, &rect);
2165 pixman_region32_fini(&rect);
2166}
2167
2168static const struct wl_region_interface region_interface = {
2169 region_destroy,
2170 region_add,
2171 region_subtract
2172};
2173
2174static void
2175compositor_create_region(struct wl_client *client,
2176 struct wl_resource *resource, uint32_t id)
2177{
2178 struct weston_region *region;
2179
2180 region = malloc(sizeof *region);
2181 if (region == NULL) {
2182 wl_resource_post_no_memory(resource);
2183 return;
2184 }
2185
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002186 pixman_region32_init(&region->region);
2187
Jason Ekstranda85118c2013-06-27 20:17:02 -05002188 region->resource =
2189 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002190 if (region->resource == NULL) {
2191 free(region);
2192 wl_resource_post_no_memory(resource);
2193 return;
2194 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002195 wl_resource_set_implementation(region->resource, &region_interface,
2196 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002197}
2198
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002199static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002200 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002201 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002202};
2203
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002204static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002205weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2206{
2207 struct weston_surface *surface = sub->surface;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002208 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002209 pixman_region32_t opaque;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210
Alexander Larsson4ea95522013-05-22 14:41:37 +02002211 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002212 /* wl_surface.set_buffer_scale */
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002213 surface->buffer_viewport = sub->cached.buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002214
Pekka Paalanene67858b2013-04-25 13:57:42 +03002215 /* wl_surface.attach */
2216 if (sub->cached.buffer_ref.buffer || sub->cached.newly_attached)
2217 weston_surface_attach(surface, sub->cached.buffer_ref.buffer);
2218 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2219
Pekka Paalanenda75ee12013-11-26 18:19:43 +01002220 weston_surface_set_size_from_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002221
2222 if (surface->configure && sub->cached.newly_attached)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002223 surface->configure(surface, sub->cached.sx, sub->cached.sy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002224 sub->cached.sx = 0;
2225 sub->cached.sy = 0;
2226 sub->cached.newly_attached = 0;
2227
2228 /* wl_surface.damage */
2229 pixman_region32_union(&surface->damage, &surface->damage,
2230 &sub->cached.damage);
2231 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
2232 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002233 surface->width,
2234 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002235 empty_region(&sub->cached.damage);
2236
2237 /* wl_surface.set_opaque_region */
2238 pixman_region32_init_rect(&opaque, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002239 surface->width,
2240 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002241 pixman_region32_intersect(&opaque,
2242 &opaque, &sub->cached.opaque);
2243
2244 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2245 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002246 wl_list_for_each(view, &surface->views, surface_link)
2247 weston_view_geometry_dirty(view);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002248 }
2249
2250 pixman_region32_fini(&opaque);
2251
2252 /* wl_surface.set_input_region */
2253 pixman_region32_fini(&surface->input);
2254 pixman_region32_init_rect(&surface->input, 0, 0,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002255 surface->width,
2256 surface->height);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002257 pixman_region32_intersect(&surface->input,
2258 &surface->input, &sub->cached.input);
2259
2260 /* wl_surface.frame */
2261 wl_list_insert_list(&surface->frame_callback_list,
2262 &sub->cached.frame_callback_list);
2263 wl_list_init(&sub->cached.frame_callback_list);
2264
2265 weston_surface_commit_subsurface_order(surface);
2266
2267 weston_surface_schedule_repaint(surface);
2268
2269 sub->cached.has_data = 0;
2270}
2271
2272static void
2273weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2274{
2275 struct weston_surface *surface = sub->surface;
2276
2277 /*
2278 * If this commit would cause the surface to move by the
2279 * attach(dx, dy) parameters, the old damage region must be
2280 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002281 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002282 */
2283 pixman_region32_translate(&sub->cached.damage,
2284 -surface->pending.sx, -surface->pending.sy);
2285 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2286 &surface->pending.damage);
2287 empty_region(&surface->pending.damage);
2288
2289 if (surface->pending.newly_attached) {
2290 sub->cached.newly_attached = 1;
2291 weston_buffer_reference(&sub->cached.buffer_ref,
2292 surface->pending.buffer);
2293 }
2294 sub->cached.sx += surface->pending.sx;
2295 sub->cached.sy += surface->pending.sy;
2296 surface->pending.sx = 0;
2297 surface->pending.sy = 0;
2298 surface->pending.newly_attached = 0;
2299
Pekka Paalanen1fd9c0f2013-11-26 18:19:41 +01002300 sub->cached.buffer_viewport = surface->pending.buffer_viewport;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002301
2302 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2303
2304 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2305
2306 wl_list_insert_list(&sub->cached.frame_callback_list,
2307 &surface->pending.frame_callback_list);
2308 wl_list_init(&surface->pending.frame_callback_list);
2309
2310 sub->cached.has_data = 1;
2311}
2312
2313static int
2314weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2315{
2316 while (sub) {
2317 if (sub->synchronized)
2318 return 1;
2319
2320 if (!sub->parent)
2321 return 0;
2322
2323 sub = weston_surface_to_subsurface(sub->parent);
2324 }
2325
2326 return 0;
2327}
2328
2329static void
2330weston_subsurface_commit(struct weston_subsurface *sub)
2331{
2332 struct weston_surface *surface = sub->surface;
2333 struct weston_subsurface *tmp;
2334
2335 /* Recursive check for effectively synchronized. */
2336 if (weston_subsurface_is_synchronized(sub)) {
2337 weston_subsurface_commit_to_cache(sub);
2338 } else {
2339 if (sub->cached.has_data) {
2340 /* flush accumulated state from cache */
2341 weston_subsurface_commit_to_cache(sub);
2342 weston_subsurface_commit_from_cache(sub);
2343 } else {
2344 weston_surface_commit(surface);
2345 }
2346
2347 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2348 if (tmp->surface != surface)
2349 weston_subsurface_parent_commit(tmp, 0);
2350 }
2351 }
2352}
2353
2354static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002355weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002356{
2357 struct weston_surface *surface = sub->surface;
2358 struct weston_subsurface *tmp;
2359
Pekka Paalanene67858b2013-04-25 13:57:42 +03002360 /* From now on, commit_from_cache the whole sub-tree, regardless of
2361 * the synchronized mode of each child. This sub-surface or some
2362 * of its ancestors were synchronized, so we are synchronized
2363 * all the way down.
2364 */
2365
2366 if (sub->cached.has_data)
2367 weston_subsurface_commit_from_cache(sub);
2368
2369 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2370 if (tmp->surface != surface)
2371 weston_subsurface_parent_commit(tmp, 1);
2372 }
2373}
2374
2375static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002376weston_subsurface_parent_commit(struct weston_subsurface *sub,
2377 int parent_is_synchronized)
2378{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002379 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002380 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002381 wl_list_for_each(view, &sub->surface->views, surface_link)
2382 weston_view_set_position(view,
2383 sub->position.x,
2384 sub->position.y);
2385
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002386 sub->position.set = 0;
2387 }
2388
2389 if (parent_is_synchronized || sub->synchronized)
2390 weston_subsurface_synchronized_commit(sub);
2391}
2392
2393static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002394subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002395{
2396 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002397 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002398
Jason Ekstranda7af7042013-10-12 22:38:11 -05002399 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002400 weston_view_set_position(view,
2401 view->geometry.x + dx,
2402 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002403
2404 /* No need to check parent mappedness, because if parent is not
2405 * mapped, parent is not in a visible layer, so this sub-surface
2406 * will not be drawn either.
2407 */
2408 if (!weston_surface_is_mapped(surface)) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002409 /* Cannot call weston_surface_update_transform(),
2410 * because that would call it also for the parent surface,
2411 * which might not be mapped yet. That would lead to
2412 * inconsistent state, where the window could never be
2413 * mapped.
2414 *
2415 * Instead just assing any output, to make
2416 * weston_surface_is_mapped() return true, so that when the
2417 * parent surface does get mapped, this one will get
2418 * included, too. See surface_list_add().
2419 */
2420 assert(!wl_list_empty(&compositor->output_list));
2421 surface->output = container_of(compositor->output_list.next,
2422 struct weston_output, link);
2423 }
2424}
2425
2426static struct weston_subsurface *
2427weston_surface_to_subsurface(struct weston_surface *surface)
2428{
2429 if (surface->configure == subsurface_configure)
2430 return surface->configure_private;
2431
2432 return NULL;
2433}
2434
Pekka Paalanen01388e22013-04-25 13:57:44 +03002435WL_EXPORT struct weston_surface *
2436weston_surface_get_main_surface(struct weston_surface *surface)
2437{
2438 struct weston_subsurface *sub;
2439
2440 while (surface && (sub = weston_surface_to_subsurface(surface)))
2441 surface = sub->parent;
2442
2443 return surface;
2444}
2445
Pekka Paalanene67858b2013-04-25 13:57:42 +03002446static void
2447subsurface_set_position(struct wl_client *client,
2448 struct wl_resource *resource, int32_t x, int32_t y)
2449{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002450 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002451
2452 if (!sub)
2453 return;
2454
2455 sub->position.x = x;
2456 sub->position.y = y;
2457 sub->position.set = 1;
2458}
2459
2460static struct weston_subsurface *
2461subsurface_from_surface(struct weston_surface *surface)
2462{
2463 struct weston_subsurface *sub;
2464
2465 sub = weston_surface_to_subsurface(surface);
2466 if (sub)
2467 return sub;
2468
2469 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2470 if (sub->surface == surface)
2471 return sub;
2472
2473 return NULL;
2474}
2475
2476static struct weston_subsurface *
2477subsurface_sibling_check(struct weston_subsurface *sub,
2478 struct weston_surface *surface,
2479 const char *request)
2480{
2481 struct weston_subsurface *sibling;
2482
2483 sibling = subsurface_from_surface(surface);
2484
2485 if (!sibling) {
2486 wl_resource_post_error(sub->resource,
2487 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2488 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002489 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002490 return NULL;
2491 }
2492
2493 if (sibling->parent != sub->parent) {
2494 wl_resource_post_error(sub->resource,
2495 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2496 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002497 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002498 return NULL;
2499 }
2500
2501 return sibling;
2502}
2503
2504static void
2505subsurface_place_above(struct wl_client *client,
2506 struct wl_resource *resource,
2507 struct wl_resource *sibling_resource)
2508{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002509 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002510 struct weston_surface *surface =
2511 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512 struct weston_subsurface *sibling;
2513
2514 if (!sub)
2515 return;
2516
2517 sibling = subsurface_sibling_check(sub, surface, "place_above");
2518 if (!sibling)
2519 return;
2520
2521 wl_list_remove(&sub->parent_link_pending);
2522 wl_list_insert(sibling->parent_link_pending.prev,
2523 &sub->parent_link_pending);
2524}
2525
2526static void
2527subsurface_place_below(struct wl_client *client,
2528 struct wl_resource *resource,
2529 struct wl_resource *sibling_resource)
2530{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002531 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002532 struct weston_surface *surface =
2533 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002534 struct weston_subsurface *sibling;
2535
2536 if (!sub)
2537 return;
2538
2539 sibling = subsurface_sibling_check(sub, surface, "place_below");
2540 if (!sibling)
2541 return;
2542
2543 wl_list_remove(&sub->parent_link_pending);
2544 wl_list_insert(&sibling->parent_link_pending,
2545 &sub->parent_link_pending);
2546}
2547
2548static void
2549subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2550{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002551 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002552
2553 if (sub)
2554 sub->synchronized = 1;
2555}
2556
2557static void
2558subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2559{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002560 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002561
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002562 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002563 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002564
2565 /* If sub became effectively desynchronized, flush. */
2566 if (!weston_subsurface_is_synchronized(sub))
2567 weston_subsurface_synchronized_commit(sub);
2568 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569}
2570
2571static void
2572weston_subsurface_cache_init(struct weston_subsurface *sub)
2573{
2574 pixman_region32_init(&sub->cached.damage);
2575 pixman_region32_init(&sub->cached.opaque);
2576 pixman_region32_init(&sub->cached.input);
2577 wl_list_init(&sub->cached.frame_callback_list);
2578 sub->cached.buffer_ref.buffer = NULL;
2579}
2580
2581static void
2582weston_subsurface_cache_fini(struct weston_subsurface *sub)
2583{
2584 struct weston_frame_callback *cb, *tmp;
2585
2586 wl_list_for_each_safe(cb, tmp, &sub->cached.frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002587 wl_resource_destroy(cb->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002588
2589 weston_buffer_reference(&sub->cached.buffer_ref, NULL);
2590 pixman_region32_fini(&sub->cached.damage);
2591 pixman_region32_fini(&sub->cached.opaque);
2592 pixman_region32_fini(&sub->cached.input);
2593}
2594
2595static void
2596weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2597{
2598 wl_list_remove(&sub->parent_link);
2599 wl_list_remove(&sub->parent_link_pending);
2600 wl_list_remove(&sub->parent_destroy_listener.link);
2601 sub->parent = NULL;
2602}
2603
2604static void
2605weston_subsurface_destroy(struct weston_subsurface *sub);
2606
2607static void
2608subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2609{
2610 struct weston_subsurface *sub =
2611 container_of(listener, struct weston_subsurface,
2612 surface_destroy_listener);
2613 assert(data == &sub->surface->resource);
2614
2615 /* The protocol object (wl_resource) is left inert. */
2616 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002617 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618
2619 weston_subsurface_destroy(sub);
2620}
2621
2622static void
2623subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2624{
2625 struct weston_subsurface *sub =
2626 container_of(listener, struct weston_subsurface,
2627 parent_destroy_listener);
2628 assert(data == &sub->parent->resource);
2629 assert(sub->surface != sub->parent);
2630
2631 if (weston_surface_is_mapped(sub->surface))
2632 weston_surface_unmap(sub->surface);
2633
2634 weston_subsurface_unlink_parent(sub);
2635}
2636
2637static void
2638subsurface_resource_destroy(struct wl_resource *resource)
2639{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002640 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641
2642 if (sub)
2643 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002644}
2645
2646static void
2647subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2648{
2649 wl_resource_destroy(resource);
2650}
2651
2652static void
2653weston_subsurface_link_parent(struct weston_subsurface *sub,
2654 struct weston_surface *parent)
2655{
2656 sub->parent = parent;
2657 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002658 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002659 &sub->parent_destroy_listener);
2660
2661 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2662 wl_list_insert(&parent->subsurface_list_pending,
2663 &sub->parent_link_pending);
2664}
2665
2666static void
2667weston_subsurface_link_surface(struct weston_subsurface *sub,
2668 struct weston_surface *surface)
2669{
2670 sub->surface = surface;
2671 sub->surface_destroy_listener.notify =
2672 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002673 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674 &sub->surface_destroy_listener);
2675}
2676
2677static void
2678weston_subsurface_destroy(struct weston_subsurface *sub)
2679{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002680 struct weston_view *view, *next;
2681
Pekka Paalanene67858b2013-04-25 13:57:42 +03002682 assert(sub->surface);
2683
2684 if (sub->resource) {
2685 assert(weston_surface_to_subsurface(sub->surface) == sub);
2686 assert(sub->parent_destroy_listener.notify ==
2687 subsurface_handle_parent_destroy);
2688
Jason Ekstranda7af7042013-10-12 22:38:11 -05002689 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link)
2690 weston_view_destroy(view);
2691
Pekka Paalanene67858b2013-04-25 13:57:42 +03002692 if (sub->parent)
2693 weston_subsurface_unlink_parent(sub);
2694
2695 weston_subsurface_cache_fini(sub);
2696
2697 sub->surface->configure = NULL;
2698 sub->surface->configure_private = NULL;
2699 } else {
2700 /* the dummy weston_subsurface for the parent itself */
2701 assert(sub->parent_destroy_listener.notify == NULL);
2702 wl_list_remove(&sub->parent_link);
2703 wl_list_remove(&sub->parent_link_pending);
2704 }
2705
2706 wl_list_remove(&sub->surface_destroy_listener.link);
2707 free(sub);
2708}
2709
2710static const struct wl_subsurface_interface subsurface_implementation = {
2711 subsurface_destroy,
2712 subsurface_set_position,
2713 subsurface_place_above,
2714 subsurface_place_below,
2715 subsurface_set_sync,
2716 subsurface_set_desync
2717};
2718
2719static struct weston_subsurface *
2720weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2721 struct weston_surface *parent)
2722{
2723 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002724 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725
2726 sub = calloc(1, sizeof *sub);
2727 if (!sub)
2728 return NULL;
2729
Jason Ekstranda7af7042013-10-12 22:38:11 -05002730 wl_list_init(&sub->unused_views);
2731
Jason Ekstranda85118c2013-06-27 20:17:02 -05002732 sub->resource =
2733 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002734 if (!sub->resource) {
2735 free(sub);
2736 return NULL;
2737 }
2738
Jason Ekstranda85118c2013-06-27 20:17:02 -05002739 wl_resource_set_implementation(sub->resource,
2740 &subsurface_implementation,
2741 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002742 weston_subsurface_link_surface(sub, surface);
2743 weston_subsurface_link_parent(sub, parent);
2744 weston_subsurface_cache_init(sub);
2745 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002746
2747 return sub;
2748}
2749
2750/* Create a dummy subsurface for having the parent itself in its
2751 * sub-surface lists. Makes stacking order manipulation easy.
2752 */
2753static struct weston_subsurface *
2754weston_subsurface_create_for_parent(struct weston_surface *parent)
2755{
2756 struct weston_subsurface *sub;
2757
2758 sub = calloc(1, sizeof *sub);
2759 if (!sub)
2760 return NULL;
2761
2762 weston_subsurface_link_surface(sub, parent);
2763 sub->parent = parent;
2764 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2765 wl_list_insert(&parent->subsurface_list_pending,
2766 &sub->parent_link_pending);
2767
2768 return sub;
2769}
2770
2771static void
2772subcompositor_get_subsurface(struct wl_client *client,
2773 struct wl_resource *resource,
2774 uint32_t id,
2775 struct wl_resource *surface_resource,
2776 struct wl_resource *parent_resource)
2777{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002778 struct weston_surface *surface =
2779 wl_resource_get_user_data(surface_resource);
2780 struct weston_surface *parent =
2781 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 struct weston_subsurface *sub;
2783 static const char where[] = "get_subsurface: wl_subsurface@";
2784
2785 if (surface == parent) {
2786 wl_resource_post_error(resource,
2787 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2788 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002789 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002790 return;
2791 }
2792
2793 if (weston_surface_to_subsurface(surface)) {
2794 wl_resource_post_error(resource,
2795 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2796 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002797 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002798 return;
2799 }
2800
2801 if (surface->configure) {
2802 wl_resource_post_error(resource,
2803 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2804 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002805 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002806 return;
2807 }
2808
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002809 if (weston_surface_get_main_surface(parent) == surface) {
2810 wl_resource_post_error(resource,
2811 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2812 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002813 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002814 return;
2815 }
2816
Pekka Paalanene67858b2013-04-25 13:57:42 +03002817 /* make sure the parent is in its own list */
2818 if (wl_list_empty(&parent->subsurface_list)) {
2819 if (!weston_subsurface_create_for_parent(parent)) {
2820 wl_resource_post_no_memory(resource);
2821 return;
2822 }
2823 }
2824
2825 sub = weston_subsurface_create(id, surface, parent);
2826 if (!sub) {
2827 wl_resource_post_no_memory(resource);
2828 return;
2829 }
2830
2831 surface->configure = subsurface_configure;
2832 surface->configure_private = sub;
2833}
2834
2835static void
2836subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2837{
2838 wl_resource_destroy(resource);
2839}
2840
2841static const struct wl_subcompositor_interface subcompositor_interface = {
2842 subcompositor_destroy,
2843 subcompositor_get_subsurface
2844};
2845
2846static void
2847bind_subcompositor(struct wl_client *client,
2848 void *data, uint32_t version, uint32_t id)
2849{
2850 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002851 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002852
Jason Ekstranda85118c2013-06-27 20:17:02 -05002853 resource =
2854 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002855 if (resource == NULL) {
2856 wl_client_post_no_memory(client);
2857 return;
2858 }
2859 wl_resource_set_implementation(resource, &subcompositor_interface,
2860 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002861}
2862
2863static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002864weston_compositor_dpms(struct weston_compositor *compositor,
2865 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002866{
2867 struct weston_output *output;
2868
2869 wl_list_for_each(output, &compositor->output_list, link)
2870 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002871 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002872}
2873
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002874WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002875weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002876{
Neil Roberts8b62e202013-09-30 13:14:47 +01002877 uint32_t old_state = compositor->state;
2878
2879 /* The state needs to be changed before emitting the wake
2880 * signal because that may try to schedule a repaint which
2881 * will not work if the compositor is still sleeping */
2882 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2883
2884 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002885 case WESTON_COMPOSITOR_SLEEPING:
2886 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2887 /* fall through */
2888 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002889 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002890 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002891 /* fall through */
2892 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002893 wl_event_source_timer_update(compositor->idle_source,
2894 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002895 }
2896}
2897
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002898WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002899weston_compositor_offscreen(struct weston_compositor *compositor)
2900{
2901 switch (compositor->state) {
2902 case WESTON_COMPOSITOR_OFFSCREEN:
2903 return;
2904 case WESTON_COMPOSITOR_SLEEPING:
2905 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
2906 /* fall through */
2907 default:
2908 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
2909 wl_event_source_timer_update(compositor->idle_source, 0);
2910 }
2911}
2912
2913WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002914weston_compositor_sleep(struct weston_compositor *compositor)
2915{
2916 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
2917 return;
2918
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002919 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002920 compositor->state = WESTON_COMPOSITOR_SLEEPING;
2921 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
2922}
2923
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002924static int
2925idle_handler(void *data)
2926{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002927 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002928
2929 if (compositor->idle_inhibit)
2930 return 1;
2931
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02002932 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02002933 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002934
2935 return 1;
2936}
2937
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002938WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08002939weston_plane_init(struct weston_plane *plane,
2940 struct weston_compositor *ec,
2941 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002942{
2943 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002944 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002945 plane->x = x;
2946 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08002947 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002948
2949 /* Init the link so that the call to wl_list_remove() when releasing
2950 * the plane without ever stacking doesn't lead to a crash */
2951 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002952}
2953
2954WL_EXPORT void
2955weston_plane_release(struct weston_plane *plane)
2956{
Xiong Zhang97116532013-10-23 13:58:31 +08002957 struct weston_view *view;
2958
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002959 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002960 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002961
Xiong Zhang97116532013-10-23 13:58:31 +08002962 wl_list_for_each(view, &plane->compositor->view_list, link) {
2963 if (view->plane == plane)
2964 view->plane = NULL;
2965 }
2966
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03002967 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002968}
2969
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002970WL_EXPORT void
2971weston_compositor_stack_plane(struct weston_compositor *ec,
2972 struct weston_plane *plane,
2973 struct weston_plane *above)
2974{
2975 if (above)
2976 wl_list_insert(above->link.prev, &plane->link);
2977 else
2978 wl_list_insert(&ec->plane_list, &plane->link);
2979}
2980
Casey Dahlin9074db52012-04-19 22:50:09 -04002981static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002982{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05002983 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002984}
2985
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002986static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002987bind_output(struct wl_client *client,
2988 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002989{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002990 struct weston_output *output = data;
2991 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002992 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002993
Jason Ekstranda85118c2013-06-27 20:17:02 -05002994 resource = wl_resource_create(client, &wl_output_interface,
2995 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002996 if (resource == NULL) {
2997 wl_client_post_no_memory(client);
2998 return;
2999 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003000
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003001 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003002 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003003
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003004 wl_output_send_geometry(resource,
3005 output->x,
3006 output->y,
3007 output->mm_width,
3008 output->mm_height,
3009 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003010 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003011 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003012 if (version >= 2)
3013 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003014 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003015
3016 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003017 wl_output_send_mode(resource,
3018 mode->flags,
3019 mode->width,
3020 mode->height,
3021 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003022 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003023
3024 if (version >= 2)
3025 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003026}
3027
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003028/* Move other outputs when one is removed so the space remains contiguos. */
3029static void
3030weston_compositor_remove_output(struct weston_compositor *compositor,
3031 struct weston_output *remove_output)
3032{
3033 struct weston_output *output;
3034 int offset = 0;
3035
3036 wl_list_for_each(output, &compositor->output_list, link) {
3037 if (output == remove_output) {
3038 offset = output->width;
3039 continue;
3040 }
3041
3042 if (offset > 0) {
3043 weston_output_move(output,
3044 output->x - offset, output->y);
3045 output->dirty = 1;
3046 }
3047 }
3048}
3049
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003050WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003051weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003052{
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003053 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003054 wl_list_remove(&output->link);
3055
Richard Hughes64ddde12013-05-01 21:52:10 +01003056 wl_signal_emit(&output->destroy_signal, output);
3057
Richard Hughesafe690c2013-05-02 10:10:04 +01003058 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003059 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003060 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003061 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003062
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003063 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003064}
3065
Scott Moreau1bad5db2012-08-18 01:04:05 -06003066static void
3067weston_output_compute_transform(struct weston_output *output)
3068{
3069 struct weston_matrix transform;
3070 int flip;
3071
3072 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003073 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003074
3075 switch(output->transform) {
3076 case WL_OUTPUT_TRANSFORM_FLIPPED:
3077 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3078 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3079 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003080 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003081 flip = -1;
3082 break;
3083 default:
3084 flip = 1;
3085 break;
3086 }
3087
3088 switch(output->transform) {
3089 case WL_OUTPUT_TRANSFORM_NORMAL:
3090 case WL_OUTPUT_TRANSFORM_FLIPPED:
3091 transform.d[0] = flip;
3092 transform.d[1] = 0;
3093 transform.d[4] = 0;
3094 transform.d[5] = 1;
3095 break;
3096 case WL_OUTPUT_TRANSFORM_90:
3097 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3098 transform.d[0] = 0;
3099 transform.d[1] = -flip;
3100 transform.d[4] = 1;
3101 transform.d[5] = 0;
3102 break;
3103 case WL_OUTPUT_TRANSFORM_180:
3104 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3105 transform.d[0] = -flip;
3106 transform.d[1] = 0;
3107 transform.d[4] = 0;
3108 transform.d[5] = -1;
3109 break;
3110 case WL_OUTPUT_TRANSFORM_270:
3111 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3112 transform.d[0] = 0;
3113 transform.d[1] = flip;
3114 transform.d[4] = -1;
3115 transform.d[5] = 0;
3116 break;
3117 default:
3118 break;
3119 }
3120
3121 weston_matrix_multiply(&output->matrix, &transform);
3122}
3123
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003124WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003125weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003126{
Scott Moreau850ca422012-05-21 15:21:25 -06003127 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003128 struct weston_matrix camera;
3129 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003131 weston_matrix_init(&output->matrix);
3132 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003133 -(output->x + output->width / 2.0),
3134 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003135
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003136 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003137 2.0 / output->width,
3138 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003139
3140 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003141
Scott Moreauccbf29d2012-02-22 14:21:41 -07003142 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003143 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003144 weston_matrix_init(&camera);
3145 weston_matrix_init(&modelview);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003146 weston_output_update_zoom(output);
Scott Moreaue6603982012-06-11 13:07:51 -06003147 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003148 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003149 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003150 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003151 weston_matrix_multiply(&output->matrix, &modelview);
3152 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003153
Scott Moreauccbf29d2012-02-22 14:21:41 -07003154 output->dirty = 0;
3155}
3156
Scott Moreau1bad5db2012-08-18 01:04:05 -06003157static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003158weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003159{
3160 output->transform = transform;
3161
3162 switch (transform) {
3163 case WL_OUTPUT_TRANSFORM_90:
3164 case WL_OUTPUT_TRANSFORM_270:
3165 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3166 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3167 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003168 output->width = output->current_mode->height;
3169 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003170 break;
3171 case WL_OUTPUT_TRANSFORM_NORMAL:
3172 case WL_OUTPUT_TRANSFORM_180:
3173 case WL_OUTPUT_TRANSFORM_FLIPPED:
3174 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003175 output->width = output->current_mode->width;
3176 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003177 break;
3178 default:
3179 break;
3180 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003181
Hardening57388e42013-09-18 23:56:36 +02003182 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003183 output->width /= scale;
3184 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003185}
3186
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003187static void
3188weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003189{
3190 output->x = x;
3191 output->y = y;
3192
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003193 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003194 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003195 output->width,
3196 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003197}
3198
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003199WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003200weston_output_move(struct weston_output *output, int x, int y)
3201{
3202 pixman_region32_t old_region;
3203 struct wl_resource *resource;
3204
3205 output->move_x = x - output->x;
3206 output->move_y = y - output->y;
3207
3208 if (output->move_x == 0 && output->move_y == 0)
3209 return;
3210
3211 pixman_region32_init(&old_region);
3212 pixman_region32_copy(&old_region, &output->region);
3213
3214 weston_output_init_geometry(output, x, y);
3215
3216 output->dirty = 1;
3217
3218 /* Move views on this output. */
3219 wl_signal_emit(&output->move_signal, output);
3220
3221 /* Notify clients of the change for output position. */
3222 wl_resource_for_each(resource, &output->resource_list)
3223 wl_output_send_geometry(resource,
3224 output->x,
3225 output->y,
3226 output->mm_width,
3227 output->mm_height,
3228 output->subpixel,
3229 output->make,
3230 output->model,
3231 output->transform);
3232}
3233
3234WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003235weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003236 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003237 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003238{
3239 output->compositor = c;
3240 output->x = x;
3241 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003242 output->mm_width = mm_width;
3243 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003244 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003245 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003246
Alexander Larsson0b135062013-05-28 16:23:36 +02003247 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003248 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003249
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003250 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003251 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003252
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003253 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003254 wl_signal_init(&output->destroy_signal);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003255 wl_signal_init(&output->move_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003256 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003257 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003258
Casey Dahlin58ba1372012-04-19 22:50:08 -04003259 output->id = ffs(~output->compositor->output_id_pool) - 1;
3260 output->compositor->output_id_pool |= 1 << output->id;
3261
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003262 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003263 wl_global_create(c->wl_display, &wl_output_interface, 2,
3264 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003265 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003266}
3267
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003268WL_EXPORT void
3269weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003270 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003271 wl_fixed_t *x, wl_fixed_t *y)
3272{
3273 wl_fixed_t tx, ty;
3274 wl_fixed_t width, height;
3275
Hardeningff39efa2013-09-18 23:56:35 +02003276 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3277 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003278
3279 switch(output->transform) {
3280 case WL_OUTPUT_TRANSFORM_NORMAL:
3281 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003282 tx = device_x;
3283 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003284 break;
3285 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003286 tx = device_y;
3287 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003288 break;
3289 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003290 tx = width - device_x;
3291 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003292 break;
3293 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003294 tx = width - device_y;
3295 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003296 break;
3297 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003298 tx = width - device_x;
3299 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003300 break;
3301 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003302 tx = width - device_y;
3303 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003304 break;
3305 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003306 tx = device_x;
3307 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003308 break;
3309 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003310 tx = device_y;
3311 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003312 break;
3313 }
3314
Hardeningff39efa2013-09-18 23:56:35 +02003315 *x = tx / output->current_scale + wl_fixed_from_int(output->x);
3316 *y = ty / output->current_scale + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003317}
3318
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003319static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003320compositor_bind(struct wl_client *client,
3321 void *data, uint32_t version, uint32_t id)
3322{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003323 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003324 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003325
Jason Ekstranda85118c2013-06-27 20:17:02 -05003326 resource = wl_resource_create(client, &wl_compositor_interface,
3327 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003328 if (resource == NULL) {
3329 wl_client_post_no_memory(client);
3330 return;
3331 }
3332
3333 wl_resource_set_implementation(resource, &compositor_interface,
3334 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003335}
3336
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003337static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003338log_uname(void)
3339{
3340 struct utsname usys;
3341
3342 uname(&usys);
3343
3344 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3345 usys.version, usys.machine);
3346}
3347
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003348WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003349weston_environment_get_fd(const char *env)
3350{
3351 char *e, *end;
3352 int fd, flags;
3353
3354 e = getenv(env);
3355 if (!e)
3356 return -1;
3357 fd = strtol(e, &end, 0);
3358 if (*end != '\0')
3359 return -1;
3360
3361 flags = fcntl(fd, F_GETFD);
3362 if (flags == -1)
3363 return -1;
3364
3365 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3366 unsetenv(env);
3367
3368 return fd;
3369}
3370
3371WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003372weston_compositor_init(struct weston_compositor *ec,
3373 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003374 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003375 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003376{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003377 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003378 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003379 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003380
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003381 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003382 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003383 wl_signal_init(&ec->destroy_signal);
3384 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003385 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003386 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003387 wl_signal_init(&ec->idle_signal);
3388 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003389 wl_signal_init(&ec->show_input_panel_signal);
3390 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003391 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003392 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003393 wl_signal_init(&ec->output_created_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003394 wl_signal_init(&ec->session_signal);
3395 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003396
Casey Dahlin58ba1372012-04-19 22:50:08 -04003397 ec->output_id_pool = 0;
3398
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003399 if (!wl_global_create(display, &wl_compositor_interface, 3,
3400 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003401 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003402
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003403 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3404 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003405 return -1;
3406
Jason Ekstranda7af7042013-10-12 22:38:11 -05003407 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003408 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003409 wl_list_init(&ec->layer_list);
3410 wl_list_init(&ec->seat_list);
3411 wl_list_init(&ec->output_list);
3412 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003413 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003414 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003415 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003416 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003417 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003418
Xiong Zhang97116532013-10-23 13:58:31 +08003419 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003420 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003421
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003422 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3423 weston_config_section_get_string(s, "keymap_rules",
3424 (char **) &xkb_names.rules, NULL);
3425 weston_config_section_get_string(s, "keymap_model",
3426 (char **) &xkb_names.model, NULL);
3427 weston_config_section_get_string(s, "keymap_layout",
3428 (char **) &xkb_names.layout, NULL);
3429 weston_config_section_get_string(s, "keymap_variant",
3430 (char **) &xkb_names.variant, NULL);
3431 weston_config_section_get_string(s, "keymap_options",
3432 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003433
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003434 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3435 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003436
3437 ec->ping_handler = NULL;
3438
3439 screenshooter_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003440 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003441
3442 wl_data_device_manager_init(ec->wl_display);
3443
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003444 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003445
Daniel Stone725c2c32012-06-22 14:04:36 +01003446 loop = wl_display_get_event_loop(ec->wl_display);
3447 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3448 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3449
3450 ec->input_loop = wl_event_loop_create();
3451
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003452 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3453 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3454
3455 weston_compositor_schedule_repaint(ec);
3456
Daniel Stone725c2c32012-06-22 14:04:36 +01003457 return 0;
3458}
3459
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003460WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003461weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003462{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003463 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003464
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003465 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003466 if (ec->input_loop_source)
3467 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003468
Matt Roper361d2ad2011-08-29 13:52:23 -07003469 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003470 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003471 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003472
Daniel Stone325fc2d2012-05-30 16:31:58 +01003473 weston_binding_list_destroy_all(&ec->key_binding_list);
3474 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003475 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003476 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003477 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003478
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003479 weston_plane_release(&ec->primary_plane);
3480
Jonas Ådahlc97af922012-03-28 22:36:09 +02003481 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003482
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003483 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003484}
3485
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003486WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003487weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3488 const struct weston_pointer_grab_interface *interface)
3489{
3490 struct weston_seat *seat;
3491
3492 ec->default_pointer_grab = interface;
3493 wl_list_for_each(seat, &ec->seat_list, link) {
3494 if (seat->pointer) {
3495 weston_pointer_set_default_grab(seat->pointer,
3496 interface);
3497 }
3498 }
3499}
3500
3501WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003502weston_version(int *major, int *minor, int *micro)
3503{
3504 *major = WESTON_VERSION_MAJOR;
3505 *minor = WESTON_VERSION_MINOR;
3506 *micro = WESTON_VERSION_MICRO;
3507}
3508
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003509static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003510 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003511 const char *desc;
3512} capability_strings[] = {
3513 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003514 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003515};
3516
3517static void
3518weston_compositor_log_capabilities(struct weston_compositor *compositor)
3519{
3520 unsigned i;
3521 int yes;
3522
3523 weston_log("Compositor capabilities:\n");
3524 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3525 yes = compositor->capabilities & capability_strings[i].bit;
3526 weston_log_continue(STAMP_SPACE "%s %s\n",
3527 capability_strings[i].desc,
3528 yes ? "yes" : "no");
3529 }
3530}
3531
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003532static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003533{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003534 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003535
Martin Minarik6d118362012-06-07 18:01:59 +02003536 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003537 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003538
3539 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003540}
3541
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003542#ifdef HAVE_LIBUNWIND
3543
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003544static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003545print_backtrace(void)
3546{
3547 unw_cursor_t cursor;
3548 unw_context_t context;
3549 unw_word_t off;
3550 unw_proc_info_t pip;
3551 int ret, i = 0;
3552 char procname[256];
3553 const char *filename;
3554 Dl_info dlinfo;
3555
3556 pip.unwind_info = NULL;
3557 ret = unw_getcontext(&context);
3558 if (ret) {
3559 weston_log("unw_getcontext: %d\n", ret);
3560 return;
3561 }
3562
3563 ret = unw_init_local(&cursor, &context);
3564 if (ret) {
3565 weston_log("unw_init_local: %d\n", ret);
3566 return;
3567 }
3568
3569 ret = unw_step(&cursor);
3570 while (ret > 0) {
3571 ret = unw_get_proc_info(&cursor, &pip);
3572 if (ret) {
3573 weston_log("unw_get_proc_info: %d\n", ret);
3574 break;
3575 }
3576
3577 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3578 if (ret && ret != -UNW_ENOMEM) {
3579 if (ret != -UNW_EUNSPEC)
3580 weston_log("unw_get_proc_name: %d\n", ret);
3581 procname[0] = '?';
3582 procname[1] = 0;
3583 }
3584
3585 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3586 *dlinfo.dli_fname)
3587 filename = dlinfo.dli_fname;
3588 else
3589 filename = "?";
3590
3591 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3592 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3593
3594 ret = unw_step(&cursor);
3595 if (ret < 0)
3596 weston_log("unw_step: %d\n", ret);
3597 }
3598}
3599
3600#else
3601
3602static void
3603print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003604{
3605 void *buffer[32];
3606 int i, count;
3607 Dl_info info;
3608
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003609 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3610 for (i = 0; i < count; i++) {
3611 dladdr(buffer[i], &info);
3612 weston_log(" [%016lx] %s (%s)\n",
3613 (long) buffer[i],
3614 info.dli_sname ? info.dli_sname : "--",
3615 info.dli_fname);
3616 }
3617}
3618
3619#endif
3620
3621static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003622on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003623{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003624 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003625 * then call the backend restore function, which will switch
3626 * back to the vt we launched from or ungrab X etc and then
3627 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003628 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003629 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003630 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003631
Peter Maatmane5b42e42013-03-27 22:38:53 +01003632 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003633
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003634 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003635
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003636 segv_compositor->restore(segv_compositor);
3637
3638 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003639}
3640
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003641WL_EXPORT void *
3642weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003643{
3644 char path[PATH_MAX];
3645 void *module, *init;
3646
3647 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003648 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003649 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003650 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003651
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003652 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3653 if (module) {
3654 weston_log("Module '%s' already loaded\n", path);
3655 dlclose(module);
3656 return NULL;
3657 }
3658
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003659 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003660 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003661 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003662 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003663 return NULL;
3664 }
3665
3666 init = dlsym(module, entrypoint);
3667 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003668 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003669 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003670 return NULL;
3671 }
3672
3673 return init;
3674}
3675
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003676static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003677load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003678 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003679{
3680 const char *p, *end;
3681 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003682 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07003683 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003684
3685 if (modules == NULL)
3686 return 0;
3687
3688 p = modules;
3689 while (*p) {
3690 end = strchrnul(p, ',');
3691 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003692 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003693 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07003694 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003695 p = end;
3696 while (*p == ',')
3697 p++;
3698
3699 }
3700
3701 return 0;
3702}
3703
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003704static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003705 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3706
3707static const char xdg_wrong_message[] =
3708 "fatal: environment variable XDG_RUNTIME_DIR\n"
3709 "is set to \"%s\", which is not a directory.\n";
3710
3711static const char xdg_wrong_mode_message[] =
3712 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3713 "correctly. Unix access mode must be 0700 but is %o,\n"
3714 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003715 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003716
3717static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003718 "Refer to your distribution on how to get it, or\n"
3719 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3720 "on how to implement it.\n";
3721
Martin Minarik37032f82012-06-18 20:15:18 +02003722static void
3723verify_xdg_runtime_dir(void)
3724{
3725 char *dir = getenv("XDG_RUNTIME_DIR");
3726 struct stat s;
3727
3728 if (!dir) {
3729 weston_log(xdg_error_message);
3730 weston_log_continue(xdg_detail_message);
3731 exit(EXIT_FAILURE);
3732 }
3733
3734 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3735 weston_log(xdg_wrong_message, dir);
3736 weston_log_continue(xdg_detail_message);
3737 exit(EXIT_FAILURE);
3738 }
3739
3740 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3741 weston_log(xdg_wrong_mode_message,
3742 dir, s.st_mode & 0777, s.st_uid);
3743 weston_log_continue(xdg_detail_message);
3744 }
3745}
3746
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003747static int
3748usage(int error_code)
3749{
3750 fprintf(stderr,
3751 "Usage: weston [OPTIONS]\n\n"
3752 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3753 "Weston supports multiple backends, and depending on which backend is in use\n"
3754 "different options will be accepted.\n\n"
3755
3756
3757 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003758 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003759 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003760 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
3761 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05003762 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003763 " -S, --socket=NAME\tName of socket to listen on\n"
3764 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003765 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003766 " --log==FILE\t\tLog to the given file\n"
3767 " -h, --help\t\tThis help message\n\n");
3768
3769 fprintf(stderr,
3770 "Options for drm-backend.so:\n\n"
3771 " --connector=ID\tBring up only this connector\n"
3772 " --seat=SEAT\t\tThe seat that weston should run on\n"
3773 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003774 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003775 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3776
3777 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01003778 "Options for fbdev-backend.so:\n\n"
3779 " --tty=TTY\t\tThe tty to use\n"
3780 " --device=DEVICE\tThe framebuffer device to use\n\n");
3781
3782 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003783 "Options for x11-backend.so:\n\n"
3784 " --width=WIDTH\t\tWidth of X window\n"
3785 " --height=HEIGHT\tHeight of X window\n"
3786 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003787 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003788 " --output-count=COUNT\tCreate multiple outputs\n"
3789 " --no-input\t\tDont create input devices\n\n");
3790
3791 fprintf(stderr,
3792 "Options for wayland-backend.so:\n\n"
3793 " --width=WIDTH\t\tWidth of Wayland surface\n"
3794 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06003795 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06003796 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003797 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05003798 " --output-count=COUNT\tCreate multiple outputs\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003799 " --display=DISPLAY\tWayland display to connect to\n\n");
3800
Pekka Paalanene31e0532013-05-22 18:03:07 +03003801#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
3802 fprintf(stderr,
3803 "Options for rpi-backend.so:\n\n"
3804 " --tty=TTY\t\tThe tty to use\n"
3805 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
3806 " --transform=TR\tThe output transformation, TR is one of:\n"
3807 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01003808 " --opaque-regions\tEnable support for opaque regions, can be "
3809 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03003810 "\n");
3811#endif
3812
Hardeningc077a842013-07-08 00:51:35 +02003813#if defined(BUILD_RDP_COMPOSITOR)
3814 fprintf(stderr,
3815 "Options for rdp-backend.so:\n\n"
3816 " --width=WIDTH\t\tWidth of desktop\n"
3817 " --height=HEIGHT\tHeight of desktop\n"
3818 " --extra-modes=MODES\t\n"
3819 " --env-socket=SOCKET\tUse that socket as peer connection\n"
3820 " --address=ADDR\tThe address to bind\n"
3821 " --port=PORT\tThe port to listen on\n"
3822 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
3823 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
3824 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
3825 "\n");
3826#endif
3827
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003828 exit(error_code);
3829}
3830
Peter Maatmane5b42e42013-03-27 22:38:53 +01003831static void
3832catch_signals(void)
3833{
3834 struct sigaction action;
3835
3836 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3837 action.sa_sigaction = on_caught_signal;
3838 sigemptyset(&action.sa_mask);
3839 sigaction(SIGSEGV, &action, NULL);
3840 sigaction(SIGABRT, &action, NULL);
3841}
3842
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003843int main(int argc, char *argv[])
3844{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003845 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003846 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003847 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003848 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003849 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003850 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003851 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003852 int *argc, char *argv[],
3853 struct weston_config *config);
Kristian Høgsberg1abe0482013-09-21 23:02:31 -07003854 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003855 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003856 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05003857 char *shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003858 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003859 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003860 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003861 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003862 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003863 int32_t version = 0;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003864 struct weston_config *config;
3865 struct weston_config_section *section;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003866
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003867 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003868 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
Jason Ekstranda985da42013-08-22 17:28:03 -05003869 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003870 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3871 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003872 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003873 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003874 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003875 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003876 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003877
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003878 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003879
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003880 if (help)
3881 usage(EXIT_SUCCESS);
3882
Scott Moreau12245142012-08-29 15:15:58 -06003883 if (version) {
3884 printf(PACKAGE_STRING "\n");
3885 return EXIT_SUCCESS;
3886 }
3887
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003888 weston_log_file_open(log);
3889
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003890 weston_log("%s\n"
3891 STAMP_SPACE "%s\n"
3892 STAMP_SPACE "Bug reports to: %s\n"
3893 STAMP_SPACE "Build: %s\n",
3894 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003895 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003896 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003897
Martin Minarik37032f82012-06-18 20:15:18 +02003898 verify_xdg_runtime_dir();
3899
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003900 display = wl_display_create();
3901
Tiago Vignatti2116b892011-08-08 05:52:59 -07003902 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003903 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3904 display);
3905 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3906 display);
3907 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3908 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003909
3910 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003911 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3912 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003913
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01003914 config = weston_config_parse("weston.ini");
3915 if (config != NULL) {
3916 weston_log("Using config file '%s'\n",
3917 weston_config_get_full_path(config));
3918 } else {
3919 weston_log("Starting with no config file.\n");
3920 }
3921 section = weston_config_get_section(config, "core", NULL, NULL);
3922
3923 weston_config_section_get_string(section, "backend", &backend, NULL);
3924 if (option_backend) {
3925 backend = option_backend;
3926 }
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003927 if (!backend) {
3928 if (getenv("WAYLAND_DISPLAY"))
3929 backend = "wayland-backend.so";
3930 else if (getenv("DISPLAY"))
3931 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003932 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003933 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003934 }
3935
Jason Ekstranda985da42013-08-22 17:28:03 -05003936 weston_config_section_get_string(section, "modules", &modules, "");
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003937
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003938 backend_init = weston_load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003939 if (!backend_init)
3940 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003941
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003942 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003943 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003944 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003945 exit(EXIT_FAILURE);
3946 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003947
Peter Maatmane5b42e42013-03-27 22:38:53 +01003948 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003949 segv_compositor = ec;
3950
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003951 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003952 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003953
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003954 setenv("WAYLAND_DISPLAY", socket_name, 1);
3955
Jason Ekstranda985da42013-08-22 17:28:03 -05003956 if (!shell)
3957 weston_config_section_get_string(section, "shell", &shell,
3958 "desktop-shell.so");
3959 if (load_modules(ec, shell, &argc, argv) < 0)
3960 goto out;
3961
Ossama Othmana50e6e42013-05-14 09:48:26 -07003962 if (load_modules(ec, modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003963 goto out;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003964 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003965 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003966
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05003967 for (i = 1; i < argc; i++)
3968 weston_log("fatal: unhandled option: %s\n", argv[i]);
3969 if (argc > 1) {
3970 ret = EXIT_FAILURE;
3971 goto out;
3972 }
3973
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003974 weston_compositor_log_capabilities(ec);
3975
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003976 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003977 weston_log("fatal: failed to add socket: %m\n");
3978 ret = EXIT_FAILURE;
3979 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003980 }
3981
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003982 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003983
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003984 wl_display_run(display);
3985
3986 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003987 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003988 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003989
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003990 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003991
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003992 for (i = ARRAY_LENGTH(signals); i;)
3993 wl_event_source_remove(signals[--i]);
3994
Daniel Stone855028f2012-05-01 20:37:10 +01003995 weston_compositor_xkb_destroy(ec);
3996
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003997 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003998 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003999
Martin Minarik19e6f262012-06-07 13:08:46 +02004000 weston_log_file_close();
4001
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004002 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004003}