blob: f6c94b6d0d536cca044462879239e0363947d23d [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
Alex Wu2dda6042012-04-17 17:20:47 +080087WL_EXPORT int
88weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
89{
90 if (!output->switch_mode)
91 return -1;
92
93 return output->switch_mode(output, mode);
94}
95
Kristian Høgsberg27da5382011-06-21 17:32:25 -040096WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050097weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040098{
99 wl_list_insert(&child_process_list, &process->link);
100}
101
Benjamin Franzke06286262011-05-06 19:12:33 +0200102static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200103child_client_exec(int sockfd, const char *path)
104{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500105 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200106 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200107 sigset_t allsigs;
108
109 /* do not give our signal mask to the new process */
110 sigfillset(&allsigs);
111 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200112
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500113 /* Launch clients as the user. */
114 seteuid(getuid());
115
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500116 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
117 * non-CLOEXEC fd to pass through exec. */
118 clientfd = dup(sockfd);
119 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200120 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500121 return;
122 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200123
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500124 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200125 setenv("WAYLAND_SOCKET", s, 1);
126
127 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200128 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200129 path);
130}
131
132WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500133weston_client_launch(struct weston_compositor *compositor,
134 struct weston_process *proc,
135 const char *path,
136 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200137{
138 int sv[2];
139 pid_t pid;
140 struct wl_client *client;
141
Pekka Paalanendf1fd362012-08-06 14:57:03 +0300142 weston_log("launching '%s'\n", path);
143
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300144 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200145 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200146 "socketpair failed while launching '%s': %m\n",
147 path);
148 return NULL;
149 }
150
151 pid = fork();
152 if (pid == -1) {
153 close(sv[0]);
154 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200155 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200156 "fork failed while launching '%s': %m\n", path);
157 return NULL;
158 }
159
160 if (pid == 0) {
161 child_client_exec(sv[1], path);
162 exit(-1);
163 }
164
165 close(sv[1]);
166
167 client = wl_client_create(compositor->wl_display, sv[0]);
168 if (!client) {
169 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200170 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200171 "wl_client_create failed while launching '%s'.\n",
172 path);
173 return NULL;
174 }
175
176 proc->pid = pid;
177 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500178 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200179
180 return client;
181}
182
183static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300184surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
185{
186 struct weston_surface *surface =
187 container_of(listener, struct weston_surface,
188 pending.buffer_destroy_listener);
189
190 surface->pending.buffer = NULL;
191}
192
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200193static void
194empty_region(pixman_region32_t *region)
195{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300196 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200197 pixman_region32_init(region);
198}
199
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300200static void
201region_init_infinite(pixman_region32_t *region)
202{
203 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
204 UINT32_MAX, UINT32_MAX);
205}
206
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500207WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500208weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500209{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500210 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400211
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200212 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400213 if (surface == NULL)
214 return NULL;
215
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400216 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500217
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500218 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500219 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500220
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400221 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400222
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500223 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400224 surface->alpha = 1.0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400225
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100226 if (compositor->renderer->create_surface(surface) < 0) {
227 free(surface);
228 return NULL;
229 }
230
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200231 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
232 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400233 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400234 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200235
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400236 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500237 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500238 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300239 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200240 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500241 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400242
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200243 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200244 wl_list_insert(&surface->geometry.transformation_list,
245 &surface->transform.position.link);
246 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200247 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200248 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500249
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300250 surface->pending.buffer_destroy_listener.notify =
251 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300252 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300253 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300254 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300255 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300256
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400257 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500258}
259
Alex Wu8811bf92012-02-28 18:07:54 +0800260WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500261weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200262 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500263{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100264 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500265}
266
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400267WL_EXPORT void
268weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200269 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200270{
271 if (surface->transform.enabled) {
272 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
273
274 weston_matrix_transform(&surface->transform.matrix, &v);
275
276 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200277 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200278 "weston_surface_to_global(), divisor = %g\n",
279 v.f[3]);
280 *x = 0;
281 *y = 0;
282 return;
283 }
284
285 *x = v.f[0] / v.f[3];
286 *y = v.f[1] / v.f[3];
287 } else {
288 *x = sx + surface->geometry.x;
289 *y = sy + surface->geometry.y;
290 }
291}
292
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500293WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200294weston_surface_to_buffer_float(struct weston_surface *surface,
295 float sx, float sy, float *bx, float *by)
296{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200297 weston_transformed_coord(surface->geometry.width,
298 surface->geometry.height,
299 surface->buffer_transform,
300 sx, sy, bx, by);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200301}
302
303WL_EXPORT pixman_box32_t
304weston_surface_to_buffer_rect(struct weston_surface *surface,
305 pixman_box32_t rect)
306{
Ander Conselvan de Oliveira409eebf2012-12-05 15:14:04 +0200307 return weston_transformed_rect(surface->geometry.width,
308 surface->geometry.height,
309 surface->buffer_transform, rect);
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200310}
311
312WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400313weston_surface_move_to_plane(struct weston_surface *surface,
314 struct weston_plane *plane)
315{
316 if (surface->plane == plane)
317 return;
318
319 weston_surface_damage_below(surface);
320 surface->plane = plane;
321 weston_surface_damage(surface);
322}
323
324WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500325weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200326{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500329 pixman_region32_init(&damage);
330 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
331 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400332 pixman_region32_union(&surface->plane->damage,
333 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500334 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200335}
336
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400337static struct wl_resource *
338find_resource_for_client(struct wl_list *list, struct wl_client *client)
339{
340 struct wl_resource *r;
341
342 wl_list_for_each(r, list, link) {
343 if (r->client == client)
344 return r;
345 }
346
347 return NULL;
348}
349
350static void
351weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
352{
353 uint32_t different = es->output_mask ^ mask;
354 uint32_t entered = mask & different;
355 uint32_t left = es->output_mask & different;
356 struct weston_output *output;
357 struct wl_resource *resource = NULL;
358 struct wl_client *client = es->surface.resource.client;
359
360 es->output_mask = mask;
361 if (es->surface.resource.client == NULL)
362 return;
363 if (different == 0)
364 return;
365
366 wl_list_for_each(output, &es->compositor->output_list, link) {
367 if (1 << output->id & different)
368 resource =
369 find_resource_for_client(&output->resource_list,
370 client);
371 if (resource == NULL)
372 continue;
373 if (1 << output->id & entered)
374 wl_surface_send_enter(&es->surface.resource, resource);
375 if (1 << output->id & left)
376 wl_surface_send_leave(&es->surface.resource, resource);
377 }
378}
379
380static void
381weston_surface_assign_output(struct weston_surface *es)
382{
383 struct weston_compositor *ec = es->compositor;
384 struct weston_output *output, *new_output;
385 pixman_region32_t region;
386 uint32_t max, area, mask;
387 pixman_box32_t *e;
388
389 new_output = NULL;
390 max = 0;
391 mask = 0;
392 pixman_region32_init(&region);
393 wl_list_for_each(output, &ec->output_list, link) {
394 pixman_region32_intersect(&region, &es->transform.boundingbox,
395 &output->region);
396
397 e = pixman_region32_extents(&region);
398 area = (e->x2 - e->x1) * (e->y2 - e->y1);
399
400 if (area > 0)
401 mask |= 1 << output->id;
402
403 if (area >= max) {
404 new_output = output;
405 max = area;
406 }
407 }
408 pixman_region32_fini(&region);
409
410 es->output = new_output;
411 weston_surface_update_output_mask(es, mask);
412}
413
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200414static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200415surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
416 int32_t width, int32_t height,
417 pixman_region32_t *bbox)
418{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200419 float min_x = HUGE_VALF, min_y = HUGE_VALF;
420 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200421 int32_t s[4][2] = {
422 { sx, sy },
423 { sx, sy + height },
424 { sx + width, sy },
425 { sx + width, sy + height }
426 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200427 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200428 int i;
429
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300430 if (width == 0 || height == 0) {
431 /* avoid rounding empty bbox to 1x1 */
432 pixman_region32_init(bbox);
433 return;
434 }
435
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200436 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200437 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400438 weston_surface_to_global_float(surface,
439 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200440 if (x < min_x)
441 min_x = x;
442 if (x > max_x)
443 max_x = x;
444 if (y < min_y)
445 min_y = y;
446 if (y > max_y)
447 max_y = y;
448 }
449
Pekka Paalanen219b9822012-02-08 15:38:37 +0200450 int_x = floorf(min_x);
451 int_y = floorf(min_y);
452 pixman_region32_init_rect(bbox, int_x, int_y,
453 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200454}
455
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200456static void
457weston_surface_update_transform_disable(struct weston_surface *surface)
458{
459 surface->transform.enabled = 0;
460
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200461 /* round off fractions when not transformed */
462 surface->geometry.x = roundf(surface->geometry.x);
463 surface->geometry.y = roundf(surface->geometry.y);
464
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200465 pixman_region32_init_rect(&surface->transform.boundingbox,
466 surface->geometry.x,
467 surface->geometry.y,
468 surface->geometry.width,
469 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500470
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400471 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500472 pixman_region32_copy(&surface->transform.opaque,
473 &surface->opaque);
474 pixman_region32_translate(&surface->transform.opaque,
475 surface->geometry.x,
476 surface->geometry.y);
477 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200478}
479
480static int
481weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200482{
483 struct weston_matrix *matrix = &surface->transform.matrix;
484 struct weston_matrix *inverse = &surface->transform.inverse;
485 struct weston_transform *tform;
486
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200487 surface->transform.enabled = 1;
488
489 /* Otherwise identity matrix, but with x and y translation. */
490 surface->transform.position.matrix.d[12] = surface->geometry.x;
491 surface->transform.position.matrix.d[13] = surface->geometry.y;
492
493 weston_matrix_init(matrix);
494 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
495 weston_matrix_multiply(matrix, &tform->matrix);
496
497 if (weston_matrix_invert(inverse, matrix) < 0) {
498 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200499 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200500 " transformation not invertible.\n", surface);
501 return -1;
502 }
503
504 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
505 surface->geometry.height,
506 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500507
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200508 return 0;
509}
510
511WL_EXPORT void
512weston_surface_update_transform(struct weston_surface *surface)
513{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200514 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200515 return;
516
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200517 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200518
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500519 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200520
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200521 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500522 pixman_region32_fini(&surface->transform.opaque);
523 pixman_region32_init(&surface->transform.opaque);
524
Pekka Paalanencd403622012-01-25 13:37:39 +0200525 /* transform.position is always in transformation_list */
526 if (surface->geometry.transformation_list.next ==
527 &surface->transform.position.link &&
528 surface->geometry.transformation_list.prev ==
529 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200530 weston_surface_update_transform_disable(surface);
531 } else {
532 if (weston_surface_update_transform_enable(surface) < 0)
533 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200534 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200535
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400536 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200537
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300538 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200539}
540
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200541WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100542weston_surface_to_global_fixed(struct weston_surface *surface,
543 wl_fixed_t sx, wl_fixed_t sy,
544 wl_fixed_t *x, wl_fixed_t *y)
545{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200546 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100547
548 weston_surface_to_global_float(surface,
549 wl_fixed_to_double(sx),
550 wl_fixed_to_double(sy),
551 &xf, &yf);
552 *x = wl_fixed_from_double(xf);
553 *y = wl_fixed_from_double(yf);
554}
555
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400556WL_EXPORT void
557weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200558 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200559{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200560 if (surface->transform.enabled) {
561 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
562
563 weston_matrix_transform(&surface->transform.inverse, &v);
564
565 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200566 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200567 "weston_surface_from_global(), divisor = %g\n",
568 v.f[3]);
569 *sx = 0;
570 *sy = 0;
571 return;
572 }
573
Pekka Paalanencd403622012-01-25 13:37:39 +0200574 *sx = v.f[0] / v.f[3];
575 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200576 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200577 *sx = x - surface->geometry.x;
578 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200579 }
580}
581
582WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100583weston_surface_from_global_fixed(struct weston_surface *surface,
584 wl_fixed_t x, wl_fixed_t y,
585 wl_fixed_t *sx, wl_fixed_t *sy)
586{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200587 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100588
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400589 weston_surface_from_global_float(surface,
590 wl_fixed_to_double(x),
591 wl_fixed_to_double(y),
592 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100593 *sx = wl_fixed_from_double(sxf);
594 *sy = wl_fixed_from_double(syf);
595}
596
597WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200598weston_surface_from_global(struct weston_surface *surface,
599 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
600{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200601 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200602
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400603 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200604 *sx = floorf(sxf);
605 *sy = floorf(syf);
606}
607
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400608WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400609weston_surface_schedule_repaint(struct weston_surface *surface)
610{
611 struct weston_output *output;
612
613 wl_list_for_each(output, &surface->compositor->output_list, link)
614 if (surface->output_mask & (1 << output->id))
615 weston_output_schedule_repaint(output);
616}
617
618WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500619weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500620{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400621 pixman_region32_union_rect(&surface->damage, &surface->damage,
622 0, 0, surface->geometry.width,
623 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200624
Kristian Høgsberg98238702012-08-03 16:29:12 -0400625 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500626}
627
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400628WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500629weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200630 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400631{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200632 surface->geometry.x = x;
633 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200634 surface->geometry.width = width;
635 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200636 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400637}
638
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200639WL_EXPORT void
640weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200641 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200642{
643 surface->geometry.x = x;
644 surface->geometry.y = y;
645 surface->geometry.dirty = 1;
646}
647
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300648WL_EXPORT int
649weston_surface_is_mapped(struct weston_surface *surface)
650{
651 if (surface->output)
652 return 1;
653 else
654 return 0;
655}
656
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200657WL_EXPORT int32_t
658weston_surface_buffer_width(struct weston_surface *surface)
659{
660 switch (surface->buffer_transform) {
661 case WL_OUTPUT_TRANSFORM_90:
662 case WL_OUTPUT_TRANSFORM_270:
663 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
664 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200665 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200666 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200667 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200668 }
669}
670
671WL_EXPORT int32_t
672weston_surface_buffer_height(struct weston_surface *surface)
673{
674 switch (surface->buffer_transform) {
675 case WL_OUTPUT_TRANSFORM_90:
676 case WL_OUTPUT_TRANSFORM_270:
677 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
678 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
Pekka Paalanende685b82012-12-04 15:58:12 +0200679 return surface->buffer_ref.buffer->width;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200680 default:
Pekka Paalanende685b82012-12-04 15:58:12 +0200681 return surface->buffer_ref.buffer->height;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200682 }
683}
684
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400685WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500686weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500687{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400688 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500689
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400690 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500691
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400692 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500693}
694
Tiago Vignatti9d393522012-02-10 16:26:19 +0200695static struct weston_surface *
696weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100697 wl_fixed_t x, wl_fixed_t y,
698 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200699{
700 struct weston_surface *surface;
701
702 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100703 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500704 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100705 wl_fixed_to_int(*sx),
706 wl_fixed_to_int(*sy),
707 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200708 return surface;
709 }
710
711 return NULL;
712}
713
714static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400715weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500716{
Scott Moreau447013d2012-02-18 05:05:29 -0700717 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500718 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400719 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500720
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400721 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100722 return;
723
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400724 surface = weston_compositor_pick_surface(seat->compositor,
725 pointer->x,
726 pointer->y,
727 &pointer->current_x,
728 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500729
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400730 if (&surface->surface != pointer->current) {
731 interface = pointer->grab->interface;
732 pointer->current = &surface->surface;
733 interface->focus(pointer->grab, &surface->surface,
734 pointer->current_x,
735 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500736 }
737
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400738 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500739 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100740 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400741 pointer->x,
742 pointer->y,
743 &pointer->grab->x,
744 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500745}
746
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400747static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500748weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400749{
Daniel Stone37816df2012-05-16 18:45:18 +0100750 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400751
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500752 if (!compositor->focus)
753 return;
754
Daniel Stone37816df2012-05-16 18:45:18 +0100755 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400756 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400757}
758
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400759WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500760weston_surface_unmap(struct weston_surface *surface)
761{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100762 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500763
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500764 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500765 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500766 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500767
Daniel Stone4dab5db2012-05-30 16:31:53 +0100768 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100769 if (seat->seat.keyboard &&
770 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100771 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100772 if (seat->seat.pointer &&
773 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100774 wl_pointer_set_focus(seat->seat.pointer,
775 NULL,
776 wl_fixed_from_int(0),
777 wl_fixed_from_int(0));
778 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500779
Kristian Høgsberg98238702012-08-03 16:29:12 -0400780 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500781}
782
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400783struct weston_frame_callback {
784 struct wl_resource resource;
785 struct wl_list link;
786};
787
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500788static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400789destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500790{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500791 struct weston_surface *surface =
792 container_of(resource,
793 struct weston_surface, surface.resource);
794 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400795 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400796
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300797 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500798 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400799
Pekka Paalanenbc106382012-10-10 12:49:31 +0300800 wl_list_for_each_safe(cb, next,
801 &surface->pending.frame_callback_list, link)
802 wl_resource_destroy(&cb->resource);
803
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300804 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300805 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300806 pixman_region32_fini(&surface->pending.damage);
807
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300808 if (surface->pending.buffer)
809 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
810
Pekka Paalanende685b82012-12-04 15:58:12 +0200811 weston_buffer_reference(&surface->buffer_ref, NULL);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400812
Kristian Høgsberg42263852012-09-06 21:59:29 -0400813 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200814
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200815 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200816 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500817 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500818 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300819 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200820
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400821 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
822 wl_resource_destroy(&cb->resource);
823
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400824 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500825}
826
Alex Wu8811bf92012-02-28 18:07:54 +0800827WL_EXPORT void
828weston_surface_destroy(struct weston_surface *surface)
829{
830 /* Not a valid way to destroy a client surface */
831 assert(surface->surface.resource.client == NULL);
832
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400833 wl_signal_emit(&surface->surface.resource.destroy_signal,
834 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800835 destroy_surface(&surface->surface.resource);
836}
837
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100838static void
Pekka Paalanende685b82012-12-04 15:58:12 +0200839weston_buffer_reference_handle_destroy(struct wl_listener *listener,
840 void *data)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100841{
Pekka Paalanende685b82012-12-04 15:58:12 +0200842 struct weston_buffer_reference *ref =
843 container_of(listener, struct weston_buffer_reference,
844 destroy_listener);
845
846 assert((struct wl_buffer *)data == ref->buffer);
847 ref->buffer = NULL;
848}
849
850WL_EXPORT void
851weston_buffer_reference(struct weston_buffer_reference *ref,
852 struct wl_buffer *buffer)
853{
854 if (ref->buffer && buffer != ref->buffer) {
855 weston_buffer_post_release(ref->buffer);
856 wl_list_remove(&ref->destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300857 }
858
Pekka Paalanende685b82012-12-04 15:58:12 +0200859 if (buffer && buffer != ref->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400860 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300861 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanende685b82012-12-04 15:58:12 +0200862 &ref->destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200863 }
864
Pekka Paalanende685b82012-12-04 15:58:12 +0200865 ref->buffer = buffer;
866 ref->destroy_listener.notify = weston_buffer_reference_handle_destroy;
867}
868
869static void
870weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
871{
872 weston_buffer_reference(&surface->buffer_ref, buffer);
873
Pekka Paalanena6421c42012-12-04 15:58:10 +0200874 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300875 if (weston_surface_is_mapped(surface))
876 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300877 }
878
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300879 surface->compositor->renderer->attach(surface, buffer);
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100880}
881
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500882WL_EXPORT void
883weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400884{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500885 wl_list_remove(&surface->layer_link);
886 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500887 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500888 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400889}
890
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400891WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500892weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400893{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500894 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400895
896 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500897 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400898}
899
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500900WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500901weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200902{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400903 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200904 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200905
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400906 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500907 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200908}
909
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400910WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500911weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400912{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400914
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400915 pixman_region32_union(&compositor->primary_plane.damage,
916 &compositor->primary_plane.damage,
917 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400918 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400919}
920
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400921static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500922fade_frame(struct weston_animation *animation,
923 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400924{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500925 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400926 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500927 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500928 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400929
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600930 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600931 compositor->fade.spring.timestamp = msecs;
932
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500933 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500934 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500935 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
936 compositor->fade.spring.current);
937 weston_surface_damage(surface);
938
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500939 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400940 compositor->fade.spring.current =
941 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400942 wl_list_remove(&animation->link);
943 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200944
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500945 if (compositor->fade.spring.current < 0.001) {
946 destroy_surface(&surface->surface.resource);
947 compositor->fade.surface = NULL;
948 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500949 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400950 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200951 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400952 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400953}
954
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400955static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400956surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400957 pixman_region32_t *opaque)
958{
Pekka Paalanende685b82012-12-04 15:58:12 +0200959 if (surface->buffer_ref.buffer &&
960 wl_buffer_is_shm(surface->buffer_ref.buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400961 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400962
963 if (surface->transform.enabled) {
964 pixman_box32_t *extents;
965
966 extents = pixman_region32_extents(&surface->damage);
967 surface_compute_bbox(surface, extents->x1, extents->y1,
968 extents->x2 - extents->x1,
969 extents->y2 - extents->y1,
970 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400971 pixman_region32_translate(&surface->damage,
972 -surface->plane->x,
973 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400974 } else {
975 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400976 surface->geometry.x - surface->plane->x,
977 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400978 }
979
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +0200980 pixman_region32_subtract(&surface->damage,
981 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400982 pixman_region32_union(&surface->plane->damage,
983 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400984 empty_region(&surface->damage);
985 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +0200986 pixman_region32_union(&surface->plane->opaque,
987 &surface->plane->opaque,
988 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -0400989 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400990}
991
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400992static void
Rob Bradford0fb79752012-08-02 15:36:57 +0100993weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -0400994{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500995 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500996 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500997 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -0500998 struct weston_animation *animation, *next;
999 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001000 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001001 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001002
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001003 weston_compositor_update_drag_surfaces(ec);
1004
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001005 /* Rebuild the surface list and update surface transforms up front. */
1006 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001007 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001008 wl_list_for_each(layer, &ec->layer_list, link) {
1009 wl_list_for_each(es, &layer->surface_list, layer_link) {
1010 weston_surface_update_transform(es);
1011 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001012 if (es->output == output) {
1013 wl_list_insert_list(&frame_callback_list,
1014 &es->frame_callback_list);
1015 wl_list_init(&es->frame_callback_list);
1016 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001017 }
1018 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001019
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001020 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001021 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001022 else
1023 wl_list_for_each(es, &ec->surface_list, link)
1024 weston_surface_move_to_plane(es, &ec->primary_plane);
1025
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001026 pixman_region32_init(&opaque);
1027
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001028 pixman_region32_fini(&ec->primary_plane.opaque);
1029 pixman_region32_init(&ec->primary_plane.opaque);
1030
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001031 wl_list_for_each(es, &ec->surface_list, link) {
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001032 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001033
Pekka Paalanenccfeae22012-12-04 15:58:14 +02001034 /* Both the renderer and the backend have seen the buffer
1035 * by now. If renderer needs the buffer, it has its own
1036 * reference set. If the backend wants to keep the buffer
1037 * around for migrating the surface into a non-primary plane
1038 * later, keep_buffer is true. Otherwise, drop the core
1039 * reference now, and allow early buffer release. This enables
1040 * clients to use single-buffering.
1041 */
1042 if (!es->keep_buffer)
1043 weston_buffer_reference(&es->buffer_ref, NULL);
1044 }
1045
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001046 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001047
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001048 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001049 pixman_region32_intersect(&output_damage,
1050 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001051
Scott Moreauccbf29d2012-02-22 14:21:41 -07001052 if (output->dirty)
1053 weston_output_update_matrix(output);
1054
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001055 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001056
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001057 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001058
Kristian Høgsbergef044142011-06-21 15:02:12 -04001059 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001060
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001061 weston_compositor_repick(ec);
1062 wl_event_loop_dispatch(ec->input_loop, 0);
1063
Jonas Ådahldb773762012-06-13 00:01:21 +02001064 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001065 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001066 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001067 }
1068
Scott Moreaud64cf212012-06-08 19:40:54 -06001069 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001070 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001071 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001072 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001073}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001074
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001075static int
1076weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001077{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001078 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001079
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001080 wl_event_loop_dispatch(compositor->input_loop, 0);
1081
1082 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001083}
1084
1085WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001086weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001087{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001088 struct weston_compositor *compositor = output->compositor;
1089 struct wl_event_loop *loop =
1090 wl_display_get_event_loop(compositor->wl_display);
1091 int fd;
1092
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001093 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001094 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001095 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001096 return;
1097 }
1098
1099 output->repaint_scheduled = 0;
1100 if (compositor->input_loop_source)
1101 return;
1102
1103 fd = wl_event_loop_get_fd(compositor->input_loop);
1104 compositor->input_loop_source =
1105 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1106 weston_compositor_read_input, compositor);
1107}
1108
1109static void
1110idle_repaint(void *data)
1111{
1112 struct weston_output *output = data;
1113
1114 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001115}
1116
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001117WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001118weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1119{
1120 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001121 if (below != NULL)
1122 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001123}
1124
1125WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001126weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001127{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001128 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001129 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001130
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001131 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001132 return;
1133
Kristian Høgsbergef044142011-06-21 15:02:12 -04001134 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001135 output->repaint_needed = 1;
1136 if (output->repaint_scheduled)
1137 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001138
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001139 wl_event_loop_add_idle(loop, idle_repaint, output);
1140 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001141
1142 if (compositor->input_loop_source) {
1143 wl_event_source_remove(compositor->input_loop_source);
1144 compositor->input_loop_source = NULL;
1145 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001146}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001147
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001148WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001149weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1150{
1151 struct weston_output *output;
1152
1153 wl_list_for_each(output, &compositor->output_list, link)
1154 weston_output_schedule_repaint(output);
1155}
1156
1157WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001158weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001159{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001160 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001161 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001162
Scott Moreau9d1b1122012-06-08 19:40:53 -06001163 output = container_of(compositor->output_list.next,
1164 struct weston_output, link);
1165
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001166 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001167 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001168 return;
1169
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001170 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001171 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001172 if (!surface)
1173 return;
1174
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001175 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001176 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001177 wl_list_insert(&compositor->fade_layer.surface_list,
1178 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001179 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001180 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001181 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001182 }
1183
1184 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001185 if (wl_list_empty(&compositor->fade.animation.link)) {
1186 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001187 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001188 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001189 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001190}
1191
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001192static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001193surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001194{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001195 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001196}
1197
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001198static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001199surface_attach(struct wl_client *client,
1200 struct wl_resource *resource,
1201 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1202{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001203 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001204 struct wl_buffer *buffer = NULL;
1205
1206 if (buffer_resource)
1207 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001208
Pekka Paalanende685b82012-12-04 15:58:12 +02001209 /* Attach, attach, without commit in between does not send
1210 * wl_buffer.release. */
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001211 if (surface->pending.buffer)
1212 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001213
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001214 surface->pending.sx = sx;
1215 surface->pending.sy = sy;
1216 surface->pending.buffer = buffer;
1217 if (buffer) {
1218 wl_signal_add(&buffer->resource.destroy_signal,
1219 &surface->pending.buffer_destroy_listener);
1220 surface->pending.remove_contents = 0;
1221 } else {
1222 surface->pending.remove_contents = 1;
1223 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001224}
1225
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001226static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001227surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001228 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001229 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001230{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001231 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001232
Pekka Paalanen8e159182012-10-10 12:49:25 +03001233 pixman_region32_union_rect(&surface->pending.damage,
1234 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001235 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001236}
1237
Kristian Høgsberg33418202011-08-16 23:01:28 -04001238static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001239destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001240{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001241 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001242
1243 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001244 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001245}
1246
1247static void
1248surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001249 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001250{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001251 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001252 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001253
1254 cb = malloc(sizeof *cb);
1255 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001256 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001257 return;
1258 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001259
Kristian Høgsberg33418202011-08-16 23:01:28 -04001260 cb->resource.object.interface = &wl_callback_interface;
1261 cb->resource.object.id = callback;
1262 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001263 cb->resource.client = client;
1264 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001265
1266 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001267 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001268}
1269
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001270static void
1271surface_set_opaque_region(struct wl_client *client,
1272 struct wl_resource *resource,
1273 struct wl_resource *region_resource)
1274{
1275 struct weston_surface *surface = resource->data;
1276 struct weston_region *region;
1277
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001278 if (region_resource) {
1279 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001280 pixman_region32_copy(&surface->pending.opaque,
1281 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001282 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001283 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001284 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001285}
1286
1287static void
1288surface_set_input_region(struct wl_client *client,
1289 struct wl_resource *resource,
1290 struct wl_resource *region_resource)
1291{
1292 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001293 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001294
1295 if (region_resource) {
1296 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001297 pixman_region32_copy(&surface->pending.input,
1298 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001299 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001300 pixman_region32_fini(&surface->pending.input);
1301 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001302 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001303}
1304
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001305static int
1306surface_pending_buffer_has_different_size(struct weston_surface *surface)
1307{
1308 int width, height;
1309
1310 switch (surface->pending.buffer_transform) {
1311 case WL_OUTPUT_TRANSFORM_90:
1312 case WL_OUTPUT_TRANSFORM_270:
1313 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1314 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1315 height = surface->pending.buffer->width;
1316 width = surface->pending.buffer->height;
1317 break;
1318 default:
1319 width = surface->pending.buffer->width;
1320 height = surface->pending.buffer->height;
1321 }
1322
1323 if (width == surface->geometry.width &&
1324 height == surface->geometry.height)
1325 return 0;
1326 else
1327 return 1;
1328}
1329
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001330static void
1331surface_commit(struct wl_client *client, struct wl_resource *resource)
1332{
1333 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001334 pixman_region32_t opaque;
1335
1336 if (surface->pending.sx || surface->pending.sy ||
1337 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001338 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001339 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001340
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001341 /* wl_surface.set_buffer_rotation */
1342 surface->buffer_transform = surface->pending.buffer_transform;
1343
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001344 /* wl_surface.attach */
1345 if (surface->pending.buffer || surface->pending.remove_contents)
1346 weston_surface_attach(surface, surface->pending.buffer);
1347
Pekka Paalanende685b82012-12-04 15:58:12 +02001348 if (surface->buffer_ref.buffer && surface->configure)
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001349 surface->configure(surface, surface->pending.sx,
1350 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001351 surface->pending.sx = 0;
1352 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001353
1354 /* wl_surface.damage */
1355 pixman_region32_union(&surface->damage, &surface->damage,
1356 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001357 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1358 0, 0,
1359 surface->geometry.width,
1360 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001361 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001362
1363 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001364 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001365 surface->geometry.width,
1366 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001367 pixman_region32_intersect(&opaque,
1368 &opaque, &surface->pending.opaque);
1369
1370 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1371 pixman_region32_copy(&surface->opaque, &opaque);
1372 surface->geometry.dirty = 1;
1373 }
1374
1375 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001376
1377 /* wl_surface.set_input_region */
1378 pixman_region32_fini(&surface->input);
1379 pixman_region32_init_rect(&surface->input, 0, 0,
1380 surface->geometry.width,
1381 surface->geometry.height);
1382 pixman_region32_intersect(&surface->input,
1383 &surface->input, &surface->pending.input);
1384
Pekka Paalanenbc106382012-10-10 12:49:31 +03001385 /* wl_surface.frame */
1386 wl_list_insert_list(&surface->frame_callback_list,
1387 &surface->pending.frame_callback_list);
1388 wl_list_init(&surface->pending.frame_callback_list);
1389
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001390 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001391}
1392
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001393static void
1394surface_set_buffer_transform(struct wl_client *client,
1395 struct wl_resource *resource, int transform)
1396{
1397 struct weston_surface *surface = resource->data;
1398
1399 surface->pending.buffer_transform = transform;
1400}
1401
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001402static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001403 surface_destroy,
1404 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001405 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001406 surface_frame,
1407 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001408 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001409 surface_commit,
1410 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001411};
1412
1413static void
1414compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001415 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001416{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001417 struct weston_compositor *ec = resource->data;
1418 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001419
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001420 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001421 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001422 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001423 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001424 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001425
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001426 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001427
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001428 surface->surface.resource.object.id = id;
1429 surface->surface.resource.object.interface = &wl_surface_interface;
1430 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001431 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001432 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001433
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001434 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001435}
1436
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001437static void
1438destroy_region(struct wl_resource *resource)
1439{
1440 struct weston_region *region =
1441 container_of(resource, struct weston_region, resource);
1442
1443 pixman_region32_fini(&region->region);
1444 free(region);
1445}
1446
1447static void
1448region_destroy(struct wl_client *client, struct wl_resource *resource)
1449{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001450 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001451}
1452
1453static void
1454region_add(struct wl_client *client, struct wl_resource *resource,
1455 int32_t x, int32_t y, int32_t width, int32_t height)
1456{
1457 struct weston_region *region = resource->data;
1458
1459 pixman_region32_union_rect(&region->region, &region->region,
1460 x, y, width, height);
1461}
1462
1463static void
1464region_subtract(struct wl_client *client, struct wl_resource *resource,
1465 int32_t x, int32_t y, int32_t width, int32_t height)
1466{
1467 struct weston_region *region = resource->data;
1468 pixman_region32_t rect;
1469
1470 pixman_region32_init_rect(&rect, x, y, width, height);
1471 pixman_region32_subtract(&region->region, &region->region, &rect);
1472 pixman_region32_fini(&rect);
1473}
1474
1475static const struct wl_region_interface region_interface = {
1476 region_destroy,
1477 region_add,
1478 region_subtract
1479};
1480
1481static void
1482compositor_create_region(struct wl_client *client,
1483 struct wl_resource *resource, uint32_t id)
1484{
1485 struct weston_region *region;
1486
1487 region = malloc(sizeof *region);
1488 if (region == NULL) {
1489 wl_resource_post_no_memory(resource);
1490 return;
1491 }
1492
1493 region->resource.destroy = destroy_region;
1494
1495 region->resource.object.id = id;
1496 region->resource.object.interface = &wl_region_interface;
1497 region->resource.object.implementation =
1498 (void (**)(void)) &region_interface;
1499 region->resource.data = region;
1500
1501 pixman_region32_init(&region->region);
1502
1503 wl_client_add_resource(client, &region->resource);
1504}
1505
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001506static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001507 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001508 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001509};
1510
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001511WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001512weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001513{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001514 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1515 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001516
1517 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001518 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001519}
1520
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001521static void
1522weston_compositor_dpms_on(struct weston_compositor *compositor)
1523{
1524 struct weston_output *output;
1525
1526 wl_list_for_each(output, &compositor->output_list, link)
1527 if (output->set_dpms)
1528 output->set_dpms(output, WESTON_DPMS_ON);
1529}
1530
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001531WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001532weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001533{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001534 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1535 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001536 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001537 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001538 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001539 }
1540}
1541
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001542static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001543weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001544{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001545 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001546 compositor->idle_inhibit++;
1547}
1548
1549static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001550weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001551{
1552 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001553 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001554}
1555
1556static int
1557idle_handler(void *data)
1558{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001559 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001560
1561 if (compositor->idle_inhibit)
1562 return 1;
1563
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001565
1566 return 1;
1567}
1568
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001569WL_EXPORT void
1570weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1571{
1572 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001573 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001574 plane->x = x;
1575 plane->y = y;
1576}
1577
1578WL_EXPORT void
1579weston_plane_release(struct weston_plane *plane)
1580{
1581 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001582 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001583}
1584
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001585static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001586weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001587
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001588static void
Daniel Stone37816df2012-05-16 18:45:18 +01001589clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001590{
Daniel Stone37816df2012-05-16 18:45:18 +01001591 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001592 struct weston_output *output, *prev = NULL;
1593 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001594
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001595 x = wl_fixed_to_int(*fx);
1596 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001597 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1598 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001599
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001600 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001601 if (pixman_region32_contains_point(&output->region,
1602 x, y, NULL))
1603 valid = 1;
1604 if (pixman_region32_contains_point(&output->region,
1605 old_x, old_y, NULL))
1606 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001607 }
Daniel Stone37816df2012-05-16 18:45:18 +01001608
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001609 if (!valid) {
1610 if (x < prev->x)
1611 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001612 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001613 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001614 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001615 if (y < prev->y)
1616 *fy = wl_fixed_from_int(prev->y);
1617 else if (y >= prev->y + prev->current->height)
1618 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001619 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001620 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001621}
1622
1623WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001624notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001625{
1626 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001627 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001628 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001629 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001630 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001631
1632 weston_compositor_activity(ec);
1633
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001634 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001635
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001636 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001637
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001638 pointer->x = x;
1639 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001640
1641 ix = wl_fixed_to_int(x);
1642 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001643
Scott Moreauccbf29d2012-02-22 14:21:41 -07001644 wl_list_for_each(output, &ec->output_list, link)
1645 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001646 pixman_region32_contains_point(&output->region,
1647 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001648 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001649
Daniel Stone37816df2012-05-16 18:45:18 +01001650 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001651 interface = pointer->grab->interface;
1652 interface->motion(pointer->grab, time,
1653 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001654
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001655 if (seat->sprite) {
1656 weston_surface_set_position(seat->sprite,
1657 ix - seat->hotspot_x,
1658 iy - seat->hotspot_y);
1659 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001660 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001661}
1662
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001663WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001664weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001665 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001666{
Daniel Stone37816df2012-05-16 18:45:18 +01001667 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001668
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001669 if (seat->seat.keyboard) {
1670 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1671 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001672 }
1673
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001674 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001675}
1676
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001677WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001678notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001679 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001680{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001681 struct weston_compositor *compositor = seat->compositor;
1682 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001683 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001684 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001685 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001686
Daniel Stone4dbadb12012-05-30 16:31:51 +01001687 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001688 if (compositor->ping_handler && focus)
1689 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001690 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001691 if (pointer->button_count == 0) {
1692 pointer->grab_button = button;
1693 pointer->grab_time = time;
1694 pointer->grab_x = pointer->x;
1695 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001696 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001697 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001698 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001699 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001700 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001701 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001702
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001703 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001704 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001705
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001706 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001707
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001708 if (pointer->button_count == 1)
1709 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001710 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001711}
1712
Scott Moreau210d0792012-03-22 10:47:01 -06001713WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001714notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001715 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001716{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001717 struct weston_compositor *compositor = seat->compositor;
1718 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001719 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001720 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001721 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1722
1723 if (compositor->ping_handler && focus)
1724 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001725
1726 weston_compositor_activity(compositor);
1727
Scott Moreau6a3633d2012-03-20 08:47:59 -06001728 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001729 weston_compositor_run_axis_binding(compositor, seat,
1730 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001731 else
1732 return;
1733
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001734 if (pointer->focus_resource)
1735 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001736 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001737}
1738
Daniel Stone05d58682012-06-22 13:21:38 +01001739WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001740notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001741{
Daniel Stone05d58682012-06-22 13:21:38 +01001742 struct wl_keyboard *keyboard = &seat->keyboard;
1743 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001744 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001745 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001746 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001747 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001748
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001749 /* Serialize and update our internal state, checking to see if it's
1750 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001751 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1752 XKB_STATE_DEPRESSED);
1753 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1754 XKB_STATE_LATCHED);
1755 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1756 XKB_STATE_LOCKED);
1757 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001758 XKB_STATE_EFFECTIVE);
1759
Daniel Stone71c38772012-06-22 13:21:30 +01001760 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1761 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1762 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1763 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001764 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001765
Daniel Stone71c38772012-06-22 13:21:30 +01001766 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1767 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1768 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1769 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001770
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001771 /* And update the modifier_state for bindings. */
1772 mods_lookup = mods_depressed | mods_latched;
1773 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001774 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001775 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001776 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001777 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001778 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001779 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001780 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1781 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001782
Daniel Stone8c8164f2012-05-30 16:31:45 +01001783 /* Finally, notify the compositor that LEDs have changed. */
1784 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001785 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001786 leds |= LED_NUM_LOCK;
1787 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001788 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001789 leds |= LED_CAPS_LOCK;
1790 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001791 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001792 leds |= LED_SCROLL_LOCK;
1793 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001794 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001795 seat->xkb_state.leds = leds;
1796
Daniel Stone05d58682012-06-22 13:21:38 +01001797 if (changed) {
1798 grab->interface->modifiers(grab,
1799 serial,
1800 keyboard->modifiers.mods_depressed,
1801 keyboard->modifiers.mods_latched,
1802 keyboard->modifiers.mods_locked,
1803 keyboard->modifiers.group);
1804 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001805}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001806
Daniel Stone05d58682012-06-22 13:21:38 +01001807static void
1808update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001809 enum wl_keyboard_key_state state)
1810{
1811 enum xkb_key_direction direction;
1812
1813 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1814 direction = XKB_KEY_DOWN;
1815 else
1816 direction = XKB_KEY_UP;
1817
1818 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1819 * broken keycode system, which starts at 8. */
1820 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1821
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001822 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001823}
1824
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001825WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001826notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001827 enum wl_keyboard_key_state state,
1828 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001829{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001830 struct weston_compositor *compositor = seat->compositor;
1831 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001832 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001833 (struct weston_surface *) keyboard->focus;
1834 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001835 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001836 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001837
Daniel Stonec9785ea2012-05-30 16:31:52 +01001838 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001839 if (compositor->ping_handler && focus)
1840 compositor->ping_handler(focus, serial);
1841
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001842 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001843 keyboard->grab_key = key;
1844 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001845 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001846 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001847 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001848
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001849 end = keyboard->keys.data + keyboard->keys.size;
1850 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001851 if (*k == key) {
1852 /* Ignore server-generated repeats. */
1853 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1854 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001855 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001856 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001857 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001858 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001859 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001860 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001861 *k = key;
1862 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001863
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001864 if (grab == &keyboard->default_grab) {
1865 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001866 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001867 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001868 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001869
Daniel Stone7ace3902012-05-30 16:31:40 +01001870 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001871
1872 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001873 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001874 wl_display_get_serial(compositor->wl_display),
1875 key,
1876 state);
1877 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001878}
1879
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001880WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001881notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001882 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001883{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001884 struct weston_compositor *compositor = seat->compositor;
1885 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001886
1887 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001888 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001889 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001890 x - pointer->x,
1891 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001892
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001893 pointer->x = x;
1894 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001895 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001896 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001897 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001898 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001899 /* FIXME: We should call wl_pointer_set_focus(seat,
1900 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001901 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001902}
1903
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001904static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001905destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001906{
Daniel Stone37816df2012-05-16 18:45:18 +01001907 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001908
Daniel Stone37816df2012-05-16 18:45:18 +01001909 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001910 saved_kbd_focus_listener);
1911
Daniel Stone37816df2012-05-16 18:45:18 +01001912 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001913}
1914
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001915WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001916notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001917 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001918{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001919 struct weston_compositor *compositor = seat->compositor;
1920 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001921 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001922 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001923
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001924 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001925 wl_array_copy(&keyboard->keys, keys);
1926 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001927 weston_compositor_idle_inhibit(compositor);
1928 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001929 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001930 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001931 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001932
Daniel Stoneef172672012-06-22 13:21:32 +01001933 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001934 wl_array_for_each(k, &keyboard->keys) {
1935 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001936 WL_KEYBOARD_KEY_STATE_PRESSED);
1937 }
1938
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001939 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001940
1941 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001942 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1943 wl_keyboard_set_focus(keyboard, surface);
1944 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001945 }
1946}
1947
1948WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001949notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001950{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001951 struct weston_compositor *compositor = seat->compositor;
1952 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001953 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001954
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001955 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001956 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001957 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001958 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001959 WL_KEYBOARD_KEY_STATE_RELEASED);
1960 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001961
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001962 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001963
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001964 if (keyboard->focus) {
1965 seat->saved_kbd_focus = keyboard->focus;
1966 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01001967 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001968 wl_signal_add(&keyboard->focus->resource.destroy_signal,
1969 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001970 }
1971
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001972 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001973 /* FIXME: We really need keyboard grab cancel here to
1974 * let the grab shut down properly. As it is we leak
1975 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001976 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001977}
1978
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001979static void
Daniel Stone37816df2012-05-16 18:45:18 +01001980touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001981{
Daniel Stone37816df2012-05-16 18:45:18 +01001982 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001983 struct wl_resource *resource;
1984
Pekka Paalanen56464252012-07-31 13:21:08 +03001985 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001986 return;
1987
Pekka Paalanen56464252012-07-31 13:21:08 +03001988 if (seat->touch->focus_resource)
1989 wl_list_remove(&seat->touch->focus_listener.link);
1990 seat->touch->focus = NULL;
1991 seat->touch->focus_resource = NULL;
1992
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001993 if (surface) {
1994 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001995 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04001996 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001997 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02001998 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001999 return;
2000 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002001
Daniel Stone37816df2012-05-16 18:45:18 +01002002 seat->touch->focus = surface;
2003 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002004 wl_signal_add(&resource->destroy_signal,
2005 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002006 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002007}
2008
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002009/**
2010 * notify_touch - emulates button touches and notifies surfaces accordingly.
2011 *
2012 * It assumes always the correct cycle sequence until it gets here: touch_down
2013 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2014 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002015 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002016 */
2017WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002018notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002019 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002020{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002021 struct weston_compositor *ec = seat->compositor;
2022 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002023 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002024 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002025 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002026
2027 /* Update grab's global coordinates. */
2028 touch->grab_x = x;
2029 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002030
2031 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002032 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002033 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002034
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002035 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002036
2037 /* the first finger down picks the surface, and all further go
2038 * to that surface for the remainder of the touch session i.e.
2039 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002040 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002041 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002042 touch_set_focus(seat, &es->surface);
2043 } else if (touch->focus) {
2044 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002045 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Rob Bradford26e009c2012-12-05 18:47:05 +00002046 } else {
2047 /* Unexpected condition: We have non-initial touch but
2048 * there is no focused surface.
2049 */
2050 weston_log("touch event received with %d points down"
2051 "but no surface focused\n", seat->num_tp);
2052 return;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002053 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002054
Matt Roper47c1b982012-10-10 16:56:53 -07002055 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002056 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002057 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002058 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002059 if (!es)
2060 break;
2061
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002062 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002063 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002064 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002065 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002066 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002067 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002068
Matt Roper47c1b982012-10-10 16:56:53 -07002069 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002070 if (seat->num_tp == 0)
2071 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002072 break;
2073 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002074}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002075
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002076static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002077pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2078{
2079 struct weston_seat *seat = container_of(listener, struct weston_seat,
2080 sprite_destroy_listener);
2081
2082 seat->sprite = NULL;
2083}
2084
2085static void
2086pointer_cursor_surface_configure(struct weston_surface *es,
2087 int32_t dx, int32_t dy)
2088{
2089 struct weston_seat *seat = es->private;
2090 int x, y;
2091
2092 assert(es == seat->sprite);
2093
2094 seat->hotspot_x -= dx;
2095 seat->hotspot_y -= dy;
2096
2097 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2098 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2099
2100 weston_surface_configure(seat->sprite, x, y,
Pekka Paalanende685b82012-12-04 15:58:12 +02002101 es->buffer_ref.buffer->width,
2102 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002103
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002104 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002105
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002106 if (!weston_surface_is_mapped(es)) {
2107 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2108 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002109 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002110 }
2111}
2112
2113static void
2114pointer_unmap_sprite(struct weston_seat *seat)
2115{
2116 if (weston_surface_is_mapped(seat->sprite))
2117 weston_surface_unmap(seat->sprite);
2118
2119 wl_list_remove(&seat->sprite_destroy_listener.link);
2120 seat->sprite->configure = NULL;
2121 seat->sprite->private = NULL;
2122 seat->sprite = NULL;
2123}
2124
2125static void
2126pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2127 uint32_t serial, struct wl_resource *surface_resource,
2128 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002129{
Daniel Stone37816df2012-05-16 18:45:18 +01002130 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002131 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002132
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002133 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002134 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002135
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002136 if (seat->seat.pointer->focus == NULL)
2137 return;
2138 if (seat->seat.pointer->focus->resource.client != client)
2139 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002140 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002141 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002142
2143 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002144 if (surface->configure) {
2145 wl_resource_post_error(&surface->surface.resource,
2146 WL_DISPLAY_ERROR_INVALID_OBJECT,
2147 "surface->configure already "
2148 "set");
2149 return;
2150 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002151 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002152
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002153 if (seat->sprite)
2154 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002155
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002156 if (!surface)
2157 return;
2158
2159 wl_signal_add(&surface->surface.resource.destroy_signal,
2160 &seat->sprite_destroy_listener);
2161
2162 surface->configure = pointer_cursor_surface_configure;
2163 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002164 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002165 seat->hotspot_x = x;
2166 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002167
Pekka Paalanende685b82012-12-04 15:58:12 +02002168 if (surface->buffer_ref.buffer)
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002169 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002170}
2171
Daniel Stone37816df2012-05-16 18:45:18 +01002172static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002173 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002174};
2175
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002176static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002177handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002178{
Daniel Stone37816df2012-05-16 18:45:18 +01002179 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002180
Daniel Stone37816df2012-05-16 18:45:18 +01002181 seat = container_of(listener, struct weston_seat,
2182 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002183
Daniel Stone37816df2012-05-16 18:45:18 +01002184 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002185}
2186
Casey Dahlin9074db52012-04-19 22:50:09 -04002187static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002188{
2189 wl_list_remove(&resource->link);
2190 free(resource);
2191}
2192
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002193static void
Daniel Stone37816df2012-05-16 18:45:18 +01002194seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2195 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002196{
Daniel Stone37816df2012-05-16 18:45:18 +01002197 struct weston_seat *seat = resource->data;
2198 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002199
Daniel Stone37816df2012-05-16 18:45:18 +01002200 if (!seat->seat.pointer)
2201 return;
2202
2203 cr = wl_client_add_object(client, &wl_pointer_interface,
2204 &pointer_interface, id, seat);
2205 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002206 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002207
2208 if (seat->seat.pointer->focus &&
2209 seat->seat.pointer->focus->resource.client == client) {
2210 struct weston_surface *surface;
2211 wl_fixed_t sx, sy;
2212
2213 surface = (struct weston_surface *) seat->seat.pointer->focus;
2214 weston_surface_from_global_fixed(surface,
2215 seat->seat.pointer->x,
2216 seat->seat.pointer->y,
2217 &sx,
2218 &sy);
2219 wl_pointer_set_focus(seat->seat.pointer,
2220 seat->seat.pointer->focus,
2221 sx,
2222 sy);
2223 }
Daniel Stone37816df2012-05-16 18:45:18 +01002224}
2225
2226static void
2227seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2228 uint32_t id)
2229{
2230 struct weston_seat *seat = resource->data;
2231 struct wl_resource *cr;
2232
2233 if (!seat->seat.keyboard)
2234 return;
2235
2236 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2237 seat);
2238 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002239 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002240
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002241 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2242 seat->xkb_info.keymap_fd,
2243 seat->xkb_info.keymap_size);
2244
Daniel Stone17b13bd2012-05-30 16:31:39 +01002245 if (seat->seat.keyboard->focus &&
2246 seat->seat.keyboard->focus->resource.client == client) {
2247 wl_keyboard_set_focus(seat->seat.keyboard,
2248 seat->seat.keyboard->focus);
2249 }
Daniel Stone37816df2012-05-16 18:45:18 +01002250}
2251
2252static void
2253seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2254 uint32_t id)
2255{
2256 struct weston_seat *seat = resource->data;
2257 struct wl_resource *cr;
2258
2259 if (!seat->seat.touch)
2260 return;
2261
2262 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2263 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002264 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002265}
2266
2267static const struct wl_seat_interface seat_interface = {
2268 seat_get_pointer,
2269 seat_get_keyboard,
2270 seat_get_touch,
2271};
2272
2273static void
2274bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2275{
2276 struct wl_seat *seat = data;
2277 struct wl_resource *resource;
2278 enum wl_seat_capability caps = 0;
2279
2280 resource = wl_client_add_object(client, &wl_seat_interface,
2281 &seat_interface, id, data);
2282 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002283 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002284
2285 if (seat->pointer)
2286 caps |= WL_SEAT_CAPABILITY_POINTER;
2287 if (seat->keyboard)
2288 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2289 if (seat->touch)
2290 caps |= WL_SEAT_CAPABILITY_TOUCH;
2291
2292 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002293}
2294
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002295static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002296device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002297{
Daniel Stone37816df2012-05-16 18:45:18 +01002298 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002299
Daniel Stone37816df2012-05-16 18:45:18 +01002300 seat = container_of(listener, struct weston_seat,
2301 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002302
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002303 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002304}
2305
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002306static void weston_compositor_xkb_init(struct weston_compositor *ec,
2307 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002308{
Daniel Stonee379da92012-05-30 16:32:04 +01002309 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002310 ec->xkb_context = xkb_context_new(0);
2311 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002312 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002313 exit(1);
2314 }
Daniel Stone994679a2012-05-30 16:31:43 +01002315 }
2316
2317 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002318 ec->xkb_names = *names;
2319 if (!ec->xkb_names.rules)
2320 ec->xkb_names.rules = strdup("evdev");
2321 if (!ec->xkb_names.model)
2322 ec->xkb_names.model = strdup("pc105");
2323 if (!ec->xkb_names.layout)
2324 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002325}
2326
Daniel Stonee379da92012-05-30 16:32:04 +01002327static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2328{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002329 if (xkb_info->keymap)
2330 xkb_map_unref(xkb_info->keymap);
2331
2332 if (xkb_info->keymap_area)
2333 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2334 if (xkb_info->keymap_fd >= 0)
2335 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002336}
2337
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002338static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2339{
Daniel Stonee379da92012-05-30 16:32:04 +01002340 free((char *) ec->xkb_names.rules);
2341 free((char *) ec->xkb_names.model);
2342 free((char *) ec->xkb_names.layout);
2343 free((char *) ec->xkb_names.variant);
2344 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002345
Daniel Stonee379da92012-05-30 16:32:04 +01002346 xkb_info_destroy(&ec->xkb_info);
2347 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002348}
2349
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002350static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002351weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002352{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002353 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002354
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002355 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2356 XKB_MOD_NAME_SHIFT);
2357 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2358 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002359 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2360 XKB_MOD_NAME_CTRL);
2361 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2362 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002363 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2364 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002365 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2366 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002367 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002368
2369 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2370 XKB_LED_NAME_NUM);
2371 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2372 XKB_LED_NAME_CAPS);
2373 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2374 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002375
2376 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2377 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002378 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002379 exit(EXIT_FAILURE);
2380 }
2381 xkb_info->keymap_size = strlen(keymap_str) + 1;
2382
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002383 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002384 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002385 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002386 (unsigned long) xkb_info->keymap_size);
2387 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002388 }
2389
2390 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2391 PROT_READ | PROT_WRITE,
2392 MAP_SHARED, xkb_info->keymap_fd, 0);
2393 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002394 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002395 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002396 goto err_dev_zero;
2397 }
2398 strcpy(xkb_info->keymap_area, keymap_str);
2399 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002400
2401 return;
2402
2403err_dev_zero:
2404 close(xkb_info->keymap_fd);
2405 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002406err_keymap_str:
2407 free(keymap_str);
2408 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002409}
2410
2411static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002412weston_compositor_build_global_keymap(struct weston_compositor *ec)
2413{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002414 if (ec->xkb_info.keymap != NULL)
2415 return;
2416
Daniel Stonee379da92012-05-30 16:32:04 +01002417 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002418 &ec->xkb_names,
2419 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002420 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002421 weston_log("failed to compile global XKB keymap\n");
2422 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002423 "options %s",
2424 ec->xkb_names.rules, ec->xkb_names.model,
2425 ec->xkb_names.layout, ec->xkb_names.variant,
2426 ec->xkb_names.options);
2427 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002428 }
2429
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002430 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002431}
2432
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002433WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002434weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002435{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002436 if (seat->has_keyboard)
2437 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002438
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002439 if (keymap != NULL) {
2440 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002441 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002442 }
2443 else {
2444 weston_compositor_build_global_keymap(seat->compositor);
2445 seat->xkb_info = seat->compositor->xkb_info;
2446 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2447 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002448
2449 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2450 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002451 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002452 exit(1);
2453 }
2454
Daniel Stone71c38772012-06-22 13:21:30 +01002455 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002456
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002457 wl_keyboard_init(&seat->keyboard);
2458 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2459
2460 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002461}
2462
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002463WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002464weston_seat_init_pointer(struct weston_seat *seat)
2465{
2466 if (seat->has_pointer)
2467 return;
2468
2469 wl_pointer_init(&seat->pointer);
2470 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2471
2472 seat->has_pointer = 1;
2473}
2474
2475WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002476weston_seat_init_touch(struct weston_seat *seat)
2477{
2478 if (seat->has_touch)
2479 return;
2480
2481 wl_touch_init(&seat->touch);
2482 wl_seat_set_touch(&seat->seat, &seat->touch);
2483
2484 seat->has_touch = 1;
2485}
2486
2487WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002488weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002489{
Daniel Stone37816df2012-05-16 18:45:18 +01002490 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002491 seat->has_pointer = 0;
2492 seat->has_keyboard = 0;
2493 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002494
Daniel Stone37816df2012-05-16 18:45:18 +01002495 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2496 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002497
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002498 seat->sprite = NULL;
2499 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002500
Daniel Stone37816df2012-05-16 18:45:18 +01002501 seat->compositor = ec;
2502 seat->hotspot_x = 16;
2503 seat->hotspot_y = 16;
2504 seat->modifier_state = 0;
2505 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002506
Daniel Stone37816df2012-05-16 18:45:18 +01002507 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002508 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002509
Daniel Stone37816df2012-05-16 18:45:18 +01002510 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002511
Daniel Stone37816df2012-05-16 18:45:18 +01002512 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2513 wl_signal_add(&seat->seat.drag_icon_signal,
2514 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002515
2516 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002517 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002518}
2519
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002520WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002521weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002522{
Daniel Stone37816df2012-05-16 18:45:18 +01002523 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002524 /* The global object is destroyed at wl_display_destroy() time. */
2525
Daniel Stone37816df2012-05-16 18:45:18 +01002526 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002527 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002528
Daniel Stone74419a22012-05-30 16:32:02 +01002529 if (seat->xkb_state.state != NULL)
2530 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002531 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002532
Daniel Stone37816df2012-05-16 18:45:18 +01002533 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002534}
2535
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002536static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002537drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2538{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002539 empty_region(&es->pending.input);
2540
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002541 weston_surface_configure(es,
2542 es->geometry.x + sx, es->geometry.y + sy,
Pekka Paalanende685b82012-12-04 15:58:12 +02002543 es->buffer_ref.buffer->width,
2544 es->buffer_ref.buffer->height);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002545}
2546
2547static int
Daniel Stone37816df2012-05-16 18:45:18 +01002548device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002549 struct weston_surface *surface)
2550{
Daniel Stone37816df2012-05-16 18:45:18 +01002551 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002552
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002553 if (surface->configure) {
2554 wl_resource_post_error(&surface->surface.resource,
2555 WL_DISPLAY_ERROR_INVALID_OBJECT,
2556 "surface->configure already set");
2557 return 0;
2558 }
2559
Daniel Stone37816df2012-05-16 18:45:18 +01002560 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002561
Daniel Stone37816df2012-05-16 18:45:18 +01002562 weston_surface_set_position(ws->drag_surface,
2563 wl_fixed_to_double(seat->pointer->x),
2564 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002565
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002566 surface->configure = drag_surface_configure;
2567
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002568 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002569 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002570
2571 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002572}
2573
2574static void
Daniel Stone37816df2012-05-16 18:45:18 +01002575device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002576{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002577 if (weston_surface_is_mapped(seat->drag_surface))
2578 weston_surface_unmap(seat->drag_surface);
2579
Daniel Stone37816df2012-05-16 18:45:18 +01002580 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002581 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002582 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2583 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002584}
2585
2586static void
Daniel Stone37816df2012-05-16 18:45:18 +01002587device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002588{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002589 struct wl_list *list;
2590
Daniel Stone37816df2012-05-16 18:45:18 +01002591 if (weston_surface_is_mapped(seat->drag_surface) ||
Pekka Paalanende685b82012-12-04 15:58:12 +02002592 !seat->drag_surface->buffer_ref.buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002593 return;
2594
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002595 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2596 list = &seat->sprite->layer_link;
2597 else
2598 list = &seat->compositor->cursor_layer.surface_list;
2599
2600 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002601 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002602 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002603}
2604
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002605static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002606weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002607 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002608{
2609 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002610
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002611 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002612 return;
2613
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002614 if (seat->drag_surface && seat->seat.drag_surface &&
2615 (&seat->drag_surface->surface.resource !=
2616 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002617 /* between calls to this funcion we got a new drag_surface */
2618 surface_changed = 1;
2619
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002620 if (!seat->seat.drag_surface || surface_changed) {
2621 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002622 if (!surface_changed)
2623 return;
2624 }
2625
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002626 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002627 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002628 seat->seat.drag_surface;
2629 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002630 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002631 }
2632
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002633 /* the client may not have attached a buffer to the drag surface
2634 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002635 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002636
2637 if (!dx && !dy)
2638 return;
2639
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002640 weston_surface_set_position(seat->drag_surface,
2641 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2642 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002643}
2644
2645WL_EXPORT void
2646weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2647{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002648 struct weston_seat *seat;
2649
2650 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002651 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002652}
2653
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002654static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002655bind_output(struct wl_client *client,
2656 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002657{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002658 struct weston_output *output = data;
2659 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002660 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002661
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002662 resource = wl_client_add_object(client,
2663 &wl_output_interface, NULL, id, data);
2664
Casey Dahlin9074db52012-04-19 22:50:09 -04002665 wl_list_insert(&output->resource_list, &resource->link);
2666 resource->destroy = unbind_resource;
2667
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002668 wl_output_send_geometry(resource,
2669 output->x,
2670 output->y,
2671 output->mm_width,
2672 output->mm_height,
2673 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002674 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002675 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002676
2677 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002678 wl_output_send_mode(resource,
2679 mode->flags,
2680 mode->width,
2681 mode->height,
2682 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002683 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002684}
2685
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002686WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002687weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002688{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002689 struct weston_compositor *c = output->compositor;
2690
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002691 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002692 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002693 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002694
2695 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002696}
2697
Scott Moreau1bad5db2012-08-18 01:04:05 -06002698static void
2699weston_output_compute_transform(struct weston_output *output)
2700{
2701 struct weston_matrix transform;
2702 int flip;
2703
2704 weston_matrix_init(&transform);
2705
2706 switch(output->transform) {
2707 case WL_OUTPUT_TRANSFORM_FLIPPED:
2708 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2709 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2710 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2711 flip = -1;
2712 break;
2713 default:
2714 flip = 1;
2715 break;
2716 }
2717
2718 switch(output->transform) {
2719 case WL_OUTPUT_TRANSFORM_NORMAL:
2720 case WL_OUTPUT_TRANSFORM_FLIPPED:
2721 transform.d[0] = flip;
2722 transform.d[1] = 0;
2723 transform.d[4] = 0;
2724 transform.d[5] = 1;
2725 break;
2726 case WL_OUTPUT_TRANSFORM_90:
2727 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2728 transform.d[0] = 0;
2729 transform.d[1] = -flip;
2730 transform.d[4] = 1;
2731 transform.d[5] = 0;
2732 break;
2733 case WL_OUTPUT_TRANSFORM_180:
2734 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2735 transform.d[0] = -flip;
2736 transform.d[1] = 0;
2737 transform.d[4] = 0;
2738 transform.d[5] = -1;
2739 break;
2740 case WL_OUTPUT_TRANSFORM_270:
2741 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2742 transform.d[0] = 0;
2743 transform.d[1] = flip;
2744 transform.d[4] = -1;
2745 transform.d[5] = 0;
2746 break;
2747 default:
2748 break;
2749 }
2750
2751 weston_matrix_multiply(&output->matrix, &transform);
2752}
2753
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002754WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002755weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002756{
Scott Moreau850ca422012-05-21 15:21:25 -06002757 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002758 struct weston_matrix camera;
2759 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002760
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002761 weston_matrix_init(&output->matrix);
2762 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002763 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2764 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002765
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002766 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002767 2.0 / (output->width + output->border.left + output->border.right),
2768 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2769
2770 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002771
Scott Moreauccbf29d2012-02-22 14:21:41 -07002772 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002773 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002774 weston_matrix_init(&camera);
2775 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002776 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002777 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002778 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002779 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002780 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002781 weston_matrix_multiply(&output->matrix, &modelview);
2782 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002783
Scott Moreauccbf29d2012-02-22 14:21:41 -07002784 output->dirty = 0;
2785}
2786
Scott Moreau1bad5db2012-08-18 01:04:05 -06002787static void
2788weston_output_transform_init(struct weston_output *output, uint32_t transform)
2789{
2790 output->transform = transform;
2791
2792 switch (transform) {
2793 case WL_OUTPUT_TRANSFORM_90:
2794 case WL_OUTPUT_TRANSFORM_270:
2795 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2796 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2797 /* Swap width and height */
2798 output->width = output->current->height;
2799 output->height = output->current->width;
2800 break;
2801 case WL_OUTPUT_TRANSFORM_NORMAL:
2802 case WL_OUTPUT_TRANSFORM_180:
2803 case WL_OUTPUT_TRANSFORM_FLIPPED:
2804 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2805 output->width = output->current->width;
2806 output->height = output->current->height;
2807 break;
2808 default:
2809 break;
2810 }
2811}
2812
Scott Moreauccbf29d2012-02-22 14:21:41 -07002813WL_EXPORT void
2814weston_output_move(struct weston_output *output, int x, int y)
2815{
2816 output->x = x;
2817 output->y = y;
2818
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002819 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002820 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002821 output->width,
2822 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002823}
2824
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002825WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002826weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002827 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002828{
2829 output->compositor = c;
2830 output->x = x;
2831 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002832 output->border.top = 0;
2833 output->border.bottom = 0;
2834 output->border.left = 0;
2835 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002836 output->mm_width = width;
2837 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002838 output->dirty = 1;
2839
Scott Moreau1bad5db2012-08-18 01:04:05 -06002840 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002841 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002842
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002843 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002844 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002845
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002846 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002847 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002848 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002849
Casey Dahlin58ba1372012-04-19 22:50:08 -04002850 output->id = ffs(~output->compositor->output_id_pool) - 1;
2851 output->compositor->output_id_pool |= 1 << output->id;
2852
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002853 output->global =
2854 wl_display_add_global(c->wl_display, &wl_output_interface,
2855 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002856}
2857
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002858static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002859compositor_bind(struct wl_client *client,
2860 void *data, uint32_t version, uint32_t id)
2861{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002862 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002863
2864 wl_client_add_object(client, &wl_compositor_interface,
2865 &compositor_interface, id, compositor);
2866}
2867
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002868static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002869log_uname(void)
2870{
2871 struct utsname usys;
2872
2873 uname(&usys);
2874
2875 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2876 usys.version, usys.machine);
2877}
2878
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002879WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002880weston_compositor_init(struct weston_compositor *ec,
2881 struct wl_display *display,
2882 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002883 char *argv[],
2884 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002885{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002886 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002887 struct xkb_rule_names xkb_names;
2888 const struct config_key keyboard_config_keys[] = {
2889 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2890 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2891 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2892 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2893 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2894 };
2895 const struct config_section cs[] = {
2896 { "keyboard",
2897 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2898 };
2899
2900 memset(&xkb_names, 0, sizeof(xkb_names));
2901 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002902
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002903 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002904 wl_signal_init(&ec->destroy_signal);
2905 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002906 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002907 wl_signal_init(&ec->lock_signal);
2908 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002909 wl_signal_init(&ec->show_input_panel_signal);
2910 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002911 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002912 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002913
Casey Dahlin58ba1372012-04-19 22:50:08 -04002914 ec->output_id_pool = 0;
2915
Kristian Høgsberga8873122011-11-23 10:39:34 -05002916 if (!wl_display_add_global(display, &wl_compositor_interface,
2917 ec, compositor_bind))
2918 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002919
Daniel Stone725c2c32012-06-22 14:04:36 +01002920 wl_list_init(&ec->surface_list);
2921 wl_list_init(&ec->layer_list);
2922 wl_list_init(&ec->seat_list);
2923 wl_list_init(&ec->output_list);
2924 wl_list_init(&ec->key_binding_list);
2925 wl_list_init(&ec->button_binding_list);
2926 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002927 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002928 wl_list_init(&ec->fade.animation.link);
2929
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002930 weston_plane_init(&ec->primary_plane, 0, 0);
2931
Daniel Stone725c2c32012-06-22 14:04:36 +01002932 weston_compositor_xkb_init(ec, &xkb_names);
2933
2934 ec->ping_handler = NULL;
2935
2936 screenshooter_create(ec);
2937 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002938 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002939
2940 wl_data_device_manager_init(ec->wl_display);
2941
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002942 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002943
Daniel Stone725c2c32012-06-22 14:04:36 +01002944 loop = wl_display_get_event_loop(ec->wl_display);
2945 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2946 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2947
2948 ec->input_loop = wl_event_loop_create();
2949
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002950 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2951 ec->fade.animation.frame = fade_frame;
2952
2953 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2954 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2955
2956 weston_compositor_schedule_repaint(ec);
2957
Daniel Stone725c2c32012-06-22 14:04:36 +01002958 return 0;
2959}
2960
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002961WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002962weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002963{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002964 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002965
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002966 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002967 if (ec->input_loop_source)
2968 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002969
Matt Roper361d2ad2011-08-29 13:52:23 -07002970 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02002971 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07002972 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02002973
Daniel Stone325fc2d2012-05-30 16:31:58 +01002974 weston_binding_list_destroy_all(&ec->key_binding_list);
2975 weston_binding_list_destroy_all(&ec->button_binding_list);
2976 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002977 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002978
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002979 weston_plane_release(&ec->primary_plane);
2980
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002981 wl_array_release(&ec->vertices);
2982 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05002983 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002984
2985 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07002986}
2987
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002988static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002989{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002990 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002991
Martin Minarik6d118362012-06-07 18:01:59 +02002992 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04002993 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04002994
2995 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05002996}
2997
Kristian Høgsberg0690da62012-01-16 11:53:54 -05002998static void
2999on_segv_signal(int s, siginfo_t *siginfo, void *context)
3000{
3001 void *buffer[32];
3002 int i, count;
3003 Dl_info info;
3004
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003005 /* This SIGSEGV handler will do a best-effort backtrace, and
3006 * then call the backend restore function, which will switch
3007 * back to the vt we launched from or ungrab X etc and then
3008 * raise SIGTRAP. If we run weston under gdb from X or a
3009 * different vt, and tell gdb "handle SIGSEGV nostop", this
3010 * will allow weston to switch back to gdb on crash and then
3011 * gdb will catch the crash with SIGTRAP. */
3012
Martin Minarik6d118362012-06-07 18:01:59 +02003013 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003014
3015 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3016 for (i = 0; i < count; i++) {
3017 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003018 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003019 (long) buffer[i],
3020 info.dli_sname ? info.dli_sname : "--",
3021 info.dli_fname);
3022 }
3023
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003024 segv_compositor->restore(segv_compositor);
3025
3026 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003027}
3028
3029
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003030static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003031load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003032{
3033 char path[PATH_MAX];
3034 void *module, *init;
3035
3036 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003037 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003038 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003039 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003040
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003041 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3042 if (module) {
3043 weston_log("Module '%s' already loaded\n", path);
3044 dlclose(module);
3045 return NULL;
3046 }
3047
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003048 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003049 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003050 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003051 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003052 return NULL;
3053 }
3054
3055 init = dlsym(module, entrypoint);
3056 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003057 weston_log("Failed to lookup init function: %s\n", dlerror());
Rob Bradfordc9e64ab2012-12-05 18:47:10 +00003058 dlclose(module);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003059 return NULL;
3060 }
3061
3062 return init;
3063}
3064
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003065static int
3066load_modules(struct weston_compositor *ec, const char *modules)
3067{
3068 const char *p, *end;
3069 char buffer[256];
3070 int (*module_init)(struct weston_compositor *ec);
3071
3072 if (modules == NULL)
3073 return 0;
3074
3075 p = modules;
3076 while (*p) {
3077 end = strchrnul(p, ',');
3078 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3079 module_init = load_module(buffer, "module_init");
3080 if (module_init)
3081 module_init(ec);
3082 p = end;
3083 while (*p == ',')
3084 p++;
3085
3086 }
3087
3088 return 0;
3089}
3090
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003091static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003092 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3093
3094static const char xdg_wrong_message[] =
3095 "fatal: environment variable XDG_RUNTIME_DIR\n"
3096 "is set to \"%s\", which is not a directory.\n";
3097
3098static const char xdg_wrong_mode_message[] =
3099 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3100 "correctly. Unix access mode must be 0700 but is %o,\n"
3101 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003102 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003103
3104static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003105 "Refer to your distribution on how to get it, or\n"
3106 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3107 "on how to implement it.\n";
3108
Martin Minarik37032f82012-06-18 20:15:18 +02003109static void
3110verify_xdg_runtime_dir(void)
3111{
3112 char *dir = getenv("XDG_RUNTIME_DIR");
3113 struct stat s;
3114
3115 if (!dir) {
3116 weston_log(xdg_error_message);
3117 weston_log_continue(xdg_detail_message);
3118 exit(EXIT_FAILURE);
3119 }
3120
3121 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3122 weston_log(xdg_wrong_message, dir);
3123 weston_log_continue(xdg_detail_message);
3124 exit(EXIT_FAILURE);
3125 }
3126
3127 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3128 weston_log(xdg_wrong_mode_message,
3129 dir, s.st_mode & 0777, s.st_uid);
3130 weston_log_continue(xdg_detail_message);
3131 }
3132}
3133
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003134static int
3135usage(int error_code)
3136{
3137 fprintf(stderr,
3138 "Usage: weston [OPTIONS]\n\n"
3139 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3140 "Weston supports multiple backends, and depending on which backend is in use\n"
3141 "different options will be accepted.\n\n"
3142
3143
3144 "Core options:\n\n"
3145 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3146 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3147 " -S, --socket=NAME\tName of socket to listen on\n"
3148 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003149 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003150 " --log==FILE\t\tLog to the given file\n"
3151 " -h, --help\t\tThis help message\n\n");
3152
3153 fprintf(stderr,
3154 "Options for drm-backend.so:\n\n"
3155 " --connector=ID\tBring up only this connector\n"
3156 " --seat=SEAT\t\tThe seat that weston should run on\n"
3157 " --tty=TTY\t\tThe tty to use\n"
3158 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3159
3160 fprintf(stderr,
3161 "Options for x11-backend.so:\n\n"
3162 " --width=WIDTH\t\tWidth of X window\n"
3163 " --height=HEIGHT\tHeight of X window\n"
3164 " --fullscreen\t\tRun in fullscreen mode\n"
3165 " --output-count=COUNT\tCreate multiple outputs\n"
3166 " --no-input\t\tDont create input devices\n\n");
3167
3168 fprintf(stderr,
3169 "Options for wayland-backend.so:\n\n"
3170 " --width=WIDTH\t\tWidth of Wayland surface\n"
3171 " --height=HEIGHT\tHeight of Wayland surface\n"
3172 " --display=DISPLAY\tWayland display to connect to\n\n");
3173
3174 exit(error_code);
3175}
3176
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003177int main(int argc, char *argv[])
3178{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003179 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003180 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003181 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003182 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003183 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003184 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003185 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003186 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003187 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003188 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003189 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003190 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003191 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003192 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003193 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003194 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003195 char *config_file;
3196
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003197 const struct config_key core_config_keys[] = {
3198 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003199 };
3200
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003201 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003202 { "core",
3203 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003204 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003205
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003206 const struct weston_option core_options[] = {
3207 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3208 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3209 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003210 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003211 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003212 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003213 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003214
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003215 argc = parse_options(core_options,
3216 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003217
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003218 if (help)
3219 usage(EXIT_SUCCESS);
3220
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003221 weston_log_file_open(log);
3222
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003223 weston_log("%s\n"
3224 STAMP_SPACE "%s\n"
3225 STAMP_SPACE "Bug reports to: %s\n"
3226 STAMP_SPACE "Build: %s\n",
3227 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003228 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003229 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003230
Martin Minarik37032f82012-06-18 20:15:18 +02003231 verify_xdg_runtime_dir();
3232
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003233 display = wl_display_create();
3234
Tiago Vignatti2116b892011-08-08 05:52:59 -07003235 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003236 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3237 display);
3238 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3239 display);
3240 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3241 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003242
3243 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003244 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3245 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003246
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003247 if (!backend) {
3248 if (getenv("WAYLAND_DISPLAY"))
3249 backend = "wayland-backend.so";
3250 else if (getenv("DISPLAY"))
3251 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003252 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003253 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003254 }
3255
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003256 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003257 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003258
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003259 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003260 if (!backend_init)
3261 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003262
Daniel Stonec1be8e52012-06-01 11:14:02 -04003263 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003264 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003265 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003266 exit(EXIT_FAILURE);
3267 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003268
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003269 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3270 segv_action.sa_sigaction = on_segv_signal;
3271 sigemptyset(&segv_action.sa_mask);
3272 sigaction(SIGSEGV, &segv_action, NULL);
3273 segv_compositor = ec;
3274
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003275 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003276 weston_log("fatal: unhandled option: %s\n", argv[i]);
3277 if (argv[1]) {
3278 ret = EXIT_FAILURE;
3279 goto out;
3280 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003281
Daniel Stonec1be8e52012-06-01 11:14:02 -04003282 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003283
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003284 ec->option_idle_time = idle_time;
3285 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003286
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003287 setenv("WAYLAND_DISPLAY", socket_name, 1);
3288
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003289 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003290 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003291 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003292 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003293
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003294 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003295 weston_log("fatal: failed to add socket: %m\n");
3296 ret = EXIT_FAILURE;
3297 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003298 }
3299
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003300 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003301 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003302
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003303 wl_display_run(display);
3304
3305 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003306 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003307 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003308
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003309 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003310
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003311 for (i = ARRAY_LENGTH(signals); i;)
3312 wl_event_source_remove(signals[--i]);
3313
Daniel Stone855028f2012-05-01 20:37:10 +01003314 weston_compositor_xkb_destroy(ec);
3315
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003316 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003317 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003318
Martin Minarik19e6f262012-06-07 13:08:46 +02003319 weston_log_file_close();
3320
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003321 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003322}