blob: e556766f1f3be78fa5dfb478ae1b755a6ab22106 [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 "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200330 "weston_surface_to_global(), divisor = %g\n",
331 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. */
542 surface->transform.position.matrix.d[12] = surface->geometry.x;
543 surface->transform.position.matrix.d[13] = surface->geometry.y;
544
545 weston_matrix_init(matrix);
546 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
547 weston_matrix_multiply(matrix, &tform->matrix);
548
549 if (weston_matrix_invert(inverse, matrix) < 0) {
550 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200551 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200552 " transformation not invertible.\n", surface);
553 return -1;
554 }
555
556 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
557 surface->geometry.height,
558 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500559
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200560 return 0;
561}
562
563WL_EXPORT void
564weston_surface_update_transform(struct weston_surface *surface)
565{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200566 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200567 return;
568
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200569 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200570
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500571 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200572
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200573 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500574 pixman_region32_fini(&surface->transform.opaque);
575 pixman_region32_init(&surface->transform.opaque);
576
Pekka Paalanencd403622012-01-25 13:37:39 +0200577 /* transform.position is always in transformation_list */
578 if (surface->geometry.transformation_list.next ==
579 &surface->transform.position.link &&
580 surface->geometry.transformation_list.prev ==
581 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200582 weston_surface_update_transform_disable(surface);
583 } else {
584 if (weston_surface_update_transform_enable(surface) < 0)
585 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200586 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200587
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400588 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200589
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300590 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200591}
592
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200593WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100594weston_surface_to_global_fixed(struct weston_surface *surface,
595 wl_fixed_t sx, wl_fixed_t sy,
596 wl_fixed_t *x, wl_fixed_t *y)
597{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200598 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100599
600 weston_surface_to_global_float(surface,
601 wl_fixed_to_double(sx),
602 wl_fixed_to_double(sy),
603 &xf, &yf);
604 *x = wl_fixed_from_double(xf);
605 *y = wl_fixed_from_double(yf);
606}
607
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400608WL_EXPORT void
609weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200610 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200611{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200612 if (surface->transform.enabled) {
613 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
614
615 weston_matrix_transform(&surface->transform.inverse, &v);
616
617 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200618 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200619 "weston_surface_from_global(), divisor = %g\n",
620 v.f[3]);
621 *sx = 0;
622 *sy = 0;
623 return;
624 }
625
Pekka Paalanencd403622012-01-25 13:37:39 +0200626 *sx = v.f[0] / v.f[3];
627 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200628 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200629 *sx = x - surface->geometry.x;
630 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200631 }
632}
633
634WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100635weston_surface_from_global_fixed(struct weston_surface *surface,
636 wl_fixed_t x, wl_fixed_t y,
637 wl_fixed_t *sx, wl_fixed_t *sy)
638{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200639 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100640
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400641 weston_surface_from_global_float(surface,
642 wl_fixed_to_double(x),
643 wl_fixed_to_double(y),
644 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100645 *sx = wl_fixed_from_double(sxf);
646 *sy = wl_fixed_from_double(syf);
647}
648
649WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200650weston_surface_from_global(struct weston_surface *surface,
651 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
652{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200653 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200654
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400655 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200656 *sx = floorf(sxf);
657 *sy = floorf(syf);
658}
659
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400660WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400661weston_surface_schedule_repaint(struct weston_surface *surface)
662{
663 struct weston_output *output;
664
665 wl_list_for_each(output, &surface->compositor->output_list, link)
666 if (surface->output_mask & (1 << output->id))
667 weston_output_schedule_repaint(output);
668}
669
670WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500671weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500672{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400673 pixman_region32_union_rect(&surface->damage, &surface->damage,
674 0, 0, surface->geometry.width,
675 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200676
Kristian Høgsberg98238702012-08-03 16:29:12 -0400677 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500678}
679
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400680WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500681weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200682 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400683{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200684 surface->geometry.x = x;
685 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200686 surface->geometry.width = width;
687 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200688 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400689}
690
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200691WL_EXPORT void
692weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200693 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200694{
695 surface->geometry.x = x;
696 surface->geometry.y = y;
697 surface->geometry.dirty = 1;
698}
699
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300700WL_EXPORT int
701weston_surface_is_mapped(struct weston_surface *surface)
702{
703 if (surface->output)
704 return 1;
705 else
706 return 0;
707}
708
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200709WL_EXPORT int32_t
710weston_surface_buffer_width(struct weston_surface *surface)
711{
712 switch (surface->buffer_transform) {
713 case WL_OUTPUT_TRANSFORM_90:
714 case WL_OUTPUT_TRANSFORM_270:
715 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
716 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200717 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200718 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200719 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200720 }
721}
722
723WL_EXPORT int32_t
724weston_surface_buffer_height(struct weston_surface *surface)
725{
726 switch (surface->buffer_transform) {
727 case WL_OUTPUT_TRANSFORM_90:
728 case WL_OUTPUT_TRANSFORM_270:
729 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
730 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200731 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200732 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200733 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200734 }
735}
736
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400737WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500738weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500739{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400740 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500741
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400742 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500743
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400744 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500745}
746
Tiago Vignatti9d393522012-02-10 16:26:19 +0200747static struct weston_surface *
748weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100749 wl_fixed_t x, wl_fixed_t y,
750 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200751{
752 struct weston_surface *surface;
753
754 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100755 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500756 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100757 wl_fixed_to_int(*sx),
758 wl_fixed_to_int(*sy),
759 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200760 return surface;
761 }
762
763 return NULL;
764}
765
766static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400767weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500768{
Scott Moreau447013d2012-02-18 05:05:29 -0700769 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500770 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400771 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500772
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400773 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100774 return;
775
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400776 surface = weston_compositor_pick_surface(seat->compositor,
777 pointer->x,
778 pointer->y,
779 &pointer->current_x,
780 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500781
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400782 if (&surface->surface != pointer->current) {
783 interface = pointer->grab->interface;
784 pointer->current = &surface->surface;
785 interface->focus(pointer->grab, &surface->surface,
786 pointer->current_x,
787 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500788 }
789
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400790 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500791 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100792 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400793 pointer->x,
794 pointer->y,
795 &pointer->grab->x,
796 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500797}
798
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400799static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500800weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400801{
Daniel Stone37816df2012-05-16 18:45:18 +0100802 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400803
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500804 if (!compositor->focus)
805 return;
806
Daniel Stone37816df2012-05-16 18:45:18 +0100807 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400808 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400809}
810
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400811WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500812weston_surface_unmap(struct weston_surface *surface)
813{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100814 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500815
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500816 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500817 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500818 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500819
Daniel Stone4dab5db2012-05-30 16:31:53 +0100820 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100821 if (seat->seat.keyboard &&
822 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100823 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100824 if (seat->seat.pointer &&
825 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100826 wl_pointer_set_focus(seat->seat.pointer,
827 NULL,
828 wl_fixed_from_int(0),
829 wl_fixed_from_int(0));
830 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500831
Kristian Høgsberg98238702012-08-03 16:29:12 -0400832 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500833}
834
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400835struct weston_frame_callback {
836 struct wl_resource resource;
837 struct wl_list link;
838};
839
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500840static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400841destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500842{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500843 struct weston_surface *surface =
844 container_of(resource,
845 struct weston_surface, surface.resource);
846 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400847 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400848
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300849 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500850 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400851
Pekka Paalanenbc106382012-10-10 12:49:31 +0300852 wl_list_for_each_safe(cb, next,
853 &surface->pending.frame_callback_list, link)
854 wl_resource_destroy(&cb->resource);
855
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300856 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300857 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300858 pixman_region32_fini(&surface->pending.damage);
859
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300860 if (surface->pending.buffer)
861 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
862
Pekka Paalanende685b82012-12-04 15:58:12 +0200863 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400864
Kristian Høgsberg42263852012-09-06 21:59:29 -0400865 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200866
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200867 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200868 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500869 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500870 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300871 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200872
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400873 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
874 wl_resource_destroy(&cb->resource);
875
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400876 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500877}
878
Alex Wu8811bf92012-02-28 18:07:54 +0800879WL_EXPORT void
880weston_surface_destroy(struct weston_surface *surface)
881{
882 /* Not a valid way to destroy a client surface */
883 assert(surface->surface.resource.client == NULL);
884
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400885 wl_signal_emit(&surface->surface.resource.destroy_signal,
886 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800887 destroy_surface(&surface->surface.resource);
888}
889
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100890static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200891weston_buffer_reference_handle_destroy(struct wl_listener *listener,
892 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100893{
Pekka Paalanende685b82012-12-04 15:58:12 +0200894 struct weston_buffer_reference *ref =
895 container_of(listener, struct weston_buffer_reference,
896 destroy_listener);
897
898 assert((struct wl_buffer *)data == ref->buffer);
899 ref->buffer = NULL;
900}
901
902WL_EXPORT void
903weston_buffer_reference(struct weston_buffer_reference *ref,
904 struct wl_buffer *buffer)
905{
906 if (ref->buffer && buffer != ref->buffer) {
907 weston_buffer_post_release(ref->buffer);
908 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300909 }
910
Pekka Paalanende685b82012-12-04 15:58:12 +0200911 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400912 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300913 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200914 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200915 }
916
Pekka Paalanende685b82012-12-04 15:58:12 +0200917 ref->buffer = buffer;
918 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
919}
920
921static void
922weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
923{
924 weston_buffer_reference(&surface->buffer_ref, buffer);
925
Pekka Paalanena6421c42012-12-04 15:58:10 +0200926 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300927 if (weston_surface_is_mapped(surface))
928 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300929 }
930
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300931 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100932}
933
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500934WL_EXPORT void
935weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400936{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500937 wl_list_remove(&surface->layer_link);
938 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500939 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500940 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400941}
942
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400943WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500944weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400945{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500946 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400947
948 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500949 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400950}
951
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500952WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500953weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200954{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400955 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200956 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200957
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400958 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500959 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200960}
961
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400962WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500963weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400964{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500965 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400966
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400967 pixman_region32_union(&compositor->primary_plane.damage,
968 &compositor->primary_plane.damage,
969 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400970 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400971}
972
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400973static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500974fade_frame(struct weston_animation *animation,
975 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400976{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500977 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400978 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500979 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500980 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400981
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600982 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600983 compositor->fade.spring.timestamp = msecs;
984
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500985 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500986 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500987 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
988 compositor->fade.spring.current);
989 weston_surface_damage(surface);
990
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500991 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400992 compositor->fade.spring.current =
993 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400994 wl_list_remove(&animation->link);
995 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200996
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500997 if (compositor->fade.spring.current < 0.001) {
998 destroy_surface(&surface->surface.resource);
999 compositor->fade.surface = NULL;
1000 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001001 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001002 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001003 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001004 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001005}
1006
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001007static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001008surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001009 pixman_region32_t *opaque)
1010{
Pekka Paalanende685b82012-12-04 15:58:12 +02001011 if (surface->buffer_ref.buffer &&
1012 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001013 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001014
1015 if (surface->transform.enabled) {
1016 pixman_box32_t *extents;
1017
1018 extents = pixman_region32_extents(&surface->damage);
1019 surface_compute_bbox(surface, extents->x1, extents->y1,
1020 extents->x2 - extents->x1,
1021 extents->y2 - extents->y1,
1022 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001023 pixman_region32_translate(&surface->damage,
1024 -surface->plane->x,
1025 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001026 } else {
1027 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001028 surface->geometry.x - surface->plane->x,
1029 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001030 }
1031
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001032 pixman_region32_subtract(&surface->damage,
1033 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001034 pixman_region32_union(&surface->plane->damage,
1035 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001036 empty_region(&surface->damage);
1037 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001038 pixman_region32_union(&surface->plane->opaque,
1039 &surface->plane->opaque,
1040 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001041 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001042}
1043
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001044static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001045weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001047 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001048 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001049 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001050 struct weston_animation *animation, *next;
1051 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001052 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001053 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001054
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001055 weston_compositor_update_drag_surfaces(ec);
1056
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001057 /* Rebuild the surface list and update surface transforms up front. */
1058 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001059 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001060 wl_list_for_each(layer, &ec->layer_list, link) {
1061 wl_list_for_each(es, &layer->surface_list, layer_link) {
1062 weston_surface_update_transform(es);
1063 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001064 if (es->output == output) {
1065 wl_list_insert_list(&frame_callback_list,
1066 &es->frame_callback_list);
1067 wl_list_init(&es->frame_callback_list);
1068 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001069 }
1070 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001071
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001072 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001073 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001074 else
1075 wl_list_for_each(es, &ec->surface_list, link)
1076 weston_surface_move_to_plane(es, &ec->primary_plane);
1077
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001078 pixman_region32_init(&opaque);
1079
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001080 pixman_region32_fini(&ec->primary_plane.opaque);
1081 pixman_region32_init(&ec->primary_plane.opaque);
1082
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001083 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001084 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001085
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001086 /* Both the renderer and the backend have seen the buffer
1087 * by now. If renderer needs the buffer, it has its own
1088 * reference set. If the backend wants to keep the buffer
1089 * around for migrating the surface into a non-primary plane
1090 * later, keep_buffer is true. Otherwise, drop the core
1091 * reference now, and allow early buffer release. This enables
1092 * clients to use single-buffering.
1093 */
1094 if (!es->keep_buffer)
1095 weston_buffer_reference(&es->buffer_ref, NULL);
1096 }
1097
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001098 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001099
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001100 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001101 pixman_region32_intersect(&output_damage,
1102 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001103
Scott Moreauccbf29d2012-02-22 14:21:41 -07001104 if (output->dirty)
1105 weston_output_update_matrix(output);
1106
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001107 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001108
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001109 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001110
Kristian Høgsbergef044142011-06-21 15:02:12 -04001111 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001112
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001113 weston_compositor_repick(ec);
1114 wl_event_loop_dispatch(ec->input_loop, 0);
1115
Jonas Ådahldb773762012-06-13 00:01:21 +02001116 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001117 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001118 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001119 }
1120
Scott Moreaud64cf212012-06-08 19:40:54 -06001121 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001122 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001123 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001124 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001125}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001126
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001127static int
1128weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001129{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001130 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001131
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001132 wl_event_loop_dispatch(compositor->input_loop, 0);
1133
1134 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001135}
1136
1137WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001138weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001139{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001140 struct weston_compositor *compositor = output->compositor;
1141 struct wl_event_loop *loop =
1142 wl_display_get_event_loop(compositor->wl_display);
1143 int fd;
1144
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001145 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001146 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001147 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001148 return;
1149 }
1150
1151 output->repaint_scheduled = 0;
1152 if (compositor->input_loop_source)
1153 return;
1154
1155 fd = wl_event_loop_get_fd(compositor->input_loop);
1156 compositor->input_loop_source =
1157 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1158 weston_compositor_read_input, compositor);
1159}
1160
1161static void
1162idle_repaint(void *data)
1163{
1164 struct weston_output *output = data;
1165
1166 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001167}
1168
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001169WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001170weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1171{
1172 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001173 if (below != NULL)
1174 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001175}
1176
1177WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001178weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001179{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001180 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001181 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001182
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001183 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001184 return;
1185
Kristian Høgsbergef044142011-06-21 15:02:12 -04001186 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001187 output->repaint_needed = 1;
1188 if (output->repaint_scheduled)
1189 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001190
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001191 wl_event_loop_add_idle(loop, idle_repaint, output);
1192 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001193
1194 if (compositor->input_loop_source) {
1195 wl_event_source_remove(compositor->input_loop_source);
1196 compositor->input_loop_source = NULL;
1197 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001198}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001199
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001200WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001201weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1202{
1203 struct weston_output *output;
1204
1205 wl_list_for_each(output, &compositor->output_list, link)
1206 weston_output_schedule_repaint(output);
1207}
1208
1209WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001210weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001211{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001212 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001213 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001214
Scott Moreau9d1b1122012-06-08 19:40:53 -06001215 output = container_of(compositor->output_list.next,
1216 struct weston_output, link);
1217
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001218 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001219 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001220 return;
1221
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001222 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001223 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001224 if (!surface)
1225 return;
1226
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001227 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001228 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001229 wl_list_insert(&compositor->fade_layer.surface_list,
1230 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001231 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001232 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001233 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001234 }
1235
1236 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001237 if (wl_list_empty(&compositor->fade.animation.link)) {
1238 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001239 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001240 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001241 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001242}
1243
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001244static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001245surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001246{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001247 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001248}
1249
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001250static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001251surface_attach(struct wl_client *client,
1252 struct wl_resource *resource,
1253 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1254{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001255 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001256 struct wl_buffer *buffer = NULL;
1257
1258 if (buffer_resource)
1259 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001260
Pekka Paalanende685b82012-12-04 15:58:12 +02001261 /* Attach, attach, without commit in between does not send
1262 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001263 if (surface->pending.buffer)
1264 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001265
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001266 surface->pending.sx = sx;
1267 surface->pending.sy = sy;
1268 surface->pending.buffer = buffer;
1269 if (buffer) {
1270 wl_signal_add(&buffer->resource.destroy_signal,
1271 &surface->pending.buffer_destroy_listener);
1272 surface->pending.remove_contents = 0;
1273 } else {
1274 surface->pending.remove_contents = 1;
1275 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001276}
1277
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001278static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001279surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001280 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001281 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001282{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001283 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001284
Pekka Paalanen8e159182012-10-10 12:49:25 +03001285 pixman_region32_union_rect(&surface->pending.damage,
1286 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001287 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001288}
1289
Kristian Høgsberg33418202011-08-16 23:01:28 -04001290static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001291destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001292{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001294
1295 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001296 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001297}
1298
1299static void
1300surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001301 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001302{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001303 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001304 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001305
1306 cb = malloc(sizeof *cb);
1307 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001308 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001309 return;
1310 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001311
Kristian Høgsberg33418202011-08-16 23:01:28 -04001312 cb->resource.object.interface = &wl_callback_interface;
1313 cb->resource.object.id = callback;
1314 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001315 cb->resource.client = client;
1316 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001317
1318 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001319 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001320}
1321
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001322static void
1323surface_set_opaque_region(struct wl_client *client,
1324 struct wl_resource *resource,
1325 struct wl_resource *region_resource)
1326{
1327 struct weston_surface *surface = resource->data;
1328 struct weston_region *region;
1329
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001330 if (region_resource) {
1331 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001332 pixman_region32_copy(&surface->pending.opaque,
1333 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001334 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001335 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001336 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337}
1338
1339static void
1340surface_set_input_region(struct wl_client *client,
1341 struct wl_resource *resource,
1342 struct wl_resource *region_resource)
1343{
1344 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001345 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001346
1347 if (region_resource) {
1348 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001349 pixman_region32_copy(&surface->pending.input,
1350 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001351 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001352 pixman_region32_fini(&surface->pending.input);
1353 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001354 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001355}
1356
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001357static int
1358surface_pending_buffer_has_different_size(struct weston_surface *surface)
1359{
1360 int width, height;
1361
1362 switch (surface->pending.buffer_transform) {
1363 case WL_OUTPUT_TRANSFORM_90:
1364 case WL_OUTPUT_TRANSFORM_270:
1365 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1366 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1367 height = surface->pending.buffer->width;
1368 width = surface->pending.buffer->height;
1369 break;
1370 default:
1371 width = surface->pending.buffer->width;
1372 height = surface->pending.buffer->height;
1373 }
1374
1375 if (width == surface->geometry.width &&
1376 height == surface->geometry.height)
1377 return 0;
1378 else
1379 return 1;
1380}
1381
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001382static void
1383surface_commit(struct wl_client *client, struct wl_resource *resource)
1384{
1385 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001386 pixman_region32_t opaque;
1387
1388 if (surface->pending.sx || surface->pending.sy ||
1389 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001390 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001391 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001392
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001393 /* wl_surface.set_buffer_rotation */
1394 surface->buffer_transform = surface->pending.buffer_transform;
1395
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001396 /* wl_surface.attach */
1397 if (surface->pending.buffer || surface->pending.remove_contents)
1398 weston_surface_attach(surface, surface->pending.buffer);
1399
Pekka Paalanende685b82012-12-04 15:58:12 +02001400 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001401 surface->configure(surface, surface->pending.sx,
1402 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001403 surface->pending.sx = 0;
1404 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001405
1406 /* wl_surface.damage */
1407 pixman_region32_union(&surface->damage, &surface->damage,
1408 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001409 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1410 0, 0,
1411 surface->geometry.width,
1412 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001413 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001414
1415 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001416 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001417 surface->geometry.width,
1418 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001419 pixman_region32_intersect(&opaque,
1420 &opaque, &surface->pending.opaque);
1421
1422 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1423 pixman_region32_copy(&surface->opaque, &opaque);
1424 surface->geometry.dirty = 1;
1425 }
1426
1427 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001428
1429 /* wl_surface.set_input_region */
1430 pixman_region32_fini(&surface->input);
1431 pixman_region32_init_rect(&surface->input, 0, 0,
1432 surface->geometry.width,
1433 surface->geometry.height);
1434 pixman_region32_intersect(&surface->input,
1435 &surface->input, &surface->pending.input);
1436
Pekka Paalanenbc106382012-10-10 12:49:31 +03001437 /* wl_surface.frame */
1438 wl_list_insert_list(&surface->frame_callback_list,
1439 &surface->pending.frame_callback_list);
1440 wl_list_init(&surface->pending.frame_callback_list);
1441
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001442 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001443}
1444
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001445static void
1446surface_set_buffer_transform(struct wl_client *client,
1447 struct wl_resource *resource, int transform)
1448{
1449 struct weston_surface *surface = resource->data;
1450
1451 surface->pending.buffer_transform = transform;
1452}
1453
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001454static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001455 surface_destroy,
1456 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001457 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001458 surface_frame,
1459 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001460 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001461 surface_commit,
1462 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001463};
1464
1465static void
1466compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001467 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001468{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001469 struct weston_compositor *ec = resource->data;
1470 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001471
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001472 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001473 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001474 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001475 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001476 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001477
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001478 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001479
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001480 surface->surface.resource.object.id = id;
1481 surface->surface.resource.object.interface = &wl_surface_interface;
1482 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001483 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001484 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001485
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001486 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001487}
1488
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001489static void
1490destroy_region(struct wl_resource *resource)
1491{
1492 struct weston_region *region =
1493 container_of(resource, struct weston_region, resource);
1494
1495 pixman_region32_fini(&region->region);
1496 free(region);
1497}
1498
1499static void
1500region_destroy(struct wl_client *client, struct wl_resource *resource)
1501{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001502 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001503}
1504
1505static void
1506region_add(struct wl_client *client, struct wl_resource *resource,
1507 int32_t x, int32_t y, int32_t width, int32_t height)
1508{
1509 struct weston_region *region = resource->data;
1510
1511 pixman_region32_union_rect(&region->region, &region->region,
1512 x, y, width, height);
1513}
1514
1515static void
1516region_subtract(struct wl_client *client, struct wl_resource *resource,
1517 int32_t x, int32_t y, int32_t width, int32_t height)
1518{
1519 struct weston_region *region = resource->data;
1520 pixman_region32_t rect;
1521
1522 pixman_region32_init_rect(&rect, x, y, width, height);
1523 pixman_region32_subtract(&region->region, &region->region, &rect);
1524 pixman_region32_fini(&rect);
1525}
1526
1527static const struct wl_region_interface region_interface = {
1528 region_destroy,
1529 region_add,
1530 region_subtract
1531};
1532
1533static void
1534compositor_create_region(struct wl_client *client,
1535 struct wl_resource *resource, uint32_t id)
1536{
1537 struct weston_region *region;
1538
1539 region = malloc(sizeof *region);
1540 if (region == NULL) {
1541 wl_resource_post_no_memory(resource);
1542 return;
1543 }
1544
1545 region->resource.destroy = destroy_region;
1546
1547 region->resource.object.id = id;
1548 region->resource.object.interface = &wl_region_interface;
1549 region->resource.object.implementation =
1550 (void (**)(void)) &region_interface;
1551 region->resource.data = region;
1552
1553 pixman_region32_init(&region->region);
1554
1555 wl_client_add_resource(client, &region->resource);
1556}
1557
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001558static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001559 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001560 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001561};
1562
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001563WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001565{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001566 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1567 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001568
1569 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001570 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001571}
1572
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001573static void
1574weston_compositor_dpms_on(struct weston_compositor *compositor)
1575{
1576 struct weston_output *output;
1577
1578 wl_list_for_each(output, &compositor->output_list, link)
1579 if (output->set_dpms)
1580 output->set_dpms(output, WESTON_DPMS_ON);
1581}
1582
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001583WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001584weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001585{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001586 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1587 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001588 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001589 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001590 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001591 }
1592}
1593
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001594static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001595weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001598 compositor->idle_inhibit++;
1599}
1600
1601static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001602weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001603{
1604 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001605 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001606}
1607
1608static int
1609idle_handler(void *data)
1610{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001611 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001612
1613 if (compositor->idle_inhibit)
1614 return 1;
1615
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001616 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001617
1618 return 1;
1619}
1620
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001621WL_EXPORT void
1622weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1623{
1624 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001625 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001626 plane->x = x;
1627 plane->y = y;
1628}
1629
1630WL_EXPORT void
1631weston_plane_release(struct weston_plane *plane)
1632{
1633 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001634 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001635}
1636
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001637static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001638weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001639
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001640static void
Daniel Stone37816df2012-05-16 18:45:18 +01001641clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001642{
Daniel Stone37816df2012-05-16 18:45:18 +01001643 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001644 struct weston_output *output, *prev = NULL;
1645 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001646
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001647 x = wl_fixed_to_int(*fx);
1648 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001649 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1650 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001651
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001652 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001653 if (pixman_region32_contains_point(&output->region,
1654 x, y, NULL))
1655 valid = 1;
1656 if (pixman_region32_contains_point(&output->region,
1657 old_x, old_y, NULL))
1658 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001659 }
Daniel Stone37816df2012-05-16 18:45:18 +01001660
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001661 if (!valid) {
1662 if (x < prev->x)
1663 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001664 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001665 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001666 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001667 if (y < prev->y)
1668 *fy = wl_fixed_from_int(prev->y);
1669 else if (y >= prev->y + prev->current->height)
1670 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001671 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001672 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001673}
1674
1675WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001676notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001677{
1678 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001679 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001680 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001681 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001682 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001683
1684 weston_compositor_activity(ec);
1685
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001686 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001687
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001688 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001689
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001690 pointer->x = x;
1691 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001692
1693 ix = wl_fixed_to_int(x);
1694 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001695
Scott Moreauccbf29d2012-02-22 14:21:41 -07001696 wl_list_for_each(output, &ec->output_list, link)
1697 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001698 pixman_region32_contains_point(&output->region,
1699 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001700 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001701
Daniel Stone37816df2012-05-16 18:45:18 +01001702 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001703 interface = pointer->grab->interface;
1704 interface->motion(pointer->grab, time,
1705 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001706
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001707 if (seat->sprite) {
1708 weston_surface_set_position(seat->sprite,
1709 ix - seat->hotspot_x,
1710 iy - seat->hotspot_y);
1711 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001712 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001713}
1714
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001715WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001716weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001717 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001718{
Daniel Stone37816df2012-05-16 18:45:18 +01001719 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001720
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001721 if (seat->seat.keyboard) {
1722 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1723 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001724 }
1725
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001726 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001727}
1728
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001729WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001730notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001731 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001732{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001733 struct weston_compositor *compositor = seat->compositor;
1734 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001735 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001736 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001737 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001738
Daniel Stone4dbadb12012-05-30 16:31:51 +01001739 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001740 if (compositor->ping_handler && focus)
1741 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001742 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001743 if (pointer->button_count == 0) {
1744 pointer->grab_button = button;
1745 pointer->grab_time = time;
1746 pointer->grab_x = pointer->x;
1747 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001748 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001749 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001750 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001751 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001752 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001753 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001754
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001755 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001756 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001757
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001759
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001760 if (pointer->button_count == 1)
1761 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001762 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001763}
1764
Scott Moreau210d0792012-03-22 10:47:01 -06001765WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001766notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001767 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001768{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001769 struct weston_compositor *compositor = seat->compositor;
1770 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001771 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001772 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001773 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1774
1775 if (compositor->ping_handler && focus)
1776 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001777
1778 weston_compositor_activity(compositor);
1779
Scott Moreau6a3633d2012-03-20 08:47:59 -06001780 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001781 weston_compositor_run_axis_binding(compositor, seat,
1782 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001783 else
1784 return;
1785
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001786 if (pointer->focus_resource)
1787 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001788 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001789}
1790
Daniel Stone05d58682012-06-22 13:21:38 +01001791WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001792notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001793{
Daniel Stone05d58682012-06-22 13:21:38 +01001794 struct wl_keyboard *keyboard = &seat->keyboard;
1795 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001796 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001797 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001798 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001799 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001800
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001801 /* Serialize and update our internal state, checking to see if it's
1802 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001803 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1804 XKB_STATE_DEPRESSED);
1805 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1806 XKB_STATE_LATCHED);
1807 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1808 XKB_STATE_LOCKED);
1809 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001810 XKB_STATE_EFFECTIVE);
1811
Daniel Stone71c38772012-06-22 13:21:30 +01001812 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1813 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1814 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1815 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001816 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001817
Daniel Stone71c38772012-06-22 13:21:30 +01001818 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1819 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1820 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1821 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001822
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001823 /* And update the modifier_state for bindings. */
1824 mods_lookup = mods_depressed | mods_latched;
1825 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001826 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001827 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001828 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001829 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001830 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001831 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001832 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1833 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001834
Daniel Stone8c8164f2012-05-30 16:31:45 +01001835 /* Finally, notify the compositor that LEDs have changed. */
1836 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001837 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001838 leds |= LED_NUM_LOCK;
1839 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001840 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001841 leds |= LED_CAPS_LOCK;
1842 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001843 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001844 leds |= LED_SCROLL_LOCK;
1845 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001846 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001847 seat->xkb_state.leds = leds;
1848
Daniel Stone05d58682012-06-22 13:21:38 +01001849 if (changed) {
1850 grab->interface->modifiers(grab,
1851 serial,
1852 keyboard->modifiers.mods_depressed,
1853 keyboard->modifiers.mods_latched,
1854 keyboard->modifiers.mods_locked,
1855 keyboard->modifiers.group);
1856 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001857}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001858
Daniel Stone05d58682012-06-22 13:21:38 +01001859static void
1860update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001861 enum wl_keyboard_key_state state)
1862{
1863 enum xkb_key_direction direction;
1864
1865 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1866 direction = XKB_KEY_DOWN;
1867 else
1868 direction = XKB_KEY_UP;
1869
1870 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1871 * broken keycode system, which starts at 8. */
1872 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1873
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001874 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001875}
1876
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001877WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001878notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001879 enum wl_keyboard_key_state state,
1880 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001881{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001882 struct weston_compositor *compositor = seat->compositor;
1883 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001884 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001885 (struct weston_surface *) keyboard->focus;
1886 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001887 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001888 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001889
Daniel Stonec9785ea2012-05-30 16:31:52 +01001890 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001891 if (compositor->ping_handler && focus)
1892 compositor->ping_handler(focus, serial);
1893
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001894 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001895 keyboard->grab_key = key;
1896 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001897 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001898 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001899 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001900
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001901 end = keyboard->keys.data + keyboard->keys.size;
1902 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001903 if (*k == key) {
1904 /* Ignore server-generated repeats. */
1905 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1906 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001907 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001908 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001909 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001910 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001911 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001912 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001913 *k = key;
1914 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001915
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001916 if (grab == &keyboard->default_grab) {
1917 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001918 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001919 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001920 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001921
Daniel Stone7ace3902012-05-30 16:31:40 +01001922 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001923
1924 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001925 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001926 wl_display_get_serial(compositor->wl_display),
1927 key,
1928 state);
1929 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001930}
1931
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001932WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001933notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001934 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001935{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001936 struct weston_compositor *compositor = seat->compositor;
1937 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001938
1939 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001940 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001941 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001942 x - pointer->x,
1943 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001944
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001945 pointer->x = x;
1946 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001947 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001948 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001949 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001950 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001951 /* FIXME: We should call wl_pointer_set_focus(seat,
1952 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001953 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001954}
1955
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001956static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001957destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001958{
Daniel Stone37816df2012-05-16 18:45:18 +01001959 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001960
Daniel Stone37816df2012-05-16 18:45:18 +01001961 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001962 saved_kbd_focus_listener);
1963
Daniel Stone37816df2012-05-16 18:45:18 +01001964 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001965}
1966
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001967WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001968notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001969 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001970{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001971 struct weston_compositor *compositor = seat->compositor;
1972 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001973 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001974 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001975
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001976 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001977 wl_array_copy(&keyboard->keys, keys);
1978 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001979 weston_compositor_idle_inhibit(compositor);
1980 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001981 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001982 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001983 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001984
Daniel Stoneef172672012-06-22 13:21:32 +01001985 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001986 wl_array_for_each(k, &keyboard->keys) {
1987 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001988 WL_KEYBOARD_KEY_STATE_PRESSED);
1989 }
1990
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001991 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001992
1993 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001994 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1995 wl_keyboard_set_focus(keyboard, surface);
1996 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001997 }
1998}
1999
2000WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002001notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002002{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002003 struct weston_compositor *compositor = seat->compositor;
2004 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002005 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002006
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002007 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002008 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002009 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002010 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002011 WL_KEYBOARD_KEY_STATE_RELEASED);
2012 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002013
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002014 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002015
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002016 if (keyboard->focus) {
2017 seat->saved_kbd_focus = keyboard->focus;
2018 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002019 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002020 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2021 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002022 }
2023
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002024 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002025 /* FIXME: We really need keyboard grab cancel here to
2026 * let the grab shut down properly. As it is we leak
2027 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002028 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002029}
2030
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002031static void
Daniel Stone37816df2012-05-16 18:45:18 +01002032touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002033{
Daniel Stone37816df2012-05-16 18:45:18 +01002034 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002035 struct wl_resource *resource;
2036
Pekka Paalanen56464252012-07-31 13:21:08 +03002037 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002038 return;
2039
Pekka Paalanen56464252012-07-31 13:21:08 +03002040 if (seat->touch->focus_resource)
2041 wl_list_remove(&seat->touch->focus_listener.link);
2042 seat->touch->focus = NULL;
2043 seat->touch->focus_resource = NULL;
2044
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002045 if (surface) {
2046 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002047 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002048 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002049 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002050 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002051 return;
2052 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002053
Daniel Stone37816df2012-05-16 18:45:18 +01002054 seat->touch->focus = surface;
2055 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002056 wl_signal_add(&resource->destroy_signal,
2057 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002058 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002059}
2060
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002061/**
2062 * notify_touch - emulates button touches and notifies surfaces accordingly.
2063 *
2064 * It assumes always the correct cycle sequence until it gets here: touch_down
2065 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2066 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002067 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002068 */
2069WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002070notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002071 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002072{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002073 struct weston_compositor *ec = seat->compositor;
2074 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002075 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002076 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002077 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002078
2079 /* Update grab's global coordinates. */
2080 touch->grab_x = x;
2081 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002082
2083 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002084 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002085 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002086
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002087 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002088
2089 /* the first finger down picks the surface, and all further go
2090 * to that surface for the remainder of the touch session i.e.
2091 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002092 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002093 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002094 touch_set_focus(seat, &es->surface);
2095 } else if (touch->focus) {
2096 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002097 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002098 } else {
2099 /* Unexpected condition: We have non-initial touch but
2100 * there is no focused surface.
2101 */
2102 weston_log("touch event received with %d points down"
2103 "but no surface focused\n", seat->num_tp);
2104 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002105 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002106
Matt Roper47c1b982012-10-10 16:56:53 -07002107 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002108 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002109 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002110 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002111 if (!es)
2112 break;
2113
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002114 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002115 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002116 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002117 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002118 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002119 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002120
Matt Roper47c1b982012-10-10 16:56:53 -07002121 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002122 if (seat->num_tp == 0)
2123 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002124 break;
2125 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002126}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002127
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002128static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002129pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2130{
2131 struct weston_seat *seat = container_of(listener, struct weston_seat,
2132 sprite_destroy_listener);
2133
2134 seat->sprite = NULL;
2135}
2136
2137static void
2138pointer_cursor_surface_configure(struct weston_surface *es,
2139 int32_t dx, int32_t dy)
2140{
2141 struct weston_seat *seat = es->private;
2142 int x, y;
2143
2144 assert(es == seat->sprite);
2145
2146 seat->hotspot_x -= dx;
2147 seat->hotspot_y -= dy;
2148
2149 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2150 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2151
2152 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002153 es->buffer_ref.buffer->width,
2154 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002155
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002156 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002157
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002158 if (!weston_surface_is_mapped(es)) {
2159 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2160 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002161 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002162 }
2163}
2164
2165static void
2166pointer_unmap_sprite(struct weston_seat *seat)
2167{
2168 if (weston_surface_is_mapped(seat->sprite))
2169 weston_surface_unmap(seat->sprite);
2170
2171 wl_list_remove(&seat->sprite_destroy_listener.link);
2172 seat->sprite->configure = NULL;
2173 seat->sprite->private = NULL;
2174 seat->sprite = NULL;
2175}
2176
2177static void
2178pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2179 uint32_t serial, struct wl_resource *surface_resource,
2180 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002181{
Daniel Stone37816df2012-05-16 18:45:18 +01002182 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002183 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002184
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002185 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002186 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002187
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002188 if (seat->seat.pointer->focus == NULL)
2189 return;
2190 if (seat->seat.pointer->focus->resource.client != client)
2191 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002192 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002193 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002194
2195 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002196 if (surface->configure) {
2197 wl_resource_post_error(&surface->surface.resource,
2198 WL_DISPLAY_ERROR_INVALID_OBJECT,
2199 "surface->configure already "
2200 "set");
2201 return;
2202 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002203 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002204
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002205 if (seat->sprite)
2206 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002207
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002208 if (!surface)
2209 return;
2210
2211 wl_signal_add(&surface->surface.resource.destroy_signal,
2212 &seat->sprite_destroy_listener);
2213
2214 surface->configure = pointer_cursor_surface_configure;
2215 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002216 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002217 seat->hotspot_x = x;
2218 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002219
Pekka Paalanende685b82012-12-04 15:58:12 +02002220 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002221 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002222}
2223
Daniel Stone37816df2012-05-16 18:45:18 +01002224static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002225 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002226};
2227
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002228static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002229handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002230{
Daniel Stone37816df2012-05-16 18:45:18 +01002231 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002232
Daniel Stone37816df2012-05-16 18:45:18 +01002233 seat = container_of(listener, struct weston_seat,
2234 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002235
Daniel Stone37816df2012-05-16 18:45:18 +01002236 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002237}
2238
Casey Dahlin9074db52012-04-19 22:50:09 -04002239static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002240{
2241 wl_list_remove(&resource->link);
2242 free(resource);
2243}
2244
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002245static void
Daniel Stone37816df2012-05-16 18:45:18 +01002246seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2247 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002248{
Daniel Stone37816df2012-05-16 18:45:18 +01002249 struct weston_seat *seat = resource->data;
2250 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002251
Daniel Stone37816df2012-05-16 18:45:18 +01002252 if (!seat->seat.pointer)
2253 return;
2254
2255 cr = wl_client_add_object(client, &wl_pointer_interface,
2256 &pointer_interface, id, seat);
2257 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002258 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002259
2260 if (seat->seat.pointer->focus &&
2261 seat->seat.pointer->focus->resource.client == client) {
2262 struct weston_surface *surface;
2263 wl_fixed_t sx, sy;
2264
2265 surface = (struct weston_surface *) seat->seat.pointer->focus;
2266 weston_surface_from_global_fixed(surface,
2267 seat->seat.pointer->x,
2268 seat->seat.pointer->y,
2269 &sx,
2270 &sy);
2271 wl_pointer_set_focus(seat->seat.pointer,
2272 seat->seat.pointer->focus,
2273 sx,
2274 sy);
2275 }
Daniel Stone37816df2012-05-16 18:45:18 +01002276}
2277
2278static void
2279seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2280 uint32_t id)
2281{
2282 struct weston_seat *seat = resource->data;
2283 struct wl_resource *cr;
2284
2285 if (!seat->seat.keyboard)
2286 return;
2287
2288 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2289 seat);
2290 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002291 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002292
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002293 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2294 seat->xkb_info.keymap_fd,
2295 seat->xkb_info.keymap_size);
2296
Daniel Stone17b13bd2012-05-30 16:31:39 +01002297 if (seat->seat.keyboard->focus &&
2298 seat->seat.keyboard->focus->resource.client == client) {
2299 wl_keyboard_set_focus(seat->seat.keyboard,
2300 seat->seat.keyboard->focus);
2301 }
Daniel Stone37816df2012-05-16 18:45:18 +01002302}
2303
2304static void
2305seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2306 uint32_t id)
2307{
2308 struct weston_seat *seat = resource->data;
2309 struct wl_resource *cr;
2310
2311 if (!seat->seat.touch)
2312 return;
2313
2314 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2315 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002316 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002317}
2318
2319static const struct wl_seat_interface seat_interface = {
2320 seat_get_pointer,
2321 seat_get_keyboard,
2322 seat_get_touch,
2323};
2324
2325static void
2326bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2327{
2328 struct wl_seat *seat = data;
2329 struct wl_resource *resource;
2330 enum wl_seat_capability caps = 0;
2331
2332 resource = wl_client_add_object(client, &wl_seat_interface,
2333 &seat_interface, id, data);
2334 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002335 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002336
2337 if (seat->pointer)
2338 caps |= WL_SEAT_CAPABILITY_POINTER;
2339 if (seat->keyboard)
2340 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2341 if (seat->touch)
2342 caps |= WL_SEAT_CAPABILITY_TOUCH;
2343
2344 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002345}
2346
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002347static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002348device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002349{
Daniel Stone37816df2012-05-16 18:45:18 +01002350 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002351
Daniel Stone37816df2012-05-16 18:45:18 +01002352 seat = container_of(listener, struct weston_seat,
2353 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002354
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002355 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002356}
2357
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002358static void weston_compositor_xkb_init(struct weston_compositor *ec,
2359 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002360{
Daniel Stonee379da92012-05-30 16:32:04 +01002361 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002362 ec->xkb_context = xkb_context_new(0);
2363 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002364 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002365 exit(1);
2366 }
Daniel Stone994679a2012-05-30 16:31:43 +01002367 }
2368
2369 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002370 ec->xkb_names = *names;
2371 if (!ec->xkb_names.rules)
2372 ec->xkb_names.rules = strdup("evdev");
2373 if (!ec->xkb_names.model)
2374 ec->xkb_names.model = strdup("pc105");
2375 if (!ec->xkb_names.layout)
2376 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002377}
2378
Daniel Stonee379da92012-05-30 16:32:04 +01002379static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2380{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002381 if (xkb_info->keymap)
2382 xkb_map_unref(xkb_info->keymap);
2383
2384 if (xkb_info->keymap_area)
2385 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2386 if (xkb_info->keymap_fd >= 0)
2387 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002388}
2389
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002390static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2391{
Daniel Stonee379da92012-05-30 16:32:04 +01002392 free((char *) ec->xkb_names.rules);
2393 free((char *) ec->xkb_names.model);
2394 free((char *) ec->xkb_names.layout);
2395 free((char *) ec->xkb_names.variant);
2396 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002397
Daniel Stonee379da92012-05-30 16:32:04 +01002398 xkb_info_destroy(&ec->xkb_info);
2399 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002400}
2401
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002402static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002403weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002404{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002405 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002406
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002407 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2408 XKB_MOD_NAME_SHIFT);
2409 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2410 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002411 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2412 XKB_MOD_NAME_CTRL);
2413 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2414 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002415 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2416 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002417 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2418 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002419 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002420
2421 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2422 XKB_LED_NAME_NUM);
2423 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2424 XKB_LED_NAME_CAPS);
2425 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2426 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002427
2428 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2429 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002430 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002431 exit(EXIT_FAILURE);
2432 }
2433 xkb_info->keymap_size = strlen(keymap_str) + 1;
2434
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002435 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002436 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002437 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002438 (unsigned long) xkb_info->keymap_size);
2439 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002440 }
2441
2442 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2443 PROT_READ | PROT_WRITE,
2444 MAP_SHARED, xkb_info->keymap_fd, 0);
2445 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002446 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002447 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002448 goto err_dev_zero;
2449 }
2450 strcpy(xkb_info->keymap_area, keymap_str);
2451 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002452
2453 return;
2454
2455err_dev_zero:
2456 close(xkb_info->keymap_fd);
2457 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002458err_keymap_str:
2459 free(keymap_str);
2460 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002461}
2462
2463static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002464weston_compositor_build_global_keymap(struct weston_compositor *ec)
2465{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002466 if (ec->xkb_info.keymap != NULL)
2467 return;
2468
Daniel Stonee379da92012-05-30 16:32:04 +01002469 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002470 &ec->xkb_names,
2471 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002472 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002473 weston_log("failed to compile global XKB keymap\n");
2474 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002475 "options %s",
2476 ec->xkb_names.rules, ec->xkb_names.model,
2477 ec->xkb_names.layout, ec->xkb_names.variant,
2478 ec->xkb_names.options);
2479 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002480 }
2481
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002482 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002483}
2484
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002485WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002486weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002487{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002488 if (seat->has_keyboard)
2489 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002490
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002491 if (keymap != NULL) {
2492 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002493 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002494 }
2495 else {
2496 weston_compositor_build_global_keymap(seat->compositor);
2497 seat->xkb_info = seat->compositor->xkb_info;
2498 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2499 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002500
2501 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2502 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002503 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002504 exit(1);
2505 }
2506
Daniel Stone71c38772012-06-22 13:21:30 +01002507 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002508
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002509 wl_keyboard_init(&seat->keyboard);
2510 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2511
2512 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002513}
2514
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002515WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002516weston_seat_init_pointer(struct weston_seat *seat)
2517{
2518 if (seat->has_pointer)
2519 return;
2520
2521 wl_pointer_init(&seat->pointer);
2522 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2523
2524 seat->has_pointer = 1;
2525}
2526
2527WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002528weston_seat_init_touch(struct weston_seat *seat)
2529{
2530 if (seat->has_touch)
2531 return;
2532
2533 wl_touch_init(&seat->touch);
2534 wl_seat_set_touch(&seat->seat, &seat->touch);
2535
2536 seat->has_touch = 1;
2537}
2538
2539WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002540weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002541{
Daniel Stone37816df2012-05-16 18:45:18 +01002542 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002543 seat->has_pointer = 0;
2544 seat->has_keyboard = 0;
2545 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002546
Daniel Stone37816df2012-05-16 18:45:18 +01002547 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2548 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002549
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002550 seat->sprite = NULL;
2551 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002552
Daniel Stone37816df2012-05-16 18:45:18 +01002553 seat->compositor = ec;
2554 seat->hotspot_x = 16;
2555 seat->hotspot_y = 16;
2556 seat->modifier_state = 0;
2557 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002558
Daniel Stone37816df2012-05-16 18:45:18 +01002559 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002560 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002561
Daniel Stone37816df2012-05-16 18:45:18 +01002562 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002563
Daniel Stone37816df2012-05-16 18:45:18 +01002564 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2565 wl_signal_add(&seat->seat.drag_icon_signal,
2566 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002567
2568 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002569 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002570}
2571
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002572WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002573weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002574{
Daniel Stone37816df2012-05-16 18:45:18 +01002575 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002576 /* The global object is destroyed at wl_display_destroy() time. */
2577
Daniel Stone37816df2012-05-16 18:45:18 +01002578 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002579 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002580
Daniel Stone74419a22012-05-30 16:32:02 +01002581 if (seat->xkb_state.state != NULL)
2582 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002583 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002584
Daniel Stone37816df2012-05-16 18:45:18 +01002585 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002586}
2587
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002588static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002589drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2590{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002591 empty_region(&es->pending.input);
2592
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002593 weston_surface_configure(es,
2594 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002595 es->buffer_ref.buffer->width,
2596 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002597}
2598
2599static int
Daniel Stone37816df2012-05-16 18:45:18 +01002600device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002601 struct weston_surface *surface)
2602{
Daniel Stone37816df2012-05-16 18:45:18 +01002603 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002604
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002605 if (surface->configure) {
2606 wl_resource_post_error(&surface->surface.resource,
2607 WL_DISPLAY_ERROR_INVALID_OBJECT,
2608 "surface->configure already set");
2609 return 0;
2610 }
2611
Daniel Stone37816df2012-05-16 18:45:18 +01002612 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002613
Daniel Stone37816df2012-05-16 18:45:18 +01002614 weston_surface_set_position(ws->drag_surface,
2615 wl_fixed_to_double(seat->pointer->x),
2616 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002617
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002618 surface->configure = drag_surface_configure;
2619
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002620 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002621 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002622
2623 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002624}
2625
2626static void
Daniel Stone37816df2012-05-16 18:45:18 +01002627device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002628{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002629 if (weston_surface_is_mapped(seat->drag_surface))
2630 weston_surface_unmap(seat->drag_surface);
2631
Daniel Stone37816df2012-05-16 18:45:18 +01002632 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002633 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002634 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2635 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002636}
2637
2638static void
Daniel Stone37816df2012-05-16 18:45:18 +01002639device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002640{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002641 struct wl_list *list;
2642
Daniel Stone37816df2012-05-16 18:45:18 +01002643 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002644 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002645 return;
2646
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002647 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2648 list = &seat->sprite->layer_link;
2649 else
2650 list = &seat->compositor->cursor_layer.surface_list;
2651
2652 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002653 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002654 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002655}
2656
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002657static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002658weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002659 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002660{
2661 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002662
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002663 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002664 return;
2665
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002666 if (seat->drag_surface && seat->seat.drag_surface &&
2667 (&seat->drag_surface->surface.resource !=
2668 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002669 /* between calls to this funcion we got a new drag_surface */
2670 surface_changed = 1;
2671
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002672 if (!seat->seat.drag_surface || surface_changed) {
2673 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002674 if (!surface_changed)
2675 return;
2676 }
2677
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002678 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002679 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002680 seat->seat.drag_surface;
2681 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002682 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002683 }
2684
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002685 /* the client may not have attached a buffer to the drag surface
2686 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002687 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002688
2689 if (!dx && !dy)
2690 return;
2691
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002692 weston_surface_set_position(seat->drag_surface,
2693 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2694 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002695}
2696
2697WL_EXPORT void
2698weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2699{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002700 struct weston_seat *seat;
2701
2702 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002703 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002704}
2705
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002706static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002707bind_output(struct wl_client *client,
2708 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002709{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002710 struct weston_output *output = data;
2711 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002712 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002713
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002714 resource = wl_client_add_object(client,
2715 &wl_output_interface, NULL, id, data);
2716
Casey Dahlin9074db52012-04-19 22:50:09 -04002717 wl_list_insert(&output->resource_list, &resource->link);
2718 resource->destroy = unbind_resource;
2719
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002720 wl_output_send_geometry(resource,
2721 output->x,
2722 output->y,
2723 output->mm_width,
2724 output->mm_height,
2725 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002726 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002727 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002728
2729 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002730 wl_output_send_mode(resource,
2731 mode->flags,
2732 mode->width,
2733 mode->height,
2734 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002735 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002736}
2737
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002738WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002739weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002740{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002741 struct weston_compositor *c = output->compositor;
2742
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002743 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002744 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002745 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002746
2747 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002748}
2749
Scott Moreau1bad5db2012-08-18 01:04:05 -06002750static void
2751weston_output_compute_transform(struct weston_output *output)
2752{
2753 struct weston_matrix transform;
2754 int flip;
2755
2756 weston_matrix_init(&transform);
2757
2758 switch(output->transform) {
2759 case WL_OUTPUT_TRANSFORM_FLIPPED:
2760 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2761 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2762 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2763 flip = -1;
2764 break;
2765 default:
2766 flip = 1;
2767 break;
2768 }
2769
2770 switch(output->transform) {
2771 case WL_OUTPUT_TRANSFORM_NORMAL:
2772 case WL_OUTPUT_TRANSFORM_FLIPPED:
2773 transform.d[0] = flip;
2774 transform.d[1] = 0;
2775 transform.d[4] = 0;
2776 transform.d[5] = 1;
2777 break;
2778 case WL_OUTPUT_TRANSFORM_90:
2779 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2780 transform.d[0] = 0;
2781 transform.d[1] = -flip;
2782 transform.d[4] = 1;
2783 transform.d[5] = 0;
2784 break;
2785 case WL_OUTPUT_TRANSFORM_180:
2786 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2787 transform.d[0] = -flip;
2788 transform.d[1] = 0;
2789 transform.d[4] = 0;
2790 transform.d[5] = -1;
2791 break;
2792 case WL_OUTPUT_TRANSFORM_270:
2793 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2794 transform.d[0] = 0;
2795 transform.d[1] = flip;
2796 transform.d[4] = -1;
2797 transform.d[5] = 0;
2798 break;
2799 default:
2800 break;
2801 }
2802
2803 weston_matrix_multiply(&output->matrix, &transform);
2804}
2805
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002806WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002807weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002808{
Scott Moreau850ca422012-05-21 15:21:25 -06002809 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002810 struct weston_matrix camera;
2811 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002812
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002813 weston_matrix_init(&output->matrix);
2814 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002815 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2816 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002817
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002818 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002819 2.0 / (output->width + output->border.left + output->border.right),
2820 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2821
2822 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002823
Scott Moreauccbf29d2012-02-22 14:21:41 -07002824 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002825 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002826 weston_matrix_init(&camera);
2827 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002828 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002829 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002830 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002831 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002832 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002833 weston_matrix_multiply(&output->matrix, &modelview);
2834 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002835
Scott Moreauccbf29d2012-02-22 14:21:41 -07002836 output->dirty = 0;
2837}
2838
Scott Moreau1bad5db2012-08-18 01:04:05 -06002839static void
2840weston_output_transform_init(struct weston_output *output, uint32_t transform)
2841{
2842 output->transform = transform;
2843
2844 switch (transform) {
2845 case WL_OUTPUT_TRANSFORM_90:
2846 case WL_OUTPUT_TRANSFORM_270:
2847 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2848 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2849 /* Swap width and height */
2850 output->width = output->current->height;
2851 output->height = output->current->width;
2852 break;
2853 case WL_OUTPUT_TRANSFORM_NORMAL:
2854 case WL_OUTPUT_TRANSFORM_180:
2855 case WL_OUTPUT_TRANSFORM_FLIPPED:
2856 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2857 output->width = output->current->width;
2858 output->height = output->current->height;
2859 break;
2860 default:
2861 break;
2862 }
2863}
2864
Scott Moreauccbf29d2012-02-22 14:21:41 -07002865WL_EXPORT void
2866weston_output_move(struct weston_output *output, int x, int y)
2867{
2868 output->x = x;
2869 output->y = y;
2870
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002871 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002872 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002873 output->width,
2874 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002875}
2876
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002877WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002878weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002879 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002880{
2881 output->compositor = c;
2882 output->x = x;
2883 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002884 output->border.top = 0;
2885 output->border.bottom = 0;
2886 output->border.left = 0;
2887 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002888 output->mm_width = width;
2889 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002890 output->dirty = 1;
2891
Scott Moreau1bad5db2012-08-18 01:04:05 -06002892 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002893 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002894
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002895 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002896 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002897
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002898 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002899 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002900 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002901
Casey Dahlin58ba1372012-04-19 22:50:08 -04002902 output->id = ffs(~output->compositor->output_id_pool) - 1;
2903 output->compositor->output_id_pool |= 1 << output->id;
2904
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002905 output->global =
2906 wl_display_add_global(c->wl_display, &wl_output_interface,
2907 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002908}
2909
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002910static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002911compositor_bind(struct wl_client *client,
2912 void *data, uint32_t version, uint32_t id)
2913{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002914 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002915
2916 wl_client_add_object(client, &wl_compositor_interface,
2917 &compositor_interface, id, compositor);
2918}
2919
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002920static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002921log_uname(void)
2922{
2923 struct utsname usys;
2924
2925 uname(&usys);
2926
2927 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2928 usys.version, usys.machine);
2929}
2930
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002931WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002932weston_compositor_init(struct weston_compositor *ec,
2933 struct wl_display *display,
2934 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002935 char *argv[],
2936 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002937{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002938 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002939 struct xkb_rule_names xkb_names;
2940 const struct config_key keyboard_config_keys[] = {
2941 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2942 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2943 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2944 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2945 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2946 };
2947 const struct config_section cs[] = {
2948 { "keyboard",
2949 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2950 };
2951
2952 memset(&xkb_names, 0, sizeof(xkb_names));
2953 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002954
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002955 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002956 wl_signal_init(&ec->destroy_signal);
2957 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002958 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002959 wl_signal_init(&ec->lock_signal);
2960 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002961 wl_signal_init(&ec->show_input_panel_signal);
2962 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002963 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002964 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002965
Casey Dahlin58ba1372012-04-19 22:50:08 -04002966 ec->output_id_pool = 0;
2967
Kristian Høgsberga8873122011-11-23 10:39:34 -05002968 if (!wl_display_add_global(display, &wl_compositor_interface,
2969 ec, compositor_bind))
2970 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002971
Daniel Stone725c2c32012-06-22 14:04:36 +01002972 wl_list_init(&ec->surface_list);
2973 wl_list_init(&ec->layer_list);
2974 wl_list_init(&ec->seat_list);
2975 wl_list_init(&ec->output_list);
2976 wl_list_init(&ec->key_binding_list);
2977 wl_list_init(&ec->button_binding_list);
2978 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002979 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002980 wl_list_init(&ec->fade.animation.link);
2981
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002982 weston_plane_init(&ec->primary_plane, 0, 0);
2983
Daniel Stone725c2c32012-06-22 14:04:36 +01002984 weston_compositor_xkb_init(ec, &xkb_names);
2985
2986 ec->ping_handler = NULL;
2987
2988 screenshooter_create(ec);
2989 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002990 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002991
2992 wl_data_device_manager_init(ec->wl_display);
2993
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002994 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002995
Daniel Stone725c2c32012-06-22 14:04:36 +01002996 loop = wl_display_get_event_loop(ec->wl_display);
2997 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2998 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2999
3000 ec->input_loop = wl_event_loop_create();
3001
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04003002 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
3003 ec->fade.animation.frame = fade_frame;
3004
3005 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3006 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3007
3008 weston_compositor_schedule_repaint(ec);
3009
Daniel Stone725c2c32012-06-22 14:04:36 +01003010 return 0;
3011}
3012
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003013WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003014weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003015{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003016 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003017
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003018 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003019 if (ec->input_loop_source)
3020 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003021
Matt Roper361d2ad2011-08-29 13:52:23 -07003022 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003023 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003024 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003025
Daniel Stone325fc2d2012-05-30 16:31:58 +01003026 weston_binding_list_destroy_all(&ec->key_binding_list);
3027 weston_binding_list_destroy_all(&ec->button_binding_list);
3028 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003029 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003030
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003031 weston_plane_release(&ec->primary_plane);
3032
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003033 wl_array_release(&ec->vertices);
3034 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003035 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003036
3037 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003038}
3039
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003040static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003041{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003042 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003043
Martin Minarik6d118362012-06-07 18:01:59 +02003044 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003045 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003046
3047 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003048}
3049
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003050static void
3051on_segv_signal(int s, siginfo_t *siginfo, void *context)
3052{
3053 void *buffer[32];
3054 int i, count;
3055 Dl_info info;
3056
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003057 /* This SIGSEGV handler will do a best-effort backtrace, and
3058 * then call the backend restore function, which will switch
3059 * back to the vt we launched from or ungrab X etc and then
3060 * raise SIGTRAP. If we run weston under gdb from X or a
3061 * different vt, and tell gdb "handle SIGSEGV nostop", this
3062 * will allow weston to switch back to gdb on crash and then
3063 * gdb will catch the crash with SIGTRAP. */
3064
Martin Minarik6d118362012-06-07 18:01:59 +02003065 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003066
3067 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3068 for (i = 0; i < count; i++) {
3069 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003070 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003071 (long) buffer[i],
3072 info.dli_sname ? info.dli_sname : "--",
3073 info.dli_fname);
3074 }
3075
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003076 segv_compositor->restore(segv_compositor);
3077
3078 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003079}
3080
3081
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003082static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003083load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003084{
3085 char path[PATH_MAX];
3086 void *module, *init;
3087
3088 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003089 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003090 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003091 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003092
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003093 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3094 if (module) {
3095 weston_log("Module '%s' already loaded\n", path);
3096 dlclose(module);
3097 return NULL;
3098 }
3099
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003100 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003101 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003102 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003103 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003104 return NULL;
3105 }
3106
3107 init = dlsym(module, entrypoint);
3108 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003109 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003110 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003111 return NULL;
3112 }
3113
3114 return init;
3115}
3116
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003117static int
3118load_modules(struct weston_compositor *ec, const char *modules)
3119{
3120 const char *p, *end;
3121 char buffer[256];
3122 int (*module_init)(struct weston_compositor *ec);
3123
3124 if (modules == NULL)
3125 return 0;
3126
3127 p = modules;
3128 while (*p) {
3129 end = strchrnul(p, ',');
3130 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3131 module_init = load_module(buffer, "module_init");
3132 if (module_init)
3133 module_init(ec);
3134 p = end;
3135 while (*p == ',')
3136 p++;
3137
3138 }
3139
3140 return 0;
3141}
3142
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003143static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003144 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3145
3146static const char xdg_wrong_message[] =
3147 "fatal: environment variable XDG_RUNTIME_DIR\n"
3148 "is set to \"%s\", which is not a directory.\n";
3149
3150static const char xdg_wrong_mode_message[] =
3151 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3152 "correctly. Unix access mode must be 0700 but is %o,\n"
3153 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003154 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003155
3156static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003157 "Refer to your distribution on how to get it, or\n"
3158 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3159 "on how to implement it.\n";
3160
Martin Minarik37032f82012-06-18 20:15:18 +02003161static void
3162verify_xdg_runtime_dir(void)
3163{
3164 char *dir = getenv("XDG_RUNTIME_DIR");
3165 struct stat s;
3166
3167 if (!dir) {
3168 weston_log(xdg_error_message);
3169 weston_log_continue(xdg_detail_message);
3170 exit(EXIT_FAILURE);
3171 }
3172
3173 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3174 weston_log(xdg_wrong_message, dir);
3175 weston_log_continue(xdg_detail_message);
3176 exit(EXIT_FAILURE);
3177 }
3178
3179 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3180 weston_log(xdg_wrong_mode_message,
3181 dir, s.st_mode & 0777, s.st_uid);
3182 weston_log_continue(xdg_detail_message);
3183 }
3184}
3185
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003186static int
3187usage(int error_code)
3188{
3189 fprintf(stderr,
3190 "Usage: weston [OPTIONS]\n\n"
3191 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3192 "Weston supports multiple backends, and depending on which backend is in use\n"
3193 "different options will be accepted.\n\n"
3194
3195
3196 "Core options:\n\n"
3197 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3198 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3199 " -S, --socket=NAME\tName of socket to listen on\n"
3200 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003201 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003202 " --log==FILE\t\tLog to the given file\n"
3203 " -h, --help\t\tThis help message\n\n");
3204
3205 fprintf(stderr,
3206 "Options for drm-backend.so:\n\n"
3207 " --connector=ID\tBring up only this connector\n"
3208 " --seat=SEAT\t\tThe seat that weston should run on\n"
3209 " --tty=TTY\t\tThe tty to use\n"
3210 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3211
3212 fprintf(stderr,
3213 "Options for x11-backend.so:\n\n"
3214 " --width=WIDTH\t\tWidth of X window\n"
3215 " --height=HEIGHT\tHeight of X window\n"
3216 " --fullscreen\t\tRun in fullscreen mode\n"
3217 " --output-count=COUNT\tCreate multiple outputs\n"
3218 " --no-input\t\tDont create input devices\n\n");
3219
3220 fprintf(stderr,
3221 "Options for wayland-backend.so:\n\n"
3222 " --width=WIDTH\t\tWidth of Wayland surface\n"
3223 " --height=HEIGHT\tHeight of Wayland surface\n"
3224 " --display=DISPLAY\tWayland display to connect to\n\n");
3225
3226 exit(error_code);
3227}
3228
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003229int main(int argc, char *argv[])
3230{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003231 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003232 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003233 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003234 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003235 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003236 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003237 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003238 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003239 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003240 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003241 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003242 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003243 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003244 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003245 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003246 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003247 char *config_file;
3248
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003249 const struct config_key core_config_keys[] = {
3250 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003251 };
3252
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003253 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003254 { "core",
3255 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003256 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003257
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003258 const struct weston_option core_options[] = {
3259 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3260 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3261 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003262 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003263 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003264 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003265 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003266
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003267 argc = parse_options(core_options,
3268 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003269
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003270 if (help)
3271 usage(EXIT_SUCCESS);
3272
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003273 weston_log_file_open(log);
3274
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003275 weston_log("%s\n"
3276 STAMP_SPACE "%s\n"
3277 STAMP_SPACE "Bug reports to: %s\n"
3278 STAMP_SPACE "Build: %s\n",
3279 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003280 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003281 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003282
Martin Minarik37032f82012-06-18 20:15:18 +02003283 verify_xdg_runtime_dir();
3284
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003285 display = wl_display_create();
3286
Tiago Vignatti2116b892011-08-08 05:52:59 -07003287 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003288 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3289 display);
3290 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3291 display);
3292 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3293 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003294
3295 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003296 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3297 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003298
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003299 if (!backend) {
3300 if (getenv("WAYLAND_DISPLAY"))
3301 backend = "wayland-backend.so";
3302 else if (getenv("DISPLAY"))
3303 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003304 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003305 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003306 }
3307
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003308 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003309 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003310
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003311 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003312 if (!backend_init)
3313 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003314
Daniel Stonec1be8e52012-06-01 11:14:02 -04003315 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003316 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003317 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003318 exit(EXIT_FAILURE);
3319 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003320
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003321 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3322 segv_action.sa_sigaction = on_segv_signal;
3323 sigemptyset(&segv_action.sa_mask);
3324 sigaction(SIGSEGV, &segv_action, NULL);
3325 segv_compositor = ec;
3326
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003327 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003328 weston_log("fatal: unhandled option: %s\n", argv[i]);
3329 if (argv[1]) {
3330 ret = EXIT_FAILURE;
3331 goto out;
3332 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003333
Daniel Stonec1be8e52012-06-01 11:14:02 -04003334 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003335
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003336 ec->option_idle_time = idle_time;
3337 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003338
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003339 setenv("WAYLAND_DISPLAY", socket_name, 1);
3340
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003341 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003342 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003343 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003344 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003345
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003346 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003347 weston_log("fatal: failed to add socket: %m\n");
3348 ret = EXIT_FAILURE;
3349 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003350 }
3351
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003352 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003353 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003354
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003355 wl_display_run(display);
3356
3357 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003358 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003359 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003360
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003361 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003362
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003363 for (i = ARRAY_LENGTH(signals); i;)
3364 wl_event_source_remove(signals[--i]);
3365
Daniel Stone855028f2012-05-01 20:37:10 +01003366 weston_compositor_xkb_destroy(ec);
3367
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003368 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003369 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003370
Martin Minarik19e6f262012-06-07 13:08:46 +02003371 weston_log_file_close();
3372
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003373 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003374}