blob: d7895084906542c48ed555badee2c385fda394fc [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
Rob Bradford0fb79752012-08-02 15:36:57 +01001899weston_output_repaint(struct weston_output *output, uint32_t msecs)
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) {
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05001954 wl_callback_send_done(cb->resource, msecs);
1955 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++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001960 animation->frame(animation, output, msecs);
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
Rob Bradford0fb79752012-08-02 15:36:57 +01001977weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001978{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001979 struct weston_compositor *compositor = output->compositor;
1980 struct wl_event_loop *loop =
1981 wl_display_get_event_loop(compositor->wl_display);
David Herrmann1edf44c2013-10-22 17:11:26 +02001982 int fd, r;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001983
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001984 output->frame_time = msecs;
Kristian Høgsberg991810c2013-10-16 11:10:12 -07001985
1986 if (output->repaint_needed &&
1987 compositor->state != WESTON_COMPOSITOR_SLEEPING &&
1988 compositor->state != WESTON_COMPOSITOR_OFFSCREEN) {
David Herrmann1edf44c2013-10-22 17:11:26 +02001989 r = weston_output_repaint(output, msecs);
1990 if (!r)
1991 return;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001992 }
1993
1994 output->repaint_scheduled = 0;
1995 if (compositor->input_loop_source)
1996 return;
1997
1998 fd = wl_event_loop_get_fd(compositor->input_loop);
1999 compositor->input_loop_source =
2000 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2001 weston_compositor_read_input, compositor);
2002}
2003
2004static void
2005idle_repaint(void *data)
2006{
2007 struct weston_output *output = data;
2008
Jonas Ådahle5a12252013-04-05 23:07:11 +02002009 output->start_repaint_loop(output);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002010}
2011
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002012WL_EXPORT void
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002013weston_layer_entry_insert(struct weston_layer_entry *list,
2014 struct weston_layer_entry *entry)
2015{
2016 wl_list_insert(&list->link, &entry->link);
2017 entry->layer = list->layer;
2018}
2019
2020WL_EXPORT void
2021weston_layer_entry_remove(struct weston_layer_entry *entry)
2022{
2023 wl_list_remove(&entry->link);
2024 wl_list_init(&entry->link);
2025 entry->layer = NULL;
2026}
2027
2028WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002029weston_layer_init(struct weston_layer *layer, struct wl_list *below)
2030{
Giulio Camuffo412e6a52014-07-09 22:12:56 +03002031 wl_list_init(&layer->view_list.link);
2032 layer->view_list.layer = layer;
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002033 weston_layer_set_mask_infinite(layer);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02002034 if (below != NULL)
2035 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05002036}
2037
2038WL_EXPORT void
Giulio Camuffo95ec0f92014-07-09 22:12:57 +03002039weston_layer_set_mask(struct weston_layer *layer,
2040 int x, int y, int width, int height)
2041{
2042 struct weston_view *view;
2043
2044 layer->mask.x1 = x;
2045 layer->mask.x2 = x + width;
2046 layer->mask.y1 = y;
2047 layer->mask.y2 = y + height;
2048
2049 wl_list_for_each(view, &layer->view_list.link, layer_link.link) {
2050 weston_view_geometry_dirty(view);
2051 }
2052}
2053
2054WL_EXPORT void
2055weston_layer_set_mask_infinite(struct weston_layer *layer)
2056{
2057 weston_layer_set_mask(layer, INT32_MIN, INT32_MIN,
2058 UINT32_MAX, UINT32_MAX);
2059}
2060
2061WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002062weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002063{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002064 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04002065 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002066
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01002067 if (compositor->state == WESTON_COMPOSITOR_SLEEPING ||
2068 compositor->state == WESTON_COMPOSITOR_OFFSCREEN)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002069 return;
2070
Kristian Høgsbergef044142011-06-21 15:02:12 -04002071 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002072 output->repaint_needed = 1;
2073 if (output->repaint_scheduled)
2074 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01002075
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002076 wl_event_loop_add_idle(loop, idle_repaint, output);
2077 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05002078
2079 if (compositor->input_loop_source) {
2080 wl_event_source_remove(compositor->input_loop_source);
2081 compositor->input_loop_source = NULL;
2082 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04002083}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05002084
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002085WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04002086weston_compositor_schedule_repaint(struct weston_compositor *compositor)
2087{
2088 struct weston_output *output;
2089
2090 wl_list_for_each(output, &compositor->output_list, link)
2091 weston_output_schedule_repaint(output);
2092}
2093
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002094static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002095surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002096{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002097 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002098}
2099
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002100static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002101surface_attach(struct wl_client *client,
2102 struct wl_resource *resource,
2103 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
2104{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002105 struct weston_surface *surface = wl_resource_get_user_data(resource);
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002106 struct weston_buffer *buffer = NULL;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002107
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002108 if (buffer_resource) {
Jason Ekstrand6bd62942013-06-20 20:38:23 -05002109 buffer = weston_buffer_from_resource(buffer_resource);
Kristian Høgsbergab19f932013-08-20 11:30:54 -07002110 if (buffer == NULL) {
2111 wl_client_post_no_memory(client);
2112 return;
2113 }
Kristian Høgsberg08b58c72013-08-15 12:26:42 -07002114 }
Kristian Høgsberga691aee2011-06-23 21:43:50 -04002115
Pekka Paalanende685b82012-12-04 15:58:12 +02002116 /* Attach, attach, without commit in between does not send
2117 * wl_buffer.release. */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002118 weston_surface_state_set_buffer(&surface->pending, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002119
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002120 surface->pending.sx = sx;
2121 surface->pending.sy = sy;
Giulio Camuffo184df502013-02-21 11:29:21 +01002122 surface->pending.newly_attached = 1;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002123}
2124
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05002125static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002126surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002127 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002128 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05002129{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002130 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04002131
Pekka Paalanen8e159182012-10-10 12:49:25 +03002132 pixman_region32_union_rect(&surface->pending.damage,
2133 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04002134 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002135}
2136
Kristian Høgsberg33418202011-08-16 23:01:28 -04002137static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002138destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002139{
Jason Ekstrandfbbbec82013-06-14 10:07:57 -05002140 struct weston_frame_callback *cb = wl_resource_get_user_data(resource);
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002141
2142 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02002143 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002144}
2145
2146static void
2147surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002148 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04002149{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002150 struct weston_frame_callback *cb;
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002151 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002152
2153 cb = malloc(sizeof *cb);
2154 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002155 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002156 return;
2157 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03002158
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002159 cb->resource = wl_resource_create(client, &wl_callback_interface, 1,
2160 callback);
2161 if (cb->resource == NULL) {
2162 free(cb);
2163 wl_resource_post_no_memory(resource);
2164 return;
2165 }
2166
Jason Ekstranda85118c2013-06-27 20:17:02 -05002167 wl_resource_set_implementation(cb->resource, NULL, cb,
2168 destroy_frame_callback);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002169
Pekka Paalanenbc106382012-10-10 12:49:31 +03002170 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04002171}
2172
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002173static void
2174surface_set_opaque_region(struct wl_client *client,
2175 struct wl_resource *resource,
2176 struct wl_resource *region_resource)
2177{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002178 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002179 struct weston_region *region;
2180
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002181 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002182 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002183 pixman_region32_copy(&surface->pending.opaque,
2184 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002185 } else {
Jason Ekstrandef540082014-06-26 10:37:36 -07002186 pixman_region32_clear(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002187 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002188}
2189
2190static void
2191surface_set_input_region(struct wl_client *client,
2192 struct wl_resource *resource,
2193 struct wl_resource *region_resource)
2194{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002195 struct weston_surface *surface = wl_resource_get_user_data(resource);
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05002196 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002197
2198 if (region_resource) {
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002199 region = wl_resource_get_user_data(region_resource);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002200 pixman_region32_copy(&surface->pending.input,
2201 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002202 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002203 pixman_region32_fini(&surface->pending.input);
2204 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002205 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002206}
2207
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002208static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002209weston_surface_commit_subsurface_order(struct weston_surface *surface)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002210{
Pekka Paalanene67858b2013-04-25 13:57:42 +03002211 struct weston_subsurface *sub;
2212
2213 wl_list_for_each_reverse(sub, &surface->subsurface_list_pending,
2214 parent_link_pending) {
2215 wl_list_remove(&sub->parent_link);
2216 wl_list_insert(&surface->subsurface_list, &sub->parent_link);
2217 }
2218}
2219
2220static void
Jason Ekstrand7b982072014-05-20 14:33:03 -05002221weston_surface_commit_state(struct weston_surface *surface,
2222 struct weston_surface_state *state)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002223{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002224 struct weston_view *view;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002225 pixman_region32_t opaque;
2226
Alexander Larsson4ea95522013-05-22 14:41:37 +02002227 /* wl_surface.set_buffer_transform */
Alexander Larsson4ea95522013-05-22 14:41:37 +02002228 /* wl_surface.set_buffer_scale */
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02002229 /* wl_viewport.set */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002230 surface->buffer_viewport = state->buffer_viewport;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002231
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002232 /* wl_surface.attach */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002233 if (state->newly_attached)
2234 weston_surface_attach(surface, state->buffer);
2235 weston_surface_state_set_buffer(state, NULL);
Giulio Camuffo184df502013-02-21 11:29:21 +01002236
Jason Ekstrand7b982072014-05-20 14:33:03 -05002237 if (state->newly_attached || state->buffer_viewport.changed) {
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002238 weston_surface_update_size(surface);
2239 if (surface->configure)
Jason Ekstrand7b982072014-05-20 14:33:03 -05002240 surface->configure(surface, state->sx, state->sy);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002241 }
Giulio Camuffo184df502013-02-21 11:29:21 +01002242
Jason Ekstrand7b982072014-05-20 14:33:03 -05002243 state->sx = 0;
2244 state->sy = 0;
2245 state->newly_attached = 0;
2246 state->buffer_viewport.changed = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03002247
2248 /* wl_surface.damage */
2249 pixman_region32_union(&surface->damage, &surface->damage,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002250 &state->damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05002251 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
Jason Ekstrandef540082014-06-26 10:37:36 -07002252 0, 0, surface->width, surface->height);
Jason Ekstrandf83a0d42014-09-06 09:01:28 -07002253 pixman_region32_clear(&state->damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03002254
2255 /* wl_surface.set_opaque_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002256 pixman_region32_init(&opaque);
2257 pixman_region32_intersect_rect(&opaque, &state->opaque,
2258 0, 0, surface->width, surface->height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002259
2260 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
2261 pixman_region32_copy(&surface->opaque, &opaque);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002262 wl_list_for_each(view, &surface->views, surface_link)
2263 weston_view_geometry_dirty(view);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02002264 }
2265
2266 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002267
2268 /* wl_surface.set_input_region */
Jason Ekstrand7b982072014-05-20 14:33:03 -05002269 pixman_region32_intersect_rect(&surface->input, &state->input,
2270 0, 0, surface->width, surface->height);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002271
Pekka Paalanenbc106382012-10-10 12:49:31 +03002272 /* wl_surface.frame */
2273 wl_list_insert_list(&surface->frame_callback_list,
Jason Ekstrand7b982072014-05-20 14:33:03 -05002274 &state->frame_callback_list);
2275 wl_list_init(&state->frame_callback_list);
2276}
2277
2278static void
2279weston_surface_commit(struct weston_surface *surface)
2280{
2281 weston_surface_commit_state(surface, &surface->pending);
Pekka Paalanenbc106382012-10-10 12:49:31 +03002282
Pekka Paalanene67858b2013-04-25 13:57:42 +03002283 weston_surface_commit_subsurface_order(surface);
2284
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002285 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002286}
2287
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002288static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002289weston_subsurface_commit(struct weston_subsurface *sub);
2290
2291static void
2292weston_subsurface_parent_commit(struct weston_subsurface *sub,
2293 int parent_is_synchronized);
2294
2295static void
2296surface_commit(struct wl_client *client, struct wl_resource *resource)
2297{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002298 struct weston_surface *surface = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002299 struct weston_subsurface *sub = weston_surface_to_subsurface(surface);
2300
2301 if (sub) {
2302 weston_subsurface_commit(sub);
2303 return;
2304 }
2305
2306 weston_surface_commit(surface);
2307
2308 wl_list_for_each(sub, &surface->subsurface_list, parent_link) {
2309 if (sub->surface != surface)
2310 weston_subsurface_parent_commit(sub, 0);
2311 }
2312}
2313
2314static void
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002315surface_set_buffer_transform(struct wl_client *client,
2316 struct wl_resource *resource, int transform)
2317{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002318 struct weston_surface *surface = wl_resource_get_user_data(resource);
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002319
Jonny Lamba55f1392014-05-30 12:07:15 +02002320 /* if wl_output.transform grows more members this will need to be updated. */
2321 if (transform < 0 ||
2322 transform > WL_OUTPUT_TRANSFORM_FLIPPED_270) {
2323 wl_resource_post_error(resource,
2324 WL_SURFACE_ERROR_INVALID_TRANSFORM,
2325 "buffer transform must be a valid transform "
2326 "('%d' specified)", transform);
2327 return;
2328 }
2329
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002330 surface->pending.buffer_viewport.buffer.transform = transform;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002331 surface->pending.buffer_viewport.changed = 1;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002332}
2333
Alexander Larsson4ea95522013-05-22 14:41:37 +02002334static void
2335surface_set_buffer_scale(struct wl_client *client,
2336 struct wl_resource *resource,
Alexander Larssonedddbd12013-05-24 13:09:43 +02002337 int32_t scale)
Alexander Larsson4ea95522013-05-22 14:41:37 +02002338{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002339 struct weston_surface *surface = wl_resource_get_user_data(resource);
Alexander Larsson4ea95522013-05-22 14:41:37 +02002340
Jonny Lamba55f1392014-05-30 12:07:15 +02002341 if (scale < 1) {
2342 wl_resource_post_error(resource,
2343 WL_SURFACE_ERROR_INVALID_SCALE,
2344 "buffer scale must be at least one "
2345 "('%d' specified)", scale);
2346 return;
2347 }
2348
Pekka Paalanen952b6c82014-03-14 14:38:15 +02002349 surface->pending.buffer_viewport.buffer.scale = scale;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002350 surface->pending.buffer_viewport.changed = 1;
Alexander Larsson4ea95522013-05-22 14:41:37 +02002351}
2352
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002353static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002354 surface_destroy,
2355 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04002356 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002357 surface_frame,
2358 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03002359 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02002360 surface_commit,
Alexander Larsson4ea95522013-05-22 14:41:37 +02002361 surface_set_buffer_transform,
2362 surface_set_buffer_scale
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002363};
2364
2365static void
2366compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04002367 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002368{
Kristian Høgsbergc2d70422013-06-25 15:34:33 -04002369 struct weston_compositor *ec = wl_resource_get_user_data(resource);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002370 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002371
Kristian Høgsberg18c93002012-01-27 11:58:31 -05002372 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002373 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04002374 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002375 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002376 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002377
Jason Ekstranda85118c2013-06-27 20:17:02 -05002378 surface->resource =
2379 wl_resource_create(client, &wl_surface_interface,
2380 wl_resource_get_version(resource), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002381 if (surface->resource == NULL) {
2382 weston_surface_destroy(surface);
2383 wl_resource_post_no_memory(resource);
2384 return;
2385 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002386 wl_resource_set_implementation(surface->resource, &surface_interface,
2387 surface, destroy_surface);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07002388
2389 wl_signal_emit(&ec->create_surface_signal, surface);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002390}
2391
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002392static void
2393destroy_region(struct wl_resource *resource)
2394{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002395 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002396
2397 pixman_region32_fini(&region->region);
2398 free(region);
2399}
2400
2401static void
2402region_destroy(struct wl_client *client, struct wl_resource *resource)
2403{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002404 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002405}
2406
2407static void
2408region_add(struct wl_client *client, struct wl_resource *resource,
2409 int32_t x, int32_t y, int32_t width, int32_t height)
2410{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002411 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002412
2413 pixman_region32_union_rect(&region->region, &region->region,
2414 x, y, width, height);
2415}
2416
2417static void
2418region_subtract(struct wl_client *client, struct wl_resource *resource,
2419 int32_t x, int32_t y, int32_t width, int32_t height)
2420{
Jason Ekstrand8895efc2013-06-14 10:07:56 -05002421 struct weston_region *region = wl_resource_get_user_data(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002422 pixman_region32_t rect;
2423
2424 pixman_region32_init_rect(&rect, x, y, width, height);
2425 pixman_region32_subtract(&region->region, &region->region, &rect);
2426 pixman_region32_fini(&rect);
2427}
2428
2429static const struct wl_region_interface region_interface = {
2430 region_destroy,
2431 region_add,
2432 region_subtract
2433};
2434
2435static void
2436compositor_create_region(struct wl_client *client,
2437 struct wl_resource *resource, uint32_t id)
2438{
2439 struct weston_region *region;
2440
2441 region = malloc(sizeof *region);
2442 if (region == NULL) {
2443 wl_resource_post_no_memory(resource);
2444 return;
2445 }
2446
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002447 pixman_region32_init(&region->region);
2448
Jason Ekstranda85118c2013-06-27 20:17:02 -05002449 region->resource =
2450 wl_resource_create(client, &wl_region_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07002451 if (region->resource == NULL) {
2452 free(region);
2453 wl_resource_post_no_memory(resource);
2454 return;
2455 }
Jason Ekstranda85118c2013-06-27 20:17:02 -05002456 wl_resource_set_implementation(region->resource, &region_interface,
2457 region, destroy_region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002458}
2459
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002460static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002461 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002462 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002463};
2464
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002465static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002466weston_subsurface_commit_from_cache(struct weston_subsurface *sub)
2467{
2468 struct weston_surface *surface = sub->surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002469
Jason Ekstrand7b982072014-05-20 14:33:03 -05002470 weston_surface_commit_state(surface, &sub->cached);
2471 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002472
2473 weston_surface_commit_subsurface_order(surface);
2474
2475 weston_surface_schedule_repaint(surface);
2476
Jason Ekstrand7b982072014-05-20 14:33:03 -05002477 sub->has_cached_data = 0;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002478}
2479
2480static void
2481weston_subsurface_commit_to_cache(struct weston_subsurface *sub)
2482{
2483 struct weston_surface *surface = sub->surface;
2484
2485 /*
2486 * If this commit would cause the surface to move by the
2487 * attach(dx, dy) parameters, the old damage region must be
2488 * translated to correspond to the new surface coordinate system
Hardening57388e42013-09-18 23:56:36 +02002489 * original_mode.
Pekka Paalanene67858b2013-04-25 13:57:42 +03002490 */
2491 pixman_region32_translate(&sub->cached.damage,
2492 -surface->pending.sx, -surface->pending.sy);
2493 pixman_region32_union(&sub->cached.damage, &sub->cached.damage,
2494 &surface->pending.damage);
Jason Ekstrandef540082014-06-26 10:37:36 -07002495 pixman_region32_clear(&surface->pending.damage);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002496
2497 if (surface->pending.newly_attached) {
2498 sub->cached.newly_attached = 1;
Jason Ekstrand7b982072014-05-20 14:33:03 -05002499 weston_surface_state_set_buffer(&sub->cached,
2500 surface->pending.buffer);
2501 weston_buffer_reference(&sub->cached_buffer_ref,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002502 surface->pending.buffer);
2503 }
2504 sub->cached.sx += surface->pending.sx;
2505 sub->cached.sy += surface->pending.sy;
Pekka Paalanen260ba382014-03-14 14:38:12 +02002506
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002507 sub->cached.buffer_viewport.changed |=
2508 surface->pending.buffer_viewport.changed;
2509 sub->cached.buffer_viewport.buffer =
2510 surface->pending.buffer_viewport.buffer;
2511 sub->cached.buffer_viewport.surface =
2512 surface->pending.buffer_viewport.surface;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002513
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02002514 weston_surface_reset_pending_buffer(surface);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002515
2516 pixman_region32_copy(&sub->cached.opaque, &surface->pending.opaque);
2517
2518 pixman_region32_copy(&sub->cached.input, &surface->pending.input);
2519
2520 wl_list_insert_list(&sub->cached.frame_callback_list,
2521 &surface->pending.frame_callback_list);
2522 wl_list_init(&surface->pending.frame_callback_list);
2523
Jason Ekstrand7b982072014-05-20 14:33:03 -05002524 sub->has_cached_data = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002525}
2526
2527static int
2528weston_subsurface_is_synchronized(struct weston_subsurface *sub)
2529{
2530 while (sub) {
2531 if (sub->synchronized)
2532 return 1;
2533
2534 if (!sub->parent)
2535 return 0;
2536
2537 sub = weston_surface_to_subsurface(sub->parent);
2538 }
2539
2540 return 0;
2541}
2542
2543static void
2544weston_subsurface_commit(struct weston_subsurface *sub)
2545{
2546 struct weston_surface *surface = sub->surface;
2547 struct weston_subsurface *tmp;
2548
2549 /* Recursive check for effectively synchronized. */
2550 if (weston_subsurface_is_synchronized(sub)) {
2551 weston_subsurface_commit_to_cache(sub);
2552 } else {
Jason Ekstrand7b982072014-05-20 14:33:03 -05002553 if (sub->has_cached_data) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002554 /* flush accumulated state from cache */
2555 weston_subsurface_commit_to_cache(sub);
2556 weston_subsurface_commit_from_cache(sub);
2557 } else {
2558 weston_surface_commit(surface);
2559 }
2560
2561 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2562 if (tmp->surface != surface)
2563 weston_subsurface_parent_commit(tmp, 0);
2564 }
2565 }
2566}
2567
2568static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002569weston_subsurface_synchronized_commit(struct weston_subsurface *sub)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002570{
2571 struct weston_surface *surface = sub->surface;
2572 struct weston_subsurface *tmp;
2573
Pekka Paalanene67858b2013-04-25 13:57:42 +03002574 /* From now on, commit_from_cache the whole sub-tree, regardless of
2575 * the synchronized mode of each child. This sub-surface or some
2576 * of its ancestors were synchronized, so we are synchronized
2577 * all the way down.
2578 */
2579
Jason Ekstrand7b982072014-05-20 14:33:03 -05002580 if (sub->has_cached_data)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002581 weston_subsurface_commit_from_cache(sub);
2582
2583 wl_list_for_each(tmp, &surface->subsurface_list, parent_link) {
2584 if (tmp->surface != surface)
2585 weston_subsurface_parent_commit(tmp, 1);
2586 }
2587}
2588
2589static void
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002590weston_subsurface_parent_commit(struct weston_subsurface *sub,
2591 int parent_is_synchronized)
2592{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002593 struct weston_view *view;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002594 if (sub->position.set) {
Jason Ekstranda7af7042013-10-12 22:38:11 -05002595 wl_list_for_each(view, &sub->surface->views, surface_link)
2596 weston_view_set_position(view,
2597 sub->position.x,
2598 sub->position.y);
2599
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002600 sub->position.set = 0;
2601 }
2602
2603 if (parent_is_synchronized || sub->synchronized)
2604 weston_subsurface_synchronized_commit(sub);
2605}
2606
2607static void
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002608subsurface_configure(struct weston_surface *surface, int32_t dx, int32_t dy)
Pekka Paalanene67858b2013-04-25 13:57:42 +03002609{
2610 struct weston_compositor *compositor = surface->compositor;
Jason Ekstranda7af7042013-10-12 22:38:11 -05002611 struct weston_view *view;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002612
Jason Ekstranda7af7042013-10-12 22:38:11 -05002613 wl_list_for_each(view, &surface->views, surface_link)
Jason Ekstrand918f2dd2013-12-02 21:01:53 -06002614 weston_view_set_position(view,
2615 view->geometry.x + dx,
2616 view->geometry.y + dy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002617
2618 /* No need to check parent mappedness, because if parent is not
2619 * mapped, parent is not in a visible layer, so this sub-surface
2620 * will not be drawn either.
2621 */
2622 if (!weston_surface_is_mapped(surface)) {
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002623 struct weston_output *output;
2624
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002625 /* Cannot call weston_view_update_transform(),
Pekka Paalanene67858b2013-04-25 13:57:42 +03002626 * because that would call it also for the parent surface,
2627 * which might not be mapped yet. That would lead to
2628 * inconsistent state, where the window could never be
2629 * mapped.
2630 *
Derek Foreman4b1a0a12014-09-10 15:37:33 -05002631 * Instead just assign any output, to make
Pekka Paalanene67858b2013-04-25 13:57:42 +03002632 * weston_surface_is_mapped() return true, so that when the
2633 * parent surface does get mapped, this one will get
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002634 * included, too. See view_list_add().
Pekka Paalanene67858b2013-04-25 13:57:42 +03002635 */
2636 assert(!wl_list_empty(&compositor->output_list));
Pekka Paalaneneb3cf222014-06-30 11:52:07 +03002637 output = container_of(compositor->output_list.next,
2638 struct weston_output, link);
2639
2640 surface->output = output;
2641 weston_surface_update_output_mask(surface, 1 << output->id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002642 }
2643}
2644
2645static struct weston_subsurface *
2646weston_surface_to_subsurface(struct weston_surface *surface)
2647{
2648 if (surface->configure == subsurface_configure)
2649 return surface->configure_private;
2650
2651 return NULL;
2652}
2653
Pekka Paalanen01388e22013-04-25 13:57:44 +03002654WL_EXPORT struct weston_surface *
2655weston_surface_get_main_surface(struct weston_surface *surface)
2656{
2657 struct weston_subsurface *sub;
2658
2659 while (surface && (sub = weston_surface_to_subsurface(surface)))
2660 surface = sub->parent;
2661
2662 return surface;
2663}
2664
Pekka Paalanene67858b2013-04-25 13:57:42 +03002665static void
2666subsurface_set_position(struct wl_client *client,
2667 struct wl_resource *resource, int32_t x, int32_t y)
2668{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002669 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002670
2671 if (!sub)
2672 return;
2673
2674 sub->position.x = x;
2675 sub->position.y = y;
2676 sub->position.set = 1;
2677}
2678
2679static struct weston_subsurface *
2680subsurface_from_surface(struct weston_surface *surface)
2681{
2682 struct weston_subsurface *sub;
2683
2684 sub = weston_surface_to_subsurface(surface);
2685 if (sub)
2686 return sub;
2687
2688 wl_list_for_each(sub, &surface->subsurface_list, parent_link)
2689 if (sub->surface == surface)
2690 return sub;
2691
2692 return NULL;
2693}
2694
2695static struct weston_subsurface *
2696subsurface_sibling_check(struct weston_subsurface *sub,
2697 struct weston_surface *surface,
2698 const char *request)
2699{
2700 struct weston_subsurface *sibling;
2701
2702 sibling = subsurface_from_surface(surface);
2703
2704 if (!sibling) {
2705 wl_resource_post_error(sub->resource,
2706 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2707 "%s: wl_surface@%d is not a parent or sibling",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002708 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002709 return NULL;
2710 }
2711
2712 if (sibling->parent != sub->parent) {
2713 wl_resource_post_error(sub->resource,
2714 WL_SUBSURFACE_ERROR_BAD_SURFACE,
2715 "%s: wl_surface@%d has a different parent",
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002716 request, wl_resource_get_id(surface->resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002717 return NULL;
2718 }
2719
2720 return sibling;
2721}
2722
2723static void
2724subsurface_place_above(struct wl_client *client,
2725 struct wl_resource *resource,
2726 struct wl_resource *sibling_resource)
2727{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002728 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002729 struct weston_surface *surface =
2730 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002731 struct weston_subsurface *sibling;
2732
2733 if (!sub)
2734 return;
2735
2736 sibling = subsurface_sibling_check(sub, surface, "place_above");
2737 if (!sibling)
2738 return;
2739
2740 wl_list_remove(&sub->parent_link_pending);
2741 wl_list_insert(sibling->parent_link_pending.prev,
2742 &sub->parent_link_pending);
2743}
2744
2745static void
2746subsurface_place_below(struct wl_client *client,
2747 struct wl_resource *resource,
2748 struct wl_resource *sibling_resource)
2749{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002750 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002751 struct weston_surface *surface =
2752 wl_resource_get_user_data(sibling_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002753 struct weston_subsurface *sibling;
2754
2755 if (!sub)
2756 return;
2757
2758 sibling = subsurface_sibling_check(sub, surface, "place_below");
2759 if (!sibling)
2760 return;
2761
2762 wl_list_remove(&sub->parent_link_pending);
2763 wl_list_insert(&sibling->parent_link_pending,
2764 &sub->parent_link_pending);
2765}
2766
2767static void
2768subsurface_set_sync(struct wl_client *client, struct wl_resource *resource)
2769{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002770 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002771
2772 if (sub)
2773 sub->synchronized = 1;
2774}
2775
2776static void
2777subsurface_set_desync(struct wl_client *client, struct wl_resource *resource)
2778{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002779 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002780
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002781 if (sub && sub->synchronized) {
Pekka Paalanene67858b2013-04-25 13:57:42 +03002782 sub->synchronized = 0;
Pekka Paalanen16abf6a2013-05-17 16:46:05 +03002783
2784 /* If sub became effectively desynchronized, flush. */
2785 if (!weston_subsurface_is_synchronized(sub))
2786 weston_subsurface_synchronized_commit(sub);
2787 }
Pekka Paalanene67858b2013-04-25 13:57:42 +03002788}
2789
2790static void
Pekka Paalanene67858b2013-04-25 13:57:42 +03002791weston_subsurface_unlink_parent(struct weston_subsurface *sub)
2792{
2793 wl_list_remove(&sub->parent_link);
2794 wl_list_remove(&sub->parent_link_pending);
2795 wl_list_remove(&sub->parent_destroy_listener.link);
2796 sub->parent = NULL;
2797}
2798
2799static void
2800weston_subsurface_destroy(struct weston_subsurface *sub);
2801
2802static void
2803subsurface_handle_surface_destroy(struct wl_listener *listener, void *data)
2804{
2805 struct weston_subsurface *sub =
2806 container_of(listener, struct weston_subsurface,
2807 surface_destroy_listener);
2808 assert(data == &sub->surface->resource);
2809
2810 /* The protocol object (wl_resource) is left inert. */
2811 if (sub->resource)
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002812 wl_resource_set_user_data(sub->resource, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002813
2814 weston_subsurface_destroy(sub);
2815}
2816
2817static void
2818subsurface_handle_parent_destroy(struct wl_listener *listener, void *data)
2819{
2820 struct weston_subsurface *sub =
2821 container_of(listener, struct weston_subsurface,
2822 parent_destroy_listener);
2823 assert(data == &sub->parent->resource);
2824 assert(sub->surface != sub->parent);
2825
2826 if (weston_surface_is_mapped(sub->surface))
2827 weston_surface_unmap(sub->surface);
2828
2829 weston_subsurface_unlink_parent(sub);
2830}
2831
2832static void
2833subsurface_resource_destroy(struct wl_resource *resource)
2834{
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002835 struct weston_subsurface *sub = wl_resource_get_user_data(resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002836
2837 if (sub)
2838 weston_subsurface_destroy(sub);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002839}
2840
2841static void
2842subsurface_destroy(struct wl_client *client, struct wl_resource *resource)
2843{
2844 wl_resource_destroy(resource);
2845}
2846
2847static void
2848weston_subsurface_link_parent(struct weston_subsurface *sub,
2849 struct weston_surface *parent)
2850{
2851 sub->parent = parent;
2852 sub->parent_destroy_listener.notify = subsurface_handle_parent_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002853 wl_signal_add(&parent->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002854 &sub->parent_destroy_listener);
2855
2856 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2857 wl_list_insert(&parent->subsurface_list_pending,
2858 &sub->parent_link_pending);
2859}
2860
2861static void
2862weston_subsurface_link_surface(struct weston_subsurface *sub,
2863 struct weston_surface *surface)
2864{
2865 sub->surface = surface;
2866 sub->surface_destroy_listener.notify =
2867 subsurface_handle_surface_destroy;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002868 wl_signal_add(&surface->destroy_signal,
Pekka Paalanene67858b2013-04-25 13:57:42 +03002869 &sub->surface_destroy_listener);
2870}
2871
2872static void
2873weston_subsurface_destroy(struct weston_subsurface *sub)
2874{
Jason Ekstranda7af7042013-10-12 22:38:11 -05002875 struct weston_view *view, *next;
2876
Pekka Paalanene67858b2013-04-25 13:57:42 +03002877 assert(sub->surface);
2878
2879 if (sub->resource) {
2880 assert(weston_surface_to_subsurface(sub->surface) == sub);
2881 assert(sub->parent_destroy_listener.notify ==
2882 subsurface_handle_parent_destroy);
2883
George Kiagiadakised04d382014-06-13 18:10:26 +02002884 wl_list_for_each_safe(view, next, &sub->surface->views, surface_link) {
2885 weston_view_unmap(view);
Jason Ekstranda7af7042013-10-12 22:38:11 -05002886 weston_view_destroy(view);
George Kiagiadakised04d382014-06-13 18:10:26 +02002887 }
Jason Ekstranda7af7042013-10-12 22:38:11 -05002888
Pekka Paalanene67858b2013-04-25 13:57:42 +03002889 if (sub->parent)
2890 weston_subsurface_unlink_parent(sub);
2891
Jason Ekstrand7b982072014-05-20 14:33:03 -05002892 weston_surface_state_fini(&sub->cached);
2893 weston_buffer_reference(&sub->cached_buffer_ref, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002894
2895 sub->surface->configure = NULL;
2896 sub->surface->configure_private = NULL;
2897 } else {
2898 /* the dummy weston_subsurface for the parent itself */
2899 assert(sub->parent_destroy_listener.notify == NULL);
2900 wl_list_remove(&sub->parent_link);
2901 wl_list_remove(&sub->parent_link_pending);
2902 }
2903
2904 wl_list_remove(&sub->surface_destroy_listener.link);
2905 free(sub);
2906}
2907
2908static const struct wl_subsurface_interface subsurface_implementation = {
2909 subsurface_destroy,
2910 subsurface_set_position,
2911 subsurface_place_above,
2912 subsurface_place_below,
2913 subsurface_set_sync,
2914 subsurface_set_desync
2915};
2916
2917static struct weston_subsurface *
2918weston_subsurface_create(uint32_t id, struct weston_surface *surface,
2919 struct weston_surface *parent)
2920{
2921 struct weston_subsurface *sub;
Jason Ekstrand26ed73c2013-06-06 22:34:41 -05002922 struct wl_client *client = wl_resource_get_client(surface->resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002923
2924 sub = calloc(1, sizeof *sub);
2925 if (!sub)
2926 return NULL;
2927
Jason Ekstranda7af7042013-10-12 22:38:11 -05002928 wl_list_init(&sub->unused_views);
2929
Jason Ekstranda85118c2013-06-27 20:17:02 -05002930 sub->resource =
2931 wl_resource_create(client, &wl_subsurface_interface, 1, id);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002932 if (!sub->resource) {
2933 free(sub);
2934 return NULL;
2935 }
2936
Jason Ekstranda85118c2013-06-27 20:17:02 -05002937 wl_resource_set_implementation(sub->resource,
2938 &subsurface_implementation,
2939 sub, subsurface_resource_destroy);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002940 weston_subsurface_link_surface(sub, surface);
2941 weston_subsurface_link_parent(sub, parent);
Jason Ekstrand7b982072014-05-20 14:33:03 -05002942 weston_surface_state_init(&sub->cached);
2943 sub->cached_buffer_ref.buffer = NULL;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002944 sub->synchronized = 1;
Pekka Paalanene67858b2013-04-25 13:57:42 +03002945
2946 return sub;
2947}
2948
2949/* Create a dummy subsurface for having the parent itself in its
2950 * sub-surface lists. Makes stacking order manipulation easy.
2951 */
2952static struct weston_subsurface *
2953weston_subsurface_create_for_parent(struct weston_surface *parent)
2954{
2955 struct weston_subsurface *sub;
2956
2957 sub = calloc(1, sizeof *sub);
2958 if (!sub)
2959 return NULL;
2960
2961 weston_subsurface_link_surface(sub, parent);
2962 sub->parent = parent;
2963 wl_list_insert(&parent->subsurface_list, &sub->parent_link);
2964 wl_list_insert(&parent->subsurface_list_pending,
2965 &sub->parent_link_pending);
2966
2967 return sub;
2968}
2969
2970static void
2971subcompositor_get_subsurface(struct wl_client *client,
2972 struct wl_resource *resource,
2973 uint32_t id,
2974 struct wl_resource *surface_resource,
2975 struct wl_resource *parent_resource)
2976{
Jason Ekstrand0f2ef7e2013-06-14 10:07:53 -05002977 struct weston_surface *surface =
2978 wl_resource_get_user_data(surface_resource);
2979 struct weston_surface *parent =
2980 wl_resource_get_user_data(parent_resource);
Pekka Paalanene67858b2013-04-25 13:57:42 +03002981 struct weston_subsurface *sub;
2982 static const char where[] = "get_subsurface: wl_subsurface@";
2983
2984 if (surface == parent) {
2985 wl_resource_post_error(resource,
2986 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2987 "%s%d: wl_surface@%d cannot be its own parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002988 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002989 return;
2990 }
2991
2992 if (weston_surface_to_subsurface(surface)) {
2993 wl_resource_post_error(resource,
2994 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
2995 "%s%d: wl_surface@%d is already a sub-surface",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05002996 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03002997 return;
2998 }
2999
3000 if (surface->configure) {
3001 wl_resource_post_error(resource,
3002 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3003 "%s%d: wl_surface@%d already has a role",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003004 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanene67858b2013-04-25 13:57:42 +03003005 return;
3006 }
3007
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003008 if (weston_surface_get_main_surface(parent) == surface) {
3009 wl_resource_post_error(resource,
3010 WL_SUBCOMPOSITOR_ERROR_BAD_SURFACE,
3011 "%s%d: wl_surface@%d is an ancestor of parent",
Jason Ekstrand0bd587e2013-06-14 10:08:02 -05003012 where, id, wl_resource_get_id(surface_resource));
Pekka Paalanen86c8ca02013-05-17 16:46:07 +03003013 return;
3014 }
3015
Pekka Paalanene67858b2013-04-25 13:57:42 +03003016 /* make sure the parent is in its own list */
3017 if (wl_list_empty(&parent->subsurface_list)) {
3018 if (!weston_subsurface_create_for_parent(parent)) {
3019 wl_resource_post_no_memory(resource);
3020 return;
3021 }
3022 }
3023
3024 sub = weston_subsurface_create(id, surface, parent);
3025 if (!sub) {
3026 wl_resource_post_no_memory(resource);
3027 return;
3028 }
3029
3030 surface->configure = subsurface_configure;
3031 surface->configure_private = sub;
3032}
3033
3034static void
3035subcompositor_destroy(struct wl_client *client, struct wl_resource *resource)
3036{
3037 wl_resource_destroy(resource);
3038}
3039
3040static const struct wl_subcompositor_interface subcompositor_interface = {
3041 subcompositor_destroy,
3042 subcompositor_get_subsurface
3043};
3044
3045static void
3046bind_subcompositor(struct wl_client *client,
3047 void *data, uint32_t version, uint32_t id)
3048{
3049 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003050 struct wl_resource *resource;
Pekka Paalanene67858b2013-04-25 13:57:42 +03003051
Jason Ekstranda85118c2013-06-27 20:17:02 -05003052 resource =
3053 wl_resource_create(client, &wl_subcompositor_interface, 1, id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003054 if (resource == NULL) {
3055 wl_client_post_no_memory(client);
3056 return;
3057 }
3058 wl_resource_set_implementation(resource, &subcompositor_interface,
3059 compositor, NULL);
Pekka Paalanene67858b2013-04-25 13:57:42 +03003060}
3061
3062static void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003063weston_compositor_dpms(struct weston_compositor *compositor,
3064 enum dpms_enum state)
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003065{
3066 struct weston_output *output;
3067
3068 wl_list_for_each(output, &compositor->output_list, link)
3069 if (output->set_dpms)
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003070 output->set_dpms(output, state);
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003071}
3072
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003073WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003074weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003075{
Neil Roberts8b62e202013-09-30 13:14:47 +01003076 uint32_t old_state = compositor->state;
3077
3078 /* The state needs to be changed before emitting the wake
3079 * signal because that may try to schedule a repaint which
3080 * will not work if the compositor is still sleeping */
3081 compositor->state = WESTON_COMPOSITOR_ACTIVE;
3082
3083 switch (old_state) {
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003084 case WESTON_COMPOSITOR_SLEEPING:
3085 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3086 /* fall through */
3087 case WESTON_COMPOSITOR_IDLE:
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003088 case WESTON_COMPOSITOR_OFFSCREEN:
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003089 wl_signal_emit(&compositor->wake_signal, compositor);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003090 /* fall through */
3091 default:
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003092 wl_event_source_timer_update(compositor->idle_source,
3093 compositor->idle_time * 1000);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02003094 }
3095}
3096
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003097WL_EXPORT void
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003098weston_compositor_offscreen(struct weston_compositor *compositor)
3099{
3100 switch (compositor->state) {
3101 case WESTON_COMPOSITOR_OFFSCREEN:
3102 return;
3103 case WESTON_COMPOSITOR_SLEEPING:
3104 weston_compositor_dpms(compositor, WESTON_DPMS_ON);
3105 /* fall through */
3106 default:
3107 compositor->state = WESTON_COMPOSITOR_OFFSCREEN;
3108 wl_event_source_timer_update(compositor->idle_source, 0);
3109 }
3110}
3111
3112WL_EXPORT void
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003113weston_compositor_sleep(struct weston_compositor *compositor)
3114{
3115 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
3116 return;
3117
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01003118 wl_event_source_timer_update(compositor->idle_source, 0);
Ander Conselvan de Oliveira87524b62013-02-21 18:35:22 +02003119 compositor->state = WESTON_COMPOSITOR_SLEEPING;
3120 weston_compositor_dpms(compositor, WESTON_DPMS_OFF);
3121}
3122
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003123static int
3124idle_handler(void *data)
3125{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003126 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003127
3128 if (compositor->idle_inhibit)
3129 return 1;
3130
Ander Conselvan de Oliveira19d10ef2013-02-21 18:35:20 +02003131 compositor->state = WESTON_COMPOSITOR_IDLE;
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003132 wl_signal_emit(&compositor->idle_signal, compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003133
3134 return 1;
3135}
3136
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003137WL_EXPORT void
Xiong Zhang97116532013-10-23 13:58:31 +08003138weston_plane_init(struct weston_plane *plane,
3139 struct weston_compositor *ec,
3140 int32_t x, int32_t y)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003141{
3142 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003143 pixman_region32_init(&plane->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003144 plane->x = x;
3145 plane->y = y;
Xiong Zhang97116532013-10-23 13:58:31 +08003146 plane->compositor = ec;
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003147
3148 /* Init the link so that the call to wl_list_remove() when releasing
3149 * the plane without ever stacking doesn't lead to a crash */
3150 wl_list_init(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003151}
3152
3153WL_EXPORT void
3154weston_plane_release(struct weston_plane *plane)
3155{
Xiong Zhang97116532013-10-23 13:58:31 +08003156 struct weston_view *view;
3157
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003158 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveirae1bd5a02013-03-05 17:30:29 +02003159 pixman_region32_fini(&plane->clip);
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003160
Xiong Zhang97116532013-10-23 13:58:31 +08003161 wl_list_for_each(view, &plane->compositor->view_list, link) {
3162 if (view->plane == plane)
3163 view->plane = NULL;
3164 }
3165
Ander Conselvan de Oliveira3c36bf32013-07-05 16:05:26 +03003166 wl_list_remove(&plane->link);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003167}
3168
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003169WL_EXPORT void
3170weston_compositor_stack_plane(struct weston_compositor *ec,
3171 struct weston_plane *plane,
3172 struct weston_plane *above)
3173{
3174 if (above)
3175 wl_list_insert(above->link.prev, &plane->link);
3176 else
3177 wl_list_insert(&ec->plane_list, &plane->link);
3178}
3179
Casey Dahlin9074db52012-04-19 22:50:09 -04003180static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003181{
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003182 wl_list_remove(wl_resource_get_link(resource));
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04003183}
3184
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003185static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003186bind_output(struct wl_client *client,
3187 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003188{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003189 struct weston_output *output = data;
3190 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003191 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003192
Jason Ekstranda85118c2013-06-27 20:17:02 -05003193 resource = wl_resource_create(client, &wl_output_interface,
3194 MIN(version, 2), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003195 if (resource == NULL) {
3196 wl_client_post_no_memory(client);
3197 return;
3198 }
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003199
Jason Ekstranda0d2dde2013-06-14 10:08:01 -05003200 wl_list_insert(&output->resource_list, wl_resource_get_link(resource));
Jason Ekstranda85118c2013-06-27 20:17:02 -05003201 wl_resource_set_implementation(resource, NULL, data, unbind_resource);
Casey Dahlin9074db52012-04-19 22:50:09 -04003202
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003203 wl_output_send_geometry(resource,
3204 output->x,
3205 output->y,
3206 output->mm_width,
3207 output->mm_height,
3208 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003209 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003210 output->transform);
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003211 if (version >= WL_OUTPUT_SCALE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003212 wl_output_send_scale(resource,
Hardeningff39efa2013-09-18 23:56:35 +02003213 output->current_scale);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003214
3215 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003216 wl_output_send_mode(resource,
3217 mode->flags,
3218 mode->width,
3219 mode->height,
3220 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003221 }
Alexander Larsson4ea95522013-05-22 14:41:37 +02003222
Jasper St. Pierre0013a292014-08-07 16:43:11 -04003223 if (version >= WL_OUTPUT_DONE_SINCE_VERSION)
Alexander Larsson4ea95522013-05-22 14:41:37 +02003224 wl_output_send_done(resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003225}
3226
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003227/* Move other outputs when one is removed so the space remains contiguos. */
3228static void
3229weston_compositor_remove_output(struct weston_compositor *compositor,
3230 struct weston_output *remove_output)
3231{
3232 struct weston_output *output;
3233 int offset = 0;
3234
3235 wl_list_for_each(output, &compositor->output_list, link) {
3236 if (output == remove_output) {
3237 offset = output->width;
3238 continue;
3239 }
3240
3241 if (offset > 0) {
3242 weston_output_move(output,
3243 output->x - offset, output->y);
3244 output->dirty = 1;
3245 }
3246 }
3247}
3248
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003249WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003250weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003251{
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003252 struct wl_resource *resource;
3253
Ander Conselvan de Oliveirae1e23522013-12-13 22:10:55 +02003254 output->destroying = 1;
3255
Zhang, Xiong Ya4b54c02013-12-13 22:10:51 +02003256 weston_compositor_remove_output(output->compositor, output);
Ander Conselvan de Oliveiraf749fc32013-12-13 22:10:50 +02003257 wl_list_remove(&output->link);
3258
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003259 wl_signal_emit(&output->compositor->output_destroyed_signal, output);
Richard Hughes64ddde12013-05-01 21:52:10 +01003260 wl_signal_emit(&output->destroy_signal, output);
3261
Richard Hughesafe690c2013-05-02 10:10:04 +01003262 free(output->name);
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003263 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003264 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003265 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003266
Giulio Camuffo00535ce2014-09-06 16:18:02 +03003267 wl_resource_for_each(resource, &output->resource_list) {
3268 wl_resource_set_destructor(resource, NULL);
3269 }
3270
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003271 wl_global_destroy(output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003272}
3273
Scott Moreau1bad5db2012-08-18 01:04:05 -06003274static void
3275weston_output_compute_transform(struct weston_output *output)
3276{
3277 struct weston_matrix transform;
3278 int flip;
3279
3280 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003281 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003282
3283 switch(output->transform) {
3284 case WL_OUTPUT_TRANSFORM_FLIPPED:
3285 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3286 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3287 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03003288 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003289 flip = -1;
3290 break;
3291 default:
3292 flip = 1;
3293 break;
3294 }
3295
3296 switch(output->transform) {
3297 case WL_OUTPUT_TRANSFORM_NORMAL:
3298 case WL_OUTPUT_TRANSFORM_FLIPPED:
3299 transform.d[0] = flip;
3300 transform.d[1] = 0;
3301 transform.d[4] = 0;
3302 transform.d[5] = 1;
3303 break;
3304 case WL_OUTPUT_TRANSFORM_90:
3305 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3306 transform.d[0] = 0;
3307 transform.d[1] = -flip;
3308 transform.d[4] = 1;
3309 transform.d[5] = 0;
3310 break;
3311 case WL_OUTPUT_TRANSFORM_180:
3312 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3313 transform.d[0] = -flip;
3314 transform.d[1] = 0;
3315 transform.d[4] = 0;
3316 transform.d[5] = -1;
3317 break;
3318 case WL_OUTPUT_TRANSFORM_270:
3319 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3320 transform.d[0] = 0;
3321 transform.d[1] = flip;
3322 transform.d[4] = -1;
3323 transform.d[5] = 0;
3324 break;
3325 default:
3326 break;
3327 }
3328
3329 weston_matrix_multiply(&output->matrix, &transform);
3330}
3331
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003332WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003333weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003334{
Scott Moreau850ca422012-05-21 15:21:25 -06003335 float magnification;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003336
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003337 weston_matrix_init(&output->matrix);
3338 weston_matrix_translate(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003339 -(output->x + output->width / 2.0),
3340 -(output->y + output->height / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003341
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003342 weston_matrix_scale(&output->matrix,
Jason Ekstrand00b84282013-10-27 22:24:59 -05003343 2.0 / output->width,
3344 -2.0 / output->height, 1);
Scott Moreau1bad5db2012-08-18 01:04:05 -06003345
Scott Moreauccbf29d2012-02-22 14:21:41 -07003346 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003347 magnification = 1 / (1 - output->zoom.spring_z.current);
Jason Ekstranda7af7042013-10-12 22:38:11 -05003348 weston_output_update_zoom(output);
Neil Roberts1e40a7e2014-04-25 13:19:37 +01003349 weston_matrix_translate(&output->matrix, -output->zoom.trans_x,
3350 output->zoom.trans_y, 0);
3351 weston_matrix_scale(&output->matrix, magnification,
3352 magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003353 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003354
Neil Roberts6c3b01f2014-05-06 19:04:15 +01003355 weston_output_compute_transform(output);
3356
Scott Moreauccbf29d2012-02-22 14:21:41 -07003357 output->dirty = 0;
3358}
3359
Scott Moreau1bad5db2012-08-18 01:04:05 -06003360static void
Alexander Larsson0b135062013-05-28 16:23:36 +02003361weston_output_transform_scale_init(struct weston_output *output, uint32_t transform, uint32_t scale)
Scott Moreau1bad5db2012-08-18 01:04:05 -06003362{
3363 output->transform = transform;
3364
3365 switch (transform) {
3366 case WL_OUTPUT_TRANSFORM_90:
3367 case WL_OUTPUT_TRANSFORM_270:
3368 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3369 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3370 /* Swap width and height */
Hardeningff39efa2013-09-18 23:56:35 +02003371 output->width = output->current_mode->height;
3372 output->height = output->current_mode->width;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003373 break;
3374 case WL_OUTPUT_TRANSFORM_NORMAL:
3375 case WL_OUTPUT_TRANSFORM_180:
3376 case WL_OUTPUT_TRANSFORM_FLIPPED:
3377 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Hardeningff39efa2013-09-18 23:56:35 +02003378 output->width = output->current_mode->width;
3379 output->height = output->current_mode->height;
Scott Moreau1bad5db2012-08-18 01:04:05 -06003380 break;
3381 default:
3382 break;
3383 }
Scott Moreau1bad5db2012-08-18 01:04:05 -06003384
Hardening57388e42013-09-18 23:56:36 +02003385 output->native_scale = output->current_scale = scale;
Alexander Larsson0b135062013-05-28 16:23:36 +02003386 output->width /= scale;
3387 output->height /= scale;
Alexander Larsson4ea95522013-05-22 14:41:37 +02003388}
3389
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003390static void
3391weston_output_init_geometry(struct weston_output *output, int x, int y)
Scott Moreauccbf29d2012-02-22 14:21:41 -07003392{
3393 output->x = x;
3394 output->y = y;
3395
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02003396 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003397 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003398 output->width,
3399 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003400}
3401
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003402WL_EXPORT void
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003403weston_output_move(struct weston_output *output, int x, int y)
3404{
3405 pixman_region32_t old_region;
3406 struct wl_resource *resource;
3407
3408 output->move_x = x - output->x;
3409 output->move_y = y - output->y;
3410
3411 if (output->move_x == 0 && output->move_y == 0)
3412 return;
3413
3414 pixman_region32_init(&old_region);
3415 pixman_region32_copy(&old_region, &output->region);
3416
3417 weston_output_init_geometry(output, x, y);
3418
3419 output->dirty = 1;
3420
3421 /* Move views on this output. */
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003422 wl_signal_emit(&output->compositor->output_moved_signal, output);
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003423
3424 /* Notify clients of the change for output position. */
Quanxian Wangb2c86362014-03-14 09:16:25 +08003425 wl_resource_for_each(resource, &output->resource_list) {
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003426 wl_output_send_geometry(resource,
3427 output->x,
3428 output->y,
3429 output->mm_width,
3430 output->mm_height,
3431 output->subpixel,
3432 output->make,
3433 output->model,
3434 output->transform);
Quanxian Wangb2c86362014-03-14 09:16:25 +08003435
3436 if (wl_resource_get_version(resource) >= 2)
3437 wl_output_send_done(resource);
3438 }
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003439}
3440
3441WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003442weston_output_init(struct weston_output *output, struct weston_compositor *c,
Alexander Larsson0b135062013-05-28 16:23:36 +02003443 int x, int y, int mm_width, int mm_height, uint32_t transform,
Alexander Larssonedddbd12013-05-24 13:09:43 +02003444 int32_t scale)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003445{
3446 output->compositor = c;
3447 output->x = x;
3448 output->y = y;
Alexander Larsson0b135062013-05-28 16:23:36 +02003449 output->mm_width = mm_width;
3450 output->mm_height = mm_height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003451 output->dirty = 1;
Hardeningff39efa2013-09-18 23:56:35 +02003452 output->original_scale = scale;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003453
Alexander Larsson0b135062013-05-28 16:23:36 +02003454 weston_output_transform_scale_init(output, transform, scale);
Scott Moreau429490d2012-06-17 18:10:59 -06003455 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003456
Zhang, Xiong Yf3012412013-12-13 22:10:53 +02003457 weston_output_init_geometry(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003458 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003459
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003460 wl_signal_init(&output->frame_signal);
Richard Hughes64ddde12013-05-01 21:52:10 +01003461 wl_signal_init(&output->destroy_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003462 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003463 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003464
Casey Dahlin58ba1372012-04-19 22:50:08 -04003465 output->id = ffs(~output->compositor->output_id_pool) - 1;
3466 output->compositor->output_id_pool |= 1 << output->id;
3467
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003468 output->global =
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003469 wl_global_create(c->wl_display, &wl_output_interface, 2,
3470 output, bind_output);
Richard Hughes59d5da72013-05-01 21:52:11 +01003471 wl_signal_emit(&c->output_created_signal, output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003472}
3473
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003474WL_EXPORT void
3475weston_output_transform_coordinate(struct weston_output *output,
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003476 wl_fixed_t device_x, wl_fixed_t device_y,
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003477 wl_fixed_t *x, wl_fixed_t *y)
3478{
3479 wl_fixed_t tx, ty;
3480 wl_fixed_t width, height;
Neil Robertseb5a2002014-05-01 16:13:55 +01003481 float zoom_scale, zx, zy;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003482
Hardeningff39efa2013-09-18 23:56:35 +02003483 width = wl_fixed_from_int(output->width * output->current_scale - 1);
3484 height = wl_fixed_from_int(output->height * output->current_scale - 1);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003485
3486 switch(output->transform) {
3487 case WL_OUTPUT_TRANSFORM_NORMAL:
3488 default:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003489 tx = device_x;
3490 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003491 break;
3492 case WL_OUTPUT_TRANSFORM_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003493 tx = device_y;
3494 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003495 break;
3496 case WL_OUTPUT_TRANSFORM_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003497 tx = width - device_x;
3498 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003499 break;
3500 case WL_OUTPUT_TRANSFORM_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003501 tx = width - device_y;
3502 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003503 break;
3504 case WL_OUTPUT_TRANSFORM_FLIPPED:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003505 tx = width - device_x;
3506 ty = device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003507 break;
3508 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003509 tx = width - device_y;
3510 ty = height - device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003511 break;
3512 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003513 tx = device_x;
3514 ty = height - device_y;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003515 break;
3516 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Jason Ekstrandb6a3cc72013-10-27 22:25:00 -05003517 tx = device_y;
3518 ty = device_x;
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003519 break;
3520 }
3521
Neil Robertseb5a2002014-05-01 16:13:55 +01003522 tx /= output->current_scale;
3523 ty /= output->current_scale;
3524
3525 if (output->zoom.active) {
3526 zoom_scale = output->zoom.spring_z.current;
3527 zx = (wl_fixed_to_double(tx) * (1.0f - zoom_scale) +
3528 output->width / 2.0f *
3529 (zoom_scale + output->zoom.trans_x));
3530 zy = (wl_fixed_to_double(ty) * (1.0f - zoom_scale) +
3531 output->height / 2.0f *
3532 (zoom_scale + output->zoom.trans_y));
3533 tx = wl_fixed_from_double(zx);
3534 ty = wl_fixed_from_double(zy);
3535 }
3536
3537 *x = tx + wl_fixed_from_int(output->x);
3538 *y = ty + wl_fixed_from_int(output->y);
Kristian Høgsberg98c774f2013-07-22 14:33:42 -07003539}
3540
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003541static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003542destroy_viewport(struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003543{
Jonny Lamb74130762013-11-26 18:19:46 +01003544 struct weston_surface *surface =
3545 wl_resource_get_user_data(resource);
3546
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003547 surface->viewport_resource = NULL;
Pekka Paalanenf0cad482014-03-14 14:38:16 +02003548 surface->pending.buffer_viewport.buffer.src_width =
3549 wl_fixed_from_int(-1);
3550 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003551 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003552}
3553
3554static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003555viewport_destroy(struct wl_client *client,
3556 struct wl_resource *resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003557{
3558 wl_resource_destroy(resource);
3559}
3560
3561static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003562viewport_set(struct wl_client *client,
3563 struct wl_resource *resource,
3564 wl_fixed_t src_x,
3565 wl_fixed_t src_y,
3566 wl_fixed_t src_width,
3567 wl_fixed_t src_height,
3568 int32_t dst_width,
3569 int32_t dst_height)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003570{
Jonny Lamb74130762013-11-26 18:19:46 +01003571 struct weston_surface *surface =
3572 wl_resource_get_user_data(resource);
3573
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003574 assert(surface->viewport_resource != NULL);
Jonny Lamb74130762013-11-26 18:19:46 +01003575
Jonny Lamb8ae35902013-11-26 18:19:45 +01003576 if (wl_fixed_to_double(src_width) < 0 ||
3577 wl_fixed_to_double(src_height) < 0) {
3578 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003579 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003580 "source dimensions must be non-negative (%fx%f)",
3581 wl_fixed_to_double(src_width),
3582 wl_fixed_to_double(src_height));
3583 return;
3584 }
3585
3586 if (dst_width <= 0 || dst_height <= 0) {
3587 wl_resource_post_error(resource,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003588 WL_VIEWPORT_ERROR_BAD_VALUE,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003589 "destination dimensions must be positive (%dx%d)",
3590 dst_width, dst_height);
3591 return;
3592 }
3593
Pekka Paalanen952b6c82014-03-14 14:38:15 +02003594 surface->pending.buffer_viewport.buffer.src_x = src_x;
3595 surface->pending.buffer_viewport.buffer.src_y = src_y;
3596 surface->pending.buffer_viewport.buffer.src_width = src_width;
3597 surface->pending.buffer_viewport.buffer.src_height = src_height;
3598 surface->pending.buffer_viewport.surface.width = dst_width;
3599 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003600 surface->pending.buffer_viewport.changed = 1;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003601}
3602
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003603static void
3604viewport_set_source(struct wl_client *client,
3605 struct wl_resource *resource,
3606 wl_fixed_t src_x,
3607 wl_fixed_t src_y,
3608 wl_fixed_t src_width,
3609 wl_fixed_t src_height)
3610{
3611 struct weston_surface *surface =
3612 wl_resource_get_user_data(resource);
3613
3614 assert(surface->viewport_resource != NULL);
3615
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003616 if (src_width == wl_fixed_from_int(-1) &&
3617 src_height == wl_fixed_from_int(-1)) {
3618 /* unset source size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003619 surface->pending.buffer_viewport.buffer.src_width =
3620 wl_fixed_from_int(-1);
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003621 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003622 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003623 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003624
3625 if (src_width <= 0 || src_height <= 0) {
3626 wl_resource_post_error(resource,
3627 WL_VIEWPORT_ERROR_BAD_VALUE,
3628 "source size must be positive (%fx%f)",
3629 wl_fixed_to_double(src_width),
3630 wl_fixed_to_double(src_height));
3631 return;
3632 }
3633
3634 surface->pending.buffer_viewport.buffer.src_x = src_x;
3635 surface->pending.buffer_viewport.buffer.src_y = src_y;
3636 surface->pending.buffer_viewport.buffer.src_width = src_width;
3637 surface->pending.buffer_viewport.buffer.src_height = src_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003638 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003639}
3640
3641static void
3642viewport_set_destination(struct wl_client *client,
3643 struct wl_resource *resource,
3644 int32_t dst_width,
3645 int32_t dst_height)
3646{
3647 struct weston_surface *surface =
3648 wl_resource_get_user_data(resource);
3649
3650 assert(surface->viewport_resource != NULL);
3651
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003652 if (dst_width == -1 && dst_height == -1) {
3653 /* unset destination size */
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003654 surface->pending.buffer_viewport.surface.width = -1;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003655 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003656 return;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003657 }
Pekka Paalanen2c8b5f52014-04-04 14:22:12 +03003658
3659 if (dst_width <= 0 || dst_height <= 0) {
3660 wl_resource_post_error(resource,
3661 WL_VIEWPORT_ERROR_BAD_VALUE,
3662 "destination size must be positive (%dx%d)",
3663 dst_width, dst_height);
3664 return;
3665 }
3666
3667 surface->pending.buffer_viewport.surface.width = dst_width;
3668 surface->pending.buffer_viewport.surface.height = dst_height;
George Kiagiadakis8f9e87f2014-06-13 18:14:20 +02003669 surface->pending.buffer_viewport.changed = 1;
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003670}
3671
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003672static const struct wl_viewport_interface viewport_interface = {
3673 viewport_destroy,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003674 viewport_set,
3675 viewport_set_source,
3676 viewport_set_destination
Jonny Lamb8ae35902013-11-26 18:19:45 +01003677};
3678
3679static void
3680scaler_destroy(struct wl_client *client,
3681 struct wl_resource *resource)
3682{
3683 wl_resource_destroy(resource);
3684}
3685
3686static void
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003687scaler_get_viewport(struct wl_client *client,
3688 struct wl_resource *scaler,
3689 uint32_t id,
3690 struct wl_resource *surface_resource)
Jonny Lamb8ae35902013-11-26 18:19:45 +01003691{
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003692 int version = wl_resource_get_version(scaler);
3693 struct weston_surface *surface =
3694 wl_resource_get_user_data(surface_resource);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003695 struct wl_resource *resource;
3696
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003697 if (surface->viewport_resource) {
Jonny Lamb74130762013-11-26 18:19:46 +01003698 wl_resource_post_error(scaler,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003699 WL_SCALER_ERROR_VIEWPORT_EXISTS,
3700 "a viewport for that surface already exists");
Jonny Lamb74130762013-11-26 18:19:46 +01003701 return;
3702 }
3703
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003704 resource = wl_resource_create(client, &wl_viewport_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003705 version, id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003706 if (resource == NULL) {
3707 wl_client_post_no_memory(client);
3708 return;
3709 }
3710
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003711 wl_resource_set_implementation(resource, &viewport_interface,
3712 surface, destroy_viewport);
Jonny Lamb74130762013-11-26 18:19:46 +01003713
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003714 surface->viewport_resource = resource;
Jonny Lamb8ae35902013-11-26 18:19:45 +01003715}
3716
3717static const struct wl_scaler_interface scaler_interface = {
3718 scaler_destroy,
Pekka Paalanenb0420ae2014-01-08 15:39:17 +02003719 scaler_get_viewport
Jonny Lamb8ae35902013-11-26 18:19:45 +01003720};
3721
3722static void
3723bind_scaler(struct wl_client *client,
3724 void *data, uint32_t version, uint32_t id)
3725{
3726 struct wl_resource *resource;
3727
3728 resource = wl_resource_create(client, &wl_scaler_interface,
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003729 MIN(version, 2), id);
Jonny Lamb8ae35902013-11-26 18:19:45 +01003730 if (resource == NULL) {
3731 wl_client_post_no_memory(client);
3732 return;
3733 }
3734
3735 wl_resource_set_implementation(resource, &scaler_interface,
3736 NULL, NULL);
3737}
3738
3739static void
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003740presentation_destroy(struct wl_client *client, struct wl_resource *resource)
3741{
3742 wl_resource_destroy(resource);
3743}
3744
3745static void
3746presentation_feedback(struct wl_client *client,
3747 struct wl_resource *resource,
3748 struct wl_resource *surface,
3749 uint32_t callback)
3750{
3751 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_METHOD,
3752 "presentation_feedback unimplemented");
3753}
3754
3755static const struct presentation_interface presentation_implementation = {
3756 presentation_destroy,
3757 presentation_feedback
3758};
3759
3760static void
3761bind_presentation(struct wl_client *client,
3762 void *data, uint32_t version, uint32_t id)
3763{
3764 struct weston_compositor *compositor = data;
3765 struct wl_resource *resource;
3766
3767 resource = wl_resource_create(client, &presentation_interface,
3768 MIN(version, 1), id);
3769 if (resource == NULL) {
3770 wl_client_post_no_memory(client);
3771 return;
3772 }
3773
3774 wl_resource_set_implementation(resource, &presentation_implementation,
3775 compositor, NULL);
3776 presentation_send_clock_id(resource, CLOCK_MONOTONIC);
3777}
3778
3779static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003780compositor_bind(struct wl_client *client,
3781 void *data, uint32_t version, uint32_t id)
3782{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003783 struct weston_compositor *compositor = data;
Jason Ekstranda85118c2013-06-27 20:17:02 -05003784 struct wl_resource *resource;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003785
Jason Ekstranda85118c2013-06-27 20:17:02 -05003786 resource = wl_resource_create(client, &wl_compositor_interface,
3787 MIN(version, 3), id);
Kristian Høgsberg0ff79082013-08-06 16:46:25 -07003788 if (resource == NULL) {
3789 wl_client_post_no_memory(client);
3790 return;
3791 }
3792
3793 wl_resource_set_implementation(resource, &compositor_interface,
3794 compositor, NULL);
Kristian Høgsberga8873122011-11-23 10:39:34 -05003795}
3796
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003797static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003798log_uname(void)
3799{
3800 struct utsname usys;
3801
3802 uname(&usys);
3803
3804 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3805 usys.version, usys.machine);
3806}
3807
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003808WL_EXPORT int
Ander Conselvan de Oliveiracbdebc22013-02-21 18:35:16 +02003809weston_environment_get_fd(const char *env)
3810{
3811 char *e, *end;
3812 int fd, flags;
3813
3814 e = getenv(env);
3815 if (!e)
3816 return -1;
3817 fd = strtol(e, &end, 0);
3818 if (*end != '\0')
3819 return -1;
3820
3821 flags = fcntl(fd, F_GETFD);
3822 if (flags == -1)
3823 return -1;
3824
3825 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
3826 unsetenv(env);
3827
3828 return fd;
3829}
3830
3831WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003832weston_compositor_init(struct weston_compositor *ec,
3833 struct wl_display *display,
Kristian Høgsberg4172f662013-02-20 15:27:49 -05003834 int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003835 struct weston_config *config)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003836{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003837 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003838 struct xkb_rule_names xkb_names;
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003839 struct weston_config_section *s;
Ossama Othmana50e6e42013-05-14 09:48:26 -07003840
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003841 ec->config = config;
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003842 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003843 wl_signal_init(&ec->destroy_signal);
Kristian Høgsbergf03a04a2014-04-06 22:04:50 -07003844 wl_signal_init(&ec->create_surface_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003845 wl_signal_init(&ec->activate_signal);
Tiago Vignattifb2adba2013-06-12 15:43:21 -03003846 wl_signal_init(&ec->transform_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03003847 wl_signal_init(&ec->kill_signal);
Ander Conselvan de Oliveiraa4575632013-02-21 18:35:23 +02003848 wl_signal_init(&ec->idle_signal);
3849 wl_signal_init(&ec->wake_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003850 wl_signal_init(&ec->show_input_panel_signal);
3851 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen14da96b2013-04-18 16:47:28 +02003852 wl_signal_init(&ec->update_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003853 wl_signal_init(&ec->seat_created_signal);
Richard Hughes59d5da72013-05-01 21:52:11 +01003854 wl_signal_init(&ec->output_created_signal);
Ander Conselvan de Oliveiraf84327a2014-01-29 18:47:51 +02003855 wl_signal_init(&ec->output_destroyed_signal);
Ander Conselvan de Oliveiraa8a9baf2014-01-29 18:47:52 +02003856 wl_signal_init(&ec->output_moved_signal);
Kristian Høgsberg61741a22013-09-17 16:02:57 -07003857 wl_signal_init(&ec->session_signal);
3858 ec->session_active = 1;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003859
Casey Dahlin58ba1372012-04-19 22:50:08 -04003860 ec->output_id_pool = 0;
3861
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003862 if (!wl_global_create(display, &wl_compositor_interface, 3,
3863 ec, compositor_bind))
Kristian Høgsberga8873122011-11-23 10:39:34 -05003864 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003865
Kristian Høgsberg919cddb2013-07-08 19:03:57 -04003866 if (!wl_global_create(display, &wl_subcompositor_interface, 1,
3867 ec, bind_subcompositor))
Pekka Paalanene67858b2013-04-25 13:57:42 +03003868 return -1;
3869
Pekka Paalanen0b4c5352014-03-14 14:38:17 +02003870 if (!wl_global_create(ec->wl_display, &wl_scaler_interface, 2,
Jonny Lamb8ae35902013-11-26 18:19:45 +01003871 ec, bind_scaler))
3872 return -1;
3873
Pekka Paalanen31f7d782014-09-23 22:08:43 -04003874 if (!wl_global_create(ec->wl_display, &presentation_interface, 1,
3875 ec, bind_presentation))
3876 return -1;
3877
Jason Ekstranda7af7042013-10-12 22:38:11 -05003878 wl_list_init(&ec->view_list);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003879 wl_list_init(&ec->plane_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003880 wl_list_init(&ec->layer_list);
3881 wl_list_init(&ec->seat_list);
3882 wl_list_init(&ec->output_list);
3883 wl_list_init(&ec->key_binding_list);
Daniel Stone96d47c02013-11-19 11:37:12 +01003884 wl_list_init(&ec->modifier_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003885 wl_list_init(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003886 wl_list_init(&ec->touch_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003887 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003888 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01003889
Xiong Zhang97116532013-10-23 13:58:31 +08003890 weston_plane_init(&ec->primary_plane, ec, 0, 0);
Ander Conselvan de Oliveira8ad19822013-03-05 17:30:27 +02003891 weston_compositor_stack_plane(ec, &ec->primary_plane, NULL);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003892
Kristian Høgsberg6a047912013-05-23 15:56:29 -04003893 s = weston_config_get_section(ec->config, "keyboard", NULL, NULL);
3894 weston_config_section_get_string(s, "keymap_rules",
3895 (char **) &xkb_names.rules, NULL);
3896 weston_config_section_get_string(s, "keymap_model",
3897 (char **) &xkb_names.model, NULL);
3898 weston_config_section_get_string(s, "keymap_layout",
3899 (char **) &xkb_names.layout, NULL);
3900 weston_config_section_get_string(s, "keymap_variant",
3901 (char **) &xkb_names.variant, NULL);
3902 weston_config_section_get_string(s, "keymap_options",
3903 (char **) &xkb_names.options, NULL);
Rob Bradford382ff462013-06-24 16:52:45 +01003904
Kristian Høgsberg2f07ef62013-02-16 14:29:24 -05003905 if (weston_compositor_xkb_init(ec, &xkb_names) < 0)
3906 return -1;
Daniel Stone725c2c32012-06-22 14:04:36 +01003907
Jonny Lamb66a41a02014-08-12 14:58:25 +02003908 weston_config_section_get_int(s, "repeat-rate",
3909 &ec->kb_repeat_rate, 40);
3910 weston_config_section_get_int(s, "repeat-delay",
3911 &ec->kb_repeat_delay, 400);
3912
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01003913 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003914
3915 wl_data_device_manager_init(ec->wl_display);
3916
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003917 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003918
Daniel Stone725c2c32012-06-22 14:04:36 +01003919 loop = wl_display_get_event_loop(ec->wl_display);
3920 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3921 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3922
3923 ec->input_loop = wl_event_loop_create();
3924
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003925 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3926 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3927
3928 weston_compositor_schedule_repaint(ec);
3929
Daniel Stone725c2c32012-06-22 14:04:36 +01003930 return 0;
3931}
3932
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003933WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003934weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003935{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003936 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003937
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003938 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003939 if (ec->input_loop_source)
3940 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003941
Matt Roper361d2ad2011-08-29 13:52:23 -07003942 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003943 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003944 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003945
Ander Conselvan de Oliveira18536762013-12-20 21:07:00 +02003946 if (ec->renderer)
3947 ec->renderer->destroy(ec);
3948
Daniel Stone325fc2d2012-05-30 16:31:58 +01003949 weston_binding_list_destroy_all(&ec->key_binding_list);
3950 weston_binding_list_destroy_all(&ec->button_binding_list);
Neil Robertsa28c6932013-10-03 16:43:04 +01003951 weston_binding_list_destroy_all(&ec->touch_binding_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003952 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003953 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003954
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003955 weston_plane_release(&ec->primary_plane);
3956
Jonas Ådahlc97af922012-03-28 22:36:09 +02003957 wl_event_loop_destroy(ec->input_loop);
Ossama Othmana50e6e42013-05-14 09:48:26 -07003958
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04003959 weston_config_destroy(ec->config);
Matt Roper361d2ad2011-08-29 13:52:23 -07003960}
3961
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003962WL_EXPORT void
Giulio Camuffocdb4d292013-11-14 23:42:53 +01003963weston_compositor_set_default_pointer_grab(struct weston_compositor *ec,
3964 const struct weston_pointer_grab_interface *interface)
3965{
3966 struct weston_seat *seat;
3967
3968 ec->default_pointer_grab = interface;
3969 wl_list_for_each(seat, &ec->seat_list, link) {
3970 if (seat->pointer) {
3971 weston_pointer_set_default_grab(seat->pointer,
3972 interface);
3973 }
3974 }
3975}
3976
3977WL_EXPORT void
Kristian Høgsbergaf4f2aa2013-02-15 20:53:20 -05003978weston_version(int *major, int *minor, int *micro)
3979{
3980 *major = WESTON_VERSION_MAJOR;
3981 *minor = WESTON_VERSION_MINOR;
3982 *micro = WESTON_VERSION_MICRO;
3983}
3984
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003985static const struct {
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003986 uint32_t bit; /* enum weston_capability */
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003987 const char *desc;
3988} capability_strings[] = {
3989 { WESTON_CAP_ROTATION_ANY, "arbitrary surface rotation:" },
Pekka Paalanen4fc5dd02013-05-22 18:03:05 +03003990 { WESTON_CAP_CAPTURE_YFLIP, "screen capture uses y-flip:" },
Pekka Paalanen7bb65102013-05-22 18:03:04 +03003991};
3992
3993static void
3994weston_compositor_log_capabilities(struct weston_compositor *compositor)
3995{
3996 unsigned i;
3997 int yes;
3998
3999 weston_log("Compositor capabilities:\n");
4000 for (i = 0; i < ARRAY_LENGTH(capability_strings); i++) {
4001 yes = compositor->capabilities & capability_strings[i].bit;
4002 weston_log_continue(STAMP_SPACE "%s %s\n",
4003 capability_strings[i].desc,
4004 yes ? "yes" : "no");
4005 }
4006}
4007
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004008static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004009{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004010 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004011
Martin Minarik6d118362012-06-07 18:01:59 +02004012 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04004013 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04004014
4015 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004016}
4017
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004018#ifdef HAVE_LIBUNWIND
4019
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004020static void
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004021print_backtrace(void)
4022{
4023 unw_cursor_t cursor;
4024 unw_context_t context;
4025 unw_word_t off;
4026 unw_proc_info_t pip;
4027 int ret, i = 0;
4028 char procname[256];
4029 const char *filename;
4030 Dl_info dlinfo;
4031
4032 pip.unwind_info = NULL;
4033 ret = unw_getcontext(&context);
4034 if (ret) {
4035 weston_log("unw_getcontext: %d\n", ret);
4036 return;
4037 }
4038
4039 ret = unw_init_local(&cursor, &context);
4040 if (ret) {
4041 weston_log("unw_init_local: %d\n", ret);
4042 return;
4043 }
4044
4045 ret = unw_step(&cursor);
4046 while (ret > 0) {
4047 ret = unw_get_proc_info(&cursor, &pip);
4048 if (ret) {
4049 weston_log("unw_get_proc_info: %d\n", ret);
4050 break;
4051 }
4052
4053 ret = unw_get_proc_name(&cursor, procname, 256, &off);
4054 if (ret && ret != -UNW_ENOMEM) {
4055 if (ret != -UNW_EUNSPEC)
4056 weston_log("unw_get_proc_name: %d\n", ret);
4057 procname[0] = '?';
4058 procname[1] = 0;
4059 }
4060
4061 if (dladdr((void *)(pip.start_ip + off), &dlinfo) && dlinfo.dli_fname &&
4062 *dlinfo.dli_fname)
4063 filename = dlinfo.dli_fname;
4064 else
4065 filename = "?";
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004066
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004067 weston_log("%u: %s (%s%s+0x%x) [%p]\n", i++, filename, procname,
4068 ret == -UNW_ENOMEM ? "..." : "", (int)off, (void *)(pip.start_ip + off));
4069
4070 ret = unw_step(&cursor);
4071 if (ret < 0)
4072 weston_log("unw_step: %d\n", ret);
4073 }
4074}
4075
4076#else
4077
4078static void
4079print_backtrace(void)
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004080{
4081 void *buffer[32];
4082 int i, count;
4083 Dl_info info;
4084
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004085 count = backtrace(buffer, ARRAY_LENGTH(buffer));
4086 for (i = 0; i < count; i++) {
4087 dladdr(buffer[i], &info);
4088 weston_log(" [%016lx] %s (%s)\n",
4089 (long) buffer[i],
4090 info.dli_sname ? info.dli_sname : "--",
4091 info.dli_fname);
4092 }
4093}
4094
4095#endif
4096
4097static void
Peter Maatmane5b42e42013-03-27 22:38:53 +01004098on_caught_signal(int s, siginfo_t *siginfo, void *context)
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004099{
Peter Maatmane5b42e42013-03-27 22:38:53 +01004100 /* This signal handler will do a best-effort backtrace, and
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004101 * then call the backend restore function, which will switch
4102 * back to the vt we launched from or ungrab X etc and then
4103 * raise SIGTRAP. If we run weston under gdb from X or a
Peter Maatmane5b42e42013-03-27 22:38:53 +01004104 * different vt, and tell gdb "handle *s* nostop", this
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004105 * will allow weston to switch back to gdb on crash and then
Peter Maatmane5b42e42013-03-27 22:38:53 +01004106 * gdb will catch the crash with SIGTRAP.*/
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004107
Peter Maatmane5b42e42013-03-27 22:38:53 +01004108 weston_log("caught signal: %d\n", s);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004109
Marcin Slusarz554a0da2013-02-18 13:27:22 -05004110 print_backtrace();
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004111
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004112 segv_compositor->restore(segv_compositor);
4113
4114 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05004115}
4116
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004117WL_EXPORT void *
4118weston_load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004119{
4120 char path[PATH_MAX];
4121 void *module, *init;
4122
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004123 if (name == NULL)
4124 return NULL;
4125
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004126 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07004127 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004128 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02004129 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004130
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004131 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
4132 if (module) {
4133 weston_log("Module '%s' already loaded\n", path);
4134 dlclose(module);
4135 return NULL;
4136 }
4137
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004138 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04004139 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004140 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004141 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004142 return NULL;
4143 }
4144
4145 init = dlsym(module, entrypoint);
4146 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03004147 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00004148 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004149 return NULL;
4150 }
4151
4152 return init;
4153}
4154
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004155static int
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004156load_modules(struct weston_compositor *ec, const char *modules,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004157 int *argc, char *argv[])
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004158{
4159 const char *p, *end;
4160 char buffer[256];
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004161 int (*module_init)(struct weston_compositor *ec,
Ossama Othmana50e6e42013-05-14 09:48:26 -07004162 int *argc, char *argv[]);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004163
4164 if (modules == NULL)
4165 return 0;
4166
4167 p = modules;
4168 while (*p) {
4169 end = strchrnul(p, ',');
4170 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004171 module_init = weston_load_module(buffer, "module_init");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004172 if (module_init)
Ossama Othmana50e6e42013-05-14 09:48:26 -07004173 module_init(ec, argc, argv);
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004174 p = end;
4175 while (*p == ',')
4176 p++;
4177
4178 }
4179
4180 return 0;
4181}
4182
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004183static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02004184 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
4185
4186static const char xdg_wrong_message[] =
4187 "fatal: environment variable XDG_RUNTIME_DIR\n"
4188 "is set to \"%s\", which is not a directory.\n";
4189
4190static const char xdg_wrong_mode_message[] =
4191 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
Guillem Jover32b793c2014-01-31 20:41:21 +01004192 "correctly. Unix access mode must be 0700 (current mode is %o),\n"
4193 "and must be owned by the user (current owner is UID %d).\n";
Martin Minarik37032f82012-06-18 20:15:18 +02004194
4195static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03004196 "Refer to your distribution on how to get it, or\n"
4197 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
4198 "on how to implement it.\n";
4199
Martin Minarik37032f82012-06-18 20:15:18 +02004200static void
4201verify_xdg_runtime_dir(void)
4202{
4203 char *dir = getenv("XDG_RUNTIME_DIR");
4204 struct stat s;
4205
4206 if (!dir) {
4207 weston_log(xdg_error_message);
4208 weston_log_continue(xdg_detail_message);
4209 exit(EXIT_FAILURE);
4210 }
4211
4212 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
4213 weston_log(xdg_wrong_message, dir);
4214 weston_log_continue(xdg_detail_message);
4215 exit(EXIT_FAILURE);
4216 }
4217
4218 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
4219 weston_log(xdg_wrong_mode_message,
4220 dir, s.st_mode & 0777, s.st_uid);
4221 weston_log_continue(xdg_detail_message);
4222 }
4223}
4224
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004225static int
4226usage(int error_code)
4227{
4228 fprintf(stderr,
4229 "Usage: weston [OPTIONS]\n\n"
4230 "This is weston version " VERSION ", the Wayland reference compositor.\n"
4231 "Weston supports multiple backends, and depending on which backend is in use\n"
4232 "different options will be accepted.\n\n"
4233
4234
4235 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06004236 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004237 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004238 "\t\t\t\tfbdev-backend.so, x11-backend.so or\n"
4239 "\t\t\t\twayland-backend.so\n"
Jason Ekstranda985da42013-08-22 17:28:03 -05004240 " --shell=MODULE\tShell module, defaults to desktop-shell.so\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004241 " -S, --socket=NAME\tName of socket to listen on\n"
4242 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004243 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004244 " --log==FILE\t\tLog to the given file\n"
Pekka Paalanen588bee12014-05-07 16:26:25 +03004245 " --no-config\t\tDo not read weston.ini\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004246 " -h, --help\t\tThis help message\n\n");
4247
4248 fprintf(stderr,
4249 "Options for drm-backend.so:\n\n"
4250 " --connector=ID\tBring up only this connector\n"
4251 " --seat=SEAT\t\tThe seat that weston should run on\n"
4252 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02004253 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004254 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
4255
4256 fprintf(stderr,
Philipp Brüschweilere14560e2013-03-30 15:18:49 +01004257 "Options for fbdev-backend.so:\n\n"
4258 " --tty=TTY\t\tThe tty to use\n"
4259 " --device=DEVICE\tThe framebuffer device to use\n\n");
4260
4261 fprintf(stderr,
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004262 "Options for x11-backend.so:\n\n"
4263 " --width=WIDTH\t\tWidth of X window\n"
4264 " --height=HEIGHT\tHeight of X window\n"
4265 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05004266 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004267 " --output-count=COUNT\tCreate multiple outputs\n"
4268 " --no-input\t\tDont create input devices\n\n");
4269
4270 fprintf(stderr,
4271 "Options for wayland-backend.so:\n\n"
4272 " --width=WIDTH\t\tWidth of Wayland surface\n"
4273 " --height=HEIGHT\tHeight of Wayland surface\n"
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06004274 " --scale=SCALE\tScale factor of ouput\n"
Jason Ekstrand5ea04802013-11-07 20:13:33 -06004275 " --fullscreen\t\tRun in fullscreen mode\n"
Jason Ekstrandff2fd462013-10-27 22:24:58 -05004276 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Jason Ekstrand48ce4212013-10-27 22:25:02 -05004277 " --output-count=COUNT\tCreate multiple outputs\n"
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05004278 " --sprawl\t\tCreate one fullscreen output for every parent output\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004279 " --display=DISPLAY\tWayland display to connect to\n\n");
4280
Pekka Paalanene31e0532013-05-22 18:03:07 +03004281#if defined(BUILD_RPI_COMPOSITOR) && defined(HAVE_BCM_HOST)
4282 fprintf(stderr,
4283 "Options for rpi-backend.so:\n\n"
4284 " --tty=TTY\t\tThe tty to use\n"
4285 " --single-buffer\tUse single-buffered Dispmanx elements.\n"
4286 " --transform=TR\tThe output transformation, TR is one of:\n"
4287 "\tnormal 90 180 270 flipped flipped-90 flipped-180 flipped-270\n"
Tomeu Vizosoe4f7b922013-12-02 17:18:58 +01004288 " --opaque-regions\tEnable support for opaque regions, can be "
4289 "very slow without support in the GPU firmware.\n"
Pekka Paalanene31e0532013-05-22 18:03:07 +03004290 "\n");
4291#endif
4292
Hardeningc077a842013-07-08 00:51:35 +02004293#if defined(BUILD_RDP_COMPOSITOR)
4294 fprintf(stderr,
4295 "Options for rdp-backend.so:\n\n"
4296 " --width=WIDTH\t\tWidth of desktop\n"
4297 " --height=HEIGHT\tHeight of desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004298 " --env-socket=SOCKET\tUse that socket as peer connection\n"
4299 " --address=ADDR\tThe address to bind\n"
4300 " --port=PORT\tThe port to listen on\n"
Hardeningf34cd2c2014-04-02 19:54:00 -05004301 " --no-clients-resize\tThe RDP peers will be forced to the size of the desktop\n"
Hardeningc077a842013-07-08 00:51:35 +02004302 " --rdp4-key=FILE\tThe file containing the key for RDP4 encryption\n"
4303 " --rdp-tls-cert=FILE\tThe file containing the certificate for TLS encryption\n"
4304 " --rdp-tls-key=FILE\tThe file containing the private key for TLS encryption\n"
4305 "\n");
4306#endif
4307
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004308 exit(error_code);
4309}
4310
Peter Maatmane5b42e42013-03-27 22:38:53 +01004311static void
4312catch_signals(void)
4313{
4314 struct sigaction action;
4315
4316 action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4317 action.sa_sigaction = on_caught_signal;
4318 sigemptyset(&action.sa_mask);
4319 sigaction(SIGSEGV, &action, NULL);
4320 sigaction(SIGABRT, &action, NULL);
4321}
4322
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004323static void
4324handle_primary_client_destroyed(struct wl_listener *listener, void *data)
4325{
4326 struct wl_client *client = data;
4327
4328 weston_log("Primary client died. Closing...\n");
4329
4330 wl_display_terminate(wl_client_get_display(client));
4331}
4332
Ryo Munakatad8deff62014-09-06 07:32:05 +09004333static char *
4334weston_choose_default_backend(void)
4335{
4336 char *backend = NULL;
4337
4338 if (getenv("WAYLAND_DISPLAY") || getenv("WAYLAND_SOCKET"))
4339 backend = strdup("wayland-backend.so");
4340 else if (getenv("DISPLAY"))
4341 backend = strdup("x11-backend.so");
4342 else
4343 backend = strdup(WESTON_NATIVE_BACKEND);
4344
4345 return backend;
4346}
4347
4348static int
4349weston_create_listening_socket(struct wl_display *display, const char *socket_name)
4350{
4351 if (socket_name) {
4352 if (wl_display_add_socket(display, socket_name)) {
4353 weston_log("fatal: failed to add socket: %m\n");
4354 return -1;
4355 }
4356 } else {
4357 socket_name = wl_display_add_socket_auto(display);
4358 if (!socket_name) {
4359 weston_log("fatal: failed to add socket: %m\n");
4360 return -1;
4361 }
4362 }
4363
4364 setenv("WAYLAND_DISPLAY", socket_name, 1);
4365
4366 return 0;
4367}
4368
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004369int main(int argc, char *argv[])
4370{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004371 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004372 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004373 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004374 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05004375 struct wl_event_loop *loop;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004376 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004377 *(*backend_init)(struct wl_display *display,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004378 int *argc, char *argv[],
4379 struct weston_config *config);
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004380 int i, fd;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004381 char *backend = NULL;
Jason Ekstranda985da42013-08-22 17:28:03 -05004382 char *shell = NULL;
Ondřej Majerech03db71c2014-09-11 15:53:15 +02004383 char *modules = NULL;
4384 char *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004385 char *log = NULL;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004386 char *server_socket = NULL, *end;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004387 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004388 int32_t help = 0;
Ryo Munakata03faed22014-09-06 07:32:51 +09004389 char *socket_name = NULL;
Scott Moreau12245142012-08-29 15:15:58 -06004390 int32_t version = 0;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004391 int32_t noconfig = 0;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004392 int32_t numlock_on;
Pekka Paalanen588bee12014-05-07 16:26:25 +03004393 struct weston_config *config = NULL;
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004394 struct weston_config_section *section;
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004395 struct wl_client *primary_client;
4396 struct wl_listener primary_client_destroyed;
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004397 struct weston_seat *seat;
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004398
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004399 const struct weston_option core_options[] = {
Ryo Munakata03faed22014-09-06 07:32:51 +09004400 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4401 { WESTON_OPTION_STRING, "shell", 0, &shell },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004402 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4403 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04004404 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02004405 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004406 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06004407 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Pekka Paalanen588bee12014-05-07 16:26:25 +03004408 { WESTON_OPTION_BOOLEAN, "no-config", 0, &noconfig },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004409 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004410
Kristian Høgsberg4172f662013-02-20 15:27:49 -05004411 parse_options(core_options, ARRAY_LENGTH(core_options), &argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004412
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004413 if (help)
4414 usage(EXIT_SUCCESS);
4415
Scott Moreau12245142012-08-29 15:15:58 -06004416 if (version) {
4417 printf(PACKAGE_STRING "\n");
4418 return EXIT_SUCCESS;
4419 }
4420
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004421 weston_log_file_open(log);
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004422
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004423 weston_log("%s\n"
4424 STAMP_SPACE "%s\n"
4425 STAMP_SPACE "Bug reports to: %s\n"
4426 STAMP_SPACE "Build: %s\n",
4427 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004428 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004429 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004430
Martin Minarik37032f82012-06-18 20:15:18 +02004431 verify_xdg_runtime_dir();
4432
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004433 display = wl_display_create();
4434
Tiago Vignatti2116b892011-08-08 05:52:59 -07004435 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004436 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4437 display);
4438 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4439 display);
4440 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4441 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004442
4443 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004444 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4445 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004446
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304447 if (!signals[0] || !signals[1] || !signals[2] || !signals[3]) {
4448 ret = EXIT_FAILURE;
4449 goto out_signals;
4450 }
4451
Pekka Paalanen588bee12014-05-07 16:26:25 +03004452 if (noconfig == 0)
4453 config = weston_config_parse("weston.ini");
Lubomir Rintelba44c6b2013-11-15 14:18:15 +01004454 if (config != NULL) {
4455 weston_log("Using config file '%s'\n",
4456 weston_config_get_full_path(config));
4457 } else {
4458 weston_log("Starting with no config file.\n");
4459 }
4460 section = weston_config_get_section(config, "core", NULL, NULL);
4461
Ryo Munakata03faed22014-09-06 07:32:51 +09004462 if (!backend) {
U. Artie Eoff2e2384a2014-01-17 13:19:01 -08004463 weston_config_section_get_string(section, "backend", &backend,
4464 NULL);
Ryo Munakata03faed22014-09-06 07:32:51 +09004465 if (!backend)
4466 backend = weston_choose_default_backend();
4467 }
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004468
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03004469 backend_init = weston_load_module(backend, "backend_init");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304470 if (!backend_init) {
4471 ret = EXIT_FAILURE;
4472 goto out_signals;
4473 }
Kristian Høgsberga9410222011-01-14 17:22:35 -05004474
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04004475 ec = backend_init(display, &argc, argv, config);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004476 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004477 weston_log("fatal: failed to create compositor\n");
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304478 ret = EXIT_FAILURE;
4479 goto out_signals;
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004480 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004481
Peter Maatmane5b42e42013-03-27 22:38:53 +01004482 catch_signals();
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004483 segv_compositor = ec;
4484
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004485 ec->idle_time = idle_time;
Giulio Camuffocdb4d292013-11-14 23:42:53 +01004486 ec->default_pointer_grab = NULL;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004487
Kristian Høgsbergcb4685b2013-02-20 15:37:49 -05004488 for (i = 1; i < argc; i++)
4489 weston_log("fatal: unhandled option: %s\n", argv[i]);
4490 if (argc > 1) {
4491 ret = EXIT_FAILURE;
4492 goto out;
4493 }
4494
Pekka Paalanen7bb65102013-05-22 18:03:04 +03004495 weston_compositor_log_capabilities(ec);
4496
Jason Ekstrand923bfe62014-04-02 19:53:58 -05004497 server_socket = getenv("WAYLAND_SERVER_SOCKET");
4498 if (server_socket) {
4499 weston_log("Running with single client\n");
4500 fd = strtol(server_socket, &end, 0);
4501 if (*end != '\0')
4502 fd = -1;
4503 } else {
4504 fd = -1;
4505 }
4506
4507 if (fd != -1) {
4508 primary_client = wl_client_create(display, fd);
4509 if (!primary_client) {
4510 weston_log("fatal: failed to add client: %m\n");
4511 ret = EXIT_FAILURE;
4512 goto out;
4513 }
4514 primary_client_destroyed.notify =
4515 handle_primary_client_destroyed;
4516 wl_client_add_destroy_listener(primary_client,
4517 &primary_client_destroyed);
Ryo Munakatad8deff62014-09-06 07:32:05 +09004518 } else if (weston_create_listening_socket(display, socket_name)) {
4519 ret = EXIT_FAILURE;
4520 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004521 }
4522
Ryo Munakata03faed22014-09-06 07:32:51 +09004523 if (!shell)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004524 weston_config_section_get_string(section, "shell", &shell,
4525 "desktop-shell.so");
4526
Ryo Munakata03faed22014-09-06 07:32:51 +09004527 if (load_modules(ec, shell, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004528 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004529
4530 weston_config_section_get_string(section, "modules", &modules, "");
Ryo Munakata03faed22014-09-06 07:32:51 +09004531 if (load_modules(ec, modules, &argc, argv) < 0)
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004532 goto out;
Jasper St. Pierree2f0f842014-07-17 13:55:44 -04004533
4534 if (load_modules(ec, option_modules, &argc, argv) < 0)
4535 goto out;
4536
Giulio Camuffode7e2b32014-08-28 19:44:10 +03004537 section = weston_config_get_section(config, "keyboard", NULL, NULL);
4538 weston_config_section_get_bool(section, "numlock-on", &numlock_on, 0);
4539 if (numlock_on) {
4540 wl_list_for_each(seat, &ec->seat_list, link) {
4541 if (seat->keyboard)
4542 weston_keyboard_set_locks(seat->keyboard,
4543 WESTON_NUM_LOCK,
4544 WESTON_NUM_LOCK);
4545 }
4546 }
4547
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004548 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004549
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004550 wl_display_run(display);
4551
Ryo Munakata03faed22014-09-06 07:32:51 +09004552out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004553 /* prevent further rendering while shutting down */
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +01004554 ec->state = WESTON_COMPOSITOR_OFFSCREEN;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004555
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004556 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004557
Daniel Stone855028f2012-05-01 20:37:10 +01004558 weston_compositor_xkb_destroy(ec);
4559
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004560 ec->destroy(ec);
Srivardhan Hebbarba2a36d2014-05-27 14:30:59 +05304561
4562out_signals:
4563 for (i = ARRAY_LENGTH(signals) - 1; i >= 0; i--)
4564 if (signals[i])
4565 wl_event_source_remove(signals[i]);
4566
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004567 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004568
Martin Minarik19e6f262012-06-07 13:08:46 +02004569 weston_log_file_close();
4570
Ryo Munakata03faed22014-09-06 07:32:51 +09004571 free(backend);
4572 free(shell);
4573 free(socket_name);
4574 free(option_modules);
4575 free(log);
4576 free(modules);
4577
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004578 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004579}