blob: 6a81cc726c993bb217f71413e5e6fa8968be39be [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 Paalanen31f7d782014-09-23 22:08:43 -04004 * Copyright © 2012-2014 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 Paalanen31f7d782014-09-23 22:08:43 -040058#include "presentation_timing-server-protocol.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030059#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040060#include "git-version.h"
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -050061#include "version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050062
Kristian Høgsberg27da5382011-06-21 17:32:25 -040063static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040064static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065
66static int
67sigchld_handler(int signal_number, void *data)
68{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050069 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040070 int status;
71 pid_t pid;
72
Pekka Paalanen23ade622014-08-27 13:31:26 +030073 while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
74 wl_list_for_each(p, &child_process_list, link) {
75 if (p->pid == pid)
76 break;
77 }
Bill Spitzak027b9622012-03-17 15:22:03 -070078
Pekka Paalanen23ade622014-08-27 13:31:26 +030079 if (&p->link == &child_process_list) {
80 weston_log("unknown child process exited\n");
81 continue;
82 }
83
84 wl_list_remove(&p->link);
85 p->cleanup(p, status);
Kristian Høgsberg27da5382011-06-21 17:32:25 -040086 }
87
Pekka Paalanen23ade622014-08-27 13:31:26 +030088 if (pid < 0 && errno != ECHILD)
89 weston_log("waitpid error %m\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040090
91 return 1;
92}
93
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020094static void
Alexander Larsson0b135062013-05-28 16:23:36 +020095weston_output_transform_scale_init(struct weston_output *output,
96 uint32_t transform, uint32_t scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020097
Rob Bradford27b17932013-06-26 18:08:46 +010098static void
Jason Ekstranda7af7042013-10-12 22:38:11 -050099weston_compositor_build_view_list(struct weston_compositor *compositor);
Rob Bradford27b17932013-06-26 18:08:46 +0100100
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600101static void weston_mode_switch_finish(struct weston_output *output,
102 int mode_changed,
103 int scale_changed)
Alex Wu2dda6042012-04-17 17:20:47 +0800104{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200105 struct weston_seat *seat;
Hardening57388e42013-09-18 23:56:36 +0200106 struct wl_resource *resource;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200107 pixman_region32_t old_output_region;
Derek Foreman41bdc272014-11-05 13:26:57 -0600108 int version;
Alexander Larsson355748e2013-05-28 16:23:38 +0200109
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200110 pixman_region32_init(&old_output_region);
111 pixman_region32_copy(&old_output_region, &output->region);
112
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200113 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200114 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200115
116 pixman_region32_init(&output->previous_damage);
117 pixman_region32_init_rect(&output->region, output->x, output->y,
118 output->width, output->height);
119
120 weston_output_update_matrix(output);
121
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200122 /* If a pointer falls outside the outputs new geometry, move it to its
123 * lower-right corner */
124 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400125 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200126 int32_t x, y;
127
128 if (!pointer)
129 continue;
130
131 x = wl_fixed_to_int(pointer->x);
132 y = wl_fixed_to_int(pointer->y);
133
134 if (!pixman_region32_contains_point(&old_output_region,
135 x, y, NULL) ||
136 pixman_region32_contains_point(&output->region,
137 x, y, NULL))
138 continue;
139
140 if (x >= output->x + output->width)
141 x = output->x + output->width - 1;
142 if (y >= output->y + output->height)
143 y = output->y + output->height - 1;
144
145 pointer->x = wl_fixed_from_int(x);
146 pointer->y = wl_fixed_from_int(y);
147 }
148
149 pixman_region32_fini(&old_output_region);
150
Derek Foremandd4cd332014-11-10 10:29:59 -0600151 if (!mode_changed && !scale_changed)
152 return;
153
Hardening57388e42013-09-18 23:56:36 +0200154 /* notify clients of the changes */
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600155 wl_resource_for_each(resource, &output->resource_list) {
156 if (mode_changed) {
157 wl_output_send_mode(resource,
158 output->current_mode->flags,
159 output->current_mode->width,
160 output->current_mode->height,
161 output->current_mode->refresh);
162 }
Hardening57388e42013-09-18 23:56:36 +0200163
Derek Foreman41bdc272014-11-05 13:26:57 -0600164 version = wl_resource_get_version(resource);
165 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION && scale_changed)
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600166 wl_output_send_scale(resource, output->current_scale);
Hardening57388e42013-09-18 23:56:36 +0200167
Derek Foreman41bdc272014-11-05 13:26:57 -0600168 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
169 wl_output_send_done(resource);
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600170 }
171}
172
173WL_EXPORT int
174weston_output_mode_set_native(struct weston_output *output,
175 struct weston_mode *mode,
176 int32_t scale)
177{
178 int ret;
179 int mode_changed = 0, scale_changed = 0;
180
181 if (!output->switch_mode)
182 return -1;
183
184 if (!output->original_mode) {
185 mode_changed = 1;
186 ret = output->switch_mode(output, mode);
187 if (ret < 0)
188 return ret;
189 if (output->current_scale != scale) {
190 scale_changed = 1;
191 output->current_scale = scale;
Hardening57388e42013-09-18 23:56:36 +0200192 }
193 }
194
Derek Foreman6ae7bc92014-11-04 10:47:33 -0600195 output->native_mode = mode;
196 output->native_scale = scale;
197
198 weston_mode_switch_finish(output, mode_changed, scale_changed);
199
200 return 0;
201}
202
203WL_EXPORT int
204weston_output_mode_switch_to_native(struct weston_output *output)
205{
206 int ret;
207 int mode_changed = 0, scale_changed = 0;
208
209 if (!output->switch_mode)
210 return -1;
211
212 if (!output->original_mode) {
213 weston_log("already in the native mode\n");
214 return -1;
215 }
216 /* the non fullscreen clients haven't seen a mode set since we
217 * switched into a temporary, so we need to notify them if the
218 * mode at that time is different from the native mode now.
219 */
220 mode_changed = (output->original_mode != output->native_mode);
221 scale_changed = (output->original_scale != output->native_scale);
222
223 ret = output->switch_mode(output, output->native_mode);
224 if (ret < 0)
225 return ret;
226
227 output->current_scale = output->native_scale;
228
229 output->original_mode = NULL;
230 output->original_scale = 0;
231
232 weston_mode_switch_finish(output, mode_changed, scale_changed);
233
234 return 0;
235}
236
237WL_EXPORT int
238weston_output_mode_switch_to_temporary(struct weston_output *output,
239 struct weston_mode *mode,
240 int32_t scale)
241{
242 int ret;
243
244 if (!output->switch_mode)
245 return -1;
246
247 /* original_mode is the last mode non full screen clients have seen,
248 * so we shouldn't change it if we already have one set.
249 */
250 if (!output->original_mode) {
251 output->original_mode = output->native_mode;
252 output->original_scale = output->native_scale;
253 }
254 ret = output->switch_mode(output, mode);
255 if (ret < 0)
256 return ret;
257
258 output->current_scale = scale;
259
260 weston_mode_switch_finish(output, 0, 0);
261
262 return 0;
Alex Wu2dda6042012-04-17 17:20:47 +0800263}
264
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400265WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500266weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400267{
268 wl_list_insert(&child_process_list, &process->link);
269}
270
Benjamin Franzke06286262011-05-06 19:12:33 +0200271static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200272child_client_exec(int sockfd, const char *path)
273{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500274 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200275 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200276 sigset_t allsigs;
277
278 /* do not give our signal mask to the new process */
279 sigfillset(&allsigs);
280 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200281
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100282 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
283 if (seteuid(getuid()) == -1) {
284 weston_log("compositor: failed seteuid\n");
285 return;
286 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500287
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500288 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
289 * non-CLOEXEC fd to pass through exec. */
290 clientfd = dup(sockfd);
291 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200292 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500293 return;
294 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200295
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500296 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200297 setenv("WAYLAND_SOCKET", s, 1);
298
299 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200300 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200301 path);
302}
303
304WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500305weston_client_launch(struct weston_compositor *compositor,
306 struct weston_process *proc,
307 const char *path,
308 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200309{
310 int sv[2];
311 pid_t pid;
312 struct wl_client *client;
313
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300314 weston_log("launching '%s'\n", path);
315
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300316 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200317 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200318 "socketpair failed while launching '%s': %m\n",
319 path);
320 return NULL;
321 }
322
323 pid = fork();
324 if (pid == -1) {
325 close(sv[0]);
326 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200327 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200328 "fork failed while launching '%s': %m\n", path);
329 return NULL;
330 }
331
332 if (pid == 0) {
333 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700334 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200335 }
336
337 close(sv[1]);
338
339 client = wl_client_create(compositor->wl_display, sv[0]);
340 if (!client) {
341 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200342 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200343 "wl_client_create failed while launching '%s'.\n",
344 path);
345 return NULL;
346 }
347
348 proc->pid = pid;
349 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500350 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200351
352 return client;
353}
354
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300355struct process_info {
356 struct weston_process proc;
357 char *path;
358};
359
360static void
361process_handle_sigchld(struct weston_process *process, int status)
362{
363 struct process_info *pinfo =
364 container_of(process, struct process_info, proc);
365
366 /*
367 * There are no guarantees whether this runs before or after
368 * the wl_client destructor.
369 */
370
371 if (WIFEXITED(status)) {
372 weston_log("%s exited with status %d\n", pinfo->path,
373 WEXITSTATUS(status));
374 } else if (WIFSIGNALED(status)) {
375 weston_log("%s died on signal %d\n", pinfo->path,
376 WTERMSIG(status));
377 } else {
378 weston_log("%s disappeared\n", pinfo->path);
379 }
380
381 free(pinfo->path);
382 free(pinfo);
383}
384
385WL_EXPORT struct wl_client *
386weston_client_start(struct weston_compositor *compositor, const char *path)
387{
388 struct process_info *pinfo;
389 struct wl_client *client;
390
391 pinfo = zalloc(sizeof *pinfo);
392 if (!pinfo)
393 return NULL;
394
395 pinfo->path = strdup(path);
396 if (!pinfo->path)
397 goto out_free;
398
399 client = weston_client_launch(compositor, &pinfo->proc, path,
400 process_handle_sigchld);
401 if (!client)
402 goto out_str;
403
404 return client;
405
406out_str:
407 free(pinfo->path);
408
409out_free:
410 free(pinfo);
411
412 return NULL;
413}
414
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200415static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300416region_init_infinite(pixman_region32_t *region)
417{
418 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
419 UINT32_MAX, UINT32_MAX);
420}
421
Pekka Paalanene67858b2013-04-25 13:57:42 +0300422static struct weston_subsurface *
423weston_surface_to_subsurface(struct weston_surface *surface);
424
Jason Ekstranda7af7042013-10-12 22:38:11 -0500425WL_EXPORT struct weston_view *
426weston_view_create(struct weston_surface *surface)
427{
428 struct weston_view *view;
429
430 view = calloc(1, sizeof *view);
431 if (view == NULL)
432 return NULL;
433
434 view->surface = surface;
435
Jason Ekstranda7af7042013-10-12 22:38:11 -0500436 /* Assign to surface */
437 wl_list_insert(&surface->views, &view->surface_link);
438
439 wl_signal_init(&view->destroy_signal);
440 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300441 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500442
Xiong Zhang97116532013-10-23 13:58:31 +0800443 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300444 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300445 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500446
447 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300448 pixman_region32_init(&view->transform.masked_boundingbox);
449 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500450
451 view->alpha = 1.0;
452 pixman_region32_init(&view->transform.opaque);
453
454 wl_list_init(&view->geometry.transformation_list);
455 wl_list_insert(&view->geometry.transformation_list,
456 &view->transform.position.link);
457 weston_matrix_init(&view->transform.position.matrix);
458 wl_list_init(&view->geometry.child_list);
459 pixman_region32_init(&view->transform.boundingbox);
460 view->transform.dirty = 1;
461
462 view->output = NULL;
463
464 return view;
465}
466
Jason Ekstrand108865d2014-06-26 10:04:49 -0700467struct weston_frame_callback {
468 struct wl_resource *resource;
469 struct wl_list link;
470};
471
Pekka Paalanen133e4392014-09-23 22:08:46 -0400472struct weston_presentation_feedback {
473 struct wl_resource *resource;
474
475 /* XXX: could use just wl_resource_get_link() instead */
476 struct wl_list link;
477};
478
479static void
480weston_presentation_feedback_discard(
481 struct weston_presentation_feedback *feedback)
482{
483 presentation_feedback_send_discarded(feedback->resource);
484 wl_resource_destroy(feedback->resource);
485}
486
487static void
488weston_presentation_feedback_discard_list(struct wl_list *list)
489{
490 struct weston_presentation_feedback *feedback, *tmp;
491
492 wl_list_for_each_safe(feedback, tmp, list, link)
493 weston_presentation_feedback_discard(feedback);
494}
495
496static void
497weston_presentation_feedback_present(
498 struct weston_presentation_feedback *feedback,
499 struct weston_output *output,
500 uint32_t refresh_nsec,
501 const struct timespec *ts,
502 uint64_t seq)
503{
504 struct wl_client *client = wl_resource_get_client(feedback->resource);
505 struct wl_resource *o;
506 uint64_t secs;
507 uint32_t flags = 0;
508
509 wl_resource_for_each(o, &output->resource_list) {
510 if (wl_resource_get_client(o) != client)
511 continue;
512
513 presentation_feedback_send_sync_output(feedback->resource, o);
514 }
515
516 secs = ts->tv_sec;
517 presentation_feedback_send_presented(feedback->resource,
518 secs >> 32, secs & 0xffffffff,
519 ts->tv_nsec,
520 refresh_nsec,
521 seq >> 32, seq & 0xffffffff,
522 flags);
523 wl_resource_destroy(feedback->resource);
524}
525
526static void
527weston_presentation_feedback_present_list(struct wl_list *list,
528 struct weston_output *output,
529 uint32_t refresh_nsec,
530 const struct timespec *ts,
531 uint64_t seq)
532{
533 struct weston_presentation_feedback *feedback, *tmp;
534
535 wl_list_for_each_safe(feedback, tmp, list, link)
536 weston_presentation_feedback_present(feedback, output,
537 refresh_nsec, ts, seq);
538}
539
Jason Ekstrand7b982072014-05-20 14:33:03 -0500540static void
541surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
542{
543 struct weston_surface_state *state =
544 container_of(listener, struct weston_surface_state,
545 buffer_destroy_listener);
546
547 state->buffer = NULL;
548}
549
550static void
551weston_surface_state_init(struct weston_surface_state *state)
552{
553 state->newly_attached = 0;
554 state->buffer = NULL;
555 state->buffer_destroy_listener.notify =
556 surface_state_handle_buffer_destroy;
557 state->sx = 0;
558 state->sy = 0;
559
560 pixman_region32_init(&state->damage);
561 pixman_region32_init(&state->opaque);
562 region_init_infinite(&state->input);
563
564 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400565 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500566
567 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
568 state->buffer_viewport.buffer.scale = 1;
569 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
570 state->buffer_viewport.surface.width = -1;
571 state->buffer_viewport.changed = 0;
572}
573
574static void
575weston_surface_state_fini(struct weston_surface_state *state)
576{
577 struct weston_frame_callback *cb, *next;
578
579 wl_list_for_each_safe(cb, next,
580 &state->frame_callback_list, link)
581 wl_resource_destroy(cb->resource);
582
Pekka Paalanen133e4392014-09-23 22:08:46 -0400583 weston_presentation_feedback_discard_list(&state->feedback_list);
584
Jason Ekstrand7b982072014-05-20 14:33:03 -0500585 pixman_region32_fini(&state->input);
586 pixman_region32_fini(&state->opaque);
587 pixman_region32_fini(&state->damage);
588
589 if (state->buffer)
590 wl_list_remove(&state->buffer_destroy_listener.link);
591 state->buffer = NULL;
592}
593
594static void
595weston_surface_state_set_buffer(struct weston_surface_state *state,
596 struct weston_buffer *buffer)
597{
598 if (state->buffer == buffer)
599 return;
600
601 if (state->buffer)
602 wl_list_remove(&state->buffer_destroy_listener.link);
603 state->buffer = buffer;
604 if (state->buffer)
605 wl_signal_add(&state->buffer->destroy_signal,
606 &state->buffer_destroy_listener);
607}
608
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500609WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500610weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500611{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500612 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400613
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200614 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400615 if (surface == NULL)
616 return NULL;
617
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500618 wl_signal_init(&surface->destroy_signal);
619
620 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500621
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500622 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200623 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400624
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200625 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
626 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200627 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
628 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500629
630 weston_surface_state_init(&surface->pending);
631
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400632 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200633
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200634 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100635
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400636 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500637 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300638 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400639
Jason Ekstranda7af7042013-10-12 22:38:11 -0500640 wl_list_init(&surface->views);
641
642 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400643 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500644
Pekka Paalanene67858b2013-04-25 13:57:42 +0300645 wl_list_init(&surface->subsurface_list);
646 wl_list_init(&surface->subsurface_list_pending);
647
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400648 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500649}
650
Alex Wu8811bf92012-02-28 18:07:54 +0800651WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500652weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200653 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500654{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100655 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500656}
657
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400658WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500659weston_view_to_global_float(struct weston_view *view,
660 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200661{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500662 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200663 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
664
Jason Ekstranda7af7042013-10-12 22:38:11 -0500665 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200666
667 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200668 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700669 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200670 v.f[3]);
671 *x = 0;
672 *y = 0;
673 return;
674 }
675
676 *x = v.f[0] / v.f[3];
677 *y = v.f[1] / v.f[3];
678 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500679 *x = sx + view->geometry.x;
680 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200681 }
682}
683
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500684WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200685weston_transformed_coord(int width, int height,
686 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200687 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200688 float sx, float sy, float *bx, float *by)
689{
690 switch (transform) {
691 case WL_OUTPUT_TRANSFORM_NORMAL:
692 default:
693 *bx = sx;
694 *by = sy;
695 break;
696 case WL_OUTPUT_TRANSFORM_FLIPPED:
697 *bx = width - sx;
698 *by = sy;
699 break;
700 case WL_OUTPUT_TRANSFORM_90:
701 *bx = height - sy;
702 *by = sx;
703 break;
704 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
705 *bx = height - sy;
706 *by = width - sx;
707 break;
708 case WL_OUTPUT_TRANSFORM_180:
709 *bx = width - sx;
710 *by = height - sy;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
713 *bx = sx;
714 *by = height - sy;
715 break;
716 case WL_OUTPUT_TRANSFORM_270:
717 *bx = sy;
718 *by = width - sx;
719 break;
720 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
721 *bx = sy;
722 *by = sx;
723 break;
724 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200725
726 *bx *= scale;
727 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200728}
729
730WL_EXPORT pixman_box32_t
731weston_transformed_rect(int width, int height,
732 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200733 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200734 pixman_box32_t rect)
735{
736 float x1, x2, y1, y2;
737
738 pixman_box32_t ret;
739
Alexander Larsson4ea95522013-05-22 14:41:37 +0200740 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200741 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200742 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200743 rect.x2, rect.y2, &x2, &y2);
744
745 if (x1 <= x2) {
746 ret.x1 = x1;
747 ret.x2 = x2;
748 } else {
749 ret.x1 = x2;
750 ret.x2 = x1;
751 }
752
753 if (y1 <= y2) {
754 ret.y1 = y1;
755 ret.y2 = y2;
756 } else {
757 ret.y1 = y2;
758 ret.y2 = y1;
759 }
760
761 return ret;
762}
763
764WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500765weston_transformed_region(int width, int height,
766 enum wl_output_transform transform,
767 int32_t scale,
768 pixman_region32_t *src, pixman_region32_t *dest)
769{
770 pixman_box32_t *src_rects, *dest_rects;
771 int nrects, i;
772
773 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
774 if (src != dest)
775 pixman_region32_copy(dest, src);
776 return;
777 }
778
779 src_rects = pixman_region32_rectangles(src, &nrects);
780 dest_rects = malloc(nrects * sizeof(*dest_rects));
781 if (!dest_rects)
782 return;
783
784 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
785 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
786 } else {
787 for (i = 0; i < nrects; i++) {
788 switch (transform) {
789 default:
790 case WL_OUTPUT_TRANSFORM_NORMAL:
791 dest_rects[i].x1 = src_rects[i].x1;
792 dest_rects[i].y1 = src_rects[i].y1;
793 dest_rects[i].x2 = src_rects[i].x2;
794 dest_rects[i].y2 = src_rects[i].y2;
795 break;
796 case WL_OUTPUT_TRANSFORM_90:
797 dest_rects[i].x1 = height - src_rects[i].y2;
798 dest_rects[i].y1 = src_rects[i].x1;
799 dest_rects[i].x2 = height - src_rects[i].y1;
800 dest_rects[i].y2 = src_rects[i].x2;
801 break;
802 case WL_OUTPUT_TRANSFORM_180:
803 dest_rects[i].x1 = width - src_rects[i].x2;
804 dest_rects[i].y1 = height - src_rects[i].y2;
805 dest_rects[i].x2 = width - src_rects[i].x1;
806 dest_rects[i].y2 = height - src_rects[i].y1;
807 break;
808 case WL_OUTPUT_TRANSFORM_270:
809 dest_rects[i].x1 = src_rects[i].y1;
810 dest_rects[i].y1 = width - src_rects[i].x2;
811 dest_rects[i].x2 = src_rects[i].y2;
812 dest_rects[i].y2 = width - src_rects[i].x1;
813 break;
814 case WL_OUTPUT_TRANSFORM_FLIPPED:
815 dest_rects[i].x1 = width - src_rects[i].x2;
816 dest_rects[i].y1 = src_rects[i].y1;
817 dest_rects[i].x2 = width - src_rects[i].x1;
818 dest_rects[i].y2 = src_rects[i].y2;
819 break;
820 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
821 dest_rects[i].x1 = height - src_rects[i].y2;
822 dest_rects[i].y1 = width - src_rects[i].x2;
823 dest_rects[i].x2 = height - src_rects[i].y1;
824 dest_rects[i].y2 = width - src_rects[i].x1;
825 break;
826 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
827 dest_rects[i].x1 = src_rects[i].x1;
828 dest_rects[i].y1 = height - src_rects[i].y2;
829 dest_rects[i].x2 = src_rects[i].x2;
830 dest_rects[i].y2 = height - src_rects[i].y1;
831 break;
832 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
833 dest_rects[i].x1 = src_rects[i].y1;
834 dest_rects[i].y1 = src_rects[i].x1;
835 dest_rects[i].x2 = src_rects[i].y2;
836 dest_rects[i].y2 = src_rects[i].x2;
837 break;
838 }
839 }
840 }
841
842 if (scale != 1) {
843 for (i = 0; i < nrects; i++) {
844 dest_rects[i].x1 *= scale;
845 dest_rects[i].x2 *= scale;
846 dest_rects[i].y1 *= scale;
847 dest_rects[i].y2 *= scale;
848 }
849 }
850
851 pixman_region32_clear(dest);
852 pixman_region32_init_rects(dest, dest_rects, nrects);
853 free(dest_rects);
854}
855
Jonny Lamb74130762013-11-26 18:19:46 +0100856static void
857scaler_surface_to_buffer(struct weston_surface *surface,
858 float sx, float sy, float *bx, float *by)
859{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200860 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200861 double src_width, src_height;
862 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200863
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200864 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
865 if (vp->surface.width == -1) {
866 *bx = sx;
867 *by = sy;
868 return;
869 }
Jonny Lamb74130762013-11-26 18:19:46 +0100870
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200871 src_x = 0.0;
872 src_y = 0.0;
873 src_width = surface->width_from_buffer;
874 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100875 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200876 src_x = wl_fixed_to_double(vp->buffer.src_x);
877 src_y = wl_fixed_to_double(vp->buffer.src_y);
878 src_width = wl_fixed_to_double(vp->buffer.src_width);
879 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100880 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200881
882 *bx = sx * src_width / surface->width + src_x;
883 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100884}
885
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500886WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200887weston_surface_to_buffer_float(struct weston_surface *surface,
888 float sx, float sy, float *bx, float *by)
889{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200890 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
891
Jonny Lamb74130762013-11-26 18:19:46 +0100892 /* first transform coordinates if the scaler is set */
893 scaler_surface_to_buffer(surface, sx, sy, bx, by);
894
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500895 weston_transformed_coord(surface->width_from_buffer,
896 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200897 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100898 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200899}
900
Alexander Larsson4ea95522013-05-22 14:41:37 +0200901WL_EXPORT void
902weston_surface_to_buffer(struct weston_surface *surface,
903 int sx, int sy, int *bx, int *by)
904{
905 float bxf, byf;
906
Jonny Lamb74130762013-11-26 18:19:46 +0100907 weston_surface_to_buffer_float(surface,
908 sx, sy, &bxf, &byf);
909
Alexander Larsson4ea95522013-05-22 14:41:37 +0200910 *bx = floorf(bxf);
911 *by = floorf(byf);
912}
913
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200914WL_EXPORT pixman_box32_t
915weston_surface_to_buffer_rect(struct weston_surface *surface,
916 pixman_box32_t rect)
917{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200918 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100919 float xf, yf;
920
921 /* first transform box coordinates if the scaler is set */
922 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
923 rect.x1 = floorf(xf);
924 rect.y1 = floorf(yf);
925
926 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
927 rect.x2 = floorf(xf);
928 rect.y2 = floorf(yf);
929
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500930 return weston_transformed_rect(surface->width_from_buffer,
931 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200932 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200933 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200934}
935
936WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400938 struct weston_plane *plane)
939{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500940 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400941 return;
942
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 weston_view_damage_below(view);
944 view->plane = plane;
945 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400946}
947
948WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500949weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200950{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500951 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200952
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500953 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300954 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500955 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800956 if (view->plane)
957 pixman_region32_union(&view->plane->damage,
958 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500959 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800960 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200961}
962
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400963static void
964weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
965{
966 uint32_t different = es->output_mask ^ mask;
967 uint32_t entered = mask & different;
968 uint32_t left = es->output_mask & different;
969 struct weston_output *output;
970 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500971 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400972
973 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500974 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400975 return;
976 if (different == 0)
977 return;
978
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500979 client = wl_resource_get_client(es->resource);
980
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400981 wl_list_for_each(output, &es->compositor->output_list, link) {
982 if (1 << output->id & different)
983 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500984 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400985 client);
986 if (resource == NULL)
987 continue;
988 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500989 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400990 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500991 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400992 }
993}
994
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200995
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400996static void
997weston_surface_assign_output(struct weston_surface *es)
998{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500999 struct weston_output *new_output;
1000 struct weston_view *view;
1001 pixman_region32_t region;
1002 uint32_t max, area, mask;
1003 pixman_box32_t *e;
1004
1005 new_output = NULL;
1006 max = 0;
1007 mask = 0;
1008 pixman_region32_init(&region);
1009 wl_list_for_each(view, &es->views, surface_link) {
1010 if (!view->output)
1011 continue;
1012
1013 pixman_region32_intersect(&region, &view->transform.boundingbox,
1014 &view->output->region);
1015
1016 e = pixman_region32_extents(&region);
1017 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1018
1019 mask |= view->output_mask;
1020
1021 if (area >= max) {
1022 new_output = view->output;
1023 max = area;
1024 }
1025 }
1026 pixman_region32_fini(&region);
1027
1028 es->output = new_output;
1029 weston_surface_update_output_mask(es, mask);
1030}
1031
1032static void
1033weston_view_assign_output(struct weston_view *ev)
1034{
1035 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001036 struct weston_output *output, *new_output;
1037 pixman_region32_t region;
1038 uint32_t max, area, mask;
1039 pixman_box32_t *e;
1040
1041 new_output = NULL;
1042 max = 0;
1043 mask = 0;
1044 pixman_region32_init(&region);
1045 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001046 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001047 &output->region);
1048
1049 e = pixman_region32_extents(&region);
1050 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1051
1052 if (area > 0)
1053 mask |= 1 << output->id;
1054
1055 if (area >= max) {
1056 new_output = output;
1057 max = area;
1058 }
1059 }
1060 pixman_region32_fini(&region);
1061
Jason Ekstranda7af7042013-10-12 22:38:11 -05001062 ev->output = new_output;
1063 ev->output_mask = mask;
1064
1065 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001066}
1067
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001068static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001069view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1070 int32_t width, int32_t height,
1071 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001072{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001073 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1074 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001075 int32_t s[4][2] = {
1076 { sx, sy },
1077 { sx, sy + height },
1078 { sx + width, sy },
1079 { sx + width, sy + height }
1080 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001081 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001082 int i;
1083
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001084 if (width == 0 || height == 0) {
1085 /* avoid rounding empty bbox to 1x1 */
1086 pixman_region32_init(bbox);
1087 return;
1088 }
1089
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001090 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001091 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001092 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001093 if (x < min_x)
1094 min_x = x;
1095 if (x > max_x)
1096 max_x = x;
1097 if (y < min_y)
1098 min_y = y;
1099 if (y > max_y)
1100 max_y = y;
1101 }
1102
Pekka Paalanen219b9822012-02-08 15:38:37 +02001103 int_x = floorf(min_x);
1104 int_y = floorf(min_y);
1105 pixman_region32_init_rect(bbox, int_x, int_y,
1106 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001107}
1108
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001109static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001110weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001111{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001112 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001113
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001114 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 view->geometry.x = roundf(view->geometry.x);
1116 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001117
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001118 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001119 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1120 view->transform.position.matrix.d[12] = view->geometry.x;
1121 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001122
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001124
Jason Ekstranda7af7042013-10-12 22:38:11 -05001125 view->transform.inverse = view->transform.position.matrix;
1126 view->transform.inverse.d[12] = -view->geometry.x;
1127 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001128
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 pixman_region32_init_rect(&view->transform.boundingbox,
1130 view->geometry.x,
1131 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001132 view->surface->width,
1133 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001134
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 if (view->alpha == 1.0) {
1136 pixman_region32_copy(&view->transform.opaque,
1137 &view->surface->opaque);
1138 pixman_region32_translate(&view->transform.opaque,
1139 view->geometry.x,
1140 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001141 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001142}
1143
1144static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001145weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001146{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001147 struct weston_view *parent = view->geometry.parent;
1148 struct weston_matrix *matrix = &view->transform.matrix;
1149 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001150 struct weston_transform *tform;
1151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001153
1154 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001155 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1156 view->transform.position.matrix.d[12] = view->geometry.x;
1157 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158
1159 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001161 weston_matrix_multiply(matrix, &tform->matrix);
1162
Pekka Paalanen483243f2013-03-08 14:56:50 +02001163 if (parent)
1164 weston_matrix_multiply(matrix, &parent->transform.matrix);
1165
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001166 if (weston_matrix_invert(inverse, matrix) < 0) {
1167 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001168 weston_log("error: weston_view %p"
1169 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001170 return -1;
1171 }
1172
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001173 view_compute_bbox(view, 0, 0,
1174 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001176
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001177 return 0;
1178}
1179
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001180static struct weston_layer *
1181get_view_layer(struct weston_view *view)
1182{
1183 if (view->parent_view)
1184 return get_view_layer(view->parent_view);
1185 return view->layer_link.layer;
1186}
1187
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001188WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001190{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001192 struct weston_layer *layer;
1193 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001194
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001196 return;
1197
Pekka Paalanen483243f2013-03-08 14:56:50 +02001198 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001199 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001200
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001202
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001204
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 pixman_region32_fini(&view->transform.boundingbox);
1206 pixman_region32_fini(&view->transform.opaque);
1207 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001208
Pekka Paalanencd403622012-01-25 13:37:39 +02001209 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001210 if (view->geometry.transformation_list.next ==
1211 &view->transform.position.link &&
1212 view->geometry.transformation_list.prev ==
1213 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001214 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001215 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001216 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001217 if (weston_view_update_transform_enable(view) < 0)
1218 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001219 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001220
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001221 layer = get_view_layer(view);
1222 if (layer) {
1223 pixman_region32_init_with_extents(&mask, &layer->mask);
1224 pixman_region32_intersect(&view->transform.masked_boundingbox,
1225 &view->transform.boundingbox, &mask);
1226 pixman_region32_intersect(&view->transform.masked_opaque,
1227 &view->transform.opaque, &mask);
1228 pixman_region32_fini(&mask);
1229 }
1230
Jason Ekstranda7af7042013-10-12 22:38:11 -05001231 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001232
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001234
Jason Ekstranda7af7042013-10-12 22:38:11 -05001235 wl_signal_emit(&view->surface->compositor->transform_signal,
1236 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001237}
1238
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001239WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001240weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001241{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001243
1244 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001245 * The invariant: if view->geometry.dirty, then all views
1246 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001247 * Corollary: if not parent->geometry.dirty, then all ancestors
1248 * are not dirty.
1249 */
1250
Jason Ekstranda7af7042013-10-12 22:38:11 -05001251 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001252 return;
1253
Jason Ekstranda7af7042013-10-12 22:38:11 -05001254 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001257 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001258 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001259}
1260
1261WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262weston_view_to_global_fixed(struct weston_view *view,
1263 wl_fixed_t vx, wl_fixed_t vy,
1264 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001265{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001266 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001267
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 weston_view_to_global_float(view,
1269 wl_fixed_to_double(vx),
1270 wl_fixed_to_double(vy),
1271 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001272 *x = wl_fixed_from_double(xf);
1273 *y = wl_fixed_from_double(yf);
1274}
1275
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001276WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277weston_view_from_global_float(struct weston_view *view,
1278 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001279{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001280 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001281 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1282
Jason Ekstranda7af7042013-10-12 22:38:11 -05001283 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001284
1285 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001286 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001288 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 *vx = 0;
1290 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001291 return;
1292 }
1293
Jason Ekstranda7af7042013-10-12 22:38:11 -05001294 *vx = v.f[0] / v.f[3];
1295 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001296 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 *vx = x - view->geometry.x;
1298 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001299 }
1300}
1301
1302WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001303weston_view_from_global_fixed(struct weston_view *view,
1304 wl_fixed_t x, wl_fixed_t y,
1305 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001306{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001307 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001308
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 weston_view_from_global_float(view,
1310 wl_fixed_to_double(x),
1311 wl_fixed_to_double(y),
1312 &vxf, &vyf);
1313 *vx = wl_fixed_from_double(vxf);
1314 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001315}
1316
1317WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001318weston_view_from_global(struct weston_view *view,
1319 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001320{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001321 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001322
Jason Ekstranda7af7042013-10-12 22:38:11 -05001323 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1324 *vx = floorf(vxf);
1325 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001326}
1327
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001328WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001329weston_surface_schedule_repaint(struct weston_surface *surface)
1330{
1331 struct weston_output *output;
1332
1333 wl_list_for_each(output, &surface->compositor->output_list, link)
1334 if (surface->output_mask & (1 << output->id))
1335 weston_output_schedule_repaint(output);
1336}
1337
1338WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001339weston_view_schedule_repaint(struct weston_view *view)
1340{
1341 struct weston_output *output;
1342
1343 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1344 if (view->output_mask & (1 << output->id))
1345 weston_output_schedule_repaint(output);
1346}
1347
1348WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001349weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001350{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001351 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 0, 0, surface->width,
1353 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001354
Kristian Høgsberg98238702012-08-03 16:29:12 -04001355 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001356}
1357
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001358WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001359weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001360{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001361 if (view->geometry.x == x && view->geometry.y == y)
1362 return;
1363
Jason Ekstranda7af7042013-10-12 22:38:11 -05001364 view->geometry.x = x;
1365 view->geometry.y = y;
1366 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001367}
1368
Pekka Paalanen483243f2013-03-08 14:56:50 +02001369static void
1370transform_parent_handle_parent_destroy(struct wl_listener *listener,
1371 void *data)
1372{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001373 struct weston_view *view =
1374 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001375 geometry.parent_destroy_listener);
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001378}
1379
1380WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001381weston_view_set_transform_parent(struct weston_view *view,
1382 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001383{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001384 if (view->geometry.parent) {
1385 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1386 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001387 }
1388
Jason Ekstranda7af7042013-10-12 22:38:11 -05001389 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001390
Jason Ekstranda7af7042013-10-12 22:38:11 -05001391 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001392 transform_parent_handle_parent_destroy;
1393 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001394 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001395 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001396 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001397 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001398 }
1399
Jason Ekstranda7af7042013-10-12 22:38:11 -05001400 weston_view_geometry_dirty(view);
1401}
1402
Derek Foreman280e7dd2014-10-03 13:13:42 -05001403WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001404weston_view_is_mapped(struct weston_view *view)
1405{
1406 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001407 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001408 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001409 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001410}
1411
Derek Foreman280e7dd2014-10-03 13:13:42 -05001412WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001413weston_surface_is_mapped(struct weston_surface *surface)
1414{
1415 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001416 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001417 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001418 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001419}
1420
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001421static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001422surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001423{
1424 struct weston_view *view;
1425
1426 if (surface->width == width && surface->height == height)
1427 return;
1428
1429 surface->width = width;
1430 surface->height = height;
1431
1432 wl_list_for_each(view, &surface->views, surface_link)
1433 weston_view_geometry_dirty(view);
1434}
1435
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001436WL_EXPORT void
1437weston_surface_set_size(struct weston_surface *surface,
1438 int32_t width, int32_t height)
1439{
1440 assert(!surface->resource);
1441 surface_set_size(surface, width, height);
1442}
1443
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001444static int
1445fixed_round_up_to_int(wl_fixed_t f)
1446{
1447 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1448}
1449
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001450static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001451weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001452{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001453 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001454 int32_t width, height;
1455
1456 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001457 surface->width_from_buffer = 0;
1458 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001459 return;
1460 }
1461
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001462 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001463 case WL_OUTPUT_TRANSFORM_90:
1464 case WL_OUTPUT_TRANSFORM_270:
1465 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1466 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001467 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1468 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001469 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001470 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001471 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1472 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001473 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001474 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001475
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001476 surface->width_from_buffer = width;
1477 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001478}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001479
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001480static void
1481weston_surface_update_size(struct weston_surface *surface)
1482{
1483 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1484 int32_t width, height;
1485
1486 width = surface->width_from_buffer;
1487 height = surface->height_from_buffer;
1488
1489 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001490 surface_set_size(surface,
1491 vp->surface.width, vp->surface.height);
1492 return;
1493 }
1494
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001495 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001496 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1497 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1498
1499 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001500 return;
1501 }
1502
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001503 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001504}
1505
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001506WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001507weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001508{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001509 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001510
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001511 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001512
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001513 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001514}
1515
Jason Ekstranda7af7042013-10-12 22:38:11 -05001516WL_EXPORT struct weston_view *
1517weston_compositor_pick_view(struct weston_compositor *compositor,
1518 wl_fixed_t x, wl_fixed_t y,
1519 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001520{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001521 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001522 int ix = wl_fixed_to_int(x);
1523 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001524
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 wl_list_for_each(view, &compositor->view_list, link) {
1526 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001527 if (pixman_region32_contains_point(
1528 &view->transform.masked_boundingbox,
1529 ix, iy, NULL) &&
1530 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001531 wl_fixed_to_int(*vx),
1532 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001533 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001535 }
1536
1537 return NULL;
1538}
1539
1540static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001541weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001542{
Daniel Stone37816df2012-05-16 18:45:18 +01001543 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001544
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001545 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001546 return;
1547
Daniel Stone37816df2012-05-16 18:45:18 +01001548 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001549 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001550}
1551
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001552WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001553weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001554{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001555 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001556
Jason Ekstranda7af7042013-10-12 22:38:11 -05001557 if (!weston_view_is_mapped(view))
1558 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001559
Jason Ekstranda7af7042013-10-12 22:38:11 -05001560 weston_view_damage_below(view);
1561 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001562 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001563 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001564 wl_list_remove(&view->link);
1565 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001566 view->output_mask = 0;
1567 weston_surface_assign_output(view->surface);
1568
1569 if (weston_surface_is_mapped(view->surface))
1570 return;
1571
1572 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1573 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001574 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001575 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001576 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001577 NULL,
1578 wl_fixed_from_int(0),
1579 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001580 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001581 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001582 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001583}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001584
Jason Ekstranda7af7042013-10-12 22:38:11 -05001585WL_EXPORT void
1586weston_surface_unmap(struct weston_surface *surface)
1587{
1588 struct weston_view *view;
1589
1590 wl_list_for_each(view, &surface->views, surface_link)
1591 weston_view_unmap(view);
1592 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001593}
1594
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001595static void
1596weston_surface_reset_pending_buffer(struct weston_surface *surface)
1597{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001598 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001599 surface->pending.sx = 0;
1600 surface->pending.sy = 0;
1601 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001602 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001603}
1604
Jason Ekstranda7af7042013-10-12 22:38:11 -05001605WL_EXPORT void
1606weston_view_destroy(struct weston_view *view)
1607{
1608 wl_signal_emit(&view->destroy_signal, view);
1609
1610 assert(wl_list_empty(&view->geometry.child_list));
1611
1612 if (weston_view_is_mapped(view)) {
1613 weston_view_unmap(view);
1614 weston_compositor_build_view_list(view->surface->compositor);
1615 }
1616
1617 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001618 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001619
1620 pixman_region32_fini(&view->clip);
1621 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001622 pixman_region32_fini(&view->transform.masked_boundingbox);
1623 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624
1625 weston_view_set_transform_parent(view, NULL);
1626
Jason Ekstranda7af7042013-10-12 22:38:11 -05001627 wl_list_remove(&view->surface_link);
1628
1629 free(view);
1630}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001631
1632WL_EXPORT void
1633weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001634{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001635 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001636 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001637
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001638 if (--surface->ref_count > 0)
1639 return;
1640
1641 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1642
Pekka Paalanene67858b2013-04-25 13:57:42 +03001643 assert(wl_list_empty(&surface->subsurface_list_pending));
1644 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001645
Jason Ekstranda7af7042013-10-12 22:38:11 -05001646 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1647 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001648
Jason Ekstrand7b982072014-05-20 14:33:03 -05001649 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001650
Pekka Paalanende685b82012-12-04 15:58:12 +02001651 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001652
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001653 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001654 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001655 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001656
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001657 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001658 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001659
Pekka Paalanen133e4392014-09-23 22:08:46 -04001660 weston_presentation_feedback_discard_list(&surface->feedback_list);
1661
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001662 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001663}
1664
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001665static void
1666destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001667{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001668 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001669
Giulio Camuffo0d379742013-11-15 22:06:15 +01001670 /* Set the resource to NULL, since we don't want to leave a
1671 * dangling pointer if the surface was refcounted and survives
1672 * the weston_surface_destroy() call. */
1673 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001674 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001675}
1676
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001677static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001678weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1679{
1680 struct weston_buffer *buffer =
1681 container_of(listener, struct weston_buffer, destroy_listener);
1682
1683 wl_signal_emit(&buffer->destroy_signal, buffer);
1684 free(buffer);
1685}
1686
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001687WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001688weston_buffer_from_resource(struct wl_resource *resource)
1689{
1690 struct weston_buffer *buffer;
1691 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001692
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001693 listener = wl_resource_get_destroy_listener(resource,
1694 weston_buffer_destroy_handler);
1695
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001696 if (listener)
1697 return container_of(listener, struct weston_buffer,
1698 destroy_listener);
1699
1700 buffer = zalloc(sizeof *buffer);
1701 if (buffer == NULL)
1702 return NULL;
1703
1704 buffer->resource = resource;
1705 wl_signal_init(&buffer->destroy_signal);
1706 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001707 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001708 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001709
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001710 return buffer;
1711}
1712
1713static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001714weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1715 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001716{
Pekka Paalanende685b82012-12-04 15:58:12 +02001717 struct weston_buffer_reference *ref =
1718 container_of(listener, struct weston_buffer_reference,
1719 destroy_listener);
1720
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001721 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001722 ref->buffer = NULL;
1723}
1724
1725WL_EXPORT void
1726weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001727 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001728{
1729 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001730 ref->buffer->busy_count--;
1731 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001732 assert(wl_resource_get_client(ref->buffer->resource));
1733 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001734 WL_BUFFER_RELEASE);
1735 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001736 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001737 }
1738
Pekka Paalanende685b82012-12-04 15:58:12 +02001739 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001740 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001741 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001742 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001743 }
1744
Pekka Paalanende685b82012-12-04 15:58:12 +02001745 ref->buffer = buffer;
1746 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1747}
1748
1749static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001750weston_surface_attach(struct weston_surface *surface,
1751 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001752{
1753 weston_buffer_reference(&surface->buffer_ref, buffer);
1754
Pekka Paalanena6421c42012-12-04 15:58:10 +02001755 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001756 if (weston_surface_is_mapped(surface))
1757 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001758 }
1759
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001760 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001761
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001762 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001763 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001764}
1765
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001766WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001767weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001768{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001769 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001770
1771 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001772 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001773}
1774
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001775WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001776weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001777{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001778 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001779
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001780 pixman_region32_union(&compositor->primary_plane.damage,
1781 &compositor->primary_plane.damage,
1782 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001783 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001784}
1785
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001786static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001787surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001788{
Pekka Paalanende685b82012-12-04 15:58:12 +02001789 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001790 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001791 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001792
Jason Ekstrandef540082014-06-26 10:37:36 -07001793 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794}
1795
1796static void
1797view_accumulate_damage(struct weston_view *view,
1798 pixman_region32_t *opaque)
1799{
1800 pixman_region32_t damage;
1801
1802 pixman_region32_init(&damage);
1803 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001804 pixman_box32_t *extents;
1805
Jason Ekstranda7af7042013-10-12 22:38:11 -05001806 extents = pixman_region32_extents(&view->surface->damage);
1807 view_compute_bbox(view, extents->x1, extents->y1,
1808 extents->x2 - extents->x1,
1809 extents->y2 - extents->y1,
1810 &damage);
1811 pixman_region32_translate(&damage,
1812 -view->plane->x,
1813 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001814 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001815 pixman_region32_copy(&damage, &view->surface->damage);
1816 pixman_region32_translate(&damage,
1817 view->geometry.x - view->plane->x,
1818 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001819 }
1820
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 pixman_region32_subtract(&damage, &damage, opaque);
1822 pixman_region32_union(&view->plane->damage,
1823 &view->plane->damage, &damage);
1824 pixman_region32_fini(&damage);
1825 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001826 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001827}
1828
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001829static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001830compositor_accumulate_damage(struct weston_compositor *ec)
1831{
1832 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001833 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001834 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001835
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001836 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001837
1838 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001839 pixman_region32_copy(&plane->clip, &clip);
1840
1841 pixman_region32_init(&opaque);
1842
Jason Ekstranda7af7042013-10-12 22:38:11 -05001843 wl_list_for_each(ev, &ec->view_list, link) {
1844 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001845 continue;
1846
Jason Ekstranda7af7042013-10-12 22:38:11 -05001847 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001848 }
1849
1850 pixman_region32_union(&clip, &clip, &opaque);
1851 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001852 }
1853
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001854 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001855
Jason Ekstranda7af7042013-10-12 22:38:11 -05001856 wl_list_for_each(ev, &ec->view_list, link)
1857 ev->surface->touched = 0;
1858
1859 wl_list_for_each(ev, &ec->view_list, link) {
1860 if (ev->surface->touched)
1861 continue;
1862 ev->surface->touched = 1;
1863
1864 surface_flush_damage(ev->surface);
1865
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001866 /* Both the renderer and the backend have seen the buffer
1867 * by now. If renderer needs the buffer, it has its own
1868 * reference set. If the backend wants to keep the buffer
1869 * around for migrating the surface into a non-primary plane
1870 * later, keep_buffer is true. Otherwise, drop the core
1871 * reference now, and allow early buffer release. This enables
1872 * clients to use single-buffering.
1873 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874 if (!ev->surface->keep_buffer)
1875 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001876 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001877}
1878
1879static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001880surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001881{
1882 struct weston_subsurface *sub;
1883
Pekka Paalanene67858b2013-04-25 13:57:42 +03001884 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001885 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001886 continue;
1887
Jason Ekstranda7af7042013-10-12 22:38:11 -05001888 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1889 wl_list_init(&sub->surface->views);
1890
1891 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001892 }
1893}
1894
1895static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001896surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001897{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001898 struct weston_subsurface *sub;
1899 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001900
Jason Ekstranda7af7042013-10-12 22:38:11 -05001901 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1902 if (sub->surface == surface)
1903 continue;
1904
George Kiagiadakised04d382014-06-13 18:10:26 +02001905 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1906 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001907 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001908 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001909
1910 surface_free_unused_subsurface_views(sub->surface);
1911 }
1912}
1913
1914static void
1915view_list_add_subsurface_view(struct weston_compositor *compositor,
1916 struct weston_subsurface *sub,
1917 struct weston_view *parent)
1918{
1919 struct weston_subsurface *child;
1920 struct weston_view *view = NULL, *iv;
1921
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001922 if (!weston_surface_is_mapped(sub->surface))
1923 return;
1924
Jason Ekstranda7af7042013-10-12 22:38:11 -05001925 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1926 if (iv->geometry.parent == parent) {
1927 view = iv;
1928 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001929 }
1930 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001931
1932 if (view) {
1933 /* Put it back in the surface's list of views */
1934 wl_list_remove(&view->surface_link);
1935 wl_list_insert(&sub->surface->views, &view->surface_link);
1936 } else {
1937 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001938 weston_view_set_position(view,
1939 sub->position.x,
1940 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001941 weston_view_set_transform_parent(view, parent);
1942 }
1943
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001944 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001945 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946
Pekka Paalanenb188e912013-11-19 14:03:35 +02001947 if (wl_list_empty(&sub->surface->subsurface_list)) {
1948 wl_list_insert(compositor->view_list.prev, &view->link);
1949 return;
1950 }
1951
1952 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1953 if (child->surface == sub->surface)
1954 wl_list_insert(compositor->view_list.prev, &view->link);
1955 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001956 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001957 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001958}
1959
1960static void
1961view_list_add(struct weston_compositor *compositor,
1962 struct weston_view *view)
1963{
1964 struct weston_subsurface *sub;
1965
1966 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001967
Pekka Paalanenb188e912013-11-19 14:03:35 +02001968 if (wl_list_empty(&view->surface->subsurface_list)) {
1969 wl_list_insert(compositor->view_list.prev, &view->link);
1970 return;
1971 }
1972
1973 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1974 if (sub->surface == view->surface)
1975 wl_list_insert(compositor->view_list.prev, &view->link);
1976 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001978 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001979}
1980
1981static void
1982weston_compositor_build_view_list(struct weston_compositor *compositor)
1983{
1984 struct weston_view *view;
1985 struct weston_layer *layer;
1986
1987 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001988 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001989 surface_stash_subsurface_views(view->surface);
1990
1991 wl_list_init(&compositor->view_list);
1992 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001993 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001994 view_list_add(compositor, view);
1995 }
1996 }
1997
1998 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001999 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05002000 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002001}
2002
David Herrmann1edf44c2013-10-22 17:11:26 +02002003static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002004weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002005{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002006 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002007 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05002008 struct weston_animation *animation, *next;
2009 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02002010 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002011 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002012 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002013
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002014 if (output->destroying)
2015 return 0;
2016
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002017 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002018 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002019
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002020 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002021 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002022 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002023 wl_list_for_each(ev, &ec->view_list, link)
2024 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002025
Pekka Paalanene67858b2013-04-25 13:57:42 +03002026 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002027 wl_list_for_each(ev, &ec->view_list, link) {
2028 /* Note: This operation is safe to do multiple times on the
2029 * same surface.
2030 */
2031 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002032 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002033 &ev->surface->frame_callback_list);
2034 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002035
2036 wl_list_insert_list(&output->feedback_list,
2037 &ev->surface->feedback_list);
2038 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002039 }
2040 }
2041
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002042 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002043
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002044 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002045 pixman_region32_intersect(&output_damage,
2046 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002047 pixman_region32_subtract(&output_damage,
2048 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002049
Scott Moreauccbf29d2012-02-22 14:21:41 -07002050 if (output->dirty)
2051 weston_output_update_matrix(output);
2052
David Herrmann1edf44c2013-10-22 17:11:26 +02002053 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002054
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002055 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002056
Kristian Høgsbergef044142011-06-21 15:02:12 -04002057 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002058
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002059 weston_compositor_repick(ec);
2060 wl_event_loop_dispatch(ec->input_loop, 0);
2061
Jonas Ådahldb773762012-06-13 00:01:21 +02002062 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002063 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002064 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002065 }
2066
Scott Moreaud64cf212012-06-08 19:40:54 -06002067 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002068 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002069 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002070 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002071
2072 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002073}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002074
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002075static int
2076weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002077{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002078 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002079
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002080 wl_event_loop_dispatch(compositor->input_loop, 0);
2081
2082 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002083}
2084
2085WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002086weston_output_finish_frame(struct weston_output *output,
2087 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002088{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002089 struct weston_compositor *compositor = output->compositor;
2090 struct wl_event_loop *loop =
2091 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002092 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002093 uint32_t refresh_nsec;
2094
2095 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2096 weston_presentation_feedback_present_list(&output->feedback_list,
2097 output, refresh_nsec, stamp,
Pekka Paalanen641307c2014-09-23 22:08:47 -04002098 output->msc);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002099
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002100 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002101
2102 if (output->repaint_needed &&
2103 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2104 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002105 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002106 if (!r)
2107 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002108 }
2109
2110 output->repaint_scheduled = 0;
2111 if (compositor->input_loop_source)
2112 return;
2113
2114 fd = wl_event_loop_get_fd(compositor->input_loop);
2115 compositor->input_loop_source =
2116 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2117 weston_compositor_read_input, compositor);
2118}
2119
2120static void
2121idle_repaint(void *data)
2122{
2123 struct weston_output *output = data;
2124
Jonas Ådahle5a12252013-04-05 23:07:11 +02002125 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002126}
2127
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002128WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002129weston_layer_entry_insert(struct weston_layer_entry *list,
2130 struct weston_layer_entry *entry)
2131{
2132 wl_list_insert(&list->link, &entry->link);
2133 entry->layer = list->layer;
2134}
2135
2136WL_EXPORT void
2137weston_layer_entry_remove(struct weston_layer_entry *entry)
2138{
2139 wl_list_remove(&entry->link);
2140 wl_list_init(&entry->link);
2141 entry->layer = NULL;
2142}
2143
2144WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002145weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2146{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002147 wl_list_init(&layer->view_list.link);
2148 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002149 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002150 if (below != NULL)
2151 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002152}
2153
2154WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002155weston_layer_set_mask(struct weston_layer *layer,
2156 int x, int y, int width, int height)
2157{
2158 struct weston_view *view;
2159
2160 layer->mask.x1 = x;
2161 layer->mask.x2 = x + width;
2162 layer->mask.y1 = y;
2163 layer->mask.y2 = y + height;
2164
2165 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2166 weston_view_geometry_dirty(view);
2167 }
2168}
2169
2170WL_EXPORT void
2171weston_layer_set_mask_infinite(struct weston_layer *layer)
2172{
2173 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2174 UINT32_MAX, UINT32_MAX);
2175}
2176
2177WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002178weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002179{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002180 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002181 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002182
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002183 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2184 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002185 return;
2186
Kristian Høgsbergef044142011-06-21 15:02:12 -04002187 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002188 output->repaint_needed = 1;
2189 if (output->repaint_scheduled)
2190 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002191
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002192 wl_event_loop_add_idle(loop, idle_repaint, output);
2193 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002194
2195 if (compositor->input_loop_source) {
2196 wl_event_source_remove(compositor->input_loop_source);
2197 compositor->input_loop_source = NULL;
2198 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002199}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002200
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002201WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002202weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2203{
2204 struct weston_output *output;
2205
2206 wl_list_for_each(output, &compositor->output_list, link)
2207 weston_output_schedule_repaint(output);
2208}
2209
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002210static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002211surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002212{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002213 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002214}
2215
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002216static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002217surface_attach(struct wl_client *client,
2218 struct wl_resource *resource,
2219 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2220{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002221 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002222 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002223
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002224 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002225 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002226 if (buffer == NULL) {
2227 wl_client_post_no_memory(client);
2228 return;
2229 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002230 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002231
Pekka Paalanende685b82012-12-04 15:58:12 +02002232 /* Attach, attach, without commit in between does not send
2233 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002234 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002235
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002236 surface->pending.sx = sx;
2237 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002238 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002239}
2240
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002241static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002242surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002243 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002244 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002245{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002246 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002247
Pekka Paalanen8e159182012-10-10 12:49:25 +03002248 pixman_region32_union_rect(&surface->pending.damage,
2249 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002250 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002251}
2252
Kristian Høgsberg33418202011-08-16 23:01:28 -04002253static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002254destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002255{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002256 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002257
2258 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002259 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002260}
2261
2262static void
2263surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002264 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002265{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002266 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002267 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002268
2269 cb = malloc(sizeof *cb);
2270 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002271 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002272 return;
2273 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002274
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002275 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2276 callback);
2277 if (cb->resource == NULL) {
2278 free(cb);
2279 wl_resource_post_no_memory(resource);
2280 return;
2281 }
2282
Jason Ekstranda85118c2013-06-27 20:17:02 -05002283 wl_resource_set_implementation(cb->resource, NULL, cb,
2284 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002285
Pekka Paalanenbc106382012-10-10 12:49:31 +03002286 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002287}
2288
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002289static void
2290surface_set_opaque_region(struct wl_client *client,
2291 struct wl_resource *resource,
2292 struct wl_resource *region_resource)
2293{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002294 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002295 struct weston_region *region;
2296
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002297 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002298 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002299 pixman_region32_copy(&surface->pending.opaque,
2300 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002301 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002302 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002303 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002304}
2305
2306static void
2307surface_set_input_region(struct wl_client *client,
2308 struct wl_resource *resource,
2309 struct wl_resource *region_resource)
2310{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002311 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002312 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002313
2314 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002315 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002316 pixman_region32_copy(&surface->pending.input,
2317 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002318 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002319 pixman_region32_fini(&surface->pending.input);
2320 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002321 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002322}
2323
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002324static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002325weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002326{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002327 struct weston_subsurface *sub;
2328
2329 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2330 parent_link_pending) {
2331 wl_list_remove(&sub->parent_link);
2332 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2333 }
2334}
2335
2336static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002337weston_surface_commit_state(struct weston_surface *surface,
2338 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002339{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002340 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002341 pixman_region32_t opaque;
2342
Alexander Larsson4ea95522013-05-22 14:41:37 +02002343 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002344 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002345 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002346 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002347
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002348 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002349 if (state->newly_attached)
2350 weston_surface_attach(surface, state->buffer);
2351 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002352
Jason Ekstrand7b982072014-05-20 14:33:03 -05002353 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002354 weston_surface_update_size(surface);
2355 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002356 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002357 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002358
Jason Ekstrand7b982072014-05-20 14:33:03 -05002359 state->sx = 0;
2360 state->sy = 0;
2361 state->newly_attached = 0;
2362 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002363
2364 /* wl_surface.damage */
2365 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002366 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002367 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002368 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002369 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002370
2371 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002372 pixman_region32_init(&opaque);
2373 pixman_region32_intersect_rect(&opaque, &state->opaque,
2374 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002375
2376 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2377 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002378 wl_list_for_each(view, &surface->views, surface_link)
2379 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002380 }
2381
2382 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002383
2384 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002385 pixman_region32_intersect_rect(&surface->input, &state->input,
2386 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002387
Pekka Paalanenbc106382012-10-10 12:49:31 +03002388 /* wl_surface.frame */
2389 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002390 &state->frame_callback_list);
2391 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002392
2393 /* XXX:
2394 * What should happen with a feedback request, if there
2395 * is no wl_buffer attached for this commit?
2396 */
2397
2398 /* presentation.feedback */
2399 wl_list_insert_list(&surface->feedback_list,
2400 &state->feedback_list);
2401 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002402}
2403
2404static void
2405weston_surface_commit(struct weston_surface *surface)
2406{
2407 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002408
Pekka Paalanene67858b2013-04-25 13:57:42 +03002409 weston_surface_commit_subsurface_order(surface);
2410
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002411 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002412}
2413
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002414static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002415weston_subsurface_commit(struct weston_subsurface *sub);
2416
2417static void
2418weston_subsurface_parent_commit(struct weston_subsurface *sub,
2419 int parent_is_synchronized);
2420
2421static void
2422surface_commit(struct wl_client *client, struct wl_resource *resource)
2423{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002424 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002425 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2426
2427 if (sub) {
2428 weston_subsurface_commit(sub);
2429 return;
2430 }
2431
2432 weston_surface_commit(surface);
2433
2434 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2435 if (sub->surface != surface)
2436 weston_subsurface_parent_commit(sub, 0);
2437 }
2438}
2439
2440static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002441surface_set_buffer_transform(struct wl_client *client,
2442 struct wl_resource *resource, int transform)
2443{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002444 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002445
Jonny Lamba55f1392014-05-30 12:07:15 +02002446 /* if wl_output.transform grows more members this will need to be updated. */
2447 if (transform < 0 ||
2448 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2449 wl_resource_post_error(resource,
2450 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2451 "buffer transform must be a valid transform "
2452 "('%d' specified)", transform);
2453 return;
2454 }
2455
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002456 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002457 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002458}
2459
Alexander Larsson4ea95522013-05-22 14:41:37 +02002460static void
2461surface_set_buffer_scale(struct wl_client *client,
2462 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002463 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002464{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002465 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002466
Jonny Lamba55f1392014-05-30 12:07:15 +02002467 if (scale < 1) {
2468 wl_resource_post_error(resource,
2469 WL_SURFACE_ERROR_INVALID_SCALE,
2470 "buffer scale must be at least one "
2471 "('%d' specified)", scale);
2472 return;
2473 }
2474
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002475 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002476 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002477}
2478
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002479static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002480 surface_destroy,
2481 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002482 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002483 surface_frame,
2484 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002485 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002486 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002487 surface_set_buffer_transform,
2488 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002489};
2490
2491static void
2492compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002493 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002494{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002495 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002496 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002497
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002498 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002499 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002500 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002501 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002502 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002503
Jason Ekstranda85118c2013-06-27 20:17:02 -05002504 surface->resource =
2505 wl_resource_create(client, &wl_surface_interface,
2506 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002507 if (surface->resource == NULL) {
2508 weston_surface_destroy(surface);
2509 wl_resource_post_no_memory(resource);
2510 return;
2511 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002512 wl_resource_set_implementation(surface->resource, &surface_interface,
2513 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002514
2515 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002516}
2517
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002518static void
2519destroy_region(struct wl_resource *resource)
2520{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002521 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002522
2523 pixman_region32_fini(&region->region);
2524 free(region);
2525}
2526
2527static void
2528region_destroy(struct wl_client *client, struct wl_resource *resource)
2529{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002530 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002531}
2532
2533static void
2534region_add(struct wl_client *client, struct wl_resource *resource,
2535 int32_t x, int32_t y, int32_t width, int32_t height)
2536{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002537 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002538
2539 pixman_region32_union_rect(&region->region, &region->region,
2540 x, y, width, height);
2541}
2542
2543static void
2544region_subtract(struct wl_client *client, struct wl_resource *resource,
2545 int32_t x, int32_t y, int32_t width, int32_t height)
2546{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002547 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002548 pixman_region32_t rect;
2549
2550 pixman_region32_init_rect(&rect, x, y, width, height);
2551 pixman_region32_subtract(&region->region, &region->region, &rect);
2552 pixman_region32_fini(&rect);
2553}
2554
2555static const struct wl_region_interface region_interface = {
2556 region_destroy,
2557 region_add,
2558 region_subtract
2559};
2560
2561static void
2562compositor_create_region(struct wl_client *client,
2563 struct wl_resource *resource, uint32_t id)
2564{
2565 struct weston_region *region;
2566
2567 region = malloc(sizeof *region);
2568 if (region == NULL) {
2569 wl_resource_post_no_memory(resource);
2570 return;
2571 }
2572
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002573 pixman_region32_init(&region->region);
2574
Jason Ekstranda85118c2013-06-27 20:17:02 -05002575 region->resource =
2576 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002577 if (region->resource == NULL) {
2578 free(region);
2579 wl_resource_post_no_memory(resource);
2580 return;
2581 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002582 wl_resource_set_implementation(region->resource, &region_interface,
2583 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002584}
2585
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002586static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002587 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002588 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002589};
2590
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002591static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2593{
2594 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002595
Jason Ekstrand7b982072014-05-20 14:33:03 -05002596 weston_surface_commit_state(surface, &sub->cached);
2597 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002598
2599 weston_surface_commit_subsurface_order(surface);
2600
2601 weston_surface_schedule_repaint(surface);
2602
Jason Ekstrand7b982072014-05-20 14:33:03 -05002603 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604}
2605
2606static void
2607weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2608{
2609 struct weston_surface *surface = sub->surface;
2610
2611 /*
2612 * If this commit would cause the surface to move by the
2613 * attach(dx, dy) parameters, the old damage region must be
2614 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002615 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002616 */
2617 pixman_region32_translate(&sub->cached.damage,
2618 -surface->pending.sx, -surface->pending.sy);
2619 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2620 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002621 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002622
2623 if (surface->pending.newly_attached) {
2624 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002625 weston_surface_state_set_buffer(&sub->cached,
2626 surface->pending.buffer);
2627 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002628 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002629 weston_presentation_feedback_discard_list(
2630 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002631 }
2632 sub->cached.sx += surface->pending.sx;
2633 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002634
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002635 sub->cached.buffer_viewport.changed |=
2636 surface->pending.buffer_viewport.changed;
2637 sub->cached.buffer_viewport.buffer =
2638 surface->pending.buffer_viewport.buffer;
2639 sub->cached.buffer_viewport.surface =
2640 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002641
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002642 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643
2644 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2645
2646 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2647
2648 wl_list_insert_list(&sub->cached.frame_callback_list,
2649 &surface->pending.frame_callback_list);
2650 wl_list_init(&surface->pending.frame_callback_list);
2651
Pekka Paalanen133e4392014-09-23 22:08:46 -04002652 wl_list_insert_list(&sub->cached.feedback_list,
2653 &surface->pending.feedback_list);
2654 wl_list_init(&surface->pending.feedback_list);
2655
Jason Ekstrand7b982072014-05-20 14:33:03 -05002656 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002657}
2658
Derek Foreman280e7dd2014-10-03 13:13:42 -05002659static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002660weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2661{
2662 while (sub) {
2663 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002664 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665
2666 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002667 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002668
2669 sub = weston_surface_to_subsurface(sub->parent);
2670 }
2671
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002672 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002673}
2674
2675static void
2676weston_subsurface_commit(struct weston_subsurface *sub)
2677{
2678 struct weston_surface *surface = sub->surface;
2679 struct weston_subsurface *tmp;
2680
2681 /* Recursive check for effectively synchronized. */
2682 if (weston_subsurface_is_synchronized(sub)) {
2683 weston_subsurface_commit_to_cache(sub);
2684 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002685 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002686 /* flush accumulated state from cache */
2687 weston_subsurface_commit_to_cache(sub);
2688 weston_subsurface_commit_from_cache(sub);
2689 } else {
2690 weston_surface_commit(surface);
2691 }
2692
2693 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2694 if (tmp->surface != surface)
2695 weston_subsurface_parent_commit(tmp, 0);
2696 }
2697 }
2698}
2699
2700static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002701weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002702{
2703 struct weston_surface *surface = sub->surface;
2704 struct weston_subsurface *tmp;
2705
Pekka Paalanene67858b2013-04-25 13:57:42 +03002706 /* From now on, commit_from_cache the whole sub-tree, regardless of
2707 * the synchronized mode of each child. This sub-surface or some
2708 * of its ancestors were synchronized, so we are synchronized
2709 * all the way down.
2710 */
2711
Jason Ekstrand7b982072014-05-20 14:33:03 -05002712 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002713 weston_subsurface_commit_from_cache(sub);
2714
2715 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2716 if (tmp->surface != surface)
2717 weston_subsurface_parent_commit(tmp, 1);
2718 }
2719}
2720
2721static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002722weston_subsurface_parent_commit(struct weston_subsurface *sub,
2723 int parent_is_synchronized)
2724{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002725 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002726 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002727 wl_list_for_each(view, &sub->surface->views, surface_link)
2728 weston_view_set_position(view,
2729 sub->position.x,
2730 sub->position.y);
2731
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002732 sub->position.set = 0;
2733 }
2734
2735 if (parent_is_synchronized || sub->synchronized)
2736 weston_subsurface_synchronized_commit(sub);
2737}
2738
2739static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002740subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002741{
2742 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002743 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002744
Jason Ekstranda7af7042013-10-12 22:38:11 -05002745 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002746 weston_view_set_position(view,
2747 view->geometry.x + dx,
2748 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002749
2750 /* No need to check parent mappedness, because if parent is not
2751 * mapped, parent is not in a visible layer, so this sub-surface
2752 * will not be drawn either.
2753 */
2754 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002755 struct weston_output *output;
2756
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002757 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758 * because that would call it also for the parent surface,
2759 * which might not be mapped yet. That would lead to
2760 * inconsistent state, where the window could never be
2761 * mapped.
2762 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002763 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002764 * weston_surface_is_mapped() return true, so that when the
2765 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002766 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002767 */
2768 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002769 output = container_of(compositor->output_list.next,
2770 struct weston_output, link);
2771
2772 surface->output = output;
2773 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002774 }
2775}
2776
2777static struct weston_subsurface *
2778weston_surface_to_subsurface(struct weston_surface *surface)
2779{
2780 if (surface->configure == subsurface_configure)
2781 return surface->configure_private;
2782
2783 return NULL;
2784}
2785
Pekka Paalanen01388e22013-04-25 13:57:44 +03002786WL_EXPORT struct weston_surface *
2787weston_surface_get_main_surface(struct weston_surface *surface)
2788{
2789 struct weston_subsurface *sub;
2790
2791 while (surface && (sub = weston_surface_to_subsurface(surface)))
2792 surface = sub->parent;
2793
2794 return surface;
2795}
2796
Pekka Paalanen50b67472014-10-01 15:02:41 +03002797WL_EXPORT int
2798weston_surface_set_role(struct weston_surface *surface,
2799 const char *role_name,
2800 struct wl_resource *error_resource,
2801 uint32_t error_code)
2802{
2803 assert(role_name);
2804
2805 if (surface->role_name == NULL ||
2806 surface->role_name == role_name ||
2807 strcmp(surface->role_name, role_name) == 0) {
2808 surface->role_name = role_name;
2809
2810 return 0;
2811 }
2812
2813 wl_resource_post_error(error_resource, error_code,
2814 "Cannot assign role %s to wl_surface@%d,"
2815 " already has role %s\n",
2816 role_name,
2817 wl_resource_get_id(surface->resource),
2818 surface->role_name);
2819 return -1;
2820}
2821
Pekka Paalanene67858b2013-04-25 13:57:42 +03002822static void
2823subsurface_set_position(struct wl_client *client,
2824 struct wl_resource *resource, int32_t x, int32_t y)
2825{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002826 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002827
2828 if (!sub)
2829 return;
2830
2831 sub->position.x = x;
2832 sub->position.y = y;
2833 sub->position.set = 1;
2834}
2835
2836static struct weston_subsurface *
2837subsurface_from_surface(struct weston_surface *surface)
2838{
2839 struct weston_subsurface *sub;
2840
2841 sub = weston_surface_to_subsurface(surface);
2842 if (sub)
2843 return sub;
2844
2845 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2846 if (sub->surface == surface)
2847 return sub;
2848
2849 return NULL;
2850}
2851
2852static struct weston_subsurface *
2853subsurface_sibling_check(struct weston_subsurface *sub,
2854 struct weston_surface *surface,
2855 const char *request)
2856{
2857 struct weston_subsurface *sibling;
2858
2859 sibling = subsurface_from_surface(surface);
2860
2861 if (!sibling) {
2862 wl_resource_post_error(sub->resource,
2863 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2864 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002865 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002866 return NULL;
2867 }
2868
2869 if (sibling->parent != sub->parent) {
2870 wl_resource_post_error(sub->resource,
2871 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2872 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002873 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002874 return NULL;
2875 }
2876
2877 return sibling;
2878}
2879
2880static void
2881subsurface_place_above(struct wl_client *client,
2882 struct wl_resource *resource,
2883 struct wl_resource *sibling_resource)
2884{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002885 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002886 struct weston_surface *surface =
2887 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002888 struct weston_subsurface *sibling;
2889
2890 if (!sub)
2891 return;
2892
2893 sibling = subsurface_sibling_check(sub, surface, "place_above");
2894 if (!sibling)
2895 return;
2896
2897 wl_list_remove(&sub->parent_link_pending);
2898 wl_list_insert(sibling->parent_link_pending.prev,
2899 &sub->parent_link_pending);
2900}
2901
2902static void
2903subsurface_place_below(struct wl_client *client,
2904 struct wl_resource *resource,
2905 struct wl_resource *sibling_resource)
2906{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002907 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002908 struct weston_surface *surface =
2909 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002910 struct weston_subsurface *sibling;
2911
2912 if (!sub)
2913 return;
2914
2915 sibling = subsurface_sibling_check(sub, surface, "place_below");
2916 if (!sibling)
2917 return;
2918
2919 wl_list_remove(&sub->parent_link_pending);
2920 wl_list_insert(&sibling->parent_link_pending,
2921 &sub->parent_link_pending);
2922}
2923
2924static void
2925subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2926{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002927 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002928
2929 if (sub)
2930 sub->synchronized = 1;
2931}
2932
2933static void
2934subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2935{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002936 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002937
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002938 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002940
2941 /* If sub became effectively desynchronized, flush. */
2942 if (!weston_subsurface_is_synchronized(sub))
2943 weston_subsurface_synchronized_commit(sub);
2944 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002945}
2946
2947static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002948weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2949{
2950 wl_list_remove(&sub->parent_link);
2951 wl_list_remove(&sub->parent_link_pending);
2952 wl_list_remove(&sub->parent_destroy_listener.link);
2953 sub->parent = NULL;
2954}
2955
2956static void
2957weston_subsurface_destroy(struct weston_subsurface *sub);
2958
2959static void
2960subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2961{
2962 struct weston_subsurface *sub =
2963 container_of(listener, struct weston_subsurface,
2964 surface_destroy_listener);
2965 assert(data == &sub->surface->resource);
2966
2967 /* The protocol object (wl_resource) is left inert. */
2968 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002969 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002970
2971 weston_subsurface_destroy(sub);
2972}
2973
2974static void
2975subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2976{
2977 struct weston_subsurface *sub =
2978 container_of(listener, struct weston_subsurface,
2979 parent_destroy_listener);
2980 assert(data == &sub->parent->resource);
2981 assert(sub->surface != sub->parent);
2982
2983 if (weston_surface_is_mapped(sub->surface))
2984 weston_surface_unmap(sub->surface);
2985
2986 weston_subsurface_unlink_parent(sub);
2987}
2988
2989static void
2990subsurface_resource_destroy(struct wl_resource *resource)
2991{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002992 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002993
2994 if (sub)
2995 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002996}
2997
2998static void
2999subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3000{
3001 wl_resource_destroy(resource);
3002}
3003
3004static void
3005weston_subsurface_link_parent(struct weston_subsurface *sub,
3006 struct weston_surface *parent)
3007{
3008 sub->parent = parent;
3009 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003010 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003011 &sub->parent_destroy_listener);
3012
3013 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3014 wl_list_insert(&parent->subsurface_list_pending,
3015 &sub->parent_link_pending);
3016}
3017
3018static void
3019weston_subsurface_link_surface(struct weston_subsurface *sub,
3020 struct weston_surface *surface)
3021{
3022 sub->surface = surface;
3023 sub->surface_destroy_listener.notify =
3024 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003025 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003026 &sub->surface_destroy_listener);
3027}
3028
3029static void
3030weston_subsurface_destroy(struct weston_subsurface *sub)
3031{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003032 struct weston_view *view, *next;
3033
Pekka Paalanene67858b2013-04-25 13:57:42 +03003034 assert(sub->surface);
3035
3036 if (sub->resource) {
3037 assert(weston_surface_to_subsurface(sub->surface) == sub);
3038 assert(sub->parent_destroy_listener.notify ==
3039 subsurface_handle_parent_destroy);
3040
George Kiagiadakised04d382014-06-13 18:10:26 +02003041 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3042 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003043 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003044 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045
Pekka Paalanene67858b2013-04-25 13:57:42 +03003046 if (sub->parent)
3047 weston_subsurface_unlink_parent(sub);
3048
Jason Ekstrand7b982072014-05-20 14:33:03 -05003049 weston_surface_state_fini(&sub->cached);
3050 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003051
3052 sub->surface->configure = NULL;
3053 sub->surface->configure_private = NULL;
3054 } else {
3055 /* the dummy weston_subsurface for the parent itself */
3056 assert(sub->parent_destroy_listener.notify == NULL);
3057 wl_list_remove(&sub->parent_link);
3058 wl_list_remove(&sub->parent_link_pending);
3059 }
3060
3061 wl_list_remove(&sub->surface_destroy_listener.link);
3062 free(sub);
3063}
3064
3065static const struct wl_subsurface_interface subsurface_implementation = {
3066 subsurface_destroy,
3067 subsurface_set_position,
3068 subsurface_place_above,
3069 subsurface_place_below,
3070 subsurface_set_sync,
3071 subsurface_set_desync
3072};
3073
3074static struct weston_subsurface *
3075weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3076 struct weston_surface *parent)
3077{
3078 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003079 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003080
3081 sub = calloc(1, sizeof *sub);
3082 if (!sub)
3083 return NULL;
3084
Jason Ekstranda7af7042013-10-12 22:38:11 -05003085 wl_list_init(&sub->unused_views);
3086
Jason Ekstranda85118c2013-06-27 20:17:02 -05003087 sub->resource =
3088 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089 if (!sub->resource) {
3090 free(sub);
3091 return NULL;
3092 }
3093
Jason Ekstranda85118c2013-06-27 20:17:02 -05003094 wl_resource_set_implementation(sub->resource,
3095 &subsurface_implementation,
3096 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003097 weston_subsurface_link_surface(sub, surface);
3098 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003099 weston_surface_state_init(&sub->cached);
3100 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003101 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003102
3103 return sub;
3104}
3105
3106/* Create a dummy subsurface for having the parent itself in its
3107 * sub-surface lists. Makes stacking order manipulation easy.
3108 */
3109static struct weston_subsurface *
3110weston_subsurface_create_for_parent(struct weston_surface *parent)
3111{
3112 struct weston_subsurface *sub;
3113
3114 sub = calloc(1, sizeof *sub);
3115 if (!sub)
3116 return NULL;
3117
3118 weston_subsurface_link_surface(sub, parent);
3119 sub->parent = parent;
3120 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3121 wl_list_insert(&parent->subsurface_list_pending,
3122 &sub->parent_link_pending);
3123
3124 return sub;
3125}
3126
3127static void
3128subcompositor_get_subsurface(struct wl_client *client,
3129 struct wl_resource *resource,
3130 uint32_t id,
3131 struct wl_resource *surface_resource,
3132 struct wl_resource *parent_resource)
3133{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003134 struct weston_surface *surface =
3135 wl_resource_get_user_data(surface_resource);
3136 struct weston_surface *parent =
3137 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003138 struct weston_subsurface *sub;
3139 static const char where[] = "get_subsurface: wl_subsurface@";
3140
3141 if (surface == parent) {
3142 wl_resource_post_error(resource,
3143 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3144 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003145 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003146 return;
3147 }
3148
3149 if (weston_surface_to_subsurface(surface)) {
3150 wl_resource_post_error(resource,
3151 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3152 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003153 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003154 return;
3155 }
3156
Pekka Paalanen50b67472014-10-01 15:02:41 +03003157 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3158 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003159 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003160
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003161 if (weston_surface_get_main_surface(parent) == surface) {
3162 wl_resource_post_error(resource,
3163 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3164 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003165 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003166 return;
3167 }
3168
Pekka Paalanene67858b2013-04-25 13:57:42 +03003169 /* make sure the parent is in its own list */
3170 if (wl_list_empty(&parent->subsurface_list)) {
3171 if (!weston_subsurface_create_for_parent(parent)) {
3172 wl_resource_post_no_memory(resource);
3173 return;
3174 }
3175 }
3176
3177 sub = weston_subsurface_create(id, surface, parent);
3178 if (!sub) {
3179 wl_resource_post_no_memory(resource);
3180 return;
3181 }
3182
3183 surface->configure = subsurface_configure;
3184 surface->configure_private = sub;
3185}
3186
3187static void
3188subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3189{
3190 wl_resource_destroy(resource);
3191}
3192
3193static const struct wl_subcompositor_interface subcompositor_interface = {
3194 subcompositor_destroy,
3195 subcompositor_get_subsurface
3196};
3197
3198static void
3199bind_subcompositor(struct wl_client *client,
3200 void *data, uint32_t version, uint32_t id)
3201{
3202 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003203 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003204
Jason Ekstranda85118c2013-06-27 20:17:02 -05003205 resource =
3206 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003207 if (resource == NULL) {
3208 wl_client_post_no_memory(client);
3209 return;
3210 }
3211 wl_resource_set_implementation(resource, &subcompositor_interface,
3212 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003213}
3214
3215static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003216weston_compositor_dpms(struct weston_compositor *compositor,
3217 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003218{
3219 struct weston_output *output;
3220
3221 wl_list_for_each(output, &compositor->output_list, link)
3222 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003223 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003224}
3225
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003226WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003227weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003228{
Neil Roberts8b62e202013-09-30 13:14:47 +01003229 uint32_t old_state = compositor->state;
3230
3231 /* The state needs to be changed before emitting the wake
3232 * signal because that may try to schedule a repaint which
3233 * will not work if the compositor is still sleeping */
3234 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3235
3236 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003237 case WESTON_COMPOSITOR_SLEEPING:
3238 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3239 /* fall through */
3240 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003241 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003242 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003243 /* fall through */
3244 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003245 wl_event_source_timer_update(compositor->idle_source,
3246 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003247 }
3248}
3249
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003250WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003251weston_compositor_offscreen(struct weston_compositor *compositor)
3252{
3253 switch (compositor->state) {
3254 case WESTON_COMPOSITOR_OFFSCREEN:
3255 return;
3256 case WESTON_COMPOSITOR_SLEEPING:
3257 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3258 /* fall through */
3259 default:
3260 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3261 wl_event_source_timer_update(compositor->idle_source, 0);
3262 }
3263}
3264
3265WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003266weston_compositor_sleep(struct weston_compositor *compositor)
3267{
3268 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3269 return;
3270
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003271 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003272 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3273 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3274}
3275
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003276static int
3277idle_handler(void *data)
3278{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003279 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003280
3281 if (compositor->idle_inhibit)
3282 return 1;
3283
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003284 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003285 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003286
3287 return 1;
3288}
3289
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003290WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003291weston_plane_init(struct weston_plane *plane,
3292 struct weston_compositor *ec,
3293 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003294{
3295 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003296 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003297 plane->x = x;
3298 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003299 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003300
3301 /* Init the link so that the call to wl_list_remove() when releasing
3302 * the plane without ever stacking doesn't lead to a crash */
3303 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003304}
3305
3306WL_EXPORT void
3307weston_plane_release(struct weston_plane *plane)
3308{
Xiong Zhang97116532013-10-23 13:58:31 +08003309 struct weston_view *view;
3310
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003311 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003312 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003313
Xiong Zhang97116532013-10-23 13:58:31 +08003314 wl_list_for_each(view, &plane->compositor->view_list, link) {
3315 if (view->plane == plane)
3316 view->plane = NULL;
3317 }
3318
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003319 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003320}
3321
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003322WL_EXPORT void
3323weston_compositor_stack_plane(struct weston_compositor *ec,
3324 struct weston_plane *plane,
3325 struct weston_plane *above)
3326{
3327 if (above)
3328 wl_list_insert(above->link.prev, &plane->link);
3329 else
3330 wl_list_insert(&ec->plane_list, &plane->link);
3331}
3332
Casey Dahlin9074db52012-04-19 22:50:09 -04003333static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003334{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003335 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003336}
3337
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003338static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003339bind_output(struct wl_client *client,
3340 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003341{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003342 struct weston_output *output = data;
3343 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003344 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003345
Jason Ekstranda85118c2013-06-27 20:17:02 -05003346 resource = wl_resource_create(client, &wl_output_interface,
3347 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003348 if (resource == NULL) {
3349 wl_client_post_no_memory(client);
3350 return;
3351 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003352
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003353 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003354 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003355
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003356 wl_output_send_geometry(resource,
3357 output->x,
3358 output->y,
3359 output->mm_width,
3360 output->mm_height,
3361 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003362 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003363 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003364 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003365 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003366 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003367
3368 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003369 wl_output_send_mode(resource,
3370 mode->flags,
3371 mode->width,
3372 mode->height,
3373 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003374 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003375
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003376 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003377 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003378}
3379
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003380/* Move other outputs when one is removed so the space remains contiguos. */
3381static void
3382weston_compositor_remove_output(struct weston_compositor *compositor,
3383 struct weston_output *remove_output)
3384{
3385 struct weston_output *output;
3386 int offset = 0;
3387
3388 wl_list_for_each(output, &compositor->output_list, link) {
3389 if (output == remove_output) {
3390 offset = output->width;
3391 continue;
3392 }
3393
3394 if (offset > 0) {
3395 weston_output_move(output,
3396 output->x - offset, output->y);
3397 output->dirty = 1;
3398 }
3399 }
3400}
3401
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003402WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003403weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003404{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003405 struct wl_resource *resource;
3406
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003407 output->destroying = 1;
3408
Pekka Paalanen133e4392014-09-23 22:08:46 -04003409 weston_presentation_feedback_discard_list(&output->feedback_list);
3410
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003411 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003412 wl_list_remove(&output->link);
3413
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003414 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003415 wl_signal_emit(&output->destroy_signal, output);
3416
Richard Hughesafe690c2013-05-02 10:10:04 +01003417 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003418 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003419 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003420 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003421
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003422 wl_resource_for_each(resource, &output->resource_list) {
3423 wl_resource_set_destructor(resource, NULL);
3424 }
3425
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003426 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003427}
3428
Scott Moreau1bad5db2012-08-18 01:04:05 -06003429static void
3430weston_output_compute_transform(struct weston_output *output)
3431{
3432 struct weston_matrix transform;
3433 int flip;
3434
3435 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003436 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003437
3438 switch(output->transform) {
3439 case WL_OUTPUT_TRANSFORM_FLIPPED:
3440 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3441 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003443 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003444 flip = -1;
3445 break;
3446 default:
3447 flip = 1;
3448 break;
3449 }
3450
3451 switch(output->transform) {
3452 case WL_OUTPUT_TRANSFORM_NORMAL:
3453 case WL_OUTPUT_TRANSFORM_FLIPPED:
3454 transform.d[0] = flip;
3455 transform.d[1] = 0;
3456 transform.d[4] = 0;
3457 transform.d[5] = 1;
3458 break;
3459 case WL_OUTPUT_TRANSFORM_90:
3460 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3461 transform.d[0] = 0;
3462 transform.d[1] = -flip;
3463 transform.d[4] = 1;
3464 transform.d[5] = 0;
3465 break;
3466 case WL_OUTPUT_TRANSFORM_180:
3467 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3468 transform.d[0] = -flip;
3469 transform.d[1] = 0;
3470 transform.d[4] = 0;
3471 transform.d[5] = -1;
3472 break;
3473 case WL_OUTPUT_TRANSFORM_270:
3474 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3475 transform.d[0] = 0;
3476 transform.d[1] = flip;
3477 transform.d[4] = -1;
3478 transform.d[5] = 0;
3479 break;
3480 default:
3481 break;
3482 }
3483
3484 weston_matrix_multiply(&output->matrix, &transform);
3485}
3486
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003487WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003488weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003489{
Scott Moreau850ca422012-05-21 15:21:25 -06003490 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003491
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003492 weston_matrix_init(&output->matrix);
3493 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003494 -(output->x + output->width / 2.0),
3495 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003496
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003497 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003498 2.0 / output->width,
3499 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003500
Scott Moreauccbf29d2012-02-22 14:21:41 -07003501 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003502 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003503 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003504 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3505 output->zoom.trans_y, 0);
3506 weston_matrix_scale(&output->matrix, magnification,
3507 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003508 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003509
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003510 weston_output_compute_transform(output);
3511
Scott Moreauccbf29d2012-02-22 14:21:41 -07003512 output->dirty = 0;
3513}
3514
Scott Moreau1bad5db2012-08-18 01:04:05 -06003515static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003516weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003517{
3518 output->transform = transform;
3519
3520 switch (transform) {
3521 case WL_OUTPUT_TRANSFORM_90:
3522 case WL_OUTPUT_TRANSFORM_270:
3523 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3524 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3525 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003526 output->width = output->current_mode->height;
3527 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003528 break;
3529 case WL_OUTPUT_TRANSFORM_NORMAL:
3530 case WL_OUTPUT_TRANSFORM_180:
3531 case WL_OUTPUT_TRANSFORM_FLIPPED:
3532 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003533 output->width = output->current_mode->width;
3534 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003535 break;
3536 default:
3537 break;
3538 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003539
Hardening57388e42013-09-18 23:56:36 +02003540 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003541 output->width /= scale;
3542 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003543}
3544
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003545static void
3546weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003547{
3548 output->x = x;
3549 output->y = y;
3550
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003551 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003552 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003553 output->width,
3554 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003555}
3556
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003557WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003558weston_output_move(struct weston_output *output, int x, int y)
3559{
3560 pixman_region32_t old_region;
3561 struct wl_resource *resource;
3562
3563 output->move_x = x - output->x;
3564 output->move_y = y - output->y;
3565
3566 if (output->move_x == 0 && output->move_y == 0)
3567 return;
3568
3569 pixman_region32_init(&old_region);
3570 pixman_region32_copy(&old_region, &output->region);
3571
3572 weston_output_init_geometry(output, x, y);
3573
3574 output->dirty = 1;
3575
3576 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003577 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003578
3579 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003580 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003581 wl_output_send_geometry(resource,
3582 output->x,
3583 output->y,
3584 output->mm_width,
3585 output->mm_height,
3586 output->subpixel,
3587 output->make,
3588 output->model,
3589 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003590
3591 if (wl_resource_get_version(resource) >= 2)
3592 wl_output_send_done(resource);
3593 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003594}
3595
3596WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003597weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003598 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003599 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003600{
3601 output->compositor = c;
3602 output->x = x;
3603 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003604 output->mm_width = mm_width;
3605 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003606 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003607 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003608
Alexander Larsson0b135062013-05-28 16:23:36 +02003609 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003610 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003611
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003612 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003613 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003614
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003615 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003616 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003617 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003618 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003619 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003620
Casey Dahlin58ba1372012-04-19 22:50:08 -04003621 output->id = ffs(~output->compositor->output_id_pool) - 1;
3622 output->compositor->output_id_pool |= 1 << output->id;
3623
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003624 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003625 wl_global_create(c->wl_display, &wl_output_interface, 2,
3626 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003627 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003628}
3629
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003630WL_EXPORT void
3631weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003632 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003633 wl_fixed_t *x, wl_fixed_t *y)
3634{
3635 wl_fixed_t tx, ty;
3636 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003637 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003638
Hardeningff39efa2013-09-18 23:56:35 +02003639 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3640 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003641
3642 switch(output->transform) {
3643 case WL_OUTPUT_TRANSFORM_NORMAL:
3644 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003645 tx = device_x;
3646 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003647 break;
3648 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003649 tx = device_y;
3650 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003651 break;
3652 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003653 tx = width - device_x;
3654 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003655 break;
3656 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003657 tx = width - device_y;
3658 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003659 break;
3660 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003661 tx = width - device_x;
3662 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003663 break;
3664 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003665 tx = width - device_y;
3666 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003667 break;
3668 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003669 tx = device_x;
3670 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003671 break;
3672 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003673 tx = device_y;
3674 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003675 break;
3676 }
3677
Neil Robertseb5a2002014-05-01 16:13:55 +01003678 tx /= output->current_scale;
3679 ty /= output->current_scale;
3680
3681 if (output->zoom.active) {
3682 zoom_scale = output->zoom.spring_z.current;
3683 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3684 output->width / 2.0f *
3685 (zoom_scale + output->zoom.trans_x));
3686 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3687 output->height / 2.0f *
3688 (zoom_scale + output->zoom.trans_y));
3689 tx = wl_fixed_from_double(zx);
3690 ty = wl_fixed_from_double(zy);
3691 }
3692
3693 *x = tx + wl_fixed_from_int(output->x);
3694 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003695}
3696
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003697static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003698destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003699{
Jonny Lamb74130762013-11-26 18:19:46 +01003700 struct weston_surface *surface =
3701 wl_resource_get_user_data(resource);
3702
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003703 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003704 surface->pending.buffer_viewport.buffer.src_width =
3705 wl_fixed_from_int(-1);
3706 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003707 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003708}
3709
3710static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003711viewport_destroy(struct wl_client *client,
3712 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003713{
3714 wl_resource_destroy(resource);
3715}
3716
3717static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003718viewport_set(struct wl_client *client,
3719 struct wl_resource *resource,
3720 wl_fixed_t src_x,
3721 wl_fixed_t src_y,
3722 wl_fixed_t src_width,
3723 wl_fixed_t src_height,
3724 int32_t dst_width,
3725 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003726{
Jonny Lamb74130762013-11-26 18:19:46 +01003727 struct weston_surface *surface =
3728 wl_resource_get_user_data(resource);
3729
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003730 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003731
Jonny Lamb8ae35902013-11-26 18:19:45 +01003732 if (wl_fixed_to_double(src_width) < 0 ||
3733 wl_fixed_to_double(src_height) < 0) {
3734 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003735 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003736 "source dimensions must be non-negative (%fx%f)",
3737 wl_fixed_to_double(src_width),
3738 wl_fixed_to_double(src_height));
3739 return;
3740 }
3741
3742 if (dst_width <= 0 || dst_height <= 0) {
3743 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003744 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003745 "destination dimensions must be positive (%dx%d)",
3746 dst_width, dst_height);
3747 return;
3748 }
3749
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003750 surface->pending.buffer_viewport.buffer.src_x = src_x;
3751 surface->pending.buffer_viewport.buffer.src_y = src_y;
3752 surface->pending.buffer_viewport.buffer.src_width = src_width;
3753 surface->pending.buffer_viewport.buffer.src_height = src_height;
3754 surface->pending.buffer_viewport.surface.width = dst_width;
3755 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003756 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003757}
3758
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003759static void
3760viewport_set_source(struct wl_client *client,
3761 struct wl_resource *resource,
3762 wl_fixed_t src_x,
3763 wl_fixed_t src_y,
3764 wl_fixed_t src_width,
3765 wl_fixed_t src_height)
3766{
3767 struct weston_surface *surface =
3768 wl_resource_get_user_data(resource);
3769
3770 assert(surface->viewport_resource != NULL);
3771
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003772 if (src_width == wl_fixed_from_int(-1) &&
3773 src_height == wl_fixed_from_int(-1)) {
3774 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003775 surface->pending.buffer_viewport.buffer.src_width =
3776 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003777 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003778 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003779 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003780
3781 if (src_width <= 0 || src_height <= 0) {
3782 wl_resource_post_error(resource,
3783 WL_VIEWPORT_ERROR_BAD_VALUE,
3784 "source size must be positive (%fx%f)",
3785 wl_fixed_to_double(src_width),
3786 wl_fixed_to_double(src_height));
3787 return;
3788 }
3789
3790 surface->pending.buffer_viewport.buffer.src_x = src_x;
3791 surface->pending.buffer_viewport.buffer.src_y = src_y;
3792 surface->pending.buffer_viewport.buffer.src_width = src_width;
3793 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003794 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003795}
3796
3797static void
3798viewport_set_destination(struct wl_client *client,
3799 struct wl_resource *resource,
3800 int32_t dst_width,
3801 int32_t dst_height)
3802{
3803 struct weston_surface *surface =
3804 wl_resource_get_user_data(resource);
3805
3806 assert(surface->viewport_resource != NULL);
3807
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003808 if (dst_width == -1 && dst_height == -1) {
3809 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003810 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003811 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003812 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003813 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003814
3815 if (dst_width <= 0 || dst_height <= 0) {
3816 wl_resource_post_error(resource,
3817 WL_VIEWPORT_ERROR_BAD_VALUE,
3818 "destination size must be positive (%dx%d)",
3819 dst_width, dst_height);
3820 return;
3821 }
3822
3823 surface->pending.buffer_viewport.surface.width = dst_width;
3824 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003825 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003826}
3827
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003828static const struct wl_viewport_interface viewport_interface = {
3829 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003830 viewport_set,
3831 viewport_set_source,
3832 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003833};
3834
3835static void
3836scaler_destroy(struct wl_client *client,
3837 struct wl_resource *resource)
3838{
3839 wl_resource_destroy(resource);
3840}
3841
3842static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003843scaler_get_viewport(struct wl_client *client,
3844 struct wl_resource *scaler,
3845 uint32_t id,
3846 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003847{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003848 int version = wl_resource_get_version(scaler);
3849 struct weston_surface *surface =
3850 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003851 struct wl_resource *resource;
3852
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003853 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003854 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003855 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3856 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003857 return;
3858 }
3859
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003860 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003861 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003862 if (resource == NULL) {
3863 wl_client_post_no_memory(client);
3864 return;
3865 }
3866
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003867 wl_resource_set_implementation(resource, &viewport_interface,
3868 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003869
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003870 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003871}
3872
3873static const struct wl_scaler_interface scaler_interface = {
3874 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003875 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003876};
3877
3878static void
3879bind_scaler(struct wl_client *client,
3880 void *data, uint32_t version, uint32_t id)
3881{
3882 struct wl_resource *resource;
3883
3884 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003885 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003886 if (resource == NULL) {
3887 wl_client_post_no_memory(client);
3888 return;
3889 }
3890
3891 wl_resource_set_implementation(resource, &scaler_interface,
3892 NULL, NULL);
3893}
3894
3895static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003896destroy_presentation_feedback(struct wl_resource *feedback_resource)
3897{
3898 struct weston_presentation_feedback *feedback;
3899
3900 feedback = wl_resource_get_user_data(feedback_resource);
3901
3902 wl_list_remove(&feedback->link);
3903 free(feedback);
3904}
3905
3906static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003907presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3908{
3909 wl_resource_destroy(resource);
3910}
3911
3912static void
3913presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003914 struct wl_resource *presentation_resource,
3915 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003916 uint32_t callback)
3917{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003918 struct weston_surface *surface;
3919 struct weston_presentation_feedback *feedback;
3920
3921 surface = wl_resource_get_user_data(surface_resource);
3922
3923 feedback = calloc(1, sizeof *feedback);
3924 if (!feedback)
3925 goto err_calloc;
3926
3927 feedback->resource = wl_resource_create(client,
3928 &presentation_feedback_interface,
3929 1, callback);
3930 if (!feedback->resource)
3931 goto err_create;
3932
3933 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3934 destroy_presentation_feedback);
3935
3936 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3937
3938 return;
3939
3940err_create:
3941 free(feedback);
3942
3943err_calloc:
3944 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003945}
3946
3947static const struct presentation_interface presentation_implementation = {
3948 presentation_destroy,
3949 presentation_feedback
3950};
3951
3952static void
3953bind_presentation(struct wl_client *client,
3954 void *data, uint32_t version, uint32_t id)
3955{
3956 struct weston_compositor *compositor = data;
3957 struct wl_resource *resource;
3958
3959 resource = wl_resource_create(client, &presentation_interface,
3960 MIN(version, 1), id);
3961 if (resource == NULL) {
3962 wl_client_post_no_memory(client);
3963 return;
3964 }
3965
3966 wl_resource_set_implementation(resource, &presentation_implementation,
3967 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003968 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003969}
3970
3971static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003972compositor_bind(struct wl_client *client,
3973 void *data, uint32_t version, uint32_t id)
3974{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003975 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003976 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003977
Jason Ekstranda85118c2013-06-27 20:17:02 -05003978 resource = wl_resource_create(client, &wl_compositor_interface,
3979 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003980 if (resource == NULL) {
3981 wl_client_post_no_memory(client);
3982 return;
3983 }
3984
3985 wl_resource_set_implementation(resource, &compositor_interface,
3986 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003987}
3988
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003989static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003990log_uname(void)
3991{
3992 struct utsname usys;
3993
3994 uname(&usys);
3995
3996 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3997 usys.version, usys.machine);
3998}
3999
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004000WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004001weston_environment_get_fd(const char *env)
4002{
4003 char *e, *end;
4004 int fd, flags;
4005
4006 e = getenv(env);
4007 if (!e)
4008 return -1;
4009 fd = strtol(e, &end, 0);
4010 if (*end != '\0')
4011 return -1;
4012
4013 flags = fcntl(fd, F_GETFD);
4014 if (flags == -1)
4015 return -1;
4016
4017 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4018 unsetenv(env);
4019
4020 return fd;
4021}
4022
4023WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004024weston_compositor_init(struct weston_compositor *ec,
4025 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004026 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004027 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004028{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004029 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004030 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004031 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004032
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004033 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004034 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004035 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004036 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004037 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004038 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004039 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004040 wl_signal_init(&ec->idle_signal);
4041 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004042 wl_signal_init(&ec->show_input_panel_signal);
4043 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004044 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004045 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004046 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004047 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004048 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004049 wl_signal_init(&ec->session_signal);
4050 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004051
Casey Dahlin58ba1372012-04-19 22:50:08 -04004052 ec->output_id_pool = 0;
4053
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004054 if (!wl_global_create(display, &wl_compositor_interface, 3,
4055 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004056 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004057
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004058 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4059 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004060 return -1;
4061
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004062 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004063 ec, bind_scaler))
4064 return -1;
4065
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004066 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4067 ec, bind_presentation))
4068 return -1;
4069
Jason Ekstranda7af7042013-10-12 22:38:11 -05004070 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004071 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004072 wl_list_init(&ec->layer_list);
4073 wl_list_init(&ec->seat_list);
4074 wl_list_init(&ec->output_list);
4075 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004076 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004077 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004078 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004079 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004080 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004081
Xiong Zhang97116532013-10-23 13:58:31 +08004082 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004083 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004084
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004085 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4086 weston_config_section_get_string(s, "keymap_rules",
4087 (char **) &xkb_names.rules, NULL);
4088 weston_config_section_get_string(s, "keymap_model",
4089 (char **) &xkb_names.model, NULL);
4090 weston_config_section_get_string(s, "keymap_layout",
4091 (char **) &xkb_names.layout, NULL);
4092 weston_config_section_get_string(s, "keymap_variant",
4093 (char **) &xkb_names.variant, NULL);
4094 weston_config_section_get_string(s, "keymap_options",
4095 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004096
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004097 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4098 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004099
Jonny Lamb66a41a02014-08-12 14:58:25 +02004100 weston_config_section_get_int(s, "repeat-rate",
4101 &ec->kb_repeat_rate, 40);
4102 weston_config_section_get_int(s, "repeat-delay",
4103 &ec->kb_repeat_delay, 400);
4104
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004105 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004106
4107 wl_data_device_manager_init(ec->wl_display);
4108
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004109 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004110
Daniel Stone725c2c32012-06-22 14:04:36 +01004111 loop = wl_display_get_event_loop(ec->wl_display);
4112 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4113 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4114
4115 ec->input_loop = wl_event_loop_create();
4116
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004117 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4118 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4119
4120 weston_compositor_schedule_repaint(ec);
4121
Daniel Stone725c2c32012-06-22 14:04:36 +01004122 return 0;
4123}
4124
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004125WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004126weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004127{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004128 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004129
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004130 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004131 if (ec->input_loop_source)
4132 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004133
Matt Roper361d2ad2011-08-29 13:52:23 -07004134 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004135 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004136 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004137
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004138 if (ec->renderer)
4139 ec->renderer->destroy(ec);
4140
Daniel Stone325fc2d2012-05-30 16:31:58 +01004141 weston_binding_list_destroy_all(&ec->key_binding_list);
4142 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004143 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004144 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004145 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004146
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004147 weston_plane_release(&ec->primary_plane);
4148
Jonas Ådahlc97af922012-03-28 22:36:09 +02004149 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004150
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004151 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004152}
4153
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004154WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004155weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4156 const struct weston_pointer_grab_interface *interface)
4157{
4158 struct weston_seat *seat;
4159
4160 ec->default_pointer_grab = interface;
4161 wl_list_for_each(seat, &ec->seat_list, link) {
4162 if (seat->pointer) {
4163 weston_pointer_set_default_grab(seat->pointer,
4164 interface);
4165 }
4166 }
4167}
4168
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004169WL_EXPORT int
4170weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4171 clockid_t clk_id)
4172{
4173 struct timespec ts;
4174
4175 if (clock_gettime(clk_id, &ts) < 0)
4176 return -1;
4177
4178 compositor->presentation_clock = clk_id;
4179
4180 return 0;
4181}
4182
4183/*
4184 * For choosing the software clock, when the display hardware or API
4185 * does not expose a compatible presentation timestamp.
4186 */
4187WL_EXPORT int
4188weston_compositor_set_presentation_clock_software(
4189 struct weston_compositor *compositor)
4190{
4191 /* In order of preference */
4192 static const clockid_t clocks[] = {
4193 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4194 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4195 CLOCK_MONOTONIC, /* no jumps, may crawl */
4196 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4197 CLOCK_REALTIME /* may jump and crawl */
4198 };
4199 unsigned i;
4200
4201 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4202 if (weston_compositor_set_presentation_clock(compositor,
4203 clocks[i]) == 0)
4204 return 0;
4205
4206 weston_log("Error: no suitable presentation clock available.\n");
4207
4208 return -1;
4209}
4210
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004211WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004212weston_version(int *major, int *minor, int *micro)
4213{
4214 *major = WESTON_VERSION_MAJOR;
4215 *minor = WESTON_VERSION_MINOR;
4216 *micro = WESTON_VERSION_MICRO;
4217}
4218
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004219static const char *
4220clock_name(clockid_t clk_id)
4221{
4222 static const char *names[] = {
4223 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4224 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4225 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4226 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4227 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4228 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4229 };
4230
4231 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4232 return "unknown";
4233
4234 return names[clk_id];
4235}
4236
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004237static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004238 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004239 const char *desc;
4240} capability_strings[] = {
4241 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004242 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004243};
4244
4245static void
4246weston_compositor_log_capabilities(struct weston_compositor *compositor)
4247{
4248 unsigned i;
4249 int yes;
4250
4251 weston_log("Compositor capabilities:\n");
4252 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4253 yes = compositor->capabilities & capability_strings[i].bit;
4254 weston_log_continue(STAMP_SPACE "%s %s\n",
4255 capability_strings[i].desc,
4256 yes ? "yes" : "no");
4257 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004258
4259 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4260 clock_name(compositor->presentation_clock),
4261 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004262}
4263
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004264static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004265{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004266 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004267
Martin Minarik6d118362012-06-07 18:01:59 +02004268 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004269 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004270
4271 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004272}
4273
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004274#ifdef HAVE_LIBUNWIND
4275
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004276static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004277print_backtrace(void)
4278{
4279 unw_cursor_t cursor;
4280 unw_context_t context;
4281 unw_word_t off;
4282 unw_proc_info_t pip;
4283 int ret, i = 0;
4284 char procname[256];
4285 const char *filename;
4286 Dl_info dlinfo;
4287
4288 pip.unwind_info = NULL;
4289 ret = unw_getcontext(&context);
4290 if (ret) {
4291 weston_log("unw_getcontext: %d\n", ret);
4292 return;
4293 }
4294
4295 ret = unw_init_local(&cursor, &context);
4296 if (ret) {
4297 weston_log("unw_init_local: %d\n", ret);
4298 return;
4299 }
4300
4301 ret = unw_step(&cursor);
4302 while (ret > 0) {
4303 ret = unw_get_proc_info(&cursor, &pip);
4304 if (ret) {
4305 weston_log("unw_get_proc_info: %d\n", ret);
4306 break;
4307 }
4308
4309 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4310 if (ret && ret != -UNW_ENOMEM) {
4311 if (ret != -UNW_EUNSPEC)
4312 weston_log("unw_get_proc_name: %d\n", ret);
4313 procname[0] = '?';
4314 procname[1] = 0;
4315 }
4316
4317 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4318 *dlinfo.dli_fname)
4319 filename = dlinfo.dli_fname;
4320 else
4321 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004322
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004323 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4324 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4325
4326 ret = unw_step(&cursor);
4327 if (ret < 0)
4328 weston_log("unw_step: %d\n", ret);
4329 }
4330}
4331
4332#else
4333
4334static void
4335print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004336{
4337 void *buffer[32];
4338 int i, count;
4339 Dl_info info;
4340
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004341 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4342 for (i = 0; i < count; i++) {
4343 dladdr(buffer[i], &info);
4344 weston_log(" [%016lx] %s (%s)\n",
4345 (long) buffer[i],
4346 info.dli_sname ? info.dli_sname : "--",
4347 info.dli_fname);
4348 }
4349}
4350
4351#endif
4352
4353static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004354on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004355{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004356 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004357 * then call the backend restore function, which will switch
4358 * back to the vt we launched from or ungrab X etc and then
4359 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004360 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004361 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004362 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004363
Peter Maatmane5b42e42013-03-27 22:38:53 +01004364 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004365
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004366 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004367
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004368 segv_compositor->restore(segv_compositor);
4369
4370 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004371}
4372
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004373WL_EXPORT void *
4374weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004375{
4376 char path[PATH_MAX];
4377 void *module, *init;
4378
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004379 if (name == NULL)
4380 return NULL;
4381
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004382 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004383 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004384 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004385 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004386
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004387 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4388 if (module) {
4389 weston_log("Module '%s' already loaded\n", path);
4390 dlclose(module);
4391 return NULL;
4392 }
4393
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004394 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004395 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004396 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004397 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004398 return NULL;
4399 }
4400
4401 init = dlsym(module, entrypoint);
4402 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004403 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004404 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004405 return NULL;
4406 }
4407
4408 return init;
4409}
4410
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004411static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004412load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004413 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004414{
4415 const char *p, *end;
4416 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004417 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004418 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004419
4420 if (modules == NULL)
4421 return 0;
4422
4423 p = modules;
4424 while (*p) {
4425 end = strchrnul(p, ',');
4426 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004427 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004428 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004429 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004430 p = end;
4431 while (*p == ',')
4432 p++;
4433
4434 }
4435
4436 return 0;
4437}
4438
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004439static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004440 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4441
4442static const char xdg_wrong_message[] =
4443 "fatal: environment variable XDG_RUNTIME_DIR\n"
4444 "is set to \"%s\", which is not a directory.\n";
4445
4446static const char xdg_wrong_mode_message[] =
4447 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004448 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4449 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004450
4451static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004452 "Refer to your distribution on how to get it, or\n"
4453 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4454 "on how to implement it.\n";
4455
Martin Minarik37032f82012-06-18 20:15:18 +02004456static void
4457verify_xdg_runtime_dir(void)
4458{
4459 char *dir = getenv("XDG_RUNTIME_DIR");
4460 struct stat s;
4461
4462 if (!dir) {
4463 weston_log(xdg_error_message);
4464 weston_log_continue(xdg_detail_message);
4465 exit(EXIT_FAILURE);
4466 }
4467
4468 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4469 weston_log(xdg_wrong_message, dir);
4470 weston_log_continue(xdg_detail_message);
4471 exit(EXIT_FAILURE);
4472 }
4473
4474 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4475 weston_log(xdg_wrong_mode_message,
4476 dir, s.st_mode & 0777, s.st_uid);
4477 weston_log_continue(xdg_detail_message);
4478 }
4479}
4480
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004481static int
4482usage(int error_code)
4483{
4484 fprintf(stderr,
4485 "Usage: weston [OPTIONS]\n\n"
4486 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4487 "Weston supports multiple backends, and depending on which backend is in use\n"
4488 "different options will be accepted.\n\n"
4489
4490
4491 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004492 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004493 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004494 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4495 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004496 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004497 " -S, --socket=NAME\tName of socket to listen on\n"
4498 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004499 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004500 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004501 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004502 " -h, --help\t\tThis help message\n\n");
4503
4504 fprintf(stderr,
4505 "Options for drm-backend.so:\n\n"
4506 " --connector=ID\tBring up only this connector\n"
4507 " --seat=SEAT\t\tThe seat that weston should run on\n"
4508 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004509 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004510 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4511
4512 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004513 "Options for fbdev-backend.so:\n\n"
4514 " --tty=TTY\t\tThe tty to use\n"
4515 " --device=DEVICE\tThe framebuffer device to use\n\n");
4516
4517 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004518 "Options for x11-backend.so:\n\n"
4519 " --width=WIDTH\t\tWidth of X window\n"
4520 " --height=HEIGHT\tHeight of X window\n"
4521 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004522 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004523 " --output-count=COUNT\tCreate multiple outputs\n"
4524 " --no-input\t\tDont create input devices\n\n");
4525
4526 fprintf(stderr,
4527 "Options for wayland-backend.so:\n\n"
4528 " --width=WIDTH\t\tWidth of Wayland surface\n"
4529 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004530 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004531 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004532 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004533 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004534 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004535 " --display=DISPLAY\tWayland display to connect to\n\n");
4536
Pekka Paalanene31e0532013-05-22 18:03:07 +03004537#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4538 fprintf(stderr,
4539 "Options for rpi-backend.so:\n\n"
4540 " --tty=TTY\t\tThe tty to use\n"
4541 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4542 " --transform=TR\tThe output transformation, TR is one of:\n"
4543 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004544 " --opaque-regions\tEnable support for opaque regions, can be "
4545 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004546 "\n");
4547#endif
4548
Hardeningc077a842013-07-08 00:51:35 +02004549#if defined(BUILD_RDP_COMPOSITOR)
4550 fprintf(stderr,
4551 "Options for rdp-backend.so:\n\n"
4552 " --width=WIDTH\t\tWidth of desktop\n"
4553 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004554 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4555 " --address=ADDR\tThe address to bind\n"
4556 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004557 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004558 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4559 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4560 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4561 "\n");
4562#endif
4563
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004564 exit(error_code);
4565}
4566
Peter Maatmane5b42e42013-03-27 22:38:53 +01004567static void
4568catch_signals(void)
4569{
4570 struct sigaction action;
4571
4572 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4573 action.sa_sigaction = on_caught_signal;
4574 sigemptyset(&action.sa_mask);
4575 sigaction(SIGSEGV, &action, NULL);
4576 sigaction(SIGABRT, &action, NULL);
4577}
4578
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004579static void
4580handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4581{
4582 struct wl_client *client = data;
4583
4584 weston_log("Primary client died. Closing...\n");
4585
4586 wl_display_terminate(wl_client_get_display(client));
4587}
4588
Ryo Munakatad8deff62014-09-06 07:32:05 +09004589static char *
4590weston_choose_default_backend(void)
4591{
4592 char *backend = NULL;
4593
4594 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4595 backend = strdup("wayland-backend.so");
4596 else if (getenv("DISPLAY"))
4597 backend = strdup("x11-backend.so");
4598 else
4599 backend = strdup(WESTON_NATIVE_BACKEND);
4600
4601 return backend;
4602}
4603
4604static int
4605weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4606{
4607 if (socket_name) {
4608 if (wl_display_add_socket(display, socket_name)) {
4609 weston_log("fatal: failed to add socket: %m\n");
4610 return -1;
4611 }
4612 } else {
4613 socket_name = wl_display_add_socket_auto(display);
4614 if (!socket_name) {
4615 weston_log("fatal: failed to add socket: %m\n");
4616 return -1;
4617 }
4618 }
4619
4620 setenv("WAYLAND_DISPLAY", socket_name, 1);
4621
4622 return 0;
4623}
4624
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004625int main(int argc, char *argv[])
4626{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004627 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004628 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004629 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004630 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004631 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004632 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004633 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004634 int *argc, char *argv[],
4635 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004636 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004637 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004638 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004639 char *modules = NULL;
4640 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004641 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004642 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004643 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004644 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004645 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004646 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004647 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004648 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004649 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004650 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004651 struct wl_client *primary_client;
4652 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004653 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004654
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004655 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004656 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4657 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004658 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4659 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004660 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004661 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004662 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004663 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004664 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004665 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004666
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004667 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004668
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004669 if (help)
4670 usage(EXIT_SUCCESS);
4671
Scott Moreau12245142012-08-29 15:15:58 -06004672 if (version) {
4673 printf(PACKAGE_STRING "\n");
4674 return EXIT_SUCCESS;
4675 }
4676
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004677 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004678
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004679 weston_log("%s\n"
4680 STAMP_SPACE "%s\n"
4681 STAMP_SPACE "Bug reports to: %s\n"
4682 STAMP_SPACE "Build: %s\n",
4683 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004684 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004685 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004686
Martin Minarik37032f82012-06-18 20:15:18 +02004687 verify_xdg_runtime_dir();
4688
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004689 display = wl_display_create();
4690
Tiago Vignatti2116b892011-08-08 05:52:59 -07004691 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004692 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4693 display);
4694 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4695 display);
4696 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4697 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004698
4699 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004700 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4701 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004702
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304703 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4704 ret = EXIT_FAILURE;
4705 goto out_signals;
4706 }
4707
Pekka Paalanen588bee12014-05-07 16:26:25 +03004708 if (noconfig == 0)
4709 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004710 if (config != NULL) {
4711 weston_log("Using config file '%s'\n",
4712 weston_config_get_full_path(config));
4713 } else {
4714 weston_log("Starting with no config file.\n");
4715 }
4716 section = weston_config_get_section(config, "core", NULL, NULL);
4717
Ryo Munakata03faed22014-09-06 07:32:51 +09004718 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004719 weston_config_section_get_string(section, "backend", &backend,
4720 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004721 if (!backend)
4722 backend = weston_choose_default_backend();
4723 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004724
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004725 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304726 if (!backend_init) {
4727 ret = EXIT_FAILURE;
4728 goto out_signals;
4729 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004730
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004731 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004732 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004733 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304734 ret = EXIT_FAILURE;
4735 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004736 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004737
Peter Maatmane5b42e42013-03-27 22:38:53 +01004738 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004739 segv_compositor = ec;
4740
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004741 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004742 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004743
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004744 for (i = 1; i < argc; i++)
4745 weston_log("fatal: unhandled option: %s\n", argv[i]);
4746 if (argc > 1) {
4747 ret = EXIT_FAILURE;
4748 goto out;
4749 }
4750
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004751 weston_compositor_log_capabilities(ec);
4752
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004753 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4754 if (server_socket) {
4755 weston_log("Running with single client\n");
4756 fd = strtol(server_socket, &end, 0);
4757 if (*end != '\0')
4758 fd = -1;
4759 } else {
4760 fd = -1;
4761 }
4762
4763 if (fd != -1) {
4764 primary_client = wl_client_create(display, fd);
4765 if (!primary_client) {
4766 weston_log("fatal: failed to add client: %m\n");
4767 ret = EXIT_FAILURE;
4768 goto out;
4769 }
4770 primary_client_destroyed.notify =
4771 handle_primary_client_destroyed;
4772 wl_client_add_destroy_listener(primary_client,
4773 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004774 } else if (weston_create_listening_socket(display, socket_name)) {
4775 ret = EXIT_FAILURE;
4776 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004777 }
4778
Ryo Munakata03faed22014-09-06 07:32:51 +09004779 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004780 weston_config_section_get_string(section, "shell", &shell,
4781 "desktop-shell.so");
4782
Ryo Munakata03faed22014-09-06 07:32:51 +09004783 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004784 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004785
4786 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004787 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004788 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004789
4790 if (load_modules(ec, option_modules, &argc, argv) < 0)
4791 goto out;
4792
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004793 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4794 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4795 if (numlock_on) {
4796 wl_list_for_each(seat, &ec->seat_list, link) {
4797 if (seat->keyboard)
4798 weston_keyboard_set_locks(seat->keyboard,
4799 WESTON_NUM_LOCK,
4800 WESTON_NUM_LOCK);
4801 }
4802 }
4803
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004804 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004805
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004806 wl_display_run(display);
4807
Ryo Munakata03faed22014-09-06 07:32:51 +09004808out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004809 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004810 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004811
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004812 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004813
Daniel Stone855028f2012-05-01 20:37:10 +01004814 weston_compositor_xkb_destroy(ec);
4815
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004816 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304817
4818out_signals:
4819 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4820 if (signals[i])
4821 wl_event_source_remove(signals[i]);
4822
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004823 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004824
Martin Minarik19e6f262012-06-07 13:08:46 +02004825 weston_log_file_close();
4826
Ryo Munakata03faed22014-09-06 07:32:51 +09004827 free(backend);
4828 free(shell);
4829 free(socket_name);
4830 free(option_modules);
4831 free(log);
4832 free(modules);
4833
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004834 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004835}