blob: 567105ed7d992bbdabcd1a00d2091c9da684ca83 [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 Oliveira5c38ef42012-12-14 13:37:25 -020093 int ret;
94
Alex Wu2dda6042012-04-17 17:20:47 +080095 if (!output->switch_mode)
96 return -1;
97
Ander Conselvan de Oliveira5c38ef42012-12-14 13:37:25 -020098 ret = output->switch_mode(output, mode);
99 if (ret < 0)
100 return ret;
101
102 /* Update output region and transformation matrix */
103 weston_output_transform_init(output, output->transform);
104
105 pixman_region32_init(&output->previous_damage);
106 pixman_region32_init_rect(&output->region, output->x, output->y,
107 output->width, output->height);
108
109 weston_output_update_matrix(output);
110
111 return ret;
Alex Wu2dda6042012-04-17 17:20:47 +0800112}
113
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400114WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500115weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -0400116{
117 wl_list_insert(&child_process_list, &process->link);
118}
119
Benjamin Franzke06286262011-05-06 19:12:33 +0200120static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200121child_client_exec(int sockfd, const char *path)
122{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500123 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200124 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200125 sigset_t allsigs;
126
127 /* do not give our signal mask to the new process */
128 sigfillset(&allsigs);
129 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200130
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500131 /* Launch clients as the user. */
132 seteuid(getuid());
133
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500134 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
135 * non-CLOEXEC fd to pass through exec. */
136 clientfd = dup(sockfd);
137 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200138 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500139 return;
140 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200141
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500142 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200143 setenv("WAYLAND_SOCKET", s, 1);
144
145 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200146 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200147 path);
148}
149
150WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500151weston_client_launch(struct weston_compositor *compositor,
152 struct weston_process *proc,
153 const char *path,
154 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200155{
156 int sv[2];
157 pid_t pid;
158 struct wl_client *client;
159
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300160 weston_log("launching '%s'\n", path);
161
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300162 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200163 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200164 "socketpair failed while launching '%s': %m\n",
165 path);
166 return NULL;
167 }
168
169 pid = fork();
170 if (pid == -1) {
171 close(sv[0]);
172 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200173 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200174 "fork failed while launching '%s': %m\n", path);
175 return NULL;
176 }
177
178 if (pid == 0) {
179 child_client_exec(sv[1], path);
180 exit(-1);
181 }
182
183 close(sv[1]);
184
185 client = wl_client_create(compositor->wl_display, sv[0]);
186 if (!client) {
187 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200188 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200189 "wl_client_create failed while launching '%s'.\n",
190 path);
191 return NULL;
192 }
193
194 proc->pid = pid;
195 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500196 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200197
198 return client;
199}
200
201static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300202surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
203{
204 struct weston_surface *surface =
205 container_of(listener, struct weston_surface,
206 pending.buffer_destroy_listener);
207
208 surface->pending.buffer = NULL;
209}
210
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200211static void
212empty_region(pixman_region32_t *region)
213{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300214 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200215 pixman_region32_init(region);
216}
217
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300218static void
219region_init_infinite(pixman_region32_t *region)
220{
221 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
222 UINT32_MAX, UINT32_MAX);
223}
224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500226weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500227{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200230 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400231 if (surface == NULL)
232 return NULL;
233
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400234 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500235
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500236 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500237 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500238
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400239 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400240
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500241 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400242 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400243
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100244 if (compositor->renderer->create_surface(surface) < 0) {
245 free(surface);
246 return NULL;
247 }
248
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200249 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
250 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400251 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400252 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200253
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400254 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500255 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500256 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300257 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200258 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500259 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400260
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200261 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200262 wl_list_insert(&surface->geometry.transformation_list,
263 &surface->transform.position.link);
264 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200265 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200266 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500267
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300268 surface->pending.buffer_destroy_listener.notify =
269 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300270 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300271 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300272 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300273 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300274
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400275 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500276}
277
Alex Wu8811bf92012-02-28 18:07:54 +0800278WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500279weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200280 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500281{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100282 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500283}
284
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400285WL_EXPORT void
286weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200287 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200288{
289 if (surface->transform.enabled) {
290 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
291
292 weston_matrix_transform(&surface->transform.matrix, &v);
293
294 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200295 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200296 "weston_surface_to_global(), divisor = %g\n",
297 v.f[3]);
298 *x = 0;
299 *y = 0;
300 return;
301 }
302
303 *x = v.f[0] / v.f[3];
304 *y = v.f[1] / v.f[3];
305 } else {
306 *x = sx + surface->geometry.x;
307 *y = sy + surface->geometry.y;
308 }
309}
310
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500311WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200312weston_surface_to_buffer_float(struct weston_surface *surface,
313 float sx, float sy, float *bx, float *by)
314{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200315 weston_transformed_coord(surface->geometry.width,
316 surface->geometry.height,
317 surface->buffer_transform,
318 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200319}
320
321WL_EXPORT pixman_box32_t
322weston_surface_to_buffer_rect(struct weston_surface *surface,
323 pixman_box32_t rect)
324{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200325 return weston_transformed_rect(surface->geometry.width,
326 surface->geometry.height,
327 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200328}
329
330WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400331weston_surface_move_to_plane(struct weston_surface *surface,
332 struct weston_plane *plane)
333{
334 if (surface->plane == plane)
335 return;
336
337 weston_surface_damage_below(surface);
338 surface->plane = plane;
339 weston_surface_damage(surface);
340}
341
342WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500343weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200344{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500345 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200346
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500347 pixman_region32_init(&damage);
348 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
349 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400350 pixman_region32_union(&surface->plane->damage,
351 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500352 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200353}
354
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400355static struct wl_resource *
356find_resource_for_client(struct wl_list *list, struct wl_client *client)
357{
358 struct wl_resource *r;
359
360 wl_list_for_each(r, list, link) {
361 if (r->client == client)
362 return r;
363 }
364
365 return NULL;
366}
367
368static void
369weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
370{
371 uint32_t different = es->output_mask ^ mask;
372 uint32_t entered = mask & different;
373 uint32_t left = es->output_mask & different;
374 struct weston_output *output;
375 struct wl_resource *resource = NULL;
376 struct wl_client *client = es->surface.resource.client;
377
378 es->output_mask = mask;
379 if (es->surface.resource.client == NULL)
380 return;
381 if (different == 0)
382 return;
383
384 wl_list_for_each(output, &es->compositor->output_list, link) {
385 if (1 << output->id & different)
386 resource =
387 find_resource_for_client(&output->resource_list,
388 client);
389 if (resource == NULL)
390 continue;
391 if (1 << output->id & entered)
392 wl_surface_send_enter(&es->surface.resource, resource);
393 if (1 << output->id & left)
394 wl_surface_send_leave(&es->surface.resource, resource);
395 }
396}
397
398static void
399weston_surface_assign_output(struct weston_surface *es)
400{
401 struct weston_compositor *ec = es->compositor;
402 struct weston_output *output, *new_output;
403 pixman_region32_t region;
404 uint32_t max, area, mask;
405 pixman_box32_t *e;
406
407 new_output = NULL;
408 max = 0;
409 mask = 0;
410 pixman_region32_init(&region);
411 wl_list_for_each(output, &ec->output_list, link) {
412 pixman_region32_intersect(&region, &es->transform.boundingbox,
413 &output->region);
414
415 e = pixman_region32_extents(&region);
416 area = (e->x2 - e->x1) * (e->y2 - e->y1);
417
418 if (area > 0)
419 mask |= 1 << output->id;
420
421 if (area >= max) {
422 new_output = output;
423 max = area;
424 }
425 }
426 pixman_region32_fini(&region);
427
428 es->output = new_output;
429 weston_surface_update_output_mask(es, mask);
430}
431
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200432static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200433surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
434 int32_t width, int32_t height,
435 pixman_region32_t *bbox)
436{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200437 float min_x = HUGE_VALF, min_y = HUGE_VALF;
438 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200439 int32_t s[4][2] = {
440 { sx, sy },
441 { sx, sy + height },
442 { sx + width, sy },
443 { sx + width, sy + height }
444 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200445 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200446 int i;
447
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300448 if (width == 0 || height == 0) {
449 /* avoid rounding empty bbox to 1x1 */
450 pixman_region32_init(bbox);
451 return;
452 }
453
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200454 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200455 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400456 weston_surface_to_global_float(surface,
457 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200458 if (x < min_x)
459 min_x = x;
460 if (x > max_x)
461 max_x = x;
462 if (y < min_y)
463 min_y = y;
464 if (y > max_y)
465 max_y = y;
466 }
467
Pekka Paalanen219b9822012-02-08 15:38:37 +0200468 int_x = floorf(min_x);
469 int_y = floorf(min_y);
470 pixman_region32_init_rect(bbox, int_x, int_y,
471 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200472}
473
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200474static void
475weston_surface_update_transform_disable(struct weston_surface *surface)
476{
477 surface->transform.enabled = 0;
478
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200479 /* round off fractions when not transformed */
480 surface->geometry.x = roundf(surface->geometry.x);
481 surface->geometry.y = roundf(surface->geometry.y);
482
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200483 pixman_region32_init_rect(&surface->transform.boundingbox,
484 surface->geometry.x,
485 surface->geometry.y,
486 surface->geometry.width,
487 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500488
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400489 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500490 pixman_region32_copy(&surface->transform.opaque,
491 &surface->opaque);
492 pixman_region32_translate(&surface->transform.opaque,
493 surface->geometry.x,
494 surface->geometry.y);
495 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200496}
497
498static int
499weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200500{
501 struct weston_matrix *matrix = &surface->transform.matrix;
502 struct weston_matrix *inverse = &surface->transform.inverse;
503 struct weston_transform *tform;
504
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200505 surface->transform.enabled = 1;
506
507 /* Otherwise identity matrix, but with x and y translation. */
508 surface->transform.position.matrix.d[12] = surface->geometry.x;
509 surface->transform.position.matrix.d[13] = surface->geometry.y;
510
511 weston_matrix_init(matrix);
512 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
513 weston_matrix_multiply(matrix, &tform->matrix);
514
515 if (weston_matrix_invert(inverse, matrix) < 0) {
516 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200517 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200518 " transformation not invertible.\n", surface);
519 return -1;
520 }
521
522 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
523 surface->geometry.height,
524 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500525
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200526 return 0;
527}
528
529WL_EXPORT void
530weston_surface_update_transform(struct weston_surface *surface)
531{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200532 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200533 return;
534
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200535 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200536
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500537 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200538
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200539 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500540 pixman_region32_fini(&surface->transform.opaque);
541 pixman_region32_init(&surface->transform.opaque);
542
Pekka Paalanencd403622012-01-25 13:37:39 +0200543 /* transform.position is always in transformation_list */
544 if (surface->geometry.transformation_list.next ==
545 &surface->transform.position.link &&
546 surface->geometry.transformation_list.prev ==
547 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200548 weston_surface_update_transform_disable(surface);
549 } else {
550 if (weston_surface_update_transform_enable(surface) < 0)
551 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200552 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200553
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400554 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200555
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300556 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200557}
558
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200559WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100560weston_surface_to_global_fixed(struct weston_surface *surface,
561 wl_fixed_t sx, wl_fixed_t sy,
562 wl_fixed_t *x, wl_fixed_t *y)
563{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200564 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100565
566 weston_surface_to_global_float(surface,
567 wl_fixed_to_double(sx),
568 wl_fixed_to_double(sy),
569 &xf, &yf);
570 *x = wl_fixed_from_double(xf);
571 *y = wl_fixed_from_double(yf);
572}
573
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400574WL_EXPORT void
575weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200576 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200577{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200578 if (surface->transform.enabled) {
579 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
580
581 weston_matrix_transform(&surface->transform.inverse, &v);
582
583 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200584 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200585 "weston_surface_from_global(), divisor = %g\n",
586 v.f[3]);
587 *sx = 0;
588 *sy = 0;
589 return;
590 }
591
Pekka Paalanencd403622012-01-25 13:37:39 +0200592 *sx = v.f[0] / v.f[3];
593 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200594 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200595 *sx = x - surface->geometry.x;
596 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200597 }
598}
599
600WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100601weston_surface_from_global_fixed(struct weston_surface *surface,
602 wl_fixed_t x, wl_fixed_t y,
603 wl_fixed_t *sx, wl_fixed_t *sy)
604{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200605 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100606
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400607 weston_surface_from_global_float(surface,
608 wl_fixed_to_double(x),
609 wl_fixed_to_double(y),
610 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100611 *sx = wl_fixed_from_double(sxf);
612 *sy = wl_fixed_from_double(syf);
613}
614
615WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200616weston_surface_from_global(struct weston_surface *surface,
617 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
618{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200619 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200620
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400621 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200622 *sx = floorf(sxf);
623 *sy = floorf(syf);
624}
625
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400626WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400627weston_surface_schedule_repaint(struct weston_surface *surface)
628{
629 struct weston_output *output;
630
631 wl_list_for_each(output, &surface->compositor->output_list, link)
632 if (surface->output_mask & (1 << output->id))
633 weston_output_schedule_repaint(output);
634}
635
636WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500637weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500638{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400639 pixman_region32_union_rect(&surface->damage, &surface->damage,
640 0, 0, surface->geometry.width,
641 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200642
Kristian Høgsberg98238702012-08-03 16:29:12 -0400643 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500644}
645
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400646WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500647weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200648 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400649{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200650 surface->geometry.x = x;
651 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200652 surface->geometry.width = width;
653 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200654 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400655}
656
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200657WL_EXPORT void
658weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200659 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200660{
661 surface->geometry.x = x;
662 surface->geometry.y = y;
663 surface->geometry.dirty = 1;
664}
665
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300666WL_EXPORT int
667weston_surface_is_mapped(struct weston_surface *surface)
668{
669 if (surface->output)
670 return 1;
671 else
672 return 0;
673}
674
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200675WL_EXPORT int32_t
676weston_surface_buffer_width(struct weston_surface *surface)
677{
678 switch (surface->buffer_transform) {
679 case WL_OUTPUT_TRANSFORM_90:
680 case WL_OUTPUT_TRANSFORM_270:
681 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
682 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200683 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200684 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200685 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200686 }
687}
688
689WL_EXPORT int32_t
690weston_surface_buffer_height(struct weston_surface *surface)
691{
692 switch (surface->buffer_transform) {
693 case WL_OUTPUT_TRANSFORM_90:
694 case WL_OUTPUT_TRANSFORM_270:
695 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
696 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200697 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200698 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200699 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200700 }
701}
702
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400703WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500704weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500705{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400706 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500707
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400708 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500709
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400710 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500711}
712
Tiago Vignatti9d393522012-02-10 16:26:19 +0200713static struct weston_surface *
714weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100715 wl_fixed_t x, wl_fixed_t y,
716 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200717{
718 struct weston_surface *surface;
719
720 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100721 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500722 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100723 wl_fixed_to_int(*sx),
724 wl_fixed_to_int(*sy),
725 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200726 return surface;
727 }
728
729 return NULL;
730}
731
732static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400733weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500734{
Scott Moreau447013d2012-02-18 05:05:29 -0700735 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500736 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400737 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500738
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400739 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100740 return;
741
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400742 surface = weston_compositor_pick_surface(seat->compositor,
743 pointer->x,
744 pointer->y,
745 &pointer->current_x,
746 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500747
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400748 if (&surface->surface != pointer->current) {
749 interface = pointer->grab->interface;
750 pointer->current = &surface->surface;
751 interface->focus(pointer->grab, &surface->surface,
752 pointer->current_x,
753 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500754 }
755
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400756 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500757 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100758 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400759 pointer->x,
760 pointer->y,
761 &pointer->grab->x,
762 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500763}
764
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400765static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500766weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400767{
Daniel Stone37816df2012-05-16 18:45:18 +0100768 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400769
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500770 if (!compositor->focus)
771 return;
772
Daniel Stone37816df2012-05-16 18:45:18 +0100773 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400774 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400775}
776
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400777WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500778weston_surface_unmap(struct weston_surface *surface)
779{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100780 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500781
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500782 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500783 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500784 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500785
Daniel Stone4dab5db2012-05-30 16:31:53 +0100786 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100787 if (seat->seat.keyboard &&
788 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100789 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100790 if (seat->seat.pointer &&
791 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100792 wl_pointer_set_focus(seat->seat.pointer,
793 NULL,
794 wl_fixed_from_int(0),
795 wl_fixed_from_int(0));
796 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500797
Kristian Høgsberg98238702012-08-03 16:29:12 -0400798 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500799}
800
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400801struct weston_frame_callback {
802 struct wl_resource resource;
803 struct wl_list link;
804};
805
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500806static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400807destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500808{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500809 struct weston_surface *surface =
810 container_of(resource,
811 struct weston_surface, surface.resource);
812 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400813 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400814
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300815 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500816 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400817
Pekka Paalanenbc106382012-10-10 12:49:31 +0300818 wl_list_for_each_safe(cb, next,
819 &surface->pending.frame_callback_list, link)
820 wl_resource_destroy(&cb->resource);
821
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300822 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300823 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300824 pixman_region32_fini(&surface->pending.damage);
825
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300826 if (surface->pending.buffer)
827 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
828
Pekka Paalanende685b82012-12-04 15:58:12 +0200829 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400830
Kristian Høgsberg42263852012-09-06 21:59:29 -0400831 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200832
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200833 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200834 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500835 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500836 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300837 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200838
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400839 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
840 wl_resource_destroy(&cb->resource);
841
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400842 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500843}
844
Alex Wu8811bf92012-02-28 18:07:54 +0800845WL_EXPORT void
846weston_surface_destroy(struct weston_surface *surface)
847{
848 /* Not a valid way to destroy a client surface */
849 assert(surface->surface.resource.client == NULL);
850
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400851 wl_signal_emit(&surface->surface.resource.destroy_signal,
852 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800853 destroy_surface(&surface->surface.resource);
854}
855
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100856static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200857weston_buffer_reference_handle_destroy(struct wl_listener *listener,
858 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100859{
Pekka Paalanende685b82012-12-04 15:58:12 +0200860 struct weston_buffer_reference *ref =
861 container_of(listener, struct weston_buffer_reference,
862 destroy_listener);
863
864 assert((struct wl_buffer *)data == ref->buffer);
865 ref->buffer = NULL;
866}
867
868WL_EXPORT void
869weston_buffer_reference(struct weston_buffer_reference *ref,
870 struct wl_buffer *buffer)
871{
872 if (ref->buffer && buffer != ref->buffer) {
873 weston_buffer_post_release(ref->buffer);
874 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300875 }
876
Pekka Paalanende685b82012-12-04 15:58:12 +0200877 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400878 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300879 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200880 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200881 }
882
Pekka Paalanende685b82012-12-04 15:58:12 +0200883 ref->buffer = buffer;
884 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
885}
886
887static void
888weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
889{
890 weston_buffer_reference(&surface->buffer_ref, buffer);
891
Pekka Paalanena6421c42012-12-04 15:58:10 +0200892 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300893 if (weston_surface_is_mapped(surface))
894 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300895 }
896
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300897 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100898}
899
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500900WL_EXPORT void
901weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400902{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500903 wl_list_remove(&surface->layer_link);
904 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500905 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500906 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400907}
908
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400909WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500910weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400911{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500912 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400913
914 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500915 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400916}
917
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500918WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500919weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200920{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400921 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200922 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200923
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400924 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500925 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200926}
927
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400928WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500929weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400930{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500931 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400932
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400933 pixman_region32_union(&compositor->primary_plane.damage,
934 &compositor->primary_plane.damage,
935 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400936 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400937}
938
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400939static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500940fade_frame(struct weston_animation *animation,
941 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400942{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500943 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400944 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500945 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500946 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400947
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600948 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600949 compositor->fade.spring.timestamp = msecs;
950
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500951 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500953 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
954 compositor->fade.spring.current);
955 weston_surface_damage(surface);
956
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400958 compositor->fade.spring.current =
959 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400960 wl_list_remove(&animation->link);
961 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200962
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500963 if (compositor->fade.spring.current < 0.001) {
964 destroy_surface(&surface->surface.resource);
965 compositor->fade.surface = NULL;
966 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500967 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400968 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200969 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400970 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400971}
972
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400973static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400974surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400975 pixman_region32_t *opaque)
976{
Pekka Paalanende685b82012-12-04 15:58:12 +0200977 if (surface->buffer_ref.buffer &&
978 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400979 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400980
981 if (surface->transform.enabled) {
982 pixman_box32_t *extents;
983
984 extents = pixman_region32_extents(&surface->damage);
985 surface_compute_bbox(surface, extents->x1, extents->y1,
986 extents->x2 - extents->x1,
987 extents->y2 - extents->y1,
988 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400989 pixman_region32_translate(&surface->damage,
990 -surface->plane->x,
991 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400992 } else {
993 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400994 surface->geometry.x - surface->plane->x,
995 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400996 }
997
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +0200998 pixman_region32_subtract(&surface->damage,
999 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001000 pixman_region32_union(&surface->plane->damage,
1001 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001002 empty_region(&surface->damage);
1003 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001004 pixman_region32_union(&surface->plane->opaque,
1005 &surface->plane->opaque,
1006 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001007 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001008}
1009
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001010static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001011weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001012{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001013 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001014 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001015 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001016 struct weston_animation *animation, *next;
1017 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001018 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001019 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001020
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001021 weston_compositor_update_drag_surfaces(ec);
1022
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001023 /* Rebuild the surface list and update surface transforms up front. */
1024 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001025 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001026 wl_list_for_each(layer, &ec->layer_list, link) {
1027 wl_list_for_each(es, &layer->surface_list, layer_link) {
1028 weston_surface_update_transform(es);
1029 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001030 if (es->output == output) {
1031 wl_list_insert_list(&frame_callback_list,
1032 &es->frame_callback_list);
1033 wl_list_init(&es->frame_callback_list);
1034 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001035 }
1036 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001037
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001038 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001039 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001040 else
1041 wl_list_for_each(es, &ec->surface_list, link)
1042 weston_surface_move_to_plane(es, &ec->primary_plane);
1043
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001044 pixman_region32_init(&opaque);
1045
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001046 pixman_region32_fini(&ec->primary_plane.opaque);
1047 pixman_region32_init(&ec->primary_plane.opaque);
1048
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001049 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001050 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001051
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001052 /* Both the renderer and the backend have seen the buffer
1053 * by now. If renderer needs the buffer, it has its own
1054 * reference set. If the backend wants to keep the buffer
1055 * around for migrating the surface into a non-primary plane
1056 * later, keep_buffer is true. Otherwise, drop the core
1057 * reference now, and allow early buffer release. This enables
1058 * clients to use single-buffering.
1059 */
1060 if (!es->keep_buffer)
1061 weston_buffer_reference(&es->buffer_ref, NULL);
1062 }
1063
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001064 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001065
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001066 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001067 pixman_region32_intersect(&output_damage,
1068 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001069
Scott Moreauccbf29d2012-02-22 14:21:41 -07001070 if (output->dirty)
1071 weston_output_update_matrix(output);
1072
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001073 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001074
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001075 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001076
Kristian Høgsbergef044142011-06-21 15:02:12 -04001077 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001078
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001079 weston_compositor_repick(ec);
1080 wl_event_loop_dispatch(ec->input_loop, 0);
1081
Jonas Ådahldb773762012-06-13 00:01:21 +02001082 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001083 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001084 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001085 }
1086
Scott Moreaud64cf212012-06-08 19:40:54 -06001087 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001088 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001089 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001090 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001091}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001092
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001093static int
1094weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001095{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001096 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001097
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001098 wl_event_loop_dispatch(compositor->input_loop, 0);
1099
1100 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001101}
1102
1103WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001104weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001105{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001106 struct weston_compositor *compositor = output->compositor;
1107 struct wl_event_loop *loop =
1108 wl_display_get_event_loop(compositor->wl_display);
1109 int fd;
1110
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001111 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001112 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001113 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001114 return;
1115 }
1116
1117 output->repaint_scheduled = 0;
1118 if (compositor->input_loop_source)
1119 return;
1120
1121 fd = wl_event_loop_get_fd(compositor->input_loop);
1122 compositor->input_loop_source =
1123 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1124 weston_compositor_read_input, compositor);
1125}
1126
1127static void
1128idle_repaint(void *data)
1129{
1130 struct weston_output *output = data;
1131
1132 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001133}
1134
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001135WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001136weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1137{
1138 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001139 if (below != NULL)
1140 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001141}
1142
1143WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001144weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001145{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001146 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001147 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001148
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001149 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001150 return;
1151
Kristian Høgsbergef044142011-06-21 15:02:12 -04001152 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001153 output->repaint_needed = 1;
1154 if (output->repaint_scheduled)
1155 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001156
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001157 wl_event_loop_add_idle(loop, idle_repaint, output);
1158 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001159
1160 if (compositor->input_loop_source) {
1161 wl_event_source_remove(compositor->input_loop_source);
1162 compositor->input_loop_source = NULL;
1163 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001164}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001165
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001166WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001167weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1168{
1169 struct weston_output *output;
1170
1171 wl_list_for_each(output, &compositor->output_list, link)
1172 weston_output_schedule_repaint(output);
1173}
1174
1175WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001176weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001177{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001178 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001179 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001180
Scott Moreau9d1b1122012-06-08 19:40:53 -06001181 output = container_of(compositor->output_list.next,
1182 struct weston_output, link);
1183
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001184 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001185 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001186 return;
1187
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001188 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001189 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001190 if (!surface)
1191 return;
1192
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001193 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001194 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001195 wl_list_insert(&compositor->fade_layer.surface_list,
1196 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001197 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001198 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001199 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001200 }
1201
1202 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001203 if (wl_list_empty(&compositor->fade.animation.link)) {
1204 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001205 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001206 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001207 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001208}
1209
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001210static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001211surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001212{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001213 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001214}
1215
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001216static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001217surface_attach(struct wl_client *client,
1218 struct wl_resource *resource,
1219 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1220{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001221 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001222 struct wl_buffer *buffer = NULL;
1223
1224 if (buffer_resource)
1225 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001226
Pekka Paalanende685b82012-12-04 15:58:12 +02001227 /* Attach, attach, without commit in between does not send
1228 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001229 if (surface->pending.buffer)
1230 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001231
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001232 surface->pending.sx = sx;
1233 surface->pending.sy = sy;
1234 surface->pending.buffer = buffer;
1235 if (buffer) {
1236 wl_signal_add(&buffer->resource.destroy_signal,
1237 &surface->pending.buffer_destroy_listener);
1238 surface->pending.remove_contents = 0;
1239 } else {
1240 surface->pending.remove_contents = 1;
1241 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001242}
1243
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001244static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001245surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001246 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001247 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001248{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001249 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001250
Pekka Paalanen8e159182012-10-10 12:49:25 +03001251 pixman_region32_union_rect(&surface->pending.damage,
1252 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001253 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001254}
1255
Kristian Høgsberg33418202011-08-16 23:01:28 -04001256static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001257destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001258{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001259 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001260
1261 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001262 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001263}
1264
1265static void
1266surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001267 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001268{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001269 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001270 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001271
1272 cb = malloc(sizeof *cb);
1273 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001274 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001275 return;
1276 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001277
Kristian Høgsberg33418202011-08-16 23:01:28 -04001278 cb->resource.object.interface = &wl_callback_interface;
1279 cb->resource.object.id = callback;
1280 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001281 cb->resource.client = client;
1282 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001283
1284 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001285 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001286}
1287
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001288static void
1289surface_set_opaque_region(struct wl_client *client,
1290 struct wl_resource *resource,
1291 struct wl_resource *region_resource)
1292{
1293 struct weston_surface *surface = resource->data;
1294 struct weston_region *region;
1295
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001296 if (region_resource) {
1297 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001298 pixman_region32_copy(&surface->pending.opaque,
1299 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001300 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001301 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001302 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001303}
1304
1305static void
1306surface_set_input_region(struct wl_client *client,
1307 struct wl_resource *resource,
1308 struct wl_resource *region_resource)
1309{
1310 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001311 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001312
1313 if (region_resource) {
1314 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001315 pixman_region32_copy(&surface->pending.input,
1316 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001317 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001318 pixman_region32_fini(&surface->pending.input);
1319 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001320 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001321}
1322
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001323static int
1324surface_pending_buffer_has_different_size(struct weston_surface *surface)
1325{
1326 int width, height;
1327
1328 switch (surface->pending.buffer_transform) {
1329 case WL_OUTPUT_TRANSFORM_90:
1330 case WL_OUTPUT_TRANSFORM_270:
1331 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1332 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1333 height = surface->pending.buffer->width;
1334 width = surface->pending.buffer->height;
1335 break;
1336 default:
1337 width = surface->pending.buffer->width;
1338 height = surface->pending.buffer->height;
1339 }
1340
1341 if (width == surface->geometry.width &&
1342 height == surface->geometry.height)
1343 return 0;
1344 else
1345 return 1;
1346}
1347
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001348static void
1349surface_commit(struct wl_client *client, struct wl_resource *resource)
1350{
1351 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001352 pixman_region32_t opaque;
1353
1354 if (surface->pending.sx || surface->pending.sy ||
1355 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001356 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001357 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001358
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001359 /* wl_surface.set_buffer_rotation */
1360 surface->buffer_transform = surface->pending.buffer_transform;
1361
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001362 /* wl_surface.attach */
1363 if (surface->pending.buffer || surface->pending.remove_contents)
1364 weston_surface_attach(surface, surface->pending.buffer);
1365
Pekka Paalanende685b82012-12-04 15:58:12 +02001366 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001367 surface->configure(surface, surface->pending.sx,
1368 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001369 surface->pending.sx = 0;
1370 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001371
1372 /* wl_surface.damage */
1373 pixman_region32_union(&surface->damage, &surface->damage,
1374 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001375 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1376 0, 0,
1377 surface->geometry.width,
1378 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001379 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001380
1381 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001382 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001383 surface->geometry.width,
1384 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001385 pixman_region32_intersect(&opaque,
1386 &opaque, &surface->pending.opaque);
1387
1388 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1389 pixman_region32_copy(&surface->opaque, &opaque);
1390 surface->geometry.dirty = 1;
1391 }
1392
1393 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001394
1395 /* wl_surface.set_input_region */
1396 pixman_region32_fini(&surface->input);
1397 pixman_region32_init_rect(&surface->input, 0, 0,
1398 surface->geometry.width,
1399 surface->geometry.height);
1400 pixman_region32_intersect(&surface->input,
1401 &surface->input, &surface->pending.input);
1402
Pekka Paalanenbc106382012-10-10 12:49:31 +03001403 /* wl_surface.frame */
1404 wl_list_insert_list(&surface->frame_callback_list,
1405 &surface->pending.frame_callback_list);
1406 wl_list_init(&surface->pending.frame_callback_list);
1407
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001408 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001409}
1410
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001411static void
1412surface_set_buffer_transform(struct wl_client *client,
1413 struct wl_resource *resource, int transform)
1414{
1415 struct weston_surface *surface = resource->data;
1416
1417 surface->pending.buffer_transform = transform;
1418}
1419
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001420static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001421 surface_destroy,
1422 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001423 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001424 surface_frame,
1425 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001426 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001427 surface_commit,
1428 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001429};
1430
1431static void
1432compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001433 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001434{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001435 struct weston_compositor *ec = resource->data;
1436 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001437
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001438 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001439 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001440 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001441 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001442 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001443
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001444 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001445
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001446 surface->surface.resource.object.id = id;
1447 surface->surface.resource.object.interface = &wl_surface_interface;
1448 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001449 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001450 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001451
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001452 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001453}
1454
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001455static void
1456destroy_region(struct wl_resource *resource)
1457{
1458 struct weston_region *region =
1459 container_of(resource, struct weston_region, resource);
1460
1461 pixman_region32_fini(&region->region);
1462 free(region);
1463}
1464
1465static void
1466region_destroy(struct wl_client *client, struct wl_resource *resource)
1467{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001468 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001469}
1470
1471static void
1472region_add(struct wl_client *client, struct wl_resource *resource,
1473 int32_t x, int32_t y, int32_t width, int32_t height)
1474{
1475 struct weston_region *region = resource->data;
1476
1477 pixman_region32_union_rect(&region->region, &region->region,
1478 x, y, width, height);
1479}
1480
1481static void
1482region_subtract(struct wl_client *client, struct wl_resource *resource,
1483 int32_t x, int32_t y, int32_t width, int32_t height)
1484{
1485 struct weston_region *region = resource->data;
1486 pixman_region32_t rect;
1487
1488 pixman_region32_init_rect(&rect, x, y, width, height);
1489 pixman_region32_subtract(&region->region, &region->region, &rect);
1490 pixman_region32_fini(&rect);
1491}
1492
1493static const struct wl_region_interface region_interface = {
1494 region_destroy,
1495 region_add,
1496 region_subtract
1497};
1498
1499static void
1500compositor_create_region(struct wl_client *client,
1501 struct wl_resource *resource, uint32_t id)
1502{
1503 struct weston_region *region;
1504
1505 region = malloc(sizeof *region);
1506 if (region == NULL) {
1507 wl_resource_post_no_memory(resource);
1508 return;
1509 }
1510
1511 region->resource.destroy = destroy_region;
1512
1513 region->resource.object.id = id;
1514 region->resource.object.interface = &wl_region_interface;
1515 region->resource.object.implementation =
1516 (void (**)(void)) &region_interface;
1517 region->resource.data = region;
1518
1519 pixman_region32_init(&region->region);
1520
1521 wl_client_add_resource(client, &region->resource);
1522}
1523
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001524static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001525 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001526 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001527};
1528
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001529WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001530weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001531{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001532 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1533 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001534
1535 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001536 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001537}
1538
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001539static void
1540weston_compositor_dpms_on(struct weston_compositor *compositor)
1541{
1542 struct weston_output *output;
1543
1544 wl_list_for_each(output, &compositor->output_list, link)
1545 if (output->set_dpms)
1546 output->set_dpms(output, WESTON_DPMS_ON);
1547}
1548
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001549WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001550weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001551{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001552 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1553 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001554 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001555 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001556 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001557 }
1558}
1559
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001560static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001561weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001562{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001563 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001564 compositor->idle_inhibit++;
1565}
1566
1567static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001568weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001569{
1570 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001571 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001572}
1573
1574static int
1575idle_handler(void *data)
1576{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001577 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001578
1579 if (compositor->idle_inhibit)
1580 return 1;
1581
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001582 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001583
1584 return 1;
1585}
1586
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001587WL_EXPORT void
1588weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1589{
1590 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001591 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001592 plane->x = x;
1593 plane->y = y;
1594}
1595
1596WL_EXPORT void
1597weston_plane_release(struct weston_plane *plane)
1598{
1599 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001600 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001601}
1602
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001603static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001604weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001605
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001606static void
Daniel Stone37816df2012-05-16 18:45:18 +01001607clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001608{
Daniel Stone37816df2012-05-16 18:45:18 +01001609 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001610 struct weston_output *output, *prev = NULL;
1611 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001612
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001613 x = wl_fixed_to_int(*fx);
1614 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001615 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1616 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001617
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001618 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001619 if (pixman_region32_contains_point(&output->region,
1620 x, y, NULL))
1621 valid = 1;
1622 if (pixman_region32_contains_point(&output->region,
1623 old_x, old_y, NULL))
1624 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001625 }
Daniel Stone37816df2012-05-16 18:45:18 +01001626
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001627 if (!valid) {
1628 if (x < prev->x)
1629 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001630 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001631 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001632 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001633 if (y < prev->y)
1634 *fy = wl_fixed_from_int(prev->y);
1635 else if (y >= prev->y + prev->current->height)
1636 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001637 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001638 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001639}
1640
1641WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001642notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001643{
1644 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001645 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001646 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001647 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001648 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001649
1650 weston_compositor_activity(ec);
1651
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001652 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001653
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001654 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001655
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001656 pointer->x = x;
1657 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001658
1659 ix = wl_fixed_to_int(x);
1660 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001661
Scott Moreauccbf29d2012-02-22 14:21:41 -07001662 wl_list_for_each(output, &ec->output_list, link)
1663 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001664 pixman_region32_contains_point(&output->region,
1665 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001666 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001667
Daniel Stone37816df2012-05-16 18:45:18 +01001668 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001669 interface = pointer->grab->interface;
1670 interface->motion(pointer->grab, time,
1671 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001672
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001673 if (seat->sprite) {
1674 weston_surface_set_position(seat->sprite,
1675 ix - seat->hotspot_x,
1676 iy - seat->hotspot_y);
1677 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001678 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001679}
1680
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001681WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001682weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001683 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001684{
Daniel Stone37816df2012-05-16 18:45:18 +01001685 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001686
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001687 if (seat->seat.keyboard) {
1688 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1689 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001690 }
1691
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001692 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001693}
1694
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001695WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001696notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001697 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001698{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001699 struct weston_compositor *compositor = seat->compositor;
1700 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001701 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001702 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001703 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001704
Daniel Stone4dbadb12012-05-30 16:31:51 +01001705 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001706 if (compositor->ping_handler && focus)
1707 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001708 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001709 if (pointer->button_count == 0) {
1710 pointer->grab_button = button;
1711 pointer->grab_time = time;
1712 pointer->grab_x = pointer->x;
1713 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001714 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001715 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001716 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001717 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001718 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001719 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001720
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001721 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001722 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001723
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001724 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001725
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001726 if (pointer->button_count == 1)
1727 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001728 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001729}
1730
Scott Moreau210d0792012-03-22 10:47:01 -06001731WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001732notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001733 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001734{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001735 struct weston_compositor *compositor = seat->compositor;
1736 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001737 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001738 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001739 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1740
1741 if (compositor->ping_handler && focus)
1742 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001743
1744 weston_compositor_activity(compositor);
1745
Scott Moreau6a3633d2012-03-20 08:47:59 -06001746 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001747 weston_compositor_run_axis_binding(compositor, seat,
1748 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001749 else
1750 return;
1751
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001752 if (pointer->focus_resource)
1753 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001754 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001755}
1756
Daniel Stone05d58682012-06-22 13:21:38 +01001757WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001759{
Daniel Stone05d58682012-06-22 13:21:38 +01001760 struct wl_keyboard *keyboard = &seat->keyboard;
1761 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001762 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001763 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001764 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001765 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001766
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001767 /* Serialize and update our internal state, checking to see if it's
1768 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001769 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1770 XKB_STATE_DEPRESSED);
1771 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1772 XKB_STATE_LATCHED);
1773 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1774 XKB_STATE_LOCKED);
1775 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001776 XKB_STATE_EFFECTIVE);
1777
Daniel Stone71c38772012-06-22 13:21:30 +01001778 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1779 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1780 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1781 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001782 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001783
Daniel Stone71c38772012-06-22 13:21:30 +01001784 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1785 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1786 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1787 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001788
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001789 /* And update the modifier_state for bindings. */
1790 mods_lookup = mods_depressed | mods_latched;
1791 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001792 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001793 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001794 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001795 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001796 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001797 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001798 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1799 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001800
Daniel Stone8c8164f2012-05-30 16:31:45 +01001801 /* Finally, notify the compositor that LEDs have changed. */
1802 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001803 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001804 leds |= LED_NUM_LOCK;
1805 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001806 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001807 leds |= LED_CAPS_LOCK;
1808 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001809 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001810 leds |= LED_SCROLL_LOCK;
1811 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001812 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001813 seat->xkb_state.leds = leds;
1814
Daniel Stone05d58682012-06-22 13:21:38 +01001815 if (changed) {
1816 grab->interface->modifiers(grab,
1817 serial,
1818 keyboard->modifiers.mods_depressed,
1819 keyboard->modifiers.mods_latched,
1820 keyboard->modifiers.mods_locked,
1821 keyboard->modifiers.group);
1822 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001823}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001824
Daniel Stone05d58682012-06-22 13:21:38 +01001825static void
1826update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001827 enum wl_keyboard_key_state state)
1828{
1829 enum xkb_key_direction direction;
1830
1831 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1832 direction = XKB_KEY_DOWN;
1833 else
1834 direction = XKB_KEY_UP;
1835
1836 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1837 * broken keycode system, which starts at 8. */
1838 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1839
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001840 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001841}
1842
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001843WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001844notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001845 enum wl_keyboard_key_state state,
1846 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001847{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001848 struct weston_compositor *compositor = seat->compositor;
1849 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001850 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001851 (struct weston_surface *) keyboard->focus;
1852 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001853 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001854 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001855
Daniel Stonec9785ea2012-05-30 16:31:52 +01001856 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001857 if (compositor->ping_handler && focus)
1858 compositor->ping_handler(focus, serial);
1859
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001860 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001861 keyboard->grab_key = key;
1862 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001863 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001864 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001865 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001866
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001867 end = keyboard->keys.data + keyboard->keys.size;
1868 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001869 if (*k == key) {
1870 /* Ignore server-generated repeats. */
1871 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1872 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001873 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001874 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001875 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001876 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001877 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001878 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001879 *k = key;
1880 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001881
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001882 if (grab == &keyboard->default_grab) {
1883 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001884 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001885 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001886 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001887
Daniel Stone7ace3902012-05-30 16:31:40 +01001888 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001889
1890 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001891 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001892 wl_display_get_serial(compositor->wl_display),
1893 key,
1894 state);
1895 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001896}
1897
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001898WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001899notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001900 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001901{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001902 struct weston_compositor *compositor = seat->compositor;
1903 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001904
1905 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001906 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001907 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001908 x - pointer->x,
1909 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001910
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 pointer->x = x;
1912 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001913 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001914 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001915 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001916 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001917 /* FIXME: We should call wl_pointer_set_focus(seat,
1918 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001919 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001920}
1921
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001922static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001923destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001924{
Daniel Stone37816df2012-05-16 18:45:18 +01001925 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001926
Daniel Stone37816df2012-05-16 18:45:18 +01001927 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001928 saved_kbd_focus_listener);
1929
Daniel Stone37816df2012-05-16 18:45:18 +01001930 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001931}
1932
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001933WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001934notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001935 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001936{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001937 struct weston_compositor *compositor = seat->compositor;
1938 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001939 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001940 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001941
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001942 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001943 wl_array_copy(&keyboard->keys, keys);
1944 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001945 weston_compositor_idle_inhibit(compositor);
1946 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001947 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001948 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001949 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001950
Daniel Stoneef172672012-06-22 13:21:32 +01001951 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001952 wl_array_for_each(k, &keyboard->keys) {
1953 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001954 WL_KEYBOARD_KEY_STATE_PRESSED);
1955 }
1956
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001957 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001958
1959 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001960 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1961 wl_keyboard_set_focus(keyboard, surface);
1962 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001963 }
1964}
1965
1966WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001967notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001968{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001969 struct weston_compositor *compositor = seat->compositor;
1970 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001971 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001972
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001973 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001974 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001975 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001976 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001977 WL_KEYBOARD_KEY_STATE_RELEASED);
1978 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001979
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001980 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001981
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001982 if (keyboard->focus) {
1983 seat->saved_kbd_focus = keyboard->focus;
1984 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001985 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001986 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1987 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001988 }
1989
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001990 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001991 /* FIXME: We really need keyboard grab cancel here to
1992 * let the grab shut down properly. As it is we leak
1993 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001994 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001995}
1996
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001997static void
Daniel Stone37816df2012-05-16 18:45:18 +01001998touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001999{
Daniel Stone37816df2012-05-16 18:45:18 +01002000 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002001 struct wl_resource *resource;
2002
Pekka Paalanen56464252012-07-31 13:21:08 +03002003 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002004 return;
2005
Pekka Paalanen56464252012-07-31 13:21:08 +03002006 if (seat->touch->focus_resource)
2007 wl_list_remove(&seat->touch->focus_listener.link);
2008 seat->touch->focus = NULL;
2009 seat->touch->focus_resource = NULL;
2010
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002011 if (surface) {
2012 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002013 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002014 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002015 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002016 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002017 return;
2018 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002019
Daniel Stone37816df2012-05-16 18:45:18 +01002020 seat->touch->focus = surface;
2021 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002022 wl_signal_add(&resource->destroy_signal,
2023 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002024 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002025}
2026
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002027/**
2028 * notify_touch - emulates button touches and notifies surfaces accordingly.
2029 *
2030 * It assumes always the correct cycle sequence until it gets here: touch_down
2031 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2032 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002033 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002034 */
2035WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002036notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002037 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002038{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002039 struct weston_compositor *ec = seat->compositor;
2040 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002041 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002042 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002043 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002044
2045 /* Update grab's global coordinates. */
2046 touch->grab_x = x;
2047 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002048
2049 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002050 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002051 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002052
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002053 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002054
2055 /* the first finger down picks the surface, and all further go
2056 * to that surface for the remainder of the touch session i.e.
2057 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002058 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002059 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002060 touch_set_focus(seat, &es->surface);
2061 } else if (touch->focus) {
2062 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002063 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002064 } else {
2065 /* Unexpected condition: We have non-initial touch but
2066 * there is no focused surface.
2067 */
2068 weston_log("touch event received with %d points down"
2069 "but no surface focused\n", seat->num_tp);
2070 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002071 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002072
Matt Roper47c1b982012-10-10 16:56:53 -07002073 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002074 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002075 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002076 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002077 if (!es)
2078 break;
2079
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002080 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002081 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002082 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002083 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002084 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002085 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002086
Matt Roper47c1b982012-10-10 16:56:53 -07002087 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002088 if (seat->num_tp == 0)
2089 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002090 break;
2091 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002092}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002093
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002094static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002095pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2096{
2097 struct weston_seat *seat = container_of(listener, struct weston_seat,
2098 sprite_destroy_listener);
2099
2100 seat->sprite = NULL;
2101}
2102
2103static void
2104pointer_cursor_surface_configure(struct weston_surface *es,
2105 int32_t dx, int32_t dy)
2106{
2107 struct weston_seat *seat = es->private;
2108 int x, y;
2109
2110 assert(es == seat->sprite);
2111
2112 seat->hotspot_x -= dx;
2113 seat->hotspot_y -= dy;
2114
2115 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2116 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2117
2118 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002119 es->buffer_ref.buffer->width,
2120 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002121
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002122 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002123
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002124 if (!weston_surface_is_mapped(es)) {
2125 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2126 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002127 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002128 }
2129}
2130
2131static void
2132pointer_unmap_sprite(struct weston_seat *seat)
2133{
2134 if (weston_surface_is_mapped(seat->sprite))
2135 weston_surface_unmap(seat->sprite);
2136
2137 wl_list_remove(&seat->sprite_destroy_listener.link);
2138 seat->sprite->configure = NULL;
2139 seat->sprite->private = NULL;
2140 seat->sprite = NULL;
2141}
2142
2143static void
2144pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2145 uint32_t serial, struct wl_resource *surface_resource,
2146 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002147{
Daniel Stone37816df2012-05-16 18:45:18 +01002148 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002149 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002150
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002151 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002152 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002153
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002154 if (seat->seat.pointer->focus == NULL)
2155 return;
2156 if (seat->seat.pointer->focus->resource.client != client)
2157 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002158 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002159 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002160
2161 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002162 if (surface->configure) {
2163 wl_resource_post_error(&surface->surface.resource,
2164 WL_DISPLAY_ERROR_INVALID_OBJECT,
2165 "surface->configure already "
2166 "set");
2167 return;
2168 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002169 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002170
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002171 if (seat->sprite)
2172 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002173
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002174 if (!surface)
2175 return;
2176
2177 wl_signal_add(&surface->surface.resource.destroy_signal,
2178 &seat->sprite_destroy_listener);
2179
2180 surface->configure = pointer_cursor_surface_configure;
2181 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002182 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002183 seat->hotspot_x = x;
2184 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002185
Pekka Paalanende685b82012-12-04 15:58:12 +02002186 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002187 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002188}
2189
Daniel Stone37816df2012-05-16 18:45:18 +01002190static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002191 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002192};
2193
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002194static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002195handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002196{
Daniel Stone37816df2012-05-16 18:45:18 +01002197 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002198
Daniel Stone37816df2012-05-16 18:45:18 +01002199 seat = container_of(listener, struct weston_seat,
2200 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002201
Daniel Stone37816df2012-05-16 18:45:18 +01002202 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002203}
2204
Casey Dahlin9074db52012-04-19 22:50:09 -04002205static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002206{
2207 wl_list_remove(&resource->link);
2208 free(resource);
2209}
2210
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002211static void
Daniel Stone37816df2012-05-16 18:45:18 +01002212seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2213 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002214{
Daniel Stone37816df2012-05-16 18:45:18 +01002215 struct weston_seat *seat = resource->data;
2216 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002217
Daniel Stone37816df2012-05-16 18:45:18 +01002218 if (!seat->seat.pointer)
2219 return;
2220
2221 cr = wl_client_add_object(client, &wl_pointer_interface,
2222 &pointer_interface, id, seat);
2223 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002224 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002225
2226 if (seat->seat.pointer->focus &&
2227 seat->seat.pointer->focus->resource.client == client) {
2228 struct weston_surface *surface;
2229 wl_fixed_t sx, sy;
2230
2231 surface = (struct weston_surface *) seat->seat.pointer->focus;
2232 weston_surface_from_global_fixed(surface,
2233 seat->seat.pointer->x,
2234 seat->seat.pointer->y,
2235 &sx,
2236 &sy);
2237 wl_pointer_set_focus(seat->seat.pointer,
2238 seat->seat.pointer->focus,
2239 sx,
2240 sy);
2241 }
Daniel Stone37816df2012-05-16 18:45:18 +01002242}
2243
2244static void
2245seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2246 uint32_t id)
2247{
2248 struct weston_seat *seat = resource->data;
2249 struct wl_resource *cr;
2250
2251 if (!seat->seat.keyboard)
2252 return;
2253
2254 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2255 seat);
2256 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002257 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002258
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002259 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2260 seat->xkb_info.keymap_fd,
2261 seat->xkb_info.keymap_size);
2262
Daniel Stone17b13bd2012-05-30 16:31:39 +01002263 if (seat->seat.keyboard->focus &&
2264 seat->seat.keyboard->focus->resource.client == client) {
2265 wl_keyboard_set_focus(seat->seat.keyboard,
2266 seat->seat.keyboard->focus);
2267 }
Daniel Stone37816df2012-05-16 18:45:18 +01002268}
2269
2270static void
2271seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2272 uint32_t id)
2273{
2274 struct weston_seat *seat = resource->data;
2275 struct wl_resource *cr;
2276
2277 if (!seat->seat.touch)
2278 return;
2279
2280 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2281 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002282 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002283}
2284
2285static const struct wl_seat_interface seat_interface = {
2286 seat_get_pointer,
2287 seat_get_keyboard,
2288 seat_get_touch,
2289};
2290
2291static void
2292bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2293{
2294 struct wl_seat *seat = data;
2295 struct wl_resource *resource;
2296 enum wl_seat_capability caps = 0;
2297
2298 resource = wl_client_add_object(client, &wl_seat_interface,
2299 &seat_interface, id, data);
2300 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002301 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002302
2303 if (seat->pointer)
2304 caps |= WL_SEAT_CAPABILITY_POINTER;
2305 if (seat->keyboard)
2306 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2307 if (seat->touch)
2308 caps |= WL_SEAT_CAPABILITY_TOUCH;
2309
2310 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002311}
2312
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002313static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002314device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002315{
Daniel Stone37816df2012-05-16 18:45:18 +01002316 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002317
Daniel Stone37816df2012-05-16 18:45:18 +01002318 seat = container_of(listener, struct weston_seat,
2319 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002320
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002321 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002322}
2323
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002324static void weston_compositor_xkb_init(struct weston_compositor *ec,
2325 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002326{
Daniel Stonee379da92012-05-30 16:32:04 +01002327 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002328 ec->xkb_context = xkb_context_new(0);
2329 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002330 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002331 exit(1);
2332 }
Daniel Stone994679a2012-05-30 16:31:43 +01002333 }
2334
2335 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002336 ec->xkb_names = *names;
2337 if (!ec->xkb_names.rules)
2338 ec->xkb_names.rules = strdup("evdev");
2339 if (!ec->xkb_names.model)
2340 ec->xkb_names.model = strdup("pc105");
2341 if (!ec->xkb_names.layout)
2342 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002343}
2344
Daniel Stonee379da92012-05-30 16:32:04 +01002345static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2346{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002347 if (xkb_info->keymap)
2348 xkb_map_unref(xkb_info->keymap);
2349
2350 if (xkb_info->keymap_area)
2351 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2352 if (xkb_info->keymap_fd >= 0)
2353 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002354}
2355
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002356static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2357{
Daniel Stonee379da92012-05-30 16:32:04 +01002358 free((char *) ec->xkb_names.rules);
2359 free((char *) ec->xkb_names.model);
2360 free((char *) ec->xkb_names.layout);
2361 free((char *) ec->xkb_names.variant);
2362 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002363
Daniel Stonee379da92012-05-30 16:32:04 +01002364 xkb_info_destroy(&ec->xkb_info);
2365 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002366}
2367
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002368static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002369weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002370{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002371 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002372
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002373 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2374 XKB_MOD_NAME_SHIFT);
2375 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2376 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002377 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2378 XKB_MOD_NAME_CTRL);
2379 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2380 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002381 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2382 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002383 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2384 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002385 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002386
2387 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2388 XKB_LED_NAME_NUM);
2389 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2390 XKB_LED_NAME_CAPS);
2391 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2392 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002393
2394 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2395 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002396 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002397 exit(EXIT_FAILURE);
2398 }
2399 xkb_info->keymap_size = strlen(keymap_str) + 1;
2400
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002401 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002402 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002403 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002404 (unsigned long) xkb_info->keymap_size);
2405 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002406 }
2407
2408 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2409 PROT_READ | PROT_WRITE,
2410 MAP_SHARED, xkb_info->keymap_fd, 0);
2411 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002412 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002413 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002414 goto err_dev_zero;
2415 }
2416 strcpy(xkb_info->keymap_area, keymap_str);
2417 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002418
2419 return;
2420
2421err_dev_zero:
2422 close(xkb_info->keymap_fd);
2423 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002424err_keymap_str:
2425 free(keymap_str);
2426 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002427}
2428
2429static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002430weston_compositor_build_global_keymap(struct weston_compositor *ec)
2431{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002432 if (ec->xkb_info.keymap != NULL)
2433 return;
2434
Daniel Stonee379da92012-05-30 16:32:04 +01002435 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002436 &ec->xkb_names,
2437 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002438 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002439 weston_log("failed to compile global XKB keymap\n");
2440 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002441 "options %s",
2442 ec->xkb_names.rules, ec->xkb_names.model,
2443 ec->xkb_names.layout, ec->xkb_names.variant,
2444 ec->xkb_names.options);
2445 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002446 }
2447
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002448 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002449}
2450
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002451WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002452weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002453{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002454 if (seat->has_keyboard)
2455 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002456
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002457 if (keymap != NULL) {
2458 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002459 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002460 }
2461 else {
2462 weston_compositor_build_global_keymap(seat->compositor);
2463 seat->xkb_info = seat->compositor->xkb_info;
2464 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2465 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002466
2467 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2468 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002469 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002470 exit(1);
2471 }
2472
Daniel Stone71c38772012-06-22 13:21:30 +01002473 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002474
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002475 wl_keyboard_init(&seat->keyboard);
2476 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2477
2478 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002479}
2480
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002481WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002482weston_seat_init_pointer(struct weston_seat *seat)
2483{
2484 if (seat->has_pointer)
2485 return;
2486
2487 wl_pointer_init(&seat->pointer);
2488 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2489
2490 seat->has_pointer = 1;
2491}
2492
2493WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002494weston_seat_init_touch(struct weston_seat *seat)
2495{
2496 if (seat->has_touch)
2497 return;
2498
2499 wl_touch_init(&seat->touch);
2500 wl_seat_set_touch(&seat->seat, &seat->touch);
2501
2502 seat->has_touch = 1;
2503}
2504
2505WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002506weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002507{
Daniel Stone37816df2012-05-16 18:45:18 +01002508 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002509 seat->has_pointer = 0;
2510 seat->has_keyboard = 0;
2511 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002512
Daniel Stone37816df2012-05-16 18:45:18 +01002513 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2514 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002515
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002516 seat->sprite = NULL;
2517 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002518
Daniel Stone37816df2012-05-16 18:45:18 +01002519 seat->compositor = ec;
2520 seat->hotspot_x = 16;
2521 seat->hotspot_y = 16;
2522 seat->modifier_state = 0;
2523 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002524
Daniel Stone37816df2012-05-16 18:45:18 +01002525 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002526 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002527
Daniel Stone37816df2012-05-16 18:45:18 +01002528 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002529
Daniel Stone37816df2012-05-16 18:45:18 +01002530 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2531 wl_signal_add(&seat->seat.drag_icon_signal,
2532 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002533
2534 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002535 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002536}
2537
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002538WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002539weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002540{
Daniel Stone37816df2012-05-16 18:45:18 +01002541 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002542 /* The global object is destroyed at wl_display_destroy() time. */
2543
Daniel Stone37816df2012-05-16 18:45:18 +01002544 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002545 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002546
Daniel Stone74419a22012-05-30 16:32:02 +01002547 if (seat->xkb_state.state != NULL)
2548 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002549 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002550
Daniel Stone37816df2012-05-16 18:45:18 +01002551 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002552}
2553
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002554static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002555drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2556{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002557 empty_region(&es->pending.input);
2558
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002559 weston_surface_configure(es,
2560 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002561 es->buffer_ref.buffer->width,
2562 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002563}
2564
2565static int
Daniel Stone37816df2012-05-16 18:45:18 +01002566device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002567 struct weston_surface *surface)
2568{
Daniel Stone37816df2012-05-16 18:45:18 +01002569 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002570
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002571 if (surface->configure) {
2572 wl_resource_post_error(&surface->surface.resource,
2573 WL_DISPLAY_ERROR_INVALID_OBJECT,
2574 "surface->configure already set");
2575 return 0;
2576 }
2577
Daniel Stone37816df2012-05-16 18:45:18 +01002578 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002579
Daniel Stone37816df2012-05-16 18:45:18 +01002580 weston_surface_set_position(ws->drag_surface,
2581 wl_fixed_to_double(seat->pointer->x),
2582 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002583
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002584 surface->configure = drag_surface_configure;
2585
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002586 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002587 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002588
2589 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002590}
2591
2592static void
Daniel Stone37816df2012-05-16 18:45:18 +01002593device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002594{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002595 if (weston_surface_is_mapped(seat->drag_surface))
2596 weston_surface_unmap(seat->drag_surface);
2597
Daniel Stone37816df2012-05-16 18:45:18 +01002598 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002599 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002600 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2601 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002602}
2603
2604static void
Daniel Stone37816df2012-05-16 18:45:18 +01002605device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002606{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002607 struct wl_list *list;
2608
Daniel Stone37816df2012-05-16 18:45:18 +01002609 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002610 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002611 return;
2612
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002613 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2614 list = &seat->sprite->layer_link;
2615 else
2616 list = &seat->compositor->cursor_layer.surface_list;
2617
2618 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002619 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002620 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002621}
2622
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002623static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002624weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002625 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002626{
2627 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002628
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002629 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002630 return;
2631
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002632 if (seat->drag_surface && seat->seat.drag_surface &&
2633 (&seat->drag_surface->surface.resource !=
2634 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002635 /* between calls to this funcion we got a new drag_surface */
2636 surface_changed = 1;
2637
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002638 if (!seat->seat.drag_surface || surface_changed) {
2639 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002640 if (!surface_changed)
2641 return;
2642 }
2643
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002644 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002645 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002646 seat->seat.drag_surface;
2647 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002648 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002649 }
2650
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002651 /* the client may not have attached a buffer to the drag surface
2652 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002653 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002654
2655 if (!dx && !dy)
2656 return;
2657
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002658 weston_surface_set_position(seat->drag_surface,
2659 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2660 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002661}
2662
2663WL_EXPORT void
2664weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2665{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002666 struct weston_seat *seat;
2667
2668 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002669 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002670}
2671
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002672static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002673bind_output(struct wl_client *client,
2674 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002675{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002676 struct weston_output *output = data;
2677 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002678 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002679
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002680 resource = wl_client_add_object(client,
2681 &wl_output_interface, NULL, id, data);
2682
Casey Dahlin9074db52012-04-19 22:50:09 -04002683 wl_list_insert(&output->resource_list, &resource->link);
2684 resource->destroy = unbind_resource;
2685
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002686 wl_output_send_geometry(resource,
2687 output->x,
2688 output->y,
2689 output->mm_width,
2690 output->mm_height,
2691 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002692 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002693 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002694
2695 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002696 wl_output_send_mode(resource,
2697 mode->flags,
2698 mode->width,
2699 mode->height,
2700 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002701 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002702}
2703
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002704WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002705weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002706{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002707 struct weston_compositor *c = output->compositor;
2708
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002709 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002710 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002711 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002712
2713 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002714}
2715
Scott Moreau1bad5db2012-08-18 01:04:05 -06002716static void
2717weston_output_compute_transform(struct weston_output *output)
2718{
2719 struct weston_matrix transform;
2720 int flip;
2721
2722 weston_matrix_init(&transform);
2723
2724 switch(output->transform) {
2725 case WL_OUTPUT_TRANSFORM_FLIPPED:
2726 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2727 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2728 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2729 flip = -1;
2730 break;
2731 default:
2732 flip = 1;
2733 break;
2734 }
2735
2736 switch(output->transform) {
2737 case WL_OUTPUT_TRANSFORM_NORMAL:
2738 case WL_OUTPUT_TRANSFORM_FLIPPED:
2739 transform.d[0] = flip;
2740 transform.d[1] = 0;
2741 transform.d[4] = 0;
2742 transform.d[5] = 1;
2743 break;
2744 case WL_OUTPUT_TRANSFORM_90:
2745 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2746 transform.d[0] = 0;
2747 transform.d[1] = -flip;
2748 transform.d[4] = 1;
2749 transform.d[5] = 0;
2750 break;
2751 case WL_OUTPUT_TRANSFORM_180:
2752 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2753 transform.d[0] = -flip;
2754 transform.d[1] = 0;
2755 transform.d[4] = 0;
2756 transform.d[5] = -1;
2757 break;
2758 case WL_OUTPUT_TRANSFORM_270:
2759 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2760 transform.d[0] = 0;
2761 transform.d[1] = flip;
2762 transform.d[4] = -1;
2763 transform.d[5] = 0;
2764 break;
2765 default:
2766 break;
2767 }
2768
2769 weston_matrix_multiply(&output->matrix, &transform);
2770}
2771
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002772WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002773weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002774{
Scott Moreau850ca422012-05-21 15:21:25 -06002775 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002776 struct weston_matrix camera;
2777 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002778
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002779 weston_matrix_init(&output->matrix);
2780 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002781 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2782 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002783
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002784 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002785 2.0 / (output->width + output->border.left + output->border.right),
2786 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2787
2788 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002789
Scott Moreauccbf29d2012-02-22 14:21:41 -07002790 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002791 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002792 weston_matrix_init(&camera);
2793 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002794 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002795 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002796 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002797 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002798 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002799 weston_matrix_multiply(&output->matrix, &modelview);
2800 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002801
Scott Moreauccbf29d2012-02-22 14:21:41 -07002802 output->dirty = 0;
2803}
2804
Scott Moreau1bad5db2012-08-18 01:04:05 -06002805static void
2806weston_output_transform_init(struct weston_output *output, uint32_t transform)
2807{
2808 output->transform = transform;
2809
2810 switch (transform) {
2811 case WL_OUTPUT_TRANSFORM_90:
2812 case WL_OUTPUT_TRANSFORM_270:
2813 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2814 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2815 /* Swap width and height */
2816 output->width = output->current->height;
2817 output->height = output->current->width;
2818 break;
2819 case WL_OUTPUT_TRANSFORM_NORMAL:
2820 case WL_OUTPUT_TRANSFORM_180:
2821 case WL_OUTPUT_TRANSFORM_FLIPPED:
2822 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2823 output->width = output->current->width;
2824 output->height = output->current->height;
2825 break;
2826 default:
2827 break;
2828 }
2829}
2830
Scott Moreauccbf29d2012-02-22 14:21:41 -07002831WL_EXPORT void
2832weston_output_move(struct weston_output *output, int x, int y)
2833{
2834 output->x = x;
2835 output->y = y;
2836
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002837 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002838 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002839 output->width,
2840 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002841}
2842
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002843WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002844weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002845 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002846{
2847 output->compositor = c;
2848 output->x = x;
2849 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002850 output->border.top = 0;
2851 output->border.bottom = 0;
2852 output->border.left = 0;
2853 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002854 output->mm_width = width;
2855 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002856 output->dirty = 1;
2857
Scott Moreau1bad5db2012-08-18 01:04:05 -06002858 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002859 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002860
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002861 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002862 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002863
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002864 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002865 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002866 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002867
Casey Dahlin58ba1372012-04-19 22:50:08 -04002868 output->id = ffs(~output->compositor->output_id_pool) - 1;
2869 output->compositor->output_id_pool |= 1 << output->id;
2870
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002871 output->global =
2872 wl_display_add_global(c->wl_display, &wl_output_interface,
2873 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002874}
2875
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002876static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002877compositor_bind(struct wl_client *client,
2878 void *data, uint32_t version, uint32_t id)
2879{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002880 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002881
2882 wl_client_add_object(client, &wl_compositor_interface,
2883 &compositor_interface, id, compositor);
2884}
2885
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002886static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002887log_uname(void)
2888{
2889 struct utsname usys;
2890
2891 uname(&usys);
2892
2893 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2894 usys.version, usys.machine);
2895}
2896
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002897WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002898weston_compositor_init(struct weston_compositor *ec,
2899 struct wl_display *display,
2900 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002901 char *argv[],
2902 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002903{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002904 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002905 struct xkb_rule_names xkb_names;
2906 const struct config_key keyboard_config_keys[] = {
2907 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2908 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2909 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2910 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2911 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2912 };
2913 const struct config_section cs[] = {
2914 { "keyboard",
2915 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2916 };
2917
2918 memset(&xkb_names, 0, sizeof(xkb_names));
2919 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002920
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002921 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002922 wl_signal_init(&ec->destroy_signal);
2923 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002924 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002925 wl_signal_init(&ec->lock_signal);
2926 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002927 wl_signal_init(&ec->show_input_panel_signal);
2928 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002929 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002930 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002931
Casey Dahlin58ba1372012-04-19 22:50:08 -04002932 ec->output_id_pool = 0;
2933
Kristian Høgsberga8873122011-11-23 10:39:34 -05002934 if (!wl_display_add_global(display, &wl_compositor_interface,
2935 ec, compositor_bind))
2936 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002937
Daniel Stone725c2c32012-06-22 14:04:36 +01002938 wl_list_init(&ec->surface_list);
2939 wl_list_init(&ec->layer_list);
2940 wl_list_init(&ec->seat_list);
2941 wl_list_init(&ec->output_list);
2942 wl_list_init(&ec->key_binding_list);
2943 wl_list_init(&ec->button_binding_list);
2944 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002945 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002946 wl_list_init(&ec->fade.animation.link);
2947
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002948 weston_plane_init(&ec->primary_plane, 0, 0);
2949
Daniel Stone725c2c32012-06-22 14:04:36 +01002950 weston_compositor_xkb_init(ec, &xkb_names);
2951
2952 ec->ping_handler = NULL;
2953
2954 screenshooter_create(ec);
2955 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002956 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002957
2958 wl_data_device_manager_init(ec->wl_display);
2959
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002960 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002961
Daniel Stone725c2c32012-06-22 14:04:36 +01002962 loop = wl_display_get_event_loop(ec->wl_display);
2963 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2964 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2965
2966 ec->input_loop = wl_event_loop_create();
2967
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002968 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2969 ec->fade.animation.frame = fade_frame;
2970
2971 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2972 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2973
2974 weston_compositor_schedule_repaint(ec);
2975
Daniel Stone725c2c32012-06-22 14:04:36 +01002976 return 0;
2977}
2978
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002979WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002980weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002981{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002982 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002983
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002984 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002985 if (ec->input_loop_source)
2986 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002987
Matt Roper361d2ad2011-08-29 13:52:23 -07002988 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002989 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002990 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002991
Daniel Stone325fc2d2012-05-30 16:31:58 +01002992 weston_binding_list_destroy_all(&ec->key_binding_list);
2993 weston_binding_list_destroy_all(&ec->button_binding_list);
2994 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002995 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002996
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002997 weston_plane_release(&ec->primary_plane);
2998
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002999 wl_array_release(&ec->vertices);
3000 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003001 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003002
3003 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003004}
3005
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003006static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003007{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003008 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003009
Martin Minarik6d118362012-06-07 18:01:59 +02003010 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003011 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003012
3013 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003014}
3015
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003016static void
3017on_segv_signal(int s, siginfo_t *siginfo, void *context)
3018{
3019 void *buffer[32];
3020 int i, count;
3021 Dl_info info;
3022
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003023 /* This SIGSEGV handler will do a best-effort backtrace, and
3024 * then call the backend restore function, which will switch
3025 * back to the vt we launched from or ungrab X etc and then
3026 * raise SIGTRAP. If we run weston under gdb from X or a
3027 * different vt, and tell gdb "handle SIGSEGV nostop", this
3028 * will allow weston to switch back to gdb on crash and then
3029 * gdb will catch the crash with SIGTRAP. */
3030
Martin Minarik6d118362012-06-07 18:01:59 +02003031 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003032
3033 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3034 for (i = 0; i < count; i++) {
3035 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003036 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003037 (long) buffer[i],
3038 info.dli_sname ? info.dli_sname : "--",
3039 info.dli_fname);
3040 }
3041
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003042 segv_compositor->restore(segv_compositor);
3043
3044 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003045}
3046
3047
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003048static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003049load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003050{
3051 char path[PATH_MAX];
3052 void *module, *init;
3053
3054 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003055 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003056 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003057 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003058
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003059 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3060 if (module) {
3061 weston_log("Module '%s' already loaded\n", path);
3062 dlclose(module);
3063 return NULL;
3064 }
3065
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003066 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003067 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003068 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003069 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003070 return NULL;
3071 }
3072
3073 init = dlsym(module, entrypoint);
3074 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003075 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003076 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003077 return NULL;
3078 }
3079
3080 return init;
3081}
3082
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003083static int
3084load_modules(struct weston_compositor *ec, const char *modules)
3085{
3086 const char *p, *end;
3087 char buffer[256];
3088 int (*module_init)(struct weston_compositor *ec);
3089
3090 if (modules == NULL)
3091 return 0;
3092
3093 p = modules;
3094 while (*p) {
3095 end = strchrnul(p, ',');
3096 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3097 module_init = load_module(buffer, "module_init");
3098 if (module_init)
3099 module_init(ec);
3100 p = end;
3101 while (*p == ',')
3102 p++;
3103
3104 }
3105
3106 return 0;
3107}
3108
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003109static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003110 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3111
3112static const char xdg_wrong_message[] =
3113 "fatal: environment variable XDG_RUNTIME_DIR\n"
3114 "is set to \"%s\", which is not a directory.\n";
3115
3116static const char xdg_wrong_mode_message[] =
3117 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3118 "correctly. Unix access mode must be 0700 but is %o,\n"
3119 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003120 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003121
3122static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003123 "Refer to your distribution on how to get it, or\n"
3124 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3125 "on how to implement it.\n";
3126
Martin Minarik37032f82012-06-18 20:15:18 +02003127static void
3128verify_xdg_runtime_dir(void)
3129{
3130 char *dir = getenv("XDG_RUNTIME_DIR");
3131 struct stat s;
3132
3133 if (!dir) {
3134 weston_log(xdg_error_message);
3135 weston_log_continue(xdg_detail_message);
3136 exit(EXIT_FAILURE);
3137 }
3138
3139 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3140 weston_log(xdg_wrong_message, dir);
3141 weston_log_continue(xdg_detail_message);
3142 exit(EXIT_FAILURE);
3143 }
3144
3145 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3146 weston_log(xdg_wrong_mode_message,
3147 dir, s.st_mode & 0777, s.st_uid);
3148 weston_log_continue(xdg_detail_message);
3149 }
3150}
3151
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003152static int
3153usage(int error_code)
3154{
3155 fprintf(stderr,
3156 "Usage: weston [OPTIONS]\n\n"
3157 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3158 "Weston supports multiple backends, and depending on which backend is in use\n"
3159 "different options will be accepted.\n\n"
3160
3161
3162 "Core options:\n\n"
3163 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3164 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3165 " -S, --socket=NAME\tName of socket to listen on\n"
3166 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003167 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003168 " --log==FILE\t\tLog to the given file\n"
3169 " -h, --help\t\tThis help message\n\n");
3170
3171 fprintf(stderr,
3172 "Options for drm-backend.so:\n\n"
3173 " --connector=ID\tBring up only this connector\n"
3174 " --seat=SEAT\t\tThe seat that weston should run on\n"
3175 " --tty=TTY\t\tThe tty to use\n"
3176 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3177
3178 fprintf(stderr,
3179 "Options for x11-backend.so:\n\n"
3180 " --width=WIDTH\t\tWidth of X window\n"
3181 " --height=HEIGHT\tHeight of X window\n"
3182 " --fullscreen\t\tRun in fullscreen mode\n"
3183 " --output-count=COUNT\tCreate multiple outputs\n"
3184 " --no-input\t\tDont create input devices\n\n");
3185
3186 fprintf(stderr,
3187 "Options for wayland-backend.so:\n\n"
3188 " --width=WIDTH\t\tWidth of Wayland surface\n"
3189 " --height=HEIGHT\tHeight of Wayland surface\n"
3190 " --display=DISPLAY\tWayland display to connect to\n\n");
3191
3192 exit(error_code);
3193}
3194
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003195int main(int argc, char *argv[])
3196{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003197 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003198 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003199 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003200 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003201 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003202 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003203 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003204 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003205 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003206 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003207 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003208 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003209 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003210 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003211 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003212 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003213 char *config_file;
3214
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003215 const struct config_key core_config_keys[] = {
3216 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003217 };
3218
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003219 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003220 { "core",
3221 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003222 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003223
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003224 const struct weston_option core_options[] = {
3225 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3226 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3227 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003228 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003229 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003230 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003231 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003232
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003233 argc = parse_options(core_options,
3234 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003235
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003236 if (help)
3237 usage(EXIT_SUCCESS);
3238
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003239 weston_log_file_open(log);
3240
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003241 weston_log("%s\n"
3242 STAMP_SPACE "%s\n"
3243 STAMP_SPACE "Bug reports to: %s\n"
3244 STAMP_SPACE "Build: %s\n",
3245 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003246 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003247 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003248
Martin Minarik37032f82012-06-18 20:15:18 +02003249 verify_xdg_runtime_dir();
3250
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003251 display = wl_display_create();
3252
Tiago Vignatti2116b892011-08-08 05:52:59 -07003253 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003254 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3255 display);
3256 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3257 display);
3258 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3259 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003260
3261 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003262 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3263 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003264
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003265 if (!backend) {
3266 if (getenv("WAYLAND_DISPLAY"))
3267 backend = "wayland-backend.so";
3268 else if (getenv("DISPLAY"))
3269 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003270 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003271 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003272 }
3273
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003274 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003275 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003276
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003277 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003278 if (!backend_init)
3279 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003280
Daniel Stonec1be8e52012-06-01 11:14:02 -04003281 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003282 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003283 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003284 exit(EXIT_FAILURE);
3285 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003286
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003287 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3288 segv_action.sa_sigaction = on_segv_signal;
3289 sigemptyset(&segv_action.sa_mask);
3290 sigaction(SIGSEGV, &segv_action, NULL);
3291 segv_compositor = ec;
3292
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003293 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003294 weston_log("fatal: unhandled option: %s\n", argv[i]);
3295 if (argv[1]) {
3296 ret = EXIT_FAILURE;
3297 goto out;
3298 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003299
Daniel Stonec1be8e52012-06-01 11:14:02 -04003300 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003301
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003302 ec->option_idle_time = idle_time;
3303 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003304
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003305 setenv("WAYLAND_DISPLAY", socket_name, 1);
3306
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003307 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003308 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003309 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003310 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003311
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003312 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003313 weston_log("fatal: failed to add socket: %m\n");
3314 ret = EXIT_FAILURE;
3315 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003316 }
3317
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003318 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003319 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003320
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003321 wl_display_run(display);
3322
3323 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003324 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003325 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003326
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003327 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003328
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003329 for (i = ARRAY_LENGTH(signals); i;)
3330 wl_event_source_remove(signals[--i]);
3331
Daniel Stone855028f2012-05-01 20:37:10 +01003332 weston_compositor_xkb_destroy(ec);
3333
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003334 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003335 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003336
Martin Minarik19e6f262012-06-07 13:08:46 +02003337 weston_log_file_close();
3338
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003339 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003340}