blob: f34d712205a8846a4d83723deefc5caaae5d1f08 [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
Alex Wu2dda6042012-04-17 17:20:47 +0800101WL_EXPORT int
Hardening57388e42013-09-18 23:56:36 +0200102weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode,
103 int32_t scale, enum weston_mode_switch_op op)
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;
Hardening57388e42013-09-18 23:56:36 +0200108 int ret, notify_mode_changed, notify_scale_changed;
109 int temporary_mode, temporary_scale;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200110
Alex Wu2dda6042012-04-17 17:20:47 +0800111 if (!output->switch_mode)
112 return -1;
113
Hardening57388e42013-09-18 23:56:36 +0200114 temporary_mode = (output->original_mode != 0);
115 temporary_scale = (output->current_scale != output->original_scale);
116 ret = 0;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200117
Hardening57388e42013-09-18 23:56:36 +0200118 notify_mode_changed = 0;
119 notify_scale_changed = 0;
120 switch(op) {
121 case WESTON_MODE_SWITCH_SET_NATIVE:
122 output->native_mode = mode;
123 if (!temporary_mode) {
124 notify_mode_changed = 1;
125 ret = output->switch_mode(output, mode);
126 if (ret < 0)
127 return ret;
128 }
129
130 output->native_scale = scale;
131 if(!temporary_scale)
132 notify_scale_changed = 1;
133 break;
134 case WESTON_MODE_SWITCH_SET_TEMPORARY:
135 if (!temporary_mode)
136 output->original_mode = output->native_mode;
137 if (!temporary_scale)
138 output->original_scale = output->native_scale;
139
140 ret = output->switch_mode(output, mode);
141 if (ret < 0)
142 return ret;
143
Hardening57388e42013-09-18 23:56:36 +0200144 output->current_scale = scale;
145 break;
146 case WESTON_MODE_SWITCH_RESTORE_NATIVE:
147 if (!temporary_mode) {
148 weston_log("already in the native mode\n");
149 return -1;
150 }
151
152 notify_mode_changed = (output->original_mode != output->native_mode);
153
154 ret = output->switch_mode(output, mode);
155 if (ret < 0)
156 return ret;
157
158 if (output->original_scale != output->native_scale) {
159 notify_scale_changed = 1;
160 scale = output->native_scale;
161 output->original_scale = scale;
162 }
163 output->original_mode = 0;
164
165 output->current_scale = output->native_scale;
166 break;
167 default:
168 weston_log("unknown weston_switch_mode_op %d\n", op);
169 break;
170 }
Alexander Larsson355748e2013-05-28 16:23:38 +0200171
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200172 pixman_region32_init(&old_output_region);
173 pixman_region32_copy(&old_output_region, &output->region);
174
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200175 /* Update output region and transformation matrix */
Hardeningff39efa2013-09-18 23:56:35 +0200176 weston_output_transform_scale_init(output, output->transform, output->current_scale);
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200177
178 pixman_region32_init(&output->previous_damage);
179 pixman_region32_init_rect(&output->region, output->x, output->y,
180 output->width, output->height);
181
182 weston_output_update_matrix(output);
183
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200184 /* If a pointer falls outside the outputs new geometry, move it to its
185 * lower-right corner */
186 wl_list_for_each(seat, &output->compositor->seat_list, link) {
Kristian Høgsberge3148752013-05-06 23:19:49 -0400187 struct weston_pointer *pointer = seat->pointer;
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200188 int32_t x, y;
189
190 if (!pointer)
191 continue;
192
193 x = wl_fixed_to_int(pointer->x);
194 y = wl_fixed_to_int(pointer->y);
195
196 if (!pixman_region32_contains_point(&old_output_region,
197 x, y, NULL) ||
198 pixman_region32_contains_point(&output->region,
199 x, y, NULL))
200 continue;
201
202 if (x >= output->x + output->width)
203 x = output->x + output->width - 1;
204 if (y >= output->y + output->height)
205 y = output->y + output->height - 1;
206
207 pointer->x = wl_fixed_from_int(x);
208 pointer->y = wl_fixed_from_int(y);
209 }
210
211 pixman_region32_fini(&old_output_region);
212
Hardening57388e42013-09-18 23:56:36 +0200213 /* notify clients of the changes */
214 if (notify_mode_changed || notify_scale_changed) {
215 wl_resource_for_each(resource, &output->resource_list) {
216 if(notify_mode_changed) {
217 wl_output_send_mode(resource,
218 mode->flags | WL_OUTPUT_MODE_CURRENT,
219 mode->width,
220 mode->height,
221 mode->refresh);
222 }
223
224 if (notify_scale_changed)
225 wl_output_send_scale(resource, scale);
226
227 if (wl_resource_get_version(resource) >= 2)
228 wl_output_send_done(resource);
229 }
230 }
231
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200232 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800233}
234
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400235WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500236weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400237{
238 wl_list_insert(&child_process_list, &process->link);
239}
240
Benjamin Franzke06286262011-05-06 19:12:33 +0200241static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200242child_client_exec(int sockfd, const char *path)
243{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500244 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200245 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200246 sigset_t allsigs;
247
248 /* do not give our signal mask to the new process */
249 sigfillset(&allsigs);
250 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200251
Alexandru DAMIAN840a4212013-09-25 14:47:47 +0100252 /* Launch clients as the user. Do not lauch clients with wrong euid.*/
253 if (seteuid(getuid()) == -1) {
254 weston_log("compositor: failed seteuid\n");
255 return;
256 }
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500257
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500258 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
259 * non-CLOEXEC fd to pass through exec. */
260 clientfd = dup(sockfd);
261 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200262 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500263 return;
264 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200265
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500266 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200267 setenv("WAYLAND_SOCKET", s, 1);
268
269 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200270 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200271 path);
272}
273
274WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500275weston_client_launch(struct weston_compositor *compositor,
276 struct weston_process *proc,
277 const char *path,
278 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200279{
280 int sv[2];
281 pid_t pid;
282 struct wl_client *client;
283
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300284 weston_log("launching '%s'\n", path);
285
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300286 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200287 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200288 "socketpair failed while launching '%s': %m\n",
289 path);
290 return NULL;
291 }
292
293 pid = fork();
294 if (pid == -1) {
295 close(sv[0]);
296 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200298 "fork failed while launching '%s': %m\n", path);
299 return NULL;
300 }
301
302 if (pid == 0) {
303 child_client_exec(sv[1], path);
U. Artie Eoff3b64d622013-06-03 16:22:31 -0700304 _exit(-1);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200305 }
306
307 close(sv[1]);
308
309 client = wl_client_create(compositor->wl_display, sv[0]);
310 if (!client) {
311 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200312 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200313 "wl_client_create failed while launching '%s'.\n",
314 path);
315 return NULL;
316 }
317
318 proc->pid = pid;
319 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500320 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200321
322 return client;
323}
324
Pekka Paalanen9c1ac7b2014-08-27 12:03:38 +0300325struct process_info {
326 struct weston_process proc;
327 char *path;
328};
329
330static void
331process_handle_sigchld(struct weston_process *process, int status)
332{
333 struct process_info *pinfo =
334 container_of(process, struct process_info, proc);
335
336 /*
337 * There are no guarantees whether this runs before or after
338 * the wl_client destructor.
339 */
340
341 if (WIFEXITED(status)) {
342 weston_log("%s exited with status %d\n", pinfo->path,
343 WEXITSTATUS(status));
344 } else if (WIFSIGNALED(status)) {
345 weston_log("%s died on signal %d\n", pinfo->path,
346 WTERMSIG(status));
347 } else {
348 weston_log("%s disappeared\n", pinfo->path);
349 }
350
351 free(pinfo->path);
352 free(pinfo);
353}
354
355WL_EXPORT struct wl_client *
356weston_client_start(struct weston_compositor *compositor, const char *path)
357{
358 struct process_info *pinfo;
359 struct wl_client *client;
360
361 pinfo = zalloc(sizeof *pinfo);
362 if (!pinfo)
363 return NULL;
364
365 pinfo->path = strdup(path);
366 if (!pinfo->path)
367 goto out_free;
368
369 client = weston_client_launch(compositor, &pinfo->proc, path,
370 process_handle_sigchld);
371 if (!client)
372 goto out_str;
373
374 return client;
375
376out_str:
377 free(pinfo->path);
378
379out_free:
380 free(pinfo);
381
382 return NULL;
383}
384
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200385static void
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300386region_init_infinite(pixman_region32_t *region)
387{
388 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
389 UINT32_MAX, UINT32_MAX);
390}
391
Pekka Paalanene67858b2013-04-25 13:57:42 +0300392static struct weston_subsurface *
393weston_surface_to_subsurface(struct weston_surface *surface);
394
Jason Ekstranda7af7042013-10-12 22:38:11 -0500395WL_EXPORT struct weston_view *
396weston_view_create(struct weston_surface *surface)
397{
398 struct weston_view *view;
399
400 view = calloc(1, sizeof *view);
401 if (view == NULL)
402 return NULL;
403
404 view->surface = surface;
405
Jason Ekstranda7af7042013-10-12 22:38:11 -0500406 /* Assign to surface */
407 wl_list_insert(&surface->views, &view->surface_link);
408
409 wl_signal_init(&view->destroy_signal);
410 wl_list_init(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300411 wl_list_init(&view->layer_link.link);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500412
Xiong Zhang97116532013-10-23 13:58:31 +0800413 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +0300414 view->layer_link.layer = NULL;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300415 view->parent_view = NULL;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500416
417 pixman_region32_init(&view->clip);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300418 pixman_region32_init(&view->transform.masked_boundingbox);
419 pixman_region32_init(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -0500420
421 view->alpha = 1.0;
422 pixman_region32_init(&view->transform.opaque);
423
424 wl_list_init(&view->geometry.transformation_list);
425 wl_list_insert(&view->geometry.transformation_list,
426 &view->transform.position.link);
427 weston_matrix_init(&view->transform.position.matrix);
428 wl_list_init(&view->geometry.child_list);
429 pixman_region32_init(&view->transform.boundingbox);
430 view->transform.dirty = 1;
431
432 view->output = NULL;
433
434 return view;
435}
436
Jason Ekstrand108865d2014-06-26 10:04:49 -0700437struct weston_frame_callback {
438 struct wl_resource *resource;
439 struct wl_list link;
440};
441
Jason Ekstrand7b982072014-05-20 14:33:03 -0500442static void
443surface_state_handle_buffer_destroy(struct wl_listener *listener, void *data)
444{
445 struct weston_surface_state *state =
446 container_of(listener, struct weston_surface_state,
447 buffer_destroy_listener);
448
449 state->buffer = NULL;
450}
451
452static void
453weston_surface_state_init(struct weston_surface_state *state)
454{
455 state->newly_attached = 0;
456 state->buffer = NULL;
457 state->buffer_destroy_listener.notify =
458 surface_state_handle_buffer_destroy;
459 state->sx = 0;
460 state->sy = 0;
461
462 pixman_region32_init(&state->damage);
463 pixman_region32_init(&state->opaque);
464 region_init_infinite(&state->input);
465
466 wl_list_init(&state->frame_callback_list);
467
468 state->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
469 state->buffer_viewport.buffer.scale = 1;
470 state->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
471 state->buffer_viewport.surface.width = -1;
472 state->buffer_viewport.changed = 0;
473}
474
475static void
476weston_surface_state_fini(struct weston_surface_state *state)
477{
478 struct weston_frame_callback *cb, *next;
479
480 wl_list_for_each_safe(cb, next,
481 &state->frame_callback_list, link)
482 wl_resource_destroy(cb->resource);
483
484 pixman_region32_fini(&state->input);
485 pixman_region32_fini(&state->opaque);
486 pixman_region32_fini(&state->damage);
487
488 if (state->buffer)
489 wl_list_remove(&state->buffer_destroy_listener.link);
490 state->buffer = NULL;
491}
492
493static void
494weston_surface_state_set_buffer(struct weston_surface_state *state,
495 struct weston_buffer *buffer)
496{
497 if (state->buffer == buffer)
498 return;
499
500 if (state->buffer)
501 wl_list_remove(&state->buffer_destroy_listener.link);
502 state->buffer = buffer;
503 if (state->buffer)
504 wl_signal_add(&state->buffer->destroy_signal,
505 &state->buffer_destroy_listener);
506}
507
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500508WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500509weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500510{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500511 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400512
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200513 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400514 if (surface == NULL)
515 return NULL;
516
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500517 wl_signal_init(&surface->destroy_signal);
518
519 surface->resource = NULL;
Kristian Høgsberg48891542012-02-23 17:38:33 -0500520
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500521 surface->compositor = compositor;
Giulio Camuffo13b85bd2013-08-13 23:10:14 +0200522 surface->ref_count = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400523
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200524 surface->buffer_viewport.buffer.transform = WL_OUTPUT_TRANSFORM_NORMAL;
525 surface->buffer_viewport.buffer.scale = 1;
Pekka Paalanenf0cad482014-03-14 14:38:16 +0200526 surface->buffer_viewport.buffer.src_width = wl_fixed_from_int(-1);
527 surface->buffer_viewport.surface.width = -1;
Jason Ekstrand7b982072014-05-20 14:33:03 -0500528
529 weston_surface_state_init(&surface->pending);
530
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400531 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200532
Pekka Paalanenb0420ae2014-01-08 15:39:17 +0200533 surface->viewport_resource = NULL;
Jonny Lamb74130762013-11-26 18:19:46 +0100534
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400535 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500536 pixman_region32_init(&surface->opaque);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300537 region_init_infinite(&surface->input);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400538
Jason Ekstranda7af7042013-10-12 22:38:11 -0500539 wl_list_init(&surface->views);
540
541 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500542
Pekka Paalanene67858b2013-04-25 13:57:42 +0300543 wl_list_init(&surface->subsurface_list);
544 wl_list_init(&surface->subsurface_list_pending);
545
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400546 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500547}
548
Alex Wu8811bf92012-02-28 18:07:54 +0800549WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500550weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200551 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500552{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100553 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500554}
555
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400556WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500557weston_view_to_global_float(struct weston_view *view,
558 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200559{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500560 if (view->transform.enabled) {
Pekka Paalanenece8a012012-02-08 15:23:15 +0200561 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
562
Jason Ekstranda7af7042013-10-12 22:38:11 -0500563 weston_matrix_transform(&view->transform.matrix, &v);
Pekka Paalanenece8a012012-02-08 15:23:15 +0200564
565 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200566 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700567 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200568 v.f[3]);
569 *x = 0;
570 *y = 0;
571 return;
572 }
573
574 *x = v.f[0] / v.f[3];
575 *y = v.f[1] / v.f[3];
576 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500577 *x = sx + view->geometry.x;
578 *y = sy + view->geometry.y;
Pekka Paalanenece8a012012-02-08 15:23:15 +0200579 }
580}
581
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500582WL_EXPORT void
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200583weston_transformed_coord(int width, int height,
584 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200585 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200586 float sx, float sy, float *bx, float *by)
587{
588 switch (transform) {
589 case WL_OUTPUT_TRANSFORM_NORMAL:
590 default:
591 *bx = sx;
592 *by = sy;
593 break;
594 case WL_OUTPUT_TRANSFORM_FLIPPED:
595 *bx = width - sx;
596 *by = sy;
597 break;
598 case WL_OUTPUT_TRANSFORM_90:
599 *bx = height - sy;
600 *by = sx;
601 break;
602 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
603 *bx = height - sy;
604 *by = width - sx;
605 break;
606 case WL_OUTPUT_TRANSFORM_180:
607 *bx = width - sx;
608 *by = height - sy;
609 break;
610 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
611 *bx = sx;
612 *by = height - sy;
613 break;
614 case WL_OUTPUT_TRANSFORM_270:
615 *bx = sy;
616 *by = width - sx;
617 break;
618 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
619 *bx = sy;
620 *by = sx;
621 break;
622 }
Alexander Larsson4ea95522013-05-22 14:41:37 +0200623
624 *bx *= scale;
625 *by *= scale;
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200626}
627
628WL_EXPORT pixman_box32_t
629weston_transformed_rect(int width, int height,
630 enum wl_output_transform transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +0200631 int32_t scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200632 pixman_box32_t rect)
633{
634 float x1, x2, y1, y2;
635
636 pixman_box32_t ret;
637
Alexander Larsson4ea95522013-05-22 14:41:37 +0200638 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200639 rect.x1, rect.y1, &x1, &y1);
Alexander Larsson4ea95522013-05-22 14:41:37 +0200640 weston_transformed_coord(width, height, transform, scale,
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +0200641 rect.x2, rect.y2, &x2, &y2);
642
643 if (x1 <= x2) {
644 ret.x1 = x1;
645 ret.x2 = x2;
646 } else {
647 ret.x1 = x2;
648 ret.x2 = x1;
649 }
650
651 if (y1 <= y2) {
652 ret.y1 = y1;
653 ret.y2 = y2;
654 } else {
655 ret.y1 = y2;
656 ret.y2 = y1;
657 }
658
659 return ret;
660}
661
662WL_EXPORT void
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500663weston_transformed_region(int width, int height,
664 enum wl_output_transform transform,
665 int32_t scale,
666 pixman_region32_t *src, pixman_region32_t *dest)
667{
668 pixman_box32_t *src_rects, *dest_rects;
669 int nrects, i;
670
671 if (transform == WL_OUTPUT_TRANSFORM_NORMAL && scale == 1) {
672 if (src != dest)
673 pixman_region32_copy(dest, src);
674 return;
675 }
676
677 src_rects = pixman_region32_rectangles(src, &nrects);
678 dest_rects = malloc(nrects * sizeof(*dest_rects));
679 if (!dest_rects)
680 return;
681
682 if (transform == WL_OUTPUT_TRANSFORM_NORMAL) {
683 memcpy(dest_rects, src_rects, nrects * sizeof(*dest_rects));
684 } else {
685 for (i = 0; i < nrects; i++) {
686 switch (transform) {
687 default:
688 case WL_OUTPUT_TRANSFORM_NORMAL:
689 dest_rects[i].x1 = src_rects[i].x1;
690 dest_rects[i].y1 = src_rects[i].y1;
691 dest_rects[i].x2 = src_rects[i].x2;
692 dest_rects[i].y2 = src_rects[i].y2;
693 break;
694 case WL_OUTPUT_TRANSFORM_90:
695 dest_rects[i].x1 = height - src_rects[i].y2;
696 dest_rects[i].y1 = src_rects[i].x1;
697 dest_rects[i].x2 = height - src_rects[i].y1;
698 dest_rects[i].y2 = src_rects[i].x2;
699 break;
700 case WL_OUTPUT_TRANSFORM_180:
701 dest_rects[i].x1 = width - src_rects[i].x2;
702 dest_rects[i].y1 = height - src_rects[i].y2;
703 dest_rects[i].x2 = width - src_rects[i].x1;
704 dest_rects[i].y2 = height - src_rects[i].y1;
705 break;
706 case WL_OUTPUT_TRANSFORM_270:
707 dest_rects[i].x1 = src_rects[i].y1;
708 dest_rects[i].y1 = width - src_rects[i].x2;
709 dest_rects[i].x2 = src_rects[i].y2;
710 dest_rects[i].y2 = width - src_rects[i].x1;
711 break;
712 case WL_OUTPUT_TRANSFORM_FLIPPED:
713 dest_rects[i].x1 = width - src_rects[i].x2;
714 dest_rects[i].y1 = src_rects[i].y1;
715 dest_rects[i].x2 = width - src_rects[i].x1;
716 dest_rects[i].y2 = src_rects[i].y2;
717 break;
718 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
719 dest_rects[i].x1 = height - src_rects[i].y2;
720 dest_rects[i].y1 = width - src_rects[i].x2;
721 dest_rects[i].x2 = height - src_rects[i].y1;
722 dest_rects[i].y2 = width - src_rects[i].x1;
723 break;
724 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
725 dest_rects[i].x1 = src_rects[i].x1;
726 dest_rects[i].y1 = height - src_rects[i].y2;
727 dest_rects[i].x2 = src_rects[i].x2;
728 dest_rects[i].y2 = height - src_rects[i].y1;
729 break;
730 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
731 dest_rects[i].x1 = src_rects[i].y1;
732 dest_rects[i].y1 = src_rects[i].x1;
733 dest_rects[i].x2 = src_rects[i].y2;
734 dest_rects[i].y2 = src_rects[i].x2;
735 break;
736 }
737 }
738 }
739
740 if (scale != 1) {
741 for (i = 0; i < nrects; i++) {
742 dest_rects[i].x1 *= scale;
743 dest_rects[i].x2 *= scale;
744 dest_rects[i].y1 *= scale;
745 dest_rects[i].y2 *= scale;
746 }
747 }
748
749 pixman_region32_clear(dest);
750 pixman_region32_init_rects(dest, dest_rects, nrects);
751 free(dest_rects);
752}
753
Jonny Lamb74130762013-11-26 18:19:46 +0100754static void
755scaler_surface_to_buffer(struct weston_surface *surface,
756 float sx, float sy, float *bx, float *by)
757{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200758 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200759 double src_width, src_height;
760 double src_x, src_y;
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200761
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200762 if (vp->buffer.src_width == wl_fixed_from_int(-1)) {
763 if (vp->surface.width == -1) {
764 *bx = sx;
765 *by = sy;
766 return;
767 }
Jonny Lamb74130762013-11-26 18:19:46 +0100768
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200769 src_x = 0.0;
770 src_y = 0.0;
771 src_width = surface->width_from_buffer;
772 src_height = surface->height_from_buffer;
Jonny Lamb74130762013-11-26 18:19:46 +0100773 } else {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200774 src_x = wl_fixed_to_double(vp->buffer.src_x);
775 src_y = wl_fixed_to_double(vp->buffer.src_y);
776 src_width = wl_fixed_to_double(vp->buffer.src_width);
777 src_height = wl_fixed_to_double(vp->buffer.src_height);
Jonny Lamb74130762013-11-26 18:19:46 +0100778 }
Pekka Paalanen0b4c5352014-03-14 14:38:17 +0200779
780 *bx = sx * src_width / surface->width + src_x;
781 *by = sy * src_height / surface->height + src_y;
Jonny Lamb74130762013-11-26 18:19:46 +0100782}
783
Jason Ekstrand33ff6362013-10-27 22:25:01 -0500784WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200785weston_surface_to_buffer_float(struct weston_surface *surface,
786 float sx, float sy, float *bx, float *by)
787{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200788 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
789
Jonny Lamb74130762013-11-26 18:19:46 +0100790 /* first transform coordinates if the scaler is set */
791 scaler_surface_to_buffer(surface, sx, sy, bx, by);
792
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500793 weston_transformed_coord(surface->width_from_buffer,
794 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200795 vp->buffer.transform, vp->buffer.scale,
Jonny Lamb74130762013-11-26 18:19:46 +0100796 *bx, *by, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200797}
798
Alexander Larsson4ea95522013-05-22 14:41:37 +0200799WL_EXPORT void
800weston_surface_to_buffer(struct weston_surface *surface,
801 int sx, int sy, int *bx, int *by)
802{
803 float bxf, byf;
804
Jonny Lamb74130762013-11-26 18:19:46 +0100805 weston_surface_to_buffer_float(surface,
806 sx, sy, &bxf, &byf);
807
Alexander Larsson4ea95522013-05-22 14:41:37 +0200808 *bx = floorf(bxf);
809 *by = floorf(byf);
810}
811
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200812WL_EXPORT pixman_box32_t
813weston_surface_to_buffer_rect(struct weston_surface *surface,
814 pixman_box32_t rect)
815{
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200816 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Jonny Lamb74130762013-11-26 18:19:46 +0100817 float xf, yf;
818
819 /* first transform box coordinates if the scaler is set */
820 scaler_surface_to_buffer(surface, rect.x1, rect.y1, &xf, &yf);
821 rect.x1 = floorf(xf);
822 rect.y1 = floorf(yf);
823
824 scaler_surface_to_buffer(surface, rect.x2, rect.y2, &xf, &yf);
825 rect.x2 = floorf(xf);
826 rect.y2 = floorf(yf);
827
Jason Ekstrandd0cebc32014-04-21 20:56:46 -0500828 return weston_transformed_rect(surface->width_from_buffer,
829 surface->height_from_buffer,
Pekka Paalanen952b6c82014-03-14 14:38:15 +0200830 vp->buffer.transform, vp->buffer.scale,
Alexander Larsson4ea95522013-05-22 14:41:37 +0200831 rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200832}
833
834WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500835weston_view_move_to_plane(struct weston_view *view,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400836 struct weston_plane *plane)
837{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500838 if (view->plane == plane)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400839 return;
840
Jason Ekstranda7af7042013-10-12 22:38:11 -0500841 weston_view_damage_below(view);
842 view->plane = plane;
843 weston_surface_damage(view->surface);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400844}
845
846WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500847weston_view_damage_below(struct weston_view *view)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200848{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500849 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200850
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500851 pixman_region32_init(&damage);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +0300852 pixman_region32_subtract(&damage, &view->transform.masked_boundingbox,
Jason Ekstranda7af7042013-10-12 22:38:11 -0500853 &view->clip);
Xiong Zhang97116532013-10-23 13:58:31 +0800854 if (view->plane)
855 pixman_region32_union(&view->plane->damage,
856 &view->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500857 pixman_region32_fini(&damage);
Kristian Høgsberga3a784a2013-11-13 21:33:43 -0800858 weston_view_schedule_repaint(view);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200859}
860
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400861static void
862weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
863{
864 uint32_t different = es->output_mask ^ mask;
865 uint32_t entered = mask & different;
866 uint32_t left = es->output_mask & different;
867 struct weston_output *output;
868 struct wl_resource *resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500869 struct wl_client *client;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400870
871 es->output_mask = mask;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500872 if (es->resource == NULL)
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400873 return;
874 if (different == 0)
875 return;
876
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500877 client = wl_resource_get_client(es->resource);
878
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400879 wl_list_for_each(output, &es->compositor->output_list, link) {
880 if (1 << output->id & different)
881 resource =
Jason Ekstranda0d2dde2013-06-14 10:08:01 -0500882 wl_resource_find_for_client(&output->resource_list,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400883 client);
884 if (resource == NULL)
885 continue;
886 if (1 << output->id & entered)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500887 wl_surface_send_enter(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400888 if (1 << output->id & left)
Jason Ekstrand26ed73c2013-06-06 22:34:41 -0500889 wl_surface_send_leave(es->resource, resource);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400890 }
891}
892
Zhang, Xiong Yf3012412013-12-13 22:10:53 +0200893
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400894static void
895weston_surface_assign_output(struct weston_surface *es)
896{
Jason Ekstranda7af7042013-10-12 22:38:11 -0500897 struct weston_output *new_output;
898 struct weston_view *view;
899 pixman_region32_t region;
900 uint32_t max, area, mask;
901 pixman_box32_t *e;
902
903 new_output = NULL;
904 max = 0;
905 mask = 0;
906 pixman_region32_init(&region);
907 wl_list_for_each(view, &es->views, surface_link) {
908 if (!view->output)
909 continue;
910
911 pixman_region32_intersect(&region, &view->transform.boundingbox,
912 &view->output->region);
913
914 e = pixman_region32_extents(&region);
915 area = (e->x2 - e->x1) * (e->y2 - e->y1);
916
917 mask |= view->output_mask;
918
919 if (area >= max) {
920 new_output = view->output;
921 max = area;
922 }
923 }
924 pixman_region32_fini(&region);
925
926 es->output = new_output;
927 weston_surface_update_output_mask(es, mask);
928}
929
930static void
931weston_view_assign_output(struct weston_view *ev)
932{
933 struct weston_compositor *ec = ev->surface->compositor;
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400934 struct weston_output *output, *new_output;
935 pixman_region32_t region;
936 uint32_t max, area, mask;
937 pixman_box32_t *e;
938
939 new_output = NULL;
940 max = 0;
941 mask = 0;
942 pixman_region32_init(&region);
943 wl_list_for_each(output, &ec->output_list, link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -0500944 pixman_region32_intersect(&region, &ev->transform.boundingbox,
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400945 &output->region);
946
947 e = pixman_region32_extents(&region);
948 area = (e->x2 - e->x1) * (e->y2 - e->y1);
949
950 if (area > 0)
951 mask |= 1 << output->id;
952
953 if (area >= max) {
954 new_output = output;
955 max = area;
956 }
957 }
958 pixman_region32_fini(&region);
959
Jason Ekstranda7af7042013-10-12 22:38:11 -0500960 ev->output = new_output;
961 ev->output_mask = mask;
962
963 weston_surface_assign_output(ev->surface);
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400964}
965
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200966static void
Jason Ekstranda7af7042013-10-12 22:38:11 -0500967view_compute_bbox(struct weston_view *view, int32_t sx, int32_t sy,
968 int32_t width, int32_t height,
969 pixman_region32_t *bbox)
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200970{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200971 float min_x = HUGE_VALF, min_y = HUGE_VALF;
972 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200973 int32_t s[4][2] = {
974 { sx, sy },
975 { sx, sy + height },
976 { sx + width, sy },
977 { sx + width, sy + height }
978 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200979 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200980 int i;
981
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300982 if (width == 0 || height == 0) {
983 /* avoid rounding empty bbox to 1x1 */
984 pixman_region32_init(bbox);
985 return;
986 }
987
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200988 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200989 float x, y;
Jason Ekstranda7af7042013-10-12 22:38:11 -0500990 weston_view_to_global_float(view, s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200991 if (x < min_x)
992 min_x = x;
993 if (x > max_x)
994 max_x = x;
995 if (y < min_y)
996 min_y = y;
997 if (y > max_y)
998 max_y = y;
999 }
1000
Pekka Paalanen219b9822012-02-08 15:38:37 +02001001 int_x = floorf(min_x);
1002 int_y = floorf(min_y);
1003 pixman_region32_init_rect(bbox, int_x, int_y,
1004 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +02001005}
1006
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001007static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001008weston_view_update_transform_disable(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001009{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001010 view->transform.enabled = 0;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001011
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001012 /* round off fractions when not transformed */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001013 view->geometry.x = roundf(view->geometry.x);
1014 view->geometry.y = roundf(view->geometry.y);
Pekka Paalanencc2f8682012-02-13 10:34:04 +02001015
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001016 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001017 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1018 view->transform.position.matrix.d[12] = view->geometry.x;
1019 view->transform.position.matrix.d[13] = view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001020
Jason Ekstranda7af7042013-10-12 22:38:11 -05001021 view->transform.matrix = view->transform.position.matrix;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001022
Jason Ekstranda7af7042013-10-12 22:38:11 -05001023 view->transform.inverse = view->transform.position.matrix;
1024 view->transform.inverse.d[12] = -view->geometry.x;
1025 view->transform.inverse.d[13] = -view->geometry.y;
Kristian Høgsbergc1e6c8a2013-02-19 17:04:50 -05001026
Jason Ekstranda7af7042013-10-12 22:38:11 -05001027 pixman_region32_init_rect(&view->transform.boundingbox,
1028 view->geometry.x,
1029 view->geometry.y,
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001030 view->surface->width,
1031 view->surface->height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001032
Jason Ekstranda7af7042013-10-12 22:38:11 -05001033 if (view->alpha == 1.0) {
1034 pixman_region32_copy(&view->transform.opaque,
1035 &view->surface->opaque);
1036 pixman_region32_translate(&view->transform.opaque,
1037 view->geometry.x,
1038 view->geometry.y);
Kristian Høgsberg3b4af202012-02-28 09:19:39 -05001039 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001040}
1041
1042static int
Jason Ekstranda7af7042013-10-12 22:38:11 -05001043weston_view_update_transform_enable(struct weston_view *view)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001044{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001045 struct weston_view *parent = view->geometry.parent;
1046 struct weston_matrix *matrix = &view->transform.matrix;
1047 struct weston_matrix *inverse = &view->transform.inverse;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001048 struct weston_transform *tform;
1049
Jason Ekstranda7af7042013-10-12 22:38:11 -05001050 view->transform.enabled = 1;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001051
1052 /* Otherwise identity matrix, but with x and y translation. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001053 view->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
1054 view->transform.position.matrix.d[12] = view->geometry.x;
1055 view->transform.position.matrix.d[13] = view->geometry.y;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001056
1057 weston_matrix_init(matrix);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001058 wl_list_for_each(tform, &view->geometry.transformation_list, link)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001059 weston_matrix_multiply(matrix, &tform->matrix);
1060
Pekka Paalanen483243f2013-03-08 14:56:50 +02001061 if (parent)
1062 weston_matrix_multiply(matrix, &parent->transform.matrix);
1063
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001064 if (weston_matrix_invert(inverse, matrix) < 0) {
1065 /* Oops, bad total transformation, not invertible */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001066 weston_log("error: weston_view %p"
1067 " transformation not invertible.\n", view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001068 return -1;
1069 }
1070
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001071 view_compute_bbox(view, 0, 0,
1072 view->surface->width, view->surface->height,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001073 &view->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001074
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001075 return 0;
1076}
1077
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001078static struct weston_layer *
1079get_view_layer(struct weston_view *view)
1080{
1081 if (view->parent_view)
1082 return get_view_layer(view->parent_view);
1083 return view->layer_link.layer;
1084}
1085
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001086WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001087weston_view_update_transform(struct weston_view *view)
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001088{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001089 struct weston_view *parent = view->geometry.parent;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001090 struct weston_layer *layer;
1091 pixman_region32_t mask;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001092
Jason Ekstranda7af7042013-10-12 22:38:11 -05001093 if (!view->transform.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001094 return;
1095
Pekka Paalanen483243f2013-03-08 14:56:50 +02001096 if (parent)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001097 weston_view_update_transform(parent);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001098
Jason Ekstranda7af7042013-10-12 22:38:11 -05001099 view->transform.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001100
Jason Ekstranda7af7042013-10-12 22:38:11 -05001101 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001102
Jason Ekstranda7af7042013-10-12 22:38:11 -05001103 pixman_region32_fini(&view->transform.boundingbox);
1104 pixman_region32_fini(&view->transform.opaque);
1105 pixman_region32_init(&view->transform.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001106
Pekka Paalanencd403622012-01-25 13:37:39 +02001107 /* transform.position is always in transformation_list */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001108 if (view->geometry.transformation_list.next ==
1109 &view->transform.position.link &&
1110 view->geometry.transformation_list.prev ==
1111 &view->transform.position.link &&
Pekka Paalanen483243f2013-03-08 14:56:50 +02001112 !parent) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001113 weston_view_update_transform_disable(view);
Pekka Paalanen80fb08d2012-02-08 15:14:17 +02001114 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001115 if (weston_view_update_transform_enable(view) < 0)
1116 weston_view_update_transform_disable(view);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001117 }
Pekka Paalanen96516782012-02-09 15:32:15 +02001118
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001119 layer = get_view_layer(view);
1120 if (layer) {
1121 pixman_region32_init_with_extents(&mask, &layer->mask);
1122 pixman_region32_intersect(&view->transform.masked_boundingbox,
1123 &view->transform.boundingbox, &mask);
1124 pixman_region32_intersect(&view->transform.masked_opaque,
1125 &view->transform.opaque, &mask);
1126 pixman_region32_fini(&mask);
1127 }
1128
Jason Ekstranda7af7042013-10-12 22:38:11 -05001129 weston_view_damage_below(view);
Pekka Paalanen96516782012-02-09 15:32:15 +02001130
Jason Ekstranda7af7042013-10-12 22:38:11 -05001131 weston_view_assign_output(view);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03001132
Jason Ekstranda7af7042013-10-12 22:38:11 -05001133 wl_signal_emit(&view->surface->compositor->transform_signal,
1134 view->surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +02001135}
1136
Pekka Paalanenddae03c2012-02-06 14:54:20 +02001137WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001138weston_view_geometry_dirty(struct weston_view *view)
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001139{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001140 struct weston_view *child;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001141
1142 /*
Jason Ekstranda7af7042013-10-12 22:38:11 -05001143 * The invariant: if view->geometry.dirty, then all views
1144 * in view->geometry.child_list have geometry.dirty too.
Pekka Paalanen483243f2013-03-08 14:56:50 +02001145 * Corollary: if not parent->geometry.dirty, then all ancestors
1146 * are not dirty.
1147 */
1148
Jason Ekstranda7af7042013-10-12 22:38:11 -05001149 if (view->transform.dirty)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001150 return;
1151
Jason Ekstranda7af7042013-10-12 22:38:11 -05001152 view->transform.dirty = 1;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001153
Jason Ekstranda7af7042013-10-12 22:38:11 -05001154 wl_list_for_each(child, &view->geometry.child_list,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001155 geometry.parent_link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001156 weston_view_geometry_dirty(child);
Pekka Paalanenc3ce7382013-03-08 14:56:49 +02001157}
1158
1159WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001160weston_view_to_global_fixed(struct weston_view *view,
1161 wl_fixed_t vx, wl_fixed_t vy,
1162 wl_fixed_t *x, wl_fixed_t *y)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001163{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +02001164 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001165
Jason Ekstranda7af7042013-10-12 22:38:11 -05001166 weston_view_to_global_float(view,
1167 wl_fixed_to_double(vx),
1168 wl_fixed_to_double(vy),
1169 &xf, &yf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001170 *x = wl_fixed_from_double(xf);
1171 *y = wl_fixed_from_double(yf);
1172}
1173
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -04001174WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001175weston_view_from_global_float(struct weston_view *view,
1176 float x, float y, float *vx, float *vy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001177{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001178 if (view->transform.enabled) {
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001179 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
1180
Jason Ekstranda7af7042013-10-12 22:38:11 -05001181 weston_matrix_transform(&view->transform.inverse, &v);
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001182
1183 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +02001184 weston_log("warning: numerical instability in "
Jason Ekstranda7af7042013-10-12 22:38:11 -05001185 "weston_view_from_global(), divisor = %g\n",
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001186 v.f[3]);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001187 *vx = 0;
1188 *vy = 0;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001189 return;
1190 }
1191
Jason Ekstranda7af7042013-10-12 22:38:11 -05001192 *vx = v.f[0] / v.f[3];
1193 *vy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001194 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001195 *vx = x - view->geometry.x;
1196 *vy = y - view->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +02001197 }
1198}
1199
1200WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001201weston_view_from_global_fixed(struct weston_view *view,
1202 wl_fixed_t x, wl_fixed_t y,
1203 wl_fixed_t *vx, wl_fixed_t *vy)
Daniel Stonebd3489b2012-05-08 17:17:53 +01001204{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001205 float vxf, vyf;
Daniel Stonebd3489b2012-05-08 17:17:53 +01001206
Jason Ekstranda7af7042013-10-12 22:38:11 -05001207 weston_view_from_global_float(view,
1208 wl_fixed_to_double(x),
1209 wl_fixed_to_double(y),
1210 &vxf, &vyf);
1211 *vx = wl_fixed_from_double(vxf);
1212 *vy = wl_fixed_from_double(vyf);
Daniel Stonebd3489b2012-05-08 17:17:53 +01001213}
1214
1215WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001216weston_view_from_global(struct weston_view *view,
1217 int32_t x, int32_t y, int32_t *vx, int32_t *vy)
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001218{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001219 float vxf, vyf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001220
Jason Ekstranda7af7042013-10-12 22:38:11 -05001221 weston_view_from_global_float(view, x, y, &vxf, &vyf);
1222 *vx = floorf(vxf);
1223 *vy = floorf(vyf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001224}
1225
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001226WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -04001227weston_surface_schedule_repaint(struct weston_surface *surface)
1228{
1229 struct weston_output *output;
1230
1231 wl_list_for_each(output, &surface->compositor->output_list, link)
1232 if (surface->output_mask & (1 << output->id))
1233 weston_output_schedule_repaint(output);
1234}
1235
1236WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001237weston_view_schedule_repaint(struct weston_view *view)
1238{
1239 struct weston_output *output;
1240
1241 wl_list_for_each(output, &view->surface->compositor->output_list, link)
1242 if (view->output_mask & (1 << output->id))
1243 weston_output_schedule_repaint(output);
1244}
1245
1246WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001247weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001248{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001249 pixman_region32_union_rect(&surface->damage, &surface->damage,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001250 0, 0, surface->width,
1251 surface->height);
Pekka Paalanen2267d452012-01-26 13:12:45 +02001252
Kristian Høgsberg98238702012-08-03 16:29:12 -04001253 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001254}
1255
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001256WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001257weston_view_set_position(struct weston_view *view, float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001258{
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001259 if (view->geometry.x == x && view->geometry.y == y)
1260 return;
1261
Jason Ekstranda7af7042013-10-12 22:38:11 -05001262 view->geometry.x = x;
1263 view->geometry.y = y;
1264 weston_view_geometry_dirty(view);
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +02001265}
1266
Pekka Paalanen483243f2013-03-08 14:56:50 +02001267static void
1268transform_parent_handle_parent_destroy(struct wl_listener *listener,
1269 void *data)
1270{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001271 struct weston_view *view =
1272 container_of(listener, struct weston_view,
Pekka Paalanen483243f2013-03-08 14:56:50 +02001273 geometry.parent_destroy_listener);
1274
Jason Ekstranda7af7042013-10-12 22:38:11 -05001275 weston_view_set_transform_parent(view, NULL);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001276}
1277
1278WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001279weston_view_set_transform_parent(struct weston_view *view,
1280 struct weston_view *parent)
Pekka Paalanen483243f2013-03-08 14:56:50 +02001281{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001282 if (view->geometry.parent) {
1283 wl_list_remove(&view->geometry.parent_destroy_listener.link);
1284 wl_list_remove(&view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001285 }
1286
Jason Ekstranda7af7042013-10-12 22:38:11 -05001287 view->geometry.parent = parent;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001288
Jason Ekstranda7af7042013-10-12 22:38:11 -05001289 view->geometry.parent_destroy_listener.notify =
Pekka Paalanen483243f2013-03-08 14:56:50 +02001290 transform_parent_handle_parent_destroy;
1291 if (parent) {
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001292 wl_signal_add(&parent->destroy_signal,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001293 &view->geometry.parent_destroy_listener);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001294 wl_list_insert(&parent->geometry.child_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001295 &view->geometry.parent_link);
Pekka Paalanen483243f2013-03-08 14:56:50 +02001296 }
1297
Jason Ekstranda7af7042013-10-12 22:38:11 -05001298 weston_view_geometry_dirty(view);
1299}
1300
1301WL_EXPORT int
1302weston_view_is_mapped(struct weston_view *view)
1303{
1304 if (view->output)
1305 return 1;
1306 else
1307 return 0;
Pekka Paalanen483243f2013-03-08 14:56:50 +02001308}
1309
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +03001310WL_EXPORT int
1311weston_surface_is_mapped(struct weston_surface *surface)
1312{
1313 if (surface->output)
1314 return 1;
1315 else
1316 return 0;
1317}
1318
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001319static void
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001320surface_set_size(struct weston_surface *surface, int32_t width, int32_t height)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001321{
1322 struct weston_view *view;
1323
1324 if (surface->width == width && surface->height == height)
1325 return;
1326
1327 surface->width = width;
1328 surface->height = height;
1329
1330 wl_list_for_each(view, &surface->views, surface_link)
1331 weston_view_geometry_dirty(view);
1332}
1333
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001334WL_EXPORT void
1335weston_surface_set_size(struct weston_surface *surface,
1336 int32_t width, int32_t height)
1337{
1338 assert(!surface->resource);
1339 surface_set_size(surface, width, height);
1340}
1341
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001342static int
1343fixed_round_up_to_int(wl_fixed_t f)
1344{
1345 return wl_fixed_to_int(wl_fixed_from_int(1) - 1 + f);
1346}
1347
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001348static void
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001349weston_surface_calculate_size_from_buffer(struct weston_surface *surface)
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001350{
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001351 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001352 int32_t width, height;
1353
1354 if (!surface->buffer_ref.buffer) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001355 surface->width_from_buffer = 0;
1356 surface->height_from_buffer = 0;
Jonny Lamb74130762013-11-26 18:19:46 +01001357 return;
1358 }
1359
Pekka Paalanen952b6c82014-03-14 14:38:15 +02001360 switch (vp->buffer.transform) {
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001361 case WL_OUTPUT_TRANSFORM_90:
1362 case WL_OUTPUT_TRANSFORM_270:
1363 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1364 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001365 width = surface->buffer_ref.buffer->height / vp->buffer.scale;
1366 height = surface->buffer_ref.buffer->width / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001367 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001368 default:
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001369 width = surface->buffer_ref.buffer->width / vp->buffer.scale;
1370 height = surface->buffer_ref.buffer->height / vp->buffer.scale;
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001371 break;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001372 }
Pekka Paalanenda75ee12013-11-26 18:19:43 +01001373
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001374 surface->width_from_buffer = width;
1375 surface->height_from_buffer = height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001376}
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001377
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001378static void
1379weston_surface_update_size(struct weston_surface *surface)
1380{
1381 struct weston_buffer_viewport *vp = &surface->buffer_viewport;
1382 int32_t width, height;
1383
1384 width = surface->width_from_buffer;
1385 height = surface->height_from_buffer;
1386
1387 if (width != 0 && vp->surface.width != -1) {
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001388 surface_set_size(surface,
1389 vp->surface.width, vp->surface.height);
1390 return;
1391 }
1392
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001393 if (width != 0 && vp->buffer.src_width != wl_fixed_from_int(-1)) {
Pekka Paalanene9317212014-04-04 14:22:13 +03001394 int32_t w = fixed_round_up_to_int(vp->buffer.src_width);
1395 int32_t h = fixed_round_up_to_int(vp->buffer.src_height);
1396
1397 surface_set_size(surface, w ?: 1, h ?: 1);
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02001398 return;
1399 }
1400
Jason Ekstrand5c11a332013-12-04 20:32:03 -06001401 surface_set_size(surface, width, height);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001402}
1403
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001404WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001405weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001406{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001407 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001408
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001409 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001410
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001411 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -05001412}
1413
Jason Ekstranda7af7042013-10-12 22:38:11 -05001414WL_EXPORT struct weston_view *
1415weston_compositor_pick_view(struct weston_compositor *compositor,
1416 wl_fixed_t x, wl_fixed_t y,
1417 wl_fixed_t *vx, wl_fixed_t *vy)
Tiago Vignatti9d393522012-02-10 16:26:19 +02001418{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001419 struct weston_view *view;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001420 int ix = wl_fixed_to_int(x);
1421 int iy = wl_fixed_to_int(y);
Tiago Vignatti9d393522012-02-10 16:26:19 +02001422
Jason Ekstranda7af7042013-10-12 22:38:11 -05001423 wl_list_for_each(view, &compositor->view_list, link) {
1424 weston_view_from_global_fixed(view, x, y, vx, vy);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001425 if (pixman_region32_contains_point(
1426 &view->transform.masked_boundingbox,
1427 ix, iy, NULL) &&
1428 pixman_region32_contains_point(&view->surface->input,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001429 wl_fixed_to_int(*vx),
1430 wl_fixed_to_int(*vy),
Daniel Stone103db7f2012-05-08 17:17:55 +01001431 NULL))
Jason Ekstranda7af7042013-10-12 22:38:11 -05001432 return view;
Tiago Vignatti9d393522012-02-10 16:26:19 +02001433 }
1434
1435 return NULL;
1436}
1437
1438static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001439weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001440{
Daniel Stone37816df2012-05-16 18:45:18 +01001441 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001442
Kristian Høgsberg10ddd972013-10-22 12:40:54 -07001443 if (!compositor->session_active)
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -05001444 return;
1445
Daniel Stone37816df2012-05-16 18:45:18 +01001446 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsberga71e8b22013-05-06 21:51:21 -04001447 weston_seat_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001448}
1449
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -04001450WL_EXPORT void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001451weston_view_unmap(struct weston_view *view)
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001452{
Daniel Stone4dab5db2012-05-30 16:31:53 +01001453 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001454
Jason Ekstranda7af7042013-10-12 22:38:11 -05001455 if (!weston_view_is_mapped(view))
1456 return;
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001457
Jason Ekstranda7af7042013-10-12 22:38:11 -05001458 weston_view_damage_below(view);
1459 view->output = NULL;
Xiong Zhang97116532013-10-23 13:58:31 +08001460 view->plane = NULL;
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001461 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001462 wl_list_remove(&view->link);
1463 wl_list_init(&view->link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001464 view->output_mask = 0;
1465 weston_surface_assign_output(view->surface);
1466
1467 if (weston_surface_is_mapped(view->surface))
1468 return;
1469
1470 wl_list_for_each(seat, &view->surface->compositor->seat_list, link) {
1471 if (seat->keyboard && seat->keyboard->focus == view->surface)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001472 weston_keyboard_set_focus(seat->keyboard, NULL);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001473 if (seat->pointer && seat->pointer->focus == view)
Kristian Høgsberge3148752013-05-06 23:19:49 -04001474 weston_pointer_set_focus(seat->pointer,
Kristian Høgsberg02bbabb2013-05-06 22:15:05 -04001475 NULL,
1476 wl_fixed_from_int(0),
1477 wl_fixed_from_int(0));
Jason Ekstranda7af7042013-10-12 22:38:11 -05001478 if (seat->touch && seat->touch->focus == view)
Michael Fua2bb7912013-07-23 15:51:06 +08001479 weston_touch_set_focus(seat, NULL);
Daniel Stone4dab5db2012-05-30 16:31:53 +01001480 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001481}
Kristian Høgsberg867dec72012-03-01 17:09:37 -05001482
Jason Ekstranda7af7042013-10-12 22:38:11 -05001483WL_EXPORT void
1484weston_surface_unmap(struct weston_surface *surface)
1485{
1486 struct weston_view *view;
1487
1488 wl_list_for_each(view, &surface->views, surface_link)
1489 weston_view_unmap(view);
1490 surface->output = NULL;
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -05001491}
1492
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001493static void
1494weston_surface_reset_pending_buffer(struct weston_surface *surface)
1495{
Jason Ekstrand7b982072014-05-20 14:33:03 -05001496 weston_surface_state_set_buffer(&surface->pending, NULL);
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001497 surface->pending.sx = 0;
1498 surface->pending.sy = 0;
1499 surface->pending.newly_attached = 0;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001500 surface->pending.buffer_viewport.changed = 0;
Pekka Paalanen3c9b8022014-03-14 14:38:13 +02001501}
1502
Jason Ekstranda7af7042013-10-12 22:38:11 -05001503WL_EXPORT void
1504weston_view_destroy(struct weston_view *view)
1505{
1506 wl_signal_emit(&view->destroy_signal, view);
1507
1508 assert(wl_list_empty(&view->geometry.child_list));
1509
1510 if (weston_view_is_mapped(view)) {
1511 weston_view_unmap(view);
1512 weston_compositor_build_view_list(view->surface->compositor);
1513 }
1514
1515 wl_list_remove(&view->link);
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001516 weston_layer_entry_remove(&view->layer_link);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001517
1518 pixman_region32_fini(&view->clip);
1519 pixman_region32_fini(&view->transform.boundingbox);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001520 pixman_region32_fini(&view->transform.masked_boundingbox);
1521 pixman_region32_fini(&view->transform.masked_opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001522
1523 weston_view_set_transform_parent(view, NULL);
1524
Jason Ekstranda7af7042013-10-12 22:38:11 -05001525 wl_list_remove(&view->surface_link);
1526
1527 free(view);
1528}
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001529
1530WL_EXPORT void
1531weston_surface_destroy(struct weston_surface *surface)
Kristian Høgsberg54879822008-11-23 17:07:32 -05001532{
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001533 struct weston_frame_callback *cb, *next;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001534 struct weston_view *ev, *nv;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001535
Giulio Camuffo13b85bd2013-08-13 23:10:14 +02001536 if (--surface->ref_count > 0)
1537 return;
1538
1539 wl_signal_emit(&surface->destroy_signal, &surface->resource);
1540
Pekka Paalanene67858b2013-04-25 13:57:42 +03001541 assert(wl_list_empty(&surface->subsurface_list_pending));
1542 assert(wl_list_empty(&surface->subsurface_list));
Pekka Paalanen483243f2013-03-08 14:56:50 +02001543
Jason Ekstranda7af7042013-10-12 22:38:11 -05001544 wl_list_for_each_safe(ev, nv, &surface->views, surface_link)
1545 weston_view_destroy(ev);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04001546
Jason Ekstrand7b982072014-05-20 14:33:03 -05001547 weston_surface_state_fini(&surface->pending);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001548
Pekka Paalanende685b82012-12-04 15:58:12 +02001549 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -04001550
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001551 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001552 pixman_region32_fini(&surface->opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001553 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +02001554
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001555 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001556 wl_resource_destroy(cb->resource);
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -04001557
Kristian Høgsberg4fa48732009-03-10 23:17:00 -04001558 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -05001559}
1560
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001561static void
1562destroy_surface(struct wl_resource *resource)
Alex Wu8811bf92012-02-28 18:07:54 +08001563{
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001564 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alex Wu8811bf92012-02-28 18:07:54 +08001565
Giulio Camuffo0d379742013-11-15 22:06:15 +01001566 /* Set the resource to NULL, since we don't want to leave a
1567 * dangling pointer if the surface was refcounted and survives
1568 * the weston_surface_destroy() call. */
1569 surface->resource = NULL;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05001570 weston_surface_destroy(surface);
Alex Wu8811bf92012-02-28 18:07:54 +08001571}
1572
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001573static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001574weston_buffer_destroy_handler(struct wl_listener *listener, void *data)
1575{
1576 struct weston_buffer *buffer =
1577 container_of(listener, struct weston_buffer, destroy_listener);
1578
1579 wl_signal_emit(&buffer->destroy_signal, buffer);
1580 free(buffer);
1581}
1582
Giulio Camuffoe058cd12013-12-12 14:14:29 +01001583WL_EXPORT struct weston_buffer *
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001584weston_buffer_from_resource(struct wl_resource *resource)
1585{
1586 struct weston_buffer *buffer;
1587 struct wl_listener *listener;
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001588
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001589 listener = wl_resource_get_destroy_listener(resource,
1590 weston_buffer_destroy_handler);
1591
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001592 if (listener)
1593 return container_of(listener, struct weston_buffer,
1594 destroy_listener);
1595
1596 buffer = zalloc(sizeof *buffer);
1597 if (buffer == NULL)
1598 return NULL;
1599
1600 buffer->resource = resource;
1601 wl_signal_init(&buffer->destroy_signal);
1602 buffer->destroy_listener.notify = weston_buffer_destroy_handler;
Stanislav Vorobiovbfbb8e52013-08-29 11:36:44 +04001603 buffer->y_inverted = 1;
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07001604 wl_resource_add_destroy_listener(resource, &buffer->destroy_listener);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08001605
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001606 return buffer;
1607}
1608
1609static void
Pekka Paalanende685b82012-12-04 15:58:12 +02001610weston_buffer_reference_handle_destroy(struct wl_listener *listener,
1611 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001612{
Pekka Paalanende685b82012-12-04 15:58:12 +02001613 struct weston_buffer_reference *ref =
1614 container_of(listener, struct weston_buffer_reference,
1615 destroy_listener);
1616
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001617 assert((struct weston_buffer *)data == ref->buffer);
Pekka Paalanende685b82012-12-04 15:58:12 +02001618 ref->buffer = NULL;
1619}
1620
1621WL_EXPORT void
1622weston_buffer_reference(struct weston_buffer_reference *ref,
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001623 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001624{
1625 if (ref->buffer && buffer != ref->buffer) {
Kristian Høgsberg20347802013-03-04 12:07:46 -05001626 ref->buffer->busy_count--;
1627 if (ref->buffer->busy_count == 0) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001628 assert(wl_resource_get_client(ref->buffer->resource));
1629 wl_resource_queue_event(ref->buffer->resource,
Kristian Høgsberg20347802013-03-04 12:07:46 -05001630 WL_BUFFER_RELEASE);
1631 }
Pekka Paalanende685b82012-12-04 15:58:12 +02001632 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001633 }
1634
Pekka Paalanende685b82012-12-04 15:58:12 +02001635 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -04001636 buffer->busy_count++;
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001637 wl_signal_add(&buffer->destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +02001638 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +02001639 }
1640
Pekka Paalanende685b82012-12-04 15:58:12 +02001641 ref->buffer = buffer;
1642 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
1643}
1644
1645static void
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001646weston_surface_attach(struct weston_surface *surface,
1647 struct weston_buffer *buffer)
Pekka Paalanende685b82012-12-04 15:58:12 +02001648{
1649 weston_buffer_reference(&surface->buffer_ref, buffer);
1650
Pekka Paalanena6421c42012-12-04 15:58:10 +02001651 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001652 if (weston_surface_is_mapped(surface))
1653 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001654 }
1655
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001656 surface->compositor->renderer->attach(surface, buffer);
Pekka Paalanenbb2f3f22014-03-14 14:38:11 +02001657
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02001658 weston_surface_calculate_size_from_buffer(surface);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +01001659}
1660
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001661WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001662weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001663{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001664 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001665
1666 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001667 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001668}
1669
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001670WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001671weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001672{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001673 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001674
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001675 pixman_region32_union(&compositor->primary_plane.damage,
1676 &compositor->primary_plane.damage,
1677 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001678 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001679}
1680
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001681static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001682surface_flush_damage(struct weston_surface *surface)
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001683{
Pekka Paalanende685b82012-12-04 15:58:12 +02001684 if (surface->buffer_ref.buffer &&
Jason Ekstrand6bd62942013-06-20 20:38:23 -05001685 wl_shm_buffer_get(surface->buffer_ref.buffer->resource))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001686 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001687
Jason Ekstrandef540082014-06-26 10:37:36 -07001688 pixman_region32_clear(&surface->damage);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001689}
1690
1691static void
1692view_accumulate_damage(struct weston_view *view,
1693 pixman_region32_t *opaque)
1694{
1695 pixman_region32_t damage;
1696
1697 pixman_region32_init(&damage);
1698 if (view->transform.enabled) {
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001699 pixman_box32_t *extents;
1700
Jason Ekstranda7af7042013-10-12 22:38:11 -05001701 extents = pixman_region32_extents(&view->surface->damage);
1702 view_compute_bbox(view, extents->x1, extents->y1,
1703 extents->x2 - extents->x1,
1704 extents->y2 - extents->y1,
1705 &damage);
1706 pixman_region32_translate(&damage,
1707 -view->plane->x,
1708 -view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001709 } else {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001710 pixman_region32_copy(&damage, &view->surface->damage);
1711 pixman_region32_translate(&damage,
1712 view->geometry.x - view->plane->x,
1713 view->geometry.y - view->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001714 }
1715
Jason Ekstranda7af7042013-10-12 22:38:11 -05001716 pixman_region32_subtract(&damage, &damage, opaque);
1717 pixman_region32_union(&view->plane->damage,
1718 &view->plane->damage, &damage);
1719 pixman_region32_fini(&damage);
1720 pixman_region32_copy(&view->clip, opaque);
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001721 pixman_region32_union(opaque, opaque, &view->transform.masked_opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001722}
1723
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001724static void
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001725compositor_accumulate_damage(struct weston_compositor *ec)
1726{
1727 struct weston_plane *plane;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001728 struct weston_view *ev;
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001729 pixman_region32_t opaque, clip;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001730
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001731 pixman_region32_init(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001732
1733 wl_list_for_each(plane, &ec->plane_list, link) {
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001734 pixman_region32_copy(&plane->clip, &clip);
1735
1736 pixman_region32_init(&opaque);
1737
Jason Ekstranda7af7042013-10-12 22:38:11 -05001738 wl_list_for_each(ev, &ec->view_list, link) {
1739 if (ev->plane != plane)
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001740 continue;
1741
Jason Ekstranda7af7042013-10-12 22:38:11 -05001742 view_accumulate_damage(ev, &opaque);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001743 }
1744
1745 pixman_region32_union(&clip, &clip, &opaque);
1746 pixman_region32_fini(&opaque);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001747 }
1748
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001749 pixman_region32_fini(&clip);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001750
Jason Ekstranda7af7042013-10-12 22:38:11 -05001751 wl_list_for_each(ev, &ec->view_list, link)
1752 ev->surface->touched = 0;
1753
1754 wl_list_for_each(ev, &ec->view_list, link) {
1755 if (ev->surface->touched)
1756 continue;
1757 ev->surface->touched = 1;
1758
1759 surface_flush_damage(ev->surface);
1760
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001761 /* Both the renderer and the backend have seen the buffer
1762 * by now. If renderer needs the buffer, it has its own
1763 * reference set. If the backend wants to keep the buffer
1764 * around for migrating the surface into a non-primary plane
1765 * later, keep_buffer is true. Otherwise, drop the core
1766 * reference now, and allow early buffer release. This enables
1767 * clients to use single-buffering.
1768 */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001769 if (!ev->surface->keep_buffer)
1770 weston_buffer_reference(&ev->surface->buffer_ref, NULL);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001771 }
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001772}
1773
1774static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001775surface_stash_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001776{
1777 struct weston_subsurface *sub;
1778
Pekka Paalanene67858b2013-04-25 13:57:42 +03001779 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001780 if (sub->surface == surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001781 continue;
1782
Jason Ekstranda7af7042013-10-12 22:38:11 -05001783 wl_list_insert_list(&sub->unused_views, &sub->surface->views);
1784 wl_list_init(&sub->surface->views);
1785
1786 surface_stash_subsurface_views(sub->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001787 }
1788}
1789
1790static void
Jason Ekstranda7af7042013-10-12 22:38:11 -05001791surface_free_unused_subsurface_views(struct weston_surface *surface)
Pekka Paalanene67858b2013-04-25 13:57:42 +03001792{
Jason Ekstranda7af7042013-10-12 22:38:11 -05001793 struct weston_subsurface *sub;
1794 struct weston_view *view, *nv;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001795
Jason Ekstranda7af7042013-10-12 22:38:11 -05001796 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
1797 if (sub->surface == surface)
1798 continue;
1799
George Kiagiadakised04d382014-06-13 18:10:26 +02001800 wl_list_for_each_safe(view, nv, &sub->unused_views, surface_link) {
1801 weston_view_unmap (view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001802 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02001803 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001804
1805 surface_free_unused_subsurface_views(sub->surface);
1806 }
1807}
1808
1809static void
1810view_list_add_subsurface_view(struct weston_compositor *compositor,
1811 struct weston_subsurface *sub,
1812 struct weston_view *parent)
1813{
1814 struct weston_subsurface *child;
1815 struct weston_view *view = NULL, *iv;
1816
Pekka Paalanen661de3a2014-07-28 12:49:24 +03001817 if (!weston_surface_is_mapped(sub->surface))
1818 return;
1819
Jason Ekstranda7af7042013-10-12 22:38:11 -05001820 wl_list_for_each(iv, &sub->unused_views, surface_link) {
1821 if (iv->geometry.parent == parent) {
1822 view = iv;
1823 break;
Pekka Paalanene67858b2013-04-25 13:57:42 +03001824 }
1825 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001826
1827 if (view) {
1828 /* Put it back in the surface's list of views */
1829 wl_list_remove(&view->surface_link);
1830 wl_list_insert(&sub->surface->views, &view->surface_link);
1831 } else {
1832 view = weston_view_create(sub->surface);
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06001833 weston_view_set_position(view,
1834 sub->position.x,
1835 sub->position.y);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001836 weston_view_set_transform_parent(view, parent);
1837 }
1838
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03001839 view->parent_view = parent;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001840 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001841
Pekka Paalanenb188e912013-11-19 14:03:35 +02001842 if (wl_list_empty(&sub->surface->subsurface_list)) {
1843 wl_list_insert(compositor->view_list.prev, &view->link);
1844 return;
1845 }
1846
1847 wl_list_for_each(child, &sub->surface->subsurface_list, parent_link) {
1848 if (child->surface == sub->surface)
1849 wl_list_insert(compositor->view_list.prev, &view->link);
1850 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001851 view_list_add_subsurface_view(compositor, child, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001852 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001853}
1854
1855static void
1856view_list_add(struct weston_compositor *compositor,
1857 struct weston_view *view)
1858{
1859 struct weston_subsurface *sub;
1860
1861 weston_view_update_transform(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001862
Pekka Paalanenb188e912013-11-19 14:03:35 +02001863 if (wl_list_empty(&view->surface->subsurface_list)) {
1864 wl_list_insert(compositor->view_list.prev, &view->link);
1865 return;
1866 }
1867
1868 wl_list_for_each(sub, &view->surface->subsurface_list, parent_link) {
1869 if (sub->surface == view->surface)
1870 wl_list_insert(compositor->view_list.prev, &view->link);
1871 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001872 view_list_add_subsurface_view(compositor, sub, view);
Pekka Paalanenb188e912013-11-19 14:03:35 +02001873 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05001874}
1875
1876static void
1877weston_compositor_build_view_list(struct weston_compositor *compositor)
1878{
1879 struct weston_view *view;
1880 struct weston_layer *layer;
1881
1882 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001883 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001884 surface_stash_subsurface_views(view->surface);
1885
1886 wl_list_init(&compositor->view_list);
1887 wl_list_for_each(layer, &compositor->layer_list, link) {
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001888 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05001889 view_list_add(compositor, view);
1890 }
1891 }
1892
1893 wl_list_for_each(layer, &compositor->layer_list, link)
Giulio Camuffo412e6a52014-07-09 22:12:56 +03001894 wl_list_for_each(view, &layer->view_list.link, layer_link.link)
Jason Ekstranda7af7042013-10-12 22:38:11 -05001895 surface_free_unused_subsurface_views(view->surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001896}
1897
David Herrmann1edf44c2013-10-22 17:11:26 +02001898static int
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001899weston_output_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001900{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001901 struct weston_compositor *ec = output->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05001902 struct weston_view *ev;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001903 struct weston_animation *animation, *next;
1904 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001905 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001906 pixman_region32_t output_damage;
David Herrmann1edf44c2013-10-22 17:11:26 +02001907 int r;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001908
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02001909 if (output->destroying)
1910 return 0;
1911
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001912 /* Rebuild the surface list and update surface transforms up front. */
Jason Ekstranda7af7042013-10-12 22:38:11 -05001913 weston_compositor_build_view_list(ec);
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001914
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001915 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001916 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001917 else
Jason Ekstranda7af7042013-10-12 22:38:11 -05001918 wl_list_for_each(ev, &ec->view_list, link)
1919 weston_view_move_to_plane(ev, &ec->primary_plane);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001920
Pekka Paalanene67858b2013-04-25 13:57:42 +03001921 wl_list_init(&frame_callback_list);
Jason Ekstranda7af7042013-10-12 22:38:11 -05001922 wl_list_for_each(ev, &ec->view_list, link) {
1923 /* Note: This operation is safe to do multiple times on the
1924 * same surface.
1925 */
1926 if (ev->surface->output == output) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03001927 wl_list_insert_list(&frame_callback_list,
Jason Ekstranda7af7042013-10-12 22:38:11 -05001928 &ev->surface->frame_callback_list);
1929 wl_list_init(&ev->surface->frame_callback_list);
Pekka Paalanene67858b2013-04-25 13:57:42 +03001930 }
1931 }
1932
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02001933 compositor_accumulate_damage(ec);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001934
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001935 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001936 pixman_region32_intersect(&output_damage,
1937 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02001938 pixman_region32_subtract(&output_damage,
1939 &output_damage, &ec->primary_plane.clip);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001940
Scott Moreauccbf29d2012-02-22 14:21:41 -07001941 if (output->dirty)
1942 weston_output_update_matrix(output);
1943
David Herrmann1edf44c2013-10-22 17:11:26 +02001944 r = output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001945
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001946 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001947
Kristian Høgsbergef044142011-06-21 15:02:12 -04001948 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001949
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001950 weston_compositor_repick(ec);
1951 wl_event_loop_dispatch(ec->input_loop, 0);
1952
Jonas Ådahldb773762012-06-13 00:01:21 +02001953 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001954 wl_callback_send_done(cb->resource, output->frame_time);
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001955 wl_resource_destroy(cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001956 }
1957
Scott Moreaud64cf212012-06-08 19:40:54 -06001958 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001959 animation->frame_counter++;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001960 animation->frame(animation, output, output->frame_time);
Scott Moreaud64cf212012-06-08 19:40:54 -06001961 }
David Herrmann1edf44c2013-10-22 17:11:26 +02001962
1963 return r;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001964}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001965
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001966static int
1967weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001968{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001969 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001970
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001971 wl_event_loop_dispatch(compositor->input_loop, 0);
1972
1973 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001974}
1975
1976WL_EXPORT void
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001977weston_output_finish_frame(struct weston_output *output,
1978 const struct timespec *stamp)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001979{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001980 struct weston_compositor *compositor = output->compositor;
1981 struct wl_event_loop *loop =
1982 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001983 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001984
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001985 output->frame_time = stamp->tv_sec * 1000 + stamp->tv_nsec / 1000000;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001986
1987 if (output->repaint_needed &&
1988 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1989 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001990 r = weston_output_repaint(output);
David Herrmann1edf44c2013-10-22 17:11:26 +02001991 if (!r)
1992 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001993 }
1994
1995 output->repaint_scheduled = 0;
1996 if (compositor->input_loop_source)
1997 return;
1998
1999 fd = wl_event_loop_get_fd(compositor->input_loop);
2000 compositor->input_loop_source =
2001 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2002 weston_compositor_read_input, compositor);
2003}
2004
2005static void
2006idle_repaint(void *data)
2007{
2008 struct weston_output *output = data;
2009
Jonas Ådahle5a12252013-04-05 23:07:11 +02002010 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002011}
2012
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002013WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002014weston_layer_entry_insert(struct weston_layer_entry *list,
2015 struct weston_layer_entry *entry)
2016{
2017 wl_list_insert(&list->link, &entry->link);
2018 entry->layer = list->layer;
2019}
2020
2021WL_EXPORT void
2022weston_layer_entry_remove(struct weston_layer_entry *entry)
2023{
2024 wl_list_remove(&entry->link);
2025 wl_list_init(&entry->link);
2026 entry->layer = NULL;
2027}
2028
2029WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002030weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2031{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002032 wl_list_init(&layer->view_list.link);
2033 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002034 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002035 if (below != NULL)
2036 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002037}
2038
2039WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002040weston_layer_set_mask(struct weston_layer *layer,
2041 int x, int y, int width, int height)
2042{
2043 struct weston_view *view;
2044
2045 layer->mask.x1 = x;
2046 layer->mask.x2 = x + width;
2047 layer->mask.y1 = y;
2048 layer->mask.y2 = y + height;
2049
2050 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2051 weston_view_geometry_dirty(view);
2052 }
2053}
2054
2055WL_EXPORT void
2056weston_layer_set_mask_infinite(struct weston_layer *layer)
2057{
2058 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2059 UINT32_MAX, UINT32_MAX);
2060}
2061
2062WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002063weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002064{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002065 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002066 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002067
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002068 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2069 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002070 return;
2071
Kristian Høgsbergef044142011-06-21 15:02:12 -04002072 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002073 output->repaint_needed = 1;
2074 if (output->repaint_scheduled)
2075 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002076
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002077 wl_event_loop_add_idle(loop, idle_repaint, output);
2078 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002079
2080 if (compositor->input_loop_source) {
2081 wl_event_source_remove(compositor->input_loop_source);
2082 compositor->input_loop_source = NULL;
2083 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002084}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002085
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002086WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002087weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2088{
2089 struct weston_output *output;
2090
2091 wl_list_for_each(output, &compositor->output_list, link)
2092 weston_output_schedule_repaint(output);
2093}
2094
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002095static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002096surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002097{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002098 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002099}
2100
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002101static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002102surface_attach(struct wl_client *client,
2103 struct wl_resource *resource,
2104 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2105{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002106 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002107 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002108
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002109 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002110 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002111 if (buffer == NULL) {
2112 wl_client_post_no_memory(client);
2113 return;
2114 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002115 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002116
Pekka Paalanende685b82012-12-04 15:58:12 +02002117 /* Attach, attach, without commit in between does not send
2118 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002119 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002120
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002121 surface->pending.sx = sx;
2122 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002123 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002124}
2125
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002126static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002127surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002128 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002129 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002130{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002131 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002132
Pekka Paalanen8e159182012-10-10 12:49:25 +03002133 pixman_region32_union_rect(&surface->pending.damage,
2134 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002135 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002136}
2137
Kristian Høgsberg33418202011-08-16 23:01:28 -04002138static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002139destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002140{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002141 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002142
2143 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002144 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002145}
2146
2147static void
2148surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002149 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002150{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002151 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002152 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002153
2154 cb = malloc(sizeof *cb);
2155 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002156 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002157 return;
2158 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002159
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002160 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2161 callback);
2162 if (cb->resource == NULL) {
2163 free(cb);
2164 wl_resource_post_no_memory(resource);
2165 return;
2166 }
2167
Jason Ekstranda85118c2013-06-27 20:17:02 -05002168 wl_resource_set_implementation(cb->resource, NULL, cb,
2169 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002170
Pekka Paalanenbc106382012-10-10 12:49:31 +03002171 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002172}
2173
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002174static void
2175surface_set_opaque_region(struct wl_client *client,
2176 struct wl_resource *resource,
2177 struct wl_resource *region_resource)
2178{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002179 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002180 struct weston_region *region;
2181
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002182 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002183 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002184 pixman_region32_copy(&surface->pending.opaque,
2185 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002186 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002187 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002188 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002189}
2190
2191static void
2192surface_set_input_region(struct wl_client *client,
2193 struct wl_resource *resource,
2194 struct wl_resource *region_resource)
2195{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002196 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002197 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002198
2199 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002200 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002201 pixman_region32_copy(&surface->pending.input,
2202 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002203 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002204 pixman_region32_fini(&surface->pending.input);
2205 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002206 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002207}
2208
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002209static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002210weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002211{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002212 struct weston_subsurface *sub;
2213
2214 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2215 parent_link_pending) {
2216 wl_list_remove(&sub->parent_link);
2217 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2218 }
2219}
2220
2221static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002222weston_surface_commit_state(struct weston_surface *surface,
2223 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002224{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002225 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002226 pixman_region32_t opaque;
2227
Alexander Larsson4ea95522013-05-22 14:41:37 +02002228 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002229 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002230 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002231 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002232
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002233 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002234 if (state->newly_attached)
2235 weston_surface_attach(surface, state->buffer);
2236 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002237
Jason Ekstrand7b982072014-05-20 14:33:03 -05002238 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002239 weston_surface_update_size(surface);
2240 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002241 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002242 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002243
Jason Ekstrand7b982072014-05-20 14:33:03 -05002244 state->sx = 0;
2245 state->sy = 0;
2246 state->newly_attached = 0;
2247 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002248
2249 /* wl_surface.damage */
2250 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002251 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002252 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002253 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002254 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002255
2256 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002257 pixman_region32_init(&opaque);
2258 pixman_region32_intersect_rect(&opaque, &state->opaque,
2259 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002260
2261 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2262 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002263 wl_list_for_each(view, &surface->views, surface_link)
2264 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002265 }
2266
2267 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002268
2269 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002270 pixman_region32_intersect_rect(&surface->input, &state->input,
2271 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002272
Pekka Paalanenbc106382012-10-10 12:49:31 +03002273 /* wl_surface.frame */
2274 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002275 &state->frame_callback_list);
2276 wl_list_init(&state->frame_callback_list);
2277}
2278
2279static void
2280weston_surface_commit(struct weston_surface *surface)
2281{
2282 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002283
Pekka Paalanene67858b2013-04-25 13:57:42 +03002284 weston_surface_commit_subsurface_order(surface);
2285
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002286 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002287}
2288
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002289static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002290weston_subsurface_commit(struct weston_subsurface *sub);
2291
2292static void
2293weston_subsurface_parent_commit(struct weston_subsurface *sub,
2294 int parent_is_synchronized);
2295
2296static void
2297surface_commit(struct wl_client *client, struct wl_resource *resource)
2298{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002299 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002300 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2301
2302 if (sub) {
2303 weston_subsurface_commit(sub);
2304 return;
2305 }
2306
2307 weston_surface_commit(surface);
2308
2309 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2310 if (sub->surface != surface)
2311 weston_subsurface_parent_commit(sub, 0);
2312 }
2313}
2314
2315static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002316surface_set_buffer_transform(struct wl_client *client,
2317 struct wl_resource *resource, int transform)
2318{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002319 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002320
Jonny Lamba55f1392014-05-30 12:07:15 +02002321 /* if wl_output.transform grows more members this will need to be updated. */
2322 if (transform < 0 ||
2323 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2324 wl_resource_post_error(resource,
2325 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2326 "buffer transform must be a valid transform "
2327 "('%d' specified)", transform);
2328 return;
2329 }
2330
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002331 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002332 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002333}
2334
Alexander Larsson4ea95522013-05-22 14:41:37 +02002335static void
2336surface_set_buffer_scale(struct wl_client *client,
2337 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002338 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002339{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002340 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002341
Jonny Lamba55f1392014-05-30 12:07:15 +02002342 if (scale < 1) {
2343 wl_resource_post_error(resource,
2344 WL_SURFACE_ERROR_INVALID_SCALE,
2345 "buffer scale must be at least one "
2346 "('%d' specified)", scale);
2347 return;
2348 }
2349
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002350 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002351 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002352}
2353
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002354static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002355 surface_destroy,
2356 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002357 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002358 surface_frame,
2359 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002360 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002361 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002362 surface_set_buffer_transform,
2363 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002364};
2365
2366static void
2367compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002368 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002369{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002370 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002371 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002372
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002373 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002374 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002375 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002376 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002377 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002378
Jason Ekstranda85118c2013-06-27 20:17:02 -05002379 surface->resource =
2380 wl_resource_create(client, &wl_surface_interface,
2381 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002382 if (surface->resource == NULL) {
2383 weston_surface_destroy(surface);
2384 wl_resource_post_no_memory(resource);
2385 return;
2386 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002387 wl_resource_set_implementation(surface->resource, &surface_interface,
2388 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002389
2390 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002391}
2392
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002393static void
2394destroy_region(struct wl_resource *resource)
2395{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002396 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002397
2398 pixman_region32_fini(&region->region);
2399 free(region);
2400}
2401
2402static void
2403region_destroy(struct wl_client *client, struct wl_resource *resource)
2404{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002405 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002406}
2407
2408static void
2409region_add(struct wl_client *client, struct wl_resource *resource,
2410 int32_t x, int32_t y, int32_t width, int32_t height)
2411{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002412 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002413
2414 pixman_region32_union_rect(&region->region, &region->region,
2415 x, y, width, height);
2416}
2417
2418static void
2419region_subtract(struct wl_client *client, struct wl_resource *resource,
2420 int32_t x, int32_t y, int32_t width, int32_t height)
2421{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002422 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002423 pixman_region32_t rect;
2424
2425 pixman_region32_init_rect(&rect, x, y, width, height);
2426 pixman_region32_subtract(&region->region, &region->region, &rect);
2427 pixman_region32_fini(&rect);
2428}
2429
2430static const struct wl_region_interface region_interface = {
2431 region_destroy,
2432 region_add,
2433 region_subtract
2434};
2435
2436static void
2437compositor_create_region(struct wl_client *client,
2438 struct wl_resource *resource, uint32_t id)
2439{
2440 struct weston_region *region;
2441
2442 region = malloc(sizeof *region);
2443 if (region == NULL) {
2444 wl_resource_post_no_memory(resource);
2445 return;
2446 }
2447
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002448 pixman_region32_init(&region->region);
2449
Jason Ekstranda85118c2013-06-27 20:17:02 -05002450 region->resource =
2451 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002452 if (region->resource == NULL) {
2453 free(region);
2454 wl_resource_post_no_memory(resource);
2455 return;
2456 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002457 wl_resource_set_implementation(region->resource, &region_interface,
2458 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002459}
2460
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002461static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002462 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002463 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002464};
2465
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002466static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002467weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2468{
2469 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002470
Jason Ekstrand7b982072014-05-20 14:33:03 -05002471 weston_surface_commit_state(surface, &sub->cached);
2472 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002473
2474 weston_surface_commit_subsurface_order(surface);
2475
2476 weston_surface_schedule_repaint(surface);
2477
Jason Ekstrand7b982072014-05-20 14:33:03 -05002478 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002479}
2480
2481static void
2482weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2483{
2484 struct weston_surface *surface = sub->surface;
2485
2486 /*
2487 * If this commit would cause the surface to move by the
2488 * attach(dx, dy) parameters, the old damage region must be
2489 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002490 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002491 */
2492 pixman_region32_translate(&sub->cached.damage,
2493 -surface->pending.sx, -surface->pending.sy);
2494 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2495 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002496 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002497
2498 if (surface->pending.newly_attached) {
2499 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002500 weston_surface_state_set_buffer(&sub->cached,
2501 surface->pending.buffer);
2502 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002503 surface->pending.buffer);
2504 }
2505 sub->cached.sx += surface->pending.sx;
2506 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002507
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002508 sub->cached.buffer_viewport.changed |=
2509 surface->pending.buffer_viewport.changed;
2510 sub->cached.buffer_viewport.buffer =
2511 surface->pending.buffer_viewport.buffer;
2512 sub->cached.buffer_viewport.surface =
2513 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002514
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002515 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002516
2517 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2518
2519 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2520
2521 wl_list_insert_list(&sub->cached.frame_callback_list,
2522 &surface->pending.frame_callback_list);
2523 wl_list_init(&surface->pending.frame_callback_list);
2524
Jason Ekstrand7b982072014-05-20 14:33:03 -05002525 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002526}
2527
2528static int
2529weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2530{
2531 while (sub) {
2532 if (sub->synchronized)
2533 return 1;
2534
2535 if (!sub->parent)
2536 return 0;
2537
2538 sub = weston_surface_to_subsurface(sub->parent);
2539 }
2540
2541 return 0;
2542}
2543
2544static void
2545weston_subsurface_commit(struct weston_subsurface *sub)
2546{
2547 struct weston_surface *surface = sub->surface;
2548 struct weston_subsurface *tmp;
2549
2550 /* Recursive check for effectively synchronized. */
2551 if (weston_subsurface_is_synchronized(sub)) {
2552 weston_subsurface_commit_to_cache(sub);
2553 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002554 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002555 /* flush accumulated state from cache */
2556 weston_subsurface_commit_to_cache(sub);
2557 weston_subsurface_commit_from_cache(sub);
2558 } else {
2559 weston_surface_commit(surface);
2560 }
2561
2562 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2563 if (tmp->surface != surface)
2564 weston_subsurface_parent_commit(tmp, 0);
2565 }
2566 }
2567}
2568
2569static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002570weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002571{
2572 struct weston_surface *surface = sub->surface;
2573 struct weston_subsurface *tmp;
2574
Pekka Paalanene67858b2013-04-25 13:57:42 +03002575 /* From now on, commit_from_cache the whole sub-tree, regardless of
2576 * the synchronized mode of each child. This sub-surface or some
2577 * of its ancestors were synchronized, so we are synchronized
2578 * all the way down.
2579 */
2580
Jason Ekstrand7b982072014-05-20 14:33:03 -05002581 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002582 weston_subsurface_commit_from_cache(sub);
2583
2584 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2585 if (tmp->surface != surface)
2586 weston_subsurface_parent_commit(tmp, 1);
2587 }
2588}
2589
2590static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002591weston_subsurface_parent_commit(struct weston_subsurface *sub,
2592 int parent_is_synchronized)
2593{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002594 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002595 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002596 wl_list_for_each(view, &sub->surface->views, surface_link)
2597 weston_view_set_position(view,
2598 sub->position.x,
2599 sub->position.y);
2600
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002601 sub->position.set = 0;
2602 }
2603
2604 if (parent_is_synchronized || sub->synchronized)
2605 weston_subsurface_synchronized_commit(sub);
2606}
2607
2608static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002609subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002610{
2611 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002612 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002613
Jason Ekstranda7af7042013-10-12 22:38:11 -05002614 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002615 weston_view_set_position(view,
2616 view->geometry.x + dx,
2617 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002618
2619 /* No need to check parent mappedness, because if parent is not
2620 * mapped, parent is not in a visible layer, so this sub-surface
2621 * will not be drawn either.
2622 */
2623 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002624 struct weston_output *output;
2625
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002626 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002627 * because that would call it also for the parent surface,
2628 * which might not be mapped yet. That would lead to
2629 * inconsistent state, where the window could never be
2630 * mapped.
2631 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002632 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002633 * weston_surface_is_mapped() return true, so that when the
2634 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002635 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002636 */
2637 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002638 output = container_of(compositor->output_list.next,
2639 struct weston_output, link);
2640
2641 surface->output = output;
2642 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002643 }
2644}
2645
2646static struct weston_subsurface *
2647weston_surface_to_subsurface(struct weston_surface *surface)
2648{
2649 if (surface->configure == subsurface_configure)
2650 return surface->configure_private;
2651
2652 return NULL;
2653}
2654
Pekka Paalanen01388e22013-04-25 13:57:44 +03002655WL_EXPORT struct weston_surface *
2656weston_surface_get_main_surface(struct weston_surface *surface)
2657{
2658 struct weston_subsurface *sub;
2659
2660 while (surface && (sub = weston_surface_to_subsurface(surface)))
2661 surface = sub->parent;
2662
2663 return surface;
2664}
2665
Pekka Paalanene67858b2013-04-25 13:57:42 +03002666static void
2667subsurface_set_position(struct wl_client *client,
2668 struct wl_resource *resource, int32_t x, int32_t y)
2669{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002670 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002671
2672 if (!sub)
2673 return;
2674
2675 sub->position.x = x;
2676 sub->position.y = y;
2677 sub->position.set = 1;
2678}
2679
2680static struct weston_subsurface *
2681subsurface_from_surface(struct weston_surface *surface)
2682{
2683 struct weston_subsurface *sub;
2684
2685 sub = weston_surface_to_subsurface(surface);
2686 if (sub)
2687 return sub;
2688
2689 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2690 if (sub->surface == surface)
2691 return sub;
2692
2693 return NULL;
2694}
2695
2696static struct weston_subsurface *
2697subsurface_sibling_check(struct weston_subsurface *sub,
2698 struct weston_surface *surface,
2699 const char *request)
2700{
2701 struct weston_subsurface *sibling;
2702
2703 sibling = subsurface_from_surface(surface);
2704
2705 if (!sibling) {
2706 wl_resource_post_error(sub->resource,
2707 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2708 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002709 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002710 return NULL;
2711 }
2712
2713 if (sibling->parent != sub->parent) {
2714 wl_resource_post_error(sub->resource,
2715 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2716 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002717 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002718 return NULL;
2719 }
2720
2721 return sibling;
2722}
2723
2724static void
2725subsurface_place_above(struct wl_client *client,
2726 struct wl_resource *resource,
2727 struct wl_resource *sibling_resource)
2728{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002729 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002730 struct weston_surface *surface =
2731 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002732 struct weston_subsurface *sibling;
2733
2734 if (!sub)
2735 return;
2736
2737 sibling = subsurface_sibling_check(sub, surface, "place_above");
2738 if (!sibling)
2739 return;
2740
2741 wl_list_remove(&sub->parent_link_pending);
2742 wl_list_insert(sibling->parent_link_pending.prev,
2743 &sub->parent_link_pending);
2744}
2745
2746static void
2747subsurface_place_below(struct wl_client *client,
2748 struct wl_resource *resource,
2749 struct wl_resource *sibling_resource)
2750{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002751 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002752 struct weston_surface *surface =
2753 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002754 struct weston_subsurface *sibling;
2755
2756 if (!sub)
2757 return;
2758
2759 sibling = subsurface_sibling_check(sub, surface, "place_below");
2760 if (!sibling)
2761 return;
2762
2763 wl_list_remove(&sub->parent_link_pending);
2764 wl_list_insert(&sibling->parent_link_pending,
2765 &sub->parent_link_pending);
2766}
2767
2768static void
2769subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2770{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002771 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002772
2773 if (sub)
2774 sub->synchronized = 1;
2775}
2776
2777static void
2778subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2779{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002780 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002781
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002782 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002783 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002784
2785 /* If sub became effectively desynchronized, flush. */
2786 if (!weston_subsurface_is_synchronized(sub))
2787 weston_subsurface_synchronized_commit(sub);
2788 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002789}
2790
2791static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002792weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2793{
2794 wl_list_remove(&sub->parent_link);
2795 wl_list_remove(&sub->parent_link_pending);
2796 wl_list_remove(&sub->parent_destroy_listener.link);
2797 sub->parent = NULL;
2798}
2799
2800static void
2801weston_subsurface_destroy(struct weston_subsurface *sub);
2802
2803static void
2804subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2805{
2806 struct weston_subsurface *sub =
2807 container_of(listener, struct weston_subsurface,
2808 surface_destroy_listener);
2809 assert(data == &sub->surface->resource);
2810
2811 /* The protocol object (wl_resource) is left inert. */
2812 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002813 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002814
2815 weston_subsurface_destroy(sub);
2816}
2817
2818static void
2819subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2820{
2821 struct weston_subsurface *sub =
2822 container_of(listener, struct weston_subsurface,
2823 parent_destroy_listener);
2824 assert(data == &sub->parent->resource);
2825 assert(sub->surface != sub->parent);
2826
2827 if (weston_surface_is_mapped(sub->surface))
2828 weston_surface_unmap(sub->surface);
2829
2830 weston_subsurface_unlink_parent(sub);
2831}
2832
2833static void
2834subsurface_resource_destroy(struct wl_resource *resource)
2835{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002836 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002837
2838 if (sub)
2839 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002840}
2841
2842static void
2843subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2844{
2845 wl_resource_destroy(resource);
2846}
2847
2848static void
2849weston_subsurface_link_parent(struct weston_subsurface *sub,
2850 struct weston_surface *parent)
2851{
2852 sub->parent = parent;
2853 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002854 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002855 &sub->parent_destroy_listener);
2856
2857 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2858 wl_list_insert(&parent->subsurface_list_pending,
2859 &sub->parent_link_pending);
2860}
2861
2862static void
2863weston_subsurface_link_surface(struct weston_subsurface *sub,
2864 struct weston_surface *surface)
2865{
2866 sub->surface = surface;
2867 sub->surface_destroy_listener.notify =
2868 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002869 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002870 &sub->surface_destroy_listener);
2871}
2872
2873static void
2874weston_subsurface_destroy(struct weston_subsurface *sub)
2875{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002876 struct weston_view *view, *next;
2877
Pekka Paalanene67858b2013-04-25 13:57:42 +03002878 assert(sub->surface);
2879
2880 if (sub->resource) {
2881 assert(weston_surface_to_subsurface(sub->surface) == sub);
2882 assert(sub->parent_destroy_listener.notify ==
2883 subsurface_handle_parent_destroy);
2884
George Kiagiadakised04d382014-06-13 18:10:26 +02002885 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2886 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002887 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002888 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002889
Pekka Paalanene67858b2013-04-25 13:57:42 +03002890 if (sub->parent)
2891 weston_subsurface_unlink_parent(sub);
2892
Jason Ekstrand7b982072014-05-20 14:33:03 -05002893 weston_surface_state_fini(&sub->cached);
2894 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002895
2896 sub->surface->configure = NULL;
2897 sub->surface->configure_private = NULL;
2898 } else {
2899 /* the dummy weston_subsurface for the parent itself */
2900 assert(sub->parent_destroy_listener.notify == NULL);
2901 wl_list_remove(&sub->parent_link);
2902 wl_list_remove(&sub->parent_link_pending);
2903 }
2904
2905 wl_list_remove(&sub->surface_destroy_listener.link);
2906 free(sub);
2907}
2908
2909static const struct wl_subsurface_interface subsurface_implementation = {
2910 subsurface_destroy,
2911 subsurface_set_position,
2912 subsurface_place_above,
2913 subsurface_place_below,
2914 subsurface_set_sync,
2915 subsurface_set_desync
2916};
2917
2918static struct weston_subsurface *
2919weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2920 struct weston_surface *parent)
2921{
2922 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002923 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002924
2925 sub = calloc(1, sizeof *sub);
2926 if (!sub)
2927 return NULL;
2928
Jason Ekstranda7af7042013-10-12 22:38:11 -05002929 wl_list_init(&sub->unused_views);
2930
Jason Ekstranda85118c2013-06-27 20:17:02 -05002931 sub->resource =
2932 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002933 if (!sub->resource) {
2934 free(sub);
2935 return NULL;
2936 }
2937
Jason Ekstranda85118c2013-06-27 20:17:02 -05002938 wl_resource_set_implementation(sub->resource,
2939 &subsurface_implementation,
2940 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002941 weston_subsurface_link_surface(sub, surface);
2942 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002943 weston_surface_state_init(&sub->cached);
2944 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002945 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002946
2947 return sub;
2948}
2949
2950/* Create a dummy subsurface for having the parent itself in its
2951 * sub-surface lists. Makes stacking order manipulation easy.
2952 */
2953static struct weston_subsurface *
2954weston_subsurface_create_for_parent(struct weston_surface *parent)
2955{
2956 struct weston_subsurface *sub;
2957
2958 sub = calloc(1, sizeof *sub);
2959 if (!sub)
2960 return NULL;
2961
2962 weston_subsurface_link_surface(sub, parent);
2963 sub->parent = parent;
2964 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2965 wl_list_insert(&parent->subsurface_list_pending,
2966 &sub->parent_link_pending);
2967
2968 return sub;
2969}
2970
2971static void
2972subcompositor_get_subsurface(struct wl_client *client,
2973 struct wl_resource *resource,
2974 uint32_t id,
2975 struct wl_resource *surface_resource,
2976 struct wl_resource *parent_resource)
2977{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002978 struct weston_surface *surface =
2979 wl_resource_get_user_data(surface_resource);
2980 struct weston_surface *parent =
2981 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002982 struct weston_subsurface *sub;
2983 static const char where[] = "get_subsurface: wl_subsurface@";
2984
2985 if (surface == parent) {
2986 wl_resource_post_error(resource,
2987 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2988 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002989 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002990 return;
2991 }
2992
2993 if (weston_surface_to_subsurface(surface)) {
2994 wl_resource_post_error(resource,
2995 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2996 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002997 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002998 return;
2999 }
3000
3001 if (surface->configure) {
3002 wl_resource_post_error(resource,
3003 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3004 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003005 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003006 return;
3007 }
3008
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003009 if (weston_surface_get_main_surface(parent) == surface) {
3010 wl_resource_post_error(resource,
3011 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3012 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003013 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003014 return;
3015 }
3016
Pekka Paalanene67858b2013-04-25 13:57:42 +03003017 /* make sure the parent is in its own list */
3018 if (wl_list_empty(&parent->subsurface_list)) {
3019 if (!weston_subsurface_create_for_parent(parent)) {
3020 wl_resource_post_no_memory(resource);
3021 return;
3022 }
3023 }
3024
3025 sub = weston_subsurface_create(id, surface, parent);
3026 if (!sub) {
3027 wl_resource_post_no_memory(resource);
3028 return;
3029 }
3030
3031 surface->configure = subsurface_configure;
3032 surface->configure_private = sub;
3033}
3034
3035static void
3036subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3037{
3038 wl_resource_destroy(resource);
3039}
3040
3041static const struct wl_subcompositor_interface subcompositor_interface = {
3042 subcompositor_destroy,
3043 subcompositor_get_subsurface
3044};
3045
3046static void
3047bind_subcompositor(struct wl_client *client,
3048 void *data, uint32_t version, uint32_t id)
3049{
3050 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003051 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003052
Jason Ekstranda85118c2013-06-27 20:17:02 -05003053 resource =
3054 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003055 if (resource == NULL) {
3056 wl_client_post_no_memory(client);
3057 return;
3058 }
3059 wl_resource_set_implementation(resource, &subcompositor_interface,
3060 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003061}
3062
3063static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003064weston_compositor_dpms(struct weston_compositor *compositor,
3065 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003066{
3067 struct weston_output *output;
3068
3069 wl_list_for_each(output, &compositor->output_list, link)
3070 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003071 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003072}
3073
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003074WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003075weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003076{
Neil Roberts8b62e202013-09-30 13:14:47 +01003077 uint32_t old_state = compositor->state;
3078
3079 /* The state needs to be changed before emitting the wake
3080 * signal because that may try to schedule a repaint which
3081 * will not work if the compositor is still sleeping */
3082 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3083
3084 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003085 case WESTON_COMPOSITOR_SLEEPING:
3086 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3087 /* fall through */
3088 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003089 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003090 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003091 /* fall through */
3092 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003093 wl_event_source_timer_update(compositor->idle_source,
3094 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003095 }
3096}
3097
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003098WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003099weston_compositor_offscreen(struct weston_compositor *compositor)
3100{
3101 switch (compositor->state) {
3102 case WESTON_COMPOSITOR_OFFSCREEN:
3103 return;
3104 case WESTON_COMPOSITOR_SLEEPING:
3105 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3106 /* fall through */
3107 default:
3108 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3109 wl_event_source_timer_update(compositor->idle_source, 0);
3110 }
3111}
3112
3113WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003114weston_compositor_sleep(struct weston_compositor *compositor)
3115{
3116 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3117 return;
3118
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003119 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003120 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3121 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3122}
3123
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003124static int
3125idle_handler(void *data)
3126{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003127 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003128
3129 if (compositor->idle_inhibit)
3130 return 1;
3131
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003132 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003133 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003134
3135 return 1;
3136}
3137
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003138WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003139weston_plane_init(struct weston_plane *plane,
3140 struct weston_compositor *ec,
3141 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003142{
3143 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003144 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003145 plane->x = x;
3146 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003147 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003148
3149 /* Init the link so that the call to wl_list_remove() when releasing
3150 * the plane without ever stacking doesn't lead to a crash */
3151 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003152}
3153
3154WL_EXPORT void
3155weston_plane_release(struct weston_plane *plane)
3156{
Xiong Zhang97116532013-10-23 13:58:31 +08003157 struct weston_view *view;
3158
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003159 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003160 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003161
Xiong Zhang97116532013-10-23 13:58:31 +08003162 wl_list_for_each(view, &plane->compositor->view_list, link) {
3163 if (view->plane == plane)
3164 view->plane = NULL;
3165 }
3166
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003167 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003168}
3169
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003170WL_EXPORT void
3171weston_compositor_stack_plane(struct weston_compositor *ec,
3172 struct weston_plane *plane,
3173 struct weston_plane *above)
3174{
3175 if (above)
3176 wl_list_insert(above->link.prev, &plane->link);
3177 else
3178 wl_list_insert(&ec->plane_list, &plane->link);
3179}
3180
Casey Dahlin9074db52012-04-19 22:50:09 -04003181static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003182{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003183 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003184}
3185
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003186static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003187bind_output(struct wl_client *client,
3188 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003189{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003190 struct weston_output *output = data;
3191 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003192 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003193
Jason Ekstranda85118c2013-06-27 20:17:02 -05003194 resource = wl_resource_create(client, &wl_output_interface,
3195 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003196 if (resource == NULL) {
3197 wl_client_post_no_memory(client);
3198 return;
3199 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003200
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003201 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003202 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003203
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003204 wl_output_send_geometry(resource,
3205 output->x,
3206 output->y,
3207 output->mm_width,
3208 output->mm_height,
3209 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003210 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003211 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003212 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003213 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003214 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003215
3216 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003217 wl_output_send_mode(resource,
3218 mode->flags,
3219 mode->width,
3220 mode->height,
3221 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003222 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003223
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003224 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003225 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003226}
3227
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003228/* Move other outputs when one is removed so the space remains contiguos. */
3229static void
3230weston_compositor_remove_output(struct weston_compositor *compositor,
3231 struct weston_output *remove_output)
3232{
3233 struct weston_output *output;
3234 int offset = 0;
3235
3236 wl_list_for_each(output, &compositor->output_list, link) {
3237 if (output == remove_output) {
3238 offset = output->width;
3239 continue;
3240 }
3241
3242 if (offset > 0) {
3243 weston_output_move(output,
3244 output->x - offset, output->y);
3245 output->dirty = 1;
3246 }
3247 }
3248}
3249
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003250WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003251weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003252{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003253 struct wl_resource *resource;
3254
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003255 output->destroying = 1;
3256
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003257 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003258 wl_list_remove(&output->link);
3259
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003260 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003261 wl_signal_emit(&output->destroy_signal, output);
3262
Richard Hughesafe690c2013-05-02 10:10:04 +01003263 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003264 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003265 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003266 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003267
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003268 wl_resource_for_each(resource, &output->resource_list) {
3269 wl_resource_set_destructor(resource, NULL);
3270 }
3271
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003272 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003273}
3274
Scott Moreau1bad5db2012-08-18 01:04:05 -06003275static void
3276weston_output_compute_transform(struct weston_output *output)
3277{
3278 struct weston_matrix transform;
3279 int flip;
3280
3281 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003282 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003283
3284 switch(output->transform) {
3285 case WL_OUTPUT_TRANSFORM_FLIPPED:
3286 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3287 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3288 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003289 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003290 flip = -1;
3291 break;
3292 default:
3293 flip = 1;
3294 break;
3295 }
3296
3297 switch(output->transform) {
3298 case WL_OUTPUT_TRANSFORM_NORMAL:
3299 case WL_OUTPUT_TRANSFORM_FLIPPED:
3300 transform.d[0] = flip;
3301 transform.d[1] = 0;
3302 transform.d[4] = 0;
3303 transform.d[5] = 1;
3304 break;
3305 case WL_OUTPUT_TRANSFORM_90:
3306 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3307 transform.d[0] = 0;
3308 transform.d[1] = -flip;
3309 transform.d[4] = 1;
3310 transform.d[5] = 0;
3311 break;
3312 case WL_OUTPUT_TRANSFORM_180:
3313 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3314 transform.d[0] = -flip;
3315 transform.d[1] = 0;
3316 transform.d[4] = 0;
3317 transform.d[5] = -1;
3318 break;
3319 case WL_OUTPUT_TRANSFORM_270:
3320 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3321 transform.d[0] = 0;
3322 transform.d[1] = flip;
3323 transform.d[4] = -1;
3324 transform.d[5] = 0;
3325 break;
3326 default:
3327 break;
3328 }
3329
3330 weston_matrix_multiply(&output->matrix, &transform);
3331}
3332
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003333WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003334weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003335{
Scott Moreau850ca422012-05-21 15:21:25 -06003336 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003337
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003338 weston_matrix_init(&output->matrix);
3339 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003340 -(output->x + output->width / 2.0),
3341 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003342
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003343 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003344 2.0 / output->width,
3345 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003346
Scott Moreauccbf29d2012-02-22 14:21:41 -07003347 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003348 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003349 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003350 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3351 output->zoom.trans_y, 0);
3352 weston_matrix_scale(&output->matrix, magnification,
3353 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003354 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003355
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003356 weston_output_compute_transform(output);
3357
Scott Moreauccbf29d2012-02-22 14:21:41 -07003358 output->dirty = 0;
3359}
3360
Scott Moreau1bad5db2012-08-18 01:04:05 -06003361static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003362weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003363{
3364 output->transform = transform;
3365
3366 switch (transform) {
3367 case WL_OUTPUT_TRANSFORM_90:
3368 case WL_OUTPUT_TRANSFORM_270:
3369 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3370 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3371 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003372 output->width = output->current_mode->height;
3373 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003374 break;
3375 case WL_OUTPUT_TRANSFORM_NORMAL:
3376 case WL_OUTPUT_TRANSFORM_180:
3377 case WL_OUTPUT_TRANSFORM_FLIPPED:
3378 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003379 output->width = output->current_mode->width;
3380 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003381 break;
3382 default:
3383 break;
3384 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003385
Hardening57388e42013-09-18 23:56:36 +02003386 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003387 output->width /= scale;
3388 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003389}
3390
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003391static void
3392weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003393{
3394 output->x = x;
3395 output->y = y;
3396
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003397 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003398 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003399 output->width,
3400 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003401}
3402
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003403WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003404weston_output_move(struct weston_output *output, int x, int y)
3405{
3406 pixman_region32_t old_region;
3407 struct wl_resource *resource;
3408
3409 output->move_x = x - output->x;
3410 output->move_y = y - output->y;
3411
3412 if (output->move_x == 0 && output->move_y == 0)
3413 return;
3414
3415 pixman_region32_init(&old_region);
3416 pixman_region32_copy(&old_region, &output->region);
3417
3418 weston_output_init_geometry(output, x, y);
3419
3420 output->dirty = 1;
3421
3422 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003423 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003424
3425 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003426 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003427 wl_output_send_geometry(resource,
3428 output->x,
3429 output->y,
3430 output->mm_width,
3431 output->mm_height,
3432 output->subpixel,
3433 output->make,
3434 output->model,
3435 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003436
3437 if (wl_resource_get_version(resource) >= 2)
3438 wl_output_send_done(resource);
3439 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003440}
3441
3442WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003443weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003444 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003445 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003446{
3447 output->compositor = c;
3448 output->x = x;
3449 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003450 output->mm_width = mm_width;
3451 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003452 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003453 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003454
Alexander Larsson0b135062013-05-28 16:23:36 +02003455 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003456 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003457
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003458 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003459 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003460
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003461 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003462 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003463 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003464 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003465
Casey Dahlin58ba1372012-04-19 22:50:08 -04003466 output->id = ffs(~output->compositor->output_id_pool) - 1;
3467 output->compositor->output_id_pool |= 1 << output->id;
3468
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003469 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003470 wl_global_create(c->wl_display, &wl_output_interface, 2,
3471 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003472 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003473}
3474
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003475WL_EXPORT void
3476weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003477 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003478 wl_fixed_t *x, wl_fixed_t *y)
3479{
3480 wl_fixed_t tx, ty;
3481 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003482 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003483
Hardeningff39efa2013-09-18 23:56:35 +02003484 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3485 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003486
3487 switch(output->transform) {
3488 case WL_OUTPUT_TRANSFORM_NORMAL:
3489 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003490 tx = device_x;
3491 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003492 break;
3493 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003494 tx = device_y;
3495 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003496 break;
3497 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003498 tx = width - device_x;
3499 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003500 break;
3501 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003502 tx = width - device_y;
3503 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003504 break;
3505 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003506 tx = width - device_x;
3507 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003508 break;
3509 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003510 tx = width - device_y;
3511 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003512 break;
3513 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003514 tx = device_x;
3515 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003516 break;
3517 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003518 tx = device_y;
3519 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003520 break;
3521 }
3522
Neil Robertseb5a2002014-05-01 16:13:55 +01003523 tx /= output->current_scale;
3524 ty /= output->current_scale;
3525
3526 if (output->zoom.active) {
3527 zoom_scale = output->zoom.spring_z.current;
3528 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3529 output->width / 2.0f *
3530 (zoom_scale + output->zoom.trans_x));
3531 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3532 output->height / 2.0f *
3533 (zoom_scale + output->zoom.trans_y));
3534 tx = wl_fixed_from_double(zx);
3535 ty = wl_fixed_from_double(zy);
3536 }
3537
3538 *x = tx + wl_fixed_from_int(output->x);
3539 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003540}
3541
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003542static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003543destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003544{
Jonny Lamb74130762013-11-26 18:19:46 +01003545 struct weston_surface *surface =
3546 wl_resource_get_user_data(resource);
3547
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003548 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003549 surface->pending.buffer_viewport.buffer.src_width =
3550 wl_fixed_from_int(-1);
3551 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003552 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003553}
3554
3555static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003556viewport_destroy(struct wl_client *client,
3557 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003558{
3559 wl_resource_destroy(resource);
3560}
3561
3562static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003563viewport_set(struct wl_client *client,
3564 struct wl_resource *resource,
3565 wl_fixed_t src_x,
3566 wl_fixed_t src_y,
3567 wl_fixed_t src_width,
3568 wl_fixed_t src_height,
3569 int32_t dst_width,
3570 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003571{
Jonny Lamb74130762013-11-26 18:19:46 +01003572 struct weston_surface *surface =
3573 wl_resource_get_user_data(resource);
3574
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003575 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003576
Jonny Lamb8ae35902013-11-26 18:19:45 +01003577 if (wl_fixed_to_double(src_width) < 0 ||
3578 wl_fixed_to_double(src_height) < 0) {
3579 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003580 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003581 "source dimensions must be non-negative (%fx%f)",
3582 wl_fixed_to_double(src_width),
3583 wl_fixed_to_double(src_height));
3584 return;
3585 }
3586
3587 if (dst_width <= 0 || dst_height <= 0) {
3588 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003589 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003590 "destination dimensions must be positive (%dx%d)",
3591 dst_width, dst_height);
3592 return;
3593 }
3594
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003595 surface->pending.buffer_viewport.buffer.src_x = src_x;
3596 surface->pending.buffer_viewport.buffer.src_y = src_y;
3597 surface->pending.buffer_viewport.buffer.src_width = src_width;
3598 surface->pending.buffer_viewport.buffer.src_height = src_height;
3599 surface->pending.buffer_viewport.surface.width = dst_width;
3600 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003601 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003602}
3603
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003604static void
3605viewport_set_source(struct wl_client *client,
3606 struct wl_resource *resource,
3607 wl_fixed_t src_x,
3608 wl_fixed_t src_y,
3609 wl_fixed_t src_width,
3610 wl_fixed_t src_height)
3611{
3612 struct weston_surface *surface =
3613 wl_resource_get_user_data(resource);
3614
3615 assert(surface->viewport_resource != NULL);
3616
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003617 if (src_width == wl_fixed_from_int(-1) &&
3618 src_height == wl_fixed_from_int(-1)) {
3619 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003620 surface->pending.buffer_viewport.buffer.src_width =
3621 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003622 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003623 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003624 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003625
3626 if (src_width <= 0 || src_height <= 0) {
3627 wl_resource_post_error(resource,
3628 WL_VIEWPORT_ERROR_BAD_VALUE,
3629 "source size must be positive (%fx%f)",
3630 wl_fixed_to_double(src_width),
3631 wl_fixed_to_double(src_height));
3632 return;
3633 }
3634
3635 surface->pending.buffer_viewport.buffer.src_x = src_x;
3636 surface->pending.buffer_viewport.buffer.src_y = src_y;
3637 surface->pending.buffer_viewport.buffer.src_width = src_width;
3638 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003639 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003640}
3641
3642static void
3643viewport_set_destination(struct wl_client *client,
3644 struct wl_resource *resource,
3645 int32_t dst_width,
3646 int32_t dst_height)
3647{
3648 struct weston_surface *surface =
3649 wl_resource_get_user_data(resource);
3650
3651 assert(surface->viewport_resource != NULL);
3652
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003653 if (dst_width == -1 && dst_height == -1) {
3654 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003655 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003656 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003657 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003658 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003659
3660 if (dst_width <= 0 || dst_height <= 0) {
3661 wl_resource_post_error(resource,
3662 WL_VIEWPORT_ERROR_BAD_VALUE,
3663 "destination size must be positive (%dx%d)",
3664 dst_width, dst_height);
3665 return;
3666 }
3667
3668 surface->pending.buffer_viewport.surface.width = dst_width;
3669 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003670 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003671}
3672
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003673static const struct wl_viewport_interface viewport_interface = {
3674 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003675 viewport_set,
3676 viewport_set_source,
3677 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003678};
3679
3680static void
3681scaler_destroy(struct wl_client *client,
3682 struct wl_resource *resource)
3683{
3684 wl_resource_destroy(resource);
3685}
3686
3687static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003688scaler_get_viewport(struct wl_client *client,
3689 struct wl_resource *scaler,
3690 uint32_t id,
3691 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003692{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003693 int version = wl_resource_get_version(scaler);
3694 struct weston_surface *surface =
3695 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003696 struct wl_resource *resource;
3697
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003698 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003699 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003700 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3701 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003702 return;
3703 }
3704
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003705 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003706 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003707 if (resource == NULL) {
3708 wl_client_post_no_memory(client);
3709 return;
3710 }
3711
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003712 wl_resource_set_implementation(resource, &viewport_interface,
3713 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003714
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003715 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003716}
3717
3718static const struct wl_scaler_interface scaler_interface = {
3719 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003720 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003721};
3722
3723static void
3724bind_scaler(struct wl_client *client,
3725 void *data, uint32_t version, uint32_t id)
3726{
3727 struct wl_resource *resource;
3728
3729 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003730 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003731 if (resource == NULL) {
3732 wl_client_post_no_memory(client);
3733 return;
3734 }
3735
3736 wl_resource_set_implementation(resource, &scaler_interface,
3737 NULL, NULL);
3738}
3739
3740static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003741presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3742{
3743 wl_resource_destroy(resource);
3744}
3745
3746static void
3747presentation_feedback(struct wl_client *client,
3748 struct wl_resource *resource,
3749 struct wl_resource *surface,
3750 uint32_t callback)
3751{
3752 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_METHOD,
3753 "presentation_feedback unimplemented");
3754}
3755
3756static const struct presentation_interface presentation_implementation = {
3757 presentation_destroy,
3758 presentation_feedback
3759};
3760
3761static void
3762bind_presentation(struct wl_client *client,
3763 void *data, uint32_t version, uint32_t id)
3764{
3765 struct weston_compositor *compositor = data;
3766 struct wl_resource *resource;
3767
3768 resource = wl_resource_create(client, &presentation_interface,
3769 MIN(version, 1), id);
3770 if (resource == NULL) {
3771 wl_client_post_no_memory(client);
3772 return;
3773 }
3774
3775 wl_resource_set_implementation(resource, &presentation_implementation,
3776 compositor, NULL);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003777 presentation_send_clock_id(resource, compositor->presentation_clock);
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003778}
3779
3780static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003781compositor_bind(struct wl_client *client,
3782 void *data, uint32_t version, uint32_t id)
3783{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003784 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003785 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003786
Jason Ekstranda85118c2013-06-27 20:17:02 -05003787 resource = wl_resource_create(client, &wl_compositor_interface,
3788 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003789 if (resource == NULL) {
3790 wl_client_post_no_memory(client);
3791 return;
3792 }
3793
3794 wl_resource_set_implementation(resource, &compositor_interface,
3795 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003796}
3797
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003798static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003799log_uname(void)
3800{
3801 struct utsname usys;
3802
3803 uname(&usys);
3804
3805 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3806 usys.version, usys.machine);
3807}
3808
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003809WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003810weston_environment_get_fd(const char *env)
3811{
3812 char *e, *end;
3813 int fd, flags;
3814
3815 e = getenv(env);
3816 if (!e)
3817 return -1;
3818 fd = strtol(e, &end, 0);
3819 if (*end != '\0')
3820 return -1;
3821
3822 flags = fcntl(fd, F_GETFD);
3823 if (flags == -1)
3824 return -1;
3825
3826 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3827 unsetenv(env);
3828
3829 return fd;
3830}
3831
3832WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003833weston_compositor_init(struct weston_compositor *ec,
3834 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003835 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003836 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003837{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003838 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003839 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003840 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003841
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003842 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003843 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003844 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003845 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003846 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003847 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003848 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003849 wl_signal_init(&ec->idle_signal);
3850 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003851 wl_signal_init(&ec->show_input_panel_signal);
3852 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003853 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003854 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003855 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003856 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003857 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003858 wl_signal_init(&ec->session_signal);
3859 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003860
Casey Dahlin58ba1372012-04-19 22:50:08 -04003861 ec->output_id_pool = 0;
3862
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003863 if (!wl_global_create(display, &wl_compositor_interface, 3,
3864 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003865 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003866
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003867 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3868 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003869 return -1;
3870
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003871 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003872 ec, bind_scaler))
3873 return -1;
3874
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003875 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
3876 ec, bind_presentation))
3877 return -1;
3878
Jason Ekstranda7af7042013-10-12 22:38:11 -05003879 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003880 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003881 wl_list_init(&ec->layer_list);
3882 wl_list_init(&ec->seat_list);
3883 wl_list_init(&ec->output_list);
3884 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003885 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003886 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003887 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003888 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003889 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003890
Xiong Zhang97116532013-10-23 13:58:31 +08003891 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003892 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003893
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003894 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3895 weston_config_section_get_string(s, "keymap_rules",
3896 (char **) &xkb_names.rules, NULL);
3897 weston_config_section_get_string(s, "keymap_model",
3898 (char **) &xkb_names.model, NULL);
3899 weston_config_section_get_string(s, "keymap_layout",
3900 (char **) &xkb_names.layout, NULL);
3901 weston_config_section_get_string(s, "keymap_variant",
3902 (char **) &xkb_names.variant, NULL);
3903 weston_config_section_get_string(s, "keymap_options",
3904 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003905
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003906 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3907 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003908
Jonny Lamb66a41a02014-08-12 14:58:25 +02003909 weston_config_section_get_int(s, "repeat-rate",
3910 &ec->kb_repeat_rate, 40);
3911 weston_config_section_get_int(s, "repeat-delay",
3912 &ec->kb_repeat_delay, 400);
3913
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003914 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003915
3916 wl_data_device_manager_init(ec->wl_display);
3917
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003918 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003919
Daniel Stone725c2c32012-06-22 14:04:36 +01003920 loop = wl_display_get_event_loop(ec->wl_display);
3921 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3922 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3923
3924 ec->input_loop = wl_event_loop_create();
3925
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003926 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3927 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3928
3929 weston_compositor_schedule_repaint(ec);
3930
Daniel Stone725c2c32012-06-22 14:04:36 +01003931 return 0;
3932}
3933
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003934WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003935weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003936{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003937 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003938
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003939 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003940 if (ec->input_loop_source)
3941 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003942
Matt Roper361d2ad2011-08-29 13:52:23 -07003943 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003944 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003945 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003946
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003947 if (ec->renderer)
3948 ec->renderer->destroy(ec);
3949
Daniel Stone325fc2d2012-05-30 16:31:58 +01003950 weston_binding_list_destroy_all(&ec->key_binding_list);
3951 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003952 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003953 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003954 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003955
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003956 weston_plane_release(&ec->primary_plane);
3957
Jonas Ådahlc97af922012-03-28 22:36:09 +02003958 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003959
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003960 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003961}
3962
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003963WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003964weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3965 const struct weston_pointer_grab_interface *interface)
3966{
3967 struct weston_seat *seat;
3968
3969 ec->default_pointer_grab = interface;
3970 wl_list_for_each(seat, &ec->seat_list, link) {
3971 if (seat->pointer) {
3972 weston_pointer_set_default_grab(seat->pointer,
3973 interface);
3974 }
3975 }
3976}
3977
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04003978WL_EXPORT int
3979weston_compositor_set_presentation_clock(struct weston_compositor *compositor,
3980 clockid_t clk_id)
3981{
3982 struct timespec ts;
3983
3984 if (clock_gettime(clk_id, &ts) < 0)
3985 return -1;
3986
3987 compositor->presentation_clock = clk_id;
3988
3989 return 0;
3990}
3991
3992/*
3993 * For choosing the software clock, when the display hardware or API
3994 * does not expose a compatible presentation timestamp.
3995 */
3996WL_EXPORT int
3997weston_compositor_set_presentation_clock_software(
3998 struct weston_compositor *compositor)
3999{
4000 /* In order of preference */
4001 static const clockid_t clocks[] = {
4002 CLOCK_MONOTONIC_RAW, /* no jumps, no crawling */
4003 CLOCK_MONOTONIC_COARSE, /* no jumps, may crawl, fast & coarse */
4004 CLOCK_MONOTONIC, /* no jumps, may crawl */
4005 CLOCK_REALTIME_COARSE, /* may jump and crawl, fast & coarse */
4006 CLOCK_REALTIME /* may jump and crawl */
4007 };
4008 unsigned i;
4009
4010 for (i = 0; i < ARRAY_LENGTH(clocks); i++)
4011 if (weston_compositor_set_presentation_clock(compositor,
4012 clocks[i]) == 0)
4013 return 0;
4014
4015 weston_log("Error: no suitable presentation clock available.\n");
4016
4017 return -1;
4018}
4019
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004020WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05004021weston_version(int *major, int *minor, int *micro)
4022{
4023 *major = WESTON_VERSION_MAJOR;
4024 *minor = WESTON_VERSION_MINOR;
4025 *micro = WESTON_VERSION_MICRO;
4026}
4027
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004028static const char *
4029clock_name(clockid_t clk_id)
4030{
4031 static const char *names[] = {
4032 [CLOCK_REALTIME] = "CLOCK_REALTIME",
4033 [CLOCK_MONOTONIC] = "CLOCK_MONOTONIC",
4034 [CLOCK_MONOTONIC_RAW] = "CLOCK_MONOTONIC_RAW",
4035 [CLOCK_REALTIME_COARSE] = "CLOCK_REALTIME_COARSE",
4036 [CLOCK_MONOTONIC_COARSE] = "CLOCK_MONOTONIC_COARSE",
4037 [CLOCK_BOOTTIME] = "CLOCK_BOOTTIME",
4038 };
4039
4040 if (clk_id < 0 || (unsigned)clk_id >= ARRAY_LENGTH(names))
4041 return "unknown";
4042
4043 return names[clk_id];
4044}
4045
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004046static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004047 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004048 const char *desc;
4049} capability_strings[] = {
4050 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03004051 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004052};
4053
4054static void
4055weston_compositor_log_capabilities(struct weston_compositor *compositor)
4056{
4057 unsigned i;
4058 int yes;
4059
4060 weston_log("Compositor capabilities:\n");
4061 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4062 yes = compositor->capabilities & capability_strings[i].bit;
4063 weston_log_continue(STAMP_SPACE "%s %s\n",
4064 capability_strings[i].desc,
4065 yes ? "yes" : "no");
4066 }
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04004067
4068 weston_log_continue(STAMP_SPACE "presentation clock: %s, id %d\n",
4069 clock_name(compositor->presentation_clock),
4070 compositor->presentation_clock);
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004071}
4072
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004073static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004074{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004075 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004076
Martin Minarik6d118362012-06-07 18:01:59 +02004077 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004078 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004079
4080 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004081}
4082
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004083#ifdef HAVE_LIBUNWIND
4084
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004085static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004086print_backtrace(void)
4087{
4088 unw_cursor_t cursor;
4089 unw_context_t context;
4090 unw_word_t off;
4091 unw_proc_info_t pip;
4092 int ret, i = 0;
4093 char procname[256];
4094 const char *filename;
4095 Dl_info dlinfo;
4096
4097 pip.unwind_info = NULL;
4098 ret = unw_getcontext(&context);
4099 if (ret) {
4100 weston_log("unw_getcontext: %d\n", ret);
4101 return;
4102 }
4103
4104 ret = unw_init_local(&cursor, &context);
4105 if (ret) {
4106 weston_log("unw_init_local: %d\n", ret);
4107 return;
4108 }
4109
4110 ret = unw_step(&cursor);
4111 while (ret > 0) {
4112 ret = unw_get_proc_info(&cursor, &pip);
4113 if (ret) {
4114 weston_log("unw_get_proc_info: %d\n", ret);
4115 break;
4116 }
4117
4118 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4119 if (ret && ret != -UNW_ENOMEM) {
4120 if (ret != -UNW_EUNSPEC)
4121 weston_log("unw_get_proc_name: %d\n", ret);
4122 procname[0] = '?';
4123 procname[1] = 0;
4124 }
4125
4126 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4127 *dlinfo.dli_fname)
4128 filename = dlinfo.dli_fname;
4129 else
4130 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004131
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004132 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4133 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4134
4135 ret = unw_step(&cursor);
4136 if (ret < 0)
4137 weston_log("unw_step: %d\n", ret);
4138 }
4139}
4140
4141#else
4142
4143static void
4144print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004145{
4146 void *buffer[32];
4147 int i, count;
4148 Dl_info info;
4149
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004150 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4151 for (i = 0; i < count; i++) {
4152 dladdr(buffer[i], &info);
4153 weston_log(" [%016lx] %s (%s)\n",
4154 (long) buffer[i],
4155 info.dli_sname ? info.dli_sname : "--",
4156 info.dli_fname);
4157 }
4158}
4159
4160#endif
4161
4162static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004163on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004164{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004165 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004166 * then call the backend restore function, which will switch
4167 * back to the vt we launched from or ungrab X etc and then
4168 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004169 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004170 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004171 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004172
Peter Maatmane5b42e42013-03-27 22:38:53 +01004173 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004174
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004175 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004176
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004177 segv_compositor->restore(segv_compositor);
4178
4179 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004180}
4181
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004182WL_EXPORT void *
4183weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004184{
4185 char path[PATH_MAX];
4186 void *module, *init;
4187
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004188 if (name == NULL)
4189 return NULL;
4190
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004191 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004192 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004193 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004194 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004195
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004196 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4197 if (module) {
4198 weston_log("Module '%s' already loaded\n", path);
4199 dlclose(module);
4200 return NULL;
4201 }
4202
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004203 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004204 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004205 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004206 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004207 return NULL;
4208 }
4209
4210 init = dlsym(module, entrypoint);
4211 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004212 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004213 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004214 return NULL;
4215 }
4216
4217 return init;
4218}
4219
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004220static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004221load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004222 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004223{
4224 const char *p, *end;
4225 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004226 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004227 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004228
4229 if (modules == NULL)
4230 return 0;
4231
4232 p = modules;
4233 while (*p) {
4234 end = strchrnul(p, ',');
4235 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004236 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004237 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004238 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004239 p = end;
4240 while (*p == ',')
4241 p++;
4242
4243 }
4244
4245 return 0;
4246}
4247
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004248static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004249 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4250
4251static const char xdg_wrong_message[] =
4252 "fatal: environment variable XDG_RUNTIME_DIR\n"
4253 "is set to \"%s\", which is not a directory.\n";
4254
4255static const char xdg_wrong_mode_message[] =
4256 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004257 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4258 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004259
4260static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004261 "Refer to your distribution on how to get it, or\n"
4262 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4263 "on how to implement it.\n";
4264
Martin Minarik37032f82012-06-18 20:15:18 +02004265static void
4266verify_xdg_runtime_dir(void)
4267{
4268 char *dir = getenv("XDG_RUNTIME_DIR");
4269 struct stat s;
4270
4271 if (!dir) {
4272 weston_log(xdg_error_message);
4273 weston_log_continue(xdg_detail_message);
4274 exit(EXIT_FAILURE);
4275 }
4276
4277 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4278 weston_log(xdg_wrong_message, dir);
4279 weston_log_continue(xdg_detail_message);
4280 exit(EXIT_FAILURE);
4281 }
4282
4283 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4284 weston_log(xdg_wrong_mode_message,
4285 dir, s.st_mode & 0777, s.st_uid);
4286 weston_log_continue(xdg_detail_message);
4287 }
4288}
4289
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004290static int
4291usage(int error_code)
4292{
4293 fprintf(stderr,
4294 "Usage: weston [OPTIONS]\n\n"
4295 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4296 "Weston supports multiple backends, and depending on which backend is in use\n"
4297 "different options will be accepted.\n\n"
4298
4299
4300 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004301 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004302 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004303 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4304 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004305 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004306 " -S, --socket=NAME\tName of socket to listen on\n"
4307 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004308 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004309 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004310 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004311 " -h, --help\t\tThis help message\n\n");
4312
4313 fprintf(stderr,
4314 "Options for drm-backend.so:\n\n"
4315 " --connector=ID\tBring up only this connector\n"
4316 " --seat=SEAT\t\tThe seat that weston should run on\n"
4317 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004318 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004319 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4320
4321 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004322 "Options for fbdev-backend.so:\n\n"
4323 " --tty=TTY\t\tThe tty to use\n"
4324 " --device=DEVICE\tThe framebuffer device to use\n\n");
4325
4326 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004327 "Options for x11-backend.so:\n\n"
4328 " --width=WIDTH\t\tWidth of X window\n"
4329 " --height=HEIGHT\tHeight of X window\n"
4330 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004331 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004332 " --output-count=COUNT\tCreate multiple outputs\n"
4333 " --no-input\t\tDont create input devices\n\n");
4334
4335 fprintf(stderr,
4336 "Options for wayland-backend.so:\n\n"
4337 " --width=WIDTH\t\tWidth of Wayland surface\n"
4338 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004339 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004340 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004341 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004342 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004343 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004344 " --display=DISPLAY\tWayland display to connect to\n\n");
4345
Pekka Paalanene31e0532013-05-22 18:03:07 +03004346#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4347 fprintf(stderr,
4348 "Options for rpi-backend.so:\n\n"
4349 " --tty=TTY\t\tThe tty to use\n"
4350 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4351 " --transform=TR\tThe output transformation, TR is one of:\n"
4352 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004353 " --opaque-regions\tEnable support for opaque regions, can be "
4354 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004355 "\n");
4356#endif
4357
Hardeningc077a842013-07-08 00:51:35 +02004358#if defined(BUILD_RDP_COMPOSITOR)
4359 fprintf(stderr,
4360 "Options for rdp-backend.so:\n\n"
4361 " --width=WIDTH\t\tWidth of desktop\n"
4362 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004363 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4364 " --address=ADDR\tThe address to bind\n"
4365 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004366 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004367 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4368 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4369 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4370 "\n");
4371#endif
4372
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004373 exit(error_code);
4374}
4375
Peter Maatmane5b42e42013-03-27 22:38:53 +01004376static void
4377catch_signals(void)
4378{
4379 struct sigaction action;
4380
4381 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4382 action.sa_sigaction = on_caught_signal;
4383 sigemptyset(&action.sa_mask);
4384 sigaction(SIGSEGV, &action, NULL);
4385 sigaction(SIGABRT, &action, NULL);
4386}
4387
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004388static void
4389handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4390{
4391 struct wl_client *client = data;
4392
4393 weston_log("Primary client died. Closing...\n");
4394
4395 wl_display_terminate(wl_client_get_display(client));
4396}
4397
Ryo Munakatad8deff62014-09-06 07:32:05 +09004398static char *
4399weston_choose_default_backend(void)
4400{
4401 char *backend = NULL;
4402
4403 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4404 backend = strdup("wayland-backend.so");
4405 else if (getenv("DISPLAY"))
4406 backend = strdup("x11-backend.so");
4407 else
4408 backend = strdup(WESTON_NATIVE_BACKEND);
4409
4410 return backend;
4411}
4412
4413static int
4414weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4415{
4416 if (socket_name) {
4417 if (wl_display_add_socket(display, socket_name)) {
4418 weston_log("fatal: failed to add socket: %m\n");
4419 return -1;
4420 }
4421 } else {
4422 socket_name = wl_display_add_socket_auto(display);
4423 if (!socket_name) {
4424 weston_log("fatal: failed to add socket: %m\n");
4425 return -1;
4426 }
4427 }
4428
4429 setenv("WAYLAND_DISPLAY", socket_name, 1);
4430
4431 return 0;
4432}
4433
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004434int main(int argc, char *argv[])
4435{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004436 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004437 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004438 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004439 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004440 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004441 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004442 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004443 int *argc, char *argv[],
4444 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004445 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004446 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004447 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004448 char *modules = NULL;
4449 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004450 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004451 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004452 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004453 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004454 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004455 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004456 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004457 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004458 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004459 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004460 struct wl_client *primary_client;
4461 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004462 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004463
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004464 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004465 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4466 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004467 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4468 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004469 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004470 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004471 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004472 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004473 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004474 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004475
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004476 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004477
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004478 if (help)
4479 usage(EXIT_SUCCESS);
4480
Scott Moreau12245142012-08-29 15:15:58 -06004481 if (version) {
4482 printf(PACKAGE_STRING "\n");
4483 return EXIT_SUCCESS;
4484 }
4485
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004486 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004487
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004488 weston_log("%s\n"
4489 STAMP_SPACE "%s\n"
4490 STAMP_SPACE "Bug reports to: %s\n"
4491 STAMP_SPACE "Build: %s\n",
4492 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004493 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004494 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004495
Martin Minarik37032f82012-06-18 20:15:18 +02004496 verify_xdg_runtime_dir();
4497
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004498 display = wl_display_create();
4499
Tiago Vignatti2116b892011-08-08 05:52:59 -07004500 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004501 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4502 display);
4503 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4504 display);
4505 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4506 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004507
4508 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004509 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4510 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004511
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304512 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4513 ret = EXIT_FAILURE;
4514 goto out_signals;
4515 }
4516
Pekka Paalanen588bee12014-05-07 16:26:25 +03004517 if (noconfig == 0)
4518 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004519 if (config != NULL) {
4520 weston_log("Using config file '%s'\n",
4521 weston_config_get_full_path(config));
4522 } else {
4523 weston_log("Starting with no config file.\n");
4524 }
4525 section = weston_config_get_section(config, "core", NULL, NULL);
4526
Ryo Munakata03faed22014-09-06 07:32:51 +09004527 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004528 weston_config_section_get_string(section, "backend", &backend,
4529 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004530 if (!backend)
4531 backend = weston_choose_default_backend();
4532 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004533
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004534 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304535 if (!backend_init) {
4536 ret = EXIT_FAILURE;
4537 goto out_signals;
4538 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004539
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004540 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004541 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004542 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304543 ret = EXIT_FAILURE;
4544 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004545 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004546
Peter Maatmane5b42e42013-03-27 22:38:53 +01004547 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004548 segv_compositor = ec;
4549
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004550 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004551 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004552
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004553 for (i = 1; i < argc; i++)
4554 weston_log("fatal: unhandled option: %s\n", argv[i]);
4555 if (argc > 1) {
4556 ret = EXIT_FAILURE;
4557 goto out;
4558 }
4559
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004560 weston_compositor_log_capabilities(ec);
4561
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004562 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4563 if (server_socket) {
4564 weston_log("Running with single client\n");
4565 fd = strtol(server_socket, &end, 0);
4566 if (*end != '\0')
4567 fd = -1;
4568 } else {
4569 fd = -1;
4570 }
4571
4572 if (fd != -1) {
4573 primary_client = wl_client_create(display, fd);
4574 if (!primary_client) {
4575 weston_log("fatal: failed to add client: %m\n");
4576 ret = EXIT_FAILURE;
4577 goto out;
4578 }
4579 primary_client_destroyed.notify =
4580 handle_primary_client_destroyed;
4581 wl_client_add_destroy_listener(primary_client,
4582 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004583 } else if (weston_create_listening_socket(display, socket_name)) {
4584 ret = EXIT_FAILURE;
4585 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004586 }
4587
Ryo Munakata03faed22014-09-06 07:32:51 +09004588 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004589 weston_config_section_get_string(section, "shell", &shell,
4590 "desktop-shell.so");
4591
Ryo Munakata03faed22014-09-06 07:32:51 +09004592 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004593 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004594
4595 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004596 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004597 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004598
4599 if (load_modules(ec, option_modules, &argc, argv) < 0)
4600 goto out;
4601
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004602 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4603 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4604 if (numlock_on) {
4605 wl_list_for_each(seat, &ec->seat_list, link) {
4606 if (seat->keyboard)
4607 weston_keyboard_set_locks(seat->keyboard,
4608 WESTON_NUM_LOCK,
4609 WESTON_NUM_LOCK);
4610 }
4611 }
4612
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004613 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004614
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004615 wl_display_run(display);
4616
Ryo Munakata03faed22014-09-06 07:32:51 +09004617out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004618 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004619 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004620
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004621 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004622
Daniel Stone855028f2012-05-01 20:37:10 +01004623 weston_compositor_xkb_destroy(ec);
4624
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004625 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304626
4627out_signals:
4628 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4629 if (signals[i])
4630 wl_event_source_remove(signals[i]);
4631
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004632 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004633
Martin Minarik19e6f262012-06-07 13:08:46 +02004634 weston_log_file_close();
4635
Ryo Munakata03faed22014-09-06 07:32:51 +09004636 free(backend);
4637 free(shell);
4638 free(socket_name);
4639 free(option_modules);
4640 free(log);
4641 free(modules);
4642
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004643 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004644}