blob: 8705950e551d388ec24bb0162339edeca0abf1ad [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>
Pekka Paalanen23ade622014-08-27 13:31:26 +030049#include <errno.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050050
Marcin Slusarz554a0da2013-02-18 13:27:22 -050051#ifdef HAVE_LIBUNWIND
52#define UNW_LOCAL_ONLY
53#include <libunwind.h>
54#endif
55
Kristian Høgsberg82863022010-06-04 21:52:02 -040056#include "compositor.h"
Jonny Lamb8ae35902013-11-26 18:19:45 +010057#include "scaler-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030058#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040059#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050060#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050061
Kristian Høgsberg27da5382011-06-21 17:32:25 -040062static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040063static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064
65static int
66sigchld_handler(int signal_number, void *data)
67{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050068 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040069 int status;
70 pid_t pid;
71
Pekka Paalanen23ade622014-08-27 13:31:26 +030072 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
73 wl_list_for_each(p, &child_process_list, link) {
74 if (p->pid == pid)
75 break;
76 }
Bill Spitzak027b9622012-03-17 15:22:03 -070077
Pekka Paalanen23ade622014-08-27 13:31:26 +030078 if (&p->link == &child_process_list) {
79 weston_log("unknown child process exited\n");
80 continue;
81 }
82
83 wl_list_remove(&p->link);
84 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040085 }
86
Pekka Paalanen23ade622014-08-27 13:31:26 +030087 if (pid < 0 && errno != ECHILD)
88 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040089
90 return 1;
91}
92
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020093static void
Alexander Larsson0b135062013-05-28 16:23:36 +020094weston_output_transform_scale_init(struct weston_output *output,
95 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020096
Rob Bradford27b17932013-06-26 18:08:46 +010097static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050098weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +010099
Alex Wu2dda6042012-04-17 17:20:47 +0800100WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +0200101weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
102 int32_t scale, enum weston_mode_switch_op op)
Alex Wu2dda6042012-04-17 17:20:47 +0800103{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200105 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200106 pixman_region32_t old_output_region;
Hardening57388e42013-09-18 23:56:36 +0200107 int ret, notify_mode_changed, notify_scale_changed;
108 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200109
Alex Wu2dda6042012-04-17 17:20:47 +0800110 if (!output->switch_mode)
111 return -1;
112
Hardening57388e42013-09-18 23:56:36 +0200113 temporary_mode = (output->original_mode != 0);
114 temporary_scale = (output->current_scale != output->original_scale);
115 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200116
Hardening57388e42013-09-18 23:56:36 +0200117 notify_mode_changed = 0;
118 notify_scale_changed = 0;
119 switch(op) {
120 case WESTON_MODE_SWITCH_SET_NATIVE:
121 output->native_mode = mode;
122 if (!temporary_mode) {
123 notify_mode_changed = 1;
124 ret = output->switch_mode(output, mode);
125 if (ret < 0)
126 return ret;
127 }
128
129 output->native_scale = scale;
130 if(!temporary_scale)
131 notify_scale_changed = 1;
132 break;
133 case WESTON_MODE_SWITCH_SET_TEMPORARY:
134 if (!temporary_mode)
135 output->original_mode = output->native_mode;
136 if (!temporary_scale)
137 output->original_scale = output->native_scale;
138
139 ret = output->switch_mode(output, mode);
140 if (ret < 0)
141 return ret;
142
Hardening57388e42013-09-18 23:56:36 +0200143 output->current_scale = scale;
144 break;
145 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
146 if (!temporary_mode) {
147 weston_log("already in the native mode\n");
148 return -1;
149 }
150
151 notify_mode_changed = (output->original_mode != output->native_mode);
152
153 ret = output->switch_mode(output, mode);
154 if (ret < 0)
155 return ret;
156
157 if (output->original_scale != output->native_scale) {
158 notify_scale_changed = 1;
159 scale = output->native_scale;
160 output->original_scale = scale;
161 }
162 output->original_mode = 0;
163
164 output->current_scale = output->native_scale;
165 break;
166 default:
167 weston_log("unknown weston_switch_mode_op %d\n", op);
168 break;
169 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200170
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200171 pixman_region32_init(&old_output_region);
172 pixman_region32_copy(&old_output_region, &output->region);
173
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200174 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200175 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200176
177 pixman_region32_init(&output->previous_damage);
178 pixman_region32_init_rect(&output->region, output->x, output->y,
179 output->width, output->height);
180
181 weston_output_update_matrix(output);
182
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200183 /* If a pointer falls outside the outputs new geometry, move it to its
184 * lower-right corner */
185 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400186 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200187 int32_t x, y;
188
189 if (!pointer)
190 continue;
191
192 x = wl_fixed_to_int(pointer->x);
193 y = wl_fixed_to_int(pointer->y);
194
195 if (!pixman_region32_contains_point(&old_output_region,
196 x, y, NULL) ||
197 pixman_region32_contains_point(&output->region,
198 x, y, NULL))
199 continue;
200
201 if (x >= output->x + output->width)
202 x = output->x + output->width - 1;
203 if (y >= output->y + output->height)
204 y = output->y + output->height - 1;
205
206 pointer->x = wl_fixed_from_int(x);
207 pointer->y = wl_fixed_from_int(y);
208 }
209
210 pixman_region32_fini(&old_output_region);
211
Hardening57388e42013-09-18 23:56:36 +0200212 /* notify clients of the changes */
213 if (notify_mode_changed || notify_scale_changed) {
214 wl_resource_for_each(resource, &output->resource_list) {
215 if(notify_mode_changed) {
216 wl_output_send_mode(resource,
217 mode->flags | WL_OUTPUT_MODE_CURRENT,
218 mode->width,
219 mode->height,
220 mode->refresh);
221 }
222
223 if (notify_scale_changed)
224 wl_output_send_scale(resource, scale);
225
226 if (wl_resource_get_version(resource) >= 2)
227 wl_output_send_done(resource);
228 }
229 }
230
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200231 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800232}
233
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400234WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500235weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400236{
237 wl_list_insert(&child_process_list, &process->link);
238}
239
Benjamin Franzke06286262011-05-06 19:12:33 +0200240static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200241child_client_exec(int sockfd, const char *path)
242{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500243 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200244 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200245 sigset_t allsigs;
246
247 /* do not give our signal mask to the new process */
248 sigfillset(&allsigs);
249 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200250
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100251 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
252 if (seteuid(getuid()) == -1) {
253 weston_log("compositor: failed seteuid\n");
254 return;
255 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500256
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500257 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
258 * non-CLOEXEC fd to pass through exec. */
259 clientfd = dup(sockfd);
260 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200261 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500262 return;
263 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200264
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500265 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200266 setenv("WAYLAND_SOCKET", s, 1);
267
268 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200269 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200270 path);
271}
272
273WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500274weston_client_launch(struct weston_compositor *compositor,
275 struct weston_process *proc,
276 const char *path,
277 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200278{
279 int sv[2];
280 pid_t pid;
281 struct wl_client *client;
282
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300283 weston_log("launching '%s'\n", path);
284
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300285 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200286 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200287 "socketpair failed while launching '%s': %m\n",
288 path);
289 return NULL;
290 }
291
292 pid = fork();
293 if (pid == -1) {
294 close(sv[0]);
295 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200296 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297 "fork failed while launching '%s': %m\n", path);
298 return NULL;
299 }
300
301 if (pid == 0) {
302 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700303 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200304 }
305
306 close(sv[1]);
307
308 client = wl_client_create(compositor->wl_display, sv[0]);
309 if (!client) {
310 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200311 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200312 "wl_client_create failed while launching '%s'.\n",
313 path);
314 return NULL;
315 }
316
317 proc->pid = pid;
318 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500319 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200320
321 return client;
322}
323
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300324struct process_info {
325 struct weston_process proc;
326 char *path;
327};
328
329static void
330process_handle_sigchld(struct weston_process *process, int status)
331{
332 struct process_info *pinfo =
333 container_of(process, struct process_info, proc);
334
335 /*
336 * There are no guarantees whether this runs before or after
337 * the wl_client destructor.
338 */
339
340 if (WIFEXITED(status)) {
341 weston_log("%s exited with status %d\n", pinfo->path,
342 WEXITSTATUS(status));
343 } else if (WIFSIGNALED(status)) {
344 weston_log("%s died on signal %d\n", pinfo->path,
345 WTERMSIG(status));
346 } else {
347 weston_log("%s disappeared\n", pinfo->path);
348 }
349
350 free(pinfo->path);
351 free(pinfo);
352}
353
354WL_EXPORT struct wl_client *
355weston_client_start(struct weston_compositor *compositor, const char *path)
356{
357 struct process_info *pinfo;
358 struct wl_client *client;
359
360 pinfo = zalloc(sizeof *pinfo);
361 if (!pinfo)
362 return NULL;
363
364 pinfo->path = strdup(path);
365 if (!pinfo->path)
366 goto out_free;
367
368 client = weston_client_launch(compositor, &pinfo->proc, path,
369 process_handle_sigchld);
370 if (!client)
371 goto out_str;
372
373 return client;
374
375out_str:
376 free(pinfo->path);
377
378out_free:
379 free(pinfo);
380
381 return NULL;
382}
383
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200384static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300385region_init_infinite(pixman_region32_t *region)
386{
387 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
388 UINT32_MAX, UINT32_MAX);
389}
390
Pekka Paalanene67858b2013-04-25 13:57:42 +0300391static struct weston_subsurface *
392weston_surface_to_subsurface(struct weston_surface *surface);
393
Jason Ekstranda7af7042013-10-12 22:38:11 -0500394WL_EXPORT struct weston_view *
395weston_view_create(struct weston_surface *surface)
396{
397 struct weston_view *view;
398
399 view = calloc(1, sizeof *view);
400 if (view == NULL)
401 return NULL;
402
403 view->surface = surface;
404
Jason Ekstranda7af7042013-10-12 22:38:11 -0500405 /* Assign to surface */
406 wl_list_insert(&surface->views, &view->surface_link);
407
408 wl_signal_init(&view->destroy_signal);
409 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300410 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500411
Xiong Zhang97116532013-10-23 13:58:31 +0800412 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300413 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300414 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500415
416 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300417 pixman_region32_init(&view->transform.masked_boundingbox);
418 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500419
420 view->alpha = 1.0;
421 pixman_region32_init(&view->transform.opaque);
422
423 wl_list_init(&view->geometry.transformation_list);
424 wl_list_insert(&view->geometry.transformation_list,
425 &view->transform.position.link);
426 weston_matrix_init(&view->transform.position.matrix);
427 wl_list_init(&view->geometry.child_list);
428 pixman_region32_init(&view->transform.boundingbox);
429 view->transform.dirty = 1;
430
431 view->output = NULL;
432
433 return view;
434}
435
Jason Ekstrand108865d2014-06-26 10:04:49 -0700436struct weston_frame_callback {
437 struct wl_resource *resource;
438 struct wl_list link;
439};
440
Jason Ekstrand7b982072014-05-20 14:33:03 -0500441static void
442surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
443{
444 struct weston_surface_state *state =
445 container_of(listener, struct weston_surface_state,
446 buffer_destroy_listener);
447
448 state->buffer = NULL;
449}
450
451static void
452weston_surface_state_init(struct weston_surface_state *state)
453{
454 state->newly_attached = 0;
455 state->buffer = NULL;
456 state->buffer_destroy_listener.notify =
457 surface_state_handle_buffer_destroy;
458 state->sx = 0;
459 state->sy = 0;
460
461 pixman_region32_init(&state->damage);
462 pixman_region32_init(&state->opaque);
463 region_init_infinite(&state->input);
464
465 wl_list_init(&state->frame_callback_list);
466
467 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
468 state->buffer_viewport.buffer.scale = 1;
469 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
470 state->buffer_viewport.surface.width = -1;
471 state->buffer_viewport.changed = 0;
472}
473
474static void
475weston_surface_state_fini(struct weston_surface_state *state)
476{
477 struct weston_frame_callback *cb, *next;
478
479 wl_list_for_each_safe(cb, next,
480 &state->frame_callback_list, link)
481 wl_resource_destroy(cb->resource);
482
483 pixman_region32_fini(&state->input);
484 pixman_region32_fini(&state->opaque);
485 pixman_region32_fini(&state->damage);
486
487 if (state->buffer)
488 wl_list_remove(&state->buffer_destroy_listener.link);
489 state->buffer = NULL;
490}
491
492static void
493weston_surface_state_set_buffer(struct weston_surface_state *state,
494 struct weston_buffer *buffer)
495{
496 if (state->buffer == buffer)
497 return;
498
499 if (state->buffer)
500 wl_list_remove(&state->buffer_destroy_listener.link);
501 state->buffer = buffer;
502 if (state->buffer)
503 wl_signal_add(&state->buffer->destroy_signal,
504 &state->buffer_destroy_listener);
505}
506
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500507WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500508weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500509{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500510 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400511
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200512 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400513 if (surface == NULL)
514 return NULL;
515
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500516 wl_signal_init(&surface->destroy_signal);
517
518 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500519
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500520 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200521 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400522
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200523 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
524 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200525 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
526 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500527
528 weston_surface_state_init(&surface->pending);
529
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400530 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200531
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200532 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100533
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400534 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500535 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300536 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400537
Jason Ekstranda7af7042013-10-12 22:38:11 -0500538 wl_list_init(&surface->views);
539
540 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500541
Pekka Paalanene67858b2013-04-25 13:57:42 +0300542 wl_list_init(&surface->subsurface_list);
543 wl_list_init(&surface->subsurface_list_pending);
544
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400545 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500546}
547
Alex Wu8811bf92012-02-28 18:07:54 +0800548WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500549weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200550 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500551{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100552 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500553}
554
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400555WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500556weston_view_to_global_float(struct weston_view *view,
557 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200558{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500559 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200560 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
561
Jason Ekstranda7af7042013-10-12 22:38:11 -0500562 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200563
564 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200565 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700566 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200567 v.f[3]);
568 *x = 0;
569 *y = 0;
570 return;
571 }
572
573 *x = v.f[0] / v.f[3];
574 *y = v.f[1] / v.f[3];
575 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500576 *x = sx + view->geometry.x;
577 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200578 }
579}
580
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500581WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200582weston_transformed_coord(int width, int height,
583 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200584 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200585 float sx, float sy, float *bx, float *by)
586{
587 switch (transform) {
588 case WL_OUTPUT_TRANSFORM_NORMAL:
589 default:
590 *bx = sx;
591 *by = sy;
592 break;
593 case WL_OUTPUT_TRANSFORM_FLIPPED:
594 *bx = width - sx;
595 *by = sy;
596 break;
597 case WL_OUTPUT_TRANSFORM_90:
598 *bx = height - sy;
599 *by = sx;
600 break;
601 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
602 *bx = height - sy;
603 *by = width - sx;
604 break;
605 case WL_OUTPUT_TRANSFORM_180:
606 *bx = width - sx;
607 *by = height - sy;
608 break;
609 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
610 *bx = sx;
611 *by = height - sy;
612 break;
613 case WL_OUTPUT_TRANSFORM_270:
614 *bx = sy;
615 *by = width - sx;
616 break;
617 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
618 *bx = sy;
619 *by = sx;
620 break;
621 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200622
623 *bx *= scale;
624 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200625}
626
627WL_EXPORT pixman_box32_t
628weston_transformed_rect(int width, int height,
629 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200630 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200631 pixman_box32_t rect)
632{
633 float x1, x2, y1, y2;
634
635 pixman_box32_t ret;
636
Alexander Larsson4ea95522013-05-22 14:41:37 +0200637 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200638 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200639 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200640 rect.x2, rect.y2, &x2, &y2);
641
642 if (x1 <= x2) {
643 ret.x1 = x1;
644 ret.x2 = x2;
645 } else {
646 ret.x1 = x2;
647 ret.x2 = x1;
648 }
649
650 if (y1 <= y2) {
651 ret.y1 = y1;
652 ret.y2 = y2;
653 } else {
654 ret.y1 = y2;
655 ret.y2 = y1;
656 }
657
658 return ret;
659}
660
661WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500662weston_transformed_region(int width, int height,
663 enum wl_output_transform transform,
664 int32_t scale,
665 pixman_region32_t *src, pixman_region32_t *dest)
666{
667 pixman_box32_t *src_rects, *dest_rects;
668 int nrects, i;
669
670 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
671 if (src != dest)
672 pixman_region32_copy(dest, src);
673 return;
674 }
675
676 src_rects = pixman_region32_rectangles(src, &nrects);
677 dest_rects = malloc(nrects * sizeof(*dest_rects));
678 if (!dest_rects)
679 return;
680
681 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
682 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
683 } else {
684 for (i = 0; i < nrects; i++) {
685 switch (transform) {
686 default:
687 case WL_OUTPUT_TRANSFORM_NORMAL:
688 dest_rects[i].x1 = src_rects[i].x1;
689 dest_rects[i].y1 = src_rects[i].y1;
690 dest_rects[i].x2 = src_rects[i].x2;
691 dest_rects[i].y2 = src_rects[i].y2;
692 break;
693 case WL_OUTPUT_TRANSFORM_90:
694 dest_rects[i].x1 = height - src_rects[i].y2;
695 dest_rects[i].y1 = src_rects[i].x1;
696 dest_rects[i].x2 = height - src_rects[i].y1;
697 dest_rects[i].y2 = src_rects[i].x2;
698 break;
699 case WL_OUTPUT_TRANSFORM_180:
700 dest_rects[i].x1 = width - src_rects[i].x2;
701 dest_rects[i].y1 = height - src_rects[i].y2;
702 dest_rects[i].x2 = width - src_rects[i].x1;
703 dest_rects[i].y2 = height - src_rects[i].y1;
704 break;
705 case WL_OUTPUT_TRANSFORM_270:
706 dest_rects[i].x1 = src_rects[i].y1;
707 dest_rects[i].y1 = width - src_rects[i].x2;
708 dest_rects[i].x2 = src_rects[i].y2;
709 dest_rects[i].y2 = width - src_rects[i].x1;
710 break;
711 case WL_OUTPUT_TRANSFORM_FLIPPED:
712 dest_rects[i].x1 = width - src_rects[i].x2;
713 dest_rects[i].y1 = src_rects[i].y1;
714 dest_rects[i].x2 = width - src_rects[i].x1;
715 dest_rects[i].y2 = src_rects[i].y2;
716 break;
717 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
718 dest_rects[i].x1 = height - src_rects[i].y2;
719 dest_rects[i].y1 = width - src_rects[i].x2;
720 dest_rects[i].x2 = height - src_rects[i].y1;
721 dest_rects[i].y2 = width - src_rects[i].x1;
722 break;
723 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
724 dest_rects[i].x1 = src_rects[i].x1;
725 dest_rects[i].y1 = height - src_rects[i].y2;
726 dest_rects[i].x2 = src_rects[i].x2;
727 dest_rects[i].y2 = height - src_rects[i].y1;
728 break;
729 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
730 dest_rects[i].x1 = src_rects[i].y1;
731 dest_rects[i].y1 = src_rects[i].x1;
732 dest_rects[i].x2 = src_rects[i].y2;
733 dest_rects[i].y2 = src_rects[i].x2;
734 break;
735 }
736 }
737 }
738
739 if (scale != 1) {
740 for (i = 0; i < nrects; i++) {
741 dest_rects[i].x1 *= scale;
742 dest_rects[i].x2 *= scale;
743 dest_rects[i].y1 *= scale;
744 dest_rects[i].y2 *= scale;
745 }
746 }
747
748 pixman_region32_clear(dest);
749 pixman_region32_init_rects(dest, dest_rects, nrects);
750 free(dest_rects);
751}
752
Jonny Lamb74130762013-11-26 18:19:46 +0100753static void
754scaler_surface_to_buffer(struct weston_surface *surface,
755 float sx, float sy, float *bx, float *by)
756{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200757 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200758 double src_width, src_height;
759 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200760
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200761 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
762 if (vp->surface.width == -1) {
763 *bx = sx;
764 *by = sy;
765 return;
766 }
Jonny Lamb74130762013-11-26 18:19:46 +0100767
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200768 src_x = 0.0;
769 src_y = 0.0;
770 src_width = surface->width_from_buffer;
771 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100772 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200773 src_x = wl_fixed_to_double(vp->buffer.src_x);
774 src_y = wl_fixed_to_double(vp->buffer.src_y);
775 src_width = wl_fixed_to_double(vp->buffer.src_width);
776 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100777 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200778
779 *bx = sx * src_width / surface->width + src_x;
780 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100781}
782
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500783WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200784weston_surface_to_buffer_float(struct weston_surface *surface,
785 float sx, float sy, float *bx, float *by)
786{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200787 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
788
Jonny Lamb74130762013-11-26 18:19:46 +0100789 /* first transform coordinates if the scaler is set */
790 scaler_surface_to_buffer(surface, sx, sy, bx, by);
791
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500792 weston_transformed_coord(surface->width_from_buffer,
793 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200794 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100795 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200796}
797
Alexander Larsson4ea95522013-05-22 14:41:37 +0200798WL_EXPORT void
799weston_surface_to_buffer(struct weston_surface *surface,
800 int sx, int sy, int *bx, int *by)
801{
802 float bxf, byf;
803
Jonny Lamb74130762013-11-26 18:19:46 +0100804 weston_surface_to_buffer_float(surface,
805 sx, sy, &bxf, &byf);
806
Alexander Larsson4ea95522013-05-22 14:41:37 +0200807 *bx = floorf(bxf);
808 *by = floorf(byf);
809}
810
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200811WL_EXPORT pixman_box32_t
812weston_surface_to_buffer_rect(struct weston_surface *surface,
813 pixman_box32_t rect)
814{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200815 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100816 float xf, yf;
817
818 /* first transform box coordinates if the scaler is set */
819 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
820 rect.x1 = floorf(xf);
821 rect.y1 = floorf(yf);
822
823 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
824 rect.x2 = floorf(xf);
825 rect.y2 = floorf(yf);
826
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500827 return weston_transformed_rect(surface->width_from_buffer,
828 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200829 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200830 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200831}
832
833WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500834weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400835 struct weston_plane *plane)
836{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500837 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400838 return;
839
Jason Ekstranda7af7042013-10-12 22:38:11 -0500840 weston_view_damage_below(view);
841 view->plane = plane;
842 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400843}
844
845WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500846weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200847{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500848 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200849
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500850 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300851 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500852 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800853 if (view->plane)
854 pixman_region32_union(&view->plane->damage,
855 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500856 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800857 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200858}
859
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400860static void
861weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
862{
863 uint32_t different = es->output_mask ^ mask;
864 uint32_t entered = mask & different;
865 uint32_t left = es->output_mask & different;
866 struct weston_output *output;
867 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500868 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400869
870 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500871 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400872 return;
873 if (different == 0)
874 return;
875
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500876 client = wl_resource_get_client(es->resource);
877
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400878 wl_list_for_each(output, &es->compositor->output_list, link) {
879 if (1 << output->id & different)
880 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500881 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400882 client);
883 if (resource == NULL)
884 continue;
885 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500886 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400887 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500888 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400889 }
890}
891
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200892
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400893static void
894weston_surface_assign_output(struct weston_surface *es)
895{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500896 struct weston_output *new_output;
897 struct weston_view *view;
898 pixman_region32_t region;
899 uint32_t max, area, mask;
900 pixman_box32_t *e;
901
902 new_output = NULL;
903 max = 0;
904 mask = 0;
905 pixman_region32_init(&region);
906 wl_list_for_each(view, &es->views, surface_link) {
907 if (!view->output)
908 continue;
909
910 pixman_region32_intersect(&region, &view->transform.boundingbox,
911 &view->output->region);
912
913 e = pixman_region32_extents(&region);
914 area = (e->x2 - e->x1) * (e->y2 - e->y1);
915
916 mask |= view->output_mask;
917
918 if (area >= max) {
919 new_output = view->output;
920 max = area;
921 }
922 }
923 pixman_region32_fini(&region);
924
925 es->output = new_output;
926 weston_surface_update_output_mask(es, mask);
927}
928
929static void
930weston_view_assign_output(struct weston_view *ev)
931{
932 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400933 struct weston_output *output, *new_output;
934 pixman_region32_t region;
935 uint32_t max, area, mask;
936 pixman_box32_t *e;
937
938 new_output = NULL;
939 max = 0;
940 mask = 0;
941 pixman_region32_init(&region);
942 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400944 &output->region);
945
946 e = pixman_region32_extents(&region);
947 area = (e->x2 - e->x1) * (e->y2 - e->y1);
948
949 if (area > 0)
950 mask |= 1 << output->id;
951
952 if (area >= max) {
953 new_output = output;
954 max = area;
955 }
956 }
957 pixman_region32_fini(&region);
958
Jason Ekstranda7af7042013-10-12 22:38:11 -0500959 ev->output = new_output;
960 ev->output_mask = mask;
961
962 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400963}
964
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200965static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500966view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
967 int32_t width, int32_t height,
968 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200969{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200970 float min_x = HUGE_VALF, min_y = HUGE_VALF;
971 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200972 int32_t s[4][2] = {
973 { sx, sy },
974 { sx, sy + height },
975 { sx + width, sy },
976 { sx + width, sy + height }
977 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200978 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200979 int i;
980
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300981 if (width == 0 || height == 0) {
982 /* avoid rounding empty bbox to 1x1 */
983 pixman_region32_init(bbox);
984 return;
985 }
986
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200987 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200988 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500989 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200990 if (x < min_x)
991 min_x = x;
992 if (x > max_x)
993 max_x = x;
994 if (y < min_y)
995 min_y = y;
996 if (y > max_y)
997 max_y = y;
998 }
999
Pekka Paalanen219b9822012-02-08 15:38:37 +02001000 int_x = floorf(min_x);
1001 int_y = floorf(min_y);
1002 pixman_region32_init_rect(bbox, int_x, int_y,
1003 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001004}
1005
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001006static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001007weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001008{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001009 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001010
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001011 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001012 view->geometry.x = roundf(view->geometry.x);
1013 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001014
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001015 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001016 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1017 view->transform.position.matrix.d[12] = view->geometry.x;
1018 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001019
Jason Ekstranda7af7042013-10-12 22:38:11 -05001020 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001021
Jason Ekstranda7af7042013-10-12 22:38:11 -05001022 view->transform.inverse = view->transform.position.matrix;
1023 view->transform.inverse.d[12] = -view->geometry.x;
1024 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001025
Jason Ekstranda7af7042013-10-12 22:38:11 -05001026 pixman_region32_init_rect(&view->transform.boundingbox,
1027 view->geometry.x,
1028 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001029 view->surface->width,
1030 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001031
Jason Ekstranda7af7042013-10-12 22:38:11 -05001032 if (view->alpha == 1.0) {
1033 pixman_region32_copy(&view->transform.opaque,
1034 &view->surface->opaque);
1035 pixman_region32_translate(&view->transform.opaque,
1036 view->geometry.x,
1037 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001038 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001039}
1040
1041static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001042weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001043{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001044 struct weston_view *parent = view->geometry.parent;
1045 struct weston_matrix *matrix = &view->transform.matrix;
1046 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001047 struct weston_transform *tform;
1048
Jason Ekstranda7af7042013-10-12 22:38:11 -05001049 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001050
1051 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001052 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1053 view->transform.position.matrix.d[12] = view->geometry.x;
1054 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001055
1056 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001058 weston_matrix_multiply(matrix, &tform->matrix);
1059
Pekka Paalanen483243f2013-03-08 14:56:50 +02001060 if (parent)
1061 weston_matrix_multiply(matrix, &parent->transform.matrix);
1062
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001063 if (weston_matrix_invert(inverse, matrix) < 0) {
1064 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001065 weston_log("error: weston_view %p"
1066 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001067 return -1;
1068 }
1069
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001070 view_compute_bbox(view, 0, 0,
1071 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001072 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001073
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001074 return 0;
1075}
1076
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001077static struct weston_layer *
1078get_view_layer(struct weston_view *view)
1079{
1080 if (view->parent_view)
1081 return get_view_layer(view->parent_view);
1082 return view->layer_link.layer;
1083}
1084
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001085WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001086weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001087{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001088 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001089 struct weston_layer *layer;
1090 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001091
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001093 return;
1094
Pekka Paalanen483243f2013-03-08 14:56:50 +02001095 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001096 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001097
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001099
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001101
Jason Ekstranda7af7042013-10-12 22:38:11 -05001102 pixman_region32_fini(&view->transform.boundingbox);
1103 pixman_region32_fini(&view->transform.opaque);
1104 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001105
Pekka Paalanencd403622012-01-25 13:37:39 +02001106 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 if (view->geometry.transformation_list.next ==
1108 &view->transform.position.link &&
1109 view->geometry.transformation_list.prev ==
1110 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001111 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001113 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001114 if (weston_view_update_transform_enable(view) < 0)
1115 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001116 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001117
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001118 layer = get_view_layer(view);
1119 if (layer) {
1120 pixman_region32_init_with_extents(&mask, &layer->mask);
1121 pixman_region32_intersect(&view->transform.masked_boundingbox,
1122 &view->transform.boundingbox, &mask);
1123 pixman_region32_intersect(&view->transform.masked_opaque,
1124 &view->transform.opaque, &mask);
1125 pixman_region32_fini(&mask);
1126 }
1127
Jason Ekstranda7af7042013-10-12 22:38:11 -05001128 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001129
Jason Ekstranda7af7042013-10-12 22:38:11 -05001130 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001131
Jason Ekstranda7af7042013-10-12 22:38:11 -05001132 wl_signal_emit(&view->surface->compositor->transform_signal,
1133 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001134}
1135
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001136WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001137weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001138{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001139 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001140
1141 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001142 * The invariant: if view->geometry.dirty, then all views
1143 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001144 * Corollary: if not parent->geometry.dirty, then all ancestors
1145 * are not dirty.
1146 */
1147
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001149 return;
1150
Jason Ekstranda7af7042013-10-12 22:38:11 -05001151 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001152
Jason Ekstranda7af7042013-10-12 22:38:11 -05001153 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001154 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001156}
1157
1158WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001159weston_view_to_global_fixed(struct weston_view *view,
1160 wl_fixed_t vx, wl_fixed_t vy,
1161 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001162{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001163 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001164
Jason Ekstranda7af7042013-10-12 22:38:11 -05001165 weston_view_to_global_float(view,
1166 wl_fixed_to_double(vx),
1167 wl_fixed_to_double(vy),
1168 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001169 *x = wl_fixed_from_double(xf);
1170 *y = wl_fixed_from_double(yf);
1171}
1172
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001173WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001174weston_view_from_global_float(struct weston_view *view,
1175 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001176{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001178 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1179
Jason Ekstranda7af7042013-10-12 22:38:11 -05001180 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001181
1182 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001183 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001184 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001185 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001186 *vx = 0;
1187 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001188 return;
1189 }
1190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 *vx = v.f[0] / v.f[3];
1192 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001193 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001194 *vx = x - view->geometry.x;
1195 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001196 }
1197}
1198
1199WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001200weston_view_from_global_fixed(struct weston_view *view,
1201 wl_fixed_t x, wl_fixed_t y,
1202 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001203{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001204 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001205
Jason Ekstranda7af7042013-10-12 22:38:11 -05001206 weston_view_from_global_float(view,
1207 wl_fixed_to_double(x),
1208 wl_fixed_to_double(y),
1209 &vxf, &vyf);
1210 *vx = wl_fixed_from_double(vxf);
1211 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001212}
1213
1214WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215weston_view_from_global(struct weston_view *view,
1216 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001217{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001218 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001219
Jason Ekstranda7af7042013-10-12 22:38:11 -05001220 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1221 *vx = floorf(vxf);
1222 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001223}
1224
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001225WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001226weston_surface_schedule_repaint(struct weston_surface *surface)
1227{
1228 struct weston_output *output;
1229
1230 wl_list_for_each(output, &surface->compositor->output_list, link)
1231 if (surface->output_mask & (1 << output->id))
1232 weston_output_schedule_repaint(output);
1233}
1234
1235WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001236weston_view_schedule_repaint(struct weston_view *view)
1237{
1238 struct weston_output *output;
1239
1240 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1241 if (view->output_mask & (1 << output->id))
1242 weston_output_schedule_repaint(output);
1243}
1244
1245WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001246weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001247{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001248 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001249 0, 0, surface->width,
1250 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001251
Kristian Høgsberg98238702012-08-03 16:29:12 -04001252 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001253}
1254
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001255WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001257{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001258 if (view->geometry.x == x && view->geometry.y == y)
1259 return;
1260
Jason Ekstranda7af7042013-10-12 22:38:11 -05001261 view->geometry.x = x;
1262 view->geometry.y = y;
1263 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001264}
1265
Pekka Paalanen483243f2013-03-08 14:56:50 +02001266static void
1267transform_parent_handle_parent_destroy(struct wl_listener *listener,
1268 void *data)
1269{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001270 struct weston_view *view =
1271 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001272 geometry.parent_destroy_listener);
1273
Jason Ekstranda7af7042013-10-12 22:38:11 -05001274 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001275}
1276
1277WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001278weston_view_set_transform_parent(struct weston_view *view,
1279 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001280{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001281 if (view->geometry.parent) {
1282 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1283 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001284 }
1285
Jason Ekstranda7af7042013-10-12 22:38:11 -05001286 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001287
Jason Ekstranda7af7042013-10-12 22:38:11 -05001288 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001289 transform_parent_handle_parent_destroy;
1290 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001291 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001292 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001293 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001295 }
1296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 weston_view_geometry_dirty(view);
1298}
1299
1300WL_EXPORT int
1301weston_view_is_mapped(struct weston_view *view)
1302{
1303 if (view->output)
1304 return 1;
1305 else
1306 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001307}
1308
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001309WL_EXPORT int
1310weston_surface_is_mapped(struct weston_surface *surface)
1311{
1312 if (surface->output)
1313 return 1;
1314 else
1315 return 0;
1316}
1317
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001318static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001319surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001320{
1321 struct weston_view *view;
1322
1323 if (surface->width == width && surface->height == height)
1324 return;
1325
1326 surface->width = width;
1327 surface->height = height;
1328
1329 wl_list_for_each(view, &surface->views, surface_link)
1330 weston_view_geometry_dirty(view);
1331}
1332
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001333WL_EXPORT void
1334weston_surface_set_size(struct weston_surface *surface,
1335 int32_t width, int32_t height)
1336{
1337 assert(!surface->resource);
1338 surface_set_size(surface, width, height);
1339}
1340
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001341static int
1342fixed_round_up_to_int(wl_fixed_t f)
1343{
1344 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1345}
1346
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001347static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001348weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001349{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001350 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001351 int32_t width, height;
1352
1353 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001354 surface->width_from_buffer = 0;
1355 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001356 return;
1357 }
1358
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001359 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001360 case WL_OUTPUT_TRANSFORM_90:
1361 case WL_OUTPUT_TRANSFORM_270:
1362 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1363 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001364 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1365 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001366 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001367 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001368 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1369 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001370 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001371 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001372
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001373 surface->width_from_buffer = width;
1374 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001375}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001376
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001377static void
1378weston_surface_update_size(struct weston_surface *surface)
1379{
1380 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1381 int32_t width, height;
1382
1383 width = surface->width_from_buffer;
1384 height = surface->height_from_buffer;
1385
1386 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001387 surface_set_size(surface,
1388 vp->surface.width, vp->surface.height);
1389 return;
1390 }
1391
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001392 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001393 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1394 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1395
1396 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001397 return;
1398 }
1399
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001400 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001401}
1402
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001403WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001405{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001406 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001407
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001408 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001409
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001410 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001411}
1412
Jason Ekstranda7af7042013-10-12 22:38:11 -05001413WL_EXPORT struct weston_view *
1414weston_compositor_pick_view(struct weston_compositor *compositor,
1415 wl_fixed_t x, wl_fixed_t y,
1416 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001417{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001418 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001419 int ix = wl_fixed_to_int(x);
1420 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001421
Jason Ekstranda7af7042013-10-12 22:38:11 -05001422 wl_list_for_each(view, &compositor->view_list, link) {
1423 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001424 if (pixman_region32_contains_point(
1425 &view->transform.masked_boundingbox,
1426 ix, iy, NULL) &&
1427 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001428 wl_fixed_to_int(*vx),
1429 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001430 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001431 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001432 }
1433
1434 return NULL;
1435}
1436
1437static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001439{
Daniel Stone37816df2012-05-16 18:45:18 +01001440 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001441
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001442 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001443 return;
1444
Daniel Stone37816df2012-05-16 18:45:18 +01001445 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001446 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001447}
1448
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001449WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001450weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001451{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001452 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001453
Jason Ekstranda7af7042013-10-12 22:38:11 -05001454 if (!weston_view_is_mapped(view))
1455 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001456
Jason Ekstranda7af7042013-10-12 22:38:11 -05001457 weston_view_damage_below(view);
1458 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001459 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001460 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001461 wl_list_remove(&view->link);
1462 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001463 view->output_mask = 0;
1464 weston_surface_assign_output(view->surface);
1465
1466 if (weston_surface_is_mapped(view->surface))
1467 return;
1468
1469 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1470 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001471 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001472 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001473 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001474 NULL,
1475 wl_fixed_from_int(0),
1476 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001477 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001478 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001479 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001480}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001481
Jason Ekstranda7af7042013-10-12 22:38:11 -05001482WL_EXPORT void
1483weston_surface_unmap(struct weston_surface *surface)
1484{
1485 struct weston_view *view;
1486
1487 wl_list_for_each(view, &surface->views, surface_link)
1488 weston_view_unmap(view);
1489 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001490}
1491
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001492static void
1493weston_surface_reset_pending_buffer(struct weston_surface *surface)
1494{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001495 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001496 surface->pending.sx = 0;
1497 surface->pending.sy = 0;
1498 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001499 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001500}
1501
Jason Ekstranda7af7042013-10-12 22:38:11 -05001502WL_EXPORT void
1503weston_view_destroy(struct weston_view *view)
1504{
1505 wl_signal_emit(&view->destroy_signal, view);
1506
1507 assert(wl_list_empty(&view->geometry.child_list));
1508
1509 if (weston_view_is_mapped(view)) {
1510 weston_view_unmap(view);
1511 weston_compositor_build_view_list(view->surface->compositor);
1512 }
1513
1514 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001515 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516
1517 pixman_region32_fini(&view->clip);
1518 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001519 pixman_region32_fini(&view->transform.masked_boundingbox);
1520 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521
1522 weston_view_set_transform_parent(view, NULL);
1523
Jason Ekstranda7af7042013-10-12 22:38:11 -05001524 wl_list_remove(&view->surface_link);
1525
1526 free(view);
1527}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001528
1529WL_EXPORT void
1530weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001531{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001532 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001533 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001534
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001535 if (--surface->ref_count > 0)
1536 return;
1537
1538 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1539
Pekka Paalanene67858b2013-04-25 13:57:42 +03001540 assert(wl_list_empty(&surface->subsurface_list_pending));
1541 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001542
Jason Ekstranda7af7042013-10-12 22:38:11 -05001543 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1544 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001545
Jason Ekstrand7b982072014-05-20 14:33:03 -05001546 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001547
Pekka Paalanende685b82012-12-04 15:58:12 +02001548 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001549
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001550 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001551 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001552 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001553
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001554 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001555 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001556
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001557 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001558}
1559
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001560static void
1561destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001562{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001563 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001564
Giulio Camuffo0d379742013-11-15 22:06:15 +01001565 /* Set the resource to NULL, since we don't want to leave a
1566 * dangling pointer if the surface was refcounted and survives
1567 * the weston_surface_destroy() call. */
1568 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001569 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001570}
1571
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001572static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001573weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1574{
1575 struct weston_buffer *buffer =
1576 container_of(listener, struct weston_buffer, destroy_listener);
1577
1578 wl_signal_emit(&buffer->destroy_signal, buffer);
1579 free(buffer);
1580}
1581
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001582WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001583weston_buffer_from_resource(struct wl_resource *resource)
1584{
1585 struct weston_buffer *buffer;
1586 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001587
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001588 listener = wl_resource_get_destroy_listener(resource,
1589 weston_buffer_destroy_handler);
1590
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001591 if (listener)
1592 return container_of(listener, struct weston_buffer,
1593 destroy_listener);
1594
1595 buffer = zalloc(sizeof *buffer);
1596 if (buffer == NULL)
1597 return NULL;
1598
1599 buffer->resource = resource;
1600 wl_signal_init(&buffer->destroy_signal);
1601 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001602 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001603 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001604
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001605 return buffer;
1606}
1607
1608static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001609weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1610 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001611{
Pekka Paalanende685b82012-12-04 15:58:12 +02001612 struct weston_buffer_reference *ref =
1613 container_of(listener, struct weston_buffer_reference,
1614 destroy_listener);
1615
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001616 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001617 ref->buffer = NULL;
1618}
1619
1620WL_EXPORT void
1621weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001622 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001623{
1624 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001625 ref->buffer->busy_count--;
1626 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001627 assert(wl_resource_get_client(ref->buffer->resource));
1628 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001629 WL_BUFFER_RELEASE);
1630 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001631 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001632 }
1633
Pekka Paalanende685b82012-12-04 15:58:12 +02001634 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001635 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001636 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001637 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001638 }
1639
Pekka Paalanende685b82012-12-04 15:58:12 +02001640 ref->buffer = buffer;
1641 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1642}
1643
1644static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001645weston_surface_attach(struct weston_surface *surface,
1646 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001647{
1648 weston_buffer_reference(&surface->buffer_ref, buffer);
1649
Pekka Paalanena6421c42012-12-04 15:58:10 +02001650 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001651 if (weston_surface_is_mapped(surface))
1652 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001653 }
1654
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001655 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001656
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001657 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001658}
1659
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001660WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001662{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001663 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001664
1665 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001666 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001667}
1668
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001669WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001670weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001671{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001672 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001673
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001674 pixman_region32_union(&compositor->primary_plane.damage,
1675 &compositor->primary_plane.damage,
1676 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001677 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001678}
1679
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001680static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001681surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001682{
Pekka Paalanende685b82012-12-04 15:58:12 +02001683 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001684 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001685 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001686
Jason Ekstrandef540082014-06-26 10:37:36 -07001687 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001688}
1689
1690static void
1691view_accumulate_damage(struct weston_view *view,
1692 pixman_region32_t *opaque)
1693{
1694 pixman_region32_t damage;
1695
1696 pixman_region32_init(&damage);
1697 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001698 pixman_box32_t *extents;
1699
Jason Ekstranda7af7042013-10-12 22:38:11 -05001700 extents = pixman_region32_extents(&view->surface->damage);
1701 view_compute_bbox(view, extents->x1, extents->y1,
1702 extents->x2 - extents->x1,
1703 extents->y2 - extents->y1,
1704 &damage);
1705 pixman_region32_translate(&damage,
1706 -view->plane->x,
1707 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001708 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001709 pixman_region32_copy(&damage, &view->surface->damage);
1710 pixman_region32_translate(&damage,
1711 view->geometry.x - view->plane->x,
1712 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001713 }
1714
Jason Ekstranda7af7042013-10-12 22:38:11 -05001715 pixman_region32_subtract(&damage, &damage, opaque);
1716 pixman_region32_union(&view->plane->damage,
1717 &view->plane->damage, &damage);
1718 pixman_region32_fini(&damage);
1719 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001720 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001721}
1722
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001723static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001724compositor_accumulate_damage(struct weston_compositor *ec)
1725{
1726 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001727 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001728 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001729
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001730 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001731
1732 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001733 pixman_region32_copy(&plane->clip, &clip);
1734
1735 pixman_region32_init(&opaque);
1736
Jason Ekstranda7af7042013-10-12 22:38:11 -05001737 wl_list_for_each(ev, &ec->view_list, link) {
1738 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001739 continue;
1740
Jason Ekstranda7af7042013-10-12 22:38:11 -05001741 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001742 }
1743
1744 pixman_region32_union(&clip, &clip, &opaque);
1745 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001746 }
1747
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001748 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001749
Jason Ekstranda7af7042013-10-12 22:38:11 -05001750 wl_list_for_each(ev, &ec->view_list, link)
1751 ev->surface->touched = 0;
1752
1753 wl_list_for_each(ev, &ec->view_list, link) {
1754 if (ev->surface->touched)
1755 continue;
1756 ev->surface->touched = 1;
1757
1758 surface_flush_damage(ev->surface);
1759
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001760 /* Both the renderer and the backend have seen the buffer
1761 * by now. If renderer needs the buffer, it has its own
1762 * reference set. If the backend wants to keep the buffer
1763 * around for migrating the surface into a non-primary plane
1764 * later, keep_buffer is true. Otherwise, drop the core
1765 * reference now, and allow early buffer release. This enables
1766 * clients to use single-buffering.
1767 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001768 if (!ev->surface->keep_buffer)
1769 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001770 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001771}
1772
1773static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001774surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001775{
1776 struct weston_subsurface *sub;
1777
Pekka Paalanene67858b2013-04-25 13:57:42 +03001778 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001779 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001780 continue;
1781
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1783 wl_list_init(&sub->surface->views);
1784
1785 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001786 }
1787}
1788
1789static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001790surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001791{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001792 struct weston_subsurface *sub;
1793 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001794
Jason Ekstranda7af7042013-10-12 22:38:11 -05001795 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1796 if (sub->surface == surface)
1797 continue;
1798
George Kiagiadakised04d382014-06-13 18:10:26 +02001799 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1800 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001801 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001802 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803
1804 surface_free_unused_subsurface_views(sub->surface);
1805 }
1806}
1807
1808static void
1809view_list_add_subsurface_view(struct weston_compositor *compositor,
1810 struct weston_subsurface *sub,
1811 struct weston_view *parent)
1812{
1813 struct weston_subsurface *child;
1814 struct weston_view *view = NULL, *iv;
1815
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001816 if (!weston_surface_is_mapped(sub->surface))
1817 return;
1818
Jason Ekstranda7af7042013-10-12 22:38:11 -05001819 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1820 if (iv->geometry.parent == parent) {
1821 view = iv;
1822 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001823 }
1824 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001825
1826 if (view) {
1827 /* Put it back in the surface's list of views */
1828 wl_list_remove(&view->surface_link);
1829 wl_list_insert(&sub->surface->views, &view->surface_link);
1830 } else {
1831 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001832 weston_view_set_position(view,
1833 sub->position.x,
1834 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 weston_view_set_transform_parent(view, parent);
1836 }
1837
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001838 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001839 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840
Pekka Paalanenb188e912013-11-19 14:03:35 +02001841 if (wl_list_empty(&sub->surface->subsurface_list)) {
1842 wl_list_insert(compositor->view_list.prev, &view->link);
1843 return;
1844 }
1845
1846 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1847 if (child->surface == sub->surface)
1848 wl_list_insert(compositor->view_list.prev, &view->link);
1849 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001850 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001851 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001852}
1853
1854static void
1855view_list_add(struct weston_compositor *compositor,
1856 struct weston_view *view)
1857{
1858 struct weston_subsurface *sub;
1859
1860 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001861
Pekka Paalanenb188e912013-11-19 14:03:35 +02001862 if (wl_list_empty(&view->surface->subsurface_list)) {
1863 wl_list_insert(compositor->view_list.prev, &view->link);
1864 return;
1865 }
1866
1867 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1868 if (sub->surface == view->surface)
1869 wl_list_insert(compositor->view_list.prev, &view->link);
1870 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001871 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001872 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873}
1874
1875static void
1876weston_compositor_build_view_list(struct weston_compositor *compositor)
1877{
1878 struct weston_view *view;
1879 struct weston_layer *layer;
1880
1881 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001882 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001883 surface_stash_subsurface_views(view->surface);
1884
1885 wl_list_init(&compositor->view_list);
1886 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001887 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 view_list_add(compositor, view);
1889 }
1890 }
1891
1892 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001893 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001894 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001895}
1896
David Herrmann1edf44c2013-10-22 17:11:26 +02001897static int
Rob Bradford0fb79752012-08-02 15:36:57 +01001898weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001899{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001900 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001902 struct weston_animation *animation, *next;
1903 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001904 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001905 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001906 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001907
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001908 if (output->destroying)
1909 return 0;
1910
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001911 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001912 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001913
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001914 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001915 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001916 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001917 wl_list_for_each(ev, &ec->view_list, link)
1918 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001919
Pekka Paalanene67858b2013-04-25 13:57:42 +03001920 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001921 wl_list_for_each(ev, &ec->view_list, link) {
1922 /* Note: This operation is safe to do multiple times on the
1923 * same surface.
1924 */
1925 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001926 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001927 &ev->surface->frame_callback_list);
1928 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001929 }
1930 }
1931
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001932 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001933
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001934 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001935 pixman_region32_intersect(&output_damage,
1936 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001937 pixman_region32_subtract(&output_damage,
1938 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001939
Scott Moreauccbf29d2012-02-22 14:21:41 -07001940 if (output->dirty)
1941 weston_output_update_matrix(output);
1942
David Herrmann1edf44c2013-10-22 17:11:26 +02001943 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001944
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001945 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001946
Kristian Høgsbergef044142011-06-21 15:02:12 -04001947 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001948
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001949 weston_compositor_repick(ec);
1950 wl_event_loop_dispatch(ec->input_loop, 0);
1951
Jonas Ådahldb773762012-06-13 00:01:21 +02001952 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001953 wl_callback_send_done(cb->resource, msecs);
1954 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001955 }
1956
Scott Moreaud64cf212012-06-08 19:40:54 -06001957 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001958 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001959 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001960 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001961
1962 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001963}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001964
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001965static int
1966weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001967{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001968 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001969
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001970 wl_event_loop_dispatch(compositor->input_loop, 0);
1971
1972 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001973}
1974
1975WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001976weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001977{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001978 struct weston_compositor *compositor = output->compositor;
1979 struct wl_event_loop *loop =
1980 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001981 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001982
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001983 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001984
1985 if (output->repaint_needed &&
1986 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1987 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001988 r = weston_output_repaint(output, msecs);
1989 if (!r)
1990 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001991 }
1992
1993 output->repaint_scheduled = 0;
1994 if (compositor->input_loop_source)
1995 return;
1996
1997 fd = wl_event_loop_get_fd(compositor->input_loop);
1998 compositor->input_loop_source =
1999 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2000 weston_compositor_read_input, compositor);
2001}
2002
2003static void
2004idle_repaint(void *data)
2005{
2006 struct weston_output *output = data;
2007
Jonas Ådahle5a12252013-04-05 23:07:11 +02002008 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002009}
2010
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002011WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002012weston_layer_entry_insert(struct weston_layer_entry *list,
2013 struct weston_layer_entry *entry)
2014{
2015 wl_list_insert(&list->link, &entry->link);
2016 entry->layer = list->layer;
2017}
2018
2019WL_EXPORT void
2020weston_layer_entry_remove(struct weston_layer_entry *entry)
2021{
2022 wl_list_remove(&entry->link);
2023 wl_list_init(&entry->link);
2024 entry->layer = NULL;
2025}
2026
2027WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002028weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2029{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002030 wl_list_init(&layer->view_list.link);
2031 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002032 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002033 if (below != NULL)
2034 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002035}
2036
2037WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002038weston_layer_set_mask(struct weston_layer *layer,
2039 int x, int y, int width, int height)
2040{
2041 struct weston_view *view;
2042
2043 layer->mask.x1 = x;
2044 layer->mask.x2 = x + width;
2045 layer->mask.y1 = y;
2046 layer->mask.y2 = y + height;
2047
2048 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2049 weston_view_geometry_dirty(view);
2050 }
2051}
2052
2053WL_EXPORT void
2054weston_layer_set_mask_infinite(struct weston_layer *layer)
2055{
2056 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2057 UINT32_MAX, UINT32_MAX);
2058}
2059
2060WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002061weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002062{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002063 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002064 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002065
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002066 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2067 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002068 return;
2069
Kristian Høgsbergef044142011-06-21 15:02:12 -04002070 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002071 output->repaint_needed = 1;
2072 if (output->repaint_scheduled)
2073 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002074
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002075 wl_event_loop_add_idle(loop, idle_repaint, output);
2076 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002077
2078 if (compositor->input_loop_source) {
2079 wl_event_source_remove(compositor->input_loop_source);
2080 compositor->input_loop_source = NULL;
2081 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002082}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002083
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002084WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002085weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2086{
2087 struct weston_output *output;
2088
2089 wl_list_for_each(output, &compositor->output_list, link)
2090 weston_output_schedule_repaint(output);
2091}
2092
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002093static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002094surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002095{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002096 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002097}
2098
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002099static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002100surface_attach(struct wl_client *client,
2101 struct wl_resource *resource,
2102 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2103{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002104 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002105 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002106
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002107 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002108 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002109 if (buffer == NULL) {
2110 wl_client_post_no_memory(client);
2111 return;
2112 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002113 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002114
Pekka Paalanende685b82012-12-04 15:58:12 +02002115 /* Attach, attach, without commit in between does not send
2116 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002117 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002118
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002119 surface->pending.sx = sx;
2120 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002121 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002122}
2123
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002124static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002125surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002126 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002127 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002128{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002129 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002130
Pekka Paalanen8e159182012-10-10 12:49:25 +03002131 pixman_region32_union_rect(&surface->pending.damage,
2132 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002133 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002134}
2135
Kristian Høgsberg33418202011-08-16 23:01:28 -04002136static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002137destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002138{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002139 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002140
2141 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002142 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002143}
2144
2145static void
2146surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002147 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002148{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002149 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002150 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002151
2152 cb = malloc(sizeof *cb);
2153 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002154 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002155 return;
2156 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002157
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002158 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2159 callback);
2160 if (cb->resource == NULL) {
2161 free(cb);
2162 wl_resource_post_no_memory(resource);
2163 return;
2164 }
2165
Jason Ekstranda85118c2013-06-27 20:17:02 -05002166 wl_resource_set_implementation(cb->resource, NULL, cb,
2167 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002168
Pekka Paalanenbc106382012-10-10 12:49:31 +03002169 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002170}
2171
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002172static void
2173surface_set_opaque_region(struct wl_client *client,
2174 struct wl_resource *resource,
2175 struct wl_resource *region_resource)
2176{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002177 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002178 struct weston_region *region;
2179
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002180 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002181 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002182 pixman_region32_copy(&surface->pending.opaque,
2183 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002184 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002185 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002186 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002187}
2188
2189static void
2190surface_set_input_region(struct wl_client *client,
2191 struct wl_resource *resource,
2192 struct wl_resource *region_resource)
2193{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002194 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002195 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002196
2197 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002198 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002199 pixman_region32_copy(&surface->pending.input,
2200 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002201 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002202 pixman_region32_fini(&surface->pending.input);
2203 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002204 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002205}
2206
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002207static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002208weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002209{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210 struct weston_subsurface *sub;
2211
2212 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2213 parent_link_pending) {
2214 wl_list_remove(&sub->parent_link);
2215 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2216 }
2217}
2218
2219static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002220weston_surface_commit_state(struct weston_surface *surface,
2221 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002222{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002223 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002224 pixman_region32_t opaque;
2225
Alexander Larsson4ea95522013-05-22 14:41:37 +02002226 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002227 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002228 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002229 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002230
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002231 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002232 if (state->newly_attached)
2233 weston_surface_attach(surface, state->buffer);
2234 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002235
Jason Ekstrand7b982072014-05-20 14:33:03 -05002236 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002237 weston_surface_update_size(surface);
2238 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002239 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002240 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002241
Jason Ekstrand7b982072014-05-20 14:33:03 -05002242 state->sx = 0;
2243 state->sy = 0;
2244 state->newly_attached = 0;
2245 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002246
2247 /* wl_surface.damage */
2248 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002249 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002250 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002251 0, 0, surface->width, surface->height);
2252 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002253
2254 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002255 pixman_region32_init(&opaque);
2256 pixman_region32_intersect_rect(&opaque, &state->opaque,
2257 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002258
2259 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2260 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002261 wl_list_for_each(view, &surface->views, surface_link)
2262 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002263 }
2264
2265 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002266
2267 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002268 pixman_region32_intersect_rect(&surface->input, &state->input,
2269 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002270
Pekka Paalanenbc106382012-10-10 12:49:31 +03002271 /* wl_surface.frame */
2272 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002273 &state->frame_callback_list);
2274 wl_list_init(&state->frame_callback_list);
2275}
2276
2277static void
2278weston_surface_commit(struct weston_surface *surface)
2279{
2280 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002281
Pekka Paalanene67858b2013-04-25 13:57:42 +03002282 weston_surface_commit_subsurface_order(surface);
2283
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002284 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002285}
2286
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002287static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002288weston_subsurface_commit(struct weston_subsurface *sub);
2289
2290static void
2291weston_subsurface_parent_commit(struct weston_subsurface *sub,
2292 int parent_is_synchronized);
2293
2294static void
2295surface_commit(struct wl_client *client, struct wl_resource *resource)
2296{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002297 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002298 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2299
2300 if (sub) {
2301 weston_subsurface_commit(sub);
2302 return;
2303 }
2304
2305 weston_surface_commit(surface);
2306
2307 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2308 if (sub->surface != surface)
2309 weston_subsurface_parent_commit(sub, 0);
2310 }
2311}
2312
2313static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002314surface_set_buffer_transform(struct wl_client *client,
2315 struct wl_resource *resource, int transform)
2316{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002317 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002318
Jonny Lamba55f1392014-05-30 12:07:15 +02002319 /* if wl_output.transform grows more members this will need to be updated. */
2320 if (transform < 0 ||
2321 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2322 wl_resource_post_error(resource,
2323 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2324 "buffer transform must be a valid transform "
2325 "('%d' specified)", transform);
2326 return;
2327 }
2328
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002329 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002330 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002331}
2332
Alexander Larsson4ea95522013-05-22 14:41:37 +02002333static void
2334surface_set_buffer_scale(struct wl_client *client,
2335 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002336 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002337{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002338 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002339
Jonny Lamba55f1392014-05-30 12:07:15 +02002340 if (scale < 1) {
2341 wl_resource_post_error(resource,
2342 WL_SURFACE_ERROR_INVALID_SCALE,
2343 "buffer scale must be at least one "
2344 "('%d' specified)", scale);
2345 return;
2346 }
2347
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002348 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002349 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002350}
2351
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002352static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002353 surface_destroy,
2354 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002355 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002356 surface_frame,
2357 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002358 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002359 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002360 surface_set_buffer_transform,
2361 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002362};
2363
2364static void
2365compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002366 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002367{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002368 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002369 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002370
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002371 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002372 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002373 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002374 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002375 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002376
Jason Ekstranda85118c2013-06-27 20:17:02 -05002377 surface->resource =
2378 wl_resource_create(client, &wl_surface_interface,
2379 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002380 if (surface->resource == NULL) {
2381 weston_surface_destroy(surface);
2382 wl_resource_post_no_memory(resource);
2383 return;
2384 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002385 wl_resource_set_implementation(surface->resource, &surface_interface,
2386 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002387
2388 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002389}
2390
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002391static void
2392destroy_region(struct wl_resource *resource)
2393{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002394 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002395
2396 pixman_region32_fini(&region->region);
2397 free(region);
2398}
2399
2400static void
2401region_destroy(struct wl_client *client, struct wl_resource *resource)
2402{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002403 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002404}
2405
2406static void
2407region_add(struct wl_client *client, struct wl_resource *resource,
2408 int32_t x, int32_t y, int32_t width, int32_t height)
2409{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002410 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002411
2412 pixman_region32_union_rect(&region->region, &region->region,
2413 x, y, width, height);
2414}
2415
2416static void
2417region_subtract(struct wl_client *client, struct wl_resource *resource,
2418 int32_t x, int32_t y, int32_t width, int32_t height)
2419{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002420 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002421 pixman_region32_t rect;
2422
2423 pixman_region32_init_rect(&rect, x, y, width, height);
2424 pixman_region32_subtract(&region->region, &region->region, &rect);
2425 pixman_region32_fini(&rect);
2426}
2427
2428static const struct wl_region_interface region_interface = {
2429 region_destroy,
2430 region_add,
2431 region_subtract
2432};
2433
2434static void
2435compositor_create_region(struct wl_client *client,
2436 struct wl_resource *resource, uint32_t id)
2437{
2438 struct weston_region *region;
2439
2440 region = malloc(sizeof *region);
2441 if (region == NULL) {
2442 wl_resource_post_no_memory(resource);
2443 return;
2444 }
2445
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002446 pixman_region32_init(&region->region);
2447
Jason Ekstranda85118c2013-06-27 20:17:02 -05002448 region->resource =
2449 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002450 if (region->resource == NULL) {
2451 free(region);
2452 wl_resource_post_no_memory(resource);
2453 return;
2454 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002455 wl_resource_set_implementation(region->resource, &region_interface,
2456 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002457}
2458
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002459static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002460 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002461 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002462};
2463
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002464static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002465weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2466{
2467 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002468
Jason Ekstrand7b982072014-05-20 14:33:03 -05002469 weston_surface_commit_state(surface, &sub->cached);
2470 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002471
2472 weston_surface_commit_subsurface_order(surface);
2473
2474 weston_surface_schedule_repaint(surface);
2475
Jason Ekstrand7b982072014-05-20 14:33:03 -05002476 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002477}
2478
2479static void
2480weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2481{
2482 struct weston_surface *surface = sub->surface;
2483
2484 /*
2485 * If this commit would cause the surface to move by the
2486 * attach(dx, dy) parameters, the old damage region must be
2487 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002488 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002489 */
2490 pixman_region32_translate(&sub->cached.damage,
2491 -surface->pending.sx, -surface->pending.sy);
2492 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2493 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002494 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002495
2496 if (surface->pending.newly_attached) {
2497 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002498 weston_surface_state_set_buffer(&sub->cached,
2499 surface->pending.buffer);
2500 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002501 surface->pending.buffer);
2502 }
2503 sub->cached.sx += surface->pending.sx;
2504 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002505
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002506 sub->cached.buffer_viewport.changed |=
2507 surface->pending.buffer_viewport.changed;
2508 sub->cached.buffer_viewport.buffer =
2509 surface->pending.buffer_viewport.buffer;
2510 sub->cached.buffer_viewport.surface =
2511 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002512
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002513 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002514
2515 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2516
2517 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2518
2519 wl_list_insert_list(&sub->cached.frame_callback_list,
2520 &surface->pending.frame_callback_list);
2521 wl_list_init(&surface->pending.frame_callback_list);
2522
Jason Ekstrand7b982072014-05-20 14:33:03 -05002523 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002524}
2525
2526static int
2527weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2528{
2529 while (sub) {
2530 if (sub->synchronized)
2531 return 1;
2532
2533 if (!sub->parent)
2534 return 0;
2535
2536 sub = weston_surface_to_subsurface(sub->parent);
2537 }
2538
2539 return 0;
2540}
2541
2542static void
2543weston_subsurface_commit(struct weston_subsurface *sub)
2544{
2545 struct weston_surface *surface = sub->surface;
2546 struct weston_subsurface *tmp;
2547
2548 /* Recursive check for effectively synchronized. */
2549 if (weston_subsurface_is_synchronized(sub)) {
2550 weston_subsurface_commit_to_cache(sub);
2551 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002552 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002553 /* flush accumulated state from cache */
2554 weston_subsurface_commit_to_cache(sub);
2555 weston_subsurface_commit_from_cache(sub);
2556 } else {
2557 weston_surface_commit(surface);
2558 }
2559
2560 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2561 if (tmp->surface != surface)
2562 weston_subsurface_parent_commit(tmp, 0);
2563 }
2564 }
2565}
2566
2567static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002568weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002569{
2570 struct weston_surface *surface = sub->surface;
2571 struct weston_subsurface *tmp;
2572
Pekka Paalanene67858b2013-04-25 13:57:42 +03002573 /* From now on, commit_from_cache the whole sub-tree, regardless of
2574 * the synchronized mode of each child. This sub-surface or some
2575 * of its ancestors were synchronized, so we are synchronized
2576 * all the way down.
2577 */
2578
Jason Ekstrand7b982072014-05-20 14:33:03 -05002579 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002580 weston_subsurface_commit_from_cache(sub);
2581
2582 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2583 if (tmp->surface != surface)
2584 weston_subsurface_parent_commit(tmp, 1);
2585 }
2586}
2587
2588static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002589weston_subsurface_parent_commit(struct weston_subsurface *sub,
2590 int parent_is_synchronized)
2591{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002592 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002593 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594 wl_list_for_each(view, &sub->surface->views, surface_link)
2595 weston_view_set_position(view,
2596 sub->position.x,
2597 sub->position.y);
2598
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002599 sub->position.set = 0;
2600 }
2601
2602 if (parent_is_synchronized || sub->synchronized)
2603 weston_subsurface_synchronized_commit(sub);
2604}
2605
2606static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002607subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002608{
2609 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002610 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002611
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002613 weston_view_set_position(view,
2614 view->geometry.x + dx,
2615 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002616
2617 /* No need to check parent mappedness, because if parent is not
2618 * mapped, parent is not in a visible layer, so this sub-surface
2619 * will not be drawn either.
2620 */
2621 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002622 struct weston_output *output;
2623
Pekka Paalanene67858b2013-04-25 13:57:42 +03002624 /* Cannot call weston_surface_update_transform(),
2625 * because that would call it also for the parent surface,
2626 * which might not be mapped yet. That would lead to
2627 * inconsistent state, where the window could never be
2628 * mapped.
2629 *
2630 * Instead just assing any output, to make
2631 * weston_surface_is_mapped() return true, so that when the
2632 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002633 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002634 */
2635 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002636 output = container_of(compositor->output_list.next,
2637 struct weston_output, link);
2638
2639 surface->output = output;
2640 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641 }
2642}
2643
2644static struct weston_subsurface *
2645weston_surface_to_subsurface(struct weston_surface *surface)
2646{
2647 if (surface->configure == subsurface_configure)
2648 return surface->configure_private;
2649
2650 return NULL;
2651}
2652
Pekka Paalanen01388e22013-04-25 13:57:44 +03002653WL_EXPORT struct weston_surface *
2654weston_surface_get_main_surface(struct weston_surface *surface)
2655{
2656 struct weston_subsurface *sub;
2657
2658 while (surface && (sub = weston_surface_to_subsurface(surface)))
2659 surface = sub->parent;
2660
2661 return surface;
2662}
2663
Pekka Paalanene67858b2013-04-25 13:57:42 +03002664static void
2665subsurface_set_position(struct wl_client *client,
2666 struct wl_resource *resource, int32_t x, int32_t y)
2667{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002668 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002669
2670 if (!sub)
2671 return;
2672
2673 sub->position.x = x;
2674 sub->position.y = y;
2675 sub->position.set = 1;
2676}
2677
2678static struct weston_subsurface *
2679subsurface_from_surface(struct weston_surface *surface)
2680{
2681 struct weston_subsurface *sub;
2682
2683 sub = weston_surface_to_subsurface(surface);
2684 if (sub)
2685 return sub;
2686
2687 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2688 if (sub->surface == surface)
2689 return sub;
2690
2691 return NULL;
2692}
2693
2694static struct weston_subsurface *
2695subsurface_sibling_check(struct weston_subsurface *sub,
2696 struct weston_surface *surface,
2697 const char *request)
2698{
2699 struct weston_subsurface *sibling;
2700
2701 sibling = subsurface_from_surface(surface);
2702
2703 if (!sibling) {
2704 wl_resource_post_error(sub->resource,
2705 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2706 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002707 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002708 return NULL;
2709 }
2710
2711 if (sibling->parent != sub->parent) {
2712 wl_resource_post_error(sub->resource,
2713 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2714 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002715 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002716 return NULL;
2717 }
2718
2719 return sibling;
2720}
2721
2722static void
2723subsurface_place_above(struct wl_client *client,
2724 struct wl_resource *resource,
2725 struct wl_resource *sibling_resource)
2726{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002727 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002728 struct weston_surface *surface =
2729 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002730 struct weston_subsurface *sibling;
2731
2732 if (!sub)
2733 return;
2734
2735 sibling = subsurface_sibling_check(sub, surface, "place_above");
2736 if (!sibling)
2737 return;
2738
2739 wl_list_remove(&sub->parent_link_pending);
2740 wl_list_insert(sibling->parent_link_pending.prev,
2741 &sub->parent_link_pending);
2742}
2743
2744static void
2745subsurface_place_below(struct wl_client *client,
2746 struct wl_resource *resource,
2747 struct wl_resource *sibling_resource)
2748{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002749 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002750 struct weston_surface *surface =
2751 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002752 struct weston_subsurface *sibling;
2753
2754 if (!sub)
2755 return;
2756
2757 sibling = subsurface_sibling_check(sub, surface, "place_below");
2758 if (!sibling)
2759 return;
2760
2761 wl_list_remove(&sub->parent_link_pending);
2762 wl_list_insert(&sibling->parent_link_pending,
2763 &sub->parent_link_pending);
2764}
2765
2766static void
2767subsurface_set_sync(struct wl_client *client, 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 sub->synchronized = 1;
2773}
2774
2775static void
2776subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2777{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002778 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002779
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002780 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002781 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002782
2783 /* If sub became effectively desynchronized, flush. */
2784 if (!weston_subsurface_is_synchronized(sub))
2785 weston_subsurface_synchronized_commit(sub);
2786 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002787}
2788
2789static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002790weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2791{
2792 wl_list_remove(&sub->parent_link);
2793 wl_list_remove(&sub->parent_link_pending);
2794 wl_list_remove(&sub->parent_destroy_listener.link);
2795 sub->parent = NULL;
2796}
2797
2798static void
2799weston_subsurface_destroy(struct weston_subsurface *sub);
2800
2801static void
2802subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2803{
2804 struct weston_subsurface *sub =
2805 container_of(listener, struct weston_subsurface,
2806 surface_destroy_listener);
2807 assert(data == &sub->surface->resource);
2808
2809 /* The protocol object (wl_resource) is left inert. */
2810 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002811 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002812
2813 weston_subsurface_destroy(sub);
2814}
2815
2816static void
2817subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2818{
2819 struct weston_subsurface *sub =
2820 container_of(listener, struct weston_subsurface,
2821 parent_destroy_listener);
2822 assert(data == &sub->parent->resource);
2823 assert(sub->surface != sub->parent);
2824
2825 if (weston_surface_is_mapped(sub->surface))
2826 weston_surface_unmap(sub->surface);
2827
2828 weston_subsurface_unlink_parent(sub);
2829}
2830
2831static void
2832subsurface_resource_destroy(struct wl_resource *resource)
2833{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002834 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002835
2836 if (sub)
2837 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002838}
2839
2840static void
2841subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2842{
2843 wl_resource_destroy(resource);
2844}
2845
2846static void
2847weston_subsurface_link_parent(struct weston_subsurface *sub,
2848 struct weston_surface *parent)
2849{
2850 sub->parent = parent;
2851 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002852 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002853 &sub->parent_destroy_listener);
2854
2855 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2856 wl_list_insert(&parent->subsurface_list_pending,
2857 &sub->parent_link_pending);
2858}
2859
2860static void
2861weston_subsurface_link_surface(struct weston_subsurface *sub,
2862 struct weston_surface *surface)
2863{
2864 sub->surface = surface;
2865 sub->surface_destroy_listener.notify =
2866 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002867 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002868 &sub->surface_destroy_listener);
2869}
2870
2871static void
2872weston_subsurface_destroy(struct weston_subsurface *sub)
2873{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002874 struct weston_view *view, *next;
2875
Pekka Paalanene67858b2013-04-25 13:57:42 +03002876 assert(sub->surface);
2877
2878 if (sub->resource) {
2879 assert(weston_surface_to_subsurface(sub->surface) == sub);
2880 assert(sub->parent_destroy_listener.notify ==
2881 subsurface_handle_parent_destroy);
2882
George Kiagiadakised04d382014-06-13 18:10:26 +02002883 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2884 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002885 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002886 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002887
Pekka Paalanene67858b2013-04-25 13:57:42 +03002888 if (sub->parent)
2889 weston_subsurface_unlink_parent(sub);
2890
Jason Ekstrand7b982072014-05-20 14:33:03 -05002891 weston_surface_state_fini(&sub->cached);
2892 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002893
2894 sub->surface->configure = NULL;
2895 sub->surface->configure_private = NULL;
2896 } else {
2897 /* the dummy weston_subsurface for the parent itself */
2898 assert(sub->parent_destroy_listener.notify == NULL);
2899 wl_list_remove(&sub->parent_link);
2900 wl_list_remove(&sub->parent_link_pending);
2901 }
2902
2903 wl_list_remove(&sub->surface_destroy_listener.link);
2904 free(sub);
2905}
2906
2907static const struct wl_subsurface_interface subsurface_implementation = {
2908 subsurface_destroy,
2909 subsurface_set_position,
2910 subsurface_place_above,
2911 subsurface_place_below,
2912 subsurface_set_sync,
2913 subsurface_set_desync
2914};
2915
2916static struct weston_subsurface *
2917weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2918 struct weston_surface *parent)
2919{
2920 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002921 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002922
2923 sub = calloc(1, sizeof *sub);
2924 if (!sub)
2925 return NULL;
2926
Jason Ekstranda7af7042013-10-12 22:38:11 -05002927 wl_list_init(&sub->unused_views);
2928
Jason Ekstranda85118c2013-06-27 20:17:02 -05002929 sub->resource =
2930 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002931 if (!sub->resource) {
2932 free(sub);
2933 return NULL;
2934 }
2935
Jason Ekstranda85118c2013-06-27 20:17:02 -05002936 wl_resource_set_implementation(sub->resource,
2937 &subsurface_implementation,
2938 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939 weston_subsurface_link_surface(sub, surface);
2940 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002941 weston_surface_state_init(&sub->cached);
2942 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002943 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002944
2945 return sub;
2946}
2947
2948/* Create a dummy subsurface for having the parent itself in its
2949 * sub-surface lists. Makes stacking order manipulation easy.
2950 */
2951static struct weston_subsurface *
2952weston_subsurface_create_for_parent(struct weston_surface *parent)
2953{
2954 struct weston_subsurface *sub;
2955
2956 sub = calloc(1, sizeof *sub);
2957 if (!sub)
2958 return NULL;
2959
2960 weston_subsurface_link_surface(sub, parent);
2961 sub->parent = parent;
2962 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2963 wl_list_insert(&parent->subsurface_list_pending,
2964 &sub->parent_link_pending);
2965
2966 return sub;
2967}
2968
2969static void
2970subcompositor_get_subsurface(struct wl_client *client,
2971 struct wl_resource *resource,
2972 uint32_t id,
2973 struct wl_resource *surface_resource,
2974 struct wl_resource *parent_resource)
2975{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002976 struct weston_surface *surface =
2977 wl_resource_get_user_data(surface_resource);
2978 struct weston_surface *parent =
2979 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002980 struct weston_subsurface *sub;
2981 static const char where[] = "get_subsurface: wl_subsurface@";
2982
2983 if (surface == parent) {
2984 wl_resource_post_error(resource,
2985 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2986 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002987 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002988 return;
2989 }
2990
2991 if (weston_surface_to_subsurface(surface)) {
2992 wl_resource_post_error(resource,
2993 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2994 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002995 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002996 return;
2997 }
2998
2999 if (surface->configure) {
3000 wl_resource_post_error(resource,
3001 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3002 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003003 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003004 return;
3005 }
3006
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003007 if (weston_surface_get_main_surface(parent) == surface) {
3008 wl_resource_post_error(resource,
3009 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3010 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003011 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003012 return;
3013 }
3014
Pekka Paalanene67858b2013-04-25 13:57:42 +03003015 /* make sure the parent is in its own list */
3016 if (wl_list_empty(&parent->subsurface_list)) {
3017 if (!weston_subsurface_create_for_parent(parent)) {
3018 wl_resource_post_no_memory(resource);
3019 return;
3020 }
3021 }
3022
3023 sub = weston_subsurface_create(id, surface, parent);
3024 if (!sub) {
3025 wl_resource_post_no_memory(resource);
3026 return;
3027 }
3028
3029 surface->configure = subsurface_configure;
3030 surface->configure_private = sub;
3031}
3032
3033static void
3034subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3035{
3036 wl_resource_destroy(resource);
3037}
3038
3039static const struct wl_subcompositor_interface subcompositor_interface = {
3040 subcompositor_destroy,
3041 subcompositor_get_subsurface
3042};
3043
3044static void
3045bind_subcompositor(struct wl_client *client,
3046 void *data, uint32_t version, uint32_t id)
3047{
3048 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003049 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003050
Jason Ekstranda85118c2013-06-27 20:17:02 -05003051 resource =
3052 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003053 if (resource == NULL) {
3054 wl_client_post_no_memory(client);
3055 return;
3056 }
3057 wl_resource_set_implementation(resource, &subcompositor_interface,
3058 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003059}
3060
3061static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003062weston_compositor_dpms(struct weston_compositor *compositor,
3063 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003064{
3065 struct weston_output *output;
3066
3067 wl_list_for_each(output, &compositor->output_list, link)
3068 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003069 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003070}
3071
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003072WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003073weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003074{
Neil Roberts8b62e202013-09-30 13:14:47 +01003075 uint32_t old_state = compositor->state;
3076
3077 /* The state needs to be changed before emitting the wake
3078 * signal because that may try to schedule a repaint which
3079 * will not work if the compositor is still sleeping */
3080 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3081
3082 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003083 case WESTON_COMPOSITOR_SLEEPING:
3084 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3085 /* fall through */
3086 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003087 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003088 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003089 /* fall through */
3090 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003091 wl_event_source_timer_update(compositor->idle_source,
3092 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003093 }
3094}
3095
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003096WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003097weston_compositor_offscreen(struct weston_compositor *compositor)
3098{
3099 switch (compositor->state) {
3100 case WESTON_COMPOSITOR_OFFSCREEN:
3101 return;
3102 case WESTON_COMPOSITOR_SLEEPING:
3103 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3104 /* fall through */
3105 default:
3106 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3107 wl_event_source_timer_update(compositor->idle_source, 0);
3108 }
3109}
3110
3111WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003112weston_compositor_sleep(struct weston_compositor *compositor)
3113{
3114 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3115 return;
3116
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003117 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003118 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3119 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3120}
3121
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003122static int
3123idle_handler(void *data)
3124{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003125 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003126
3127 if (compositor->idle_inhibit)
3128 return 1;
3129
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003130 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003131 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003132
3133 return 1;
3134}
3135
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003136WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003137weston_plane_init(struct weston_plane *plane,
3138 struct weston_compositor *ec,
3139 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003140{
3141 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003142 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003143 plane->x = x;
3144 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003145 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003146
3147 /* Init the link so that the call to wl_list_remove() when releasing
3148 * the plane without ever stacking doesn't lead to a crash */
3149 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003150}
3151
3152WL_EXPORT void
3153weston_plane_release(struct weston_plane *plane)
3154{
Xiong Zhang97116532013-10-23 13:58:31 +08003155 struct weston_view *view;
3156
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003157 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003158 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003159
Xiong Zhang97116532013-10-23 13:58:31 +08003160 wl_list_for_each(view, &plane->compositor->view_list, link) {
3161 if (view->plane == plane)
3162 view->plane = NULL;
3163 }
3164
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003165 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003166}
3167
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003168WL_EXPORT void
3169weston_compositor_stack_plane(struct weston_compositor *ec,
3170 struct weston_plane *plane,
3171 struct weston_plane *above)
3172{
3173 if (above)
3174 wl_list_insert(above->link.prev, &plane->link);
3175 else
3176 wl_list_insert(&ec->plane_list, &plane->link);
3177}
3178
Casey Dahlin9074db52012-04-19 22:50:09 -04003179static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003180{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003181 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003182}
3183
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003184static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003185bind_output(struct wl_client *client,
3186 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003187{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003188 struct weston_output *output = data;
3189 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003190 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003191
Jason Ekstranda85118c2013-06-27 20:17:02 -05003192 resource = wl_resource_create(client, &wl_output_interface,
3193 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003194 if (resource == NULL) {
3195 wl_client_post_no_memory(client);
3196 return;
3197 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003198
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003199 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003200 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003201
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003202 wl_output_send_geometry(resource,
3203 output->x,
3204 output->y,
3205 output->mm_width,
3206 output->mm_height,
3207 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003208 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003209 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003210 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003211 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003212 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003213
3214 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003215 wl_output_send_mode(resource,
3216 mode->flags,
3217 mode->width,
3218 mode->height,
3219 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003220 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003221
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003222 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003223 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003224}
3225
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003226/* Move other outputs when one is removed so the space remains contiguos. */
3227static void
3228weston_compositor_remove_output(struct weston_compositor *compositor,
3229 struct weston_output *remove_output)
3230{
3231 struct weston_output *output;
3232 int offset = 0;
3233
3234 wl_list_for_each(output, &compositor->output_list, link) {
3235 if (output == remove_output) {
3236 offset = output->width;
3237 continue;
3238 }
3239
3240 if (offset > 0) {
3241 weston_output_move(output,
3242 output->x - offset, output->y);
3243 output->dirty = 1;
3244 }
3245 }
3246}
3247
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003248WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003249weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003250{
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003251 output->destroying = 1;
3252
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003253 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003254 wl_list_remove(&output->link);
3255
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003256 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003257 wl_signal_emit(&output->destroy_signal, output);
3258
Richard Hughesafe690c2013-05-02 10:10:04 +01003259 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003260 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003261 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003262 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003263
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003264 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003265}
3266
Scott Moreau1bad5db2012-08-18 01:04:05 -06003267static void
3268weston_output_compute_transform(struct weston_output *output)
3269{
3270 struct weston_matrix transform;
3271 int flip;
3272
3273 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003274 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003275
3276 switch(output->transform) {
3277 case WL_OUTPUT_TRANSFORM_FLIPPED:
3278 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3279 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3280 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003281 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003282 flip = -1;
3283 break;
3284 default:
3285 flip = 1;
3286 break;
3287 }
3288
3289 switch(output->transform) {
3290 case WL_OUTPUT_TRANSFORM_NORMAL:
3291 case WL_OUTPUT_TRANSFORM_FLIPPED:
3292 transform.d[0] = flip;
3293 transform.d[1] = 0;
3294 transform.d[4] = 0;
3295 transform.d[5] = 1;
3296 break;
3297 case WL_OUTPUT_TRANSFORM_90:
3298 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3299 transform.d[0] = 0;
3300 transform.d[1] = -flip;
3301 transform.d[4] = 1;
3302 transform.d[5] = 0;
3303 break;
3304 case WL_OUTPUT_TRANSFORM_180:
3305 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3306 transform.d[0] = -flip;
3307 transform.d[1] = 0;
3308 transform.d[4] = 0;
3309 transform.d[5] = -1;
3310 break;
3311 case WL_OUTPUT_TRANSFORM_270:
3312 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3313 transform.d[0] = 0;
3314 transform.d[1] = flip;
3315 transform.d[4] = -1;
3316 transform.d[5] = 0;
3317 break;
3318 default:
3319 break;
3320 }
3321
3322 weston_matrix_multiply(&output->matrix, &transform);
3323}
3324
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003325WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003326weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003327{
Scott Moreau850ca422012-05-21 15:21:25 -06003328 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003329
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003330 weston_matrix_init(&output->matrix);
3331 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003332 -(output->x + output->width / 2.0),
3333 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003334
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003335 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003336 2.0 / output->width,
3337 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003338
Scott Moreauccbf29d2012-02-22 14:21:41 -07003339 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003340 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003341 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003342 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3343 output->zoom.trans_y, 0);
3344 weston_matrix_scale(&output->matrix, magnification,
3345 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003346 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003347
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003348 weston_output_compute_transform(output);
3349
Scott Moreauccbf29d2012-02-22 14:21:41 -07003350 output->dirty = 0;
3351}
3352
Scott Moreau1bad5db2012-08-18 01:04:05 -06003353static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003354weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003355{
3356 output->transform = transform;
3357
3358 switch (transform) {
3359 case WL_OUTPUT_TRANSFORM_90:
3360 case WL_OUTPUT_TRANSFORM_270:
3361 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3362 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3363 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003364 output->width = output->current_mode->height;
3365 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003366 break;
3367 case WL_OUTPUT_TRANSFORM_NORMAL:
3368 case WL_OUTPUT_TRANSFORM_180:
3369 case WL_OUTPUT_TRANSFORM_FLIPPED:
3370 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003371 output->width = output->current_mode->width;
3372 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003373 break;
3374 default:
3375 break;
3376 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003377
Hardening57388e42013-09-18 23:56:36 +02003378 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003379 output->width /= scale;
3380 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003381}
3382
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003383static void
3384weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003385{
3386 output->x = x;
3387 output->y = y;
3388
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003389 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003390 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003391 output->width,
3392 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003393}
3394
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003395WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003396weston_output_move(struct weston_output *output, int x, int y)
3397{
3398 pixman_region32_t old_region;
3399 struct wl_resource *resource;
3400
3401 output->move_x = x - output->x;
3402 output->move_y = y - output->y;
3403
3404 if (output->move_x == 0 && output->move_y == 0)
3405 return;
3406
3407 pixman_region32_init(&old_region);
3408 pixman_region32_copy(&old_region, &output->region);
3409
3410 weston_output_init_geometry(output, x, y);
3411
3412 output->dirty = 1;
3413
3414 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003415 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003416
3417 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003418 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003419 wl_output_send_geometry(resource,
3420 output->x,
3421 output->y,
3422 output->mm_width,
3423 output->mm_height,
3424 output->subpixel,
3425 output->make,
3426 output->model,
3427 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003428
3429 if (wl_resource_get_version(resource) >= 2)
3430 wl_output_send_done(resource);
3431 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003432}
3433
3434WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003435weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003436 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003437 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003438{
3439 output->compositor = c;
3440 output->x = x;
3441 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003442 output->mm_width = mm_width;
3443 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003444 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003445 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003446
Alexander Larsson0b135062013-05-28 16:23:36 +02003447 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003448 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003449
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003450 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003451 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003452
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003453 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003454 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003455 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003456 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003457
Casey Dahlin58ba1372012-04-19 22:50:08 -04003458 output->id = ffs(~output->compositor->output_id_pool) - 1;
3459 output->compositor->output_id_pool |= 1 << output->id;
3460
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003461 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003462 wl_global_create(c->wl_display, &wl_output_interface, 2,
3463 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003464 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003465}
3466
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003467WL_EXPORT void
3468weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003469 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003470 wl_fixed_t *x, wl_fixed_t *y)
3471{
3472 wl_fixed_t tx, ty;
3473 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003474 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003475
Hardeningff39efa2013-09-18 23:56:35 +02003476 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3477 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003478
3479 switch(output->transform) {
3480 case WL_OUTPUT_TRANSFORM_NORMAL:
3481 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003482 tx = device_x;
3483 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003484 break;
3485 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003486 tx = device_y;
3487 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003488 break;
3489 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003490 tx = width - device_x;
3491 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003492 break;
3493 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003494 tx = width - device_y;
3495 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003496 break;
3497 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003498 tx = width - device_x;
3499 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003500 break;
3501 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003502 tx = width - device_y;
3503 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003504 break;
3505 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003506 tx = device_x;
3507 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003508 break;
3509 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003510 tx = device_y;
3511 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003512 break;
3513 }
3514
Neil Robertseb5a2002014-05-01 16:13:55 +01003515 tx /= output->current_scale;
3516 ty /= output->current_scale;
3517
3518 if (output->zoom.active) {
3519 zoom_scale = output->zoom.spring_z.current;
3520 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3521 output->width / 2.0f *
3522 (zoom_scale + output->zoom.trans_x));
3523 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3524 output->height / 2.0f *
3525 (zoom_scale + output->zoom.trans_y));
3526 tx = wl_fixed_from_double(zx);
3527 ty = wl_fixed_from_double(zy);
3528 }
3529
3530 *x = tx + wl_fixed_from_int(output->x);
3531 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003532}
3533
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003534static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003535destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003536{
Jonny Lamb74130762013-11-26 18:19:46 +01003537 struct weston_surface *surface =
3538 wl_resource_get_user_data(resource);
3539
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003540 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003541 surface->pending.buffer_viewport.buffer.src_width =
3542 wl_fixed_from_int(-1);
3543 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003544 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003545}
3546
3547static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003548viewport_destroy(struct wl_client *client,
3549 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003550{
3551 wl_resource_destroy(resource);
3552}
3553
3554static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003555viewport_set(struct wl_client *client,
3556 struct wl_resource *resource,
3557 wl_fixed_t src_x,
3558 wl_fixed_t src_y,
3559 wl_fixed_t src_width,
3560 wl_fixed_t src_height,
3561 int32_t dst_width,
3562 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003563{
Jonny Lamb74130762013-11-26 18:19:46 +01003564 struct weston_surface *surface =
3565 wl_resource_get_user_data(resource);
3566
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003567 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003568
Jonny Lamb8ae35902013-11-26 18:19:45 +01003569 if (wl_fixed_to_double(src_width) < 0 ||
3570 wl_fixed_to_double(src_height) < 0) {
3571 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003572 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003573 "source dimensions must be non-negative (%fx%f)",
3574 wl_fixed_to_double(src_width),
3575 wl_fixed_to_double(src_height));
3576 return;
3577 }
3578
3579 if (dst_width <= 0 || dst_height <= 0) {
3580 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003581 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003582 "destination dimensions must be positive (%dx%d)",
3583 dst_width, dst_height);
3584 return;
3585 }
3586
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003587 surface->pending.buffer_viewport.buffer.src_x = src_x;
3588 surface->pending.buffer_viewport.buffer.src_y = src_y;
3589 surface->pending.buffer_viewport.buffer.src_width = src_width;
3590 surface->pending.buffer_viewport.buffer.src_height = src_height;
3591 surface->pending.buffer_viewport.surface.width = dst_width;
3592 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003593 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003594}
3595
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003596static void
3597viewport_set_source(struct wl_client *client,
3598 struct wl_resource *resource,
3599 wl_fixed_t src_x,
3600 wl_fixed_t src_y,
3601 wl_fixed_t src_width,
3602 wl_fixed_t src_height)
3603{
3604 struct weston_surface *surface =
3605 wl_resource_get_user_data(resource);
3606
3607 assert(surface->viewport_resource != NULL);
3608
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003609 if (src_width == wl_fixed_from_int(-1) &&
3610 src_height == wl_fixed_from_int(-1)) {
3611 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003612 surface->pending.buffer_viewport.buffer.src_width =
3613 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003614 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003615 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003616 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003617
3618 if (src_width <= 0 || src_height <= 0) {
3619 wl_resource_post_error(resource,
3620 WL_VIEWPORT_ERROR_BAD_VALUE,
3621 "source size must be positive (%fx%f)",
3622 wl_fixed_to_double(src_width),
3623 wl_fixed_to_double(src_height));
3624 return;
3625 }
3626
3627 surface->pending.buffer_viewport.buffer.src_x = src_x;
3628 surface->pending.buffer_viewport.buffer.src_y = src_y;
3629 surface->pending.buffer_viewport.buffer.src_width = src_width;
3630 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003631 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003632}
3633
3634static void
3635viewport_set_destination(struct wl_client *client,
3636 struct wl_resource *resource,
3637 int32_t dst_width,
3638 int32_t dst_height)
3639{
3640 struct weston_surface *surface =
3641 wl_resource_get_user_data(resource);
3642
3643 assert(surface->viewport_resource != NULL);
3644
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003645 if (dst_width == -1 && dst_height == -1) {
3646 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003647 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003648 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003649 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003650 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003651
3652 if (dst_width <= 0 || dst_height <= 0) {
3653 wl_resource_post_error(resource,
3654 WL_VIEWPORT_ERROR_BAD_VALUE,
3655 "destination size must be positive (%dx%d)",
3656 dst_width, dst_height);
3657 return;
3658 }
3659
3660 surface->pending.buffer_viewport.surface.width = dst_width;
3661 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003662 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003663}
3664
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003665static const struct wl_viewport_interface viewport_interface = {
3666 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003667 viewport_set,
3668 viewport_set_source,
3669 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003670};
3671
3672static void
3673scaler_destroy(struct wl_client *client,
3674 struct wl_resource *resource)
3675{
3676 wl_resource_destroy(resource);
3677}
3678
3679static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003680scaler_get_viewport(struct wl_client *client,
3681 struct wl_resource *scaler,
3682 uint32_t id,
3683 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003684{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003685 int version = wl_resource_get_version(scaler);
3686 struct weston_surface *surface =
3687 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003688 struct wl_resource *resource;
3689
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003690 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003691 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003692 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3693 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003694 return;
3695 }
3696
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003697 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003698 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003699 if (resource == NULL) {
3700 wl_client_post_no_memory(client);
3701 return;
3702 }
3703
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003704 wl_resource_set_implementation(resource, &viewport_interface,
3705 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003706
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003707 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003708}
3709
3710static const struct wl_scaler_interface scaler_interface = {
3711 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003712 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003713};
3714
3715static void
3716bind_scaler(struct wl_client *client,
3717 void *data, uint32_t version, uint32_t id)
3718{
3719 struct wl_resource *resource;
3720
3721 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003722 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003723 if (resource == NULL) {
3724 wl_client_post_no_memory(client);
3725 return;
3726 }
3727
3728 wl_resource_set_implementation(resource, &scaler_interface,
3729 NULL, NULL);
3730}
3731
3732static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003733compositor_bind(struct wl_client *client,
3734 void *data, uint32_t version, uint32_t id)
3735{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003736 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003737 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003738
Jason Ekstranda85118c2013-06-27 20:17:02 -05003739 resource = wl_resource_create(client, &wl_compositor_interface,
3740 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003741 if (resource == NULL) {
3742 wl_client_post_no_memory(client);
3743 return;
3744 }
3745
3746 wl_resource_set_implementation(resource, &compositor_interface,
3747 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003748}
3749
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003750static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003751log_uname(void)
3752{
3753 struct utsname usys;
3754
3755 uname(&usys);
3756
3757 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3758 usys.version, usys.machine);
3759}
3760
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003761WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003762weston_environment_get_fd(const char *env)
3763{
3764 char *e, *end;
3765 int fd, flags;
3766
3767 e = getenv(env);
3768 if (!e)
3769 return -1;
3770 fd = strtol(e, &end, 0);
3771 if (*end != '\0')
3772 return -1;
3773
3774 flags = fcntl(fd, F_GETFD);
3775 if (flags == -1)
3776 return -1;
3777
3778 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3779 unsetenv(env);
3780
3781 return fd;
3782}
3783
3784WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003785weston_compositor_init(struct weston_compositor *ec,
3786 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003787 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003788 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003789{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003790 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003791 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003792 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003793
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003794 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003795 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003796 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003797 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003798 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003799 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003800 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003801 wl_signal_init(&ec->idle_signal);
3802 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003803 wl_signal_init(&ec->show_input_panel_signal);
3804 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003805 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003806 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003807 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003808 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003809 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003810 wl_signal_init(&ec->session_signal);
3811 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003812
Casey Dahlin58ba1372012-04-19 22:50:08 -04003813 ec->output_id_pool = 0;
3814
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003815 if (!wl_global_create(display, &wl_compositor_interface, 3,
3816 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003817 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003818
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003819 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3820 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003821 return -1;
3822
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003823 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003824 ec, bind_scaler))
3825 return -1;
3826
Jason Ekstranda7af7042013-10-12 22:38:11 -05003827 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003828 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003829 wl_list_init(&ec->layer_list);
3830 wl_list_init(&ec->seat_list);
3831 wl_list_init(&ec->output_list);
3832 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003833 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003834 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003835 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003836 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003837 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003838
Xiong Zhang97116532013-10-23 13:58:31 +08003839 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003840 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003841
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003842 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3843 weston_config_section_get_string(s, "keymap_rules",
3844 (char **) &xkb_names.rules, NULL);
3845 weston_config_section_get_string(s, "keymap_model",
3846 (char **) &xkb_names.model, NULL);
3847 weston_config_section_get_string(s, "keymap_layout",
3848 (char **) &xkb_names.layout, NULL);
3849 weston_config_section_get_string(s, "keymap_variant",
3850 (char **) &xkb_names.variant, NULL);
3851 weston_config_section_get_string(s, "keymap_options",
3852 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003853
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003854 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3855 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003856
Jonny Lamb66a41a02014-08-12 14:58:25 +02003857 weston_config_section_get_int(s, "repeat-rate",
3858 &ec->kb_repeat_rate, 40);
3859 weston_config_section_get_int(s, "repeat-delay",
3860 &ec->kb_repeat_delay, 400);
3861
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003862 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003863
3864 wl_data_device_manager_init(ec->wl_display);
3865
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003866 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003867
Daniel Stone725c2c32012-06-22 14:04:36 +01003868 loop = wl_display_get_event_loop(ec->wl_display);
3869 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3870 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3871
3872 ec->input_loop = wl_event_loop_create();
3873
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003874 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3875 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3876
3877 weston_compositor_schedule_repaint(ec);
3878
Daniel Stone725c2c32012-06-22 14:04:36 +01003879 return 0;
3880}
3881
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003882WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003883weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003884{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003885 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003886
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003887 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003888 if (ec->input_loop_source)
3889 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003890
Matt Roper361d2ad2011-08-29 13:52:23 -07003891 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003892 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003893 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003894
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003895 if (ec->renderer)
3896 ec->renderer->destroy(ec);
3897
Daniel Stone325fc2d2012-05-30 16:31:58 +01003898 weston_binding_list_destroy_all(&ec->key_binding_list);
3899 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003900 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003901 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003902 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003903
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003904 weston_plane_release(&ec->primary_plane);
3905
Jonas Ådahlc97af922012-03-28 22:36:09 +02003906 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003907
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003908 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003909}
3910
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003911WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003912weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3913 const struct weston_pointer_grab_interface *interface)
3914{
3915 struct weston_seat *seat;
3916
3917 ec->default_pointer_grab = interface;
3918 wl_list_for_each(seat, &ec->seat_list, link) {
3919 if (seat->pointer) {
3920 weston_pointer_set_default_grab(seat->pointer,
3921 interface);
3922 }
3923 }
3924}
3925
3926WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003927weston_version(int *major, int *minor, int *micro)
3928{
3929 *major = WESTON_VERSION_MAJOR;
3930 *minor = WESTON_VERSION_MINOR;
3931 *micro = WESTON_VERSION_MICRO;
3932}
3933
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003934static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003935 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003936 const char *desc;
3937} capability_strings[] = {
3938 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003939 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003940};
3941
3942static void
3943weston_compositor_log_capabilities(struct weston_compositor *compositor)
3944{
3945 unsigned i;
3946 int yes;
3947
3948 weston_log("Compositor capabilities:\n");
3949 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
3950 yes = compositor->capabilities & capability_strings[i].bit;
3951 weston_log_continue(STAMP_SPACE "%s %s\n",
3952 capability_strings[i].desc,
3953 yes ? "yes" : "no");
3954 }
3955}
3956
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003957static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003958{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003959 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003960
Martin Minarik6d118362012-06-07 18:01:59 +02003961 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003962 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003963
3964 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003965}
3966
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003967#ifdef HAVE_LIBUNWIND
3968
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003969static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05003970print_backtrace(void)
3971{
3972 unw_cursor_t cursor;
3973 unw_context_t context;
3974 unw_word_t off;
3975 unw_proc_info_t pip;
3976 int ret, i = 0;
3977 char procname[256];
3978 const char *filename;
3979 Dl_info dlinfo;
3980
3981 pip.unwind_info = NULL;
3982 ret = unw_getcontext(&context);
3983 if (ret) {
3984 weston_log("unw_getcontext: %d\n", ret);
3985 return;
3986 }
3987
3988 ret = unw_init_local(&cursor, &context);
3989 if (ret) {
3990 weston_log("unw_init_local: %d\n", ret);
3991 return;
3992 }
3993
3994 ret = unw_step(&cursor);
3995 while (ret > 0) {
3996 ret = unw_get_proc_info(&cursor, &pip);
3997 if (ret) {
3998 weston_log("unw_get_proc_info: %d\n", ret);
3999 break;
4000 }
4001
4002 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4003 if (ret && ret != -UNW_ENOMEM) {
4004 if (ret != -UNW_EUNSPEC)
4005 weston_log("unw_get_proc_name: %d\n", ret);
4006 procname[0] = '?';
4007 procname[1] = 0;
4008 }
4009
4010 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4011 *dlinfo.dli_fname)
4012 filename = dlinfo.dli_fname;
4013 else
4014 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004015
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004016 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4017 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4018
4019 ret = unw_step(&cursor);
4020 if (ret < 0)
4021 weston_log("unw_step: %d\n", ret);
4022 }
4023}
4024
4025#else
4026
4027static void
4028print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004029{
4030 void *buffer[32];
4031 int i, count;
4032 Dl_info info;
4033
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004034 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4035 for (i = 0; i < count; i++) {
4036 dladdr(buffer[i], &info);
4037 weston_log(" [%016lx] %s (%s)\n",
4038 (long) buffer[i],
4039 info.dli_sname ? info.dli_sname : "--",
4040 info.dli_fname);
4041 }
4042}
4043
4044#endif
4045
4046static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004047on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004048{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004049 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004050 * then call the backend restore function, which will switch
4051 * back to the vt we launched from or ungrab X etc and then
4052 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004053 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004054 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004055 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004056
Peter Maatmane5b42e42013-03-27 22:38:53 +01004057 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004058
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004059 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004060
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004061 segv_compositor->restore(segv_compositor);
4062
4063 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004064}
4065
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004066WL_EXPORT void *
4067weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004068{
4069 char path[PATH_MAX];
4070 void *module, *init;
4071
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004072 if (name == NULL)
4073 return NULL;
4074
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004075 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004076 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004077 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004078 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004079
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004080 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4081 if (module) {
4082 weston_log("Module '%s' already loaded\n", path);
4083 dlclose(module);
4084 return NULL;
4085 }
4086
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004087 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004088 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004089 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004090 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004091 return NULL;
4092 }
4093
4094 init = dlsym(module, entrypoint);
4095 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004096 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004097 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004098 return NULL;
4099 }
4100
4101 return init;
4102}
4103
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004104static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004105load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004106 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004107{
4108 const char *p, *end;
4109 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004110 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004111 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004112
4113 if (modules == NULL)
4114 return 0;
4115
4116 p = modules;
4117 while (*p) {
4118 end = strchrnul(p, ',');
4119 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004120 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004121 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004122 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004123 p = end;
4124 while (*p == ',')
4125 p++;
4126
4127 }
4128
4129 return 0;
4130}
4131
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004132static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004133 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4134
4135static const char xdg_wrong_message[] =
4136 "fatal: environment variable XDG_RUNTIME_DIR\n"
4137 "is set to \"%s\", which is not a directory.\n";
4138
4139static const char xdg_wrong_mode_message[] =
4140 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004141 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4142 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004143
4144static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004145 "Refer to your distribution on how to get it, or\n"
4146 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4147 "on how to implement it.\n";
4148
Martin Minarik37032f82012-06-18 20:15:18 +02004149static void
4150verify_xdg_runtime_dir(void)
4151{
4152 char *dir = getenv("XDG_RUNTIME_DIR");
4153 struct stat s;
4154
4155 if (!dir) {
4156 weston_log(xdg_error_message);
4157 weston_log_continue(xdg_detail_message);
4158 exit(EXIT_FAILURE);
4159 }
4160
4161 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4162 weston_log(xdg_wrong_message, dir);
4163 weston_log_continue(xdg_detail_message);
4164 exit(EXIT_FAILURE);
4165 }
4166
4167 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4168 weston_log(xdg_wrong_mode_message,
4169 dir, s.st_mode & 0777, s.st_uid);
4170 weston_log_continue(xdg_detail_message);
4171 }
4172}
4173
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004174static int
4175usage(int error_code)
4176{
4177 fprintf(stderr,
4178 "Usage: weston [OPTIONS]\n\n"
4179 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4180 "Weston supports multiple backends, and depending on which backend is in use\n"
4181 "different options will be accepted.\n\n"
4182
4183
4184 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004185 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004186 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004187 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4188 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004189 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004190 " -S, --socket=NAME\tName of socket to listen on\n"
4191 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004192 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004193 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004194 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004195 " -h, --help\t\tThis help message\n\n");
4196
4197 fprintf(stderr,
4198 "Options for drm-backend.so:\n\n"
4199 " --connector=ID\tBring up only this connector\n"
4200 " --seat=SEAT\t\tThe seat that weston should run on\n"
4201 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004202 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004203 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4204
4205 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004206 "Options for fbdev-backend.so:\n\n"
4207 " --tty=TTY\t\tThe tty to use\n"
4208 " --device=DEVICE\tThe framebuffer device to use\n\n");
4209
4210 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004211 "Options for x11-backend.so:\n\n"
4212 " --width=WIDTH\t\tWidth of X window\n"
4213 " --height=HEIGHT\tHeight of X window\n"
4214 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004215 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004216 " --output-count=COUNT\tCreate multiple outputs\n"
4217 " --no-input\t\tDont create input devices\n\n");
4218
4219 fprintf(stderr,
4220 "Options for wayland-backend.so:\n\n"
4221 " --width=WIDTH\t\tWidth of Wayland surface\n"
4222 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004223 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004224 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004225 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004226 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004227 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004228 " --display=DISPLAY\tWayland display to connect to\n\n");
4229
Pekka Paalanene31e0532013-05-22 18:03:07 +03004230#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4231 fprintf(stderr,
4232 "Options for rpi-backend.so:\n\n"
4233 " --tty=TTY\t\tThe tty to use\n"
4234 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4235 " --transform=TR\tThe output transformation, TR is one of:\n"
4236 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004237 " --opaque-regions\tEnable support for opaque regions, can be "
4238 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004239 "\n");
4240#endif
4241
Hardeningc077a842013-07-08 00:51:35 +02004242#if defined(BUILD_RDP_COMPOSITOR)
4243 fprintf(stderr,
4244 "Options for rdp-backend.so:\n\n"
4245 " --width=WIDTH\t\tWidth of desktop\n"
4246 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004247 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4248 " --address=ADDR\tThe address to bind\n"
4249 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004250 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004251 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4252 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4253 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4254 "\n");
4255#endif
4256
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004257 exit(error_code);
4258}
4259
Peter Maatmane5b42e42013-03-27 22:38:53 +01004260static void
4261catch_signals(void)
4262{
4263 struct sigaction action;
4264
4265 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4266 action.sa_sigaction = on_caught_signal;
4267 sigemptyset(&action.sa_mask);
4268 sigaction(SIGSEGV, &action, NULL);
4269 sigaction(SIGABRT, &action, NULL);
4270}
4271
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004272static void
4273handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4274{
4275 struct wl_client *client = data;
4276
4277 weston_log("Primary client died. Closing...\n");
4278
4279 wl_display_terminate(wl_client_get_display(client));
4280}
4281
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004282int main(int argc, char *argv[])
4283{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004284 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004285 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004286 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004287 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004288 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004289 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004290 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004291 int *argc, char *argv[],
4292 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004293 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004294 char *backend = NULL;
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004295 char *option_backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004296 char *shell = NULL;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004297 char *option_shell = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004298 char *modules, *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004299 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004300 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004301 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004302 int32_t help = 0;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004303 const char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004304 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004305 int32_t noconfig = 0;
4306 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004307 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004308 struct wl_client *primary_client;
4309 struct wl_listener primary_client_destroyed;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004310
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004311 const struct weston_option core_options[] = {
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004312 { WESTON_OPTION_STRING, "backend", 'B', &option_backend },
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004313 { WESTON_OPTION_STRING, "shell", 0, &option_shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004314 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4315 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004316 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004317 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004318 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004319 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004320 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004321 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004322
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004323 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004324
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004325 if (help)
4326 usage(EXIT_SUCCESS);
4327
Scott Moreau12245142012-08-29 15:15:58 -06004328 if (version) {
4329 printf(PACKAGE_STRING "\n");
4330 return EXIT_SUCCESS;
4331 }
4332
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004333 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004334
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004335 weston_log("%s\n"
4336 STAMP_SPACE "%s\n"
4337 STAMP_SPACE "Bug reports to: %s\n"
4338 STAMP_SPACE "Build: %s\n",
4339 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004340 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004341 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004342
Martin Minarik37032f82012-06-18 20:15:18 +02004343 verify_xdg_runtime_dir();
4344
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004345 display = wl_display_create();
4346
Tiago Vignatti2116b892011-08-08 05:52:59 -07004347 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004348 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4349 display);
4350 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4351 display);
4352 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4353 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004354
4355 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004356 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4357 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004358
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304359 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4360 ret = EXIT_FAILURE;
4361 goto out_signals;
4362 }
4363
Pekka Paalanen588bee12014-05-07 16:26:25 +03004364 if (noconfig == 0)
4365 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004366 if (config != NULL) {
4367 weston_log("Using config file '%s'\n",
4368 weston_config_get_full_path(config));
4369 } else {
4370 weston_log("Starting with no config file.\n");
4371 }
4372 section = weston_config_get_section(config, "core", NULL, NULL);
4373
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004374 if (option_backend)
4375 backend = strdup(option_backend);
4376 else
4377 weston_config_section_get_string(section, "backend", &backend,
4378 NULL);
4379
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004380 if (!backend) {
Jason Ekstrandcf40a132014-04-02 19:53:56 -05004381 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004382 backend = strdup("wayland-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004383 else if (getenv("DISPLAY"))
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004384 backend = strdup("x11-backend.so");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004385 else
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004386 backend = strdup(WESTON_NATIVE_BACKEND);
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004387 }
4388
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004389 backend_init = weston_load_module(backend, "backend_init");
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004390 free(backend);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304391 if (!backend_init) {
4392 ret = EXIT_FAILURE;
4393 goto out_signals;
4394 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004395
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004396 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004397 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004398 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304399 ret = EXIT_FAILURE;
4400 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004401 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004402
Peter Maatmane5b42e42013-03-27 22:38:53 +01004403 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004404 segv_compositor = ec;
4405
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004406 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004407 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004408
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004409 for (i = 1; i < argc; i++)
4410 weston_log("fatal: unhandled option: %s\n", argv[i]);
4411 if (argc > 1) {
4412 ret = EXIT_FAILURE;
4413 goto out;
4414 }
4415
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004416 weston_compositor_log_capabilities(ec);
4417
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004418 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4419 if (server_socket) {
4420 weston_log("Running with single client\n");
4421 fd = strtol(server_socket, &end, 0);
4422 if (*end != '\0')
4423 fd = -1;
4424 } else {
4425 fd = -1;
4426 }
4427
4428 if (fd != -1) {
4429 primary_client = wl_client_create(display, fd);
4430 if (!primary_client) {
4431 weston_log("fatal: failed to add client: %m\n");
4432 ret = EXIT_FAILURE;
4433 goto out;
4434 }
4435 primary_client_destroyed.notify =
4436 handle_primary_client_destroyed;
4437 wl_client_add_destroy_listener(primary_client,
4438 &primary_client_destroyed);
4439 } else {
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004440 if (socket_name) {
4441 if (wl_display_add_socket(display, socket_name)) {
4442 weston_log("fatal: failed to add socket: %m\n");
4443 ret = EXIT_FAILURE;
4444 goto out;
4445 }
4446 } else {
4447 socket_name = wl_display_add_socket_auto(display);
4448 if (!socket_name) {
4449 weston_log("fatal: failed to add socket: %m\n");
4450 ret = EXIT_FAILURE;
4451 goto out;
4452 }
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004453 }
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004454
4455 setenv("WAYLAND_DISPLAY", socket_name, 1);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004456 }
4457
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004458 if (option_shell)
4459 shell = strdup(option_shell);
4460 else
4461 weston_config_section_get_string(section, "shell", &shell,
4462 "desktop-shell.so");
4463
4464 if (load_modules(ec, shell, &argc, argv) < 0) {
4465 free(shell);
4466 goto out;
4467 }
4468 free(shell);
4469
4470 weston_config_section_get_string(section, "modules", &modules, "");
4471 if (load_modules(ec, modules, &argc, argv) < 0) {
4472 free(modules);
4473 goto out;
4474 }
4475 free(modules);
4476
4477 if (load_modules(ec, option_modules, &argc, argv) < 0)
4478 goto out;
4479
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004480 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004481
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004482 wl_display_run(display);
4483
4484 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004485 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004486 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004487
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004488 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004489
Daniel Stone855028f2012-05-01 20:37:10 +01004490 weston_compositor_xkb_destroy(ec);
4491
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004492 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304493
4494out_signals:
4495 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4496 if (signals[i])
4497 wl_event_source_remove(signals[i]);
4498
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004499 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004500
Martin Minarik19e6f262012-06-07 13:08:46 +02004501 weston_log_file_close();
4502
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004503 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004504}