blob: 02f569f66af01c325db66f4c054e3a658b63cc5f [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"
Jonny Lamb8ae35902013-11-26 18:19:45 +010056#include "scaler-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030057#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040058#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050059#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050060
Kristian Høgsberg27da5382011-06-21 17:32:25 -040061static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040062static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063
64static int
65sigchld_handler(int signal_number, void *data)
66{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050067 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040068 int status;
69 pid_t pid;
70
Bill Spitzak027b9622012-03-17 15:22:03 -070071 pid = waitpid(-1, &status, WNOHANG);
72 if (!pid)
73 return 1;
74
Kristian Høgsberg27da5382011-06-21 17:32:25 -040075 wl_list_for_each(p, &child_process_list, link) {
76 if (p->pid == pid)
77 break;
78 }
79
80 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020081 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040082 return 1;
83 }
84
85 wl_list_remove(&p->link);
86 p->cleanup(p, status);
87
88 return 1;
89}
90
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020091static void
Alexander Larsson0b135062013-05-28 16:23:36 +020092weston_output_transform_scale_init(struct weston_output *output,
93 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094
Rob Bradford27b17932013-06-26 18:08:46 +010095static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050096weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010097
Alex Wu2dda6042012-04-17 17:20:47 +080098WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +020099weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
100 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800101{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200102 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200103 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200105 int ret, notify_mode_changed, notify_scale_changed;
106 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107
Alex Wu2dda6042012-04-17 17:20:47 +0800108 if (!output->switch_mode)
109 return -1;
110
Hardening57388e42013-09-18 23:56:36 +0200111 temporary_mode = (output->original_mode != 0);
112 temporary_scale = (output->current_scale != output->original_scale);
113 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200114
Hardening57388e42013-09-18 23:56:36 +0200115 notify_mode_changed = 0;
116 notify_scale_changed = 0;
117 switch(op) {
118 case WESTON_MODE_SWITCH_SET_NATIVE:
119 output->native_mode = mode;
120 if (!temporary_mode) {
121 notify_mode_changed = 1;
122 ret = output->switch_mode(output, mode);
123 if (ret < 0)
124 return ret;
125 }
126
127 output->native_scale = scale;
128 if(!temporary_scale)
129 notify_scale_changed = 1;
130 break;
131 case WESTON_MODE_SWITCH_SET_TEMPORARY:
132 if (!temporary_mode)
133 output->original_mode = output->native_mode;
134 if (!temporary_scale)
135 output->original_scale = output->native_scale;
136
137 ret = output->switch_mode(output, mode);
138 if (ret < 0)
139 return ret;
140
Hardening57388e42013-09-18 23:56:36 +0200141 output->current_scale = scale;
142 break;
143 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
144 if (!temporary_mode) {
145 weston_log("already in the native mode\n");
146 return -1;
147 }
148
149 notify_mode_changed = (output->original_mode != output->native_mode);
150
151 ret = output->switch_mode(output, mode);
152 if (ret < 0)
153 return ret;
154
155 if (output->original_scale != output->native_scale) {
156 notify_scale_changed = 1;
157 scale = output->native_scale;
158 output->original_scale = scale;
159 }
160 output->original_mode = 0;
161
162 output->current_scale = output->native_scale;
163 break;
164 default:
165 weston_log("unknown weston_switch_mode_op %d\n", op);
166 break;
167 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200168
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200169 pixman_region32_init(&old_output_region);
170 pixman_region32_copy(&old_output_region, &output->region);
171
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200172 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200173 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174
175 pixman_region32_init(&output->previous_damage);
176 pixman_region32_init_rect(&output->region, output->x, output->y,
177 output->width, output->height);
178
179 weston_output_update_matrix(output);
180
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200181 /* If a pointer falls outside the outputs new geometry, move it to its
182 * lower-right corner */
183 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400184 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200185 int32_t x, y;
186
187 if (!pointer)
188 continue;
189
190 x = wl_fixed_to_int(pointer->x);
191 y = wl_fixed_to_int(pointer->y);
192
193 if (!pixman_region32_contains_point(&old_output_region,
194 x, y, NULL) ||
195 pixman_region32_contains_point(&output->region,
196 x, y, NULL))
197 continue;
198
199 if (x >= output->x + output->width)
200 x = output->x + output->width - 1;
201 if (y >= output->y + output->height)
202 y = output->y + output->height - 1;
203
204 pointer->x = wl_fixed_from_int(x);
205 pointer->y = wl_fixed_from_int(y);
206 }
207
208 pixman_region32_fini(&old_output_region);
209
Hardening57388e42013-09-18 23:56:36 +0200210 /* notify clients of the changes */
211 if (notify_mode_changed || notify_scale_changed) {
212 wl_resource_for_each(resource, &output->resource_list) {
213 if(notify_mode_changed) {
214 wl_output_send_mode(resource,
215 mode->flags | WL_OUTPUT_MODE_CURRENT,
216 mode->width,
217 mode->height,
218 mode->refresh);
219 }
220
221 if (notify_scale_changed)
222 wl_output_send_scale(resource, scale);
223
224 if (wl_resource_get_version(resource) >= 2)
225 wl_output_send_done(resource);
226 }
227 }
228
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200229 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800230}
231
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400232WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500233weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234{
235 wl_list_insert(&child_process_list, &process->link);
236}
237
Benjamin Franzke06286262011-05-06 19:12:33 +0200238static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200239child_client_exec(int sockfd, const char *path)
240{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500241 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200243 sigset_t allsigs;
244
245 /* do not give our signal mask to the new process */
246 sigfillset(&allsigs);
247 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200248
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100249 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
250 if (seteuid(getuid()) == -1) {
251 weston_log("compositor: failed seteuid\n");
252 return;
253 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500254
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500255 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
256 * non-CLOEXEC fd to pass through exec. */
257 clientfd = dup(sockfd);
258 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200259 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500260 return;
261 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200262
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264 setenv("WAYLAND_SOCKET", s, 1);
265
266 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200267 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200268 path);
269}
270
271WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272weston_client_launch(struct weston_compositor *compositor,
273 struct weston_process *proc,
274 const char *path,
275 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200276{
277 int sv[2];
278 pid_t pid;
279 struct wl_client *client;
280
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300281 weston_log("launching '%s'\n", path);
282
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300283 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200284 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200285 "socketpair failed while launching '%s': %m\n",
286 path);
287 return NULL;
288 }
289
290 pid = fork();
291 if (pid == -1) {
292 close(sv[0]);
293 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200294 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295 "fork failed while launching '%s': %m\n", path);
296 return NULL;
297 }
298
299 if (pid == 0) {
300 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700301 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200302 }
303
304 close(sv[1]);
305
306 client = wl_client_create(compositor->wl_display, sv[0]);
307 if (!client) {
308 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200309 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200310 "wl_client_create failed while launching '%s'.\n",
311 path);
312 return NULL;
313 }
314
315 proc->pid = pid;
316 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500317 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318
319 return client;
320}
321
322static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300323region_init_infinite(pixman_region32_t *region)
324{
325 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
326 UINT32_MAX, UINT32_MAX);
327}
328
Pekka Paalanene67858b2013-04-25 13:57:42 +0300329static struct weston_subsurface *
330weston_surface_to_subsurface(struct weston_surface *surface);
331
Jason Ekstranda7af7042013-10-12 22:38:11 -0500332WL_EXPORT struct weston_view *
333weston_view_create(struct weston_surface *surface)
334{
335 struct weston_view *view;
336
337 view = calloc(1, sizeof *view);
338 if (view == NULL)
339 return NULL;
340
341 view->surface = surface;
342
Jason Ekstranda7af7042013-10-12 22:38:11 -0500343 /* Assign to surface */
344 wl_list_insert(&surface->views, &view->surface_link);
345
346 wl_signal_init(&view->destroy_signal);
347 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300348 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500349
Xiong Zhang97116532013-10-23 13:58:31 +0800350 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300351 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300352 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500353
354 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300355 pixman_region32_init(&view->transform.masked_boundingbox);
356 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500357
358 view->alpha = 1.0;
359 pixman_region32_init(&view->transform.opaque);
360
361 wl_list_init(&view->geometry.transformation_list);
362 wl_list_insert(&view->geometry.transformation_list,
363 &view->transform.position.link);
364 weston_matrix_init(&view->transform.position.matrix);
365 wl_list_init(&view->geometry.child_list);
366 pixman_region32_init(&view->transform.boundingbox);
367 view->transform.dirty = 1;
368
369 view->output = NULL;
370
371 return view;
372}
373
Jason Ekstrand108865d2014-06-26 10:04:49 -0700374struct weston_frame_callback {
375 struct wl_resource *resource;
376 struct wl_list link;
377};
378
Jason Ekstrand7b982072014-05-20 14:33:03 -0500379static void
380surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
381{
382 struct weston_surface_state *state =
383 container_of(listener, struct weston_surface_state,
384 buffer_destroy_listener);
385
386 state->buffer = NULL;
387}
388
389static void
390weston_surface_state_init(struct weston_surface_state *state)
391{
392 state->newly_attached = 0;
393 state->buffer = NULL;
394 state->buffer_destroy_listener.notify =
395 surface_state_handle_buffer_destroy;
396 state->sx = 0;
397 state->sy = 0;
398
399 pixman_region32_init(&state->damage);
400 pixman_region32_init(&state->opaque);
401 region_init_infinite(&state->input);
402
403 wl_list_init(&state->frame_callback_list);
404
405 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
406 state->buffer_viewport.buffer.scale = 1;
407 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
408 state->buffer_viewport.surface.width = -1;
409 state->buffer_viewport.changed = 0;
410}
411
412static void
413weston_surface_state_fini(struct weston_surface_state *state)
414{
415 struct weston_frame_callback *cb, *next;
416
417 wl_list_for_each_safe(cb, next,
418 &state->frame_callback_list, link)
419 wl_resource_destroy(cb->resource);
420
421 pixman_region32_fini(&state->input);
422 pixman_region32_fini(&state->opaque);
423 pixman_region32_fini(&state->damage);
424
425 if (state->buffer)
426 wl_list_remove(&state->buffer_destroy_listener.link);
427 state->buffer = NULL;
428}
429
430static void
431weston_surface_state_set_buffer(struct weston_surface_state *state,
432 struct weston_buffer *buffer)
433{
434 if (state->buffer == buffer)
435 return;
436
437 if (state->buffer)
438 wl_list_remove(&state->buffer_destroy_listener.link);
439 state->buffer = buffer;
440 if (state->buffer)
441 wl_signal_add(&state->buffer->destroy_signal,
442 &state->buffer_destroy_listener);
443}
444
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500445WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500446weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500447{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500448 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400449
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200450 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400451 if (surface == NULL)
452 return NULL;
453
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500454 wl_signal_init(&surface->destroy_signal);
455
456 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500457
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500458 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200459 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400460
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200461 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
462 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200463 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
464 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500465
466 weston_surface_state_init(&surface->pending);
467
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400468 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200469
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200470 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100471
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400472 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500473 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300474 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400475
Jason Ekstranda7af7042013-10-12 22:38:11 -0500476 wl_list_init(&surface->views);
477
478 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500479
Pekka Paalanene67858b2013-04-25 13:57:42 +0300480 wl_list_init(&surface->subsurface_list);
481 wl_list_init(&surface->subsurface_list_pending);
482
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400483 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500484}
485
Alex Wu8811bf92012-02-28 18:07:54 +0800486WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500487weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200488 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500489{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100490 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500491}
492
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400493WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500494weston_view_to_global_float(struct weston_view *view,
495 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200496{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500497 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200498 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
499
Jason Ekstranda7af7042013-10-12 22:38:11 -0500500 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200501
502 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200503 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700504 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200505 v.f[3]);
506 *x = 0;
507 *y = 0;
508 return;
509 }
510
511 *x = v.f[0] / v.f[3];
512 *y = v.f[1] / v.f[3];
513 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500514 *x = sx + view->geometry.x;
515 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200516 }
517}
518
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500519WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200520weston_transformed_coord(int width, int height,
521 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200522 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200523 float sx, float sy, float *bx, float *by)
524{
525 switch (transform) {
526 case WL_OUTPUT_TRANSFORM_NORMAL:
527 default:
528 *bx = sx;
529 *by = sy;
530 break;
531 case WL_OUTPUT_TRANSFORM_FLIPPED:
532 *bx = width - sx;
533 *by = sy;
534 break;
535 case WL_OUTPUT_TRANSFORM_90:
536 *bx = height - sy;
537 *by = sx;
538 break;
539 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
540 *bx = height - sy;
541 *by = width - sx;
542 break;
543 case WL_OUTPUT_TRANSFORM_180:
544 *bx = width - sx;
545 *by = height - sy;
546 break;
547 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
548 *bx = sx;
549 *by = height - sy;
550 break;
551 case WL_OUTPUT_TRANSFORM_270:
552 *bx = sy;
553 *by = width - sx;
554 break;
555 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
556 *bx = sy;
557 *by = sx;
558 break;
559 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200560
561 *bx *= scale;
562 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200563}
564
565WL_EXPORT pixman_box32_t
566weston_transformed_rect(int width, int height,
567 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200568 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200569 pixman_box32_t rect)
570{
571 float x1, x2, y1, y2;
572
573 pixman_box32_t ret;
574
Alexander Larsson4ea95522013-05-22 14:41:37 +0200575 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200576 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200577 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200578 rect.x2, rect.y2, &x2, &y2);
579
580 if (x1 <= x2) {
581 ret.x1 = x1;
582 ret.x2 = x2;
583 } else {
584 ret.x1 = x2;
585 ret.x2 = x1;
586 }
587
588 if (y1 <= y2) {
589 ret.y1 = y1;
590 ret.y2 = y2;
591 } else {
592 ret.y1 = y2;
593 ret.y2 = y1;
594 }
595
596 return ret;
597}
598
599WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500600weston_transformed_region(int width, int height,
601 enum wl_output_transform transform,
602 int32_t scale,
603 pixman_region32_t *src, pixman_region32_t *dest)
604{
605 pixman_box32_t *src_rects, *dest_rects;
606 int nrects, i;
607
608 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
609 if (src != dest)
610 pixman_region32_copy(dest, src);
611 return;
612 }
613
614 src_rects = pixman_region32_rectangles(src, &nrects);
615 dest_rects = malloc(nrects * sizeof(*dest_rects));
616 if (!dest_rects)
617 return;
618
619 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
620 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
621 } else {
622 for (i = 0; i < nrects; i++) {
623 switch (transform) {
624 default:
625 case WL_OUTPUT_TRANSFORM_NORMAL:
626 dest_rects[i].x1 = src_rects[i].x1;
627 dest_rects[i].y1 = src_rects[i].y1;
628 dest_rects[i].x2 = src_rects[i].x2;
629 dest_rects[i].y2 = src_rects[i].y2;
630 break;
631 case WL_OUTPUT_TRANSFORM_90:
632 dest_rects[i].x1 = height - src_rects[i].y2;
633 dest_rects[i].y1 = src_rects[i].x1;
634 dest_rects[i].x2 = height - src_rects[i].y1;
635 dest_rects[i].y2 = src_rects[i].x2;
636 break;
637 case WL_OUTPUT_TRANSFORM_180:
638 dest_rects[i].x1 = width - src_rects[i].x2;
639 dest_rects[i].y1 = height - src_rects[i].y2;
640 dest_rects[i].x2 = width - src_rects[i].x1;
641 dest_rects[i].y2 = height - src_rects[i].y1;
642 break;
643 case WL_OUTPUT_TRANSFORM_270:
644 dest_rects[i].x1 = src_rects[i].y1;
645 dest_rects[i].y1 = width - src_rects[i].x2;
646 dest_rects[i].x2 = src_rects[i].y2;
647 dest_rects[i].y2 = width - src_rects[i].x1;
648 break;
649 case WL_OUTPUT_TRANSFORM_FLIPPED:
650 dest_rects[i].x1 = width - src_rects[i].x2;
651 dest_rects[i].y1 = src_rects[i].y1;
652 dest_rects[i].x2 = width - src_rects[i].x1;
653 dest_rects[i].y2 = src_rects[i].y2;
654 break;
655 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
656 dest_rects[i].x1 = height - src_rects[i].y2;
657 dest_rects[i].y1 = width - src_rects[i].x2;
658 dest_rects[i].x2 = height - src_rects[i].y1;
659 dest_rects[i].y2 = width - src_rects[i].x1;
660 break;
661 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
662 dest_rects[i].x1 = src_rects[i].x1;
663 dest_rects[i].y1 = height - src_rects[i].y2;
664 dest_rects[i].x2 = src_rects[i].x2;
665 dest_rects[i].y2 = height - src_rects[i].y1;
666 break;
667 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
668 dest_rects[i].x1 = src_rects[i].y1;
669 dest_rects[i].y1 = src_rects[i].x1;
670 dest_rects[i].x2 = src_rects[i].y2;
671 dest_rects[i].y2 = src_rects[i].x2;
672 break;
673 }
674 }
675 }
676
677 if (scale != 1) {
678 for (i = 0; i < nrects; i++) {
679 dest_rects[i].x1 *= scale;
680 dest_rects[i].x2 *= scale;
681 dest_rects[i].y1 *= scale;
682 dest_rects[i].y2 *= scale;
683 }
684 }
685
686 pixman_region32_clear(dest);
687 pixman_region32_init_rects(dest, dest_rects, nrects);
688 free(dest_rects);
689}
690
Jonny Lamb74130762013-11-26 18:19:46 +0100691static void
692scaler_surface_to_buffer(struct weston_surface *surface,
693 float sx, float sy, float *bx, float *by)
694{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200695 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200696 double src_width, src_height;
697 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200698
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200699 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
700 if (vp->surface.width == -1) {
701 *bx = sx;
702 *by = sy;
703 return;
704 }
Jonny Lamb74130762013-11-26 18:19:46 +0100705
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200706 src_x = 0.0;
707 src_y = 0.0;
708 src_width = surface->width_from_buffer;
709 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100710 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200711 src_x = wl_fixed_to_double(vp->buffer.src_x);
712 src_y = wl_fixed_to_double(vp->buffer.src_y);
713 src_width = wl_fixed_to_double(vp->buffer.src_width);
714 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100715 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200716
717 *bx = sx * src_width / surface->width + src_x;
718 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100719}
720
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500721WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200722weston_surface_to_buffer_float(struct weston_surface *surface,
723 float sx, float sy, float *bx, float *by)
724{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200725 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
726
Jonny Lamb74130762013-11-26 18:19:46 +0100727 /* first transform coordinates if the scaler is set */
728 scaler_surface_to_buffer(surface, sx, sy, bx, by);
729
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500730 weston_transformed_coord(surface->width_from_buffer,
731 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200732 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100733 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200734}
735
Alexander Larsson4ea95522013-05-22 14:41:37 +0200736WL_EXPORT void
737weston_surface_to_buffer(struct weston_surface *surface,
738 int sx, int sy, int *bx, int *by)
739{
740 float bxf, byf;
741
Jonny Lamb74130762013-11-26 18:19:46 +0100742 weston_surface_to_buffer_float(surface,
743 sx, sy, &bxf, &byf);
744
Alexander Larsson4ea95522013-05-22 14:41:37 +0200745 *bx = floorf(bxf);
746 *by = floorf(byf);
747}
748
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200749WL_EXPORT pixman_box32_t
750weston_surface_to_buffer_rect(struct weston_surface *surface,
751 pixman_box32_t rect)
752{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200753 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100754 float xf, yf;
755
756 /* first transform box coordinates if the scaler is set */
757 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
758 rect.x1 = floorf(xf);
759 rect.y1 = floorf(yf);
760
761 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
762 rect.x2 = floorf(xf);
763 rect.y2 = floorf(yf);
764
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500765 return weston_transformed_rect(surface->width_from_buffer,
766 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200767 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200768 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200769}
770
771WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500772weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400773 struct weston_plane *plane)
774{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500775 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400776 return;
777
Jason Ekstranda7af7042013-10-12 22:38:11 -0500778 weston_view_damage_below(view);
779 view->plane = plane;
780 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400781}
782
783WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500784weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200785{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500786 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200787
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500788 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300789 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500790 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800791 if (view->plane)
792 pixman_region32_union(&view->plane->damage,
793 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500794 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800795 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200796}
797
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400798static void
799weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
800{
801 uint32_t different = es->output_mask ^ mask;
802 uint32_t entered = mask & different;
803 uint32_t left = es->output_mask & different;
804 struct weston_output *output;
805 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500806 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400807
808 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500809 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400810 return;
811 if (different == 0)
812 return;
813
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500814 client = wl_resource_get_client(es->resource);
815
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400816 wl_list_for_each(output, &es->compositor->output_list, link) {
817 if (1 << output->id & different)
818 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500819 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400820 client);
821 if (resource == NULL)
822 continue;
823 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500824 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400825 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500826 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400827 }
828}
829
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200830
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400831static void
832weston_surface_assign_output(struct weston_surface *es)
833{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500834 struct weston_output *new_output;
835 struct weston_view *view;
836 pixman_region32_t region;
837 uint32_t max, area, mask;
838 pixman_box32_t *e;
839
840 new_output = NULL;
841 max = 0;
842 mask = 0;
843 pixman_region32_init(&region);
844 wl_list_for_each(view, &es->views, surface_link) {
845 if (!view->output)
846 continue;
847
848 pixman_region32_intersect(&region, &view->transform.boundingbox,
849 &view->output->region);
850
851 e = pixman_region32_extents(&region);
852 area = (e->x2 - e->x1) * (e->y2 - e->y1);
853
854 mask |= view->output_mask;
855
856 if (area >= max) {
857 new_output = view->output;
858 max = area;
859 }
860 }
861 pixman_region32_fini(&region);
862
863 es->output = new_output;
864 weston_surface_update_output_mask(es, mask);
865}
866
867static void
868weston_view_assign_output(struct weston_view *ev)
869{
870 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400871 struct weston_output *output, *new_output;
872 pixman_region32_t region;
873 uint32_t max, area, mask;
874 pixman_box32_t *e;
875
876 new_output = NULL;
877 max = 0;
878 mask = 0;
879 pixman_region32_init(&region);
880 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500881 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400882 &output->region);
883
884 e = pixman_region32_extents(&region);
885 area = (e->x2 - e->x1) * (e->y2 - e->y1);
886
887 if (area > 0)
888 mask |= 1 << output->id;
889
890 if (area >= max) {
891 new_output = output;
892 max = area;
893 }
894 }
895 pixman_region32_fini(&region);
896
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 ev->output = new_output;
898 ev->output_mask = mask;
899
900 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400901}
902
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200903static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500904view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
905 int32_t width, int32_t height,
906 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200907{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200908 float min_x = HUGE_VALF, min_y = HUGE_VALF;
909 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200910 int32_t s[4][2] = {
911 { sx, sy },
912 { sx, sy + height },
913 { sx + width, sy },
914 { sx + width, sy + height }
915 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200916 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200917 int i;
918
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300919 if (width == 0 || height == 0) {
920 /* avoid rounding empty bbox to 1x1 */
921 pixman_region32_init(bbox);
922 return;
923 }
924
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200925 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200926 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500927 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200928 if (x < min_x)
929 min_x = x;
930 if (x > max_x)
931 max_x = x;
932 if (y < min_y)
933 min_y = y;
934 if (y > max_y)
935 max_y = y;
936 }
937
Pekka Paalanen219b9822012-02-08 15:38:37 +0200938 int_x = floorf(min_x);
939 int_y = floorf(min_y);
940 pixman_region32_init_rect(bbox, int_x, int_y,
941 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200942}
943
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200944static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500945weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200946{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500947 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200948
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200949 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500950 view->geometry.x = roundf(view->geometry.x);
951 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200952
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500953 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500954 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
955 view->transform.position.matrix.d[12] = view->geometry.x;
956 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500957
Jason Ekstranda7af7042013-10-12 22:38:11 -0500958 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500959
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 view->transform.inverse = view->transform.position.matrix;
961 view->transform.inverse.d[12] = -view->geometry.x;
962 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -0500963
Jason Ekstranda7af7042013-10-12 22:38:11 -0500964 pixman_region32_init_rect(&view->transform.boundingbox,
965 view->geometry.x,
966 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -0600967 view->surface->width,
968 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500969
Jason Ekstranda7af7042013-10-12 22:38:11 -0500970 if (view->alpha == 1.0) {
971 pixman_region32_copy(&view->transform.opaque,
972 &view->surface->opaque);
973 pixman_region32_translate(&view->transform.opaque,
974 view->geometry.x,
975 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500976 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200977}
978
979static int
Jason Ekstranda7af7042013-10-12 22:38:11 -0500980weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200981{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500982 struct weston_view *parent = view->geometry.parent;
983 struct weston_matrix *matrix = &view->transform.matrix;
984 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200985 struct weston_transform *tform;
986
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200988
989 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
991 view->transform.position.matrix.d[12] = view->geometry.x;
992 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200993
994 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500995 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200996 weston_matrix_multiply(matrix, &tform->matrix);
997
Pekka Paalanen483243f2013-03-08 14:56:50 +0200998 if (parent)
999 weston_matrix_multiply(matrix, &parent->transform.matrix);
1000
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001001 if (weston_matrix_invert(inverse, matrix) < 0) {
1002 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001003 weston_log("error: weston_view %p"
1004 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001005 return -1;
1006 }
1007
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001008 view_compute_bbox(view, 0, 0,
1009 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001011
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001012 return 0;
1013}
1014
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001015static struct weston_layer *
1016get_view_layer(struct weston_view *view)
1017{
1018 if (view->parent_view)
1019 return get_view_layer(view->parent_view);
1020 return view->layer_link.layer;
1021}
1022
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001023WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001024weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001025{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001027 struct weston_layer *layer;
1028 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001029
Jason Ekstranda7af7042013-10-12 22:38:11 -05001030 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001031 return;
1032
Pekka Paalanen483243f2013-03-08 14:56:50 +02001033 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001035
Jason Ekstranda7af7042013-10-12 22:38:11 -05001036 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001037
Jason Ekstranda7af7042013-10-12 22:38:11 -05001038 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001039
Jason Ekstranda7af7042013-10-12 22:38:11 -05001040 pixman_region32_fini(&view->transform.boundingbox);
1041 pixman_region32_fini(&view->transform.opaque);
1042 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001043
Pekka Paalanencd403622012-01-25 13:37:39 +02001044 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 if (view->geometry.transformation_list.next ==
1046 &view->transform.position.link &&
1047 view->geometry.transformation_list.prev ==
1048 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001049 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001051 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 if (weston_view_update_transform_enable(view) < 0)
1053 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001054 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001055
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001056 layer = get_view_layer(view);
1057 if (layer) {
1058 pixman_region32_init_with_extents(&mask, &layer->mask);
1059 pixman_region32_intersect(&view->transform.masked_boundingbox,
1060 &view->transform.boundingbox, &mask);
1061 pixman_region32_intersect(&view->transform.masked_opaque,
1062 &view->transform.opaque, &mask);
1063 pixman_region32_fini(&mask);
1064 }
1065
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001067
Jason Ekstranda7af7042013-10-12 22:38:11 -05001068 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001069
Jason Ekstranda7af7042013-10-12 22:38:11 -05001070 wl_signal_emit(&view->surface->compositor->transform_signal,
1071 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001072}
1073
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001074WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001075weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001076{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001077 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001078
1079 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 * The invariant: if view->geometry.dirty, then all views
1081 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001082 * Corollary: if not parent->geometry.dirty, then all ancestors
1083 * are not dirty.
1084 */
1085
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001087 return;
1088
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001090
Jason Ekstranda7af7042013-10-12 22:38:11 -05001091 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001092 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001094}
1095
1096WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097weston_view_to_global_fixed(struct weston_view *view,
1098 wl_fixed_t vx, wl_fixed_t vy,
1099 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001100{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001101 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001102
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 weston_view_to_global_float(view,
1104 wl_fixed_to_double(vx),
1105 wl_fixed_to_double(vy),
1106 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001107 *x = wl_fixed_from_double(xf);
1108 *y = wl_fixed_from_double(yf);
1109}
1110
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001111WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112weston_view_from_global_float(struct weston_view *view,
1113 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001114{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001116 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1117
Jason Ekstranda7af7042013-10-12 22:38:11 -05001118 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001119
1120 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001121 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001122 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001123 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001124 *vx = 0;
1125 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001126 return;
1127 }
1128
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 *vx = v.f[0] / v.f[3];
1130 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001131 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 *vx = x - view->geometry.x;
1133 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001134 }
1135}
1136
1137WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138weston_view_from_global_fixed(struct weston_view *view,
1139 wl_fixed_t x, wl_fixed_t y,
1140 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001141{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001143
Jason Ekstranda7af7042013-10-12 22:38:11 -05001144 weston_view_from_global_float(view,
1145 wl_fixed_to_double(x),
1146 wl_fixed_to_double(y),
1147 &vxf, &vyf);
1148 *vx = wl_fixed_from_double(vxf);
1149 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001150}
1151
1152WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153weston_view_from_global(struct weston_view *view,
1154 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001155{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001157
Jason Ekstranda7af7042013-10-12 22:38:11 -05001158 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1159 *vx = floorf(vxf);
1160 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001161}
1162
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001163WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001164weston_surface_schedule_repaint(struct weston_surface *surface)
1165{
1166 struct weston_output *output;
1167
1168 wl_list_for_each(output, &surface->compositor->output_list, link)
1169 if (surface->output_mask & (1 << output->id))
1170 weston_output_schedule_repaint(output);
1171}
1172
1173WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174weston_view_schedule_repaint(struct weston_view *view)
1175{
1176 struct weston_output *output;
1177
1178 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1179 if (view->output_mask & (1 << output->id))
1180 weston_output_schedule_repaint(output);
1181}
1182
1183WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001184weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001185{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001186 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 0, 0, surface->width,
1188 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001189
Kristian Høgsberg98238702012-08-03 16:29:12 -04001190 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001191}
1192
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001193WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001195{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001196 if (view->geometry.x == x && view->geometry.y == y)
1197 return;
1198
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 view->geometry.x = x;
1200 view->geometry.y = y;
1201 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001202}
1203
Pekka Paalanen483243f2013-03-08 14:56:50 +02001204static void
1205transform_parent_handle_parent_destroy(struct wl_listener *listener,
1206 void *data)
1207{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001208 struct weston_view *view =
1209 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001210 geometry.parent_destroy_listener);
1211
Jason Ekstranda7af7042013-10-12 22:38:11 -05001212 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001213}
1214
1215WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216weston_view_set_transform_parent(struct weston_view *view,
1217 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001218{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 if (view->geometry.parent) {
1220 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1221 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001222 }
1223
Jason Ekstranda7af7042013-10-12 22:38:11 -05001224 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001225
Jason Ekstranda7af7042013-10-12 22:38:11 -05001226 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001227 transform_parent_handle_parent_destroy;
1228 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001229 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001231 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001232 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001233 }
1234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 weston_view_geometry_dirty(view);
1236}
1237
1238WL_EXPORT int
1239weston_view_is_mapped(struct weston_view *view)
1240{
1241 if (view->output)
1242 return 1;
1243 else
1244 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001245}
1246
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001247WL_EXPORT int
1248weston_surface_is_mapped(struct weston_surface *surface)
1249{
1250 if (surface->output)
1251 return 1;
1252 else
1253 return 0;
1254}
1255
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001256static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001257surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001258{
1259 struct weston_view *view;
1260
1261 if (surface->width == width && surface->height == height)
1262 return;
1263
1264 surface->width = width;
1265 surface->height = height;
1266
1267 wl_list_for_each(view, &surface->views, surface_link)
1268 weston_view_geometry_dirty(view);
1269}
1270
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001271WL_EXPORT void
1272weston_surface_set_size(struct weston_surface *surface,
1273 int32_t width, int32_t height)
1274{
1275 assert(!surface->resource);
1276 surface_set_size(surface, width, height);
1277}
1278
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001279static int
1280fixed_round_up_to_int(wl_fixed_t f)
1281{
1282 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1283}
1284
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001285static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001286weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001287{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001288 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001289 int32_t width, height;
1290
1291 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001292 surface->width_from_buffer = 0;
1293 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001294 return;
1295 }
1296
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001297 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001298 case WL_OUTPUT_TRANSFORM_90:
1299 case WL_OUTPUT_TRANSFORM_270:
1300 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1301 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001302 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1303 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001304 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001305 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001306 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1307 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001308 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001309 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001310
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001311 surface->width_from_buffer = width;
1312 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001313}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001314
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001315static void
1316weston_surface_update_size(struct weston_surface *surface)
1317{
1318 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1319 int32_t width, height;
1320
1321 width = surface->width_from_buffer;
1322 height = surface->height_from_buffer;
1323
1324 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001325 surface_set_size(surface,
1326 vp->surface.width, vp->surface.height);
1327 return;
1328 }
1329
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001330 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001331 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1332 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1333
1334 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001335 return;
1336 }
1337
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001338 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001339}
1340
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001341WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001342weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001343{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001344 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001345
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001346 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001347
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001348 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001349}
1350
Jason Ekstranda7af7042013-10-12 22:38:11 -05001351WL_EXPORT struct weston_view *
1352weston_compositor_pick_view(struct weston_compositor *compositor,
1353 wl_fixed_t x, wl_fixed_t y,
1354 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001355{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001356 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001357 int ix = wl_fixed_to_int(x);
1358 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001359
Jason Ekstranda7af7042013-10-12 22:38:11 -05001360 wl_list_for_each(view, &compositor->view_list, link) {
1361 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001362 if (pixman_region32_contains_point(
1363 &view->transform.masked_boundingbox,
1364 ix, iy, NULL) &&
1365 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001366 wl_fixed_to_int(*vx),
1367 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001368 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001370 }
1371
1372 return NULL;
1373}
1374
1375static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001376weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001377{
Daniel Stone37816df2012-05-16 18:45:18 +01001378 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001379
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001380 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001381 return;
1382
Daniel Stone37816df2012-05-16 18:45:18 +01001383 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001384 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001385}
1386
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001387WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001389{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001390 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001391
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392 if (!weston_view_is_mapped(view))
1393 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001394
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 weston_view_damage_below(view);
1396 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001397 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001398 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001399 wl_list_remove(&view->link);
1400 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001401 view->output_mask = 0;
1402 weston_surface_assign_output(view->surface);
1403
1404 if (weston_surface_is_mapped(view->surface))
1405 return;
1406
1407 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1408 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001409 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001410 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001411 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001412 NULL,
1413 wl_fixed_from_int(0),
1414 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001415 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001416 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001417 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001419
Jason Ekstranda7af7042013-10-12 22:38:11 -05001420WL_EXPORT void
1421weston_surface_unmap(struct weston_surface *surface)
1422{
1423 struct weston_view *view;
1424
1425 wl_list_for_each(view, &surface->views, surface_link)
1426 weston_view_unmap(view);
1427 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001428}
1429
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001430static void
1431weston_surface_reset_pending_buffer(struct weston_surface *surface)
1432{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001433 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001434 surface->pending.sx = 0;
1435 surface->pending.sy = 0;
1436 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001437 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001438}
1439
Jason Ekstranda7af7042013-10-12 22:38:11 -05001440WL_EXPORT void
1441weston_view_destroy(struct weston_view *view)
1442{
1443 wl_signal_emit(&view->destroy_signal, view);
1444
1445 assert(wl_list_empty(&view->geometry.child_list));
1446
1447 if (weston_view_is_mapped(view)) {
1448 weston_view_unmap(view);
1449 weston_compositor_build_view_list(view->surface->compositor);
1450 }
1451
1452 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001453 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454
1455 pixman_region32_fini(&view->clip);
1456 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001457 pixman_region32_fini(&view->transform.masked_boundingbox);
1458 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001459
1460 weston_view_set_transform_parent(view, NULL);
1461
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 wl_list_remove(&view->surface_link);
1463
1464 free(view);
1465}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001466
1467WL_EXPORT void
1468weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001469{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001470 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001471 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001472
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001473 if (--surface->ref_count > 0)
1474 return;
1475
1476 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1477
Pekka Paalanene67858b2013-04-25 13:57:42 +03001478 assert(wl_list_empty(&surface->subsurface_list_pending));
1479 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001480
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1482 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001483
Jason Ekstrand7b982072014-05-20 14:33:03 -05001484 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001485
Pekka Paalanende685b82012-12-04 15:58:12 +02001486 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001487
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001488 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001489 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001490 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001491
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001492 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001493 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001494
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001495 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001496}
1497
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001498static void
1499destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001500{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001501 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001502
Giulio Camuffo0d379742013-11-15 22:06:15 +01001503 /* Set the resource to NULL, since we don't want to leave a
1504 * dangling pointer if the surface was refcounted and survives
1505 * the weston_surface_destroy() call. */
1506 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001507 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001508}
1509
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001510static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001511weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1512{
1513 struct weston_buffer *buffer =
1514 container_of(listener, struct weston_buffer, destroy_listener);
1515
1516 wl_signal_emit(&buffer->destroy_signal, buffer);
1517 free(buffer);
1518}
1519
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001520WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001521weston_buffer_from_resource(struct wl_resource *resource)
1522{
1523 struct weston_buffer *buffer;
1524 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001525
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001526 listener = wl_resource_get_destroy_listener(resource,
1527 weston_buffer_destroy_handler);
1528
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001529 if (listener)
1530 return container_of(listener, struct weston_buffer,
1531 destroy_listener);
1532
1533 buffer = zalloc(sizeof *buffer);
1534 if (buffer == NULL)
1535 return NULL;
1536
1537 buffer->resource = resource;
1538 wl_signal_init(&buffer->destroy_signal);
1539 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001540 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001541 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001542
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001543 return buffer;
1544}
1545
1546static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001547weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1548 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001549{
Pekka Paalanende685b82012-12-04 15:58:12 +02001550 struct weston_buffer_reference *ref =
1551 container_of(listener, struct weston_buffer_reference,
1552 destroy_listener);
1553
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001554 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001555 ref->buffer = NULL;
1556}
1557
1558WL_EXPORT void
1559weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001560 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001561{
1562 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001563 ref->buffer->busy_count--;
1564 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001565 assert(wl_resource_get_client(ref->buffer->resource));
1566 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001567 WL_BUFFER_RELEASE);
1568 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001569 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001570 }
1571
Pekka Paalanende685b82012-12-04 15:58:12 +02001572 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001573 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001574 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001575 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001576 }
1577
Pekka Paalanende685b82012-12-04 15:58:12 +02001578 ref->buffer = buffer;
1579 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1580}
1581
1582static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001583weston_surface_attach(struct weston_surface *surface,
1584 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001585{
1586 weston_buffer_reference(&surface->buffer_ref, buffer);
1587
Pekka Paalanena6421c42012-12-04 15:58:10 +02001588 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001589 if (weston_surface_is_mapped(surface))
1590 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001591 }
1592
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001593 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001594
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001595 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001596}
1597
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001598WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001599weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001600{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001601 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001602
1603 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001604 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001605}
1606
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001607WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001608weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001609{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001610 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001611
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001612 pixman_region32_union(&compositor->primary_plane.damage,
1613 &compositor->primary_plane.damage,
1614 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001615 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001616}
1617
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001618static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001620{
Pekka Paalanende685b82012-12-04 15:58:12 +02001621 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001622 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001623 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001624
Jason Ekstrandef540082014-06-26 10:37:36 -07001625 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001626}
1627
1628static void
1629view_accumulate_damage(struct weston_view *view,
1630 pixman_region32_t *opaque)
1631{
1632 pixman_region32_t damage;
1633
1634 pixman_region32_init(&damage);
1635 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001636 pixman_box32_t *extents;
1637
Jason Ekstranda7af7042013-10-12 22:38:11 -05001638 extents = pixman_region32_extents(&view->surface->damage);
1639 view_compute_bbox(view, extents->x1, extents->y1,
1640 extents->x2 - extents->x1,
1641 extents->y2 - extents->y1,
1642 &damage);
1643 pixman_region32_translate(&damage,
1644 -view->plane->x,
1645 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001646 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001647 pixman_region32_copy(&damage, &view->surface->damage);
1648 pixman_region32_translate(&damage,
1649 view->geometry.x - view->plane->x,
1650 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001651 }
1652
Jason Ekstranda7af7042013-10-12 22:38:11 -05001653 pixman_region32_subtract(&damage, &damage, opaque);
1654 pixman_region32_union(&view->plane->damage,
1655 &view->plane->damage, &damage);
1656 pixman_region32_fini(&damage);
1657 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001658 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001659}
1660
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001661static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001662compositor_accumulate_damage(struct weston_compositor *ec)
1663{
1664 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001665 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001666 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001667
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001668 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001669
1670 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001671 pixman_region32_copy(&plane->clip, &clip);
1672
1673 pixman_region32_init(&opaque);
1674
Jason Ekstranda7af7042013-10-12 22:38:11 -05001675 wl_list_for_each(ev, &ec->view_list, link) {
1676 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001677 continue;
1678
Jason Ekstranda7af7042013-10-12 22:38:11 -05001679 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001680 }
1681
1682 pixman_region32_union(&clip, &clip, &opaque);
1683 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001684 }
1685
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001686 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001687
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688 wl_list_for_each(ev, &ec->view_list, link)
1689 ev->surface->touched = 0;
1690
1691 wl_list_for_each(ev, &ec->view_list, link) {
1692 if (ev->surface->touched)
1693 continue;
1694 ev->surface->touched = 1;
1695
1696 surface_flush_damage(ev->surface);
1697
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001698 /* Both the renderer and the backend have seen the buffer
1699 * by now. If renderer needs the buffer, it has its own
1700 * reference set. If the backend wants to keep the buffer
1701 * around for migrating the surface into a non-primary plane
1702 * later, keep_buffer is true. Otherwise, drop the core
1703 * reference now, and allow early buffer release. This enables
1704 * clients to use single-buffering.
1705 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001706 if (!ev->surface->keep_buffer)
1707 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001708 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001709}
1710
1711static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001712surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001713{
1714 struct weston_subsurface *sub;
1715
Pekka Paalanene67858b2013-04-25 13:57:42 +03001716 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001717 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001718 continue;
1719
Jason Ekstranda7af7042013-10-12 22:38:11 -05001720 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1721 wl_list_init(&sub->surface->views);
1722
1723 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001724 }
1725}
1726
1727static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001729{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001730 struct weston_subsurface *sub;
1731 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001732
Jason Ekstranda7af7042013-10-12 22:38:11 -05001733 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1734 if (sub->surface == surface)
1735 continue;
1736
George Kiagiadakised04d382014-06-13 18:10:26 +02001737 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1738 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001739 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001740 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741
1742 surface_free_unused_subsurface_views(sub->surface);
1743 }
1744}
1745
1746static void
1747view_list_add_subsurface_view(struct weston_compositor *compositor,
1748 struct weston_subsurface *sub,
1749 struct weston_view *parent)
1750{
1751 struct weston_subsurface *child;
1752 struct weston_view *view = NULL, *iv;
1753
1754 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1755 if (iv->geometry.parent == parent) {
1756 view = iv;
1757 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001758 }
1759 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001760
1761 if (view) {
1762 /* Put it back in the surface's list of views */
1763 wl_list_remove(&view->surface_link);
1764 wl_list_insert(&sub->surface->views, &view->surface_link);
1765 } else {
1766 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001767 weston_view_set_position(view,
1768 sub->position.x,
1769 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001770 weston_view_set_transform_parent(view, parent);
1771 }
1772
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001773 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775
Pekka Paalanenb188e912013-11-19 14:03:35 +02001776 if (wl_list_empty(&sub->surface->subsurface_list)) {
1777 wl_list_insert(compositor->view_list.prev, &view->link);
1778 return;
1779 }
1780
1781 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1782 if (child->surface == sub->surface)
1783 wl_list_insert(compositor->view_list.prev, &view->link);
1784 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001785 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001786 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787}
1788
1789static void
1790view_list_add(struct weston_compositor *compositor,
1791 struct weston_view *view)
1792{
1793 struct weston_subsurface *sub;
1794
1795 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796
Pekka Paalanenb188e912013-11-19 14:03:35 +02001797 if (wl_list_empty(&view->surface->subsurface_list)) {
1798 wl_list_insert(compositor->view_list.prev, &view->link);
1799 return;
1800 }
1801
1802 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1803 if (sub->surface == view->surface)
1804 wl_list_insert(compositor->view_list.prev, &view->link);
1805 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001807 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001808}
1809
1810static void
1811weston_compositor_build_view_list(struct weston_compositor *compositor)
1812{
1813 struct weston_view *view;
1814 struct weston_layer *layer;
1815
1816 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001817 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001818 surface_stash_subsurface_views(view->surface);
1819
1820 wl_list_init(&compositor->view_list);
1821 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001822 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001823 view_list_add(compositor, view);
1824 }
1825 }
1826
1827 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001828 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001829 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001830}
1831
David Herrmann1edf44c2013-10-22 17:11:26 +02001832static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001833weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001834{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001835 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001837 struct weston_animation *animation, *next;
1838 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001839 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001840 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001841 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001842
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001843 if (output->destroying)
1844 return 0;
1845
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001846 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001848
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001849 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001850 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001851 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852 wl_list_for_each(ev, &ec->view_list, link)
1853 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001854
Pekka Paalanene67858b2013-04-25 13:57:42 +03001855 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 wl_list_for_each(ev, &ec->view_list, link) {
1857 /* Note: This operation is safe to do multiple times on the
1858 * same surface.
1859 */
1860 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001861 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 &ev->surface->frame_callback_list);
1863 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001864 }
1865 }
1866
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001867 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001868
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001869 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001870 pixman_region32_intersect(&output_damage,
1871 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001872 pixman_region32_subtract(&output_damage,
1873 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001874
Scott Moreauccbf29d2012-02-22 14:21:41 -07001875 if (output->dirty)
1876 weston_output_update_matrix(output);
1877
David Herrmann1edf44c2013-10-22 17:11:26 +02001878 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001879
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001880 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001881
Kristian Høgsbergef044142011-06-21 15:02:12 -04001882 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001883
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001884 weston_compositor_repick(ec);
1885 wl_event_loop_dispatch(ec->input_loop, 0);
1886
Jonas Ådahldb773762012-06-13 00:01:21 +02001887 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001888 wl_callback_send_done(cb->resource, msecs);
1889 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001890 }
1891
Scott Moreaud64cf212012-06-08 19:40:54 -06001892 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001893 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001894 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001895 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001896
1897 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001898}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001899
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001900static int
1901weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001902{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001903 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001904
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001905 wl_event_loop_dispatch(compositor->input_loop, 0);
1906
1907 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001908}
1909
1910WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001911weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001912{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001913 struct weston_compositor *compositor = output->compositor;
1914 struct wl_event_loop *loop =
1915 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001916 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001917
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001918 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001919
1920 if (output->repaint_needed &&
1921 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1922 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001923 r = weston_output_repaint(output, msecs);
1924 if (!r)
1925 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001926 }
1927
1928 output->repaint_scheduled = 0;
1929 if (compositor->input_loop_source)
1930 return;
1931
1932 fd = wl_event_loop_get_fd(compositor->input_loop);
1933 compositor->input_loop_source =
1934 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1935 weston_compositor_read_input, compositor);
1936}
1937
1938static void
1939idle_repaint(void *data)
1940{
1941 struct weston_output *output = data;
1942
Jonas Ådahle5a12252013-04-05 23:07:11 +02001943 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001944}
1945
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001946WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001947weston_layer_entry_insert(struct weston_layer_entry *list,
1948 struct weston_layer_entry *entry)
1949{
1950 wl_list_insert(&list->link, &entry->link);
1951 entry->layer = list->layer;
1952}
1953
1954WL_EXPORT void
1955weston_layer_entry_remove(struct weston_layer_entry *entry)
1956{
1957 wl_list_remove(&entry->link);
1958 wl_list_init(&entry->link);
1959 entry->layer = NULL;
1960}
1961
1962WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001963weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1964{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001965 wl_list_init(&layer->view_list.link);
1966 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001967 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001968 if (below != NULL)
1969 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001970}
1971
1972WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001973weston_layer_set_mask(struct weston_layer *layer,
1974 int x, int y, int width, int height)
1975{
1976 struct weston_view *view;
1977
1978 layer->mask.x1 = x;
1979 layer->mask.x2 = x + width;
1980 layer->mask.y1 = y;
1981 layer->mask.y2 = y + height;
1982
1983 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
1984 weston_view_geometry_dirty(view);
1985 }
1986}
1987
1988WL_EXPORT void
1989weston_layer_set_mask_infinite(struct weston_layer *layer)
1990{
1991 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
1992 UINT32_MAX, UINT32_MAX);
1993}
1994
1995WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001996weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001997{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001998 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001999 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002000
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002001 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2002 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002003 return;
2004
Kristian Høgsbergef044142011-06-21 15:02:12 -04002005 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002006 output->repaint_needed = 1;
2007 if (output->repaint_scheduled)
2008 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002009
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002010 wl_event_loop_add_idle(loop, idle_repaint, output);
2011 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002012
2013 if (compositor->input_loop_source) {
2014 wl_event_source_remove(compositor->input_loop_source);
2015 compositor->input_loop_source = NULL;
2016 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002017}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002018
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002019WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002020weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2021{
2022 struct weston_output *output;
2023
2024 wl_list_for_each(output, &compositor->output_list, link)
2025 weston_output_schedule_repaint(output);
2026}
2027
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002028static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002029surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002030{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002031 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002032}
2033
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002034static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002035surface_attach(struct wl_client *client,
2036 struct wl_resource *resource,
2037 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2038{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002039 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002040 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002041
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002042 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002043 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002044 if (buffer == NULL) {
2045 wl_client_post_no_memory(client);
2046 return;
2047 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002048 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002049
Pekka Paalanende685b82012-12-04 15:58:12 +02002050 /* Attach, attach, without commit in between does not send
2051 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002052 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002053
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002054 surface->pending.sx = sx;
2055 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002056 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002057}
2058
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002059static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002060surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002061 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002062 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002063{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002064 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002065
Pekka Paalanen8e159182012-10-10 12:49:25 +03002066 pixman_region32_union_rect(&surface->pending.damage,
2067 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002068 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002069}
2070
Kristian Høgsberg33418202011-08-16 23:01:28 -04002071static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002072destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002073{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002074 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002075
2076 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002077 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002078}
2079
2080static void
2081surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002082 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002083{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002084 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002085 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002086
2087 cb = malloc(sizeof *cb);
2088 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002089 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002090 return;
2091 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002092
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002093 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2094 callback);
2095 if (cb->resource == NULL) {
2096 free(cb);
2097 wl_resource_post_no_memory(resource);
2098 return;
2099 }
2100
Jason Ekstranda85118c2013-06-27 20:17:02 -05002101 wl_resource_set_implementation(cb->resource, NULL, cb,
2102 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002103
Pekka Paalanenbc106382012-10-10 12:49:31 +03002104 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002105}
2106
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002107static void
2108surface_set_opaque_region(struct wl_client *client,
2109 struct wl_resource *resource,
2110 struct wl_resource *region_resource)
2111{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002112 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002113 struct weston_region *region;
2114
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002115 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002116 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002117 pixman_region32_copy(&surface->pending.opaque,
2118 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002119 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002120 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002121 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002122}
2123
2124static void
2125surface_set_input_region(struct wl_client *client,
2126 struct wl_resource *resource,
2127 struct wl_resource *region_resource)
2128{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002129 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002130 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002131
2132 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002133 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002134 pixman_region32_copy(&surface->pending.input,
2135 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002136 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002137 pixman_region32_fini(&surface->pending.input);
2138 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002139 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002140}
2141
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002142static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002143weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002144{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002145 struct weston_subsurface *sub;
2146
2147 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2148 parent_link_pending) {
2149 wl_list_remove(&sub->parent_link);
2150 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2151 }
2152}
2153
2154static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002155weston_surface_commit_state(struct weston_surface *surface,
2156 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002157{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002158 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002159 pixman_region32_t opaque;
2160
Alexander Larsson4ea95522013-05-22 14:41:37 +02002161 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002162 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002163 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002164 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002165
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002166 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002167 if (state->newly_attached)
2168 weston_surface_attach(surface, state->buffer);
2169 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002170
Jason Ekstrand7b982072014-05-20 14:33:03 -05002171 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002172 weston_surface_update_size(surface);
2173 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002174 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002175 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002176
Jason Ekstrand7b982072014-05-20 14:33:03 -05002177 state->sx = 0;
2178 state->sy = 0;
2179 state->newly_attached = 0;
2180 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002181
2182 /* wl_surface.damage */
2183 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002184 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002185 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002186 0, 0, surface->width, surface->height);
2187 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002188
2189 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002190 pixman_region32_init(&opaque);
2191 pixman_region32_intersect_rect(&opaque, &state->opaque,
2192 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002193
2194 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2195 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002196 wl_list_for_each(view, &surface->views, surface_link)
2197 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002198 }
2199
2200 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002201
2202 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002203 pixman_region32_intersect_rect(&surface->input, &state->input,
2204 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002205
Pekka Paalanenbc106382012-10-10 12:49:31 +03002206 /* wl_surface.frame */
2207 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002208 &state->frame_callback_list);
2209 wl_list_init(&state->frame_callback_list);
2210}
2211
2212static void
2213weston_surface_commit(struct weston_surface *surface)
2214{
2215 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002216
Pekka Paalanene67858b2013-04-25 13:57:42 +03002217 weston_surface_commit_subsurface_order(surface);
2218
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002219 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002220}
2221
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002222static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002223weston_subsurface_commit(struct weston_subsurface *sub);
2224
2225static void
2226weston_subsurface_parent_commit(struct weston_subsurface *sub,
2227 int parent_is_synchronized);
2228
2229static void
2230surface_commit(struct wl_client *client, struct wl_resource *resource)
2231{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002232 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002233 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2234
2235 if (sub) {
2236 weston_subsurface_commit(sub);
2237 return;
2238 }
2239
2240 weston_surface_commit(surface);
2241
2242 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2243 if (sub->surface != surface)
2244 weston_subsurface_parent_commit(sub, 0);
2245 }
2246}
2247
2248static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002249surface_set_buffer_transform(struct wl_client *client,
2250 struct wl_resource *resource, int transform)
2251{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002252 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002253
Jonny Lamba55f1392014-05-30 12:07:15 +02002254 /* if wl_output.transform grows more members this will need to be updated. */
2255 if (transform < 0 ||
2256 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2257 wl_resource_post_error(resource,
2258 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2259 "buffer transform must be a valid transform "
2260 "('%d' specified)", transform);
2261 return;
2262 }
2263
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002264 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002265 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002266}
2267
Alexander Larsson4ea95522013-05-22 14:41:37 +02002268static void
2269surface_set_buffer_scale(struct wl_client *client,
2270 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002271 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002272{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002273 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002274
Jonny Lamba55f1392014-05-30 12:07:15 +02002275 if (scale < 1) {
2276 wl_resource_post_error(resource,
2277 WL_SURFACE_ERROR_INVALID_SCALE,
2278 "buffer scale must be at least one "
2279 "('%d' specified)", scale);
2280 return;
2281 }
2282
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002283 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002284 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002285}
2286
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002287static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002288 surface_destroy,
2289 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002290 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002291 surface_frame,
2292 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002293 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002294 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002295 surface_set_buffer_transform,
2296 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002297};
2298
2299static void
2300compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002301 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002302{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002303 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002304 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002305
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002306 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002307 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002308 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002309 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002310 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002311
Jason Ekstranda85118c2013-06-27 20:17:02 -05002312 surface->resource =
2313 wl_resource_create(client, &wl_surface_interface,
2314 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002315 if (surface->resource == NULL) {
2316 weston_surface_destroy(surface);
2317 wl_resource_post_no_memory(resource);
2318 return;
2319 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002320 wl_resource_set_implementation(surface->resource, &surface_interface,
2321 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002322
2323 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002324}
2325
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002326static void
2327destroy_region(struct wl_resource *resource)
2328{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002329 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002330
2331 pixman_region32_fini(&region->region);
2332 free(region);
2333}
2334
2335static void
2336region_destroy(struct wl_client *client, struct wl_resource *resource)
2337{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002338 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002339}
2340
2341static void
2342region_add(struct wl_client *client, struct wl_resource *resource,
2343 int32_t x, int32_t y, int32_t width, int32_t height)
2344{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002345 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002346
2347 pixman_region32_union_rect(&region->region, &region->region,
2348 x, y, width, height);
2349}
2350
2351static void
2352region_subtract(struct wl_client *client, struct wl_resource *resource,
2353 int32_t x, int32_t y, int32_t width, int32_t height)
2354{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002355 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002356 pixman_region32_t rect;
2357
2358 pixman_region32_init_rect(&rect, x, y, width, height);
2359 pixman_region32_subtract(&region->region, &region->region, &rect);
2360 pixman_region32_fini(&rect);
2361}
2362
2363static const struct wl_region_interface region_interface = {
2364 region_destroy,
2365 region_add,
2366 region_subtract
2367};
2368
2369static void
2370compositor_create_region(struct wl_client *client,
2371 struct wl_resource *resource, uint32_t id)
2372{
2373 struct weston_region *region;
2374
2375 region = malloc(sizeof *region);
2376 if (region == NULL) {
2377 wl_resource_post_no_memory(resource);
2378 return;
2379 }
2380
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002381 pixman_region32_init(&region->region);
2382
Jason Ekstranda85118c2013-06-27 20:17:02 -05002383 region->resource =
2384 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002385 if (region->resource == NULL) {
2386 free(region);
2387 wl_resource_post_no_memory(resource);
2388 return;
2389 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002390 wl_resource_set_implementation(region->resource, &region_interface,
2391 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002392}
2393
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002394static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002395 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002396 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002397};
2398
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002399static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002400weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2401{
2402 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002403
Jason Ekstrand7b982072014-05-20 14:33:03 -05002404 weston_surface_commit_state(surface, &sub->cached);
2405 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002406
2407 weston_surface_commit_subsurface_order(surface);
2408
2409 weston_surface_schedule_repaint(surface);
2410
Jason Ekstrand7b982072014-05-20 14:33:03 -05002411 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002412}
2413
2414static void
2415weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2416{
2417 struct weston_surface *surface = sub->surface;
2418
2419 /*
2420 * If this commit would cause the surface to move by the
2421 * attach(dx, dy) parameters, the old damage region must be
2422 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002423 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002424 */
2425 pixman_region32_translate(&sub->cached.damage,
2426 -surface->pending.sx, -surface->pending.sy);
2427 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2428 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002429 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002430
2431 if (surface->pending.newly_attached) {
2432 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002433 weston_surface_state_set_buffer(&sub->cached,
2434 surface->pending.buffer);
2435 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002436 surface->pending.buffer);
2437 }
2438 sub->cached.sx += surface->pending.sx;
2439 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002440
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002441 sub->cached.buffer_viewport.changed |=
2442 surface->pending.buffer_viewport.changed;
2443 sub->cached.buffer_viewport.buffer =
2444 surface->pending.buffer_viewport.buffer;
2445 sub->cached.buffer_viewport.surface =
2446 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002447
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002448 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002449
2450 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2451
2452 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2453
2454 wl_list_insert_list(&sub->cached.frame_callback_list,
2455 &surface->pending.frame_callback_list);
2456 wl_list_init(&surface->pending.frame_callback_list);
2457
Jason Ekstrand7b982072014-05-20 14:33:03 -05002458 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002459}
2460
2461static int
2462weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2463{
2464 while (sub) {
2465 if (sub->synchronized)
2466 return 1;
2467
2468 if (!sub->parent)
2469 return 0;
2470
2471 sub = weston_surface_to_subsurface(sub->parent);
2472 }
2473
2474 return 0;
2475}
2476
2477static void
2478weston_subsurface_commit(struct weston_subsurface *sub)
2479{
2480 struct weston_surface *surface = sub->surface;
2481 struct weston_subsurface *tmp;
2482
2483 /* Recursive check for effectively synchronized. */
2484 if (weston_subsurface_is_synchronized(sub)) {
2485 weston_subsurface_commit_to_cache(sub);
2486 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002487 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002488 /* flush accumulated state from cache */
2489 weston_subsurface_commit_to_cache(sub);
2490 weston_subsurface_commit_from_cache(sub);
2491 } else {
2492 weston_surface_commit(surface);
2493 }
2494
2495 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2496 if (tmp->surface != surface)
2497 weston_subsurface_parent_commit(tmp, 0);
2498 }
2499 }
2500}
2501
2502static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002503weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002504{
2505 struct weston_surface *surface = sub->surface;
2506 struct weston_subsurface *tmp;
2507
Pekka Paalanene67858b2013-04-25 13:57:42 +03002508 /* From now on, commit_from_cache the whole sub-tree, regardless of
2509 * the synchronized mode of each child. This sub-surface or some
2510 * of its ancestors were synchronized, so we are synchronized
2511 * all the way down.
2512 */
2513
Jason Ekstrand7b982072014-05-20 14:33:03 -05002514 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002515 weston_subsurface_commit_from_cache(sub);
2516
2517 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2518 if (tmp->surface != surface)
2519 weston_subsurface_parent_commit(tmp, 1);
2520 }
2521}
2522
2523static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002524weston_subsurface_parent_commit(struct weston_subsurface *sub,
2525 int parent_is_synchronized)
2526{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002527 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002528 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002529 wl_list_for_each(view, &sub->surface->views, surface_link)
2530 weston_view_set_position(view,
2531 sub->position.x,
2532 sub->position.y);
2533
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002534 sub->position.set = 0;
2535 }
2536
2537 if (parent_is_synchronized || sub->synchronized)
2538 weston_subsurface_synchronized_commit(sub);
2539}
2540
2541static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002542subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002543{
2544 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002545 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002546
Jason Ekstranda7af7042013-10-12 22:38:11 -05002547 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002548 weston_view_set_position(view,
2549 view->geometry.x + dx,
2550 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002551
2552 /* No need to check parent mappedness, because if parent is not
2553 * mapped, parent is not in a visible layer, so this sub-surface
2554 * will not be drawn either.
2555 */
2556 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002557 struct weston_output *output;
2558
Pekka Paalanene67858b2013-04-25 13:57:42 +03002559 /* Cannot call weston_surface_update_transform(),
2560 * because that would call it also for the parent surface,
2561 * which might not be mapped yet. That would lead to
2562 * inconsistent state, where the window could never be
2563 * mapped.
2564 *
2565 * Instead just assing any output, to make
2566 * weston_surface_is_mapped() return true, so that when the
2567 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002568 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569 */
2570 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002571 output = container_of(compositor->output_list.next,
2572 struct weston_output, link);
2573
2574 surface->output = output;
2575 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002576 }
2577}
2578
2579static struct weston_subsurface *
2580weston_surface_to_subsurface(struct weston_surface *surface)
2581{
2582 if (surface->configure == subsurface_configure)
2583 return surface->configure_private;
2584
2585 return NULL;
2586}
2587
Pekka Paalanen01388e22013-04-25 13:57:44 +03002588WL_EXPORT struct weston_surface *
2589weston_surface_get_main_surface(struct weston_surface *surface)
2590{
2591 struct weston_subsurface *sub;
2592
2593 while (surface && (sub = weston_surface_to_subsurface(surface)))
2594 surface = sub->parent;
2595
2596 return surface;
2597}
2598
Pekka Paalanene67858b2013-04-25 13:57:42 +03002599static void
2600subsurface_set_position(struct wl_client *client,
2601 struct wl_resource *resource, int32_t x, int32_t y)
2602{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002603 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604
2605 if (!sub)
2606 return;
2607
2608 sub->position.x = x;
2609 sub->position.y = y;
2610 sub->position.set = 1;
2611}
2612
2613static struct weston_subsurface *
2614subsurface_from_surface(struct weston_surface *surface)
2615{
2616 struct weston_subsurface *sub;
2617
2618 sub = weston_surface_to_subsurface(surface);
2619 if (sub)
2620 return sub;
2621
2622 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2623 if (sub->surface == surface)
2624 return sub;
2625
2626 return NULL;
2627}
2628
2629static struct weston_subsurface *
2630subsurface_sibling_check(struct weston_subsurface *sub,
2631 struct weston_surface *surface,
2632 const char *request)
2633{
2634 struct weston_subsurface *sibling;
2635
2636 sibling = subsurface_from_surface(surface);
2637
2638 if (!sibling) {
2639 wl_resource_post_error(sub->resource,
2640 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2641 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002642 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 return NULL;
2644 }
2645
2646 if (sibling->parent != sub->parent) {
2647 wl_resource_post_error(sub->resource,
2648 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2649 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002650 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002651 return NULL;
2652 }
2653
2654 return sibling;
2655}
2656
2657static void
2658subsurface_place_above(struct wl_client *client,
2659 struct wl_resource *resource,
2660 struct wl_resource *sibling_resource)
2661{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002662 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002663 struct weston_surface *surface =
2664 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665 struct weston_subsurface *sibling;
2666
2667 if (!sub)
2668 return;
2669
2670 sibling = subsurface_sibling_check(sub, surface, "place_above");
2671 if (!sibling)
2672 return;
2673
2674 wl_list_remove(&sub->parent_link_pending);
2675 wl_list_insert(sibling->parent_link_pending.prev,
2676 &sub->parent_link_pending);
2677}
2678
2679static void
2680subsurface_place_below(struct wl_client *client,
2681 struct wl_resource *resource,
2682 struct wl_resource *sibling_resource)
2683{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002684 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002685 struct weston_surface *surface =
2686 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002687 struct weston_subsurface *sibling;
2688
2689 if (!sub)
2690 return;
2691
2692 sibling = subsurface_sibling_check(sub, surface, "place_below");
2693 if (!sibling)
2694 return;
2695
2696 wl_list_remove(&sub->parent_link_pending);
2697 wl_list_insert(&sibling->parent_link_pending,
2698 &sub->parent_link_pending);
2699}
2700
2701static void
2702subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2703{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002704 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002705
2706 if (sub)
2707 sub->synchronized = 1;
2708}
2709
2710static void
2711subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2712{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002713 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002714
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002715 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002716 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002717
2718 /* If sub became effectively desynchronized, flush. */
2719 if (!weston_subsurface_is_synchronized(sub))
2720 weston_subsurface_synchronized_commit(sub);
2721 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002722}
2723
2724static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002725weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2726{
2727 wl_list_remove(&sub->parent_link);
2728 wl_list_remove(&sub->parent_link_pending);
2729 wl_list_remove(&sub->parent_destroy_listener.link);
2730 sub->parent = NULL;
2731}
2732
2733static void
2734weston_subsurface_destroy(struct weston_subsurface *sub);
2735
2736static void
2737subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2738{
2739 struct weston_subsurface *sub =
2740 container_of(listener, struct weston_subsurface,
2741 surface_destroy_listener);
2742 assert(data == &sub->surface->resource);
2743
2744 /* The protocol object (wl_resource) is left inert. */
2745 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002746 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002747
2748 weston_subsurface_destroy(sub);
2749}
2750
2751static void
2752subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2753{
2754 struct weston_subsurface *sub =
2755 container_of(listener, struct weston_subsurface,
2756 parent_destroy_listener);
2757 assert(data == &sub->parent->resource);
2758 assert(sub->surface != sub->parent);
2759
2760 if (weston_surface_is_mapped(sub->surface))
2761 weston_surface_unmap(sub->surface);
2762
2763 weston_subsurface_unlink_parent(sub);
2764}
2765
2766static void
2767subsurface_resource_destroy(struct wl_resource *resource)
2768{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002769 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002770
2771 if (sub)
2772 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002773}
2774
2775static void
2776subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2777{
2778 wl_resource_destroy(resource);
2779}
2780
2781static void
2782weston_subsurface_link_parent(struct weston_subsurface *sub,
2783 struct weston_surface *parent)
2784{
2785 sub->parent = parent;
2786 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002787 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788 &sub->parent_destroy_listener);
2789
2790 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2791 wl_list_insert(&parent->subsurface_list_pending,
2792 &sub->parent_link_pending);
2793}
2794
2795static void
2796weston_subsurface_link_surface(struct weston_subsurface *sub,
2797 struct weston_surface *surface)
2798{
2799 sub->surface = surface;
2800 sub->surface_destroy_listener.notify =
2801 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002802 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002803 &sub->surface_destroy_listener);
2804}
2805
2806static void
2807weston_subsurface_destroy(struct weston_subsurface *sub)
2808{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002809 struct weston_view *view, *next;
2810
Pekka Paalanene67858b2013-04-25 13:57:42 +03002811 assert(sub->surface);
2812
2813 if (sub->resource) {
2814 assert(weston_surface_to_subsurface(sub->surface) == sub);
2815 assert(sub->parent_destroy_listener.notify ==
2816 subsurface_handle_parent_destroy);
2817
George Kiagiadakised04d382014-06-13 18:10:26 +02002818 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2819 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002820 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002821 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002822
Pekka Paalanene67858b2013-04-25 13:57:42 +03002823 if (sub->parent)
2824 weston_subsurface_unlink_parent(sub);
2825
Jason Ekstrand7b982072014-05-20 14:33:03 -05002826 weston_surface_state_fini(&sub->cached);
2827 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002828
2829 sub->surface->configure = NULL;
2830 sub->surface->configure_private = NULL;
2831 } else {
2832 /* the dummy weston_subsurface for the parent itself */
2833 assert(sub->parent_destroy_listener.notify == NULL);
2834 wl_list_remove(&sub->parent_link);
2835 wl_list_remove(&sub->parent_link_pending);
2836 }
2837
2838 wl_list_remove(&sub->surface_destroy_listener.link);
2839 free(sub);
2840}
2841
2842static const struct wl_subsurface_interface subsurface_implementation = {
2843 subsurface_destroy,
2844 subsurface_set_position,
2845 subsurface_place_above,
2846 subsurface_place_below,
2847 subsurface_set_sync,
2848 subsurface_set_desync
2849};
2850
2851static struct weston_subsurface *
2852weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2853 struct weston_surface *parent)
2854{
2855 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002856 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002857
2858 sub = calloc(1, sizeof *sub);
2859 if (!sub)
2860 return NULL;
2861
Jason Ekstranda7af7042013-10-12 22:38:11 -05002862 wl_list_init(&sub->unused_views);
2863
Jason Ekstranda85118c2013-06-27 20:17:02 -05002864 sub->resource =
2865 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002866 if (!sub->resource) {
2867 free(sub);
2868 return NULL;
2869 }
2870
Jason Ekstranda85118c2013-06-27 20:17:02 -05002871 wl_resource_set_implementation(sub->resource,
2872 &subsurface_implementation,
2873 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002874 weston_subsurface_link_surface(sub, surface);
2875 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002876 weston_surface_state_init(&sub->cached);
2877 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002878 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002879
2880 return sub;
2881}
2882
2883/* Create a dummy subsurface for having the parent itself in its
2884 * sub-surface lists. Makes stacking order manipulation easy.
2885 */
2886static struct weston_subsurface *
2887weston_subsurface_create_for_parent(struct weston_surface *parent)
2888{
2889 struct weston_subsurface *sub;
2890
2891 sub = calloc(1, sizeof *sub);
2892 if (!sub)
2893 return NULL;
2894
2895 weston_subsurface_link_surface(sub, parent);
2896 sub->parent = parent;
2897 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2898 wl_list_insert(&parent->subsurface_list_pending,
2899 &sub->parent_link_pending);
2900
2901 return sub;
2902}
2903
2904static void
2905subcompositor_get_subsurface(struct wl_client *client,
2906 struct wl_resource *resource,
2907 uint32_t id,
2908 struct wl_resource *surface_resource,
2909 struct wl_resource *parent_resource)
2910{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002911 struct weston_surface *surface =
2912 wl_resource_get_user_data(surface_resource);
2913 struct weston_surface *parent =
2914 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002915 struct weston_subsurface *sub;
2916 static const char where[] = "get_subsurface: wl_subsurface@";
2917
2918 if (surface == parent) {
2919 wl_resource_post_error(resource,
2920 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2921 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002922 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002923 return;
2924 }
2925
2926 if (weston_surface_to_subsurface(surface)) {
2927 wl_resource_post_error(resource,
2928 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2929 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002930 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002931 return;
2932 }
2933
2934 if (surface->configure) {
2935 wl_resource_post_error(resource,
2936 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2937 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002938 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939 return;
2940 }
2941
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002942 if (weston_surface_get_main_surface(parent) == surface) {
2943 wl_resource_post_error(resource,
2944 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2945 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002946 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03002947 return;
2948 }
2949
Pekka Paalanene67858b2013-04-25 13:57:42 +03002950 /* make sure the parent is in its own list */
2951 if (wl_list_empty(&parent->subsurface_list)) {
2952 if (!weston_subsurface_create_for_parent(parent)) {
2953 wl_resource_post_no_memory(resource);
2954 return;
2955 }
2956 }
2957
2958 sub = weston_subsurface_create(id, surface, parent);
2959 if (!sub) {
2960 wl_resource_post_no_memory(resource);
2961 return;
2962 }
2963
2964 surface->configure = subsurface_configure;
2965 surface->configure_private = sub;
2966}
2967
2968static void
2969subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
2970{
2971 wl_resource_destroy(resource);
2972}
2973
2974static const struct wl_subcompositor_interface subcompositor_interface = {
2975 subcompositor_destroy,
2976 subcompositor_get_subsurface
2977};
2978
2979static void
2980bind_subcompositor(struct wl_client *client,
2981 void *data, uint32_t version, uint32_t id)
2982{
2983 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05002984 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002985
Jason Ekstranda85118c2013-06-27 20:17:02 -05002986 resource =
2987 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002988 if (resource == NULL) {
2989 wl_client_post_no_memory(client);
2990 return;
2991 }
2992 wl_resource_set_implementation(resource, &subcompositor_interface,
2993 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002994}
2995
2996static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02002997weston_compositor_dpms(struct weston_compositor *compositor,
2998 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002999{
3000 struct weston_output *output;
3001
3002 wl_list_for_each(output, &compositor->output_list, link)
3003 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003004 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003005}
3006
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003007WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003008weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003009{
Neil Roberts8b62e202013-09-30 13:14:47 +01003010 uint32_t old_state = compositor->state;
3011
3012 /* The state needs to be changed before emitting the wake
3013 * signal because that may try to schedule a repaint which
3014 * will not work if the compositor is still sleeping */
3015 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3016
3017 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003018 case WESTON_COMPOSITOR_SLEEPING:
3019 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3020 /* fall through */
3021 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003022 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003023 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003024 /* fall through */
3025 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003026 wl_event_source_timer_update(compositor->idle_source,
3027 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003028 }
3029}
3030
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003031WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003032weston_compositor_offscreen(struct weston_compositor *compositor)
3033{
3034 switch (compositor->state) {
3035 case WESTON_COMPOSITOR_OFFSCREEN:
3036 return;
3037 case WESTON_COMPOSITOR_SLEEPING:
3038 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3039 /* fall through */
3040 default:
3041 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3042 wl_event_source_timer_update(compositor->idle_source, 0);
3043 }
3044}
3045
3046WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003047weston_compositor_sleep(struct weston_compositor *compositor)
3048{
3049 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3050 return;
3051
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003052 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003053 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3054 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3055}
3056
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003057static int
3058idle_handler(void *data)
3059{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003060 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003061
3062 if (compositor->idle_inhibit)
3063 return 1;
3064
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003065 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003066 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003067
3068 return 1;
3069}
3070
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003071WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003072weston_plane_init(struct weston_plane *plane,
3073 struct weston_compositor *ec,
3074 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003075{
3076 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003077 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003078 plane->x = x;
3079 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003080 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003081
3082 /* Init the link so that the call to wl_list_remove() when releasing
3083 * the plane without ever stacking doesn't lead to a crash */
3084 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003085}
3086
3087WL_EXPORT void
3088weston_plane_release(struct weston_plane *plane)
3089{
Xiong Zhang97116532013-10-23 13:58:31 +08003090 struct weston_view *view;
3091
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003092 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003093 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003094
Xiong Zhang97116532013-10-23 13:58:31 +08003095 wl_list_for_each(view, &plane->compositor->view_list, link) {
3096 if (view->plane == plane)
3097 view->plane = NULL;
3098 }
3099
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003100 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003101}
3102
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003103WL_EXPORT void
3104weston_compositor_stack_plane(struct weston_compositor *ec,
3105 struct weston_plane *plane,
3106 struct weston_plane *above)
3107{
3108 if (above)
3109 wl_list_insert(above->link.prev, &plane->link);
3110 else
3111 wl_list_insert(&ec->plane_list, &plane->link);
3112}
3113
Casey Dahlin9074db52012-04-19 22:50:09 -04003114static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003115{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003116 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003117}
3118
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003119static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003120bind_output(struct wl_client *client,
3121 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003122{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003123 struct weston_output *output = data;
3124 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003125 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003126
Jason Ekstranda85118c2013-06-27 20:17:02 -05003127 resource = wl_resource_create(client, &wl_output_interface,
3128 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003129 if (resource == NULL) {
3130 wl_client_post_no_memory(client);
3131 return;
3132 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003133
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003134 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003135 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003136
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003137 wl_output_send_geometry(resource,
3138 output->x,
3139 output->y,
3140 output->mm_width,
3141 output->mm_height,
3142 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003143 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003144 output->transform);
Alexander Larsson4ea95522013-05-22 14:41:37 +02003145 if (version >= 2)
3146 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003147 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003148
3149 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003150 wl_output_send_mode(resource,
3151 mode->flags,
3152 mode->width,
3153 mode->height,
3154 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003155 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003156
3157 if (version >= 2)
3158 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003159}
3160
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003161/* Move other outputs when one is removed so the space remains contiguos. */
3162static void
3163weston_compositor_remove_output(struct weston_compositor *compositor,
3164 struct weston_output *remove_output)
3165{
3166 struct weston_output *output;
3167 int offset = 0;
3168
3169 wl_list_for_each(output, &compositor->output_list, link) {
3170 if (output == remove_output) {
3171 offset = output->width;
3172 continue;
3173 }
3174
3175 if (offset > 0) {
3176 weston_output_move(output,
3177 output->x - offset, output->y);
3178 output->dirty = 1;
3179 }
3180 }
3181}
3182
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003183WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003184weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003185{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003186 output->destroying = 1;
3187
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003188 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003189 wl_list_remove(&output->link);
3190
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003191 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003192 wl_signal_emit(&output->destroy_signal, output);
3193
Richard Hughesafe690c2013-05-02 10:10:04 +01003194 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003195 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003196 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003197 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003198
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003199 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003200}
3201
Scott Moreau1bad5db2012-08-18 01:04:05 -06003202static void
3203weston_output_compute_transform(struct weston_output *output)
3204{
3205 struct weston_matrix transform;
3206 int flip;
3207
3208 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003209 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003210
3211 switch(output->transform) {
3212 case WL_OUTPUT_TRANSFORM_FLIPPED:
3213 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3214 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3215 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003216 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003217 flip = -1;
3218 break;
3219 default:
3220 flip = 1;
3221 break;
3222 }
3223
3224 switch(output->transform) {
3225 case WL_OUTPUT_TRANSFORM_NORMAL:
3226 case WL_OUTPUT_TRANSFORM_FLIPPED:
3227 transform.d[0] = flip;
3228 transform.d[1] = 0;
3229 transform.d[4] = 0;
3230 transform.d[5] = 1;
3231 break;
3232 case WL_OUTPUT_TRANSFORM_90:
3233 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3234 transform.d[0] = 0;
3235 transform.d[1] = -flip;
3236 transform.d[4] = 1;
3237 transform.d[5] = 0;
3238 break;
3239 case WL_OUTPUT_TRANSFORM_180:
3240 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3241 transform.d[0] = -flip;
3242 transform.d[1] = 0;
3243 transform.d[4] = 0;
3244 transform.d[5] = -1;
3245 break;
3246 case WL_OUTPUT_TRANSFORM_270:
3247 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3248 transform.d[0] = 0;
3249 transform.d[1] = flip;
3250 transform.d[4] = -1;
3251 transform.d[5] = 0;
3252 break;
3253 default:
3254 break;
3255 }
3256
3257 weston_matrix_multiply(&output->matrix, &transform);
3258}
3259
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003260WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003261weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003262{
Scott Moreau850ca422012-05-21 15:21:25 -06003263 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003264
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003265 weston_matrix_init(&output->matrix);
3266 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003267 -(output->x + output->width / 2.0),
3268 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003269
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003270 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003271 2.0 / output->width,
3272 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003273
Scott Moreauccbf29d2012-02-22 14:21:41 -07003274 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003275 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003276 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003277 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3278 output->zoom.trans_y, 0);
3279 weston_matrix_scale(&output->matrix, magnification,
3280 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003281 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003282
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003283 weston_output_compute_transform(output);
3284
Scott Moreauccbf29d2012-02-22 14:21:41 -07003285 output->dirty = 0;
3286}
3287
Scott Moreau1bad5db2012-08-18 01:04:05 -06003288static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003289weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003290{
3291 output->transform = transform;
3292
3293 switch (transform) {
3294 case WL_OUTPUT_TRANSFORM_90:
3295 case WL_OUTPUT_TRANSFORM_270:
3296 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3297 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3298 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003299 output->width = output->current_mode->height;
3300 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003301 break;
3302 case WL_OUTPUT_TRANSFORM_NORMAL:
3303 case WL_OUTPUT_TRANSFORM_180:
3304 case WL_OUTPUT_TRANSFORM_FLIPPED:
3305 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003306 output->width = output->current_mode->width;
3307 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003308 break;
3309 default:
3310 break;
3311 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003312
Hardening57388e42013-09-18 23:56:36 +02003313 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003314 output->width /= scale;
3315 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003316}
3317
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003318static void
3319weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003320{
3321 output->x = x;
3322 output->y = y;
3323
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003324 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003325 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003326 output->width,
3327 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003328}
3329
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003330WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003331weston_output_move(struct weston_output *output, int x, int y)
3332{
3333 pixman_region32_t old_region;
3334 struct wl_resource *resource;
3335
3336 output->move_x = x - output->x;
3337 output->move_y = y - output->y;
3338
3339 if (output->move_x == 0 && output->move_y == 0)
3340 return;
3341
3342 pixman_region32_init(&old_region);
3343 pixman_region32_copy(&old_region, &output->region);
3344
3345 weston_output_init_geometry(output, x, y);
3346
3347 output->dirty = 1;
3348
3349 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003350 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003351
3352 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003353 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003354 wl_output_send_geometry(resource,
3355 output->x,
3356 output->y,
3357 output->mm_width,
3358 output->mm_height,
3359 output->subpixel,
3360 output->make,
3361 output->model,
3362 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003363
3364 if (wl_resource_get_version(resource) >= 2)
3365 wl_output_send_done(resource);
3366 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003367}
3368
3369WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003370weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003371 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003372 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003373{
3374 output->compositor = c;
3375 output->x = x;
3376 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003377 output->mm_width = mm_width;
3378 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003379 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003380 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003381
Alexander Larsson0b135062013-05-28 16:23:36 +02003382 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003383 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003384
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003385 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003386 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003387
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003388 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003389 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003390 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003391 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003392
Casey Dahlin58ba1372012-04-19 22:50:08 -04003393 output->id = ffs(~output->compositor->output_id_pool) - 1;
3394 output->compositor->output_id_pool |= 1 << output->id;
3395
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003396 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003397 wl_global_create(c->wl_display, &wl_output_interface, 2,
3398 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003399 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003400}
3401
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003402WL_EXPORT void
3403weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003404 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003405 wl_fixed_t *x, wl_fixed_t *y)
3406{
3407 wl_fixed_t tx, ty;
3408 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003409 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003410
Hardeningff39efa2013-09-18 23:56:35 +02003411 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3412 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003413
3414 switch(output->transform) {
3415 case WL_OUTPUT_TRANSFORM_NORMAL:
3416 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003417 tx = device_x;
3418 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003419 break;
3420 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003421 tx = device_y;
3422 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003423 break;
3424 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003425 tx = width - device_x;
3426 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003427 break;
3428 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003429 tx = width - device_y;
3430 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003431 break;
3432 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003433 tx = width - device_x;
3434 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003435 break;
3436 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003437 tx = width - device_y;
3438 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003439 break;
3440 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003441 tx = device_x;
3442 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003443 break;
3444 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003445 tx = device_y;
3446 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003447 break;
3448 }
3449
Neil Robertseb5a2002014-05-01 16:13:55 +01003450 tx /= output->current_scale;
3451 ty /= output->current_scale;
3452
3453 if (output->zoom.active) {
3454 zoom_scale = output->zoom.spring_z.current;
3455 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3456 output->width / 2.0f *
3457 (zoom_scale + output->zoom.trans_x));
3458 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3459 output->height / 2.0f *
3460 (zoom_scale + output->zoom.trans_y));
3461 tx = wl_fixed_from_double(zx);
3462 ty = wl_fixed_from_double(zy);
3463 }
3464
3465 *x = tx + wl_fixed_from_int(output->x);
3466 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003467}
3468
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003469static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003470destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003471{
Jonny Lamb74130762013-11-26 18:19:46 +01003472 struct weston_surface *surface =
3473 wl_resource_get_user_data(resource);
3474
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003475 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003476 surface->pending.buffer_viewport.buffer.src_width =
3477 wl_fixed_from_int(-1);
3478 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003479 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003480}
3481
3482static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003483viewport_destroy(struct wl_client *client,
3484 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003485{
3486 wl_resource_destroy(resource);
3487}
3488
3489static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003490viewport_set(struct wl_client *client,
3491 struct wl_resource *resource,
3492 wl_fixed_t src_x,
3493 wl_fixed_t src_y,
3494 wl_fixed_t src_width,
3495 wl_fixed_t src_height,
3496 int32_t dst_width,
3497 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003498{
Jonny Lamb74130762013-11-26 18:19:46 +01003499 struct weston_surface *surface =
3500 wl_resource_get_user_data(resource);
3501
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003502 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003503
Jonny Lamb8ae35902013-11-26 18:19:45 +01003504 if (wl_fixed_to_double(src_width) < 0 ||
3505 wl_fixed_to_double(src_height) < 0) {
3506 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003507 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003508 "source dimensions must be non-negative (%fx%f)",
3509 wl_fixed_to_double(src_width),
3510 wl_fixed_to_double(src_height));
3511 return;
3512 }
3513
3514 if (dst_width <= 0 || dst_height <= 0) {
3515 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003516 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003517 "destination dimensions must be positive (%dx%d)",
3518 dst_width, dst_height);
3519 return;
3520 }
3521
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003522 surface->pending.buffer_viewport.buffer.src_x = src_x;
3523 surface->pending.buffer_viewport.buffer.src_y = src_y;
3524 surface->pending.buffer_viewport.buffer.src_width = src_width;
3525 surface->pending.buffer_viewport.buffer.src_height = src_height;
3526 surface->pending.buffer_viewport.surface.width = dst_width;
3527 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003528 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003529}
3530
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003531static void
3532viewport_set_source(struct wl_client *client,
3533 struct wl_resource *resource,
3534 wl_fixed_t src_x,
3535 wl_fixed_t src_y,
3536 wl_fixed_t src_width,
3537 wl_fixed_t src_height)
3538{
3539 struct weston_surface *surface =
3540 wl_resource_get_user_data(resource);
3541
3542 assert(surface->viewport_resource != NULL);
3543
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003544 if (src_width == wl_fixed_from_int(-1) &&
3545 src_height == wl_fixed_from_int(-1)) {
3546 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003547 surface->pending.buffer_viewport.buffer.src_width =
3548 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003549 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003550 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003551 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003552
3553 if (src_width <= 0 || src_height <= 0) {
3554 wl_resource_post_error(resource,
3555 WL_VIEWPORT_ERROR_BAD_VALUE,
3556 "source size must be positive (%fx%f)",
3557 wl_fixed_to_double(src_width),
3558 wl_fixed_to_double(src_height));
3559 return;
3560 }
3561
3562 surface->pending.buffer_viewport.buffer.src_x = src_x;
3563 surface->pending.buffer_viewport.buffer.src_y = src_y;
3564 surface->pending.buffer_viewport.buffer.src_width = src_width;
3565 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003566 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003567}
3568
3569static void
3570viewport_set_destination(struct wl_client *client,
3571 struct wl_resource *resource,
3572 int32_t dst_width,
3573 int32_t dst_height)
3574{
3575 struct weston_surface *surface =
3576 wl_resource_get_user_data(resource);
3577
3578 assert(surface->viewport_resource != NULL);
3579
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003580 if (dst_width == -1 && dst_height == -1) {
3581 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003582 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003583 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003584 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003585 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003586
3587 if (dst_width <= 0 || dst_height <= 0) {
3588 wl_resource_post_error(resource,
3589 WL_VIEWPORT_ERROR_BAD_VALUE,
3590 "destination size must be positive (%dx%d)",
3591 dst_width, dst_height);
3592 return;
3593 }
3594
3595 surface->pending.buffer_viewport.surface.width = dst_width;
3596 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003597 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003598}
3599
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003600static const struct wl_viewport_interface viewport_interface = {
3601 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003602 viewport_set,
3603 viewport_set_source,
3604 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003605};
3606
3607static void
3608scaler_destroy(struct wl_client *client,
3609 struct wl_resource *resource)
3610{
3611 wl_resource_destroy(resource);
3612}
3613
3614static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003615scaler_get_viewport(struct wl_client *client,
3616 struct wl_resource *scaler,
3617 uint32_t id,
3618 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003619{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003620 int version = wl_resource_get_version(scaler);
3621 struct weston_surface *surface =
3622 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003623 struct wl_resource *resource;
3624
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003625 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003626 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003627 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3628 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003629 return;
3630 }
3631
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003632 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003633 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003634 if (resource == NULL) {
3635 wl_client_post_no_memory(client);
3636 return;
3637 }
3638
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003639 wl_resource_set_implementation(resource, &viewport_interface,
3640 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003641
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003642 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003643}
3644
3645static const struct wl_scaler_interface scaler_interface = {
3646 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003647 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003648};
3649
3650static void
3651bind_scaler(struct wl_client *client,
3652 void *data, uint32_t version, uint32_t id)
3653{
3654 struct wl_resource *resource;
3655
3656 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003657 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003658 if (resource == NULL) {
3659 wl_client_post_no_memory(client);
3660 return;
3661 }
3662
3663 wl_resource_set_implementation(resource, &scaler_interface,
3664 NULL, NULL);
3665}
3666
3667static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003668compositor_bind(struct wl_client *client,
3669 void *data, uint32_t version, uint32_t id)
3670{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003671 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003672 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003673
Jason Ekstranda85118c2013-06-27 20:17:02 -05003674 resource = wl_resource_create(client, &wl_compositor_interface,
3675 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003676 if (resource == NULL) {
3677 wl_client_post_no_memory(client);
3678 return;
3679 }
3680
3681 wl_resource_set_implementation(resource, &compositor_interface,
3682 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003683}
3684
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003685static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003686log_uname(void)
3687{
3688 struct utsname usys;
3689
3690 uname(&usys);
3691
3692 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3693 usys.version, usys.machine);
3694}
3695
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003696WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003697weston_environment_get_fd(const char *env)
3698{
3699 char *e, *end;
3700 int fd, flags;
3701
3702 e = getenv(env);
3703 if (!e)
3704 return -1;
3705 fd = strtol(e, &end, 0);
3706 if (*end != '\0')
3707 return -1;
3708
3709 flags = fcntl(fd, F_GETFD);
3710 if (flags == -1)
3711 return -1;
3712
3713 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3714 unsetenv(env);
3715
3716 return fd;
3717}
3718
3719WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003720weston_compositor_init(struct weston_compositor *ec,
3721 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003722 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003723 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003724{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003725 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003726 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003727 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003728
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003729 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003730 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003731 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003732 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003733 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003734 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003735 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003736 wl_signal_init(&ec->idle_signal);
3737 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003738 wl_signal_init(&ec->show_input_panel_signal);
3739 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003740 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003741 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003742 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003743 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003744 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003745 wl_signal_init(&ec->session_signal);
3746 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003747
Casey Dahlin58ba1372012-04-19 22:50:08 -04003748 ec->output_id_pool = 0;
3749
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003750 if (!wl_global_create(display, &wl_compositor_interface, 3,
3751 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003752 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003753
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003754 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3755 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003756 return -1;
3757
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003758 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003759 ec, bind_scaler))
3760 return -1;
3761
Jason Ekstranda7af7042013-10-12 22:38:11 -05003762 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003763 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003764 wl_list_init(&ec->layer_list);
3765 wl_list_init(&ec->seat_list);
3766 wl_list_init(&ec->output_list);
3767 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003768 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003769 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003770 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003771 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003772 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003773
Xiong Zhang97116532013-10-23 13:58:31 +08003774 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003775 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003776
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003777 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3778 weston_config_section_get_string(s, "keymap_rules",
3779 (char **) &xkb_names.rules, NULL);
3780 weston_config_section_get_string(s, "keymap_model",
3781 (char **) &xkb_names.model, NULL);
3782 weston_config_section_get_string(s, "keymap_layout",
3783 (char **) &xkb_names.layout, NULL);
3784 weston_config_section_get_string(s, "keymap_variant",
3785 (char **) &xkb_names.variant, NULL);
3786 weston_config_section_get_string(s, "keymap_options",
3787 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003788
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003789 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3790 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003791
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003792 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003793
3794 wl_data_device_manager_init(ec->wl_display);
3795
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003796 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003797
Daniel Stone725c2c32012-06-22 14:04:36 +01003798 loop = wl_display_get_event_loop(ec->wl_display);
3799 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3800 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3801
3802 ec->input_loop = wl_event_loop_create();
3803
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003804 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3805 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3806
3807 weston_compositor_schedule_repaint(ec);
3808
Daniel Stone725c2c32012-06-22 14:04:36 +01003809 return 0;
3810}
3811
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003812WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003813weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003814{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003815 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003816
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003817 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003818 if (ec->input_loop_source)
3819 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003820
Matt Roper361d2ad2011-08-29 13:52:23 -07003821 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003822 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003823 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003824
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003825 if (ec->renderer)
3826 ec->renderer->destroy(ec);
3827
Daniel Stone325fc2d2012-05-30 16:31:58 +01003828 weston_binding_list_destroy_all(&ec->key_binding_list);
3829 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003830 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003831 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003832 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003833
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003834 weston_plane_release(&ec->primary_plane);
3835
Jonas Ådahlc97af922012-03-28 22:36:09 +02003836 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003837
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003838 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003839}
3840
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003841WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003842weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3843 const struct weston_pointer_grab_interface *interface)
3844{
3845 struct weston_seat *seat;
3846
3847 ec->default_pointer_grab = interface;
3848 wl_list_for_each(seat, &ec->seat_list, link) {
3849 if (seat->pointer) {
3850 weston_pointer_set_default_grab(seat->pointer,
3851 interface);
3852 }
3853 }
3854}
3855
3856WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003857weston_version(int *major, int *minor, int *micro)
3858{
3859 *major = WESTON_VERSION_MAJOR;
3860 *minor = WESTON_VERSION_MINOR;
3861 *micro = WESTON_VERSION_MICRO;
3862}
3863
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003864static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003865 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003866 const char *desc;
3867} capability_strings[] = {
3868 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003869 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003870};
3871
3872static void
3873weston_compositor_log_capabilities(struct weston_compositor *compositor)
3874{
3875 unsigned i;
3876 int yes;
3877
3878 weston_log("Compositor capabilities:\n");
3879 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3880 yes = compositor->capabilities & capability_strings[i].bit;
3881 weston_log_continue(STAMP_SPACE "%s %s\n",
3882 capability_strings[i].desc,
3883 yes ? "yes" : "no");
3884 }
3885}
3886
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003887static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003888{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003889 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003890
Martin Minarik6d118362012-06-07 18:01:59 +02003891 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003892 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003893
3894 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003895}
3896
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003897#ifdef HAVE_LIBUNWIND
3898
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003899static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003900print_backtrace(void)
3901{
3902 unw_cursor_t cursor;
3903 unw_context_t context;
3904 unw_word_t off;
3905 unw_proc_info_t pip;
3906 int ret, i = 0;
3907 char procname[256];
3908 const char *filename;
3909 Dl_info dlinfo;
3910
3911 pip.unwind_info = NULL;
3912 ret = unw_getcontext(&context);
3913 if (ret) {
3914 weston_log("unw_getcontext: %d\n", ret);
3915 return;
3916 }
3917
3918 ret = unw_init_local(&cursor, &context);
3919 if (ret) {
3920 weston_log("unw_init_local: %d\n", ret);
3921 return;
3922 }
3923
3924 ret = unw_step(&cursor);
3925 while (ret > 0) {
3926 ret = unw_get_proc_info(&cursor, &pip);
3927 if (ret) {
3928 weston_log("unw_get_proc_info: %d\n", ret);
3929 break;
3930 }
3931
3932 ret = unw_get_proc_name(&cursor, procname, 256, &off);
3933 if (ret && ret != -UNW_ENOMEM) {
3934 if (ret != -UNW_EUNSPEC)
3935 weston_log("unw_get_proc_name: %d\n", ret);
3936 procname[0] = '?';
3937 procname[1] = 0;
3938 }
3939
3940 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
3941 *dlinfo.dli_fname)
3942 filename = dlinfo.dli_fname;
3943 else
3944 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08003945
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003946 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
3947 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
3948
3949 ret = unw_step(&cursor);
3950 if (ret < 0)
3951 weston_log("unw_step: %d\n", ret);
3952 }
3953}
3954
3955#else
3956
3957static void
3958print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003959{
3960 void *buffer[32];
3961 int i, count;
3962 Dl_info info;
3963
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003964 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3965 for (i = 0; i < count; i++) {
3966 dladdr(buffer[i], &info);
3967 weston_log(" [%016lx] %s (%s)\n",
3968 (long) buffer[i],
3969 info.dli_sname ? info.dli_sname : "--",
3970 info.dli_fname);
3971 }
3972}
3973
3974#endif
3975
3976static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01003977on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003978{
Peter Maatmane5b42e42013-03-27 22:38:53 +01003979 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003980 * then call the backend restore function, which will switch
3981 * back to the vt we launched from or ungrab X etc and then
3982 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01003983 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003984 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01003985 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003986
Peter Maatmane5b42e42013-03-27 22:38:53 +01003987 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003988
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003989 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003990
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003991 segv_compositor->restore(segv_compositor);
3992
3993 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003994}
3995
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03003996WL_EXPORT void *
3997weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003998{
3999 char path[PATH_MAX];
4000 void *module, *init;
4001
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004002 if (name == NULL)
4003 return NULL;
4004
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004005 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004006 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004007 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004008 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004009
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004010 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4011 if (module) {
4012 weston_log("Module '%s' already loaded\n", path);
4013 dlclose(module);
4014 return NULL;
4015 }
4016
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004017 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004018 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004019 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004020 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004021 return NULL;
4022 }
4023
4024 init = dlsym(module, entrypoint);
4025 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004026 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004027 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004028 return NULL;
4029 }
4030
4031 return init;
4032}
4033
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004034static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004035load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004036 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004037{
4038 const char *p, *end;
4039 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004040 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004041 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004042
4043 if (modules == NULL)
4044 return 0;
4045
4046 p = modules;
4047 while (*p) {
4048 end = strchrnul(p, ',');
4049 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004050 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004051 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004052 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004053 p = end;
4054 while (*p == ',')
4055 p++;
4056
4057 }
4058
4059 return 0;
4060}
4061
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004062static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004063 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4064
4065static const char xdg_wrong_message[] =
4066 "fatal: environment variable XDG_RUNTIME_DIR\n"
4067 "is set to \"%s\", which is not a directory.\n";
4068
4069static const char xdg_wrong_mode_message[] =
4070 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004071 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4072 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004073
4074static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004075 "Refer to your distribution on how to get it, or\n"
4076 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4077 "on how to implement it.\n";
4078
Martin Minarik37032f82012-06-18 20:15:18 +02004079static void
4080verify_xdg_runtime_dir(void)
4081{
4082 char *dir = getenv("XDG_RUNTIME_DIR");
4083 struct stat s;
4084
4085 if (!dir) {
4086 weston_log(xdg_error_message);
4087 weston_log_continue(xdg_detail_message);
4088 exit(EXIT_FAILURE);
4089 }
4090
4091 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4092 weston_log(xdg_wrong_message, dir);
4093 weston_log_continue(xdg_detail_message);
4094 exit(EXIT_FAILURE);
4095 }
4096
4097 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4098 weston_log(xdg_wrong_mode_message,
4099 dir, s.st_mode & 0777, s.st_uid);
4100 weston_log_continue(xdg_detail_message);
4101 }
4102}
4103
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004104static int
4105usage(int error_code)
4106{
4107 fprintf(stderr,
4108 "Usage: weston [OPTIONS]\n\n"
4109 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4110 "Weston supports multiple backends, and depending on which backend is in use\n"
4111 "different options will be accepted.\n\n"
4112
4113
4114 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004115 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004116 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004117 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4118 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004119 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004120 " -S, --socket=NAME\tName of socket to listen on\n"
4121 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004122 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004123 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004124 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004125 " -h, --help\t\tThis help message\n\n");
4126
4127 fprintf(stderr,
4128 "Options for drm-backend.so:\n\n"
4129 " --connector=ID\tBring up only this connector\n"
4130 " --seat=SEAT\t\tThe seat that weston should run on\n"
4131 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004132 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004133 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4134
4135 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004136 "Options for fbdev-backend.so:\n\n"
4137 " --tty=TTY\t\tThe tty to use\n"
4138 " --device=DEVICE\tThe framebuffer device to use\n\n");
4139
4140 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004141 "Options for x11-backend.so:\n\n"
4142 " --width=WIDTH\t\tWidth of X window\n"
4143 " --height=HEIGHT\tHeight of X window\n"
4144 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004145 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004146 " --output-count=COUNT\tCreate multiple outputs\n"
4147 " --no-input\t\tDont create input devices\n\n");
4148
4149 fprintf(stderr,
4150 "Options for wayland-backend.so:\n\n"
4151 " --width=WIDTH\t\tWidth of Wayland surface\n"
4152 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004153 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004154 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004155 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004156 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004157 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004158 " --display=DISPLAY\tWayland display to connect to\n\n");
4159
Pekka Paalanene31e0532013-05-22 18:03:07 +03004160#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4161 fprintf(stderr,
4162 "Options for rpi-backend.so:\n\n"
4163 " --tty=TTY\t\tThe tty to use\n"
4164 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4165 " --transform=TR\tThe output transformation, TR is one of:\n"
4166 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004167 " --opaque-regions\tEnable support for opaque regions, can be "
4168 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004169 "\n");
4170#endif
4171
Hardeningc077a842013-07-08 00:51:35 +02004172#if defined(BUILD_RDP_COMPOSITOR)
4173 fprintf(stderr,
4174 "Options for rdp-backend.so:\n\n"
4175 " --width=WIDTH\t\tWidth of desktop\n"
4176 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004177 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4178 " --address=ADDR\tThe address to bind\n"
4179 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004180 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004181 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4182 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4183 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4184 "\n");
4185#endif
4186
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004187 exit(error_code);
4188}
4189
Peter Maatmane5b42e42013-03-27 22:38:53 +01004190static void
4191catch_signals(void)
4192{
4193 struct sigaction action;
4194
4195 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4196 action.sa_sigaction = on_caught_signal;
4197 sigemptyset(&action.sa_mask);
4198 sigaction(SIGSEGV, &action, NULL);
4199 sigaction(SIGABRT, &action, NULL);
4200}
4201
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004202static void
4203handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4204{
4205 struct wl_client *client = data;
4206
4207 weston_log("Primary client died. Closing...\n");
4208
4209 wl_display_terminate(wl_client_get_display(client));
4210}
4211
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004212int main(int argc, char *argv[])
4213{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004214 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004215 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004216 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004217 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004218 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004219 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004220 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004221 int *argc, char *argv[],
4222 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004223 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004224 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004225 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004226 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004227 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004228 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004229 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004230 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004231 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004232 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004233 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06004234 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004235 int32_t noconfig = 0;
4236 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004237 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004238 struct wl_client *primary_client;
4239 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004240
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004241 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004242 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004243 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004244 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4245 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004246 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004247 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004248 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004249 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004250 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004251 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004252
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004253 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004254
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004255 if (help)
4256 usage(EXIT_SUCCESS);
4257
Scott Moreau12245142012-08-29 15:15:58 -06004258 if (version) {
4259 printf(PACKAGE_STRING "\n");
4260 return EXIT_SUCCESS;
4261 }
4262
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004263 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004264
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004265 weston_log("%s\n"
4266 STAMP_SPACE "%s\n"
4267 STAMP_SPACE "Bug reports to: %s\n"
4268 STAMP_SPACE "Build: %s\n",
4269 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004270 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004271 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004272
Martin Minarik37032f82012-06-18 20:15:18 +02004273 verify_xdg_runtime_dir();
4274
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004275 display = wl_display_create();
4276
Tiago Vignatti2116b892011-08-08 05:52:59 -07004277 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004278 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4279 display);
4280 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4281 display);
4282 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4283 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004284
4285 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004286 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4287 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004288
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304289 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4290 ret = EXIT_FAILURE;
4291 goto out_signals;
4292 }
4293
Pekka Paalanen588bee12014-05-07 16:26:25 +03004294 if (noconfig == 0)
4295 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004296 if (config != NULL) {
4297 weston_log("Using config file '%s'\n",
4298 weston_config_get_full_path(config));
4299 } else {
4300 weston_log("Starting with no config file.\n");
4301 }
4302 section = weston_config_get_section(config, "core", NULL, NULL);
4303
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004304 if (option_backend)
4305 backend = strdup(option_backend);
4306 else
4307 weston_config_section_get_string(section, "backend", &backend,
4308 NULL);
4309
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004310 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004311 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004312 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004313 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004314 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004315 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004316 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004317 }
4318
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004319 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004320 free(backend);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304321 if (!backend_init) {
4322 ret = EXIT_FAILURE;
4323 goto out_signals;
4324 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004325
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004326 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004327 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004328 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304329 ret = EXIT_FAILURE;
4330 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004331 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004332
Peter Maatmane5b42e42013-03-27 22:38:53 +01004333 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004334 segv_compositor = ec;
4335
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004336 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004337 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004338
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04004339 setenv("WAYLAND_DISPLAY", socket_name, 1);
4340
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004341 if (option_shell)
4342 shell = strdup(option_shell);
4343 else
Jason Ekstranda985da42013-08-22 17:28:03 -05004344 weston_config_section_get_string(section, "shell", &shell,
4345 "desktop-shell.so");
Jason Ekstranda985da42013-08-22 17:28:03 -05004346
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004347 if (load_modules(ec, shell, &argc, argv) < 0) {
4348 free(shell);
Pekka Paalanen33616392012-07-30 16:56:57 +03004349 goto out;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004350 }
4351 free(shell);
4352
4353 weston_config_section_get_string(section, "modules", &modules, "");
4354 if (load_modules(ec, modules, &argc, argv) < 0) {
4355 free(modules);
4356 goto out;
4357 }
4358 free(modules);
4359
Ossama Othmana50e6e42013-05-14 09:48:26 -07004360 if (load_modules(ec, option_modules, &argc, argv) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03004361 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004362
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004363 for (i = 1; i < argc; i++)
4364 weston_log("fatal: unhandled option: %s\n", argv[i]);
4365 if (argc > 1) {
4366 ret = EXIT_FAILURE;
4367 goto out;
4368 }
4369
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004370 weston_compositor_log_capabilities(ec);
4371
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004372 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4373 if (server_socket) {
4374 weston_log("Running with single client\n");
4375 fd = strtol(server_socket, &end, 0);
4376 if (*end != '\0')
4377 fd = -1;
4378 } else {
4379 fd = -1;
4380 }
4381
4382 if (fd != -1) {
4383 primary_client = wl_client_create(display, fd);
4384 if (!primary_client) {
4385 weston_log("fatal: failed to add client: %m\n");
4386 ret = EXIT_FAILURE;
4387 goto out;
4388 }
4389 primary_client_destroyed.notify =
4390 handle_primary_client_destroyed;
4391 wl_client_add_destroy_listener(primary_client,
4392 &primary_client_destroyed);
4393 } else {
4394 if (wl_display_add_socket(display, socket_name)) {
4395 weston_log("fatal: failed to add socket: %m\n");
4396 ret = EXIT_FAILURE;
4397 goto out;
4398 }
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004399 }
4400
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004401 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004402
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004403 wl_display_run(display);
4404
4405 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004406 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004407 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004408
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004409 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004410
Daniel Stone855028f2012-05-01 20:37:10 +01004411 weston_compositor_xkb_destroy(ec);
4412
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004413 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304414
4415out_signals:
4416 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4417 if (signals[i])
4418 wl_event_source_remove(signals[i]);
4419
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004420 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004421
Martin Minarik19e6f262012-06-07 13:08:46 +02004422 weston_log_file_close();
4423
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004424 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004425}