blob: 53f6220ea670adeb999b76ac5e7408929cb5ff0b [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
2727static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002728subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002729{
2730 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002731 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732
Jason Ekstranda7af7042013-10-12 22:38:11 -05002733 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002734 weston_view_set_position(view,
2735 view->geometry.x + dx,
2736 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002737
2738 /* No need to check parent mappedness, because if parent is not
2739 * mapped, parent is not in a visible layer, so this sub-surface
2740 * will not be drawn either.
2741 */
2742 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002743 struct weston_output *output;
2744
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002745 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002746 * because that would call it also for the parent surface,
2747 * which might not be mapped yet. That would lead to
2748 * inconsistent state, where the window could never be
2749 * mapped.
2750 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002751 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002752 * weston_surface_is_mapped() return true, so that when the
2753 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002754 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002755 */
2756 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002757 output = container_of(compositor->output_list.next,
2758 struct weston_output, link);
2759
2760 surface->output = output;
2761 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002762 }
2763}
2764
2765static struct weston_subsurface *
2766weston_surface_to_subsurface(struct weston_surface *surface)
2767{
2768 if (surface->configure == subsurface_configure)
2769 return surface->configure_private;
2770
2771 return NULL;
2772}
2773
Pekka Paalanen01388e22013-04-25 13:57:44 +03002774WL_EXPORT struct weston_surface *
2775weston_surface_get_main_surface(struct weston_surface *surface)
2776{
2777 struct weston_subsurface *sub;
2778
2779 while (surface && (sub = weston_surface_to_subsurface(surface)))
2780 surface = sub->parent;
2781
2782 return surface;
2783}
2784
Pekka Paalanen50b67472014-10-01 15:02:41 +03002785WL_EXPORT int
2786weston_surface_set_role(struct weston_surface *surface,
2787 const char *role_name,
2788 struct wl_resource *error_resource,
2789 uint32_t error_code)
2790{
2791 assert(role_name);
2792
2793 if (surface->role_name == NULL ||
2794 surface->role_name == role_name ||
2795 strcmp(surface->role_name, role_name) == 0) {
2796 surface->role_name = role_name;
2797
2798 return 0;
2799 }
2800
2801 wl_resource_post_error(error_resource, error_code,
2802 "Cannot assign role %s to wl_surface@%d,"
2803 " already has role %s\n",
2804 role_name,
2805 wl_resource_get_id(surface->resource),
2806 surface->role_name);
2807 return -1;
2808}
2809
Pekka Paalanene67858b2013-04-25 13:57:42 +03002810static void
2811subsurface_set_position(struct wl_client *client,
2812 struct wl_resource *resource, int32_t x, int32_t y)
2813{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002814 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002815
2816 if (!sub)
2817 return;
2818
2819 sub->position.x = x;
2820 sub->position.y = y;
2821 sub->position.set = 1;
2822}
2823
2824static struct weston_subsurface *
2825subsurface_from_surface(struct weston_surface *surface)
2826{
2827 struct weston_subsurface *sub;
2828
2829 sub = weston_surface_to_subsurface(surface);
2830 if (sub)
2831 return sub;
2832
2833 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2834 if (sub->surface == surface)
2835 return sub;
2836
2837 return NULL;
2838}
2839
2840static struct weston_subsurface *
2841subsurface_sibling_check(struct weston_subsurface *sub,
2842 struct weston_surface *surface,
2843 const char *request)
2844{
2845 struct weston_subsurface *sibling;
2846
2847 sibling = subsurface_from_surface(surface);
2848
2849 if (!sibling) {
2850 wl_resource_post_error(sub->resource,
2851 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2852 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002853 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002854 return NULL;
2855 }
2856
2857 if (sibling->parent != sub->parent) {
2858 wl_resource_post_error(sub->resource,
2859 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2860 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002861 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002862 return NULL;
2863 }
2864
2865 return sibling;
2866}
2867
2868static void
2869subsurface_place_above(struct wl_client *client,
2870 struct wl_resource *resource,
2871 struct wl_resource *sibling_resource)
2872{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002873 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002874 struct weston_surface *surface =
2875 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002876 struct weston_subsurface *sibling;
2877
2878 if (!sub)
2879 return;
2880
2881 sibling = subsurface_sibling_check(sub, surface, "place_above");
2882 if (!sibling)
2883 return;
2884
2885 wl_list_remove(&sub->parent_link_pending);
2886 wl_list_insert(sibling->parent_link_pending.prev,
2887 &sub->parent_link_pending);
2888}
2889
2890static void
2891subsurface_place_below(struct wl_client *client,
2892 struct wl_resource *resource,
2893 struct wl_resource *sibling_resource)
2894{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002895 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002896 struct weston_surface *surface =
2897 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002898 struct weston_subsurface *sibling;
2899
2900 if (!sub)
2901 return;
2902
2903 sibling = subsurface_sibling_check(sub, surface, "place_below");
2904 if (!sibling)
2905 return;
2906
2907 wl_list_remove(&sub->parent_link_pending);
2908 wl_list_insert(&sibling->parent_link_pending,
2909 &sub->parent_link_pending);
2910}
2911
2912static void
2913subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2914{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002915 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002916
2917 if (sub)
2918 sub->synchronized = 1;
2919}
2920
2921static void
2922subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2923{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002924 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002925
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002926 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002927 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002928
2929 /* If sub became effectively desynchronized, flush. */
2930 if (!weston_subsurface_is_synchronized(sub))
2931 weston_subsurface_synchronized_commit(sub);
2932 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002933}
2934
2935static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002936weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2937{
2938 wl_list_remove(&sub->parent_link);
2939 wl_list_remove(&sub->parent_link_pending);
2940 wl_list_remove(&sub->parent_destroy_listener.link);
2941 sub->parent = NULL;
2942}
2943
2944static void
2945weston_subsurface_destroy(struct weston_subsurface *sub);
2946
2947static void
2948subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2949{
2950 struct weston_subsurface *sub =
2951 container_of(listener, struct weston_subsurface,
2952 surface_destroy_listener);
2953 assert(data == &sub->surface->resource);
2954
2955 /* The protocol object (wl_resource) is left inert. */
2956 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002957 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002958
2959 weston_subsurface_destroy(sub);
2960}
2961
2962static void
2963subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2964{
2965 struct weston_subsurface *sub =
2966 container_of(listener, struct weston_subsurface,
2967 parent_destroy_listener);
2968 assert(data == &sub->parent->resource);
2969 assert(sub->surface != sub->parent);
2970
2971 if (weston_surface_is_mapped(sub->surface))
2972 weston_surface_unmap(sub->surface);
2973
2974 weston_subsurface_unlink_parent(sub);
2975}
2976
2977static void
2978subsurface_resource_destroy(struct wl_resource *resource)
2979{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002980 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002981
2982 if (sub)
2983 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002984}
2985
2986static void
2987subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2988{
2989 wl_resource_destroy(resource);
2990}
2991
2992static void
2993weston_subsurface_link_parent(struct weston_subsurface *sub,
2994 struct weston_surface *parent)
2995{
2996 sub->parent = parent;
2997 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002998 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002999 &sub->parent_destroy_listener);
3000
3001 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3002 wl_list_insert(&parent->subsurface_list_pending,
3003 &sub->parent_link_pending);
3004}
3005
3006static void
3007weston_subsurface_link_surface(struct weston_subsurface *sub,
3008 struct weston_surface *surface)
3009{
3010 sub->surface = surface;
3011 sub->surface_destroy_listener.notify =
3012 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003013 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03003014 &sub->surface_destroy_listener);
3015}
3016
3017static void
3018weston_subsurface_destroy(struct weston_subsurface *sub)
3019{
Jason Ekstranda7af7042013-10-12 22:38:11 -05003020 struct weston_view *view, *next;
3021
Pekka Paalanene67858b2013-04-25 13:57:42 +03003022 assert(sub->surface);
3023
3024 if (sub->resource) {
3025 assert(weston_surface_to_subsurface(sub->surface) == sub);
3026 assert(sub->parent_destroy_listener.notify ==
3027 subsurface_handle_parent_destroy);
3028
George Kiagiadakised04d382014-06-13 18:10:26 +02003029 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
3030 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003031 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02003032 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05003033
Pekka Paalanene67858b2013-04-25 13:57:42 +03003034 if (sub->parent)
3035 weston_subsurface_unlink_parent(sub);
3036
Jason Ekstrand7b982072014-05-20 14:33:03 -05003037 weston_surface_state_fini(&sub->cached);
3038 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003039
3040 sub->surface->configure = NULL;
3041 sub->surface->configure_private = NULL;
3042 } else {
3043 /* the dummy weston_subsurface for the parent itself */
3044 assert(sub->parent_destroy_listener.notify == NULL);
3045 wl_list_remove(&sub->parent_link);
3046 wl_list_remove(&sub->parent_link_pending);
3047 }
3048
3049 wl_list_remove(&sub->surface_destroy_listener.link);
3050 free(sub);
3051}
3052
3053static const struct wl_subsurface_interface subsurface_implementation = {
3054 subsurface_destroy,
3055 subsurface_set_position,
3056 subsurface_place_above,
3057 subsurface_place_below,
3058 subsurface_set_sync,
3059 subsurface_set_desync
3060};
3061
3062static struct weston_subsurface *
3063weston_subsurface_create(uint32_t id, struct weston_surface *surface,
3064 struct weston_surface *parent)
3065{
3066 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05003067 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003068
Bryce Harringtonde16d892014-11-20 22:21:57 -08003069 sub = zalloc(sizeof *sub);
3070 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003071 return NULL;
3072
Jason Ekstranda7af7042013-10-12 22:38:11 -05003073 wl_list_init(&sub->unused_views);
3074
Jason Ekstranda85118c2013-06-27 20:17:02 -05003075 sub->resource =
3076 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003077 if (!sub->resource) {
3078 free(sub);
3079 return NULL;
3080 }
3081
Jason Ekstranda85118c2013-06-27 20:17:02 -05003082 wl_resource_set_implementation(sub->resource,
3083 &subsurface_implementation,
3084 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003085 weston_subsurface_link_surface(sub, surface);
3086 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05003087 weston_surface_state_init(&sub->cached);
3088 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003089 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003090
3091 return sub;
3092}
3093
3094/* Create a dummy subsurface for having the parent itself in its
3095 * sub-surface lists. Makes stacking order manipulation easy.
3096 */
3097static struct weston_subsurface *
3098weston_subsurface_create_for_parent(struct weston_surface *parent)
3099{
3100 struct weston_subsurface *sub;
3101
Bryce Harringtonde16d892014-11-20 22:21:57 -08003102 sub = zalloc(sizeof *sub);
3103 if (sub == NULL)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003104 return NULL;
3105
3106 weston_subsurface_link_surface(sub, parent);
3107 sub->parent = parent;
3108 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
3109 wl_list_insert(&parent->subsurface_list_pending,
3110 &sub->parent_link_pending);
3111
3112 return sub;
3113}
3114
3115static void
3116subcompositor_get_subsurface(struct wl_client *client,
3117 struct wl_resource *resource,
3118 uint32_t id,
3119 struct wl_resource *surface_resource,
3120 struct wl_resource *parent_resource)
3121{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05003122 struct weston_surface *surface =
3123 wl_resource_get_user_data(surface_resource);
3124 struct weston_surface *parent =
3125 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003126 struct weston_subsurface *sub;
3127 static const char where[] = "get_subsurface: wl_subsurface@";
3128
3129 if (surface == parent) {
3130 wl_resource_post_error(resource,
3131 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3132 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003133 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003134 return;
3135 }
3136
3137 if (weston_surface_to_subsurface(surface)) {
3138 wl_resource_post_error(resource,
3139 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3140 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003141 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003142 return;
3143 }
3144
Pekka Paalanen50b67472014-10-01 15:02:41 +03003145 if (weston_surface_set_role(surface, "wl_subsurface", resource,
3146 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE) < 0)
Pekka Paalanene67858b2013-04-25 13:57:42 +03003147 return;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003148
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003149 if (weston_surface_get_main_surface(parent) == surface) {
3150 wl_resource_post_error(resource,
3151 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3152 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003153 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003154 return;
3155 }
3156
Pekka Paalanene67858b2013-04-25 13:57:42 +03003157 /* make sure the parent is in its own list */
3158 if (wl_list_empty(&parent->subsurface_list)) {
3159 if (!weston_subsurface_create_for_parent(parent)) {
3160 wl_resource_post_no_memory(resource);
3161 return;
3162 }
3163 }
3164
3165 sub = weston_subsurface_create(id, surface, parent);
3166 if (!sub) {
3167 wl_resource_post_no_memory(resource);
3168 return;
3169 }
3170
3171 surface->configure = subsurface_configure;
3172 surface->configure_private = sub;
3173}
3174
3175static void
3176subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3177{
3178 wl_resource_destroy(resource);
3179}
3180
3181static const struct wl_subcompositor_interface subcompositor_interface = {
3182 subcompositor_destroy,
3183 subcompositor_get_subsurface
3184};
3185
3186static void
3187bind_subcompositor(struct wl_client *client,
3188 void *data, uint32_t version, uint32_t id)
3189{
3190 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003191 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003192
Jason Ekstranda85118c2013-06-27 20:17:02 -05003193 resource =
3194 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003195 if (resource == NULL) {
3196 wl_client_post_no_memory(client);
3197 return;
3198 }
3199 wl_resource_set_implementation(resource, &subcompositor_interface,
3200 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003201}
3202
3203static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003204weston_compositor_dpms(struct weston_compositor *compositor,
3205 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003206{
3207 struct weston_output *output;
3208
3209 wl_list_for_each(output, &compositor->output_list, link)
3210 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003211 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003212}
3213
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003214WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003215weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003216{
Neil Roberts8b62e202013-09-30 13:14:47 +01003217 uint32_t old_state = compositor->state;
3218
3219 /* The state needs to be changed before emitting the wake
3220 * signal because that may try to schedule a repaint which
3221 * will not work if the compositor is still sleeping */
3222 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3223
3224 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003225 case WESTON_COMPOSITOR_SLEEPING:
3226 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3227 /* fall through */
3228 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003229 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003230 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003231 /* fall through */
3232 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003233 wl_event_source_timer_update(compositor->idle_source,
3234 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003235 }
3236}
3237
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003238WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003239weston_compositor_offscreen(struct weston_compositor *compositor)
3240{
3241 switch (compositor->state) {
3242 case WESTON_COMPOSITOR_OFFSCREEN:
3243 return;
3244 case WESTON_COMPOSITOR_SLEEPING:
3245 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3246 /* fall through */
3247 default:
3248 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3249 wl_event_source_timer_update(compositor->idle_source, 0);
3250 }
3251}
3252
3253WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003254weston_compositor_sleep(struct weston_compositor *compositor)
3255{
3256 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3257 return;
3258
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003259 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003260 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3261 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3262}
3263
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003264static int
3265idle_handler(void *data)
3266{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003267 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003268
3269 if (compositor->idle_inhibit)
3270 return 1;
3271
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003272 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003273 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003274
3275 return 1;
3276}
3277
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003278WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003279weston_plane_init(struct weston_plane *plane,
3280 struct weston_compositor *ec,
3281 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003282{
3283 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003284 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003285 plane->x = x;
3286 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003287 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003288
3289 /* Init the link so that the call to wl_list_remove() when releasing
3290 * the plane without ever stacking doesn't lead to a crash */
3291 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003292}
3293
3294WL_EXPORT void
3295weston_plane_release(struct weston_plane *plane)
3296{
Xiong Zhang97116532013-10-23 13:58:31 +08003297 struct weston_view *view;
3298
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003299 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003300 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003301
Xiong Zhang97116532013-10-23 13:58:31 +08003302 wl_list_for_each(view, &plane->compositor->view_list, link) {
3303 if (view->plane == plane)
3304 view->plane = NULL;
3305 }
3306
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003307 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003308}
3309
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003310WL_EXPORT void
3311weston_compositor_stack_plane(struct weston_compositor *ec,
3312 struct weston_plane *plane,
3313 struct weston_plane *above)
3314{
3315 if (above)
3316 wl_list_insert(above->link.prev, &plane->link);
3317 else
3318 wl_list_insert(&ec->plane_list, &plane->link);
3319}
3320
Casey Dahlin9074db52012-04-19 22:50:09 -04003321static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003322{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003323 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003324}
3325
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003326static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003327bind_output(struct wl_client *client,
3328 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003329{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003330 struct weston_output *output = data;
3331 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003332 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003333
Jason Ekstranda85118c2013-06-27 20:17:02 -05003334 resource = wl_resource_create(client, &wl_output_interface,
3335 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003336 if (resource == NULL) {
3337 wl_client_post_no_memory(client);
3338 return;
3339 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003340
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003341 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003342 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003343
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003344 wl_output_send_geometry(resource,
3345 output->x,
3346 output->y,
3347 output->mm_width,
3348 output->mm_height,
3349 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003350 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003351 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003352 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003353 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003354 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003355
3356 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003357 wl_output_send_mode(resource,
3358 mode->flags,
3359 mode->width,
3360 mode->height,
3361 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003362 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003363
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003364 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003365 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003366}
3367
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003368/* Move other outputs when one is removed so the space remains contiguos. */
3369static void
3370weston_compositor_remove_output(struct weston_compositor *compositor,
3371 struct weston_output *remove_output)
3372{
3373 struct weston_output *output;
3374 int offset = 0;
3375
3376 wl_list_for_each(output, &compositor->output_list, link) {
3377 if (output == remove_output) {
3378 offset = output->width;
3379 continue;
3380 }
3381
3382 if (offset > 0) {
3383 weston_output_move(output,
3384 output->x - offset, output->y);
3385 output->dirty = 1;
3386 }
3387 }
3388}
3389
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003390WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003391weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003392{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003393 struct wl_resource *resource;
3394
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003395 output->destroying = 1;
3396
Pekka Paalanen133e4392014-09-23 22:08:46 -04003397 weston_presentation_feedback_discard_list(&output->feedback_list);
3398
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003399 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003400 wl_list_remove(&output->link);
3401
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003402 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003403 wl_signal_emit(&output->destroy_signal, output);
3404
Richard Hughesafe690c2013-05-02 10:10:04 +01003405 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003406 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003407 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003408 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003409
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003410 wl_resource_for_each(resource, &output->resource_list) {
3411 wl_resource_set_destructor(resource, NULL);
3412 }
3413
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003414 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003415}
3416
Scott Moreau1bad5db2012-08-18 01:04:05 -06003417static void
3418weston_output_compute_transform(struct weston_output *output)
3419{
3420 struct weston_matrix transform;
3421 int flip;
3422
3423 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003424 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003425
3426 switch(output->transform) {
3427 case WL_OUTPUT_TRANSFORM_FLIPPED:
3428 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3429 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3430 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003431 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003432 flip = -1;
3433 break;
3434 default:
3435 flip = 1;
3436 break;
3437 }
3438
3439 switch(output->transform) {
3440 case WL_OUTPUT_TRANSFORM_NORMAL:
3441 case WL_OUTPUT_TRANSFORM_FLIPPED:
3442 transform.d[0] = flip;
3443 transform.d[1] = 0;
3444 transform.d[4] = 0;
3445 transform.d[5] = 1;
3446 break;
3447 case WL_OUTPUT_TRANSFORM_90:
3448 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3449 transform.d[0] = 0;
3450 transform.d[1] = -flip;
3451 transform.d[4] = 1;
3452 transform.d[5] = 0;
3453 break;
3454 case WL_OUTPUT_TRANSFORM_180:
3455 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3456 transform.d[0] = -flip;
3457 transform.d[1] = 0;
3458 transform.d[4] = 0;
3459 transform.d[5] = -1;
3460 break;
3461 case WL_OUTPUT_TRANSFORM_270:
3462 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3463 transform.d[0] = 0;
3464 transform.d[1] = flip;
3465 transform.d[4] = -1;
3466 transform.d[5] = 0;
3467 break;
3468 default:
3469 break;
3470 }
3471
3472 weston_matrix_multiply(&output->matrix, &transform);
3473}
3474
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003475WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003476weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003477{
Scott Moreau850ca422012-05-21 15:21:25 -06003478 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003479
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003480 weston_matrix_init(&output->matrix);
3481 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003482 -(output->x + output->width / 2.0),
3483 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003484
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003485 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003486 2.0 / output->width,
3487 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003488
Scott Moreauccbf29d2012-02-22 14:21:41 -07003489 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003490 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003491 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003492 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3493 output->zoom.trans_y, 0);
3494 weston_matrix_scale(&output->matrix, magnification,
3495 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003496 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003497
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003498 weston_output_compute_transform(output);
3499
Scott Moreauccbf29d2012-02-22 14:21:41 -07003500 output->dirty = 0;
3501}
3502
Scott Moreau1bad5db2012-08-18 01:04:05 -06003503static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003504weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003505{
3506 output->transform = transform;
3507
3508 switch (transform) {
3509 case WL_OUTPUT_TRANSFORM_90:
3510 case WL_OUTPUT_TRANSFORM_270:
3511 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3512 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3513 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003514 output->width = output->current_mode->height;
3515 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003516 break;
3517 case WL_OUTPUT_TRANSFORM_NORMAL:
3518 case WL_OUTPUT_TRANSFORM_180:
3519 case WL_OUTPUT_TRANSFORM_FLIPPED:
3520 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003521 output->width = output->current_mode->width;
3522 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003523 break;
3524 default:
3525 break;
3526 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003527
Hardening57388e42013-09-18 23:56:36 +02003528 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003529 output->width /= scale;
3530 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003531}
3532
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003533static void
3534weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003535{
3536 output->x = x;
3537 output->y = y;
3538
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003539 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003540 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003541 output->width,
3542 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003543}
3544
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003545WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003546weston_output_move(struct weston_output *output, int x, int y)
3547{
3548 pixman_region32_t old_region;
3549 struct wl_resource *resource;
3550
3551 output->move_x = x - output->x;
3552 output->move_y = y - output->y;
3553
3554 if (output->move_x == 0 && output->move_y == 0)
3555 return;
3556
3557 pixman_region32_init(&old_region);
3558 pixman_region32_copy(&old_region, &output->region);
3559
3560 weston_output_init_geometry(output, x, y);
3561
3562 output->dirty = 1;
3563
3564 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003565 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003566
3567 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003568 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003569 wl_output_send_geometry(resource,
3570 output->x,
3571 output->y,
3572 output->mm_width,
3573 output->mm_height,
3574 output->subpixel,
3575 output->make,
3576 output->model,
3577 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003578
3579 if (wl_resource_get_version(resource) >= 2)
3580 wl_output_send_done(resource);
3581 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003582}
3583
3584WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003585weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003586 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003587 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003588{
3589 output->compositor = c;
3590 output->x = x;
3591 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003592 output->mm_width = mm_width;
3593 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003594 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003595 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003596
Alexander Larsson0b135062013-05-28 16:23:36 +02003597 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003598 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003599
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003600 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003601 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003602
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003603 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003604 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003605 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003606 wl_list_init(&output->resource_list);
Pekka Paalanen133e4392014-09-23 22:08:46 -04003607 wl_list_init(&output->feedback_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003608
Casey Dahlin58ba1372012-04-19 22:50:08 -04003609 output->id = ffs(~output->compositor->output_id_pool) - 1;
3610 output->compositor->output_id_pool |= 1 << output->id;
3611
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003612 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003613 wl_global_create(c->wl_display, &wl_output_interface, 2,
3614 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003615 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003616}
3617
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003618WL_EXPORT void
3619weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003620 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003621 wl_fixed_t *x, wl_fixed_t *y)
3622{
3623 wl_fixed_t tx, ty;
3624 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003625 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003626
Hardeningff39efa2013-09-18 23:56:35 +02003627 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3628 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003629
3630 switch(output->transform) {
3631 case WL_OUTPUT_TRANSFORM_NORMAL:
3632 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003633 tx = device_x;
3634 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003635 break;
3636 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003637 tx = device_y;
3638 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003639 break;
3640 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003641 tx = width - device_x;
3642 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003643 break;
3644 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003645 tx = width - device_y;
3646 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003647 break;
3648 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003649 tx = width - device_x;
3650 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003651 break;
3652 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003653 tx = width - device_y;
3654 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003655 break;
3656 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003657 tx = device_x;
3658 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003659 break;
3660 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003661 tx = device_y;
3662 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003663 break;
3664 }
3665
Neil Robertseb5a2002014-05-01 16:13:55 +01003666 tx /= output->current_scale;
3667 ty /= output->current_scale;
3668
3669 if (output->zoom.active) {
3670 zoom_scale = output->zoom.spring_z.current;
3671 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3672 output->width / 2.0f *
3673 (zoom_scale + output->zoom.trans_x));
3674 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3675 output->height / 2.0f *
3676 (zoom_scale + output->zoom.trans_y));
3677 tx = wl_fixed_from_double(zx);
3678 ty = wl_fixed_from_double(zy);
3679 }
3680
3681 *x = tx + wl_fixed_from_int(output->x);
3682 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003683}
3684
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003685static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003686destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003687{
Jonny Lamb74130762013-11-26 18:19:46 +01003688 struct weston_surface *surface =
3689 wl_resource_get_user_data(resource);
3690
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003691 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003692 surface->pending.buffer_viewport.buffer.src_width =
3693 wl_fixed_from_int(-1);
3694 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003695 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003696}
3697
3698static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003699viewport_destroy(struct wl_client *client,
3700 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003701{
3702 wl_resource_destroy(resource);
3703}
3704
3705static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003706viewport_set(struct wl_client *client,
3707 struct wl_resource *resource,
3708 wl_fixed_t src_x,
3709 wl_fixed_t src_y,
3710 wl_fixed_t src_width,
3711 wl_fixed_t src_height,
3712 int32_t dst_width,
3713 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003714{
Jonny Lamb74130762013-11-26 18:19:46 +01003715 struct weston_surface *surface =
3716 wl_resource_get_user_data(resource);
3717
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003718 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003719
Jonny Lamb8ae35902013-11-26 18:19:45 +01003720 if (wl_fixed_to_double(src_width) < 0 ||
3721 wl_fixed_to_double(src_height) < 0) {
3722 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003723 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003724 "source dimensions must be non-negative (%fx%f)",
3725 wl_fixed_to_double(src_width),
3726 wl_fixed_to_double(src_height));
3727 return;
3728 }
3729
3730 if (dst_width <= 0 || dst_height <= 0) {
3731 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003732 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003733 "destination dimensions must be positive (%dx%d)",
3734 dst_width, dst_height);
3735 return;
3736 }
3737
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003738 surface->pending.buffer_viewport.buffer.src_x = src_x;
3739 surface->pending.buffer_viewport.buffer.src_y = src_y;
3740 surface->pending.buffer_viewport.buffer.src_width = src_width;
3741 surface->pending.buffer_viewport.buffer.src_height = src_height;
3742 surface->pending.buffer_viewport.surface.width = dst_width;
3743 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003744 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003745}
3746
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003747static void
3748viewport_set_source(struct wl_client *client,
3749 struct wl_resource *resource,
3750 wl_fixed_t src_x,
3751 wl_fixed_t src_y,
3752 wl_fixed_t src_width,
3753 wl_fixed_t src_height)
3754{
3755 struct weston_surface *surface =
3756 wl_resource_get_user_data(resource);
3757
3758 assert(surface->viewport_resource != NULL);
3759
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003760 if (src_width == wl_fixed_from_int(-1) &&
3761 src_height == wl_fixed_from_int(-1)) {
3762 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003763 surface->pending.buffer_viewport.buffer.src_width =
3764 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003765 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003766 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003767 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003768
3769 if (src_width <= 0 || src_height <= 0) {
3770 wl_resource_post_error(resource,
3771 WL_VIEWPORT_ERROR_BAD_VALUE,
3772 "source size must be positive (%fx%f)",
3773 wl_fixed_to_double(src_width),
3774 wl_fixed_to_double(src_height));
3775 return;
3776 }
3777
3778 surface->pending.buffer_viewport.buffer.src_x = src_x;
3779 surface->pending.buffer_viewport.buffer.src_y = src_y;
3780 surface->pending.buffer_viewport.buffer.src_width = src_width;
3781 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003782 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003783}
3784
3785static void
3786viewport_set_destination(struct wl_client *client,
3787 struct wl_resource *resource,
3788 int32_t dst_width,
3789 int32_t dst_height)
3790{
3791 struct weston_surface *surface =
3792 wl_resource_get_user_data(resource);
3793
3794 assert(surface->viewport_resource != NULL);
3795
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003796 if (dst_width == -1 && dst_height == -1) {
3797 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003798 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003799 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003800 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003801 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003802
3803 if (dst_width <= 0 || dst_height <= 0) {
3804 wl_resource_post_error(resource,
3805 WL_VIEWPORT_ERROR_BAD_VALUE,
3806 "destination size must be positive (%dx%d)",
3807 dst_width, dst_height);
3808 return;
3809 }
3810
3811 surface->pending.buffer_viewport.surface.width = dst_width;
3812 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003813 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003814}
3815
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003816static const struct wl_viewport_interface viewport_interface = {
3817 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003818 viewport_set,
3819 viewport_set_source,
3820 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003821};
3822
3823static void
3824scaler_destroy(struct wl_client *client,
3825 struct wl_resource *resource)
3826{
3827 wl_resource_destroy(resource);
3828}
3829
3830static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003831scaler_get_viewport(struct wl_client *client,
3832 struct wl_resource *scaler,
3833 uint32_t id,
3834 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003835{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003836 int version = wl_resource_get_version(scaler);
3837 struct weston_surface *surface =
3838 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003839 struct wl_resource *resource;
3840
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003841 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003842 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003843 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3844 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003845 return;
3846 }
3847
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003848 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003849 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003850 if (resource == NULL) {
3851 wl_client_post_no_memory(client);
3852 return;
3853 }
3854
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003855 wl_resource_set_implementation(resource, &viewport_interface,
3856 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003857
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003858 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003859}
3860
3861static const struct wl_scaler_interface scaler_interface = {
3862 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003863 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003864};
3865
3866static void
3867bind_scaler(struct wl_client *client,
3868 void *data, uint32_t version, uint32_t id)
3869{
3870 struct wl_resource *resource;
3871
3872 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003873 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003874 if (resource == NULL) {
3875 wl_client_post_no_memory(client);
3876 return;
3877 }
3878
3879 wl_resource_set_implementation(resource, &scaler_interface,
3880 NULL, NULL);
3881}
3882
3883static void
Pekka Paalanen133e4392014-09-23 22:08:46 -04003884destroy_presentation_feedback(struct wl_resource *feedback_resource)
3885{
3886 struct weston_presentation_feedback *feedback;
3887
3888 feedback = wl_resource_get_user_data(feedback_resource);
3889
3890 wl_list_remove(&feedback->link);
3891 free(feedback);
3892}
3893
3894static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003895presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3896{
3897 wl_resource_destroy(resource);
3898}
3899
3900static void
3901presentation_feedback(struct wl_client *client,
Pekka Paalanen133e4392014-09-23 22:08:46 -04003902 struct wl_resource *presentation_resource,
3903 struct wl_resource *surface_resource,
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003904 uint32_t callback)
3905{
Pekka Paalanen133e4392014-09-23 22:08:46 -04003906 struct weston_surface *surface;
3907 struct weston_presentation_feedback *feedback;
3908
3909 surface = wl_resource_get_user_data(surface_resource);
3910
Bryce Harringtonde16d892014-11-20 22:21:57 -08003911 feedback = zalloc(sizeof *feedback);
3912 if (feedback == NULL)
Pekka Paalanen133e4392014-09-23 22:08:46 -04003913 goto err_calloc;
3914
3915 feedback->resource = wl_resource_create(client,
3916 &presentation_feedback_interface,
3917 1, callback);
3918 if (!feedback->resource)
3919 goto err_create;
3920
3921 wl_resource_set_implementation(feedback->resource, NULL, feedback,
3922 destroy_presentation_feedback);
3923
3924 wl_list_insert(&surface->pending.feedback_list, &feedback->link);
3925
3926 return;
3927
3928err_create:
3929 free(feedback);
3930
3931err_calloc:
3932 wl_client_post_no_memory(client);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003933}
3934
3935static const struct presentation_interface presentation_implementation = {
3936 presentation_destroy,
3937 presentation_feedback
3938};
3939
3940static void
3941bind_presentation(struct wl_client *client,
3942 void *data, uint32_t version, uint32_t id)
3943{
3944 struct weston_compositor *compositor = data;
3945 struct wl_resource *resource;
3946
3947 resource = wl_resource_create(client, &presentation_interface,
3948 MIN(version, 1), id);
3949 if (resource == NULL) {
3950 wl_client_post_no_memory(client);
3951 return;
3952 }
3953
3954 wl_resource_set_implementation(resource, &presentation_implementation,
3955 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003956 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003957}
3958
3959static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003960compositor_bind(struct wl_client *client,
3961 void *data, uint32_t version, uint32_t id)
3962{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003963 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003964 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003965
Jason Ekstranda85118c2013-06-27 20:17:02 -05003966 resource = wl_resource_create(client, &wl_compositor_interface,
3967 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003968 if (resource == NULL) {
3969 wl_client_post_no_memory(client);
3970 return;
3971 }
3972
3973 wl_resource_set_implementation(resource, &compositor_interface,
3974 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003975}
3976
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003977static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003978log_uname(void)
3979{
3980 struct utsname usys;
3981
3982 uname(&usys);
3983
3984 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3985 usys.version, usys.machine);
3986}
3987
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003988WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003989weston_environment_get_fd(const char *env)
3990{
3991 char *e, *end;
3992 int fd, flags;
3993
3994 e = getenv(env);
3995 if (!e)
3996 return -1;
3997 fd = strtol(e, &end, 0);
3998 if (*end != '\0')
3999 return -1;
4000
4001 flags = fcntl(fd, F_GETFD);
4002 if (flags == -1)
4003 return -1;
4004
4005 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
4006 unsetenv(env);
4007
4008 return fd;
4009}
4010
4011WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04004012weston_compositor_init(struct weston_compositor *ec,
4013 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004014 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004015 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04004016{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05004017 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01004018 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004019 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07004020
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004021 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04004022 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004023 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07004024 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004025 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03004026 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03004027 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02004028 wl_signal_init(&ec->idle_signal);
4029 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02004030 wl_signal_init(&ec->show_input_panel_signal);
4031 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02004032 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004033 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01004034 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02004035 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02004036 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07004037 wl_signal_init(&ec->session_signal);
4038 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004039
Casey Dahlin58ba1372012-04-19 22:50:08 -04004040 ec->output_id_pool = 0;
4041
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004042 if (!wl_global_create(display, &wl_compositor_interface, 3,
4043 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05004044 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05004045
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04004046 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
4047 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03004048 return -1;
4049
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02004050 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01004051 ec, bind_scaler))
4052 return -1;
4053
Pekka Paalanen31f7d782014-09-23 22:08:43 -04004054 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
4055 ec, bind_presentation))
4056 return -1;
4057
Jason Ekstranda7af7042013-10-12 22:38:11 -05004058 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004059 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004060 wl_list_init(&ec->layer_list);
4061 wl_list_init(&ec->seat_list);
4062 wl_list_init(&ec->output_list);
4063 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01004064 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004065 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004066 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004067 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004068 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01004069
Xiong Zhang97116532013-10-23 13:58:31 +08004070 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02004071 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004072
Kristian Høgsberg6a047912013-05-23 15:56:29 -04004073 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
4074 weston_config_section_get_string(s, "keymap_rules",
4075 (char **) &xkb_names.rules, NULL);
4076 weston_config_section_get_string(s, "keymap_model",
4077 (char **) &xkb_names.model, NULL);
4078 weston_config_section_get_string(s, "keymap_layout",
4079 (char **) &xkb_names.layout, NULL);
4080 weston_config_section_get_string(s, "keymap_variant",
4081 (char **) &xkb_names.variant, NULL);
4082 weston_config_section_get_string(s, "keymap_options",
4083 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01004084
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05004085 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
4086 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01004087
Jonny Lamb66a41a02014-08-12 14:58:25 +02004088 weston_config_section_get_int(s, "repeat-rate",
4089 &ec->kb_repeat_rate, 40);
4090 weston_config_section_get_int(s, "repeat-delay",
4091 &ec->kb_repeat_delay, 400);
4092
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01004093 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01004094
4095 wl_data_device_manager_init(ec->wl_display);
4096
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04004097 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004098
Daniel Stone725c2c32012-06-22 14:04:36 +01004099 loop = wl_display_get_event_loop(ec->wl_display);
4100 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
4101 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
4102
4103 ec->input_loop = wl_event_loop_create();
4104
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04004105 weston_layer_init(&ec->fade_layer, &ec->layer_list);
4106 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
4107
4108 weston_compositor_schedule_repaint(ec);
4109
Daniel Stone725c2c32012-06-22 14:04:36 +01004110 return 0;
4111}
4112
Benjamin Franzkeb8263022011-08-30 11:32:47 +02004113WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004114weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07004115{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004116 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07004117
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004118 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02004119 if (ec->input_loop_source)
4120 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004121
Matt Roper361d2ad2011-08-29 13:52:23 -07004122 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02004123 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07004124 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02004125
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02004126 if (ec->renderer)
4127 ec->renderer->destroy(ec);
4128
Daniel Stone325fc2d2012-05-30 16:31:58 +01004129 weston_binding_list_destroy_all(&ec->key_binding_list);
4130 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01004131 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01004132 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02004133 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02004134
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04004135 weston_plane_release(&ec->primary_plane);
4136
Jonas Ådahlc97af922012-03-28 22:36:09 +02004137 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07004138
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004139 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07004140}
4141
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004142WL_EXPORT void
Frederic Plourdec336f062014-10-29 14:44:33 -04004143weston_compositor_exit_with_code(struct weston_compositor *compositor,
4144 int exit_code)
4145{
Pekka Paalanenf5ef88f2014-11-18 15:57:04 +02004146 if (compositor->exit_code == EXIT_SUCCESS)
4147 compositor->exit_code = exit_code;
4148
Frederic Plourdec336f062014-10-29 14:44:33 -04004149 wl_display_terminate(compositor->wl_display);
4150}
4151
4152WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004153weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
4154 const struct weston_pointer_grab_interface *interface)
4155{
4156 struct weston_seat *seat;
4157
4158 ec->default_pointer_grab = interface;
4159 wl_list_for_each(seat, &ec->seat_list, link) {
4160 if (seat->pointer) {
4161 weston_pointer_set_default_grab(seat->pointer,
4162 interface);
4163 }
4164 }
4165}
4166
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004167WL_EXPORT int
4168weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
4169 clockid_t clk_id)
4170{
4171 struct timespec ts;
4172
4173 if (clock_gettime(clk_id, &ts) < 0)
4174 return -1;
4175
4176 compositor->presentation_clock = clk_id;
4177
4178 return 0;
4179}
4180
4181/*
4182 * For choosing the software clock, when the display hardware or API
4183 * does not expose a compatible presentation timestamp.
4184 */
4185WL_EXPORT int
4186weston_compositor_set_presentation_clock_software(
4187 struct weston_compositor *compositor)
4188{
4189 /* In order of preference */
4190 static const clockid_t clocks[] = {
4191 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4192 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4193 CLOCK_MONOTONIC, /* no jumps, may crawl */
4194 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4195 CLOCK_REALTIME /* may jump and crawl */
4196 };
4197 unsigned i;
4198
4199 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4200 if (weston_compositor_set_presentation_clock(compositor,
4201 clocks[i]) == 0)
4202 return 0;
4203
4204 weston_log("Error: no suitable presentation clock available.\n");
4205
4206 return -1;
4207}
4208
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004209WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004210weston_version(int *major, int *minor, int *micro)
4211{
4212 *major = WESTON_VERSION_MAJOR;
4213 *minor = WESTON_VERSION_MINOR;
4214 *micro = WESTON_VERSION_MICRO;
4215}
4216
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004217static const char *
4218clock_name(clockid_t clk_id)
4219{
4220 static const char *names[] = {
4221 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4222 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4223 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4224 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4225 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4226 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4227 };
4228
4229 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4230 return "unknown";
4231
4232 return names[clk_id];
4233}
4234
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004235static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004236 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004237 const char *desc;
4238} capability_strings[] = {
4239 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004240 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004241};
4242
4243static void
4244weston_compositor_log_capabilities(struct weston_compositor *compositor)
4245{
4246 unsigned i;
4247 int yes;
4248
4249 weston_log("Compositor capabilities:\n");
4250 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4251 yes = compositor->capabilities & capability_strings[i].bit;
4252 weston_log_continue(STAMP_SPACE "%s %s\n",
4253 capability_strings[i].desc,
4254 yes ? "yes" : "no");
4255 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004256
4257 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4258 clock_name(compositor->presentation_clock),
4259 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004260}
4261
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004262static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004263{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004264 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004265
Martin Minarik6d118362012-06-07 18:01:59 +02004266 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004267 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004268
4269 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004270}
4271
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004272#ifdef HAVE_LIBUNWIND
4273
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004274static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004275print_backtrace(void)
4276{
4277 unw_cursor_t cursor;
4278 unw_context_t context;
4279 unw_word_t off;
4280 unw_proc_info_t pip;
4281 int ret, i = 0;
4282 char procname[256];
4283 const char *filename;
4284 Dl_info dlinfo;
4285
4286 pip.unwind_info = NULL;
4287 ret = unw_getcontext(&context);
4288 if (ret) {
4289 weston_log("unw_getcontext: %d\n", ret);
4290 return;
4291 }
4292
4293 ret = unw_init_local(&cursor, &context);
4294 if (ret) {
4295 weston_log("unw_init_local: %d\n", ret);
4296 return;
4297 }
4298
4299 ret = unw_step(&cursor);
4300 while (ret > 0) {
4301 ret = unw_get_proc_info(&cursor, &pip);
4302 if (ret) {
4303 weston_log("unw_get_proc_info: %d\n", ret);
4304 break;
4305 }
4306
4307 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4308 if (ret && ret != -UNW_ENOMEM) {
4309 if (ret != -UNW_EUNSPEC)
4310 weston_log("unw_get_proc_name: %d\n", ret);
4311 procname[0] = '?';
4312 procname[1] = 0;
4313 }
4314
4315 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4316 *dlinfo.dli_fname)
4317 filename = dlinfo.dli_fname;
4318 else
4319 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004320
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004321 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4322 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4323
4324 ret = unw_step(&cursor);
4325 if (ret < 0)
4326 weston_log("unw_step: %d\n", ret);
4327 }
4328}
4329
4330#else
4331
4332static void
4333print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004334{
4335 void *buffer[32];
4336 int i, count;
4337 Dl_info info;
4338
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004339 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4340 for (i = 0; i < count; i++) {
4341 dladdr(buffer[i], &info);
4342 weston_log(" [%016lx] %s (%s)\n",
4343 (long) buffer[i],
4344 info.dli_sname ? info.dli_sname : "--",
4345 info.dli_fname);
4346 }
4347}
4348
4349#endif
4350
4351static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004352on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004353{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004354 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004355 * then call the backend restore function, which will switch
4356 * back to the vt we launched from or ungrab X etc and then
4357 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004358 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004359 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004360 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004361
Peter Maatmane5b42e42013-03-27 22:38:53 +01004362 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004363
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004364 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004365
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004366 segv_compositor->restore(segv_compositor);
4367
4368 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004369}
4370
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004371WL_EXPORT void *
4372weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004373{
4374 char path[PATH_MAX];
4375 void *module, *init;
4376
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004377 if (name == NULL)
4378 return NULL;
4379
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004380 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004381 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004382 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004383 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004384
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004385 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4386 if (module) {
4387 weston_log("Module '%s' already loaded\n", path);
4388 dlclose(module);
4389 return NULL;
4390 }
4391
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004392 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004393 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004394 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004395 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004396 return NULL;
4397 }
4398
4399 init = dlsym(module, entrypoint);
4400 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004401 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004402 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004403 return NULL;
4404 }
4405
4406 return init;
4407}
4408
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004409static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004410load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004411 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004412{
4413 const char *p, *end;
4414 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004415 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004416 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004417
4418 if (modules == NULL)
4419 return 0;
4420
4421 p = modules;
4422 while (*p) {
4423 end = strchrnul(p, ',');
4424 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004425 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004426 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004427 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004428 p = end;
4429 while (*p == ',')
4430 p++;
4431
4432 }
4433
4434 return 0;
4435}
4436
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004437static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004438 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4439
4440static const char xdg_wrong_message[] =
4441 "fatal: environment variable XDG_RUNTIME_DIR\n"
4442 "is set to \"%s\", which is not a directory.\n";
4443
4444static const char xdg_wrong_mode_message[] =
4445 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004446 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4447 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004448
4449static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004450 "Refer to your distribution on how to get it, or\n"
4451 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4452 "on how to implement it.\n";
4453
Martin Minarik37032f82012-06-18 20:15:18 +02004454static void
4455verify_xdg_runtime_dir(void)
4456{
4457 char *dir = getenv("XDG_RUNTIME_DIR");
4458 struct stat s;
4459
4460 if (!dir) {
4461 weston_log(xdg_error_message);
4462 weston_log_continue(xdg_detail_message);
4463 exit(EXIT_FAILURE);
4464 }
4465
4466 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4467 weston_log(xdg_wrong_message, dir);
4468 weston_log_continue(xdg_detail_message);
4469 exit(EXIT_FAILURE);
4470 }
4471
4472 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4473 weston_log(xdg_wrong_mode_message,
4474 dir, s.st_mode & 0777, s.st_uid);
4475 weston_log_continue(xdg_detail_message);
4476 }
4477}
4478
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004479static int
4480usage(int error_code)
4481{
4482 fprintf(stderr,
4483 "Usage: weston [OPTIONS]\n\n"
4484 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4485 "Weston supports multiple backends, and depending on which backend is in use\n"
4486 "different options will be accepted.\n\n"
4487
4488
4489 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004490 " --version\t\tPrint weston version\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004491 " -B, --backend=MODULE\tBackend module, one of\n"
4492#if defined(BUILD_DRM_COMPOSITOR)
4493 "\t\t\t\tdrm-backend.so\n"
4494#endif
4495#if defined(BUILD_FBDEV_COMPOSITOR)
Pekka Paalanen86b70e12014-11-11 14:10:46 +02004496 "\t\t\t\tfbdev-backend.so\n"
Bryce Harrington32297c92014-10-23 15:25:04 -07004497#endif
4498#if defined(BUILD_X11_COMPOSITOR)
4499 "\t\t\t\tx11-backend.so\n"
4500#endif
4501#if defined(BUILD_WAYLAND_COMPOSITOR)
4502 "\t\t\t\twayland-backend.so\n"
4503#endif
4504#if defined(BUILD_RDP_COMPOSITOR)
4505 "\t\t\t\trdp-backend.so\n"
4506#endif
4507#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4508 "\t\t\t\trpi-backend.so\n"
4509#endif
Jason Ekstranda985da42013-08-22 17:28:03 -05004510 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004511 " -S, --socket=NAME\tName of socket to listen on\n"
4512 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004513 " --modules\t\tLoad the comma-separated list of modules\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004514 " --log=FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004515 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004516 " -h, --help\t\tThis help message\n\n");
4517
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004518#if defined(BUILD_DRM_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004519 fprintf(stderr,
4520 "Options for drm-backend.so:\n\n"
4521 " --connector=ID\tBring up only this connector\n"
4522 " --seat=SEAT\t\tThe seat that weston should run on\n"
4523 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004524 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004525 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004526#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004527
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004528#if defined(BUILD_FBDEV_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004529 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004530 "Options for fbdev-backend.so:\n\n"
4531 " --tty=TTY\t\tThe tty to use\n"
4532 " --device=DEVICE\tThe framebuffer device to use\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004533#endif
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004534
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004535#if defined(BUILD_X11_COMPOSITOR)
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004536 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004537 "Options for x11-backend.so:\n\n"
4538 " --width=WIDTH\t\tWidth of X window\n"
4539 " --height=HEIGHT\tHeight of X window\n"
4540 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004541 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004542 " --output-count=COUNT\tCreate multiple outputs\n"
4543 " --no-input\t\tDont create input devices\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004544#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004545
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004546#if defined(BUILD_WAYLAND_COMPOSITOR)
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004547 fprintf(stderr,
4548 "Options for wayland-backend.so:\n\n"
4549 " --width=WIDTH\t\tWidth of Wayland surface\n"
4550 " --height=HEIGHT\tHeight of Wayland surface\n"
Bryce Harringtonb8b25bd2014-10-23 14:44:36 -07004551 " --scale=SCALE\t\tScale factor of output\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004552 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004553 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004554 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004555 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004556 " --display=DISPLAY\tWayland display to connect to\n\n");
Bryce Harrington8eb95c42014-10-23 15:25:03 -07004557#endif
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004558
Pekka Paalanene31e0532013-05-22 18:03:07 +03004559#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4560 fprintf(stderr,
4561 "Options for rpi-backend.so:\n\n"
4562 " --tty=TTY\t\tThe tty to use\n"
4563 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4564 " --transform=TR\tThe output transformation, TR is one of:\n"
4565 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004566 " --opaque-regions\tEnable support for opaque regions, can be "
4567 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004568 "\n");
4569#endif
4570
Hardeningc077a842013-07-08 00:51:35 +02004571#if defined(BUILD_RDP_COMPOSITOR)
Bryce Harrington59fe4232014-10-23 14:44:34 -07004572 fprintf(stderr,
4573 "Options for rdp-backend.so:\n\n"
4574 " --width=WIDTH\t\tWidth of desktop\n"
4575 " --height=HEIGHT\tHeight of desktop\n"
4576 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4577 " --address=ADDR\tThe address to bind\n"
Bryce Harringtonf5b34ae2014-10-23 14:44:35 -07004578 " --port=PORT\t\tThe port to listen on\n"
Bryce Harrington59fe4232014-10-23 14:44:34 -07004579 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
4580 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4581 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4582 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4583 "\n");
Hardeningc077a842013-07-08 00:51:35 +02004584#endif
4585
Bryce Harrington3e3b59e2014-11-19 15:06:19 -08004586#if defined(BUILD_HEADLESS_COMPOSITOR)
4587 fprintf(stderr,
4588 "Options for headless-backend.so:\n\n"
4589 " --width=WIDTH\t\tWidth of memory surface\n"
4590 " --height=HEIGHT\tHeight of memory surface\n"
4591 " --transform=TR\tThe output transformation, TR is one of:\n"
4592 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
4593 " --use-pixman\t\tUse the pixman (CPU) renderer (default: no rendering)\n\n");
4594#endif
4595
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004596 exit(error_code);
4597}
4598
Peter Maatmane5b42e42013-03-27 22:38:53 +01004599static void
4600catch_signals(void)
4601{
4602 struct sigaction action;
4603
4604 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4605 action.sa_sigaction = on_caught_signal;
4606 sigemptyset(&action.sa_mask);
4607 sigaction(SIGSEGV, &action, NULL);
4608 sigaction(SIGABRT, &action, NULL);
4609}
4610
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004611static void
4612handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4613{
4614 struct wl_client *client = data;
4615
4616 weston_log("Primary client died. Closing...\n");
4617
4618 wl_display_terminate(wl_client_get_display(client));
4619}
4620
Ryo Munakatad8deff62014-09-06 07:32:05 +09004621static char *
4622weston_choose_default_backend(void)
4623{
4624 char *backend = NULL;
4625
4626 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4627 backend = strdup("wayland-backend.so");
4628 else if (getenv("DISPLAY"))
4629 backend = strdup("x11-backend.so");
4630 else
4631 backend = strdup(WESTON_NATIVE_BACKEND);
4632
4633 return backend;
4634}
4635
4636static int
4637weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4638{
4639 if (socket_name) {
4640 if (wl_display_add_socket(display, socket_name)) {
4641 weston_log("fatal: failed to add socket: %m\n");
4642 return -1;
4643 }
4644 } else {
4645 socket_name = wl_display_add_socket_auto(display);
4646 if (!socket_name) {
4647 weston_log("fatal: failed to add socket: %m\n");
4648 return -1;
4649 }
4650 }
4651
4652 setenv("WAYLAND_DISPLAY", socket_name, 1);
4653
4654 return 0;
4655}
4656
Derek Foreman64a3df02014-10-23 12:24:18 -05004657static const struct { const char *name; uint32_t token; } transforms[] = {
4658 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
4659 { "90", WL_OUTPUT_TRANSFORM_90 },
4660 { "180", WL_OUTPUT_TRANSFORM_180 },
4661 { "270", WL_OUTPUT_TRANSFORM_270 },
4662 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
4663 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
4664 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
4665 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
4666};
4667
4668WL_EXPORT int
4669weston_parse_transform(const char *transform, uint32_t *out)
4670{
4671 unsigned int i;
4672
4673 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4674 if (strcmp(transforms[i].name, transform) == 0) {
4675 *out = transforms[i].token;
4676 return 0;
4677 }
4678
4679 *out = WL_OUTPUT_TRANSFORM_NORMAL;
4680 return -1;
4681}
4682
4683WL_EXPORT const char *
4684weston_transform_to_string(uint32_t output_transform)
4685{
4686 unsigned int i;
4687
4688 for (i = 0; i < ARRAY_LENGTH(transforms); i++)
4689 if (transforms[i].token == output_transform)
4690 return transforms[i].name;
4691
4692 return "<illegal value>";
4693}
4694
4695
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004696int main(int argc, char *argv[])
4697{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004698 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004699 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004700 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004701 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004702 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004703 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004704 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004705 int *argc, char *argv[],
4706 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004707 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004708 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004709 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004710 char *modules = NULL;
4711 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004712 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004713 char *server_socket = NULL, *end;
Frederic Plourde4a84c832014-10-30 15:06:34 -04004714 int32_t idle_time = -1;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004715 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004716 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004717 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004718 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004719 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004720 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004721 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004722 struct wl_client *primary_client;
4723 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004724 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004725
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004726 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004727 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4728 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004729 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4730 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004731 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004732 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004733 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004734 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004735 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004736 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004737
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004738 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004739
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004740 if (help)
4741 usage(EXIT_SUCCESS);
4742
Scott Moreau12245142012-08-29 15:15:58 -06004743 if (version) {
4744 printf(PACKAGE_STRING "\n");
4745 return EXIT_SUCCESS;
4746 }
4747
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004748 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004749
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004750 weston_log("%s\n"
4751 STAMP_SPACE "%s\n"
4752 STAMP_SPACE "Bug reports to: %s\n"
4753 STAMP_SPACE "Build: %s\n",
4754 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004755 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004756 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004757
Martin Minarik37032f82012-06-18 20:15:18 +02004758 verify_xdg_runtime_dir();
4759
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004760 display = wl_display_create();
4761
Tiago Vignatti2116b892011-08-08 05:52:59 -07004762 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004763 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4764 display);
4765 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4766 display);
4767 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4768 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004769
4770 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004771 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4772 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004773
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304774 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4775 ret = EXIT_FAILURE;
4776 goto out_signals;
4777 }
4778
Pekka Paalanen588bee12014-05-07 16:26:25 +03004779 if (noconfig == 0)
4780 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004781 if (config != NULL) {
4782 weston_log("Using config file '%s'\n",
4783 weston_config_get_full_path(config));
4784 } else {
4785 weston_log("Starting with no config file.\n");
4786 }
4787 section = weston_config_get_section(config, "core", NULL, NULL);
4788
Ryo Munakata03faed22014-09-06 07:32:51 +09004789 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004790 weston_config_section_get_string(section, "backend", &backend,
4791 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004792 if (!backend)
4793 backend = weston_choose_default_backend();
4794 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004795
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004796 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304797 if (!backend_init) {
4798 ret = EXIT_FAILURE;
4799 goto out_signals;
4800 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004801
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004802 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004803 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004804 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304805 ret = EXIT_FAILURE;
4806 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004807 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004808
Peter Maatmane5b42e42013-03-27 22:38:53 +01004809 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004810 segv_compositor = ec;
4811
Frederic Plourde4a84c832014-10-30 15:06:34 -04004812 if (idle_time < 0)
4813 weston_config_section_get_int(section, "idle-time", &idle_time, -1);
4814 if (idle_time < 0)
4815 idle_time = 300; /* default idle timeout, in seconds */
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004816 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004817 ec->default_pointer_grab = NULL;
Frederic Plourdec336f062014-10-29 14:44:33 -04004818 ec->exit_code = EXIT_SUCCESS;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004819
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004820 for (i = 1; i < argc; i++)
4821 weston_log("fatal: unhandled option: %s\n", argv[i]);
4822 if (argc > 1) {
4823 ret = EXIT_FAILURE;
4824 goto out;
4825 }
4826
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004827 weston_compositor_log_capabilities(ec);
4828
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004829 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4830 if (server_socket) {
4831 weston_log("Running with single client\n");
4832 fd = strtol(server_socket, &end, 0);
4833 if (*end != '\0')
4834 fd = -1;
4835 } else {
4836 fd = -1;
4837 }
4838
4839 if (fd != -1) {
4840 primary_client = wl_client_create(display, fd);
4841 if (!primary_client) {
4842 weston_log("fatal: failed to add client: %m\n");
4843 ret = EXIT_FAILURE;
4844 goto out;
4845 }
4846 primary_client_destroyed.notify =
4847 handle_primary_client_destroyed;
4848 wl_client_add_destroy_listener(primary_client,
4849 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004850 } else if (weston_create_listening_socket(display, socket_name)) {
4851 ret = EXIT_FAILURE;
4852 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004853 }
4854
Ryo Munakata03faed22014-09-06 07:32:51 +09004855 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004856 weston_config_section_get_string(section, "shell", &shell,
4857 "desktop-shell.so");
4858
Ryo Munakata03faed22014-09-06 07:32:51 +09004859 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004860 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004861
4862 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004863 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004864 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004865
4866 if (load_modules(ec, option_modules, &argc, argv) < 0)
4867 goto out;
4868
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004869 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4870 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4871 if (numlock_on) {
4872 wl_list_for_each(seat, &ec->seat_list, link) {
4873 if (seat->keyboard)
4874 weston_keyboard_set_locks(seat->keyboard,
4875 WESTON_NUM_LOCK,
4876 WESTON_NUM_LOCK);
4877 }
4878 }
4879
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004880 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004881
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004882 wl_display_run(display);
4883
Frederic Plourdec336f062014-10-29 14:44:33 -04004884 /* Allow for setting return exit code after
4885 * wl_display_run returns normally. This is
4886 * useful for devs/testers and automated tests
4887 * that want to indicate failure status to
4888 * testing infrastructure above
4889 */
4890 ret = ec->exit_code;
4891
Ryo Munakata03faed22014-09-06 07:32:51 +09004892out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004893 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004894 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004895
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004896 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004897
Daniel Stone855028f2012-05-01 20:37:10 +01004898 weston_compositor_xkb_destroy(ec);
4899
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004900 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304901
4902out_signals:
4903 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4904 if (signals[i])
4905 wl_event_source_remove(signals[i]);
4906
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004907 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004908
Martin Minarik19e6f262012-06-07 13:08:46 +02004909 weston_log_file_close();
4910
Ryo Munakata03faed22014-09-06 07:32:51 +09004911 free(backend);
4912 free(shell);
4913 free(socket_name);
4914 free(option_modules);
4915 free(log);
4916 free(modules);
4917
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004918 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004919}