blob: 994f814642071e7aa061eaaad3fe29759607d700 [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 Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 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øgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020041#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020042#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050044#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040047#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050048#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040049#include <sys/time.h>
50#include <time.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050051
Pekka Paalanen50719bc2011-11-22 14:18:50 +020052#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040053#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030054#include "../shared/os-compatibility.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040055#include "git-version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050056
Kristian Høgsberg27da5382011-06-21 17:32:25 -040057static struct wl_list child_process_list;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -040058static struct weston_compositor *segv_compositor;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040059
60static int
61sigchld_handler(int signal_number, void *data)
62{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050063 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040064 int status;
65 pid_t pid;
66
Bill Spitzak027b9622012-03-17 15:22:03 -070067 pid = waitpid(-1, &status, WNOHANG);
68 if (!pid)
69 return 1;
70
Kristian Høgsberg27da5382011-06-21 17:32:25 -040071 wl_list_for_each(p, &child_process_list, link) {
72 if (p->pid == pid)
73 break;
74 }
75
76 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020077 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040078 return 1;
79 }
80
81 wl_list_remove(&p->link);
82 p->cleanup(p, status);
83
84 return 1;
85}
86
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020087static void
88weston_output_transform_init(struct weston_output *output, uint32_t transform);
89
Alex Wu2dda6042012-04-17 17:20:47 +080090WL_EXPORT int
91weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
92{
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -020093 struct weston_seat *seat;
94 pixman_region32_t old_output_region;
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020095 int ret;
96
Alex Wu2dda6042012-04-17 17:20:47 +080097 if (!output->switch_mode)
98 return -1;
99
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200100 ret = output->switch_mode(output, mode);
101 if (ret < 0)
102 return ret;
103
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200104 pixman_region32_init(&old_output_region);
105 pixman_region32_copy(&old_output_region, &output->region);
106
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200107 /* Update output region and transformation matrix */
108 weston_output_transform_init(output, output->transform);
109
110 pixman_region32_init(&output->previous_damage);
111 pixman_region32_init_rect(&output->region, output->x, output->y,
112 output->width, output->height);
113
114 weston_output_update_matrix(output);
115
Ander Conselvan de Oliveira2bbb2b82012-12-14 13:37:26 -0200116 /* If a pointer falls outside the outputs new geometry, move it to its
117 * lower-right corner */
118 wl_list_for_each(seat, &output->compositor->seat_list, link) {
119 struct wl_pointer *pointer = seat->seat.pointer;
120 int32_t x, y;
121
122 if (!pointer)
123 continue;
124
125 x = wl_fixed_to_int(pointer->x);
126 y = wl_fixed_to_int(pointer->y);
127
128 if (!pixman_region32_contains_point(&old_output_region,
129 x, y, NULL) ||
130 pixman_region32_contains_point(&output->region,
131 x, y, NULL))
132 continue;
133
134 if (x >= output->x + output->width)
135 x = output->x + output->width - 1;
136 if (y >= output->y + output->height)
137 y = output->y + output->height - 1;
138
139 pointer->x = wl_fixed_from_int(x);
140 pointer->y = wl_fixed_from_int(y);
141 }
142
143 pixman_region32_fini(&old_output_region);
144
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -0200145 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800146}
147
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400148WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500149weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400150{
151 wl_list_insert(&child_process_list, &process->link);
152}
153
Benjamin Franzke06286262011-05-06 19:12:33 +0200154static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200155child_client_exec(int sockfd, const char *path)
156{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500157 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200158 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200159 sigset_t allsigs;
160
161 /* do not give our signal mask to the new process */
162 sigfillset(&allsigs);
163 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200164
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500165 /* Launch clients as the user. */
166 seteuid(getuid());
167
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500168 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
169 * non-CLOEXEC fd to pass through exec. */
170 clientfd = dup(sockfd);
171 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200172 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500173 return;
174 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200175
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500176 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200177 setenv("WAYLAND_SOCKET", s, 1);
178
179 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200180 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200181 path);
182}
183
184WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500185weston_client_launch(struct weston_compositor *compositor,
186 struct weston_process *proc,
187 const char *path,
188 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200189{
190 int sv[2];
191 pid_t pid;
192 struct wl_client *client;
193
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300194 weston_log("launching '%s'\n", path);
195
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300196 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200197 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200198 "socketpair failed while launching '%s': %m\n",
199 path);
200 return NULL;
201 }
202
203 pid = fork();
204 if (pid == -1) {
205 close(sv[0]);
206 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200207 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200208 "fork failed while launching '%s': %m\n", path);
209 return NULL;
210 }
211
212 if (pid == 0) {
213 child_client_exec(sv[1], path);
214 exit(-1);
215 }
216
217 close(sv[1]);
218
219 client = wl_client_create(compositor->wl_display, sv[0]);
220 if (!client) {
221 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200222 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200223 "wl_client_create failed while launching '%s'.\n",
224 path);
225 return NULL;
226 }
227
228 proc->pid = pid;
229 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500230 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200231
232 return client;
233}
234
235static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300236surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
237{
238 struct weston_surface *surface =
239 container_of(listener, struct weston_surface,
240 pending.buffer_destroy_listener);
241
242 surface->pending.buffer = NULL;
243}
244
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200245static void
246empty_region(pixman_region32_t *region)
247{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300248 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200249 pixman_region32_init(region);
250}
251
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300252static void
253region_init_infinite(pixman_region32_t *region)
254{
255 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
256 UINT32_MAX, UINT32_MAX);
257}
258
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500259WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500260weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500261{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500262 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400263
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200264 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400265 if (surface == NULL)
266 return NULL;
267
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400268 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500269
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500270 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500271 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500272
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400273 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400274
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500275 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400276 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400277
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100278 if (compositor->renderer->create_surface(surface) < 0) {
279 free(surface);
280 return NULL;
281 }
282
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200283 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
284 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400285 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400286 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200287
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400288 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500289 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500290 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300291 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200292 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500293 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400294
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200295 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200296 wl_list_insert(&surface->geometry.transformation_list,
297 &surface->transform.position.link);
298 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200299 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200300 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500301
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300302 surface->pending.buffer_destroy_listener.notify =
303 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300304 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300305 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300306 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300307 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300308
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400309 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500310}
311
Alex Wu8811bf92012-02-28 18:07:54 +0800312WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500313weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200314 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500315{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100316 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500317}
318
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400319WL_EXPORT void
320weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200321 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200322{
323 if (surface->transform.enabled) {
324 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
325
326 weston_matrix_transform(&surface->transform.matrix, &v);
327
328 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200329 weston_log("warning: numerical instability in "
Scott Moreau088c62e2013-02-11 04:45:38 -0700330 "%s(), divisor = %g\n", __func__,
Pekka Paalanenece8a012012-02-08 15:23:15 +0200331 v.f[3]);
332 *x = 0;
333 *y = 0;
334 return;
335 }
336
337 *x = v.f[0] / v.f[3];
338 *y = v.f[1] / v.f[3];
339 } else {
340 *x = sx + surface->geometry.x;
341 *y = sy + surface->geometry.y;
342 }
343}
344
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500345WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200346weston_surface_to_buffer_float(struct weston_surface *surface,
347 float sx, float sy, float *bx, float *by)
348{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200349 weston_transformed_coord(surface->geometry.width,
350 surface->geometry.height,
351 surface->buffer_transform,
352 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200353}
354
355WL_EXPORT pixman_box32_t
356weston_surface_to_buffer_rect(struct weston_surface *surface,
357 pixman_box32_t rect)
358{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200359 return weston_transformed_rect(surface->geometry.width,
360 surface->geometry.height,
361 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200362}
363
364WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400365weston_surface_move_to_plane(struct weston_surface *surface,
366 struct weston_plane *plane)
367{
368 if (surface->plane == plane)
369 return;
370
371 weston_surface_damage_below(surface);
372 surface->plane = plane;
373 weston_surface_damage(surface);
374}
375
376WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500377weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200378{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500379 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200380
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500381 pixman_region32_init(&damage);
382 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
383 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400384 pixman_region32_union(&surface->plane->damage,
385 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500386 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200387}
388
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400389static struct wl_resource *
390find_resource_for_client(struct wl_list *list, struct wl_client *client)
391{
392 struct wl_resource *r;
393
394 wl_list_for_each(r, list, link) {
395 if (r->client == client)
396 return r;
397 }
398
399 return NULL;
400}
401
402static void
403weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
404{
405 uint32_t different = es->output_mask ^ mask;
406 uint32_t entered = mask & different;
407 uint32_t left = es->output_mask & different;
408 struct weston_output *output;
409 struct wl_resource *resource = NULL;
410 struct wl_client *client = es->surface.resource.client;
411
412 es->output_mask = mask;
413 if (es->surface.resource.client == NULL)
414 return;
415 if (different == 0)
416 return;
417
418 wl_list_for_each(output, &es->compositor->output_list, link) {
419 if (1 << output->id & different)
420 resource =
421 find_resource_for_client(&output->resource_list,
422 client);
423 if (resource == NULL)
424 continue;
425 if (1 << output->id & entered)
426 wl_surface_send_enter(&es->surface.resource, resource);
427 if (1 << output->id & left)
428 wl_surface_send_leave(&es->surface.resource, resource);
429 }
430}
431
432static void
433weston_surface_assign_output(struct weston_surface *es)
434{
435 struct weston_compositor *ec = es->compositor;
436 struct weston_output *output, *new_output;
437 pixman_region32_t region;
438 uint32_t max, area, mask;
439 pixman_box32_t *e;
440
441 new_output = NULL;
442 max = 0;
443 mask = 0;
444 pixman_region32_init(&region);
445 wl_list_for_each(output, &ec->output_list, link) {
446 pixman_region32_intersect(&region, &es->transform.boundingbox,
447 &output->region);
448
449 e = pixman_region32_extents(&region);
450 area = (e->x2 - e->x1) * (e->y2 - e->y1);
451
452 if (area > 0)
453 mask |= 1 << output->id;
454
455 if (area >= max) {
456 new_output = output;
457 max = area;
458 }
459 }
460 pixman_region32_fini(&region);
461
462 es->output = new_output;
463 weston_surface_update_output_mask(es, mask);
464}
465
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200466static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200467surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
468 int32_t width, int32_t height,
469 pixman_region32_t *bbox)
470{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200471 float min_x = HUGE_VALF, min_y = HUGE_VALF;
472 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200473 int32_t s[4][2] = {
474 { sx, sy },
475 { sx, sy + height },
476 { sx + width, sy },
477 { sx + width, sy + height }
478 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200479 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200480 int i;
481
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300482 if (width == 0 || height == 0) {
483 /* avoid rounding empty bbox to 1x1 */
484 pixman_region32_init(bbox);
485 return;
486 }
487
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200488 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200489 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400490 weston_surface_to_global_float(surface,
491 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200492 if (x < min_x)
493 min_x = x;
494 if (x > max_x)
495 max_x = x;
496 if (y < min_y)
497 min_y = y;
498 if (y > max_y)
499 max_y = y;
500 }
501
Pekka Paalanen219b9822012-02-08 15:38:37 +0200502 int_x = floorf(min_x);
503 int_y = floorf(min_y);
504 pixman_region32_init_rect(bbox, int_x, int_y,
505 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200506}
507
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200508static void
509weston_surface_update_transform_disable(struct weston_surface *surface)
510{
511 surface->transform.enabled = 0;
512
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200513 /* round off fractions when not transformed */
514 surface->geometry.x = roundf(surface->geometry.x);
515 surface->geometry.y = roundf(surface->geometry.y);
516
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200517 pixman_region32_init_rect(&surface->transform.boundingbox,
518 surface->geometry.x,
519 surface->geometry.y,
520 surface->geometry.width,
521 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500522
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400523 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500524 pixman_region32_copy(&surface->transform.opaque,
525 &surface->opaque);
526 pixman_region32_translate(&surface->transform.opaque,
527 surface->geometry.x,
528 surface->geometry.y);
529 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200530}
531
532static int
533weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200534{
535 struct weston_matrix *matrix = &surface->transform.matrix;
536 struct weston_matrix *inverse = &surface->transform.inverse;
537 struct weston_transform *tform;
538
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200539 surface->transform.enabled = 1;
540
541 /* Otherwise identity matrix, but with x and y translation. */
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +0300542 surface->transform.position.matrix.type = WESTON_MATRIX_TRANSFORM_TRANSLATE;
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200543 surface->transform.position.matrix.d[12] = surface->geometry.x;
544 surface->transform.position.matrix.d[13] = surface->geometry.y;
545
546 weston_matrix_init(matrix);
547 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
548 weston_matrix_multiply(matrix, &tform->matrix);
549
550 if (weston_matrix_invert(inverse, matrix) < 0) {
551 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200552 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200553 " transformation not invertible.\n", surface);
554 return -1;
555 }
556
557 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
558 surface->geometry.height,
559 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500560
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200561 return 0;
562}
563
564WL_EXPORT void
565weston_surface_update_transform(struct weston_surface *surface)
566{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200567 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200568 return;
569
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200570 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200571
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500572 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200573
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200574 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500575 pixman_region32_fini(&surface->transform.opaque);
576 pixman_region32_init(&surface->transform.opaque);
577
Pekka Paalanencd403622012-01-25 13:37:39 +0200578 /* transform.position is always in transformation_list */
579 if (surface->geometry.transformation_list.next ==
580 &surface->transform.position.link &&
581 surface->geometry.transformation_list.prev ==
582 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200583 weston_surface_update_transform_disable(surface);
584 } else {
585 if (weston_surface_update_transform_enable(surface) < 0)
586 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200587 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200588
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400589 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200590
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300591 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200592}
593
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200594WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100595weston_surface_to_global_fixed(struct weston_surface *surface,
596 wl_fixed_t sx, wl_fixed_t sy,
597 wl_fixed_t *x, wl_fixed_t *y)
598{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200599 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100600
601 weston_surface_to_global_float(surface,
602 wl_fixed_to_double(sx),
603 wl_fixed_to_double(sy),
604 &xf, &yf);
605 *x = wl_fixed_from_double(xf);
606 *y = wl_fixed_from_double(yf);
607}
608
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400609WL_EXPORT void
610weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200611 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200612{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200613 if (surface->transform.enabled) {
614 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
615
616 weston_matrix_transform(&surface->transform.inverse, &v);
617
618 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200619 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200620 "weston_surface_from_global(), divisor = %g\n",
621 v.f[3]);
622 *sx = 0;
623 *sy = 0;
624 return;
625 }
626
Pekka Paalanencd403622012-01-25 13:37:39 +0200627 *sx = v.f[0] / v.f[3];
628 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200629 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200630 *sx = x - surface->geometry.x;
631 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200632 }
633}
634
635WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100636weston_surface_from_global_fixed(struct weston_surface *surface,
637 wl_fixed_t x, wl_fixed_t y,
638 wl_fixed_t *sx, wl_fixed_t *sy)
639{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200640 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100641
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400642 weston_surface_from_global_float(surface,
643 wl_fixed_to_double(x),
644 wl_fixed_to_double(y),
645 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100646 *sx = wl_fixed_from_double(sxf);
647 *sy = wl_fixed_from_double(syf);
648}
649
650WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200651weston_surface_from_global(struct weston_surface *surface,
652 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
653{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200654 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200655
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400656 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200657 *sx = floorf(sxf);
658 *sy = floorf(syf);
659}
660
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400661WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400662weston_surface_schedule_repaint(struct weston_surface *surface)
663{
664 struct weston_output *output;
665
666 wl_list_for_each(output, &surface->compositor->output_list, link)
667 if (surface->output_mask & (1 << output->id))
668 weston_output_schedule_repaint(output);
669}
670
671WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500672weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500673{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400674 pixman_region32_union_rect(&surface->damage, &surface->damage,
675 0, 0, surface->geometry.width,
676 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200677
Kristian Høgsberg98238702012-08-03 16:29:12 -0400678 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500679}
680
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400681WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500682weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200683 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400684{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200685 surface->geometry.x = x;
686 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200687 surface->geometry.width = width;
688 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200689 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400690}
691
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200692WL_EXPORT void
693weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200694 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200695{
696 surface->geometry.x = x;
697 surface->geometry.y = y;
698 surface->geometry.dirty = 1;
699}
700
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300701WL_EXPORT int
702weston_surface_is_mapped(struct weston_surface *surface)
703{
704 if (surface->output)
705 return 1;
706 else
707 return 0;
708}
709
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200710WL_EXPORT int32_t
711weston_surface_buffer_width(struct weston_surface *surface)
712{
713 switch (surface->buffer_transform) {
714 case WL_OUTPUT_TRANSFORM_90:
715 case WL_OUTPUT_TRANSFORM_270:
716 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
717 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200718 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200719 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200720 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200721 }
722}
723
724WL_EXPORT int32_t
725weston_surface_buffer_height(struct weston_surface *surface)
726{
727 switch (surface->buffer_transform) {
728 case WL_OUTPUT_TRANSFORM_90:
729 case WL_OUTPUT_TRANSFORM_270:
730 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
731 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200732 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200733 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200734 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200735 }
736}
737
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400738WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500739weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500740{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400741 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500742
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400743 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500744
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400745 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500746}
747
Tiago Vignatti9d393522012-02-10 16:26:19 +0200748static struct weston_surface *
749weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100750 wl_fixed_t x, wl_fixed_t y,
751 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200752{
753 struct weston_surface *surface;
754
755 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100756 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500757 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100758 wl_fixed_to_int(*sx),
759 wl_fixed_to_int(*sy),
760 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200761 return surface;
762 }
763
764 return NULL;
765}
766
767static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400768weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500769{
Scott Moreau447013d2012-02-18 05:05:29 -0700770 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500771 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400772 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500773
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400774 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100775 return;
776
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400777 surface = weston_compositor_pick_surface(seat->compositor,
778 pointer->x,
779 pointer->y,
780 &pointer->current_x,
781 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500782
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400783 if (&surface->surface != pointer->current) {
784 interface = pointer->grab->interface;
785 pointer->current = &surface->surface;
786 interface->focus(pointer->grab, &surface->surface,
787 pointer->current_x,
788 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500789 }
790
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400791 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500792 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100793 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400794 pointer->x,
795 pointer->y,
796 &pointer->grab->x,
797 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500798}
799
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400800static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500801weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400802{
Daniel Stone37816df2012-05-16 18:45:18 +0100803 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400804
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500805 if (!compositor->focus)
806 return;
807
Daniel Stone37816df2012-05-16 18:45:18 +0100808 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400809 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400810}
811
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400812WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500813weston_surface_unmap(struct weston_surface *surface)
814{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100815 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500816
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500817 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500818 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500819 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500820
Daniel Stone4dab5db2012-05-30 16:31:53 +0100821 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100822 if (seat->seat.keyboard &&
823 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100824 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100825 if (seat->seat.pointer &&
826 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100827 wl_pointer_set_focus(seat->seat.pointer,
828 NULL,
829 wl_fixed_from_int(0),
830 wl_fixed_from_int(0));
831 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500832
Kristian Høgsberg98238702012-08-03 16:29:12 -0400833 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500834}
835
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400836struct weston_frame_callback {
837 struct wl_resource resource;
838 struct wl_list link;
839};
840
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500841static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400842destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500843{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500844 struct weston_surface *surface =
845 container_of(resource,
846 struct weston_surface, surface.resource);
847 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400848 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400849
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300850 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500851 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400852
Pekka Paalanenbc106382012-10-10 12:49:31 +0300853 wl_list_for_each_safe(cb, next,
854 &surface->pending.frame_callback_list, link)
855 wl_resource_destroy(&cb->resource);
856
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300857 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300858 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300859 pixman_region32_fini(&surface->pending.damage);
860
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300861 if (surface->pending.buffer)
862 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
863
Pekka Paalanende685b82012-12-04 15:58:12 +0200864 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400865
Kristian Høgsberg42263852012-09-06 21:59:29 -0400866 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200867
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200868 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200869 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500870 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500871 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300872 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200873
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400874 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
875 wl_resource_destroy(&cb->resource);
876
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400877 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500878}
879
Alex Wu8811bf92012-02-28 18:07:54 +0800880WL_EXPORT void
881weston_surface_destroy(struct weston_surface *surface)
882{
883 /* Not a valid way to destroy a client surface */
884 assert(surface->surface.resource.client == NULL);
885
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400886 wl_signal_emit(&surface->surface.resource.destroy_signal,
887 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800888 destroy_surface(&surface->surface.resource);
889}
890
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100891static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200892weston_buffer_reference_handle_destroy(struct wl_listener *listener,
893 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100894{
Pekka Paalanende685b82012-12-04 15:58:12 +0200895 struct weston_buffer_reference *ref =
896 container_of(listener, struct weston_buffer_reference,
897 destroy_listener);
898
899 assert((struct wl_buffer *)data == ref->buffer);
900 ref->buffer = NULL;
901}
902
903WL_EXPORT void
904weston_buffer_reference(struct weston_buffer_reference *ref,
905 struct wl_buffer *buffer)
906{
907 if (ref->buffer && buffer != ref->buffer) {
908 weston_buffer_post_release(ref->buffer);
909 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300910 }
911
Pekka Paalanende685b82012-12-04 15:58:12 +0200912 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400913 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300914 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200915 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200916 }
917
Pekka Paalanende685b82012-12-04 15:58:12 +0200918 ref->buffer = buffer;
919 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
920}
921
922static void
923weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
924{
925 weston_buffer_reference(&surface->buffer_ref, buffer);
926
Pekka Paalanena6421c42012-12-04 15:58:10 +0200927 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300928 if (weston_surface_is_mapped(surface))
929 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300930 }
931
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300932 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100933}
934
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500935WL_EXPORT void
936weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400937{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500938 wl_list_remove(&surface->layer_link);
939 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500940 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500941 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400942}
943
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400944WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500945weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500947 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400948
949 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400951}
952
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500953WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500954weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200955{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400956 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200957 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200958
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400959 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500960 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200961}
962
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400963WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500964weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500966 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400967
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400968 pixman_region32_union(&compositor->primary_plane.damage,
969 &compositor->primary_plane.damage,
970 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400971 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400972}
973
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400974static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500975fade_frame(struct weston_animation *animation,
976 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400977{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500978 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400979 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500980 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500981 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400982
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600983 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600984 compositor->fade.spring.timestamp = msecs;
985
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500986 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500987 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500988 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
989 compositor->fade.spring.current);
990 weston_surface_damage(surface);
991
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500992 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400993 compositor->fade.spring.current =
994 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400995 wl_list_remove(&animation->link);
996 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200997
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500998 if (compositor->fade.spring.current < 0.001) {
999 destroy_surface(&surface->surface.resource);
1000 compositor->fade.surface = NULL;
1001 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001003 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001004 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001005 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001006}
1007
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001008static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001009surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001010 pixman_region32_t *opaque)
1011{
Pekka Paalanende685b82012-12-04 15:58:12 +02001012 if (surface->buffer_ref.buffer &&
1013 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001014 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001015
1016 if (surface->transform.enabled) {
1017 pixman_box32_t *extents;
1018
1019 extents = pixman_region32_extents(&surface->damage);
1020 surface_compute_bbox(surface, extents->x1, extents->y1,
1021 extents->x2 - extents->x1,
1022 extents->y2 - extents->y1,
1023 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001024 pixman_region32_translate(&surface->damage,
1025 -surface->plane->x,
1026 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001027 } else {
1028 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001029 surface->geometry.x - surface->plane->x,
1030 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001031 }
1032
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001033 pixman_region32_subtract(&surface->damage,
1034 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001035 pixman_region32_union(&surface->plane->damage,
1036 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001037 empty_region(&surface->damage);
1038 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001039 pixman_region32_union(&surface->plane->opaque,
1040 &surface->plane->opaque,
1041 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001042 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001043}
1044
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001045static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001046weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001047{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001048 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001049 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001050 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001051 struct weston_animation *animation, *next;
1052 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001053 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001054 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001055
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001056 weston_compositor_update_drag_surfaces(ec);
1057
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001058 /* Rebuild the surface list and update surface transforms up front. */
1059 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001060 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001061 wl_list_for_each(layer, &ec->layer_list, link) {
1062 wl_list_for_each(es, &layer->surface_list, layer_link) {
1063 weston_surface_update_transform(es);
1064 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001065 if (es->output == output) {
1066 wl_list_insert_list(&frame_callback_list,
1067 &es->frame_callback_list);
1068 wl_list_init(&es->frame_callback_list);
1069 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001070 }
1071 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001072
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001073 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001074 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001075 else
1076 wl_list_for_each(es, &ec->surface_list, link)
1077 weston_surface_move_to_plane(es, &ec->primary_plane);
1078
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001079 pixman_region32_init(&opaque);
1080
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001081 pixman_region32_fini(&ec->primary_plane.opaque);
1082 pixman_region32_init(&ec->primary_plane.opaque);
1083
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001084 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001085 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001086
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001087 /* Both the renderer and the backend have seen the buffer
1088 * by now. If renderer needs the buffer, it has its own
1089 * reference set. If the backend wants to keep the buffer
1090 * around for migrating the surface into a non-primary plane
1091 * later, keep_buffer is true. Otherwise, drop the core
1092 * reference now, and allow early buffer release. This enables
1093 * clients to use single-buffering.
1094 */
1095 if (!es->keep_buffer)
1096 weston_buffer_reference(&es->buffer_ref, NULL);
1097 }
1098
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001099 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001100
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001101 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001102 pixman_region32_intersect(&output_damage,
1103 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001104
Scott Moreauccbf29d2012-02-22 14:21:41 -07001105 if (output->dirty)
1106 weston_output_update_matrix(output);
1107
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001108 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001109
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001110 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001111
Kristian Høgsbergef044142011-06-21 15:02:12 -04001112 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001113
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001114 weston_compositor_repick(ec);
1115 wl_event_loop_dispatch(ec->input_loop, 0);
1116
Jonas Ådahldb773762012-06-13 00:01:21 +02001117 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001118 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001119 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001120 }
1121
Scott Moreaud64cf212012-06-08 19:40:54 -06001122 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001123 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001124 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001125 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001126}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001127
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001128static int
1129weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001130{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001131 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001132
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001133 wl_event_loop_dispatch(compositor->input_loop, 0);
1134
1135 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001136}
1137
1138WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001139weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001140{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001141 struct weston_compositor *compositor = output->compositor;
1142 struct wl_event_loop *loop =
1143 wl_display_get_event_loop(compositor->wl_display);
1144 int fd;
1145
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001146 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001147 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001148 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001149 return;
1150 }
1151
1152 output->repaint_scheduled = 0;
1153 if (compositor->input_loop_source)
1154 return;
1155
1156 fd = wl_event_loop_get_fd(compositor->input_loop);
1157 compositor->input_loop_source =
1158 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1159 weston_compositor_read_input, compositor);
1160}
1161
1162static void
1163idle_repaint(void *data)
1164{
1165 struct weston_output *output = data;
1166
1167 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001168}
1169
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001170WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001171weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1172{
1173 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001174 if (below != NULL)
1175 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001176}
1177
1178WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001179weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001180{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001181 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001182 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001183
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001184 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001185 return;
1186
Kristian Høgsbergef044142011-06-21 15:02:12 -04001187 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001188 output->repaint_needed = 1;
1189 if (output->repaint_scheduled)
1190 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001191
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001192 wl_event_loop_add_idle(loop, idle_repaint, output);
1193 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001194
1195 if (compositor->input_loop_source) {
1196 wl_event_source_remove(compositor->input_loop_source);
1197 compositor->input_loop_source = NULL;
1198 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001199}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001200
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001201WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001202weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1203{
1204 struct weston_output *output;
1205
1206 wl_list_for_each(output, &compositor->output_list, link)
1207 weston_output_schedule_repaint(output);
1208}
1209
1210WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001211weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001212{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001213 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001214 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001215
Scott Moreau9d1b1122012-06-08 19:40:53 -06001216 output = container_of(compositor->output_list.next,
1217 struct weston_output, link);
1218
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001219 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001220 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001221 return;
1222
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001223 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001224 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001225 if (!surface)
1226 return;
1227
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001228 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001229 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001230 wl_list_insert(&compositor->fade_layer.surface_list,
1231 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001232 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001233 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001234 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001235 }
1236
1237 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001238 if (wl_list_empty(&compositor->fade.animation.link)) {
1239 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001240 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001241 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001242 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001243}
1244
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001245static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001246surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001247{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001248 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001249}
1250
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001251static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001252surface_attach(struct wl_client *client,
1253 struct wl_resource *resource,
1254 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1255{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001256 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001257 struct wl_buffer *buffer = NULL;
1258
1259 if (buffer_resource)
1260 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001261
Pekka Paalanende685b82012-12-04 15:58:12 +02001262 /* Attach, attach, without commit in between does not send
1263 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001264 if (surface->pending.buffer)
1265 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001266
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001267 surface->pending.sx = sx;
1268 surface->pending.sy = sy;
1269 surface->pending.buffer = buffer;
1270 if (buffer) {
1271 wl_signal_add(&buffer->resource.destroy_signal,
1272 &surface->pending.buffer_destroy_listener);
1273 surface->pending.remove_contents = 0;
1274 } else {
1275 surface->pending.remove_contents = 1;
1276 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001277}
1278
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001279static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001280surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001281 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001282 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001283{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001284 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001285
Pekka Paalanen8e159182012-10-10 12:49:25 +03001286 pixman_region32_union_rect(&surface->pending.damage,
1287 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001288 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001289}
1290
Kristian Høgsberg33418202011-08-16 23:01:28 -04001291static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001292destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001293{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001294 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001295
1296 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001297 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001298}
1299
1300static void
1301surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001302 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001303{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001304 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001305 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001306
1307 cb = malloc(sizeof *cb);
1308 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001309 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001310 return;
1311 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001312
Kristian Høgsberg33418202011-08-16 23:01:28 -04001313 cb->resource.object.interface = &wl_callback_interface;
1314 cb->resource.object.id = callback;
1315 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001316 cb->resource.client = client;
1317 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001318
1319 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001320 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001321}
1322
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001323static void
1324surface_set_opaque_region(struct wl_client *client,
1325 struct wl_resource *resource,
1326 struct wl_resource *region_resource)
1327{
1328 struct weston_surface *surface = resource->data;
1329 struct weston_region *region;
1330
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001331 if (region_resource) {
1332 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001333 pixman_region32_copy(&surface->pending.opaque,
1334 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001335 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001336 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001338}
1339
1340static void
1341surface_set_input_region(struct wl_client *client,
1342 struct wl_resource *resource,
1343 struct wl_resource *region_resource)
1344{
1345 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001346 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001347
1348 if (region_resource) {
1349 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001350 pixman_region32_copy(&surface->pending.input,
1351 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001352 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001353 pixman_region32_fini(&surface->pending.input);
1354 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001355 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001356}
1357
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001358static int
1359surface_pending_buffer_has_different_size(struct weston_surface *surface)
1360{
1361 int width, height;
1362
1363 switch (surface->pending.buffer_transform) {
1364 case WL_OUTPUT_TRANSFORM_90:
1365 case WL_OUTPUT_TRANSFORM_270:
1366 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1367 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1368 height = surface->pending.buffer->width;
1369 width = surface->pending.buffer->height;
1370 break;
1371 default:
1372 width = surface->pending.buffer->width;
1373 height = surface->pending.buffer->height;
1374 }
1375
1376 if (width == surface->geometry.width &&
1377 height == surface->geometry.height)
1378 return 0;
1379 else
1380 return 1;
1381}
1382
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001383static void
1384surface_commit(struct wl_client *client, struct wl_resource *resource)
1385{
1386 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001387 pixman_region32_t opaque;
1388
1389 if (surface->pending.sx || surface->pending.sy ||
1390 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001391 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001392 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001393
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001394 /* wl_surface.set_buffer_rotation */
1395 surface->buffer_transform = surface->pending.buffer_transform;
1396
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001397 /* wl_surface.attach */
1398 if (surface->pending.buffer || surface->pending.remove_contents)
1399 weston_surface_attach(surface, surface->pending.buffer);
1400
Pekka Paalanende685b82012-12-04 15:58:12 +02001401 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001402 surface->configure(surface, surface->pending.sx,
1403 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001404 surface->pending.sx = 0;
1405 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001406
1407 /* wl_surface.damage */
1408 pixman_region32_union(&surface->damage, &surface->damage,
1409 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001410 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1411 0, 0,
1412 surface->geometry.width,
1413 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001414 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001415
1416 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001417 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001418 surface->geometry.width,
1419 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001420 pixman_region32_intersect(&opaque,
1421 &opaque, &surface->pending.opaque);
1422
1423 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1424 pixman_region32_copy(&surface->opaque, &opaque);
1425 surface->geometry.dirty = 1;
1426 }
1427
1428 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001429
1430 /* wl_surface.set_input_region */
1431 pixman_region32_fini(&surface->input);
1432 pixman_region32_init_rect(&surface->input, 0, 0,
1433 surface->geometry.width,
1434 surface->geometry.height);
1435 pixman_region32_intersect(&surface->input,
1436 &surface->input, &surface->pending.input);
1437
Pekka Paalanenbc106382012-10-10 12:49:31 +03001438 /* wl_surface.frame */
1439 wl_list_insert_list(&surface->frame_callback_list,
1440 &surface->pending.frame_callback_list);
1441 wl_list_init(&surface->pending.frame_callback_list);
1442
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001443 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001444}
1445
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001446static void
1447surface_set_buffer_transform(struct wl_client *client,
1448 struct wl_resource *resource, int transform)
1449{
1450 struct weston_surface *surface = resource->data;
1451
1452 surface->pending.buffer_transform = transform;
1453}
1454
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001455static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001456 surface_destroy,
1457 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001458 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001459 surface_frame,
1460 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001461 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001462 surface_commit,
1463 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001464};
1465
1466static void
1467compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001468 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001469{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001470 struct weston_compositor *ec = resource->data;
1471 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001472
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001473 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001474 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001475 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001476 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001477 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001478
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001479 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001480
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001481 surface->surface.resource.object.id = id;
1482 surface->surface.resource.object.interface = &wl_surface_interface;
1483 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001484 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001485 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001486
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001487 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001488}
1489
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001490static void
1491destroy_region(struct wl_resource *resource)
1492{
1493 struct weston_region *region =
1494 container_of(resource, struct weston_region, resource);
1495
1496 pixman_region32_fini(&region->region);
1497 free(region);
1498}
1499
1500static void
1501region_destroy(struct wl_client *client, struct wl_resource *resource)
1502{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001503 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001504}
1505
1506static void
1507region_add(struct wl_client *client, struct wl_resource *resource,
1508 int32_t x, int32_t y, int32_t width, int32_t height)
1509{
1510 struct weston_region *region = resource->data;
1511
1512 pixman_region32_union_rect(&region->region, &region->region,
1513 x, y, width, height);
1514}
1515
1516static void
1517region_subtract(struct wl_client *client, struct wl_resource *resource,
1518 int32_t x, int32_t y, int32_t width, int32_t height)
1519{
1520 struct weston_region *region = resource->data;
1521 pixman_region32_t rect;
1522
1523 pixman_region32_init_rect(&rect, x, y, width, height);
1524 pixman_region32_subtract(&region->region, &region->region, &rect);
1525 pixman_region32_fini(&rect);
1526}
1527
1528static const struct wl_region_interface region_interface = {
1529 region_destroy,
1530 region_add,
1531 region_subtract
1532};
1533
1534static void
1535compositor_create_region(struct wl_client *client,
1536 struct wl_resource *resource, uint32_t id)
1537{
1538 struct weston_region *region;
1539
1540 region = malloc(sizeof *region);
1541 if (region == NULL) {
1542 wl_resource_post_no_memory(resource);
1543 return;
1544 }
1545
1546 region->resource.destroy = destroy_region;
1547
1548 region->resource.object.id = id;
1549 region->resource.object.interface = &wl_region_interface;
1550 region->resource.object.implementation =
1551 (void (**)(void)) &region_interface;
1552 region->resource.data = region;
1553
1554 pixman_region32_init(&region->region);
1555
1556 wl_client_add_resource(client, &region->resource);
1557}
1558
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001559static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001560 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001561 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001562};
1563
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001564WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001565weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001566{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001567 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1568 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001569
1570 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001571 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001572}
1573
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001574static void
1575weston_compositor_dpms_on(struct weston_compositor *compositor)
1576{
1577 struct weston_output *output;
1578
1579 wl_list_for_each(output, &compositor->output_list, link)
1580 if (output->set_dpms)
1581 output->set_dpms(output, WESTON_DPMS_ON);
1582}
1583
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001584WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001585weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001586{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001587 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1588 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001589 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001590 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001591 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001592 }
1593}
1594
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001595static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001596weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001597{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001598 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001599 compositor->idle_inhibit++;
1600}
1601
1602static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001603weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001604{
1605 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001606 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001607}
1608
1609static int
1610idle_handler(void *data)
1611{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001612 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001613
1614 if (compositor->idle_inhibit)
1615 return 1;
1616
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001617 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001618
1619 return 1;
1620}
1621
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001622WL_EXPORT void
1623weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1624{
1625 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001626 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001627 plane->x = x;
1628 plane->y = y;
1629}
1630
1631WL_EXPORT void
1632weston_plane_release(struct weston_plane *plane)
1633{
1634 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001635 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001636}
1637
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001638static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001639weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001640
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001641static void
Daniel Stone37816df2012-05-16 18:45:18 +01001642clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001643{
Daniel Stone37816df2012-05-16 18:45:18 +01001644 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001645 struct weston_output *output, *prev = NULL;
1646 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001647
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001648 x = wl_fixed_to_int(*fx);
1649 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001650 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1651 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001652
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001653 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001654 if (pixman_region32_contains_point(&output->region,
1655 x, y, NULL))
1656 valid = 1;
1657 if (pixman_region32_contains_point(&output->region,
1658 old_x, old_y, NULL))
1659 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001660 }
Daniel Stone37816df2012-05-16 18:45:18 +01001661
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001662 if (!valid) {
1663 if (x < prev->x)
1664 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001665 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001666 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001667 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001668 if (y < prev->y)
1669 *fy = wl_fixed_from_int(prev->y);
1670 else if (y >= prev->y + prev->current->height)
1671 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001672 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001673 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001674}
1675
1676WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001677notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001678{
1679 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001680 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001681 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001682 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001683 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001684
1685 weston_compositor_activity(ec);
1686
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001687 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001688
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001689 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001690
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001691 pointer->x = x;
1692 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001693
1694 ix = wl_fixed_to_int(x);
1695 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001696
Scott Moreauccbf29d2012-02-22 14:21:41 -07001697 wl_list_for_each(output, &ec->output_list, link)
1698 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001699 pixman_region32_contains_point(&output->region,
1700 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001701 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001702
Daniel Stone37816df2012-05-16 18:45:18 +01001703 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001704 interface = pointer->grab->interface;
1705 interface->motion(pointer->grab, time,
1706 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001707
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001708 if (seat->sprite) {
1709 weston_surface_set_position(seat->sprite,
1710 ix - seat->hotspot_x,
1711 iy - seat->hotspot_y);
1712 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001713 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001714}
1715
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001716WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001718 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001719{
Daniel Stone37816df2012-05-16 18:45:18 +01001720 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001721
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001722 if (seat->seat.keyboard) {
1723 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1724 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001725 }
1726
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001727 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001728}
1729
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001730WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001731notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001732 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001733{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001734 struct weston_compositor *compositor = seat->compositor;
1735 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001736 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001737 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001738 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001739
Daniel Stone4dbadb12012-05-30 16:31:51 +01001740 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001741 if (compositor->ping_handler && focus)
1742 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001743 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001744 if (pointer->button_count == 0) {
1745 pointer->grab_button = button;
1746 pointer->grab_time = time;
1747 pointer->grab_x = pointer->x;
1748 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001749 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001750 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001751 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001752 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001753 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001754 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001755
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001756 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001757 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001758
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001759 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001760
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001761 if (pointer->button_count == 1)
1762 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001763 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001764}
1765
Scott Moreau210d0792012-03-22 10:47:01 -06001766WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001767notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001768 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001769{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001770 struct weston_compositor *compositor = seat->compositor;
1771 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001772 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001773 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001774 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1775
1776 if (compositor->ping_handler && focus)
1777 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001778
1779 weston_compositor_activity(compositor);
1780
Scott Moreau6a3633d2012-03-20 08:47:59 -06001781 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001782 weston_compositor_run_axis_binding(compositor, seat,
1783 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001784 else
1785 return;
1786
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001787 if (pointer->focus_resource)
1788 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001789 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001790}
1791
Daniel Stone05d58682012-06-22 13:21:38 +01001792WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001793notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001794{
Daniel Stone05d58682012-06-22 13:21:38 +01001795 struct wl_keyboard *keyboard = &seat->keyboard;
1796 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001797 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001798 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001799 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001800 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001801
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001802 /* Serialize and update our internal state, checking to see if it's
1803 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001804 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1805 XKB_STATE_DEPRESSED);
1806 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1807 XKB_STATE_LATCHED);
1808 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1809 XKB_STATE_LOCKED);
1810 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001811 XKB_STATE_EFFECTIVE);
1812
Daniel Stone71c38772012-06-22 13:21:30 +01001813 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1814 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1815 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1816 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001817 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001818
Daniel Stone71c38772012-06-22 13:21:30 +01001819 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1820 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1821 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1822 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001823
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001824 /* And update the modifier_state for bindings. */
1825 mods_lookup = mods_depressed | mods_latched;
1826 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001827 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001828 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001829 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001830 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001831 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001832 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001833 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1834 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001835
Daniel Stone8c8164f2012-05-30 16:31:45 +01001836 /* Finally, notify the compositor that LEDs have changed. */
1837 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001838 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001839 leds |= LED_NUM_LOCK;
1840 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001841 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001842 leds |= LED_CAPS_LOCK;
1843 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001844 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001845 leds |= LED_SCROLL_LOCK;
1846 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001847 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001848 seat->xkb_state.leds = leds;
1849
Daniel Stone05d58682012-06-22 13:21:38 +01001850 if (changed) {
1851 grab->interface->modifiers(grab,
1852 serial,
1853 keyboard->modifiers.mods_depressed,
1854 keyboard->modifiers.mods_latched,
1855 keyboard->modifiers.mods_locked,
1856 keyboard->modifiers.group);
1857 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001858}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001859
Daniel Stone05d58682012-06-22 13:21:38 +01001860static void
1861update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001862 enum wl_keyboard_key_state state)
1863{
1864 enum xkb_key_direction direction;
1865
1866 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1867 direction = XKB_KEY_DOWN;
1868 else
1869 direction = XKB_KEY_UP;
1870
1871 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1872 * broken keycode system, which starts at 8. */
1873 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1874
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001875 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001876}
1877
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001878WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001879notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001880 enum wl_keyboard_key_state state,
1881 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001882{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001883 struct weston_compositor *compositor = seat->compositor;
1884 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001885 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001886 (struct weston_surface *) keyboard->focus;
1887 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001888 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001889 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001890
Daniel Stonec9785ea2012-05-30 16:31:52 +01001891 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001892 if (compositor->ping_handler && focus)
1893 compositor->ping_handler(focus, serial);
1894
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001895 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001896 keyboard->grab_key = key;
1897 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001898 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001899 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001900 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001901
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001902 end = keyboard->keys.data + keyboard->keys.size;
1903 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001904 if (*k == key) {
1905 /* Ignore server-generated repeats. */
1906 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1907 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001908 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001909 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001910 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001912 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001913 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001914 *k = key;
1915 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001916
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001917 if (grab == &keyboard->default_grab) {
1918 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001919 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001920 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001921 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001922
Daniel Stone7ace3902012-05-30 16:31:40 +01001923 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001924
1925 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001926 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001927 wl_display_get_serial(compositor->wl_display),
1928 key,
1929 state);
1930 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001931}
1932
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001933WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001934notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001935 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001936{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001937 struct weston_compositor *compositor = seat->compositor;
1938 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001939
1940 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001941 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001942 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001943 x - pointer->x,
1944 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001945
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001946 pointer->x = x;
1947 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001948 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001949 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001950 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001951 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001952 /* FIXME: We should call wl_pointer_set_focus(seat,
1953 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001954 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001955}
1956
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001957static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001958destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001959{
Daniel Stone37816df2012-05-16 18:45:18 +01001960 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001961
Daniel Stone37816df2012-05-16 18:45:18 +01001962 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001963 saved_kbd_focus_listener);
1964
Daniel Stone37816df2012-05-16 18:45:18 +01001965 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001966}
1967
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001968WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001969notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001970 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001971{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001972 struct weston_compositor *compositor = seat->compositor;
1973 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001974 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001975 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001976
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001977 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001978 wl_array_copy(&keyboard->keys, keys);
1979 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001980 weston_compositor_idle_inhibit(compositor);
1981 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001982 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001983 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001984 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001985
Daniel Stoneef172672012-06-22 13:21:32 +01001986 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001987 wl_array_for_each(k, &keyboard->keys) {
1988 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001989 WL_KEYBOARD_KEY_STATE_PRESSED);
1990 }
1991
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001992 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001993
1994 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001995 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1996 wl_keyboard_set_focus(keyboard, surface);
1997 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001998 }
1999}
2000
2001WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002002notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002003{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002004 struct weston_compositor *compositor = seat->compositor;
2005 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002006 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002007
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002008 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002009 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002010 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002011 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002012 WL_KEYBOARD_KEY_STATE_RELEASED);
2013 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002014
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002015 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002016
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002017 if (keyboard->focus) {
2018 seat->saved_kbd_focus = keyboard->focus;
2019 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002020 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002021 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2022 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002023 }
2024
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002025 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002026 /* FIXME: We really need keyboard grab cancel here to
2027 * let the grab shut down properly. As it is we leak
2028 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002029 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002030}
2031
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002032static void
Daniel Stone37816df2012-05-16 18:45:18 +01002033touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002034{
Daniel Stone37816df2012-05-16 18:45:18 +01002035 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002036 struct wl_resource *resource;
2037
Pekka Paalanen56464252012-07-31 13:21:08 +03002038 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002039 return;
2040
Pekka Paalanen56464252012-07-31 13:21:08 +03002041 if (seat->touch->focus_resource)
2042 wl_list_remove(&seat->touch->focus_listener.link);
2043 seat->touch->focus = NULL;
2044 seat->touch->focus_resource = NULL;
2045
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002046 if (surface) {
2047 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002048 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002049 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002050 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002051 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002052 return;
2053 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002054
Daniel Stone37816df2012-05-16 18:45:18 +01002055 seat->touch->focus = surface;
2056 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002057 wl_signal_add(&resource->destroy_signal,
2058 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002059 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002060}
2061
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002062/**
2063 * notify_touch - emulates button touches and notifies surfaces accordingly.
2064 *
2065 * It assumes always the correct cycle sequence until it gets here: touch_down
2066 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2067 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002068 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002069 */
2070WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002071notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002072 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002073{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002074 struct weston_compositor *ec = seat->compositor;
2075 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002076 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002077 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002078 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002079
2080 /* Update grab's global coordinates. */
2081 touch->grab_x = x;
2082 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002083
2084 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002085 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002086 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002087
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002088 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002089
2090 /* the first finger down picks the surface, and all further go
2091 * to that surface for the remainder of the touch session i.e.
2092 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002093 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002094 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002095 touch_set_focus(seat, &es->surface);
2096 } else if (touch->focus) {
2097 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002098 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002099 } else {
2100 /* Unexpected condition: We have non-initial touch but
2101 * there is no focused surface.
2102 */
2103 weston_log("touch event received with %d points down"
2104 "but no surface focused\n", seat->num_tp);
2105 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002106 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002107
Matt Roper47c1b982012-10-10 16:56:53 -07002108 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002109 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002110 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002111 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002112 if (!es)
2113 break;
2114
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002115 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002116 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002117 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002118 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002119 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002120 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002121
Matt Roper47c1b982012-10-10 16:56:53 -07002122 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002123 if (seat->num_tp == 0)
2124 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002125 break;
2126 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002127}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002128
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002129static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002130pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2131{
2132 struct weston_seat *seat = container_of(listener, struct weston_seat,
2133 sprite_destroy_listener);
2134
2135 seat->sprite = NULL;
2136}
2137
2138static void
2139pointer_cursor_surface_configure(struct weston_surface *es,
2140 int32_t dx, int32_t dy)
2141{
2142 struct weston_seat *seat = es->private;
2143 int x, y;
2144
2145 assert(es == seat->sprite);
2146
2147 seat->hotspot_x -= dx;
2148 seat->hotspot_y -= dy;
2149
2150 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2151 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2152
2153 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002154 es->buffer_ref.buffer->width,
2155 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002156
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002157 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002158
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002159 if (!weston_surface_is_mapped(es)) {
2160 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2161 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002162 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002163 }
2164}
2165
2166static void
2167pointer_unmap_sprite(struct weston_seat *seat)
2168{
2169 if (weston_surface_is_mapped(seat->sprite))
2170 weston_surface_unmap(seat->sprite);
2171
2172 wl_list_remove(&seat->sprite_destroy_listener.link);
2173 seat->sprite->configure = NULL;
2174 seat->sprite->private = NULL;
2175 seat->sprite = NULL;
2176}
2177
2178static void
2179pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2180 uint32_t serial, struct wl_resource *surface_resource,
2181 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002182{
Daniel Stone37816df2012-05-16 18:45:18 +01002183 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002184 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002185
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002186 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002187 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002188
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002189 if (seat->seat.pointer->focus == NULL)
2190 return;
2191 if (seat->seat.pointer->focus->resource.client != client)
2192 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002193 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002194 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002195
2196 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002197 if (surface->configure) {
2198 wl_resource_post_error(&surface->surface.resource,
2199 WL_DISPLAY_ERROR_INVALID_OBJECT,
2200 "surface->configure already "
2201 "set");
2202 return;
2203 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002204 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002205
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002206 if (seat->sprite)
2207 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002208
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002209 if (!surface)
2210 return;
2211
2212 wl_signal_add(&surface->surface.resource.destroy_signal,
2213 &seat->sprite_destroy_listener);
2214
2215 surface->configure = pointer_cursor_surface_configure;
2216 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002217 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002218 seat->hotspot_x = x;
2219 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002220
Pekka Paalanende685b82012-12-04 15:58:12 +02002221 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002222 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002223}
2224
Daniel Stone37816df2012-05-16 18:45:18 +01002225static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002226 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002227};
2228
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002229static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002230handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002231{
Daniel Stone37816df2012-05-16 18:45:18 +01002232 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002233
Daniel Stone37816df2012-05-16 18:45:18 +01002234 seat = container_of(listener, struct weston_seat,
2235 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002236
Daniel Stone37816df2012-05-16 18:45:18 +01002237 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002238}
2239
Casey Dahlin9074db52012-04-19 22:50:09 -04002240static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002241{
2242 wl_list_remove(&resource->link);
2243 free(resource);
2244}
2245
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002246static void
Daniel Stone37816df2012-05-16 18:45:18 +01002247seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2248 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002249{
Daniel Stone37816df2012-05-16 18:45:18 +01002250 struct weston_seat *seat = resource->data;
2251 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002252
Daniel Stone37816df2012-05-16 18:45:18 +01002253 if (!seat->seat.pointer)
2254 return;
2255
2256 cr = wl_client_add_object(client, &wl_pointer_interface,
2257 &pointer_interface, id, seat);
2258 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002259 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002260
2261 if (seat->seat.pointer->focus &&
2262 seat->seat.pointer->focus->resource.client == client) {
2263 struct weston_surface *surface;
2264 wl_fixed_t sx, sy;
2265
2266 surface = (struct weston_surface *) seat->seat.pointer->focus;
2267 weston_surface_from_global_fixed(surface,
2268 seat->seat.pointer->x,
2269 seat->seat.pointer->y,
2270 &sx,
2271 &sy);
2272 wl_pointer_set_focus(seat->seat.pointer,
2273 seat->seat.pointer->focus,
2274 sx,
2275 sy);
2276 }
Daniel Stone37816df2012-05-16 18:45:18 +01002277}
2278
2279static void
2280seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2281 uint32_t id)
2282{
2283 struct weston_seat *seat = resource->data;
2284 struct wl_resource *cr;
2285
2286 if (!seat->seat.keyboard)
2287 return;
2288
2289 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2290 seat);
2291 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002292 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002293
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002294 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2295 seat->xkb_info.keymap_fd,
2296 seat->xkb_info.keymap_size);
2297
Daniel Stone17b13bd2012-05-30 16:31:39 +01002298 if (seat->seat.keyboard->focus &&
2299 seat->seat.keyboard->focus->resource.client == client) {
2300 wl_keyboard_set_focus(seat->seat.keyboard,
2301 seat->seat.keyboard->focus);
2302 }
Daniel Stone37816df2012-05-16 18:45:18 +01002303}
2304
2305static void
2306seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2307 uint32_t id)
2308{
2309 struct weston_seat *seat = resource->data;
2310 struct wl_resource *cr;
2311
2312 if (!seat->seat.touch)
2313 return;
2314
2315 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2316 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002317 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002318}
2319
2320static const struct wl_seat_interface seat_interface = {
2321 seat_get_pointer,
2322 seat_get_keyboard,
2323 seat_get_touch,
2324};
2325
2326static void
2327bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2328{
2329 struct wl_seat *seat = data;
2330 struct wl_resource *resource;
2331 enum wl_seat_capability caps = 0;
2332
2333 resource = wl_client_add_object(client, &wl_seat_interface,
2334 &seat_interface, id, data);
2335 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002336 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002337
2338 if (seat->pointer)
2339 caps |= WL_SEAT_CAPABILITY_POINTER;
2340 if (seat->keyboard)
2341 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2342 if (seat->touch)
2343 caps |= WL_SEAT_CAPABILITY_TOUCH;
2344
2345 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002346}
2347
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002348static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002349device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002350{
Daniel Stone37816df2012-05-16 18:45:18 +01002351 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002352
Daniel Stone37816df2012-05-16 18:45:18 +01002353 seat = container_of(listener, struct weston_seat,
2354 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002355
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002356 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002357}
2358
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002359static void weston_compositor_xkb_init(struct weston_compositor *ec,
2360 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002361{
Daniel Stonee379da92012-05-30 16:32:04 +01002362 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002363 ec->xkb_context = xkb_context_new(0);
2364 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002365 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002366 exit(1);
2367 }
Daniel Stone994679a2012-05-30 16:31:43 +01002368 }
2369
2370 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002371 ec->xkb_names = *names;
2372 if (!ec->xkb_names.rules)
2373 ec->xkb_names.rules = strdup("evdev");
2374 if (!ec->xkb_names.model)
2375 ec->xkb_names.model = strdup("pc105");
2376 if (!ec->xkb_names.layout)
2377 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002378}
2379
Daniel Stonee379da92012-05-30 16:32:04 +01002380static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2381{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002382 if (xkb_info->keymap)
2383 xkb_map_unref(xkb_info->keymap);
2384
2385 if (xkb_info->keymap_area)
2386 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2387 if (xkb_info->keymap_fd >= 0)
2388 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002389}
2390
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002391static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2392{
Daniel Stonee379da92012-05-30 16:32:04 +01002393 free((char *) ec->xkb_names.rules);
2394 free((char *) ec->xkb_names.model);
2395 free((char *) ec->xkb_names.layout);
2396 free((char *) ec->xkb_names.variant);
2397 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002398
Daniel Stonee379da92012-05-30 16:32:04 +01002399 xkb_info_destroy(&ec->xkb_info);
2400 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002401}
2402
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002403static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002404weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002405{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002406 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002407
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002408 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2409 XKB_MOD_NAME_SHIFT);
2410 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2411 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002412 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2413 XKB_MOD_NAME_CTRL);
2414 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2415 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002416 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2417 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002418 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2419 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002420 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002421
2422 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2423 XKB_LED_NAME_NUM);
2424 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2425 XKB_LED_NAME_CAPS);
2426 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2427 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002428
2429 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2430 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002431 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002432 exit(EXIT_FAILURE);
2433 }
2434 xkb_info->keymap_size = strlen(keymap_str) + 1;
2435
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002436 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002437 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002438 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002439 (unsigned long) xkb_info->keymap_size);
2440 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002441 }
2442
2443 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2444 PROT_READ | PROT_WRITE,
2445 MAP_SHARED, xkb_info->keymap_fd, 0);
2446 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002447 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002448 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002449 goto err_dev_zero;
2450 }
2451 strcpy(xkb_info->keymap_area, keymap_str);
2452 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002453
2454 return;
2455
2456err_dev_zero:
2457 close(xkb_info->keymap_fd);
2458 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002459err_keymap_str:
2460 free(keymap_str);
2461 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002462}
2463
2464static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002465weston_compositor_build_global_keymap(struct weston_compositor *ec)
2466{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002467 if (ec->xkb_info.keymap != NULL)
2468 return;
2469
Daniel Stonee379da92012-05-30 16:32:04 +01002470 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002471 &ec->xkb_names,
2472 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002473 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002474 weston_log("failed to compile global XKB keymap\n");
2475 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002476 "options %s",
2477 ec->xkb_names.rules, ec->xkb_names.model,
2478 ec->xkb_names.layout, ec->xkb_names.variant,
2479 ec->xkb_names.options);
2480 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002481 }
2482
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002483 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002484}
2485
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002486WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002487weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002488{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002489 if (seat->has_keyboard)
2490 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002491
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002492 if (keymap != NULL) {
2493 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002494 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002495 }
2496 else {
2497 weston_compositor_build_global_keymap(seat->compositor);
2498 seat->xkb_info = seat->compositor->xkb_info;
2499 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2500 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002501
2502 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2503 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002504 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002505 exit(1);
2506 }
2507
Daniel Stone71c38772012-06-22 13:21:30 +01002508 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002509
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002510 wl_keyboard_init(&seat->keyboard);
2511 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2512
2513 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002514}
2515
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002516WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002517weston_seat_init_pointer(struct weston_seat *seat)
2518{
2519 if (seat->has_pointer)
2520 return;
2521
2522 wl_pointer_init(&seat->pointer);
2523 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2524
2525 seat->has_pointer = 1;
2526}
2527
2528WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002529weston_seat_init_touch(struct weston_seat *seat)
2530{
2531 if (seat->has_touch)
2532 return;
2533
2534 wl_touch_init(&seat->touch);
2535 wl_seat_set_touch(&seat->seat, &seat->touch);
2536
2537 seat->has_touch = 1;
2538}
2539
2540WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002541weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002542{
Daniel Stone37816df2012-05-16 18:45:18 +01002543 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002544 seat->has_pointer = 0;
2545 seat->has_keyboard = 0;
2546 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002547
Daniel Stone37816df2012-05-16 18:45:18 +01002548 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2549 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002550
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002551 seat->sprite = NULL;
2552 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002553
Daniel Stone37816df2012-05-16 18:45:18 +01002554 seat->compositor = ec;
2555 seat->hotspot_x = 16;
2556 seat->hotspot_y = 16;
2557 seat->modifier_state = 0;
2558 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002559
Daniel Stone37816df2012-05-16 18:45:18 +01002560 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002561 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002562
Daniel Stone37816df2012-05-16 18:45:18 +01002563 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002564
Daniel Stone37816df2012-05-16 18:45:18 +01002565 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2566 wl_signal_add(&seat->seat.drag_icon_signal,
2567 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002568
2569 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002570 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002571}
2572
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002573WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002574weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002575{
Daniel Stone37816df2012-05-16 18:45:18 +01002576 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002577 /* The global object is destroyed at wl_display_destroy() time. */
2578
Daniel Stone37816df2012-05-16 18:45:18 +01002579 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002580 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002581
Daniel Stone74419a22012-05-30 16:32:02 +01002582 if (seat->xkb_state.state != NULL)
2583 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002584 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002585
Daniel Stone37816df2012-05-16 18:45:18 +01002586 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002587}
2588
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002589static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002590drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2591{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002592 empty_region(&es->pending.input);
2593
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002594 weston_surface_configure(es,
2595 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002596 es->buffer_ref.buffer->width,
2597 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002598}
2599
2600static int
Daniel Stone37816df2012-05-16 18:45:18 +01002601device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002602 struct weston_surface *surface)
2603{
Daniel Stone37816df2012-05-16 18:45:18 +01002604 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002605
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002606 if (surface->configure) {
2607 wl_resource_post_error(&surface->surface.resource,
2608 WL_DISPLAY_ERROR_INVALID_OBJECT,
2609 "surface->configure already set");
2610 return 0;
2611 }
2612
Daniel Stone37816df2012-05-16 18:45:18 +01002613 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002614
Daniel Stone37816df2012-05-16 18:45:18 +01002615 weston_surface_set_position(ws->drag_surface,
2616 wl_fixed_to_double(seat->pointer->x),
2617 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002618
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002619 surface->configure = drag_surface_configure;
2620
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002621 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002622 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002623
2624 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002625}
2626
2627static void
Daniel Stone37816df2012-05-16 18:45:18 +01002628device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002629{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002630 if (weston_surface_is_mapped(seat->drag_surface))
2631 weston_surface_unmap(seat->drag_surface);
2632
Daniel Stone37816df2012-05-16 18:45:18 +01002633 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002634 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002635 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2636 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002637}
2638
2639static void
Daniel Stone37816df2012-05-16 18:45:18 +01002640device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002641{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002642 struct wl_list *list;
2643
Daniel Stone37816df2012-05-16 18:45:18 +01002644 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002645 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002646 return;
2647
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002648 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2649 list = &seat->sprite->layer_link;
2650 else
2651 list = &seat->compositor->cursor_layer.surface_list;
2652
2653 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002654 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002655 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002656}
2657
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002658static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002659weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002660 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002661{
2662 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002663
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002664 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002665 return;
2666
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002667 if (seat->drag_surface && seat->seat.drag_surface &&
2668 (&seat->drag_surface->surface.resource !=
2669 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002670 /* between calls to this funcion we got a new drag_surface */
2671 surface_changed = 1;
2672
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002673 if (!seat->seat.drag_surface || surface_changed) {
2674 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002675 if (!surface_changed)
2676 return;
2677 }
2678
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002679 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002680 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002681 seat->seat.drag_surface;
2682 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002683 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002684 }
2685
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002686 /* the client may not have attached a buffer to the drag surface
2687 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002688 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002689
2690 if (!dx && !dy)
2691 return;
2692
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002693 weston_surface_set_position(seat->drag_surface,
2694 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2695 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002696}
2697
2698WL_EXPORT void
2699weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2700{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002701 struct weston_seat *seat;
2702
2703 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002704 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002705}
2706
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002707static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002708bind_output(struct wl_client *client,
2709 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002710{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002711 struct weston_output *output = data;
2712 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002713 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002714
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002715 resource = wl_client_add_object(client,
2716 &wl_output_interface, NULL, id, data);
2717
Casey Dahlin9074db52012-04-19 22:50:09 -04002718 wl_list_insert(&output->resource_list, &resource->link);
2719 resource->destroy = unbind_resource;
2720
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002721 wl_output_send_geometry(resource,
2722 output->x,
2723 output->y,
2724 output->mm_width,
2725 output->mm_height,
2726 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002727 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002728 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002729
2730 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002731 wl_output_send_mode(resource,
2732 mode->flags,
2733 mode->width,
2734 mode->height,
2735 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002736 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002737}
2738
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002739WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002740weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002741{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002742 struct weston_compositor *c = output->compositor;
2743
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002744 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002745 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002746 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002747
2748 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002749}
2750
Scott Moreau1bad5db2012-08-18 01:04:05 -06002751static void
2752weston_output_compute_transform(struct weston_output *output)
2753{
2754 struct weston_matrix transform;
2755 int flip;
2756
2757 weston_matrix_init(&transform);
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002758 transform.type = WESTON_MATRIX_TRANSFORM_ROTATE;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002759
2760 switch(output->transform) {
2761 case WL_OUTPUT_TRANSFORM_FLIPPED:
2762 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2763 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2764 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Vasily Khoruzhick1bbf3722013-01-28 22:40:28 +03002765 transform.type |= WESTON_MATRIX_TRANSFORM_OTHER;
Scott Moreau1bad5db2012-08-18 01:04:05 -06002766 flip = -1;
2767 break;
2768 default:
2769 flip = 1;
2770 break;
2771 }
2772
2773 switch(output->transform) {
2774 case WL_OUTPUT_TRANSFORM_NORMAL:
2775 case WL_OUTPUT_TRANSFORM_FLIPPED:
2776 transform.d[0] = flip;
2777 transform.d[1] = 0;
2778 transform.d[4] = 0;
2779 transform.d[5] = 1;
2780 break;
2781 case WL_OUTPUT_TRANSFORM_90:
2782 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2783 transform.d[0] = 0;
2784 transform.d[1] = -flip;
2785 transform.d[4] = 1;
2786 transform.d[5] = 0;
2787 break;
2788 case WL_OUTPUT_TRANSFORM_180:
2789 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2790 transform.d[0] = -flip;
2791 transform.d[1] = 0;
2792 transform.d[4] = 0;
2793 transform.d[5] = -1;
2794 break;
2795 case WL_OUTPUT_TRANSFORM_270:
2796 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2797 transform.d[0] = 0;
2798 transform.d[1] = flip;
2799 transform.d[4] = -1;
2800 transform.d[5] = 0;
2801 break;
2802 default:
2803 break;
2804 }
2805
2806 weston_matrix_multiply(&output->matrix, &transform);
2807}
2808
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002809WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002810weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002811{
Scott Moreau850ca422012-05-21 15:21:25 -06002812 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002813 struct weston_matrix camera;
2814 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002815
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002816 weston_matrix_init(&output->matrix);
2817 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002818 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2819 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002820
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002821 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002822 2.0 / (output->width + output->border.left + output->border.right),
2823 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2824
2825 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002826
Scott Moreauccbf29d2012-02-22 14:21:41 -07002827 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002828 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002829 weston_matrix_init(&camera);
2830 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002831 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002832 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002833 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002834 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002835 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002836 weston_matrix_multiply(&output->matrix, &modelview);
2837 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002838
Scott Moreauccbf29d2012-02-22 14:21:41 -07002839 output->dirty = 0;
2840}
2841
Scott Moreau1bad5db2012-08-18 01:04:05 -06002842static void
2843weston_output_transform_init(struct weston_output *output, uint32_t transform)
2844{
2845 output->transform = transform;
2846
2847 switch (transform) {
2848 case WL_OUTPUT_TRANSFORM_90:
2849 case WL_OUTPUT_TRANSFORM_270:
2850 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2851 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2852 /* Swap width and height */
2853 output->width = output->current->height;
2854 output->height = output->current->width;
2855 break;
2856 case WL_OUTPUT_TRANSFORM_NORMAL:
2857 case WL_OUTPUT_TRANSFORM_180:
2858 case WL_OUTPUT_TRANSFORM_FLIPPED:
2859 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2860 output->width = output->current->width;
2861 output->height = output->current->height;
2862 break;
2863 default:
2864 break;
2865 }
2866}
2867
Scott Moreauccbf29d2012-02-22 14:21:41 -07002868WL_EXPORT void
2869weston_output_move(struct weston_output *output, int x, int y)
2870{
2871 output->x = x;
2872 output->y = y;
2873
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002874 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002875 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002876 output->width,
2877 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002878}
2879
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002880WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002881weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002882 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002883{
2884 output->compositor = c;
2885 output->x = x;
2886 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002887 output->border.top = 0;
2888 output->border.bottom = 0;
2889 output->border.left = 0;
2890 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002891 output->mm_width = width;
2892 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002893 output->dirty = 1;
2894
Scott Moreau1bad5db2012-08-18 01:04:05 -06002895 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002896 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002897
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002898 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002899 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002900
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002901 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002902 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002903 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002904
Casey Dahlin58ba1372012-04-19 22:50:08 -04002905 output->id = ffs(~output->compositor->output_id_pool) - 1;
2906 output->compositor->output_id_pool |= 1 << output->id;
2907
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002908 output->global =
2909 wl_display_add_global(c->wl_display, &wl_output_interface,
2910 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002911}
2912
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002913static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002914compositor_bind(struct wl_client *client,
2915 void *data, uint32_t version, uint32_t id)
2916{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002917 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002918
2919 wl_client_add_object(client, &wl_compositor_interface,
2920 &compositor_interface, id, compositor);
2921}
2922
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002923static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002924log_uname(void)
2925{
2926 struct utsname usys;
2927
2928 uname(&usys);
2929
2930 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2931 usys.version, usys.machine);
2932}
2933
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002934WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002935weston_compositor_init(struct weston_compositor *ec,
2936 struct wl_display *display,
2937 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002938 char *argv[],
2939 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002940{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002941 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002942 struct xkb_rule_names xkb_names;
2943 const struct config_key keyboard_config_keys[] = {
2944 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2945 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2946 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2947 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2948 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2949 };
2950 const struct config_section cs[] = {
2951 { "keyboard",
2952 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2953 };
2954
2955 memset(&xkb_names, 0, sizeof(xkb_names));
2956 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002957
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002958 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002959 wl_signal_init(&ec->destroy_signal);
2960 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002961 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002962 wl_signal_init(&ec->lock_signal);
2963 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002964 wl_signal_init(&ec->show_input_panel_signal);
2965 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002966 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002967 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002968
Casey Dahlin58ba1372012-04-19 22:50:08 -04002969 ec->output_id_pool = 0;
2970
Kristian Høgsberga8873122011-11-23 10:39:34 -05002971 if (!wl_display_add_global(display, &wl_compositor_interface,
2972 ec, compositor_bind))
2973 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002974
Daniel Stone725c2c32012-06-22 14:04:36 +01002975 wl_list_init(&ec->surface_list);
2976 wl_list_init(&ec->layer_list);
2977 wl_list_init(&ec->seat_list);
2978 wl_list_init(&ec->output_list);
2979 wl_list_init(&ec->key_binding_list);
2980 wl_list_init(&ec->button_binding_list);
2981 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002982 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002983 wl_list_init(&ec->fade.animation.link);
2984
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002985 weston_plane_init(&ec->primary_plane, 0, 0);
2986
Daniel Stone725c2c32012-06-22 14:04:36 +01002987 weston_compositor_xkb_init(ec, &xkb_names);
2988
2989 ec->ping_handler = NULL;
2990
2991 screenshooter_create(ec);
2992 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002993 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002994
2995 wl_data_device_manager_init(ec->wl_display);
2996
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002997 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002998
Daniel Stone725c2c32012-06-22 14:04:36 +01002999 loop = wl_display_get_event_loop(ec->wl_display);
3000 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3001 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3002
3003 ec->input_loop = wl_event_loop_create();
3004
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003005 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
3006 ec->fade.animation.frame = fade_frame;
3007
3008 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3009 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3010
3011 weston_compositor_schedule_repaint(ec);
3012
Daniel Stone725c2c32012-06-22 14:04:36 +01003013 return 0;
3014}
3015
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003016WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003017weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003018{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003019 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003020
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003021 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003022 if (ec->input_loop_source)
3023 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003024
Matt Roper361d2ad2011-08-29 13:52:23 -07003025 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003026 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003027 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003028
Daniel Stone325fc2d2012-05-30 16:31:58 +01003029 weston_binding_list_destroy_all(&ec->key_binding_list);
3030 weston_binding_list_destroy_all(&ec->button_binding_list);
3031 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003032 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003033
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003034 weston_plane_release(&ec->primary_plane);
3035
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003036 wl_array_release(&ec->vertices);
3037 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003038 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003039
3040 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003041}
3042
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003043static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003044{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003045 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003046
Martin Minarik6d118362012-06-07 18:01:59 +02003047 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003048 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003049
3050 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003051}
3052
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003053static void
3054on_segv_signal(int s, siginfo_t *siginfo, void *context)
3055{
3056 void *buffer[32];
3057 int i, count;
3058 Dl_info info;
3059
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003060 /* This SIGSEGV handler will do a best-effort backtrace, and
3061 * then call the backend restore function, which will switch
3062 * back to the vt we launched from or ungrab X etc and then
3063 * raise SIGTRAP. If we run weston under gdb from X or a
3064 * different vt, and tell gdb "handle SIGSEGV nostop", this
3065 * will allow weston to switch back to gdb on crash and then
3066 * gdb will catch the crash with SIGTRAP. */
3067
Martin Minarik6d118362012-06-07 18:01:59 +02003068 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003069
3070 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3071 for (i = 0; i < count; i++) {
3072 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003073 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003074 (long) buffer[i],
3075 info.dli_sname ? info.dli_sname : "--",
3076 info.dli_fname);
3077 }
3078
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003079 segv_compositor->restore(segv_compositor);
3080
3081 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003082}
3083
3084
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003085static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003086load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003087{
3088 char path[PATH_MAX];
3089 void *module, *init;
3090
3091 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003092 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003093 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003094 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003095
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003096 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3097 if (module) {
3098 weston_log("Module '%s' already loaded\n", path);
3099 dlclose(module);
3100 return NULL;
3101 }
3102
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003103 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003104 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003105 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003106 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003107 return NULL;
3108 }
3109
3110 init = dlsym(module, entrypoint);
3111 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003112 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003113 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003114 return NULL;
3115 }
3116
3117 return init;
3118}
3119
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003120static int
3121load_modules(struct weston_compositor *ec, const char *modules)
3122{
3123 const char *p, *end;
3124 char buffer[256];
3125 int (*module_init)(struct weston_compositor *ec);
3126
3127 if (modules == NULL)
3128 return 0;
3129
3130 p = modules;
3131 while (*p) {
3132 end = strchrnul(p, ',');
3133 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3134 module_init = load_module(buffer, "module_init");
3135 if (module_init)
3136 module_init(ec);
3137 p = end;
3138 while (*p == ',')
3139 p++;
3140
3141 }
3142
3143 return 0;
3144}
3145
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003146static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003147 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3148
3149static const char xdg_wrong_message[] =
3150 "fatal: environment variable XDG_RUNTIME_DIR\n"
3151 "is set to \"%s\", which is not a directory.\n";
3152
3153static const char xdg_wrong_mode_message[] =
3154 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3155 "correctly. Unix access mode must be 0700 but is %o,\n"
3156 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003157 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003158
3159static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003160 "Refer to your distribution on how to get it, or\n"
3161 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3162 "on how to implement it.\n";
3163
Martin Minarik37032f82012-06-18 20:15:18 +02003164static void
3165verify_xdg_runtime_dir(void)
3166{
3167 char *dir = getenv("XDG_RUNTIME_DIR");
3168 struct stat s;
3169
3170 if (!dir) {
3171 weston_log(xdg_error_message);
3172 weston_log_continue(xdg_detail_message);
3173 exit(EXIT_FAILURE);
3174 }
3175
3176 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3177 weston_log(xdg_wrong_message, dir);
3178 weston_log_continue(xdg_detail_message);
3179 exit(EXIT_FAILURE);
3180 }
3181
3182 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3183 weston_log(xdg_wrong_mode_message,
3184 dir, s.st_mode & 0777, s.st_uid);
3185 weston_log_continue(xdg_detail_message);
3186 }
3187}
3188
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003189static int
3190usage(int error_code)
3191{
3192 fprintf(stderr,
3193 "Usage: weston [OPTIONS]\n\n"
3194 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3195 "Weston supports multiple backends, and depending on which backend is in use\n"
3196 "different options will be accepted.\n\n"
3197
3198
3199 "Core options:\n\n"
Scott Moreau12245142012-08-29 15:15:58 -06003200 " --version\t\tPrint weston version\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003201 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3202 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3203 " -S, --socket=NAME\tName of socket to listen on\n"
3204 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003205 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003206 " --log==FILE\t\tLog to the given file\n"
3207 " -h, --help\t\tThis help message\n\n");
3208
3209 fprintf(stderr,
3210 "Options for drm-backend.so:\n\n"
3211 " --connector=ID\tBring up only this connector\n"
3212 " --seat=SEAT\t\tThe seat that weston should run on\n"
3213 " --tty=TTY\t\tThe tty to use\n"
Ander Conselvan de Oliveira23e72b82013-01-25 15:13:06 +02003214 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003215 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3216
3217 fprintf(stderr,
3218 "Options for x11-backend.so:\n\n"
3219 " --width=WIDTH\t\tWidth of X window\n"
3220 " --height=HEIGHT\tHeight of X window\n"
3221 " --fullscreen\t\tRun in fullscreen mode\n"
Kristian Høgsbergefaca342013-01-07 15:52:44 -05003222 " --use-pixman\t\tUse the pixman (CPU) renderer\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003223 " --output-count=COUNT\tCreate multiple outputs\n"
3224 " --no-input\t\tDont create input devices\n\n");
3225
3226 fprintf(stderr,
3227 "Options for wayland-backend.so:\n\n"
3228 " --width=WIDTH\t\tWidth of Wayland surface\n"
3229 " --height=HEIGHT\tHeight of Wayland surface\n"
3230 " --display=DISPLAY\tWayland display to connect to\n\n");
3231
3232 exit(error_code);
3233}
3234
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003235int main(int argc, char *argv[])
3236{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003237 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003238 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003239 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003240 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003241 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003242 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003243 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003244 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003245 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003246 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003247 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003248 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003249 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003250 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003251 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003252 char *socket_name = "wayland-0";
Scott Moreau12245142012-08-29 15:15:58 -06003253 int32_t version = 0;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003254 char *config_file;
3255
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003256 const struct config_key core_config_keys[] = {
3257 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003258 };
3259
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003260 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003261 { "core",
3262 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003263 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003264
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003265 const struct weston_option core_options[] = {
3266 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3267 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3268 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003269 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003270 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003271 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Scott Moreau12245142012-08-29 15:15:58 -06003272 { WESTON_OPTION_BOOLEAN, "version", 0, &version },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003273 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003274
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003275 argc = parse_options(core_options,
3276 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003277
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003278 if (help)
3279 usage(EXIT_SUCCESS);
3280
Scott Moreau12245142012-08-29 15:15:58 -06003281 if (version) {
3282 printf(PACKAGE_STRING "\n");
3283 return EXIT_SUCCESS;
3284 }
3285
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003286 weston_log_file_open(log);
3287
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003288 weston_log("%s\n"
3289 STAMP_SPACE "%s\n"
3290 STAMP_SPACE "Bug reports to: %s\n"
3291 STAMP_SPACE "Build: %s\n",
3292 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003293 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003294 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003295
Martin Minarik37032f82012-06-18 20:15:18 +02003296 verify_xdg_runtime_dir();
3297
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003298 display = wl_display_create();
3299
Tiago Vignatti2116b892011-08-08 05:52:59 -07003300 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003301 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3302 display);
3303 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3304 display);
3305 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3306 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003307
3308 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003309 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3310 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003311
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003312 if (!backend) {
3313 if (getenv("WAYLAND_DISPLAY"))
3314 backend = "wayland-backend.so";
3315 else if (getenv("DISPLAY"))
3316 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003317 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003318 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003319 }
3320
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003321 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003322 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003323
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003324 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003325 if (!backend_init)
3326 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003327
Daniel Stonec1be8e52012-06-01 11:14:02 -04003328 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003329 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003330 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003331 exit(EXIT_FAILURE);
3332 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003333
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003334 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3335 segv_action.sa_sigaction = on_segv_signal;
3336 sigemptyset(&segv_action.sa_mask);
3337 sigaction(SIGSEGV, &segv_action, NULL);
3338 segv_compositor = ec;
3339
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003340 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003341 weston_log("fatal: unhandled option: %s\n", argv[i]);
3342 if (argv[1]) {
3343 ret = EXIT_FAILURE;
3344 goto out;
3345 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003346
Daniel Stonec1be8e52012-06-01 11:14:02 -04003347 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003348
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003349 ec->option_idle_time = idle_time;
3350 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003351
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003352 setenv("WAYLAND_DISPLAY", socket_name, 1);
3353
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003354 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003355 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003356 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003357 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003358
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003359 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003360 weston_log("fatal: failed to add socket: %m\n");
3361 ret = EXIT_FAILURE;
3362 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003363 }
3364
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003365 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003366 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003367
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003368 wl_display_run(display);
3369
3370 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003371 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003372 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003373
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003374 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003375
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003376 for (i = ARRAY_LENGTH(signals); i;)
3377 wl_event_source_remove(signals[--i]);
3378
Daniel Stone855028f2012-05-01 20:37:10 +01003379 weston_compositor_xkb_destroy(ec);
3380
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003381 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003382 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003383
Martin Minarik19e6f262012-06-07 13:08:46 +02003384 weston_log_file_close();
3385
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003386 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003387}