blob: af9022c5ada26cc8e37443d655734de5afc400c5 [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
Bryce Harringtonde16d892014-11-20 22:21:57 -0800430 view = zalloc(sizeof *view);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500431 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
Jason Ekstranda7af7042013-10-12 22:38:11 -0500443 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300444 pixman_region32_init(&view->transform.masked_boundingbox);
445 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500446
447 view->alpha = 1.0;
448 pixman_region32_init(&view->transform.opaque);
449
450 wl_list_init(&view->geometry.transformation_list);
451 wl_list_insert(&view->geometry.transformation_list,
452 &view->transform.position.link);
453 weston_matrix_init(&view->transform.position.matrix);
454 wl_list_init(&view->geometry.child_list);
455 pixman_region32_init(&view->transform.boundingbox);
456 view->transform.dirty = 1;
457
Jason Ekstranda7af7042013-10-12 22:38:11 -0500458 return view;
459}
460
Jason Ekstrand108865d2014-06-26 10:04:49 -0700461struct weston_frame_callback {
462 struct wl_resource *resource;
463 struct wl_list link;
464};
465
Pekka Paalanen133e4392014-09-23 22:08:46 -0400466struct weston_presentation_feedback {
467 struct wl_resource *resource;
468
469 /* XXX: could use just wl_resource_get_link() instead */
470 struct wl_list link;
471};
472
473static void
474weston_presentation_feedback_discard(
475 struct weston_presentation_feedback *feedback)
476{
477 presentation_feedback_send_discarded(feedback->resource);
478 wl_resource_destroy(feedback->resource);
479}
480
481static void
482weston_presentation_feedback_discard_list(struct wl_list *list)
483{
484 struct weston_presentation_feedback *feedback, *tmp;
485
486 wl_list_for_each_safe(feedback, tmp, list, link)
487 weston_presentation_feedback_discard(feedback);
488}
489
490static void
491weston_presentation_feedback_present(
492 struct weston_presentation_feedback *feedback,
493 struct weston_output *output,
494 uint32_t refresh_nsec,
495 const struct timespec *ts,
496 uint64_t seq)
497{
498 struct wl_client *client = wl_resource_get_client(feedback->resource);
499 struct wl_resource *o;
500 uint64_t secs;
501 uint32_t flags = 0;
502
503 wl_resource_for_each(o, &output->resource_list) {
504 if (wl_resource_get_client(o) != client)
505 continue;
506
507 presentation_feedback_send_sync_output(feedback->resource, o);
508 }
509
510 secs = ts->tv_sec;
511 presentation_feedback_send_presented(feedback->resource,
512 secs >> 32, secs & 0xffffffff,
513 ts->tv_nsec,
514 refresh_nsec,
515 seq >> 32, seq & 0xffffffff,
516 flags);
517 wl_resource_destroy(feedback->resource);
518}
519
520static void
521weston_presentation_feedback_present_list(struct wl_list *list,
522 struct weston_output *output,
523 uint32_t refresh_nsec,
524 const struct timespec *ts,
525 uint64_t seq)
526{
527 struct weston_presentation_feedback *feedback, *tmp;
528
529 wl_list_for_each_safe(feedback, tmp, list, link)
530 weston_presentation_feedback_present(feedback, output,
531 refresh_nsec, ts, seq);
532}
533
Jason Ekstrand7b982072014-05-20 14:33:03 -0500534static void
535surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
536{
537 struct weston_surface_state *state =
538 container_of(listener, struct weston_surface_state,
539 buffer_destroy_listener);
540
541 state->buffer = NULL;
542}
543
544static void
545weston_surface_state_init(struct weston_surface_state *state)
546{
547 state->newly_attached = 0;
548 state->buffer = NULL;
549 state->buffer_destroy_listener.notify =
550 surface_state_handle_buffer_destroy;
551 state->sx = 0;
552 state->sy = 0;
553
554 pixman_region32_init(&state->damage);
555 pixman_region32_init(&state->opaque);
556 region_init_infinite(&state->input);
557
558 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400559 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -0500560
561 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
562 state->buffer_viewport.buffer.scale = 1;
563 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
564 state->buffer_viewport.surface.width = -1;
565 state->buffer_viewport.changed = 0;
566}
567
568static void
569weston_surface_state_fini(struct weston_surface_state *state)
570{
571 struct weston_frame_callback *cb, *next;
572
573 wl_list_for_each_safe(cb, next,
574 &state->frame_callback_list, link)
575 wl_resource_destroy(cb->resource);
576
Pekka Paalanen133e4392014-09-23 22:08:46 -0400577 weston_presentation_feedback_discard_list(&state->feedback_list);
578
Jason Ekstrand7b982072014-05-20 14:33:03 -0500579 pixman_region32_fini(&state->input);
580 pixman_region32_fini(&state->opaque);
581 pixman_region32_fini(&state->damage);
582
583 if (state->buffer)
584 wl_list_remove(&state->buffer_destroy_listener.link);
585 state->buffer = NULL;
586}
587
588static void
589weston_surface_state_set_buffer(struct weston_surface_state *state,
590 struct weston_buffer *buffer)
591{
592 if (state->buffer == buffer)
593 return;
594
595 if (state->buffer)
596 wl_list_remove(&state->buffer_destroy_listener.link);
597 state->buffer = buffer;
598 if (state->buffer)
599 wl_signal_add(&state->buffer->destroy_signal,
600 &state->buffer_destroy_listener);
601}
602
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500603WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500604weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500605{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500606 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400607
Bryce Harringtonde16d892014-11-20 22:21:57 -0800608 surface = zalloc(sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400609 if (surface == NULL)
610 return NULL;
611
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500612 wl_signal_init(&surface->destroy_signal);
613
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500614 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200615 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400616
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200617 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
618 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200619 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
620 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500621
622 weston_surface_state_init(&surface->pending);
623
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400624 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500625 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300626 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400627
Jason Ekstranda7af7042013-10-12 22:38:11 -0500628 wl_list_init(&surface->views);
629
630 wl_list_init(&surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -0400631 wl_list_init(&surface->feedback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500632
Pekka Paalanene67858b2013-04-25 13:57:42 +0300633 wl_list_init(&surface->subsurface_list);
634 wl_list_init(&surface->subsurface_list_pending);
635
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400636 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500637}
638
Alex Wu8811bf92012-02-28 18:07:54 +0800639WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500640weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200641 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500642{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100643 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500644}
645
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400646WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500647weston_view_to_global_float(struct weston_view *view,
648 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200649{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500650 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200651 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
652
Jason Ekstranda7af7042013-10-12 22:38:11 -0500653 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200654
655 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200656 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700657 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200658 v.f[3]);
659 *x = 0;
660 *y = 0;
661 return;
662 }
663
664 *x = v.f[0] / v.f[3];
665 *y = v.f[1] / v.f[3];
666 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500667 *x = sx + view->geometry.x;
668 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200669 }
670}
671
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500672WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200673weston_transformed_coord(int width, int height,
674 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200675 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200676 float sx, float sy, float *bx, float *by)
677{
678 switch (transform) {
679 case WL_OUTPUT_TRANSFORM_NORMAL:
680 default:
681 *bx = sx;
682 *by = sy;
683 break;
684 case WL_OUTPUT_TRANSFORM_FLIPPED:
685 *bx = width - sx;
686 *by = sy;
687 break;
688 case WL_OUTPUT_TRANSFORM_90:
689 *bx = height - sy;
690 *by = sx;
691 break;
692 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
693 *bx = height - sy;
694 *by = width - sx;
695 break;
696 case WL_OUTPUT_TRANSFORM_180:
697 *bx = width - sx;
698 *by = height - sy;
699 break;
700 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
701 *bx = sx;
702 *by = height - sy;
703 break;
704 case WL_OUTPUT_TRANSFORM_270:
705 *bx = sy;
706 *by = width - sx;
707 break;
708 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
709 *bx = sy;
710 *by = sx;
711 break;
712 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200713
714 *bx *= scale;
715 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200716}
717
718WL_EXPORT pixman_box32_t
719weston_transformed_rect(int width, int height,
720 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200721 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200722 pixman_box32_t rect)
723{
724 float x1, x2, y1, y2;
725
726 pixman_box32_t ret;
727
Alexander Larsson4ea95522013-05-22 14:41:37 +0200728 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200729 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200730 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200731 rect.x2, rect.y2, &x2, &y2);
732
733 if (x1 <= x2) {
734 ret.x1 = x1;
735 ret.x2 = x2;
736 } else {
737 ret.x1 = x2;
738 ret.x2 = x1;
739 }
740
741 if (y1 <= y2) {
742 ret.y1 = y1;
743 ret.y2 = y2;
744 } else {
745 ret.y1 = y2;
746 ret.y2 = y1;
747 }
748
749 return ret;
750}
751
752WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500753weston_transformed_region(int width, int height,
754 enum wl_output_transform transform,
755 int32_t scale,
756 pixman_region32_t *src, pixman_region32_t *dest)
757{
758 pixman_box32_t *src_rects, *dest_rects;
759 int nrects, i;
760
761 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
762 if (src != dest)
763 pixman_region32_copy(dest, src);
764 return;
765 }
766
767 src_rects = pixman_region32_rectangles(src, &nrects);
768 dest_rects = malloc(nrects * sizeof(*dest_rects));
769 if (!dest_rects)
770 return;
771
772 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
773 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
774 } else {
775 for (i = 0; i < nrects; i++) {
776 switch (transform) {
777 default:
778 case WL_OUTPUT_TRANSFORM_NORMAL:
779 dest_rects[i].x1 = src_rects[i].x1;
780 dest_rects[i].y1 = src_rects[i].y1;
781 dest_rects[i].x2 = src_rects[i].x2;
782 dest_rects[i].y2 = src_rects[i].y2;
783 break;
784 case WL_OUTPUT_TRANSFORM_90:
785 dest_rects[i].x1 = height - src_rects[i].y2;
786 dest_rects[i].y1 = src_rects[i].x1;
787 dest_rects[i].x2 = height - src_rects[i].y1;
788 dest_rects[i].y2 = src_rects[i].x2;
789 break;
790 case WL_OUTPUT_TRANSFORM_180:
791 dest_rects[i].x1 = width - src_rects[i].x2;
792 dest_rects[i].y1 = height - src_rects[i].y2;
793 dest_rects[i].x2 = width - src_rects[i].x1;
794 dest_rects[i].y2 = height - src_rects[i].y1;
795 break;
796 case WL_OUTPUT_TRANSFORM_270:
797 dest_rects[i].x1 = src_rects[i].y1;
798 dest_rects[i].y1 = width - src_rects[i].x2;
799 dest_rects[i].x2 = src_rects[i].y2;
800 dest_rects[i].y2 = width - src_rects[i].x1;
801 break;
802 case WL_OUTPUT_TRANSFORM_FLIPPED:
803 dest_rects[i].x1 = width - src_rects[i].x2;
804 dest_rects[i].y1 = src_rects[i].y1;
805 dest_rects[i].x2 = width - src_rects[i].x1;
806 dest_rects[i].y2 = src_rects[i].y2;
807 break;
808 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
809 dest_rects[i].x1 = height - src_rects[i].y2;
810 dest_rects[i].y1 = width - src_rects[i].x2;
811 dest_rects[i].x2 = height - src_rects[i].y1;
812 dest_rects[i].y2 = width - src_rects[i].x1;
813 break;
814 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
815 dest_rects[i].x1 = src_rects[i].x1;
816 dest_rects[i].y1 = height - src_rects[i].y2;
817 dest_rects[i].x2 = src_rects[i].x2;
818 dest_rects[i].y2 = height - src_rects[i].y1;
819 break;
820 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
821 dest_rects[i].x1 = src_rects[i].y1;
822 dest_rects[i].y1 = src_rects[i].x1;
823 dest_rects[i].x2 = src_rects[i].y2;
824 dest_rects[i].y2 = src_rects[i].x2;
825 break;
826 }
827 }
828 }
829
830 if (scale != 1) {
831 for (i = 0; i < nrects; i++) {
832 dest_rects[i].x1 *= scale;
833 dest_rects[i].x2 *= scale;
834 dest_rects[i].y1 *= scale;
835 dest_rects[i].y2 *= scale;
836 }
837 }
838
839 pixman_region32_clear(dest);
840 pixman_region32_init_rects(dest, dest_rects, nrects);
841 free(dest_rects);
842}
843
Jonny Lamb74130762013-11-26 18:19:46 +0100844static void
845scaler_surface_to_buffer(struct weston_surface *surface,
846 float sx, float sy, float *bx, float *by)
847{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200848 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200849 double src_width, src_height;
850 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200851
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200852 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
853 if (vp->surface.width == -1) {
854 *bx = sx;
855 *by = sy;
856 return;
857 }
Jonny Lamb74130762013-11-26 18:19:46 +0100858
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200859 src_x = 0.0;
860 src_y = 0.0;
861 src_width = surface->width_from_buffer;
862 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100863 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200864 src_x = wl_fixed_to_double(vp->buffer.src_x);
865 src_y = wl_fixed_to_double(vp->buffer.src_y);
866 src_width = wl_fixed_to_double(vp->buffer.src_width);
867 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100868 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200869
870 *bx = sx * src_width / surface->width + src_x;
871 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100872}
873
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500874WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200875weston_surface_to_buffer_float(struct weston_surface *surface,
876 float sx, float sy, float *bx, float *by)
877{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200878 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
879
Jonny Lamb74130762013-11-26 18:19:46 +0100880 /* first transform coordinates if the scaler is set */
881 scaler_surface_to_buffer(surface, sx, sy, bx, by);
882
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500883 weston_transformed_coord(surface->width_from_buffer,
884 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200885 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100886 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200887}
888
Alexander Larsson4ea95522013-05-22 14:41:37 +0200889WL_EXPORT void
890weston_surface_to_buffer(struct weston_surface *surface,
891 int sx, int sy, int *bx, int *by)
892{
893 float bxf, byf;
894
Jonny Lamb74130762013-11-26 18:19:46 +0100895 weston_surface_to_buffer_float(surface,
896 sx, sy, &bxf, &byf);
897
Alexander Larsson4ea95522013-05-22 14:41:37 +0200898 *bx = floorf(bxf);
899 *by = floorf(byf);
900}
901
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200902WL_EXPORT pixman_box32_t
903weston_surface_to_buffer_rect(struct weston_surface *surface,
904 pixman_box32_t rect)
905{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200906 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100907 float xf, yf;
908
909 /* first transform box coordinates if the scaler is set */
910 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
911 rect.x1 = floorf(xf);
912 rect.y1 = floorf(yf);
913
914 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
915 rect.x2 = floorf(xf);
916 rect.y2 = floorf(yf);
917
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500918 return weston_transformed_rect(surface->width_from_buffer,
919 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200920 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200921 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200922}
923
924WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500925weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400926 struct weston_plane *plane)
927{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500928 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400929 return;
930
Jason Ekstranda7af7042013-10-12 22:38:11 -0500931 weston_view_damage_below(view);
932 view->plane = plane;
933 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400934}
935
936WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500937weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200938{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500939 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200940
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500941 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300942 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500943 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800944 if (view->plane)
945 pixman_region32_union(&view->plane->damage,
946 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500947 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800948 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200949}
950
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400951static void
952weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
953{
954 uint32_t different = es->output_mask ^ mask;
955 uint32_t entered = mask & different;
956 uint32_t left = es->output_mask & different;
957 struct weston_output *output;
958 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500959 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400960
961 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500962 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400963 return;
964 if (different == 0)
965 return;
966
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500967 client = wl_resource_get_client(es->resource);
968
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400969 wl_list_for_each(output, &es->compositor->output_list, link) {
970 if (1 << output->id & different)
971 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500972 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400973 client);
974 if (resource == NULL)
975 continue;
976 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500977 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400978 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500979 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400980 }
981}
982
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200983
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400984static void
985weston_surface_assign_output(struct weston_surface *es)
986{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500987 struct weston_output *new_output;
988 struct weston_view *view;
989 pixman_region32_t region;
990 uint32_t max, area, mask;
991 pixman_box32_t *e;
992
993 new_output = NULL;
994 max = 0;
995 mask = 0;
996 pixman_region32_init(&region);
997 wl_list_for_each(view, &es->views, surface_link) {
998 if (!view->output)
999 continue;
1000
1001 pixman_region32_intersect(&region, &view->transform.boundingbox,
1002 &view->output->region);
1003
1004 e = pixman_region32_extents(&region);
1005 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1006
1007 mask |= view->output_mask;
1008
1009 if (area >= max) {
1010 new_output = view->output;
1011 max = area;
1012 }
1013 }
1014 pixman_region32_fini(&region);
1015
1016 es->output = new_output;
1017 weston_surface_update_output_mask(es, mask);
1018}
1019
1020static void
1021weston_view_assign_output(struct weston_view *ev)
1022{
1023 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001024 struct weston_output *output, *new_output;
1025 pixman_region32_t region;
1026 uint32_t max, area, mask;
1027 pixman_box32_t *e;
1028
1029 new_output = NULL;
1030 max = 0;
1031 mask = 0;
1032 pixman_region32_init(&region);
1033 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001034 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001035 &output->region);
1036
1037 e = pixman_region32_extents(&region);
1038 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1039
1040 if (area > 0)
1041 mask |= 1 << output->id;
1042
1043 if (area >= max) {
1044 new_output = output;
1045 max = area;
1046 }
1047 }
1048 pixman_region32_fini(&region);
1049
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 ev->output = new_output;
1051 ev->output_mask = mask;
1052
1053 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -04001054}
1055
Pekka Paalanen9abf3932012-02-08 14:49:37 +02001056static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001057view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
1058 int32_t width, int32_t height,
1059 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001060{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001061 float min_x = HUGE_VALF, min_y = HUGE_VALF;
1062 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001063 int32_t s[4][2] = {
1064 { sx, sy },
1065 { sx, sy + height },
1066 { sx + width, sy },
1067 { sx + width, sy + height }
1068 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001069 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001070 int i;
1071
Pekka Paalanen7c7d4642012-09-04 13:55:44 +03001072 if (width == 0 || height == 0) {
1073 /* avoid rounding empty bbox to 1x1 */
1074 pixman_region32_init(bbox);
1075 return;
1076 }
1077
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001078 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001079 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001080 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001081 if (x < min_x)
1082 min_x = x;
1083 if (x > max_x)
1084 max_x = x;
1085 if (y < min_y)
1086 min_y = y;
1087 if (y > max_y)
1088 max_y = y;
1089 }
1090
Pekka Paalanen219b9822012-02-08 15:38:37 +02001091 int_x = floorf(min_x);
1092 int_y = floorf(min_y);
1093 pixman_region32_init_rect(bbox, int_x, int_y,
1094 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001095}
1096
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001097static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001098weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001099{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001100 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001101
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001102 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 view->geometry.x = roundf(view->geometry.x);
1104 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001105
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001106 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001107 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1108 view->transform.position.matrix.d[12] = view->geometry.x;
1109 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001110
Jason Ekstranda7af7042013-10-12 22:38:11 -05001111 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001112
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 view->transform.inverse = view->transform.position.matrix;
1114 view->transform.inverse.d[12] = -view->geometry.x;
1115 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001116
Jason Ekstranda7af7042013-10-12 22:38:11 -05001117 pixman_region32_init_rect(&view->transform.boundingbox,
1118 view->geometry.x,
1119 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001120 view->surface->width,
1121 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001122
Jason Ekstranda7af7042013-10-12 22:38:11 -05001123 if (view->alpha == 1.0) {
1124 pixman_region32_copy(&view->transform.opaque,
1125 &view->surface->opaque);
1126 pixman_region32_translate(&view->transform.opaque,
1127 view->geometry.x,
1128 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001129 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001130}
1131
1132static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001134{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001135 struct weston_view *parent = view->geometry.parent;
1136 struct weston_matrix *matrix = &view->transform.matrix;
1137 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001138 struct weston_transform *tform;
1139
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001141
1142 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1144 view->transform.position.matrix.d[12] = view->geometry.x;
1145 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001146
1147 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001148 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001149 weston_matrix_multiply(matrix, &tform->matrix);
1150
Pekka Paalanen483243f2013-03-08 14:56:50 +02001151 if (parent)
1152 weston_matrix_multiply(matrix, &parent->transform.matrix);
1153
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001154 if (weston_matrix_invert(inverse, matrix) < 0) {
1155 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 weston_log("error: weston_view %p"
1157 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001158 return -1;
1159 }
1160
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001161 view_compute_bbox(view, 0, 0,
1162 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001163 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001164
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001165 return 0;
1166}
1167
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001168static struct weston_layer *
1169get_view_layer(struct weston_view *view)
1170{
1171 if (view->parent_view)
1172 return get_view_layer(view->parent_view);
1173 return view->layer_link.layer;
1174}
1175
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001176WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001177weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001178{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001179 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001180 struct weston_layer *layer;
1181 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001182
Jason Ekstranda7af7042013-10-12 22:38:11 -05001183 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001184 return;
1185
Pekka Paalanen483243f2013-03-08 14:56:50 +02001186 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001188
Jason Ekstranda7af7042013-10-12 22:38:11 -05001189 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001190
Jason Ekstranda7af7042013-10-12 22:38:11 -05001191 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001192
Jason Ekstranda7af7042013-10-12 22:38:11 -05001193 pixman_region32_fini(&view->transform.boundingbox);
1194 pixman_region32_fini(&view->transform.opaque);
1195 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001196
Pekka Paalanencd403622012-01-25 13:37:39 +02001197 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001198 if (view->geometry.transformation_list.next ==
1199 &view->transform.position.link &&
1200 view->geometry.transformation_list.prev ==
1201 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001202 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001203 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001204 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 if (weston_view_update_transform_enable(view) < 0)
1206 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001207 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001208
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001209 layer = get_view_layer(view);
1210 if (layer) {
1211 pixman_region32_init_with_extents(&mask, &layer->mask);
1212 pixman_region32_intersect(&view->transform.masked_boundingbox,
1213 &view->transform.boundingbox, &mask);
1214 pixman_region32_intersect(&view->transform.masked_opaque,
1215 &view->transform.opaque, &mask);
1216 pixman_region32_fini(&mask);
1217 }
1218
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001222
Jason Ekstranda7af7042013-10-12 22:38:11 -05001223 wl_signal_emit(&view->surface->compositor->transform_signal,
1224 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001225}
1226
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001227WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001228weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001229{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001230 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001231
1232 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001233 * The invariant: if view->geometry.dirty, then all views
1234 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001235 * Corollary: if not parent->geometry.dirty, then all ancestors
1236 * are not dirty.
1237 */
1238
Jason Ekstranda7af7042013-10-12 22:38:11 -05001239 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001240 return;
1241
Jason Ekstranda7af7042013-10-12 22:38:11 -05001242 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001243
Jason Ekstranda7af7042013-10-12 22:38:11 -05001244 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001245 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001246 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001247}
1248
1249WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250weston_view_to_global_fixed(struct weston_view *view,
1251 wl_fixed_t vx, wl_fixed_t vy,
1252 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001253{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001254 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001255
Jason Ekstranda7af7042013-10-12 22:38:11 -05001256 weston_view_to_global_float(view,
1257 wl_fixed_to_double(vx),
1258 wl_fixed_to_double(vy),
1259 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001260 *x = wl_fixed_from_double(xf);
1261 *y = wl_fixed_from_double(yf);
1262}
1263
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001264WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001265weston_view_from_global_float(struct weston_view *view,
1266 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001267{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001268 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001269 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1270
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001272
1273 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001274 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001276 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001277 *vx = 0;
1278 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001279 return;
1280 }
1281
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 *vx = v.f[0] / v.f[3];
1283 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001284 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001285 *vx = x - view->geometry.x;
1286 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001287 }
1288}
1289
1290WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001291weston_view_from_global_fixed(struct weston_view *view,
1292 wl_fixed_t x, wl_fixed_t y,
1293 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001294{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001296
Jason Ekstranda7af7042013-10-12 22:38:11 -05001297 weston_view_from_global_float(view,
1298 wl_fixed_to_double(x),
1299 wl_fixed_to_double(y),
1300 &vxf, &vyf);
1301 *vx = wl_fixed_from_double(vxf);
1302 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001303}
1304
1305WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001306weston_view_from_global(struct weston_view *view,
1307 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001308{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001309 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001310
Jason Ekstranda7af7042013-10-12 22:38:11 -05001311 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1312 *vx = floorf(vxf);
1313 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001314}
1315
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001316WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001317weston_surface_schedule_repaint(struct weston_surface *surface)
1318{
1319 struct weston_output *output;
1320
1321 wl_list_for_each(output, &surface->compositor->output_list, link)
1322 if (surface->output_mask & (1 << output->id))
1323 weston_output_schedule_repaint(output);
1324}
1325
1326WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001327weston_view_schedule_repaint(struct weston_view *view)
1328{
1329 struct weston_output *output;
1330
1331 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1332 if (view->output_mask & (1 << output->id))
1333 weston_output_schedule_repaint(output);
1334}
1335
1336WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001337weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001338{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001339 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001340 0, 0, surface->width,
1341 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001342
Kristian Høgsberg98238702012-08-03 16:29:12 -04001343 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001344}
1345
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001346WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001347weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001348{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001349 if (view->geometry.x == x && view->geometry.y == y)
1350 return;
1351
Jason Ekstranda7af7042013-10-12 22:38:11 -05001352 view->geometry.x = x;
1353 view->geometry.y = y;
1354 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001355}
1356
Pekka Paalanen483243f2013-03-08 14:56:50 +02001357static void
1358transform_parent_handle_parent_destroy(struct wl_listener *listener,
1359 void *data)
1360{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001361 struct weston_view *view =
1362 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001363 geometry.parent_destroy_listener);
1364
Jason Ekstranda7af7042013-10-12 22:38:11 -05001365 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001366}
1367
1368WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001369weston_view_set_transform_parent(struct weston_view *view,
1370 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001371{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001372 if (view->geometry.parent) {
1373 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1374 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001375 }
1376
Jason Ekstranda7af7042013-10-12 22:38:11 -05001377 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001378
Jason Ekstranda7af7042013-10-12 22:38:11 -05001379 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001380 transform_parent_handle_parent_destroy;
1381 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001382 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001383 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001384 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001385 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001386 }
1387
Jason Ekstranda7af7042013-10-12 22:38:11 -05001388 weston_view_geometry_dirty(view);
1389}
1390
Derek Foreman280e7dd2014-10-03 13:13:42 -05001391WL_EXPORT bool
Jason Ekstranda7af7042013-10-12 22:38:11 -05001392weston_view_is_mapped(struct weston_view *view)
1393{
1394 if (view->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001395 return true;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001396 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001397 return false;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001398}
1399
Derek Foreman280e7dd2014-10-03 13:13:42 -05001400WL_EXPORT bool
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001401weston_surface_is_mapped(struct weston_surface *surface)
1402{
1403 if (surface->output)
Derek Foreman280e7dd2014-10-03 13:13:42 -05001404 return true;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001405 else
Derek Foreman280e7dd2014-10-03 13:13:42 -05001406 return false;
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001407}
1408
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001409static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001410surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001411{
1412 struct weston_view *view;
1413
1414 if (surface->width == width && surface->height == height)
1415 return;
1416
1417 surface->width = width;
1418 surface->height = height;
1419
1420 wl_list_for_each(view, &surface->views, surface_link)
1421 weston_view_geometry_dirty(view);
1422}
1423
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001424WL_EXPORT void
1425weston_surface_set_size(struct weston_surface *surface,
1426 int32_t width, int32_t height)
1427{
1428 assert(!surface->resource);
1429 surface_set_size(surface, width, height);
1430}
1431
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001432static int
1433fixed_round_up_to_int(wl_fixed_t f)
1434{
1435 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1436}
1437
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001438static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001439weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001440{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001441 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001442 int32_t width, height;
1443
1444 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001445 surface->width_from_buffer = 0;
1446 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001447 return;
1448 }
1449
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001450 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001451 case WL_OUTPUT_TRANSFORM_90:
1452 case WL_OUTPUT_TRANSFORM_270:
1453 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1454 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001455 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1456 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001457 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001458 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001459 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1460 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001461 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001462 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001463
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001464 surface->width_from_buffer = width;
1465 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001466}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001467
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001468static void
1469weston_surface_update_size(struct weston_surface *surface)
1470{
1471 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1472 int32_t width, height;
1473
1474 width = surface->width_from_buffer;
1475 height = surface->height_from_buffer;
1476
1477 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001478 surface_set_size(surface,
1479 vp->surface.width, vp->surface.height);
1480 return;
1481 }
1482
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001483 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001484 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1485 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1486
1487 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001488 return;
1489 }
1490
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001491 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001492}
1493
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001494WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001495weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001496{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001497 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001498
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001499 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001500
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001501 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001502}
1503
Jason Ekstranda7af7042013-10-12 22:38:11 -05001504WL_EXPORT struct weston_view *
1505weston_compositor_pick_view(struct weston_compositor *compositor,
1506 wl_fixed_t x, wl_fixed_t y,
1507 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001508{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001509 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001510 int ix = wl_fixed_to_int(x);
1511 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001512
Jason Ekstranda7af7042013-10-12 22:38:11 -05001513 wl_list_for_each(view, &compositor->view_list, link) {
1514 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001515 if (pixman_region32_contains_point(
1516 &view->transform.masked_boundingbox,
1517 ix, iy, NULL) &&
1518 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001519 wl_fixed_to_int(*vx),
1520 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001521 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001523 }
1524
1525 return NULL;
1526}
1527
1528static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001529weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001530{
Daniel Stone37816df2012-05-16 18:45:18 +01001531 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001532
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001533 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001534 return;
1535
Daniel Stone37816df2012-05-16 18:45:18 +01001536 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001537 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001538}
1539
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001540WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001541weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001542{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001543 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001544
Jason Ekstranda7af7042013-10-12 22:38:11 -05001545 if (!weston_view_is_mapped(view))
1546 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001547
Jason Ekstranda7af7042013-10-12 22:38:11 -05001548 weston_view_damage_below(view);
1549 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001550 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001551 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001552 wl_list_remove(&view->link);
1553 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001554 view->output_mask = 0;
1555 weston_surface_assign_output(view->surface);
1556
1557 if (weston_surface_is_mapped(view->surface))
1558 return;
1559
1560 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1561 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001562 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001563 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001564 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001565 NULL,
1566 wl_fixed_from_int(0),
1567 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001568 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001569 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001570 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001571}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001572
Jason Ekstranda7af7042013-10-12 22:38:11 -05001573WL_EXPORT void
1574weston_surface_unmap(struct weston_surface *surface)
1575{
1576 struct weston_view *view;
1577
1578 wl_list_for_each(view, &surface->views, surface_link)
1579 weston_view_unmap(view);
1580 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001581}
1582
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001583static void
1584weston_surface_reset_pending_buffer(struct weston_surface *surface)
1585{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001586 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001587 surface->pending.sx = 0;
1588 surface->pending.sy = 0;
1589 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001590 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001591}
1592
Jason Ekstranda7af7042013-10-12 22:38:11 -05001593WL_EXPORT void
1594weston_view_destroy(struct weston_view *view)
1595{
1596 wl_signal_emit(&view->destroy_signal, view);
1597
1598 assert(wl_list_empty(&view->geometry.child_list));
1599
1600 if (weston_view_is_mapped(view)) {
1601 weston_view_unmap(view);
1602 weston_compositor_build_view_list(view->surface->compositor);
1603 }
1604
1605 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001606 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001607
1608 pixman_region32_fini(&view->clip);
1609 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001610 pixman_region32_fini(&view->transform.masked_boundingbox);
1611 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001612
1613 weston_view_set_transform_parent(view, NULL);
1614
Jason Ekstranda7af7042013-10-12 22:38:11 -05001615 wl_list_remove(&view->surface_link);
1616
1617 free(view);
1618}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001619
1620WL_EXPORT void
1621weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001622{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001623 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001624 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001625
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001626 if (--surface->ref_count > 0)
1627 return;
1628
1629 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1630
Pekka Paalanene67858b2013-04-25 13:57:42 +03001631 assert(wl_list_empty(&surface->subsurface_list_pending));
1632 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001633
Jason Ekstranda7af7042013-10-12 22:38:11 -05001634 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1635 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001636
Jason Ekstrand7b982072014-05-20 14:33:03 -05001637 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001638
Pekka Paalanende685b82012-12-04 15:58:12 +02001639 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001640
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001641 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001642 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001643 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001644
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001645 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001646 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001647
Pekka Paalanen133e4392014-09-23 22:08:46 -04001648 weston_presentation_feedback_discard_list(&surface->feedback_list);
1649
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001650 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001651}
1652
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001653static void
1654destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001655{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001656 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001657
Giulio Camuffo0d379742013-11-15 22:06:15 +01001658 /* Set the resource to NULL, since we don't want to leave a
1659 * dangling pointer if the surface was refcounted and survives
1660 * the weston_surface_destroy() call. */
1661 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001662 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001663}
1664
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001665static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001666weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1667{
1668 struct weston_buffer *buffer =
1669 container_of(listener, struct weston_buffer, destroy_listener);
1670
1671 wl_signal_emit(&buffer->destroy_signal, buffer);
1672 free(buffer);
1673}
1674
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001675WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001676weston_buffer_from_resource(struct wl_resource *resource)
1677{
1678 struct weston_buffer *buffer;
1679 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001680
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001681 listener = wl_resource_get_destroy_listener(resource,
1682 weston_buffer_destroy_handler);
1683
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001684 if (listener)
1685 return container_of(listener, struct weston_buffer,
1686 destroy_listener);
1687
1688 buffer = zalloc(sizeof *buffer);
1689 if (buffer == NULL)
1690 return NULL;
1691
1692 buffer->resource = resource;
1693 wl_signal_init(&buffer->destroy_signal);
1694 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001695 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001696 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001697
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001698 return buffer;
1699}
1700
1701static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001702weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1703 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001704{
Pekka Paalanende685b82012-12-04 15:58:12 +02001705 struct weston_buffer_reference *ref =
1706 container_of(listener, struct weston_buffer_reference,
1707 destroy_listener);
1708
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001709 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001710 ref->buffer = NULL;
1711}
1712
1713WL_EXPORT void
1714weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001715 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001716{
1717 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001718 ref->buffer->busy_count--;
1719 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001720 assert(wl_resource_get_client(ref->buffer->resource));
1721 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001722 WL_BUFFER_RELEASE);
1723 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001724 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001725 }
1726
Pekka Paalanende685b82012-12-04 15:58:12 +02001727 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001728 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001729 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001730 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001731 }
1732
Pekka Paalanende685b82012-12-04 15:58:12 +02001733 ref->buffer = buffer;
1734 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1735}
1736
1737static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001738weston_surface_attach(struct weston_surface *surface,
1739 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001740{
1741 weston_buffer_reference(&surface->buffer_ref, buffer);
1742
Pekka Paalanena6421c42012-12-04 15:58:10 +02001743 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001744 if (weston_surface_is_mapped(surface))
1745 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001746 }
1747
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001748 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001749
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001750 weston_surface_calculate_size_from_buffer(surface);
Pekka Paalanen133e4392014-09-23 22:08:46 -04001751 weston_presentation_feedback_discard_list(&surface->feedback_list);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001752}
1753
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001754WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001755weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001756{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001757 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001758
1759 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001760 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001761}
1762
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001763WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001764weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001765{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001766 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001767
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001768 pixman_region32_union(&compositor->primary_plane.damage,
1769 &compositor->primary_plane.damage,
1770 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001771 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001772}
1773
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001774static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001776{
Pekka Paalanende685b82012-12-04 15:58:12 +02001777 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001778 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001779 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001780
Jason Ekstrandef540082014-06-26 10:37:36 -07001781 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001782}
1783
1784static void
1785view_accumulate_damage(struct weston_view *view,
1786 pixman_region32_t *opaque)
1787{
1788 pixman_region32_t damage;
1789
1790 pixman_region32_init(&damage);
1791 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001792 pixman_box32_t *extents;
1793
Jason Ekstranda7af7042013-10-12 22:38:11 -05001794 extents = pixman_region32_extents(&view->surface->damage);
1795 view_compute_bbox(view, extents->x1, extents->y1,
1796 extents->x2 - extents->x1,
1797 extents->y2 - extents->y1,
1798 &damage);
1799 pixman_region32_translate(&damage,
1800 -view->plane->x,
1801 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001802 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001803 pixman_region32_copy(&damage, &view->surface->damage);
1804 pixman_region32_translate(&damage,
1805 view->geometry.x - view->plane->x,
1806 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001807 }
1808
Jason Ekstranda7af7042013-10-12 22:38:11 -05001809 pixman_region32_subtract(&damage, &damage, opaque);
1810 pixman_region32_union(&view->plane->damage,
1811 &view->plane->damage, &damage);
1812 pixman_region32_fini(&damage);
1813 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001814 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001815}
1816
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001817static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001818compositor_accumulate_damage(struct weston_compositor *ec)
1819{
1820 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001821 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001822 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001823
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001824 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001825
1826 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001827 pixman_region32_copy(&plane->clip, &clip);
1828
1829 pixman_region32_init(&opaque);
1830
Jason Ekstranda7af7042013-10-12 22:38:11 -05001831 wl_list_for_each(ev, &ec->view_list, link) {
1832 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001833 continue;
1834
Jason Ekstranda7af7042013-10-12 22:38:11 -05001835 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001836 }
1837
1838 pixman_region32_union(&clip, &clip, &opaque);
1839 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001840 }
1841
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001842 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001843
Jason Ekstranda7af7042013-10-12 22:38:11 -05001844 wl_list_for_each(ev, &ec->view_list, link)
1845 ev->surface->touched = 0;
1846
1847 wl_list_for_each(ev, &ec->view_list, link) {
1848 if (ev->surface->touched)
1849 continue;
1850 ev->surface->touched = 1;
1851
1852 surface_flush_damage(ev->surface);
1853
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001854 /* Both the renderer and the backend have seen the buffer
1855 * by now. If renderer needs the buffer, it has its own
1856 * reference set. If the backend wants to keep the buffer
1857 * around for migrating the surface into a non-primary plane
1858 * later, keep_buffer is true. Otherwise, drop the core
1859 * reference now, and allow early buffer release. This enables
1860 * clients to use single-buffering.
1861 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862 if (!ev->surface->keep_buffer)
1863 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001864 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001865}
1866
1867static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001868surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001869{
1870 struct weston_subsurface *sub;
1871
Pekka Paalanene67858b2013-04-25 13:57:42 +03001872 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001873 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001874 continue;
1875
Jason Ekstranda7af7042013-10-12 22:38:11 -05001876 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1877 wl_list_init(&sub->surface->views);
1878
1879 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001880 }
1881}
1882
1883static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001885{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001886 struct weston_subsurface *sub;
1887 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001888
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1890 if (sub->surface == surface)
1891 continue;
1892
George Kiagiadakised04d382014-06-13 18:10:26 +02001893 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1894 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001896 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001897
1898 surface_free_unused_subsurface_views(sub->surface);
1899 }
1900}
1901
1902static void
1903view_list_add_subsurface_view(struct weston_compositor *compositor,
1904 struct weston_subsurface *sub,
1905 struct weston_view *parent)
1906{
1907 struct weston_subsurface *child;
1908 struct weston_view *view = NULL, *iv;
1909
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001910 if (!weston_surface_is_mapped(sub->surface))
1911 return;
1912
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1914 if (iv->geometry.parent == parent) {
1915 view = iv;
1916 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001917 }
1918 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001919
1920 if (view) {
1921 /* Put it back in the surface's list of views */
1922 wl_list_remove(&view->surface_link);
1923 wl_list_insert(&sub->surface->views, &view->surface_link);
1924 } else {
1925 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001926 weston_view_set_position(view,
1927 sub->position.x,
1928 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001929 weston_view_set_transform_parent(view, parent);
1930 }
1931
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001932 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001933 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001934
Pekka Paalanenb188e912013-11-19 14:03:35 +02001935 if (wl_list_empty(&sub->surface->subsurface_list)) {
1936 wl_list_insert(compositor->view_list.prev, &view->link);
1937 return;
1938 }
1939
1940 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1941 if (child->surface == sub->surface)
1942 wl_list_insert(compositor->view_list.prev, &view->link);
1943 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001944 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001945 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001946}
1947
1948static void
1949view_list_add(struct weston_compositor *compositor,
1950 struct weston_view *view)
1951{
1952 struct weston_subsurface *sub;
1953
1954 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001955
Pekka Paalanenb188e912013-11-19 14:03:35 +02001956 if (wl_list_empty(&view->surface->subsurface_list)) {
1957 wl_list_insert(compositor->view_list.prev, &view->link);
1958 return;
1959 }
1960
1961 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1962 if (sub->surface == view->surface)
1963 wl_list_insert(compositor->view_list.prev, &view->link);
1964 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001965 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001966 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001967}
1968
1969static void
1970weston_compositor_build_view_list(struct weston_compositor *compositor)
1971{
1972 struct weston_view *view;
1973 struct weston_layer *layer;
1974
1975 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001976 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001977 surface_stash_subsurface_views(view->surface);
1978
1979 wl_list_init(&compositor->view_list);
1980 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001981 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001982 view_list_add(compositor, view);
1983 }
1984 }
1985
1986 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001987 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001988 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001989}
1990
David Herrmann1edf44c2013-10-22 17:11:26 +02001991static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001992weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001993{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001994 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001995 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001996 struct weston_animation *animation, *next;
1997 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001998 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001999 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02002000 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002001
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02002002 if (output->destroying)
2003 return 0;
2004
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002005 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05002006 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02002007
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002008 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08002009 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002010 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05002011 wl_list_for_each(ev, &ec->view_list, link)
2012 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04002013
Pekka Paalanene67858b2013-04-25 13:57:42 +03002014 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002015 wl_list_for_each(ev, &ec->view_list, link) {
2016 /* Note: This operation is safe to do multiple times on the
2017 * same surface.
2018 */
2019 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002020 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05002021 &ev->surface->frame_callback_list);
2022 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002023
2024 wl_list_insert_list(&output->feedback_list,
2025 &ev->surface->feedback_list);
2026 wl_list_init(&ev->surface->feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002027 }
2028 }
2029
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02002030 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04002031
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002032 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002033 pixman_region32_intersect(&output_damage,
2034 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02002035 pixman_region32_subtract(&output_damage,
2036 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04002037
Scott Moreauccbf29d2012-02-22 14:21:41 -07002038 if (output->dirty)
2039 weston_output_update_matrix(output);
2040
David Herrmann1edf44c2013-10-22 17:11:26 +02002041 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002042
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05002043 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002044
Kristian Høgsbergef044142011-06-21 15:02:12 -04002045 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002046
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04002047 weston_compositor_repick(ec);
2048 wl_event_loop_dispatch(ec->input_loop, 0);
2049
Jonas Ådahldb773762012-06-13 00:01:21 +02002050 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002051 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002052 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002053 }
2054
Scott Moreaud64cf212012-06-08 19:40:54 -06002055 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06002056 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002057 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06002058 }
David Herrmann1edf44c2013-10-22 17:11:26 +02002059
2060 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002061}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002062
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002063static int
2064weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002065{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002066 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05002067
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002068 wl_event_loop_dispatch(compositor->input_loop, 0);
2069
2070 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002071}
2072
2073WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002074weston_output_finish_frame(struct weston_output *output,
2075 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04002076{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002077 struct weston_compositor *compositor = output->compositor;
2078 struct wl_event_loop *loop =
2079 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02002080 int fd, r;
Pekka Paalanen133e4392014-09-23 22:08:46 -04002081 uint32_t refresh_nsec;
2082
2083 refresh_nsec = 1000000000000UL / output->current_mode->refresh;
2084 weston_presentation_feedback_present_list(&output->feedback_list,
2085 output, refresh_nsec, stamp,
Pekka Paalanen641307c2014-09-23 22:08:47 -04002086 output->msc);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002087
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002088 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07002089
2090 if (output->repaint_needed &&
2091 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
2092 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04002093 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02002094 if (!r)
2095 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002096 }
2097
2098 output->repaint_scheduled = 0;
2099 if (compositor->input_loop_source)
2100 return;
2101
2102 fd = wl_event_loop_get_fd(compositor->input_loop);
2103 compositor->input_loop_source =
2104 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2105 weston_compositor_read_input, compositor);
2106}
2107
2108static void
2109idle_repaint(void *data)
2110{
2111 struct weston_output *output = data;
2112
Jonas Ådahle5a12252013-04-05 23:07:11 +02002113 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002114}
2115
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002116WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002117weston_layer_entry_insert(struct weston_layer_entry *list,
2118 struct weston_layer_entry *entry)
2119{
2120 wl_list_insert(&list->link, &entry->link);
2121 entry->layer = list->layer;
2122}
2123
2124WL_EXPORT void
2125weston_layer_entry_remove(struct weston_layer_entry *entry)
2126{
2127 wl_list_remove(&entry->link);
2128 wl_list_init(&entry->link);
2129 entry->layer = NULL;
2130}
2131
2132WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002133weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2134{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002135 wl_list_init(&layer->view_list.link);
2136 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002137 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002138 if (below != NULL)
2139 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002140}
2141
2142WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002143weston_layer_set_mask(struct weston_layer *layer,
2144 int x, int y, int width, int height)
2145{
2146 struct weston_view *view;
2147
2148 layer->mask.x1 = x;
2149 layer->mask.x2 = x + width;
2150 layer->mask.y1 = y;
2151 layer->mask.y2 = y + height;
2152
2153 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2154 weston_view_geometry_dirty(view);
2155 }
2156}
2157
2158WL_EXPORT void
2159weston_layer_set_mask_infinite(struct weston_layer *layer)
2160{
2161 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2162 UINT32_MAX, UINT32_MAX);
2163}
2164
2165WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002166weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002167{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002168 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002169 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002170
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002171 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2172 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002173 return;
2174
Kristian Høgsbergef044142011-06-21 15:02:12 -04002175 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002176 output->repaint_needed = 1;
2177 if (output->repaint_scheduled)
2178 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002179
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002180 wl_event_loop_add_idle(loop, idle_repaint, output);
2181 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002182
2183 if (compositor->input_loop_source) {
2184 wl_event_source_remove(compositor->input_loop_source);
2185 compositor->input_loop_source = NULL;
2186 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002187}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002188
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002189WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002190weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2191{
2192 struct weston_output *output;
2193
2194 wl_list_for_each(output, &compositor->output_list, link)
2195 weston_output_schedule_repaint(output);
2196}
2197
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002198static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002199surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002200{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002201 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002202}
2203
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002204static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002205surface_attach(struct wl_client *client,
2206 struct wl_resource *resource,
2207 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2208{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002209 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002210 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002211
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002212 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002213 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002214 if (buffer == NULL) {
2215 wl_client_post_no_memory(client);
2216 return;
2217 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002218 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002219
Pekka Paalanende685b82012-12-04 15:58:12 +02002220 /* Attach, attach, without commit in between does not send
2221 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002222 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002223
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002224 surface->pending.sx = sx;
2225 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002226 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002227}
2228
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002229static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002230surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002231 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002232 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002233{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002234 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002235
Pekka Paalanen8e159182012-10-10 12:49:25 +03002236 pixman_region32_union_rect(&surface->pending.damage,
2237 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002238 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002239}
2240
Kristian Høgsberg33418202011-08-16 23:01:28 -04002241static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002242destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002243{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002244 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002245
2246 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002247 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002248}
2249
2250static void
2251surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002252 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002253{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002254 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002255 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002256
2257 cb = malloc(sizeof *cb);
2258 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002259 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002260 return;
2261 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002262
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002263 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2264 callback);
2265 if (cb->resource == NULL) {
2266 free(cb);
2267 wl_resource_post_no_memory(resource);
2268 return;
2269 }
2270
Jason Ekstranda85118c2013-06-27 20:17:02 -05002271 wl_resource_set_implementation(cb->resource, NULL, cb,
2272 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002273
Pekka Paalanenbc106382012-10-10 12:49:31 +03002274 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002275}
2276
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002277static void
2278surface_set_opaque_region(struct wl_client *client,
2279 struct wl_resource *resource,
2280 struct wl_resource *region_resource)
2281{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002282 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002283 struct weston_region *region;
2284
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002285 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002286 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002287 pixman_region32_copy(&surface->pending.opaque,
2288 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002289 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002290 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002291 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002292}
2293
2294static void
2295surface_set_input_region(struct wl_client *client,
2296 struct wl_resource *resource,
2297 struct wl_resource *region_resource)
2298{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002299 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002300 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002301
2302 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002303 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002304 pixman_region32_copy(&surface->pending.input,
2305 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002306 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002307 pixman_region32_fini(&surface->pending.input);
2308 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002309 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002310}
2311
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002312static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002313weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002314{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002315 struct weston_subsurface *sub;
2316
2317 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2318 parent_link_pending) {
2319 wl_list_remove(&sub->parent_link);
2320 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2321 }
2322}
2323
2324static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002325weston_surface_commit_state(struct weston_surface *surface,
2326 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002327{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002328 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002329 pixman_region32_t opaque;
2330
Alexander Larsson4ea95522013-05-22 14:41:37 +02002331 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002332 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002333 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002334 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002335
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002336 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002337 if (state->newly_attached)
2338 weston_surface_attach(surface, state->buffer);
2339 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002340
Jason Ekstrand7b982072014-05-20 14:33:03 -05002341 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002342 weston_surface_update_size(surface);
2343 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002344 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002345 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002346
Jason Ekstrand7b982072014-05-20 14:33:03 -05002347 state->sx = 0;
2348 state->sy = 0;
2349 state->newly_attached = 0;
2350 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002351
2352 /* wl_surface.damage */
2353 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002354 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002355 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002356 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002357 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002358
2359 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002360 pixman_region32_init(&opaque);
2361 pixman_region32_intersect_rect(&opaque, &state->opaque,
2362 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002363
2364 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2365 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002366 wl_list_for_each(view, &surface->views, surface_link)
2367 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002368 }
2369
2370 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002371
2372 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002373 pixman_region32_intersect_rect(&surface->input, &state->input,
2374 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002375
Pekka Paalanenbc106382012-10-10 12:49:31 +03002376 /* wl_surface.frame */
2377 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002378 &state->frame_callback_list);
2379 wl_list_init(&state->frame_callback_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002380
2381 /* XXX:
2382 * What should happen with a feedback request, if there
2383 * is no wl_buffer attached for this commit?
2384 */
2385
2386 /* presentation.feedback */
2387 wl_list_insert_list(&surface->feedback_list,
2388 &state->feedback_list);
2389 wl_list_init(&state->feedback_list);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002390}
2391
2392static void
2393weston_surface_commit(struct weston_surface *surface)
2394{
2395 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002396
Pekka Paalanene67858b2013-04-25 13:57:42 +03002397 weston_surface_commit_subsurface_order(surface);
2398
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002399 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002400}
2401
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002402static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002403weston_subsurface_commit(struct weston_subsurface *sub);
2404
2405static void
2406weston_subsurface_parent_commit(struct weston_subsurface *sub,
2407 int parent_is_synchronized);
2408
2409static void
2410surface_commit(struct wl_client *client, struct wl_resource *resource)
2411{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002412 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002413 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2414
2415 if (sub) {
2416 weston_subsurface_commit(sub);
2417 return;
2418 }
2419
2420 weston_surface_commit(surface);
2421
2422 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2423 if (sub->surface != surface)
2424 weston_subsurface_parent_commit(sub, 0);
2425 }
2426}
2427
2428static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002429surface_set_buffer_transform(struct wl_client *client,
2430 struct wl_resource *resource, int transform)
2431{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002432 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002433
Jonny Lamba55f1392014-05-30 12:07:15 +02002434 /* if wl_output.transform grows more members this will need to be updated. */
2435 if (transform < 0 ||
2436 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2437 wl_resource_post_error(resource,
2438 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2439 "buffer transform must be a valid transform "
2440 "('%d' specified)", transform);
2441 return;
2442 }
2443
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002444 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002445 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002446}
2447
Alexander Larsson4ea95522013-05-22 14:41:37 +02002448static void
2449surface_set_buffer_scale(struct wl_client *client,
2450 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002451 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002452{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002453 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002454
Jonny Lamba55f1392014-05-30 12:07:15 +02002455 if (scale < 1) {
2456 wl_resource_post_error(resource,
2457 WL_SURFACE_ERROR_INVALID_SCALE,
2458 "buffer scale must be at least one "
2459 "('%d' specified)", scale);
2460 return;
2461 }
2462
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002463 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002464 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002465}
2466
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002467static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002468 surface_destroy,
2469 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002470 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002471 surface_frame,
2472 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002473 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002474 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002475 surface_set_buffer_transform,
2476 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002477};
2478
2479static void
2480compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002481 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002482{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002483 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002484 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002485
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002486 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002487 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002488 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002489 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002490 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002491
Jason Ekstranda85118c2013-06-27 20:17:02 -05002492 surface->resource =
2493 wl_resource_create(client, &wl_surface_interface,
2494 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002495 if (surface->resource == NULL) {
2496 weston_surface_destroy(surface);
2497 wl_resource_post_no_memory(resource);
2498 return;
2499 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002500 wl_resource_set_implementation(surface->resource, &surface_interface,
2501 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002502
2503 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002504}
2505
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002506static void
2507destroy_region(struct wl_resource *resource)
2508{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002509 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002510
2511 pixman_region32_fini(&region->region);
2512 free(region);
2513}
2514
2515static void
2516region_destroy(struct wl_client *client, struct wl_resource *resource)
2517{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002518 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002519}
2520
2521static void
2522region_add(struct wl_client *client, struct wl_resource *resource,
2523 int32_t x, int32_t y, int32_t width, int32_t height)
2524{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002525 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002526
2527 pixman_region32_union_rect(&region->region, &region->region,
2528 x, y, width, height);
2529}
2530
2531static void
2532region_subtract(struct wl_client *client, struct wl_resource *resource,
2533 int32_t x, int32_t y, int32_t width, int32_t height)
2534{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002535 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002536 pixman_region32_t rect;
2537
2538 pixman_region32_init_rect(&rect, x, y, width, height);
2539 pixman_region32_subtract(&region->region, &region->region, &rect);
2540 pixman_region32_fini(&rect);
2541}
2542
2543static const struct wl_region_interface region_interface = {
2544 region_destroy,
2545 region_add,
2546 region_subtract
2547};
2548
2549static void
2550compositor_create_region(struct wl_client *client,
2551 struct wl_resource *resource, uint32_t id)
2552{
2553 struct weston_region *region;
2554
2555 region = malloc(sizeof *region);
2556 if (region == NULL) {
2557 wl_resource_post_no_memory(resource);
2558 return;
2559 }
2560
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002561 pixman_region32_init(&region->region);
2562
Jason Ekstranda85118c2013-06-27 20:17:02 -05002563 region->resource =
2564 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002565 if (region->resource == NULL) {
2566 free(region);
2567 wl_resource_post_no_memory(resource);
2568 return;
2569 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002570 wl_resource_set_implementation(region->resource, &region_interface,
2571 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002572}
2573
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002574static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002575 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002576 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002577};
2578
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002579static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002580weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2581{
2582 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002583
Jason Ekstrand7b982072014-05-20 14:33:03 -05002584 weston_surface_commit_state(surface, &sub->cached);
2585 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002586
2587 weston_surface_commit_subsurface_order(surface);
2588
2589 weston_surface_schedule_repaint(surface);
2590
Jason Ekstrand7b982072014-05-20 14:33:03 -05002591 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002592}
2593
2594static void
2595weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2596{
2597 struct weston_surface *surface = sub->surface;
2598
2599 /*
2600 * If this commit would cause the surface to move by the
2601 * attach(dx, dy) parameters, the old damage region must be
2602 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002603 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002604 */
2605 pixman_region32_translate(&sub->cached.damage,
2606 -surface->pending.sx, -surface->pending.sy);
2607 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2608 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002609 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610
2611 if (surface->pending.newly_attached) {
2612 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002613 weston_surface_state_set_buffer(&sub->cached,
2614 surface->pending.buffer);
2615 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002616 surface->pending.buffer);
Pekka Paalanen133e4392014-09-23 22:08:46 -04002617 weston_presentation_feedback_discard_list(
2618 &sub->cached.feedback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002619 }
2620 sub->cached.sx += surface->pending.sx;
2621 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002622
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002623 sub->cached.buffer_viewport.changed |=
2624 surface->pending.buffer_viewport.changed;
2625 sub->cached.buffer_viewport.buffer =
2626 surface->pending.buffer_viewport.buffer;
2627 sub->cached.buffer_viewport.surface =
2628 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002629
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002630 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002631
2632 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2633
2634 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2635
2636 wl_list_insert_list(&sub->cached.frame_callback_list,
2637 &surface->pending.frame_callback_list);
2638 wl_list_init(&surface->pending.frame_callback_list);
2639
Pekka Paalanen133e4392014-09-23 22:08:46 -04002640 wl_list_insert_list(&sub->cached.feedback_list,
2641 &surface->pending.feedback_list);
2642 wl_list_init(&surface->pending.feedback_list);
2643
Jason Ekstrand7b982072014-05-20 14:33:03 -05002644 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002645}
2646
Derek Foreman280e7dd2014-10-03 13:13:42 -05002647static bool
Pekka Paalanene67858b2013-04-25 13:57:42 +03002648weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2649{
2650 while (sub) {
2651 if (sub->synchronized)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002652 return true;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002653
2654 if (!sub->parent)
Derek Foreman280e7dd2014-10-03 13:13:42 -05002655 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002656
2657 sub = weston_surface_to_subsurface(sub->parent);
2658 }
2659
Carlos Olmedo Escobar61a9bf52014-11-04 14:38:39 +01002660 return false;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002661}
2662
2663static void
2664weston_subsurface_commit(struct weston_subsurface *sub)
2665{
2666 struct weston_surface *surface = sub->surface;
2667 struct weston_subsurface *tmp;
2668
2669 /* Recursive check for effectively synchronized. */
2670 if (weston_subsurface_is_synchronized(sub)) {
2671 weston_subsurface_commit_to_cache(sub);
2672 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002673 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002674 /* flush accumulated state from cache */
2675 weston_subsurface_commit_to_cache(sub);
2676 weston_subsurface_commit_from_cache(sub);
2677 } else {
2678 weston_surface_commit(surface);
2679 }
2680
2681 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2682 if (tmp->surface != surface)
2683 weston_subsurface_parent_commit(tmp, 0);
2684 }
2685 }
2686}
2687
2688static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002689weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002690{
2691 struct weston_surface *surface = sub->surface;
2692 struct weston_subsurface *tmp;
2693
Pekka Paalanene67858b2013-04-25 13:57:42 +03002694 /* From now on, commit_from_cache the whole sub-tree, regardless of
2695 * the synchronized mode of each child. This sub-surface or some
2696 * of its ancestors were synchronized, so we are synchronized
2697 * all the way down.
2698 */
2699
Jason Ekstrand7b982072014-05-20 14:33:03 -05002700 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002701 weston_subsurface_commit_from_cache(sub);
2702
2703 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2704 if (tmp->surface != surface)
2705 weston_subsurface_parent_commit(tmp, 1);
2706 }
2707}
2708
2709static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002710weston_subsurface_parent_commit(struct weston_subsurface *sub,
2711 int parent_is_synchronized)
2712{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002713 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002714 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002715 wl_list_for_each(view, &sub->surface->views, surface_link)
2716 weston_view_set_position(view,
2717 sub->position.x,
2718 sub->position.y);
2719
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002720 sub->position.set = 0;
2721 }
2722
2723 if (parent_is_synchronized || sub->synchronized)
2724 weston_subsurface_synchronized_commit(sub);
2725}
2726
Pekka Paalanen8274d902014-08-06 19:36:51 +03002727static int
2728subsurface_get_label(struct weston_surface *surface, char *buf, size_t len)
2729{
2730 return snprintf(buf, len, "sub-surface");
2731}
2732
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002733static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002734subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002735{
2736 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002737 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002738
Jason Ekstranda7af7042013-10-12 22:38:11 -05002739 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002740 weston_view_set_position(view,
2741 view->geometry.x + dx,
2742 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002743
2744 /* No need to check parent mappedness, because if parent is not
2745 * mapped, parent is not in a visible layer, so this sub-surface
2746 * will not be drawn either.
2747 */
2748 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002749 struct weston_output *output;
2750
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002751 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002752 * because that would call it also for the parent surface,
2753 * which might not be mapped yet. That would lead to
2754 * inconsistent state, where the window could never be
2755 * mapped.
2756 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002757 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002758 * weston_surface_is_mapped() return true, so that when the
2759 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002760 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002761 */
2762 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002763 output = container_of(compositor->output_list.next,
2764 struct weston_output, link);
2765
2766 surface->output = output;
2767 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002768 }
2769}
2770
2771static struct weston_subsurface *
2772weston_surface_to_subsurface(struct weston_surface *surface)
2773{
2774 if (surface->configure == subsurface_configure)
2775 return surface->configure_private;
2776
2777 return NULL;
2778}
2779
Pekka Paalanen01388e22013-04-25 13:57:44 +03002780WL_EXPORT struct weston_surface *
2781weston_surface_get_main_surface(struct weston_surface *surface)
2782{
2783 struct weston_subsurface *sub;
2784
2785 while (surface && (sub = weston_surface_to_subsurface(surface)))
2786 surface = sub->parent;
2787
2788 return surface;
2789}
2790
Pekka Paalanen50b67472014-10-01 15:02:41 +03002791WL_EXPORT int
2792weston_surface_set_role(struct weston_surface *surface,
2793 const char *role_name,
2794 struct wl_resource *error_resource,
2795 uint32_t error_code)
2796{
2797 assert(role_name);
2798
2799 if (surface->role_name == NULL ||
2800 surface->role_name == role_name ||
2801 strcmp(surface->role_name, role_name) == 0) {
2802 surface->role_name = role_name;
2803
2804 return 0;
2805 }
2806
2807 wl_resource_post_error(error_resource, error_code,
2808 "Cannot assign role %s to wl_surface@%d,"
2809 " already has role %s\n",
2810 role_name,
2811 wl_resource_get_id(surface->resource),
2812 surface->role_name);
2813 return -1;
2814}
2815
Pekka Paalanen8274d902014-08-06 19:36:51 +03002816WL_EXPORT void
2817weston_surface_set_label_func(struct weston_surface *surface,
2818 int (*desc)(struct weston_surface *,
2819 char *, size_t))
2820{
2821 surface->get_label = desc;
2822}
2823
Pekka Paalanene67858b2013-04-25 13:57:42 +03002824static void
2825subsurface_set_position(struct wl_client *client,
2826 struct wl_resource *resource, int32_t x, int32_t y)
2827{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002828 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002829
2830 if (!sub)
2831 return;
2832
2833 sub->position.x = x;
2834 sub->position.y = y;
2835 sub->position.set = 1;
2836}
2837
2838static struct weston_subsurface *
2839subsurface_from_surface(struct weston_surface *surface)
2840{
2841 struct weston_subsurface *sub;
2842
2843 sub = weston_surface_to_subsurface(surface);
2844 if (sub)
2845 return sub;
2846
2847 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2848 if (sub->surface == surface)
2849 return sub;
2850
2851 return NULL;
2852}
2853
2854static struct weston_subsurface *
2855subsurface_sibling_check(struct weston_subsurface *sub,
2856 struct weston_surface *surface,
2857 const char *request)
2858{
2859 struct weston_subsurface *sibling;
2860
2861 sibling = subsurface_from_surface(surface);
2862
2863 if (!sibling) {
2864 wl_resource_post_error(sub->resource,
2865 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2866 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002867 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002868 return NULL;
2869 }
2870
2871 if (sibling->parent != sub->parent) {
2872 wl_resource_post_error(sub->resource,
2873 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2874 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002875 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002876 return NULL;
2877 }
2878
2879 return sibling;
2880}
2881
2882static void
2883subsurface_place_above(struct wl_client *client,
2884 struct wl_resource *resource,
2885 struct wl_resource *sibling_resource)
2886{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002887 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002888 struct weston_surface *surface =
2889 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890 struct weston_subsurface *sibling;
2891
2892 if (!sub)
2893 return;
2894
2895 sibling = subsurface_sibling_check(sub, surface, "place_above");
2896 if (!sibling)
2897 return;
2898
2899 wl_list_remove(&sub->parent_link_pending);
2900 wl_list_insert(sibling->parent_link_pending.prev,
2901 &sub->parent_link_pending);
2902}
2903
2904static void
2905subsurface_place_below(struct wl_client *client,
2906 struct wl_resource *resource,
2907 struct wl_resource *sibling_resource)
2908{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002909 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002910 struct weston_surface *surface =
2911 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002912 struct weston_subsurface *sibling;
2913
2914 if (!sub)
2915 return;
2916
2917 sibling = subsurface_sibling_check(sub, surface, "place_below");
2918 if (!sibling)
2919 return;
2920
2921 wl_list_remove(&sub->parent_link_pending);
2922 wl_list_insert(&sibling->parent_link_pending,
2923 &sub->parent_link_pending);
2924}
2925
2926static void
2927subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2928{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002929 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002930
2931 if (sub)
2932 sub->synchronized = 1;
2933}
2934
2935static void
2936subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2937{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002938 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002939
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002940 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002942
2943 /* If sub became effectively desynchronized, flush. */
2944 if (!weston_subsurface_is_synchronized(sub))
2945 weston_subsurface_synchronized_commit(sub);
2946 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002947}
2948
2949static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002950weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2951{
2952 wl_list_remove(&sub->parent_link);
2953 wl_list_remove(&sub->parent_link_pending);
2954 wl_list_remove(&sub->parent_destroy_listener.link);
2955 sub->parent = NULL;
2956}
2957
2958static void
2959weston_subsurface_destroy(struct weston_subsurface *sub);
2960
2961static void
2962subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2963{
2964 struct weston_subsurface *sub =
2965 container_of(listener, struct weston_subsurface,
2966 surface_destroy_listener);
2967 assert(data == &sub->surface->resource);
2968
2969 /* The protocol object (wl_resource) is left inert. */
2970 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002971 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002972
2973 weston_subsurface_destroy(sub);
2974}
2975
2976static void
2977subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2978{
2979 struct weston_subsurface *sub =
2980 container_of(listener, struct weston_subsurface,
2981 parent_destroy_listener);
2982 assert(data == &sub->parent->resource);
2983 assert(sub->surface != sub->parent);
2984
2985 if (weston_surface_is_mapped(sub->surface))
2986 weston_surface_unmap(sub->surface);
2987
2988 weston_subsurface_unlink_parent(sub);
2989}
2990
2991static void
2992subsurface_resource_destroy(struct wl_resource *resource)
2993{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002994 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002995
2996 if (sub)
2997 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002998}
2999
3000static void
3001subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
3002{
3003 wl_resource_destroy(resource);
3004}
3005
3006static void
3007weston_subsurface_link_parent(struct weston_subsurface *sub,
3008 struct weston_surface *parent)
3009{
3010 sub->parent = parent;
3011 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003012 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003013 &sub->parent_destroy_listener);
3014
3015 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3016 wl_list_insert(&parent->subsurface_list_pending,
3017 &sub->parent_link_pending);
3018}
3019
3020static void
3021weston_subsurface_link_surface(struct weston_subsurface *sub,
3022 struct weston_surface *surface)
3023{
3024 sub->surface = surface;
3025 sub->surface_destroy_listener.notify =
3026 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003027 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003028 &sub->surface_destroy_listener);
3029}
3030
3031static void
3032weston_subsurface_destroy(struct weston_subsurface *sub)
3033{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003034 struct weston_view *view, *next;
3035
Pekka Paalanene67858b2013-04-25 13:57:42 +03003036 assert(sub->surface);
3037
3038 if (sub->resource) {
3039 assert(weston_surface_to_subsurface(sub->surface) == sub);
3040 assert(sub->parent_destroy_listener.notify ==
3041 subsurface_handle_parent_destroy);
3042
George Kiagiadakised04d382014-06-13 18:10:26 +02003043 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3044 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003045 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003046 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003047
Pekka Paalanene67858b2013-04-25 13:57:42 +03003048 if (sub->parent)
3049 weston_subsurface_unlink_parent(sub);
3050
Jason Ekstrand7b982072014-05-20 14:33:03 -05003051 weston_surface_state_fini(&sub->cached);
3052 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003053
3054 sub->surface->configure = NULL;
3055 sub->surface->configure_private = NULL;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003056 weston_surface_set_label_func(sub->surface, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003057 } else {
3058 /* the dummy weston_subsurface for the parent itself */
3059 assert(sub->parent_destroy_listener.notify == NULL);
3060 wl_list_remove(&sub->parent_link);
3061 wl_list_remove(&sub->parent_link_pending);
3062 }
3063
3064 wl_list_remove(&sub->surface_destroy_listener.link);
3065 free(sub);
3066}
3067
3068static const struct wl_subsurface_interface subsurface_implementation = {
3069 subsurface_destroy,
3070 subsurface_set_position,
3071 subsurface_place_above,
3072 subsurface_place_below,
3073 subsurface_set_sync,
3074 subsurface_set_desync
3075};
3076
3077static struct weston_subsurface *
3078weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3079 struct weston_surface *parent)
3080{
3081 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003082 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003083
Bryce Harringtonde16d892014-11-20 22:21:57 -08003084 sub = zalloc(sizeof *sub);
3085 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003086 return NULL;
3087
Jason Ekstranda7af7042013-10-12 22:38:11 -05003088 wl_list_init(&sub->unused_views);
3089
Jason Ekstranda85118c2013-06-27 20:17:02 -05003090 sub->resource =
3091 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003092 if (!sub->resource) {
3093 free(sub);
3094 return NULL;
3095 }
3096
Jason Ekstranda85118c2013-06-27 20:17:02 -05003097 wl_resource_set_implementation(sub->resource,
3098 &subsurface_implementation,
3099 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003100 weston_subsurface_link_surface(sub, surface);
3101 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003102 weston_surface_state_init(&sub->cached);
3103 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003104 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003105
3106 return sub;
3107}
3108
3109/* Create a dummy subsurface for having the parent itself in its
3110 * sub-surface lists. Makes stacking order manipulation easy.
3111 */
3112static struct weston_subsurface *
3113weston_subsurface_create_for_parent(struct weston_surface *parent)
3114{
3115 struct weston_subsurface *sub;
3116
Bryce Harringtonde16d892014-11-20 22:21:57 -08003117 sub = zalloc(sizeof *sub);
3118 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003119 return NULL;
3120
3121 weston_subsurface_link_surface(sub, parent);
3122 sub->parent = parent;
3123 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3124 wl_list_insert(&parent->subsurface_list_pending,
3125 &sub->parent_link_pending);
3126
3127 return sub;
3128}
3129
3130static void
3131subcompositor_get_subsurface(struct wl_client *client,
3132 struct wl_resource *resource,
3133 uint32_t id,
3134 struct wl_resource *surface_resource,
3135 struct wl_resource *parent_resource)
3136{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003137 struct weston_surface *surface =
3138 wl_resource_get_user_data(surface_resource);
3139 struct weston_surface *parent =
3140 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003141 struct weston_subsurface *sub;
3142 static const char where[] = "get_subsurface: wl_subsurface@";
3143
3144 if (surface == parent) {
3145 wl_resource_post_error(resource,
3146 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3147 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003148 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003149 return;
3150 }
3151
3152 if (weston_surface_to_subsurface(surface)) {
3153 wl_resource_post_error(resource,
3154 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3155 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003156 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157 return;
3158 }
3159
Pekka Paalanen50b67472014-10-01 15:02:41 +03003160 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3161 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003162 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003163
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003164 if (weston_surface_get_main_surface(parent) == surface) {
3165 wl_resource_post_error(resource,
3166 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3167 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003168 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003169 return;
3170 }
3171
Pekka Paalanene67858b2013-04-25 13:57:42 +03003172 /* make sure the parent is in its own list */
3173 if (wl_list_empty(&parent->subsurface_list)) {
3174 if (!weston_subsurface_create_for_parent(parent)) {
3175 wl_resource_post_no_memory(resource);
3176 return;
3177 }
3178 }
3179
3180 sub = weston_subsurface_create(id, surface, parent);
3181 if (!sub) {
3182 wl_resource_post_no_memory(resource);
3183 return;
3184 }
3185
3186 surface->configure = subsurface_configure;
3187 surface->configure_private = sub;
Pekka Paalanen8274d902014-08-06 19:36:51 +03003188 weston_surface_set_label_func(surface, subsurface_get_label);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003189}
3190
3191static void
3192subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3193{
3194 wl_resource_destroy(resource);
3195}
3196
3197static const struct wl_subcompositor_interface subcompositor_interface = {
3198 subcompositor_destroy,
3199 subcompositor_get_subsurface
3200};
3201
3202static void
3203bind_subcompositor(struct wl_client *client,
3204 void *data, uint32_t version, uint32_t id)
3205{
3206 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003207 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003208
Jason Ekstranda85118c2013-06-27 20:17:02 -05003209 resource =
3210 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003211 if (resource == NULL) {
3212 wl_client_post_no_memory(client);
3213 return;
3214 }
3215 wl_resource_set_implementation(resource, &subcompositor_interface,
3216 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003217}
3218
3219static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003220weston_compositor_dpms(struct weston_compositor *compositor,
3221 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003222{
3223 struct weston_output *output;
3224
3225 wl_list_for_each(output, &compositor->output_list, link)
3226 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003227 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003228}
3229
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003230WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003231weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003232{
Neil Roberts8b62e202013-09-30 13:14:47 +01003233 uint32_t old_state = compositor->state;
3234
3235 /* The state needs to be changed before emitting the wake
3236 * signal because that may try to schedule a repaint which
3237 * will not work if the compositor is still sleeping */
3238 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3239
3240 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003241 case WESTON_COMPOSITOR_SLEEPING:
3242 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3243 /* fall through */
3244 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003245 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003246 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003247 /* fall through */
3248 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003249 wl_event_source_timer_update(compositor->idle_source,
3250 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003251 }
3252}
3253
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003254WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003255weston_compositor_offscreen(struct weston_compositor *compositor)
3256{
3257 switch (compositor->state) {
3258 case WESTON_COMPOSITOR_OFFSCREEN:
3259 return;
3260 case WESTON_COMPOSITOR_SLEEPING:
3261 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3262 /* fall through */
3263 default:
3264 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3265 wl_event_source_timer_update(compositor->idle_source, 0);
3266 }
3267}
3268
3269WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003270weston_compositor_sleep(struct weston_compositor *compositor)
3271{
3272 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3273 return;
3274
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003275 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003276 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3277 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3278}
3279
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003280static int
3281idle_handler(void *data)
3282{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003283 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003284
3285 if (compositor->idle_inhibit)
3286 return 1;
3287
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003288 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003289 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003290
3291 return 1;
3292}
3293
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003294WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003295weston_plane_init(struct weston_plane *plane,
3296 struct weston_compositor *ec,
3297 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003298{
3299 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003300 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003301 plane->x = x;
3302 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003303 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003304
3305 /* Init the link so that the call to wl_list_remove() when releasing
3306 * the plane without ever stacking doesn't lead to a crash */
3307 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003308}
3309
3310WL_EXPORT void
3311weston_plane_release(struct weston_plane *plane)
3312{
Xiong Zhang97116532013-10-23 13:58:31 +08003313 struct weston_view *view;
3314
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003315 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003316 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003317
Xiong Zhang97116532013-10-23 13:58:31 +08003318 wl_list_for_each(view, &plane->compositor->view_list, link) {
3319 if (view->plane == plane)
3320 view->plane = NULL;
3321 }
3322
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003323 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003324}
3325
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003326WL_EXPORT void
3327weston_compositor_stack_plane(struct weston_compositor *ec,
3328 struct weston_plane *plane,
3329 struct weston_plane *above)
3330{
3331 if (above)
3332 wl_list_insert(above->link.prev, &plane->link);
3333 else
3334 wl_list_insert(&ec->plane_list, &plane->link);
3335}
3336
Casey Dahlin9074db52012-04-19 22:50:09 -04003337static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003338{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003339 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003340}
3341
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003342static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003343bind_output(struct wl_client *client,
3344 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003345{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003346 struct weston_output *output = data;
3347 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003348 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003349
Jason Ekstranda85118c2013-06-27 20:17:02 -05003350 resource = wl_resource_create(client, &wl_output_interface,
3351 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003352 if (resource == NULL) {
3353 wl_client_post_no_memory(client);
3354 return;
3355 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003356
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003357 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003358 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003359
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003360 wl_output_send_geometry(resource,
3361 output->x,
3362 output->y,
3363 output->mm_width,
3364 output->mm_height,
3365 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003366 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003367 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003368 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003369 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003370 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003371
3372 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003373 wl_output_send_mode(resource,
3374 mode->flags,
3375 mode->width,
3376 mode->height,
3377 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003378 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003379
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003380 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003381 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003382}
3383
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003384/* Move other outputs when one is removed so the space remains contiguos. */
3385static void
3386weston_compositor_remove_output(struct weston_compositor *compositor,
3387 struct weston_output *remove_output)
3388{
3389 struct weston_output *output;
3390 int offset = 0;
3391
3392 wl_list_for_each(output, &compositor->output_list, link) {
3393 if (output == remove_output) {
3394 offset = output->width;
3395 continue;
3396 }
3397
3398 if (offset > 0) {
3399 weston_output_move(output,
3400 output->x - offset, output->y);
3401 output->dirty = 1;
3402 }
3403 }
3404}
3405
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003406WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003407weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003408{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003409 struct wl_resource *resource;
3410
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003411 output->destroying = 1;
3412
Pekka Paalanen133e4392014-09-23 22:08:46 -04003413 weston_presentation_feedback_discard_list(&output->feedback_list);
3414
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003415 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003416 wl_list_remove(&output->link);
3417
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003418 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003419 wl_signal_emit(&output->destroy_signal, output);
3420
Richard Hughesafe690c2013-05-02 10:10:04 +01003421 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003422 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003423 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003424 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003425
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003426 wl_resource_for_each(resource, &output->resource_list) {
3427 wl_resource_set_destructor(resource, NULL);
3428 }
3429
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003430 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003431}
3432
Scott Moreau1bad5db2012-08-18 01:04:05 -06003433static void
3434weston_output_compute_transform(struct weston_output *output)
3435{
3436 struct weston_matrix transform;
3437 int flip;
3438
3439 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003440 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003441
3442 switch(output->transform) {
3443 case WL_OUTPUT_TRANSFORM_FLIPPED:
3444 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3445 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3446 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003447 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003448 flip = -1;
3449 break;
3450 default:
3451 flip = 1;
3452 break;
3453 }
3454
3455 switch(output->transform) {
3456 case WL_OUTPUT_TRANSFORM_NORMAL:
3457 case WL_OUTPUT_TRANSFORM_FLIPPED:
3458 transform.d[0] = flip;
3459 transform.d[1] = 0;
3460 transform.d[4] = 0;
3461 transform.d[5] = 1;
3462 break;
3463 case WL_OUTPUT_TRANSFORM_90:
3464 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3465 transform.d[0] = 0;
3466 transform.d[1] = -flip;
3467 transform.d[4] = 1;
3468 transform.d[5] = 0;
3469 break;
3470 case WL_OUTPUT_TRANSFORM_180:
3471 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3472 transform.d[0] = -flip;
3473 transform.d[1] = 0;
3474 transform.d[4] = 0;
3475 transform.d[5] = -1;
3476 break;
3477 case WL_OUTPUT_TRANSFORM_270:
3478 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3479 transform.d[0] = 0;
3480 transform.d[1] = flip;
3481 transform.d[4] = -1;
3482 transform.d[5] = 0;
3483 break;
3484 default:
3485 break;
3486 }
3487
3488 weston_matrix_multiply(&output->matrix, &transform);
3489}
3490
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003491WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003492weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003493{
Scott Moreau850ca422012-05-21 15:21:25 -06003494 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003495
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003496 weston_matrix_init(&output->matrix);
3497 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003498 -(output->x + output->width / 2.0),
3499 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003500
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003501 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003502 2.0 / output->width,
3503 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003504
Scott Moreauccbf29d2012-02-22 14:21:41 -07003505 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003506 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003507 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003508 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3509 output->zoom.trans_y, 0);
3510 weston_matrix_scale(&output->matrix, magnification,
3511 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003512 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003513
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003514 weston_output_compute_transform(output);
3515
Scott Moreauccbf29d2012-02-22 14:21:41 -07003516 output->dirty = 0;
3517}
3518
Scott Moreau1bad5db2012-08-18 01:04:05 -06003519static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003520weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003521{
3522 output->transform = transform;
3523
3524 switch (transform) {
3525 case WL_OUTPUT_TRANSFORM_90:
3526 case WL_OUTPUT_TRANSFORM_270:
3527 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3528 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3529 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003530 output->width = output->current_mode->height;
3531 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003532 break;
3533 case WL_OUTPUT_TRANSFORM_NORMAL:
3534 case WL_OUTPUT_TRANSFORM_180:
3535 case WL_OUTPUT_TRANSFORM_FLIPPED:
3536 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003537 output->width = output->current_mode->width;
3538 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003539 break;
3540 default:
3541 break;
3542 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003543
Hardening57388e42013-09-18 23:56:36 +02003544 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003545 output->width /= scale;
3546 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003547}
3548
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003549static void
3550weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003551{
3552 output->x = x;
3553 output->y = y;
3554
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003555 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003556 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003557 output->width,
3558 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003559}
3560
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003561WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003562weston_output_move(struct weston_output *output, int x, int y)
3563{
3564 pixman_region32_t old_region;
3565 struct wl_resource *resource;
3566
3567 output->move_x = x - output->x;
3568 output->move_y = y - output->y;
3569
3570 if (output->move_x == 0 && output->move_y == 0)
3571 return;
3572
3573 pixman_region32_init(&old_region);
3574 pixman_region32_copy(&old_region, &output->region);
3575
3576 weston_output_init_geometry(output, x, y);
3577
3578 output->dirty = 1;
3579
3580 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003581 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003582
3583 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003584 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003585 wl_output_send_geometry(resource,
3586 output->x,
3587 output->y,
3588 output->mm_width,
3589 output->mm_height,
3590 output->subpixel,
3591 output->make,
3592 output->model,
3593 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003594
3595 if (wl_resource_get_version(resource) >= 2)
3596 wl_output_send_done(resource);
3597 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003598}
3599
3600WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003601weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003602 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003603 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003604{
3605 output->compositor = c;
3606 output->x = x;
3607 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003608 output->mm_width = mm_width;
3609 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003610 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003611 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003612
Alexander Larsson0b135062013-05-28 16:23:36 +02003613 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003614 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003615
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003616 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003617 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003618
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003619 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003620 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003621 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003622 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003623 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003624
Casey Dahlin58ba1372012-04-19 22:50:08 -04003625 output->id = ffs(~output->compositor->output_id_pool) - 1;
3626 output->compositor->output_id_pool |= 1 << output->id;
3627
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003628 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003629 wl_global_create(c->wl_display, &wl_output_interface, 2,
3630 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003631 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003632}
3633
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003634WL_EXPORT void
3635weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003636 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003637 wl_fixed_t *x, wl_fixed_t *y)
3638{
3639 wl_fixed_t tx, ty;
3640 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003641 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003642
Hardeningff39efa2013-09-18 23:56:35 +02003643 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3644 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003645
3646 switch(output->transform) {
3647 case WL_OUTPUT_TRANSFORM_NORMAL:
3648 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003649 tx = device_x;
3650 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003651 break;
3652 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003653 tx = device_y;
3654 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003655 break;
3656 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003657 tx = width - device_x;
3658 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003659 break;
3660 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003661 tx = width - device_y;
3662 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003663 break;
3664 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003665 tx = width - device_x;
3666 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003667 break;
3668 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003669 tx = width - device_y;
3670 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003671 break;
3672 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003673 tx = device_x;
3674 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003675 break;
3676 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003677 tx = device_y;
3678 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003679 break;
3680 }
3681
Neil Robertseb5a2002014-05-01 16:13:55 +01003682 tx /= output->current_scale;
3683 ty /= output->current_scale;
3684
3685 if (output->zoom.active) {
3686 zoom_scale = output->zoom.spring_z.current;
3687 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3688 output->width / 2.0f *
3689 (zoom_scale + output->zoom.trans_x));
3690 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3691 output->height / 2.0f *
3692 (zoom_scale + output->zoom.trans_y));
3693 tx = wl_fixed_from_double(zx);
3694 ty = wl_fixed_from_double(zy);
3695 }
3696
3697 *x = tx + wl_fixed_from_int(output->x);
3698 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003699}
3700
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003701static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003702destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003703{
Jonny Lamb74130762013-11-26 18:19:46 +01003704 struct weston_surface *surface =
3705 wl_resource_get_user_data(resource);
3706
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003707 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003708 surface->pending.buffer_viewport.buffer.src_width =
3709 wl_fixed_from_int(-1);
3710 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003711 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003712}
3713
3714static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003715viewport_destroy(struct wl_client *client,
3716 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003717{
3718 wl_resource_destroy(resource);
3719}
3720
3721static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003722viewport_set(struct wl_client *client,
3723 struct wl_resource *resource,
3724 wl_fixed_t src_x,
3725 wl_fixed_t src_y,
3726 wl_fixed_t src_width,
3727 wl_fixed_t src_height,
3728 int32_t dst_width,
3729 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003730{
Jonny Lamb74130762013-11-26 18:19:46 +01003731 struct weston_surface *surface =
3732 wl_resource_get_user_data(resource);
3733
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003734 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003735
Jonny Lamb8ae35902013-11-26 18:19:45 +01003736 if (wl_fixed_to_double(src_width) < 0 ||
3737 wl_fixed_to_double(src_height) < 0) {
3738 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003739 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003740 "source dimensions must be non-negative (%fx%f)",
3741 wl_fixed_to_double(src_width),
3742 wl_fixed_to_double(src_height));
3743 return;
3744 }
3745
3746 if (dst_width <= 0 || dst_height <= 0) {
3747 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003748 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003749 "destination dimensions must be positive (%dx%d)",
3750 dst_width, dst_height);
3751 return;
3752 }
3753
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003754 surface->pending.buffer_viewport.buffer.src_x = src_x;
3755 surface->pending.buffer_viewport.buffer.src_y = src_y;
3756 surface->pending.buffer_viewport.buffer.src_width = src_width;
3757 surface->pending.buffer_viewport.buffer.src_height = src_height;
3758 surface->pending.buffer_viewport.surface.width = dst_width;
3759 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003760 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003761}
3762
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003763static void
3764viewport_set_source(struct wl_client *client,
3765 struct wl_resource *resource,
3766 wl_fixed_t src_x,
3767 wl_fixed_t src_y,
3768 wl_fixed_t src_width,
3769 wl_fixed_t src_height)
3770{
3771 struct weston_surface *surface =
3772 wl_resource_get_user_data(resource);
3773
3774 assert(surface->viewport_resource != NULL);
3775
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003776 if (src_width == wl_fixed_from_int(-1) &&
3777 src_height == wl_fixed_from_int(-1)) {
3778 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003779 surface->pending.buffer_viewport.buffer.src_width =
3780 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003781 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003782 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003783 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003784
3785 if (src_width <= 0 || src_height <= 0) {
3786 wl_resource_post_error(resource,
3787 WL_VIEWPORT_ERROR_BAD_VALUE,
3788 "source size must be positive (%fx%f)",
3789 wl_fixed_to_double(src_width),
3790 wl_fixed_to_double(src_height));
3791 return;
3792 }
3793
3794 surface->pending.buffer_viewport.buffer.src_x = src_x;
3795 surface->pending.buffer_viewport.buffer.src_y = src_y;
3796 surface->pending.buffer_viewport.buffer.src_width = src_width;
3797 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003798 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003799}
3800
3801static void
3802viewport_set_destination(struct wl_client *client,
3803 struct wl_resource *resource,
3804 int32_t dst_width,
3805 int32_t dst_height)
3806{
3807 struct weston_surface *surface =
3808 wl_resource_get_user_data(resource);
3809
3810 assert(surface->viewport_resource != NULL);
3811
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003812 if (dst_width == -1 && dst_height == -1) {
3813 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003814 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003815 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003816 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003817 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003818
3819 if (dst_width <= 0 || dst_height <= 0) {
3820 wl_resource_post_error(resource,
3821 WL_VIEWPORT_ERROR_BAD_VALUE,
3822 "destination size must be positive (%dx%d)",
3823 dst_width, dst_height);
3824 return;
3825 }
3826
3827 surface->pending.buffer_viewport.surface.width = dst_width;
3828 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003829 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003830}
3831
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003832static const struct wl_viewport_interface viewport_interface = {
3833 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003834 viewport_set,
3835 viewport_set_source,
3836 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003837};
3838
3839static void
3840scaler_destroy(struct wl_client *client,
3841 struct wl_resource *resource)
3842{
3843 wl_resource_destroy(resource);
3844}
3845
3846static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003847scaler_get_viewport(struct wl_client *client,
3848 struct wl_resource *scaler,
3849 uint32_t id,
3850 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003851{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003852 int version = wl_resource_get_version(scaler);
3853 struct weston_surface *surface =
3854 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003855 struct wl_resource *resource;
3856
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003857 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003858 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003859 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3860 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003861 return;
3862 }
3863
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003864 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003865 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003866 if (resource == NULL) {
3867 wl_client_post_no_memory(client);
3868 return;
3869 }
3870
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003871 wl_resource_set_implementation(resource, &viewport_interface,
3872 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003873
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003874 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003875}
3876
3877static const struct wl_scaler_interface scaler_interface = {
3878 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003879 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003880};
3881
3882static void
3883bind_scaler(struct wl_client *client,
3884 void *data, uint32_t version, uint32_t id)
3885{
3886 struct wl_resource *resource;
3887
3888 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003889 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003890 if (resource == NULL) {
3891 wl_client_post_no_memory(client);
3892 return;
3893 }
3894
3895 wl_resource_set_implementation(resource, &scaler_interface,
3896 NULL, NULL);
3897}
3898
3899static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003900destroy_presentation_feedback(struct wl_resource *feedback_resource)
3901{
3902 struct weston_presentation_feedback *feedback;
3903
3904 feedback = wl_resource_get_user_data(feedback_resource);
3905
3906 wl_list_remove(&feedback->link);
3907 free(feedback);
3908}
3909
3910static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003911presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3912{
3913 wl_resource_destroy(resource);
3914}
3915
3916static void
3917presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003918 struct wl_resource *presentation_resource,
3919 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003920 uint32_t callback)
3921{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003922 struct weston_surface *surface;
3923 struct weston_presentation_feedback *feedback;
3924
3925 surface = wl_resource_get_user_data(surface_resource);
3926
Bryce Harringtonde16d892014-11-20 22:21:57 -08003927 feedback = zalloc(sizeof *feedback);
3928 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04003929 goto err_calloc;
3930
3931 feedback->resource = wl_resource_create(client,
3932 &presentation_feedback_interface,
3933 1, callback);
3934 if (!feedback->resource)
3935 goto err_create;
3936
3937 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3938 destroy_presentation_feedback);
3939
3940 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3941
3942 return;
3943
3944err_create:
3945 free(feedback);
3946
3947err_calloc:
3948 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003949}
3950
3951static const struct presentation_interface presentation_implementation = {
3952 presentation_destroy,
3953 presentation_feedback
3954};
3955
3956static void
3957bind_presentation(struct wl_client *client,
3958 void *data, uint32_t version, uint32_t id)
3959{
3960 struct weston_compositor *compositor = data;
3961 struct wl_resource *resource;
3962
3963 resource = wl_resource_create(client, &presentation_interface,
3964 MIN(version, 1), id);
3965 if (resource == NULL) {
3966 wl_client_post_no_memory(client);
3967 return;
3968 }
3969
3970 wl_resource_set_implementation(resource, &presentation_implementation,
3971 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003972 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003973}
3974
3975static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003976compositor_bind(struct wl_client *client,
3977 void *data, uint32_t version, uint32_t id)
3978{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003979 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003980 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003981
Jason Ekstranda85118c2013-06-27 20:17:02 -05003982 resource = wl_resource_create(client, &wl_compositor_interface,
3983 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003984 if (resource == NULL) {
3985 wl_client_post_no_memory(client);
3986 return;
3987 }
3988
3989 wl_resource_set_implementation(resource, &compositor_interface,
3990 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003991}
3992
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003993static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003994log_uname(void)
3995{
3996 struct utsname usys;
3997
3998 uname(&usys);
3999
4000 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
4001 usys.version, usys.machine);
4002}
4003
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004004WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02004005weston_environment_get_fd(const char *env)
4006{
4007 char *e, *end;
4008 int fd, flags;
4009
4010 e = getenv(env);
4011 if (!e)
4012 return -1;
4013 fd = strtol(e, &end, 0);
4014 if (*end != '\0')
4015 return -1;
4016
4017 flags = fcntl(fd, F_GETFD);
4018 if (flags == -1)
4019 return -1;
4020
4021 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4022 unsetenv(env);
4023
4024 return fd;
4025}
4026
4027WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004028weston_compositor_init(struct weston_compositor *ec,
4029 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004030 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004031 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004032{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004033 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004034 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004035 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004036
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004037 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004038 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004039 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004040 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004041 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004042 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004043 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004044 wl_signal_init(&ec->idle_signal);
4045 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004046 wl_signal_init(&ec->show_input_panel_signal);
4047 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004048 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004049 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004050 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004051 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004052 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004053 wl_signal_init(&ec->session_signal);
4054 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004055
Casey Dahlin58ba1372012-04-19 22:50:08 -04004056 ec->output_id_pool = 0;
4057
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004058 if (!wl_global_create(display, &wl_compositor_interface, 3,
4059 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004060 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004061
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004062 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4063 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004064 return -1;
4065
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004066 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004067 ec, bind_scaler))
4068 return -1;
4069
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004070 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4071 ec, bind_presentation))
4072 return -1;
4073
Jason Ekstranda7af7042013-10-12 22:38:11 -05004074 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004075 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004076 wl_list_init(&ec->layer_list);
4077 wl_list_init(&ec->seat_list);
4078 wl_list_init(&ec->output_list);
4079 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004080 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004081 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004082 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004083 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004084 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004085
Xiong Zhang97116532013-10-23 13:58:31 +08004086 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004087 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004088
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004089 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4090 weston_config_section_get_string(s, "keymap_rules",
4091 (char **) &xkb_names.rules, NULL);
4092 weston_config_section_get_string(s, "keymap_model",
4093 (char **) &xkb_names.model, NULL);
4094 weston_config_section_get_string(s, "keymap_layout",
4095 (char **) &xkb_names.layout, NULL);
4096 weston_config_section_get_string(s, "keymap_variant",
4097 (char **) &xkb_names.variant, NULL);
4098 weston_config_section_get_string(s, "keymap_options",
4099 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004100
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004101 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4102 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004103
Jonny Lamb66a41a02014-08-12 14:58:25 +02004104 weston_config_section_get_int(s, "repeat-rate",
4105 &ec->kb_repeat_rate, 40);
4106 weston_config_section_get_int(s, "repeat-delay",
4107 &ec->kb_repeat_delay, 400);
4108
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004109 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004110
4111 wl_data_device_manager_init(ec->wl_display);
4112
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004113 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004114
Daniel Stone725c2c32012-06-22 14:04:36 +01004115 loop = wl_display_get_event_loop(ec->wl_display);
4116 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4117 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4118
4119 ec->input_loop = wl_event_loop_create();
4120
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004121 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4122 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4123
4124 weston_compositor_schedule_repaint(ec);
4125
Daniel Stone725c2c32012-06-22 14:04:36 +01004126 return 0;
4127}
4128
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004129WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004130weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004131{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004132 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004133
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004134 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004135 if (ec->input_loop_source)
4136 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004137
Matt Roper361d2ad2011-08-29 13:52:23 -07004138 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004139 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004140 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004141
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004142 if (ec->renderer)
4143 ec->renderer->destroy(ec);
4144
Daniel Stone325fc2d2012-05-30 16:31:58 +01004145 weston_binding_list_destroy_all(&ec->key_binding_list);
4146 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004147 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004148 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004149 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004150
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004151 weston_plane_release(&ec->primary_plane);
4152
Jonas Ådahlc97af922012-03-28 22:36:09 +02004153 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004154
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004155 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004156}
4157
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004158WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004159weston_compositor_exit_with_code(struct weston_compositor *compositor,
4160 int exit_code)
4161{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004162 if (compositor->exit_code == EXIT_SUCCESS)
4163 compositor->exit_code = exit_code;
4164
Frederic Plourdec336f062014-10-29 14:44:33 -04004165 wl_display_terminate(compositor->wl_display);
4166}
4167
4168WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004169weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4170 const struct weston_pointer_grab_interface *interface)
4171{
4172 struct weston_seat *seat;
4173
4174 ec->default_pointer_grab = interface;
4175 wl_list_for_each(seat, &ec->seat_list, link) {
4176 if (seat->pointer) {
4177 weston_pointer_set_default_grab(seat->pointer,
4178 interface);
4179 }
4180 }
4181}
4182
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004183WL_EXPORT int
4184weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4185 clockid_t clk_id)
4186{
4187 struct timespec ts;
4188
4189 if (clock_gettime(clk_id, &ts) < 0)
4190 return -1;
4191
4192 compositor->presentation_clock = clk_id;
4193
4194 return 0;
4195}
4196
4197/*
4198 * For choosing the software clock, when the display hardware or API
4199 * does not expose a compatible presentation timestamp.
4200 */
4201WL_EXPORT int
4202weston_compositor_set_presentation_clock_software(
4203 struct weston_compositor *compositor)
4204{
4205 /* In order of preference */
4206 static const clockid_t clocks[] = {
4207 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4208 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4209 CLOCK_MONOTONIC, /* no jumps, may crawl */
4210 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4211 CLOCK_REALTIME /* may jump and crawl */
4212 };
4213 unsigned i;
4214
4215 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4216 if (weston_compositor_set_presentation_clock(compositor,
4217 clocks[i]) == 0)
4218 return 0;
4219
4220 weston_log("Error: no suitable presentation clock available.\n");
4221
4222 return -1;
4223}
4224
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004225WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004226weston_version(int *major, int *minor, int *micro)
4227{
4228 *major = WESTON_VERSION_MAJOR;
4229 *minor = WESTON_VERSION_MINOR;
4230 *micro = WESTON_VERSION_MICRO;
4231}
4232
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004233static const char *
4234clock_name(clockid_t clk_id)
4235{
4236 static const char *names[] = {
4237 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4238 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4239 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4240 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4241 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4242 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4243 };
4244
4245 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4246 return "unknown";
4247
4248 return names[clk_id];
4249}
4250
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004251static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004252 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004253 const char *desc;
4254} capability_strings[] = {
4255 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004256 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004257};
4258
4259static void
4260weston_compositor_log_capabilities(struct weston_compositor *compositor)
4261{
4262 unsigned i;
4263 int yes;
4264
4265 weston_log("Compositor capabilities:\n");
4266 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4267 yes = compositor->capabilities & capability_strings[i].bit;
4268 weston_log_continue(STAMP_SPACE "%s %s\n",
4269 capability_strings[i].desc,
4270 yes ? "yes" : "no");
4271 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004272
4273 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4274 clock_name(compositor->presentation_clock),
4275 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004276}
4277
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004278static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004279{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004280 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004281
Martin Minarik6d118362012-06-07 18:01:59 +02004282 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004283 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004284
4285 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004286}
4287
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004288#ifdef HAVE_LIBUNWIND
4289
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004290static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004291print_backtrace(void)
4292{
4293 unw_cursor_t cursor;
4294 unw_context_t context;
4295 unw_word_t off;
4296 unw_proc_info_t pip;
4297 int ret, i = 0;
4298 char procname[256];
4299 const char *filename;
4300 Dl_info dlinfo;
4301
4302 pip.unwind_info = NULL;
4303 ret = unw_getcontext(&context);
4304 if (ret) {
4305 weston_log("unw_getcontext: %d\n", ret);
4306 return;
4307 }
4308
4309 ret = unw_init_local(&cursor, &context);
4310 if (ret) {
4311 weston_log("unw_init_local: %d\n", ret);
4312 return;
4313 }
4314
4315 ret = unw_step(&cursor);
4316 while (ret > 0) {
4317 ret = unw_get_proc_info(&cursor, &pip);
4318 if (ret) {
4319 weston_log("unw_get_proc_info: %d\n", ret);
4320 break;
4321 }
4322
4323 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4324 if (ret && ret != -UNW_ENOMEM) {
4325 if (ret != -UNW_EUNSPEC)
4326 weston_log("unw_get_proc_name: %d\n", ret);
4327 procname[0] = '?';
4328 procname[1] = 0;
4329 }
4330
4331 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4332 *dlinfo.dli_fname)
4333 filename = dlinfo.dli_fname;
4334 else
4335 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004336
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004337 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4338 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4339
4340 ret = unw_step(&cursor);
4341 if (ret < 0)
4342 weston_log("unw_step: %d\n", ret);
4343 }
4344}
4345
4346#else
4347
4348static void
4349print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004350{
4351 void *buffer[32];
4352 int i, count;
4353 Dl_info info;
4354
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004355 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4356 for (i = 0; i < count; i++) {
4357 dladdr(buffer[i], &info);
4358 weston_log(" [%016lx] %s (%s)\n",
4359 (long) buffer[i],
4360 info.dli_sname ? info.dli_sname : "--",
4361 info.dli_fname);
4362 }
4363}
4364
4365#endif
4366
4367static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004368on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004369{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004370 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004371 * then call the backend restore function, which will switch
4372 * back to the vt we launched from or ungrab X etc and then
4373 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004374 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004375 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004376 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004377
Peter Maatmane5b42e42013-03-27 22:38:53 +01004378 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004379
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004380 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004381
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004382 segv_compositor->restore(segv_compositor);
4383
4384 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004385}
4386
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004387WL_EXPORT void *
4388weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004389{
4390 char path[PATH_MAX];
4391 void *module, *init;
4392
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004393 if (name == NULL)
4394 return NULL;
4395
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004396 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004397 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004398 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004399 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004400
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004401 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4402 if (module) {
4403 weston_log("Module '%s' already loaded\n", path);
4404 dlclose(module);
4405 return NULL;
4406 }
4407
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004408 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004409 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004410 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004411 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004412 return NULL;
4413 }
4414
4415 init = dlsym(module, entrypoint);
4416 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004417 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004418 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004419 return NULL;
4420 }
4421
4422 return init;
4423}
4424
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004425static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004426load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004427 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004428{
4429 const char *p, *end;
4430 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004431 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004432 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004433
4434 if (modules == NULL)
4435 return 0;
4436
4437 p = modules;
4438 while (*p) {
4439 end = strchrnul(p, ',');
4440 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004441 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004442 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004443 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004444 p = end;
4445 while (*p == ',')
4446 p++;
4447
4448 }
4449
4450 return 0;
4451}
4452
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004453static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004454 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4455
4456static const char xdg_wrong_message[] =
4457 "fatal: environment variable XDG_RUNTIME_DIR\n"
4458 "is set to \"%s\", which is not a directory.\n";
4459
4460static const char xdg_wrong_mode_message[] =
4461 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004462 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4463 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004464
4465static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004466 "Refer to your distribution on how to get it, or\n"
4467 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4468 "on how to implement it.\n";
4469
Martin Minarik37032f82012-06-18 20:15:18 +02004470static void
4471verify_xdg_runtime_dir(void)
4472{
4473 char *dir = getenv("XDG_RUNTIME_DIR");
4474 struct stat s;
4475
4476 if (!dir) {
4477 weston_log(xdg_error_message);
4478 weston_log_continue(xdg_detail_message);
4479 exit(EXIT_FAILURE);
4480 }
4481
4482 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4483 weston_log(xdg_wrong_message, dir);
4484 weston_log_continue(xdg_detail_message);
4485 exit(EXIT_FAILURE);
4486 }
4487
4488 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4489 weston_log(xdg_wrong_mode_message,
4490 dir, s.st_mode & 0777, s.st_uid);
4491 weston_log_continue(xdg_detail_message);
4492 }
4493}
4494
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004495static int
4496usage(int error_code)
4497{
4498 fprintf(stderr,
4499 "Usage: weston [OPTIONS]\n\n"
4500 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4501 "Weston supports multiple backends, and depending on which backend is in use\n"
4502 "different options will be accepted.\n\n"
4503
4504
4505 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004506 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004507 " -B, --backend=MODULE\tBackend module, one of\n"
4508#if defined(BUILD_DRM_COMPOSITOR)
4509 "\t\t\t\tdrm-backend.so\n"
4510#endif
4511#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004512 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004513#endif
4514#if defined(BUILD_X11_COMPOSITOR)
4515 "\t\t\t\tx11-backend.so\n"
4516#endif
4517#if defined(BUILD_WAYLAND_COMPOSITOR)
4518 "\t\t\t\twayland-backend.so\n"
4519#endif
4520#if defined(BUILD_RDP_COMPOSITOR)
4521 "\t\t\t\trdp-backend.so\n"
4522#endif
4523#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4524 "\t\t\t\trpi-backend.so\n"
4525#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004526 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004527 " -S, --socket=NAME\tName of socket to listen on\n"
4528 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004529 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004530 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004531 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004532 " -h, --help\t\tThis help message\n\n");
4533
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004534#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004535 fprintf(stderr,
4536 "Options for drm-backend.so:\n\n"
4537 " --connector=ID\tBring up only this connector\n"
4538 " --seat=SEAT\t\tThe seat that weston should run on\n"
4539 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004540 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004541 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004542#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004543
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004544#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004545 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004546 "Options for fbdev-backend.so:\n\n"
4547 " --tty=TTY\t\tThe tty to use\n"
4548 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004549#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004550
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004551#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004552 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004553 "Options for x11-backend.so:\n\n"
4554 " --width=WIDTH\t\tWidth of X window\n"
4555 " --height=HEIGHT\tHeight of X window\n"
4556 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004557 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004558 " --output-count=COUNT\tCreate multiple outputs\n"
4559 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004560#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004561
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004562#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004563 fprintf(stderr,
4564 "Options for wayland-backend.so:\n\n"
4565 " --width=WIDTH\t\tWidth of Wayland surface\n"
4566 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004567 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004568 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004569 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004570 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004571 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004572 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004573#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004574
Pekka Paalanene31e0532013-05-22 18:03:07 +03004575#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4576 fprintf(stderr,
4577 "Options for rpi-backend.so:\n\n"
4578 " --tty=TTY\t\tThe tty to use\n"
4579 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4580 " --transform=TR\tThe output transformation, TR is one of:\n"
4581 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004582 " --opaque-regions\tEnable support for opaque regions, can be "
4583 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004584 "\n");
4585#endif
4586
Hardeningc077a842013-07-08 00:51:35 +02004587#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004588 fprintf(stderr,
4589 "Options for rdp-backend.so:\n\n"
4590 " --width=WIDTH\t\tWidth of desktop\n"
4591 " --height=HEIGHT\tHeight of desktop\n"
4592 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4593 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004594 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004595 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4596 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4597 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4598 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4599 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004600#endif
4601
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004602#if defined(BUILD_HEADLESS_COMPOSITOR)
4603 fprintf(stderr,
4604 "Options for headless-backend.so:\n\n"
4605 " --width=WIDTH\t\tWidth of memory surface\n"
4606 " --height=HEIGHT\tHeight of memory surface\n"
4607 " --transform=TR\tThe output transformation, TR is one of:\n"
4608 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4609 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4610#endif
4611
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004612 exit(error_code);
4613}
4614
Peter Maatmane5b42e42013-03-27 22:38:53 +01004615static void
4616catch_signals(void)
4617{
4618 struct sigaction action;
4619
4620 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4621 action.sa_sigaction = on_caught_signal;
4622 sigemptyset(&action.sa_mask);
4623 sigaction(SIGSEGV, &action, NULL);
4624 sigaction(SIGABRT, &action, NULL);
4625}
4626
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004627static void
4628handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4629{
4630 struct wl_client *client = data;
4631
4632 weston_log("Primary client died. Closing...\n");
4633
4634 wl_display_terminate(wl_client_get_display(client));
4635}
4636
Ryo Munakatad8deff62014-09-06 07:32:05 +09004637static char *
4638weston_choose_default_backend(void)
4639{
4640 char *backend = NULL;
4641
4642 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4643 backend = strdup("wayland-backend.so");
4644 else if (getenv("DISPLAY"))
4645 backend = strdup("x11-backend.so");
4646 else
4647 backend = strdup(WESTON_NATIVE_BACKEND);
4648
4649 return backend;
4650}
4651
4652static int
4653weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4654{
4655 if (socket_name) {
4656 if (wl_display_add_socket(display, socket_name)) {
4657 weston_log("fatal: failed to add socket: %m\n");
4658 return -1;
4659 }
4660 } else {
4661 socket_name = wl_display_add_socket_auto(display);
4662 if (!socket_name) {
4663 weston_log("fatal: failed to add socket: %m\n");
4664 return -1;
4665 }
4666 }
4667
4668 setenv("WAYLAND_DISPLAY", socket_name, 1);
4669
4670 return 0;
4671}
4672
Derek Foreman64a3df02014-10-23 12:24:18 -05004673static const struct { const char *name; uint32_t token; } transforms[] = {
4674 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4675 { "90", WL_OUTPUT_TRANSFORM_90 },
4676 { "180", WL_OUTPUT_TRANSFORM_180 },
4677 { "270", WL_OUTPUT_TRANSFORM_270 },
4678 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4679 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4680 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4681 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4682};
4683
4684WL_EXPORT int
4685weston_parse_transform(const char *transform, uint32_t *out)
4686{
4687 unsigned int i;
4688
4689 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4690 if (strcmp(transforms[i].name, transform) == 0) {
4691 *out = transforms[i].token;
4692 return 0;
4693 }
4694
4695 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4696 return -1;
4697}
4698
4699WL_EXPORT const char *
4700weston_transform_to_string(uint32_t output_transform)
4701{
4702 unsigned int i;
4703
4704 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4705 if (transforms[i].token == output_transform)
4706 return transforms[i].name;
4707
4708 return "<illegal value>";
4709}
4710
4711
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004712int main(int argc, char *argv[])
4713{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004714 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004715 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004716 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004717 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004718 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004719 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004720 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004721 int *argc, char *argv[],
4722 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004723 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004724 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004725 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004726 char *modules = NULL;
4727 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004728 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004729 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004730 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004731 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004732 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004733 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004734 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004735 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004736 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004737 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004738 struct wl_client *primary_client;
4739 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004740 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004741
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004742 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004743 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4744 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004745 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4746 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004747 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004748 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004749 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004750 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004751 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004752 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004753
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004754 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004755
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004756 if (help)
4757 usage(EXIT_SUCCESS);
4758
Scott Moreau12245142012-08-29 15:15:58 -06004759 if (version) {
4760 printf(PACKAGE_STRING "\n");
4761 return EXIT_SUCCESS;
4762 }
4763
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004764 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004765
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004766 weston_log("%s\n"
4767 STAMP_SPACE "%s\n"
4768 STAMP_SPACE "Bug reports to: %s\n"
4769 STAMP_SPACE "Build: %s\n",
4770 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004771 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004772 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004773
Martin Minarik37032f82012-06-18 20:15:18 +02004774 verify_xdg_runtime_dir();
4775
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004776 display = wl_display_create();
4777
Tiago Vignatti2116b892011-08-08 05:52:59 -07004778 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004779 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4780 display);
4781 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4782 display);
4783 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4784 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004785
4786 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004787 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4788 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004789
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304790 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4791 ret = EXIT_FAILURE;
4792 goto out_signals;
4793 }
4794
Pekka Paalanen588bee12014-05-07 16:26:25 +03004795 if (noconfig == 0)
4796 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004797 if (config != NULL) {
4798 weston_log("Using config file '%s'\n",
4799 weston_config_get_full_path(config));
4800 } else {
4801 weston_log("Starting with no config file.\n");
4802 }
4803 section = weston_config_get_section(config, "core", NULL, NULL);
4804
Ryo Munakata03faed22014-09-06 07:32:51 +09004805 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004806 weston_config_section_get_string(section, "backend", &backend,
4807 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004808 if (!backend)
4809 backend = weston_choose_default_backend();
4810 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004811
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004812 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304813 if (!backend_init) {
4814 ret = EXIT_FAILURE;
4815 goto out_signals;
4816 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004817
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004818 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004819 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004820 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304821 ret = EXIT_FAILURE;
4822 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004823 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004824
Peter Maatmane5b42e42013-03-27 22:38:53 +01004825 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004826 segv_compositor = ec;
4827
Frederic Plourde4a84c832014-10-30 15:06:34 -04004828 if (idle_time < 0)
4829 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4830 if (idle_time < 0)
4831 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004832 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004833 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004834 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004835
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004836 for (i = 1; i < argc; i++)
4837 weston_log("fatal: unhandled option: %s\n", argv[i]);
4838 if (argc > 1) {
4839 ret = EXIT_FAILURE;
4840 goto out;
4841 }
4842
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004843 weston_compositor_log_capabilities(ec);
4844
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004845 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4846 if (server_socket) {
4847 weston_log("Running with single client\n");
4848 fd = strtol(server_socket, &end, 0);
4849 if (*end != '\0')
4850 fd = -1;
4851 } else {
4852 fd = -1;
4853 }
4854
4855 if (fd != -1) {
4856 primary_client = wl_client_create(display, fd);
4857 if (!primary_client) {
4858 weston_log("fatal: failed to add client: %m\n");
4859 ret = EXIT_FAILURE;
4860 goto out;
4861 }
4862 primary_client_destroyed.notify =
4863 handle_primary_client_destroyed;
4864 wl_client_add_destroy_listener(primary_client,
4865 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004866 } else if (weston_create_listening_socket(display, socket_name)) {
4867 ret = EXIT_FAILURE;
4868 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004869 }
4870
Ryo Munakata03faed22014-09-06 07:32:51 +09004871 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004872 weston_config_section_get_string(section, "shell", &shell,
4873 "desktop-shell.so");
4874
Ryo Munakata03faed22014-09-06 07:32:51 +09004875 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004876 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004877
4878 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004879 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004880 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004881
4882 if (load_modules(ec, option_modules, &argc, argv) < 0)
4883 goto out;
4884
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004885 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4886 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4887 if (numlock_on) {
4888 wl_list_for_each(seat, &ec->seat_list, link) {
4889 if (seat->keyboard)
4890 weston_keyboard_set_locks(seat->keyboard,
4891 WESTON_NUM_LOCK,
4892 WESTON_NUM_LOCK);
4893 }
4894 }
4895
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004896 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004897
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004898 wl_display_run(display);
4899
Frederic Plourdec336f062014-10-29 14:44:33 -04004900 /* Allow for setting return exit code after
4901 * wl_display_run returns normally. This is
4902 * useful for devs/testers and automated tests
4903 * that want to indicate failure status to
4904 * testing infrastructure above
4905 */
4906 ret = ec->exit_code;
4907
Ryo Munakata03faed22014-09-06 07:32:51 +09004908out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004909 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004910 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004911
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004912 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004913
Daniel Stone855028f2012-05-01 20:37:10 +01004914 weston_compositor_xkb_destroy(ec);
4915
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004916 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304917
4918out_signals:
4919 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4920 if (signals[i])
4921 wl_event_source_remove(signals[i]);
4922
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004923 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004924
Martin Minarik19e6f262012-06-07 13:08:46 +02004925 weston_log_file_close();
4926
Ryo Munakata03faed22014-09-06 07:32:51 +09004927 free(backend);
4928 free(shell);
4929 free(socket_name);
4930 free(option_modules);
4931 free(log);
4932 free(modules);
4933
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004934 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004935}