blob: 88a37f7a6ed79300c24f7a54b799c38f7f178252 [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
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400184surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200185{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500186 struct weston_surface *es =
187 container_of(listener, struct weston_surface,
188 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200189
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400190 if (es->buffer && wl_buffer_is_shm(es->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400191 es->compositor->renderer->flush_damage(es);
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400192
Kristian Høgsberg24596942011-10-24 17:51:02 -0400193 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200194}
195
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300196static void
197surface_handle_pending_buffer_destroy(struct wl_listener *listener, void *data)
198{
199 struct weston_surface *surface =
200 container_of(listener, struct weston_surface,
201 pending.buffer_destroy_listener);
202
203 surface->pending.buffer = NULL;
204}
205
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200206static void
207empty_region(pixman_region32_t *region)
208{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300209 pixman_region32_fini(region);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200210 pixman_region32_init(region);
211}
212
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300213static void
214region_init_infinite(pixman_region32_t *region)
215{
216 pixman_region32_init_rect(region, INT32_MIN, INT32_MIN,
217 UINT32_MAX, UINT32_MAX);
218}
219
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500220WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500221weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500222{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500223 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400224
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200225 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400226 if (surface == NULL)
227 return NULL;
228
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400229 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500230
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500231 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500232 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500233
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400234 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400235
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500236 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400237 surface->alpha = 1.0;
Juan Zhao46436612012-03-20 01:48:46 +0800238 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400239
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100240 if (compositor->renderer->create_surface(surface) < 0) {
241 free(surface);
242 return NULL;
243 }
244
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200245 pixman_region32_init(&surface->texture_damage);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200246
Benjamin Franzke06286262011-05-06 19:12:33 +0200247 surface->buffer = NULL;
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200248 surface->buffer_transform = WL_OUTPUT_TRANSFORM_NORMAL;
249 surface->pending.buffer_transform = surface->buffer_transform;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400250 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400251 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200252
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400253 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500254 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500255 pixman_region32_init(&surface->clip);
Pekka Paalanen8ec4ab62012-10-10 12:49:32 +0300256 region_init_infinite(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200257 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500258 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400259
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400260 surface->buffer_destroy_listener.notify =
261 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200262
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200263 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200264 wl_list_insert(&surface->geometry.transformation_list,
265 &surface->transform.position.link);
266 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200267 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200268 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500269
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300270 surface->pending.buffer_destroy_listener.notify =
271 surface_handle_pending_buffer_destroy;
Pekka Paalanen8e159182012-10-10 12:49:25 +0300272 pixman_region32_init(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300273 pixman_region32_init(&surface->pending.opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300274 region_init_infinite(&surface->pending.input);
Pekka Paalanenbc106382012-10-10 12:49:31 +0300275 wl_list_init(&surface->pending.frame_callback_list);
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300276
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400277 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500278}
279
Alex Wu8811bf92012-02-28 18:07:54 +0800280WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500281weston_surface_set_color(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200282 float red, float green, float blue, float alpha)
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500283{
John Kåre Alsaker878f4492012-11-13 19:10:23 +0100284 surface->compositor->renderer->surface_set_color(surface, red, green, blue, alpha);
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500285}
286
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400287WL_EXPORT void
288weston_surface_to_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200289 float sx, float sy, float *x, float *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200290{
291 if (surface->transform.enabled) {
292 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
293
294 weston_matrix_transform(&surface->transform.matrix, &v);
295
296 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200297 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200298 "weston_surface_to_global(), divisor = %g\n",
299 v.f[3]);
300 *x = 0;
301 *y = 0;
302 return;
303 }
304
305 *x = v.f[0] / v.f[3];
306 *y = v.f[1] / v.f[3];
307 } else {
308 *x = sx + surface->geometry.x;
309 *y = sy + surface->geometry.y;
310 }
311}
312
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500313WL_EXPORT void
Ander Conselvan de Oliveira0396ba22012-11-28 17:10:26 +0200314weston_surface_to_buffer_float(struct weston_surface *surface,
315 float sx, float sy, float *bx, float *by)
316{
317 switch (surface->buffer_transform) {
318 case WL_OUTPUT_TRANSFORM_NORMAL:
319 default:
320 *bx = sx;
321 *by = sy;
322 break;
323 case WL_OUTPUT_TRANSFORM_FLIPPED:
324 *bx = surface->geometry.width - sx;
325 *by = sy;
326 break;
327 case WL_OUTPUT_TRANSFORM_90:
328 *bx = surface->geometry.height - sy;
329 *by = sx;
330 break;
331 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
332 *bx = surface->geometry.height - sy;
333 *by = surface->geometry.width - sx;
334 break;
335 case WL_OUTPUT_TRANSFORM_180:
336 *bx = surface->geometry.width - sx;
337 *by = surface->geometry.height - sy;
338 break;
339 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
340 *bx = sx;
341 *by = surface->geometry.height - sy;
342 break;
343 case WL_OUTPUT_TRANSFORM_270:
344 *bx = sy;
345 *by = surface->geometry.width - sx;
346 break;
347 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
348 *bx = sy;
349 *by = sx;
350 break;
351 }
352}
353
354WL_EXPORT pixman_box32_t
355weston_surface_to_buffer_rect(struct weston_surface *surface,
356 pixman_box32_t rect)
357{
358 float x1, x2, y1, y2;
359
360 pixman_box32_t ret;
361
362 weston_surface_to_buffer_float(surface, rect.x1, rect.y1, &x1, &y1);
363 weston_surface_to_buffer_float(surface, rect.x2, rect.y2, &x2, &y2);
364
365 if (x1 <= x2) {
366 ret.x1 = x1;
367 ret.x2 = x2;
368 } else {
369 ret.x1 = x2;
370 ret.x2 = x1;
371 }
372
373 if (y1 <= y2) {
374 ret.y1 = y1;
375 ret.y2 = y2;
376 } else {
377 ret.y1 = y2;
378 ret.y2 = y1;
379 }
380
381 return ret;
382}
383
384WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400385weston_surface_move_to_plane(struct weston_surface *surface,
386 struct weston_plane *plane)
387{
388 if (surface->plane == plane)
389 return;
390
391 weston_surface_damage_below(surface);
392 surface->plane = plane;
393 weston_surface_damage(surface);
394}
395
396WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500397weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200398{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500399 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200400
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500401 pixman_region32_init(&damage);
402 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
403 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400404 pixman_region32_union(&surface->plane->damage,
405 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500406 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200407}
408
Kristian Høgsbergb9af4792012-09-25 14:48:04 -0400409static struct wl_resource *
410find_resource_for_client(struct wl_list *list, struct wl_client *client)
411{
412 struct wl_resource *r;
413
414 wl_list_for_each(r, list, link) {
415 if (r->client == client)
416 return r;
417 }
418
419 return NULL;
420}
421
422static void
423weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
424{
425 uint32_t different = es->output_mask ^ mask;
426 uint32_t entered = mask & different;
427 uint32_t left = es->output_mask & different;
428 struct weston_output *output;
429 struct wl_resource *resource = NULL;
430 struct wl_client *client = es->surface.resource.client;
431
432 es->output_mask = mask;
433 if (es->surface.resource.client == NULL)
434 return;
435 if (different == 0)
436 return;
437
438 wl_list_for_each(output, &es->compositor->output_list, link) {
439 if (1 << output->id & different)
440 resource =
441 find_resource_for_client(&output->resource_list,
442 client);
443 if (resource == NULL)
444 continue;
445 if (1 << output->id & entered)
446 wl_surface_send_enter(&es->surface.resource, resource);
447 if (1 << output->id & left)
448 wl_surface_send_leave(&es->surface.resource, resource);
449 }
450}
451
452static void
453weston_surface_assign_output(struct weston_surface *es)
454{
455 struct weston_compositor *ec = es->compositor;
456 struct weston_output *output, *new_output;
457 pixman_region32_t region;
458 uint32_t max, area, mask;
459 pixman_box32_t *e;
460
461 new_output = NULL;
462 max = 0;
463 mask = 0;
464 pixman_region32_init(&region);
465 wl_list_for_each(output, &ec->output_list, link) {
466 pixman_region32_intersect(&region, &es->transform.boundingbox,
467 &output->region);
468
469 e = pixman_region32_extents(&region);
470 area = (e->x2 - e->x1) * (e->y2 - e->y1);
471
472 if (area > 0)
473 mask |= 1 << output->id;
474
475 if (area >= max) {
476 new_output = output;
477 max = area;
478 }
479 }
480 pixman_region32_fini(&region);
481
482 es->output = new_output;
483 weston_surface_update_output_mask(es, mask);
484}
485
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200486static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200487surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
488 int32_t width, int32_t height,
489 pixman_region32_t *bbox)
490{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200491 float min_x = HUGE_VALF, min_y = HUGE_VALF;
492 float max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200493 int32_t s[4][2] = {
494 { sx, sy },
495 { sx, sy + height },
496 { sx + width, sy },
497 { sx + width, sy + height }
498 };
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200499 float int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200500 int i;
501
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300502 if (width == 0 || height == 0) {
503 /* avoid rounding empty bbox to 1x1 */
504 pixman_region32_init(bbox);
505 return;
506 }
507
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200508 for (i = 0; i < 4; ++i) {
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200509 float x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400510 weston_surface_to_global_float(surface,
511 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200512 if (x < min_x)
513 min_x = x;
514 if (x > max_x)
515 max_x = x;
516 if (y < min_y)
517 min_y = y;
518 if (y > max_y)
519 max_y = y;
520 }
521
Pekka Paalanen219b9822012-02-08 15:38:37 +0200522 int_x = floorf(min_x);
523 int_y = floorf(min_y);
524 pixman_region32_init_rect(bbox, int_x, int_y,
525 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200526}
527
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200528static void
529weston_surface_update_transform_disable(struct weston_surface *surface)
530{
531 surface->transform.enabled = 0;
532
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200533 /* round off fractions when not transformed */
534 surface->geometry.x = roundf(surface->geometry.x);
535 surface->geometry.y = roundf(surface->geometry.y);
536
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200537 pixman_region32_init_rect(&surface->transform.boundingbox,
538 surface->geometry.x,
539 surface->geometry.y,
540 surface->geometry.width,
541 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500542
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400543 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500544 pixman_region32_copy(&surface->transform.opaque,
545 &surface->opaque);
546 pixman_region32_translate(&surface->transform.opaque,
547 surface->geometry.x,
548 surface->geometry.y);
549 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200550}
551
552static int
553weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200554{
555 struct weston_matrix *matrix = &surface->transform.matrix;
556 struct weston_matrix *inverse = &surface->transform.inverse;
557 struct weston_transform *tform;
558
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200559 surface->transform.enabled = 1;
560
561 /* Otherwise identity matrix, but with x and y translation. */
562 surface->transform.position.matrix.d[12] = surface->geometry.x;
563 surface->transform.position.matrix.d[13] = surface->geometry.y;
564
565 weston_matrix_init(matrix);
566 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
567 weston_matrix_multiply(matrix, &tform->matrix);
568
569 if (weston_matrix_invert(inverse, matrix) < 0) {
570 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200571 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200572 " transformation not invertible.\n", surface);
573 return -1;
574 }
575
576 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
577 surface->geometry.height,
578 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500579
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200580 return 0;
581}
582
583WL_EXPORT void
584weston_surface_update_transform(struct weston_surface *surface)
585{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200586 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200587 return;
588
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200589 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200590
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500591 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200592
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200593 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500594 pixman_region32_fini(&surface->transform.opaque);
595 pixman_region32_init(&surface->transform.opaque);
596
Pekka Paalanencd403622012-01-25 13:37:39 +0200597 /* transform.position is always in transformation_list */
598 if (surface->geometry.transformation_list.next ==
599 &surface->transform.position.link &&
600 surface->geometry.transformation_list.prev ==
601 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200602 weston_surface_update_transform_disable(surface);
603 } else {
604 if (weston_surface_update_transform_enable(surface) < 0)
605 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200606 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200607
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400608 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200609
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +0300610 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200611}
612
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200613WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100614weston_surface_to_global_fixed(struct weston_surface *surface,
615 wl_fixed_t sx, wl_fixed_t sy,
616 wl_fixed_t *x, wl_fixed_t *y)
617{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200618 float xf, yf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100619
620 weston_surface_to_global_float(surface,
621 wl_fixed_to_double(sx),
622 wl_fixed_to_double(sy),
623 &xf, &yf);
624 *x = wl_fixed_from_double(xf);
625 *y = wl_fixed_from_double(yf);
626}
627
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400628WL_EXPORT void
629weston_surface_from_global_float(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200630 float x, float y, float *sx, float *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200631{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200632 if (surface->transform.enabled) {
633 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
634
635 weston_matrix_transform(&surface->transform.inverse, &v);
636
637 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200638 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200639 "weston_surface_from_global(), divisor = %g\n",
640 v.f[3]);
641 *sx = 0;
642 *sy = 0;
643 return;
644 }
645
Pekka Paalanencd403622012-01-25 13:37:39 +0200646 *sx = v.f[0] / v.f[3];
647 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200648 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200649 *sx = x - surface->geometry.x;
650 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200651 }
652}
653
654WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100655weston_surface_from_global_fixed(struct weston_surface *surface,
656 wl_fixed_t x, wl_fixed_t y,
657 wl_fixed_t *sx, wl_fixed_t *sy)
658{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200659 float sxf, syf;
Daniel Stonebd3489b2012-05-08 17:17:53 +0100660
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400661 weston_surface_from_global_float(surface,
662 wl_fixed_to_double(x),
663 wl_fixed_to_double(y),
664 &sxf, &syf);
Daniel Stonebd3489b2012-05-08 17:17:53 +0100665 *sx = wl_fixed_from_double(sxf);
666 *sy = wl_fixed_from_double(syf);
667}
668
669WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200670weston_surface_from_global(struct weston_surface *surface,
671 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
672{
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200673 float sxf, syf;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200674
Kristian Høgsbergecf6ede2012-09-05 21:59:35 -0400675 weston_surface_from_global_float(surface, x, y, &sxf, &syf);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200676 *sx = floorf(sxf);
677 *sy = floorf(syf);
678}
679
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400680WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400681weston_surface_schedule_repaint(struct weston_surface *surface)
682{
683 struct weston_output *output;
684
685 wl_list_for_each(output, &surface->compositor->output_list, link)
686 if (surface->output_mask & (1 << output->id))
687 weston_output_schedule_repaint(output);
688}
689
690WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500691weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500692{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400693 pixman_region32_union_rect(&surface->damage, &surface->damage,
694 0, 0, surface->geometry.width,
695 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200696
Kristian Høgsberg98238702012-08-03 16:29:12 -0400697 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500698}
699
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400700WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500701weston_surface_configure(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200702 float x, float y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400703{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200704 surface->geometry.x = x;
705 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200706 surface->geometry.width = width;
707 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200708 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400709}
710
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200711WL_EXPORT void
712weston_surface_set_position(struct weston_surface *surface,
John Kåre Alsaker490d02a2012-09-30 02:57:21 +0200713 float x, float y)
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200714{
715 surface->geometry.x = x;
716 surface->geometry.y = y;
717 surface->geometry.dirty = 1;
718}
719
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300720WL_EXPORT int
721weston_surface_is_mapped(struct weston_surface *surface)
722{
723 if (surface->output)
724 return 1;
725 else
726 return 0;
727}
728
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +0200729WL_EXPORT int32_t
730weston_surface_buffer_width(struct weston_surface *surface)
731{
732 switch (surface->buffer_transform) {
733 case WL_OUTPUT_TRANSFORM_90:
734 case WL_OUTPUT_TRANSFORM_270:
735 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
736 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
737 return surface->buffer->height;
738 default:
739 return surface->buffer->width;
740 }
741}
742
743WL_EXPORT int32_t
744weston_surface_buffer_height(struct weston_surface *surface)
745{
746 switch (surface->buffer_transform) {
747 case WL_OUTPUT_TRANSFORM_90:
748 case WL_OUTPUT_TRANSFORM_270:
749 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
750 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
751 return surface->buffer->width;
752 default:
753 return surface->buffer->height;
754 }
755}
756
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400757WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500758weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500759{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400760 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500761
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400762 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500763
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400764 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500765}
766
Tiago Vignatti9d393522012-02-10 16:26:19 +0200767static struct weston_surface *
768weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100769 wl_fixed_t x, wl_fixed_t y,
770 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200771{
772 struct weston_surface *surface;
773
774 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100775 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500776 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100777 wl_fixed_to_int(*sx),
778 wl_fixed_to_int(*sy),
779 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200780 return surface;
781 }
782
783 return NULL;
784}
785
786static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400787weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500788{
Scott Moreau447013d2012-02-18 05:05:29 -0700789 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500790 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400791 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500792
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400793 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100794 return;
795
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400796 surface = weston_compositor_pick_surface(seat->compositor,
797 pointer->x,
798 pointer->y,
799 &pointer->current_x,
800 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500801
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400802 if (&surface->surface != pointer->current) {
803 interface = pointer->grab->interface;
804 pointer->current = &surface->surface;
805 interface->focus(pointer->grab, &surface->surface,
806 pointer->current_x,
807 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500808 }
809
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400810 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500811 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100812 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400813 pointer->x,
814 pointer->y,
815 &pointer->grab->x,
816 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500817}
818
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400819static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500820weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400821{
Daniel Stone37816df2012-05-16 18:45:18 +0100822 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400823
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500824 if (!compositor->focus)
825 return;
826
Daniel Stone37816df2012-05-16 18:45:18 +0100827 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400828 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400829}
830
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400831WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500832weston_surface_unmap(struct weston_surface *surface)
833{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100834 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500835
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500836 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500837 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500838 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500839
Daniel Stone4dab5db2012-05-30 16:31:53 +0100840 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100841 if (seat->seat.keyboard &&
842 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100843 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100844 if (seat->seat.pointer &&
845 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100846 wl_pointer_set_focus(seat->seat.pointer,
847 NULL,
848 wl_fixed_from_int(0),
849 wl_fixed_from_int(0));
850 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500851
Kristian Høgsberg98238702012-08-03 16:29:12 -0400852 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500853}
854
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400855struct weston_frame_callback {
856 struct wl_resource resource;
857 struct wl_list link;
858};
859
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500860static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400861destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500862{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500863 struct weston_surface *surface =
864 container_of(resource,
865 struct weston_surface, surface.resource);
866 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400867 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400868
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300869 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500870 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400871
Pekka Paalanenbc106382012-10-10 12:49:31 +0300872 wl_list_for_each_safe(cb, next,
873 &surface->pending.frame_callback_list, link)
874 wl_resource_destroy(&cb->resource);
875
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300876 pixman_region32_fini(&surface->pending.input);
Pekka Paalanen512dde82012-10-10 12:49:27 +0300877 pixman_region32_fini(&surface->pending.opaque);
Pekka Paalanen8e159182012-10-10 12:49:25 +0300878 pixman_region32_fini(&surface->pending.damage);
879
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300880 if (surface->pending.buffer)
881 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
882
Benjamin Franzke06286262011-05-06 19:12:33 +0200883 if (surface->buffer)
884 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400885
Pekka Paalanenbcdd5792012-11-07 12:25:13 +0200886 pixman_region32_fini(&surface->texture_damage);
Kristian Høgsberg42263852012-09-06 21:59:29 -0400887 compositor->renderer->destroy_surface(surface);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200888
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200889 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200890 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500891 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500892 pixman_region32_fini(&surface->clip);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300893 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200894
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400895 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
896 wl_resource_destroy(&cb->resource);
897
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400898 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500899}
900
Alex Wu8811bf92012-02-28 18:07:54 +0800901WL_EXPORT void
902weston_surface_destroy(struct weston_surface *surface)
903{
904 /* Not a valid way to destroy a client surface */
905 assert(surface->surface.resource.client == NULL);
906
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400907 wl_signal_emit(&surface->surface.resource.destroy_signal,
908 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800909 destroy_surface(&surface->surface.resource);
910}
911
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100912static void
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300913weston_surface_attach(struct weston_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100914{
Pekka Paalanena6421c42012-12-04 15:58:10 +0200915 if (surface->buffer && buffer != surface->buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300916 weston_buffer_post_release(surface->buffer);
917 wl_list_remove(&surface->buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300918 }
919
Pekka Paalanena6421c42012-12-04 15:58:10 +0200920 if (buffer && buffer != surface->buffer) {
Kristian Høgsbergb7b77e62012-09-05 22:38:18 -0400921 buffer->busy_count++;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +0300922 wl_signal_add(&buffer->resource.destroy_signal,
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300923 &surface->buffer_destroy_listener);
Pekka Paalanena6421c42012-12-04 15:58:10 +0200924 }
925
926 if (!buffer) {
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300927 if (weston_surface_is_mapped(surface))
928 weston_surface_unmap(surface);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300929 }
930
Pekka Paalanen5df44de2012-10-10 12:49:23 +0300931 surface->compositor->renderer->attach(surface, buffer);
932 surface->buffer = buffer;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100933}
934
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500935WL_EXPORT void
936weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400937{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500938 wl_list_remove(&surface->layer_link);
939 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500940 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500941 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400942}
943
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400944WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500945weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400946{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500947 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400948
949 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500950 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400951}
952
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500953WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500954weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200955{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400956 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200957 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200958
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400959 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500960 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200961}
962
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400963WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500964weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400965{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500966 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400967
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400968 pixman_region32_union(&compositor->primary_plane.damage,
969 &compositor->primary_plane.damage,
970 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400971 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400972}
973
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400974static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500975fade_frame(struct weston_animation *animation,
976 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400977{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500978 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400979 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500980 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500981 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400982
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600983 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600984 compositor->fade.spring.timestamp = msecs;
985
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500986 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500987 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500988 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
989 compositor->fade.spring.current);
990 weston_surface_damage(surface);
991
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500992 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400993 compositor->fade.spring.current =
994 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400995 wl_list_remove(&animation->link);
996 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200997
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500998 if (compositor->fade.spring.current < 0.001) {
999 destroy_surface(&surface->surface.resource);
1000 compositor->fade.surface = NULL;
1001 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001002 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001003 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001004 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001005 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001006}
1007
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001008static void
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001009surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001010 pixman_region32_t *opaque)
1011{
Pekka Paalanenbcdd5792012-11-07 12:25:13 +02001012 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
Kristian Høgsbergfa1be022012-09-05 22:49:55 -04001013 surface->compositor->renderer->flush_damage(surface);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001014
1015 if (surface->transform.enabled) {
1016 pixman_box32_t *extents;
1017
1018 extents = pixman_region32_extents(&surface->damage);
1019 surface_compute_bbox(surface, extents->x1, extents->y1,
1020 extents->x2 - extents->x1,
1021 extents->y2 - extents->y1,
1022 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001023 pixman_region32_translate(&surface->damage,
1024 -surface->plane->x,
1025 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001026 } else {
1027 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001028 surface->geometry.x - surface->plane->x,
1029 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001030 }
1031
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001032 pixman_region32_subtract(&surface->damage,
1033 &surface->damage, &surface->plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001034 pixman_region32_union(&surface->plane->damage,
1035 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001036 empty_region(&surface->damage);
1037 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001038 pixman_region32_union(&surface->plane->opaque,
1039 &surface->plane->opaque,
1040 &surface->transform.opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001041 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001042}
1043
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001044static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001045weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001046{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001047 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001048 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001049 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001050 struct weston_animation *animation, *next;
1051 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001052 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira8ea818f2012-09-14 16:12:03 +03001053 pixman_region32_t opaque, output_damage;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001054
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001055 weston_compositor_update_drag_surfaces(ec);
1056
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001057 /* Rebuild the surface list and update surface transforms up front. */
1058 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001059 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001060 wl_list_for_each(layer, &ec->layer_list, link) {
1061 wl_list_for_each(es, &layer->surface_list, layer_link) {
1062 weston_surface_update_transform(es);
1063 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001064 if (es->output == output) {
1065 wl_list_insert_list(&frame_callback_list,
1066 &es->frame_callback_list);
1067 wl_list_init(&es->frame_callback_list);
1068 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001069 }
1070 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001071
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001072 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001073 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001074 else
1075 wl_list_for_each(es, &ec->surface_list, link)
1076 weston_surface_move_to_plane(es, &ec->primary_plane);
1077
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001078 pixman_region32_init(&opaque);
1079
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001080 pixman_region32_fini(&ec->primary_plane.opaque);
1081 pixman_region32_init(&ec->primary_plane.opaque);
1082
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001083 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001084 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001085
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001086 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001087
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001088 pixman_region32_init(&output_damage);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001089 pixman_region32_intersect(&output_damage,
1090 &ec->primary_plane.damage, &output->region);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001091
Scott Moreauccbf29d2012-02-22 14:21:41 -07001092 if (output->dirty)
1093 weston_output_update_matrix(output);
1094
Kristian Høgsbergd7c17262012-09-05 21:54:15 -04001095 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001096
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001097 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001098
Kristian Høgsbergef044142011-06-21 15:02:12 -04001099 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001100
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001101 weston_compositor_repick(ec);
1102 wl_event_loop_dispatch(ec->input_loop, 0);
1103
Jonas Ådahldb773762012-06-13 00:01:21 +02001104 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001105 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001106 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001107 }
1108
Scott Moreaud64cf212012-06-08 19:40:54 -06001109 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001110 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001111 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001112 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001113}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001114
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001115static int
1116weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001117{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001118 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001119
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001120 wl_event_loop_dispatch(compositor->input_loop, 0);
1121
1122 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001123}
1124
1125WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001126weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001127{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001128 struct weston_compositor *compositor = output->compositor;
1129 struct wl_event_loop *loop =
1130 wl_display_get_event_loop(compositor->wl_display);
1131 int fd;
1132
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001133 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001134 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001135 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001136 return;
1137 }
1138
1139 output->repaint_scheduled = 0;
1140 if (compositor->input_loop_source)
1141 return;
1142
1143 fd = wl_event_loop_get_fd(compositor->input_loop);
1144 compositor->input_loop_source =
1145 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1146 weston_compositor_read_input, compositor);
1147}
1148
1149static void
1150idle_repaint(void *data)
1151{
1152 struct weston_output *output = data;
1153
1154 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001155}
1156
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001157WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001158weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1159{
1160 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001161 if (below != NULL)
1162 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001163}
1164
1165WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001166weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001167{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001168 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001169 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001170
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001171 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001172 return;
1173
Kristian Høgsbergef044142011-06-21 15:02:12 -04001174 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001175 output->repaint_needed = 1;
1176 if (output->repaint_scheduled)
1177 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001178
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001179 wl_event_loop_add_idle(loop, idle_repaint, output);
1180 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001181
1182 if (compositor->input_loop_source) {
1183 wl_event_source_remove(compositor->input_loop_source);
1184 compositor->input_loop_source = NULL;
1185 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001186}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001187
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001188WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001189weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1190{
1191 struct weston_output *output;
1192
1193 wl_list_for_each(output, &compositor->output_list, link)
1194 weston_output_schedule_repaint(output);
1195}
1196
1197WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001198weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001199{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001200 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001201 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001202
Scott Moreau9d1b1122012-06-08 19:40:53 -06001203 output = container_of(compositor->output_list.next,
1204 struct weston_output, link);
1205
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001206 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001207 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001208 return;
1209
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001210 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001211 surface = weston_surface_create(compositor);
John Kåre Alsakere2e3d072012-10-12 12:25:09 +02001212 if (!surface)
1213 return;
1214
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001215 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001216 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001217 wl_list_insert(&compositor->fade_layer.surface_list,
1218 &surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03001219 weston_surface_update_transform(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001220 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001221 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001222 }
1223
1224 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001225 if (wl_list_empty(&compositor->fade.animation.link)) {
1226 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001227 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001228 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001229 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001230}
1231
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001232static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001233surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001234{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001235 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001236}
1237
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001238static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001239surface_attach(struct wl_client *client,
1240 struct wl_resource *resource,
1241 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1242{
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001243 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001244 struct wl_buffer *buffer = NULL;
1245
1246 if (buffer_resource)
1247 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001248
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001249 if (surface->pending.buffer)
1250 wl_list_remove(&surface->pending.buffer_destroy_listener.link);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001251
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001252 surface->pending.sx = sx;
1253 surface->pending.sy = sy;
1254 surface->pending.buffer = buffer;
1255 if (buffer) {
1256 wl_signal_add(&buffer->resource.destroy_signal,
1257 &surface->pending.buffer_destroy_listener);
1258 surface->pending.remove_contents = 0;
1259 } else {
1260 surface->pending.remove_contents = 1;
1261 }
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001262}
1263
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001264static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001265surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001266 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001267 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001268{
Pekka Paalanen8e159182012-10-10 12:49:25 +03001269 struct weston_surface *surface = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001270
Pekka Paalanen8e159182012-10-10 12:49:25 +03001271 pixman_region32_union_rect(&surface->pending.damage,
1272 &surface->pending.damage,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001273 x, y, width, height);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001274}
1275
Kristian Høgsberg33418202011-08-16 23:01:28 -04001276static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001277destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001278{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001279 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001280
1281 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001282 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001283}
1284
1285static void
1286surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001287 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001288{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001289 struct weston_frame_callback *cb;
Pekka Paalanenbc106382012-10-10 12:49:31 +03001290 struct weston_surface *surface = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001291
1292 cb = malloc(sizeof *cb);
1293 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001294 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001295 return;
1296 }
Pekka Paalanenbc106382012-10-10 12:49:31 +03001297
Kristian Høgsberg33418202011-08-16 23:01:28 -04001298 cb->resource.object.interface = &wl_callback_interface;
1299 cb->resource.object.id = callback;
1300 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001301 cb->resource.client = client;
1302 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001303
1304 wl_client_add_resource(client, &cb->resource);
Pekka Paalanenbc106382012-10-10 12:49:31 +03001305 wl_list_insert(surface->pending.frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001306}
1307
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001308static void
1309surface_set_opaque_region(struct wl_client *client,
1310 struct wl_resource *resource,
1311 struct wl_resource *region_resource)
1312{
1313 struct weston_surface *surface = resource->data;
1314 struct weston_region *region;
1315
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001316 if (region_resource) {
1317 region = region_resource->data;
Pekka Paalanen512dde82012-10-10 12:49:27 +03001318 pixman_region32_copy(&surface->pending.opaque,
1319 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001320 } else {
Pekka Paalanen512dde82012-10-10 12:49:27 +03001321 empty_region(&surface->pending.opaque);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001322 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001323}
1324
1325static void
1326surface_set_input_region(struct wl_client *client,
1327 struct wl_resource *resource,
1328 struct wl_resource *region_resource)
1329{
1330 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001331 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001332
1333 if (region_resource) {
1334 region = region_resource->data;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001335 pixman_region32_copy(&surface->pending.input,
1336 &region->region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001337 } else {
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001338 pixman_region32_fini(&surface->pending.input);
1339 region_init_infinite(&surface->pending.input);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001340 }
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001341}
1342
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001343static int
1344surface_pending_buffer_has_different_size(struct weston_surface *surface)
1345{
1346 int width, height;
1347
1348 switch (surface->pending.buffer_transform) {
1349 case WL_OUTPUT_TRANSFORM_90:
1350 case WL_OUTPUT_TRANSFORM_270:
1351 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
1352 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
1353 height = surface->pending.buffer->width;
1354 width = surface->pending.buffer->height;
1355 break;
1356 default:
1357 width = surface->pending.buffer->width;
1358 height = surface->pending.buffer->height;
1359 }
1360
1361 if (width == surface->geometry.width &&
1362 height == surface->geometry.height)
1363 return 0;
1364 else
1365 return 1;
1366}
1367
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001368static void
1369surface_commit(struct wl_client *client, struct wl_resource *resource)
1370{
1371 struct weston_surface *surface = resource->data;
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001372 pixman_region32_t opaque;
1373
1374 if (surface->pending.sx || surface->pending.sy ||
1375 (surface->pending.buffer &&
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001376 surface_pending_buffer_has_different_size(surface)))
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001377 surface->geometry.dirty = 1;
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001378
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001379 /* wl_surface.set_buffer_rotation */
1380 surface->buffer_transform = surface->pending.buffer_transform;
1381
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001382 /* wl_surface.attach */
1383 if (surface->pending.buffer || surface->pending.remove_contents)
1384 weston_surface_attach(surface, surface->pending.buffer);
1385
1386 if (surface->buffer && surface->configure)
1387 surface->configure(surface, surface->pending.sx,
1388 surface->pending.sy);
Daniel Stoneb4f4a592012-11-07 17:51:44 +11001389 surface->pending.sx = 0;
1390 surface->pending.sy = 0;
Pekka Paalanen8e159182012-10-10 12:49:25 +03001391
1392 /* wl_surface.damage */
1393 pixman_region32_union(&surface->damage, &surface->damage,
1394 &surface->pending.damage);
Kristian Høgsberg4d0214c2012-11-08 11:36:02 -05001395 pixman_region32_intersect_rect(&surface->damage, &surface->damage,
1396 0, 0,
1397 surface->geometry.width,
1398 surface->geometry.height);
Pekka Paalanen8e159182012-10-10 12:49:25 +03001399 empty_region(&surface->pending.damage);
Pekka Paalanen512dde82012-10-10 12:49:27 +03001400
1401 /* wl_surface.set_opaque_region */
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001402 pixman_region32_init_rect(&opaque, 0, 0,
Pekka Paalanen512dde82012-10-10 12:49:27 +03001403 surface->geometry.width,
1404 surface->geometry.height);
Ander Conselvan de Oliveira5df8eca2012-10-30 17:44:01 +02001405 pixman_region32_intersect(&opaque,
1406 &opaque, &surface->pending.opaque);
1407
1408 if (!pixman_region32_equal(&opaque, &surface->opaque)) {
1409 pixman_region32_copy(&surface->opaque, &opaque);
1410 surface->geometry.dirty = 1;
1411 }
1412
1413 pixman_region32_fini(&opaque);
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001414
1415 /* wl_surface.set_input_region */
1416 pixman_region32_fini(&surface->input);
1417 pixman_region32_init_rect(&surface->input, 0, 0,
1418 surface->geometry.width,
1419 surface->geometry.height);
1420 pixman_region32_intersect(&surface->input,
1421 &surface->input, &surface->pending.input);
1422
Pekka Paalanenbc106382012-10-10 12:49:31 +03001423 /* wl_surface.frame */
1424 wl_list_insert_list(&surface->frame_callback_list,
1425 &surface->pending.frame_callback_list);
1426 wl_list_init(&surface->pending.frame_callback_list);
1427
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03001428 weston_surface_schedule_repaint(surface);
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001429}
1430
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001431static void
1432surface_set_buffer_transform(struct wl_client *client,
1433 struct wl_resource *resource, int transform)
1434{
1435 struct weston_surface *surface = resource->data;
1436
1437 surface->pending.buffer_transform = transform;
1438}
1439
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001440static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001441 surface_destroy,
1442 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001443 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001444 surface_frame,
1445 surface_set_opaque_region,
Pekka Paalanen5df44de2012-10-10 12:49:23 +03001446 surface_set_input_region,
Ander Conselvan de Oliveira012b4c72012-11-27 17:03:42 +02001447 surface_commit,
1448 surface_set_buffer_transform
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001449};
1450
1451static void
1452compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001453 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001454{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001455 struct weston_compositor *ec = resource->data;
1456 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001457
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001458 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001459 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001460 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001461 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001462 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001463
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001464 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001465
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001466 surface->surface.resource.object.id = id;
1467 surface->surface.resource.object.interface = &wl_surface_interface;
1468 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001469 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001470 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001471
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001472 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001473}
1474
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001475static void
1476destroy_region(struct wl_resource *resource)
1477{
1478 struct weston_region *region =
1479 container_of(resource, struct weston_region, resource);
1480
1481 pixman_region32_fini(&region->region);
1482 free(region);
1483}
1484
1485static void
1486region_destroy(struct wl_client *client, struct wl_resource *resource)
1487{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001488 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001489}
1490
1491static void
1492region_add(struct wl_client *client, struct wl_resource *resource,
1493 int32_t x, int32_t y, int32_t width, int32_t height)
1494{
1495 struct weston_region *region = resource->data;
1496
1497 pixman_region32_union_rect(&region->region, &region->region,
1498 x, y, width, height);
1499}
1500
1501static void
1502region_subtract(struct wl_client *client, struct wl_resource *resource,
1503 int32_t x, int32_t y, int32_t width, int32_t height)
1504{
1505 struct weston_region *region = resource->data;
1506 pixman_region32_t rect;
1507
1508 pixman_region32_init_rect(&rect, x, y, width, height);
1509 pixman_region32_subtract(&region->region, &region->region, &rect);
1510 pixman_region32_fini(&rect);
1511}
1512
1513static const struct wl_region_interface region_interface = {
1514 region_destroy,
1515 region_add,
1516 region_subtract
1517};
1518
1519static void
1520compositor_create_region(struct wl_client *client,
1521 struct wl_resource *resource, uint32_t id)
1522{
1523 struct weston_region *region;
1524
1525 region = malloc(sizeof *region);
1526 if (region == NULL) {
1527 wl_resource_post_no_memory(resource);
1528 return;
1529 }
1530
1531 region->resource.destroy = destroy_region;
1532
1533 region->resource.object.id = id;
1534 region->resource.object.interface = &wl_region_interface;
1535 region->resource.object.implementation =
1536 (void (**)(void)) &region_interface;
1537 region->resource.data = region;
1538
1539 pixman_region32_init(&region->region);
1540
1541 wl_client_add_resource(client, &region->resource);
1542}
1543
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001544static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001545 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001546 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001547};
1548
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001549WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001550weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001551{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001552 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1553 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001554
1555 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001556 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001557}
1558
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001559static void
1560weston_compositor_dpms_on(struct weston_compositor *compositor)
1561{
1562 struct weston_output *output;
1563
1564 wl_list_for_each(output, &compositor->output_list, link)
1565 if (output->set_dpms)
1566 output->set_dpms(output, WESTON_DPMS_ON);
1567}
1568
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001569WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001570weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001571{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001572 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1573 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001574 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001575 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001576 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001577 }
1578}
1579
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001580static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001581weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001582{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001583 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001584 compositor->idle_inhibit++;
1585}
1586
1587static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001588weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001589{
1590 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001592}
1593
1594static int
1595idle_handler(void *data)
1596{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001597 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001598
1599 if (compositor->idle_inhibit)
1600 return 1;
1601
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001602 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001603
1604 return 1;
1605}
1606
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001607WL_EXPORT void
1608weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1609{
1610 pixman_region32_init(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001611 pixman_region32_init(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001612 plane->x = x;
1613 plane->y = y;
1614}
1615
1616WL_EXPORT void
1617weston_plane_release(struct weston_plane *plane)
1618{
1619 pixman_region32_fini(&plane->damage);
Ander Conselvan de Oliveira4bcf3a52012-10-31 17:55:45 +02001620 pixman_region32_fini(&plane->opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001621}
1622
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001623static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001624weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001625
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001626static void
Daniel Stone37816df2012-05-16 18:45:18 +01001627clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001628{
Daniel Stone37816df2012-05-16 18:45:18 +01001629 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001630 struct weston_output *output, *prev = NULL;
1631 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001632
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001633 x = wl_fixed_to_int(*fx);
1634 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001635 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1636 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001637
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001638 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001639 if (pixman_region32_contains_point(&output->region,
1640 x, y, NULL))
1641 valid = 1;
1642 if (pixman_region32_contains_point(&output->region,
1643 old_x, old_y, NULL))
1644 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001645 }
Daniel Stone37816df2012-05-16 18:45:18 +01001646
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001647 if (!valid) {
1648 if (x < prev->x)
1649 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06001650 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001651 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001652 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001653 if (y < prev->y)
1654 *fy = wl_fixed_from_int(prev->y);
1655 else if (y >= prev->y + prev->current->height)
1656 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06001657 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001658 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001659}
1660
1661WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001662notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001663{
1664 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001665 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001666 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001667 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001668 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001669
1670 weston_compositor_activity(ec);
1671
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001672 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001673
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001674 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001675
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001676 pointer->x = x;
1677 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001678
1679 ix = wl_fixed_to_int(x);
1680 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001681
Scott Moreauccbf29d2012-02-22 14:21:41 -07001682 wl_list_for_each(output, &ec->output_list, link)
1683 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001684 pixman_region32_contains_point(&output->region,
1685 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001686 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001687
Daniel Stone37816df2012-05-16 18:45:18 +01001688 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001689 interface = pointer->grab->interface;
1690 interface->motion(pointer->grab, time,
1691 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001692
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001693 if (seat->sprite) {
1694 weston_surface_set_position(seat->sprite,
1695 ix - seat->hotspot_x,
1696 iy - seat->hotspot_y);
1697 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001698 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001699}
1700
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001701WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001702weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001703 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001704{
Daniel Stone37816df2012-05-16 18:45:18 +01001705 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001706
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001707 if (seat->seat.keyboard) {
1708 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1709 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001710 }
1711
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001712 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001713}
1714
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001715WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001716notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001717 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001718{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001719 struct weston_compositor *compositor = seat->compositor;
1720 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001721 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001722 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001723 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001724
Daniel Stone4dbadb12012-05-30 16:31:51 +01001725 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001726 if (compositor->ping_handler && focus)
1727 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001728 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001729 if (pointer->button_count == 0) {
1730 pointer->grab_button = button;
1731 pointer->grab_time = time;
1732 pointer->grab_x = pointer->x;
1733 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001734 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001735 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001736 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001737 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001738 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001739 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001740
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001741 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001742 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001743
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001744 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001745
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001746 if (pointer->button_count == 1)
1747 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001748 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001749}
1750
Scott Moreau210d0792012-03-22 10:47:01 -06001751WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001752notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01001753 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001754{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001755 struct weston_compositor *compositor = seat->compositor;
1756 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01001757 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001758 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001759 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1760
1761 if (compositor->ping_handler && focus)
1762 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001763
1764 weston_compositor_activity(compositor);
1765
Scott Moreau6a3633d2012-03-20 08:47:59 -06001766 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001767 weston_compositor_run_axis_binding(compositor, seat,
1768 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001769 else
1770 return;
1771
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001772 if (pointer->focus_resource)
1773 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001774 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001775}
1776
Daniel Stone05d58682012-06-22 13:21:38 +01001777WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001778notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001779{
Daniel Stone05d58682012-06-22 13:21:38 +01001780 struct wl_keyboard *keyboard = &seat->keyboard;
1781 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001782 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001783 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001784 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001785 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001786
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001787 /* Serialize and update our internal state, checking to see if it's
1788 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001789 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1790 XKB_STATE_DEPRESSED);
1791 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1792 XKB_STATE_LATCHED);
1793 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1794 XKB_STATE_LOCKED);
1795 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001796 XKB_STATE_EFFECTIVE);
1797
Daniel Stone71c38772012-06-22 13:21:30 +01001798 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1799 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1800 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1801 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001802 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001803
Daniel Stone71c38772012-06-22 13:21:30 +01001804 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1805 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1806 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1807 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001808
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001809 /* And update the modifier_state for bindings. */
1810 mods_lookup = mods_depressed | mods_latched;
1811 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001812 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001813 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001814 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001815 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001816 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001817 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001818 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1819 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001820
Daniel Stone8c8164f2012-05-30 16:31:45 +01001821 /* Finally, notify the compositor that LEDs have changed. */
1822 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001823 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001824 leds |= LED_NUM_LOCK;
1825 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001826 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001827 leds |= LED_CAPS_LOCK;
1828 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001829 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001830 leds |= LED_SCROLL_LOCK;
1831 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001832 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001833 seat->xkb_state.leds = leds;
1834
Daniel Stone05d58682012-06-22 13:21:38 +01001835 if (changed) {
1836 grab->interface->modifiers(grab,
1837 serial,
1838 keyboard->modifiers.mods_depressed,
1839 keyboard->modifiers.mods_latched,
1840 keyboard->modifiers.mods_locked,
1841 keyboard->modifiers.group);
1842 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001843}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001844
Daniel Stone05d58682012-06-22 13:21:38 +01001845static void
1846update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001847 enum wl_keyboard_key_state state)
1848{
1849 enum xkb_key_direction direction;
1850
1851 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1852 direction = XKB_KEY_DOWN;
1853 else
1854 direction = XKB_KEY_UP;
1855
1856 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1857 * broken keycode system, which starts at 8. */
1858 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1859
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001860 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001861}
1862
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001863WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001864notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001865 enum wl_keyboard_key_state state,
1866 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001867{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001868 struct weston_compositor *compositor = seat->compositor;
1869 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01001870 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001871 (struct weston_surface *) keyboard->focus;
1872 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001873 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001874 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001875
Daniel Stonec9785ea2012-05-30 16:31:52 +01001876 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001877 if (compositor->ping_handler && focus)
1878 compositor->ping_handler(focus, serial);
1879
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001880 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001881 keyboard->grab_key = key;
1882 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001883 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001884 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001885 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001886
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001887 end = keyboard->keys.data + keyboard->keys.size;
1888 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001889 if (*k == key) {
1890 /* Ignore server-generated repeats. */
1891 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1892 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001893 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001894 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001895 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001896 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001897 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001898 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001899 *k = key;
1900 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001901
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001902 if (grab == &keyboard->default_grab) {
1903 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01001904 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001905 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06001906 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001907
Daniel Stone7ace3902012-05-30 16:31:40 +01001908 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001909
1910 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001911 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01001912 wl_display_get_serial(compositor->wl_display),
1913 key,
1914 state);
1915 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001916}
1917
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001918WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001919notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01001920 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001921{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001922 struct weston_compositor *compositor = seat->compositor;
1923 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001924
1925 if (output) {
Kristian Høgsberg00fbbe62012-10-23 13:04:09 -04001926 clip_pointer_motion(seat, &x, &y);
Daniel Stone37816df2012-05-16 18:45:18 +01001927 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001928 x - pointer->x,
1929 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001930
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001931 pointer->x = x;
1932 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001933 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001934 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001935 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001936 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03001937 /* FIXME: We should call wl_pointer_set_focus(seat,
1938 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001939 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001940}
1941
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001942static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001943destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001944{
Daniel Stone37816df2012-05-16 18:45:18 +01001945 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001946
Daniel Stone37816df2012-05-16 18:45:18 +01001947 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001948 saved_kbd_focus_listener);
1949
Daniel Stone37816df2012-05-16 18:45:18 +01001950 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001951}
1952
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001953WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001954notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001955 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001956{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001957 struct weston_compositor *compositor = seat->compositor;
1958 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001959 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001960 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001961
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001962 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001963 wl_array_copy(&keyboard->keys, keys);
1964 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001965 weston_compositor_idle_inhibit(compositor);
1966 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001967 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001968 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001969 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001970
Daniel Stoneef172672012-06-22 13:21:32 +01001971 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001972 wl_array_for_each(k, &keyboard->keys) {
1973 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01001974 WL_KEYBOARD_KEY_STATE_PRESSED);
1975 }
1976
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001977 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001978
1979 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001980 wl_list_remove(&seat->saved_kbd_focus_listener.link);
1981 wl_keyboard_set_focus(keyboard, surface);
1982 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001983 }
1984}
1985
1986WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001987notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01001988{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001989 struct weston_compositor *compositor = seat->compositor;
1990 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001991 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01001992
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001993 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001994 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01001995 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001996 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04001997 WL_KEYBOARD_KEY_STATE_RELEASED);
1998 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01001999
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002000 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002001
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002002 if (keyboard->focus) {
2003 seat->saved_kbd_focus = keyboard->focus;
2004 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002005 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002006 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2007 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002008 }
2009
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002010 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002011 /* FIXME: We really need keyboard grab cancel here to
2012 * let the grab shut down properly. As it is we leak
2013 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002014 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002015}
2016
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002017static void
Daniel Stone37816df2012-05-16 18:45:18 +01002018touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002019{
Daniel Stone37816df2012-05-16 18:45:18 +01002020 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002021 struct wl_resource *resource;
2022
Pekka Paalanen56464252012-07-31 13:21:08 +03002023 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002024 return;
2025
Pekka Paalanen56464252012-07-31 13:21:08 +03002026 if (seat->touch->focus_resource)
2027 wl_list_remove(&seat->touch->focus_listener.link);
2028 seat->touch->focus = NULL;
2029 seat->touch->focus_resource = NULL;
2030
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002031 if (surface) {
2032 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002033 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002034 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002035 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002036 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002037 return;
2038 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002039
Daniel Stone37816df2012-05-16 18:45:18 +01002040 seat->touch->focus = surface;
2041 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002042 wl_signal_add(&resource->destroy_signal,
2043 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002044 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002045}
2046
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002047/**
2048 * notify_touch - emulates button touches and notifies surfaces accordingly.
2049 *
2050 * It assumes always the correct cycle sequence until it gets here: touch_down
2051 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2052 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002053 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002054 */
2055WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002056notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002057 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002058{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002059 struct weston_compositor *ec = seat->compositor;
2060 struct wl_touch *touch = seat->seat.touch;
Matt Roper47c1b982012-10-10 16:56:53 -07002061 struct wl_touch_grab *grab = touch->grab;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002063 wl_fixed_t sx, sy;
Matt Roper47c1b982012-10-10 16:56:53 -07002064
2065 /* Update grab's global coordinates. */
2066 touch->grab_x = x;
2067 touch->grab_y = y;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002068
2069 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002070 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002071 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002072
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002073 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002074
2075 /* the first finger down picks the surface, and all further go
2076 * to that surface for the remainder of the touch session i.e.
2077 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002078 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002079 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002080 touch_set_focus(seat, &es->surface);
2081 } else if (touch->focus) {
2082 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002083 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002084 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002085
Matt Roper47c1b982012-10-10 16:56:53 -07002086 grab->interface->down(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002087 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002088 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002089 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002090 if (!es)
2091 break;
2092
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002093 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Matt Roper47c1b982012-10-10 16:56:53 -07002094 grab->interface->motion(grab, time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002095 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002096 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002097 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002098 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002099
Matt Roper47c1b982012-10-10 16:56:53 -07002100 grab->interface->up(grab, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002101 if (seat->num_tp == 0)
2102 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002103 break;
2104 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002105}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002106
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002107static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002108pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2109{
2110 struct weston_seat *seat = container_of(listener, struct weston_seat,
2111 sprite_destroy_listener);
2112
2113 seat->sprite = NULL;
2114}
2115
2116static void
2117pointer_cursor_surface_configure(struct weston_surface *es,
2118 int32_t dx, int32_t dy)
2119{
2120 struct weston_seat *seat = es->private;
2121 int x, y;
2122
2123 assert(es == seat->sprite);
2124
2125 seat->hotspot_x -= dx;
2126 seat->hotspot_y -= dy;
2127
2128 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2129 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2130
2131 weston_surface_configure(seat->sprite, x, y,
2132 es->buffer->width, es->buffer->height);
2133
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002134 empty_region(&es->pending.input);
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002135
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002136 if (!weston_surface_is_mapped(es)) {
2137 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2138 &es->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002139 weston_surface_update_transform(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002140 }
2141}
2142
2143static void
2144pointer_unmap_sprite(struct weston_seat *seat)
2145{
2146 if (weston_surface_is_mapped(seat->sprite))
2147 weston_surface_unmap(seat->sprite);
2148
2149 wl_list_remove(&seat->sprite_destroy_listener.link);
2150 seat->sprite->configure = NULL;
2151 seat->sprite->private = NULL;
2152 seat->sprite = NULL;
2153}
2154
2155static void
2156pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2157 uint32_t serial, struct wl_resource *surface_resource,
2158 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002159{
Daniel Stone37816df2012-05-16 18:45:18 +01002160 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002161 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002162
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002163 if (surface_resource)
Pekka Paalanen6c71ee12012-10-10 12:49:30 +03002164 surface = surface_resource->data;
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002165
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002166 if (seat->seat.pointer->focus == NULL)
2167 return;
2168 if (seat->seat.pointer->focus->resource.client != client)
2169 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002170 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002171 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002172
2173 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002174 if (surface->configure) {
2175 wl_resource_post_error(&surface->surface.resource,
2176 WL_DISPLAY_ERROR_INVALID_OBJECT,
2177 "surface->configure already "
2178 "set");
2179 return;
2180 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002181 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002182
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002183 if (seat->sprite)
2184 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002185
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002186 if (!surface)
2187 return;
2188
2189 wl_signal_add(&surface->surface.resource.destroy_signal,
2190 &seat->sprite_destroy_listener);
2191
2192 surface->configure = pointer_cursor_surface_configure;
2193 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002194 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002195 seat->hotspot_x = x;
2196 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002197
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002198 if (surface->buffer)
2199 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002200}
2201
Daniel Stone37816df2012-05-16 18:45:18 +01002202static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002203 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002204};
2205
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002206static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002207handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002208{
Daniel Stone37816df2012-05-16 18:45:18 +01002209 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002210
Daniel Stone37816df2012-05-16 18:45:18 +01002211 seat = container_of(listener, struct weston_seat,
2212 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002213
Daniel Stone37816df2012-05-16 18:45:18 +01002214 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002215}
2216
Casey Dahlin9074db52012-04-19 22:50:09 -04002217static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002218{
2219 wl_list_remove(&resource->link);
2220 free(resource);
2221}
2222
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002223static void
Daniel Stone37816df2012-05-16 18:45:18 +01002224seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2225 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002226{
Daniel Stone37816df2012-05-16 18:45:18 +01002227 struct weston_seat *seat = resource->data;
2228 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002229
Daniel Stone37816df2012-05-16 18:45:18 +01002230 if (!seat->seat.pointer)
2231 return;
2232
2233 cr = wl_client_add_object(client, &wl_pointer_interface,
2234 &pointer_interface, id, seat);
2235 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002236 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002237
2238 if (seat->seat.pointer->focus &&
2239 seat->seat.pointer->focus->resource.client == client) {
2240 struct weston_surface *surface;
2241 wl_fixed_t sx, sy;
2242
2243 surface = (struct weston_surface *) seat->seat.pointer->focus;
2244 weston_surface_from_global_fixed(surface,
2245 seat->seat.pointer->x,
2246 seat->seat.pointer->y,
2247 &sx,
2248 &sy);
2249 wl_pointer_set_focus(seat->seat.pointer,
2250 seat->seat.pointer->focus,
2251 sx,
2252 sy);
2253 }
Daniel Stone37816df2012-05-16 18:45:18 +01002254}
2255
2256static void
2257seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2258 uint32_t id)
2259{
2260 struct weston_seat *seat = resource->data;
2261 struct wl_resource *cr;
2262
2263 if (!seat->seat.keyboard)
2264 return;
2265
2266 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2267 seat);
2268 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002269 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002270
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002271 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2272 seat->xkb_info.keymap_fd,
2273 seat->xkb_info.keymap_size);
2274
Daniel Stone17b13bd2012-05-30 16:31:39 +01002275 if (seat->seat.keyboard->focus &&
2276 seat->seat.keyboard->focus->resource.client == client) {
2277 wl_keyboard_set_focus(seat->seat.keyboard,
2278 seat->seat.keyboard->focus);
2279 }
Daniel Stone37816df2012-05-16 18:45:18 +01002280}
2281
2282static void
2283seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2284 uint32_t id)
2285{
2286 struct weston_seat *seat = resource->data;
2287 struct wl_resource *cr;
2288
2289 if (!seat->seat.touch)
2290 return;
2291
2292 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2293 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002294 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002295}
2296
2297static const struct wl_seat_interface seat_interface = {
2298 seat_get_pointer,
2299 seat_get_keyboard,
2300 seat_get_touch,
2301};
2302
2303static void
2304bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2305{
2306 struct wl_seat *seat = data;
2307 struct wl_resource *resource;
2308 enum wl_seat_capability caps = 0;
2309
2310 resource = wl_client_add_object(client, &wl_seat_interface,
2311 &seat_interface, id, data);
2312 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002313 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002314
2315 if (seat->pointer)
2316 caps |= WL_SEAT_CAPABILITY_POINTER;
2317 if (seat->keyboard)
2318 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2319 if (seat->touch)
2320 caps |= WL_SEAT_CAPABILITY_TOUCH;
2321
2322 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002323}
2324
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002325static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002326device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002327{
Daniel Stone37816df2012-05-16 18:45:18 +01002328 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002329
Daniel Stone37816df2012-05-16 18:45:18 +01002330 seat = container_of(listener, struct weston_seat,
2331 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002332
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002333 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002334}
2335
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002336static void weston_compositor_xkb_init(struct weston_compositor *ec,
2337 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002338{
Daniel Stonee379da92012-05-30 16:32:04 +01002339 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002340 ec->xkb_context = xkb_context_new(0);
2341 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002342 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002343 exit(1);
2344 }
Daniel Stone994679a2012-05-30 16:31:43 +01002345 }
2346
2347 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002348 ec->xkb_names = *names;
2349 if (!ec->xkb_names.rules)
2350 ec->xkb_names.rules = strdup("evdev");
2351 if (!ec->xkb_names.model)
2352 ec->xkb_names.model = strdup("pc105");
2353 if (!ec->xkb_names.layout)
2354 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002355}
2356
Daniel Stonee379da92012-05-30 16:32:04 +01002357static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2358{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002359 if (xkb_info->keymap)
2360 xkb_map_unref(xkb_info->keymap);
2361
2362 if (xkb_info->keymap_area)
2363 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2364 if (xkb_info->keymap_fd >= 0)
2365 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002366}
2367
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002368static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2369{
Daniel Stonee379da92012-05-30 16:32:04 +01002370 free((char *) ec->xkb_names.rules);
2371 free((char *) ec->xkb_names.model);
2372 free((char *) ec->xkb_names.layout);
2373 free((char *) ec->xkb_names.variant);
2374 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002375
Daniel Stonee379da92012-05-30 16:32:04 +01002376 xkb_info_destroy(&ec->xkb_info);
2377 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002378}
2379
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002380static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002381weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002382{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002383 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002384
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002385 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2386 XKB_MOD_NAME_SHIFT);
2387 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2388 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002389 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2390 XKB_MOD_NAME_CTRL);
2391 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2392 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002393 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2394 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002395 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2396 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002397 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002398
2399 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2400 XKB_LED_NAME_NUM);
2401 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2402 XKB_LED_NAME_CAPS);
2403 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2404 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002405
2406 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2407 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002408 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002409 exit(EXIT_FAILURE);
2410 }
2411 xkb_info->keymap_size = strlen(keymap_str) + 1;
2412
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002413 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002414 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002415 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002416 (unsigned long) xkb_info->keymap_size);
2417 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002418 }
2419
2420 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2421 PROT_READ | PROT_WRITE,
2422 MAP_SHARED, xkb_info->keymap_fd, 0);
2423 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002424 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002425 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002426 goto err_dev_zero;
2427 }
2428 strcpy(xkb_info->keymap_area, keymap_str);
2429 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002430
2431 return;
2432
2433err_dev_zero:
2434 close(xkb_info->keymap_fd);
2435 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002436err_keymap_str:
2437 free(keymap_str);
2438 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002439}
2440
2441static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002442weston_compositor_build_global_keymap(struct weston_compositor *ec)
2443{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002444 if (ec->xkb_info.keymap != NULL)
2445 return;
2446
Daniel Stonee379da92012-05-30 16:32:04 +01002447 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002448 &ec->xkb_names,
2449 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002450 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002451 weston_log("failed to compile global XKB keymap\n");
2452 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002453 "options %s",
2454 ec->xkb_names.rules, ec->xkb_names.model,
2455 ec->xkb_names.layout, ec->xkb_names.variant,
2456 ec->xkb_names.options);
2457 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002458 }
2459
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002460 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002461}
2462
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002463WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002464weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002465{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002466 if (seat->has_keyboard)
2467 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002468
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002469 if (keymap != NULL) {
2470 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002471 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002472 }
2473 else {
2474 weston_compositor_build_global_keymap(seat->compositor);
2475 seat->xkb_info = seat->compositor->xkb_info;
2476 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2477 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002478
2479 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2480 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002481 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002482 exit(1);
2483 }
2484
Daniel Stone71c38772012-06-22 13:21:30 +01002485 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002486
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002487 wl_keyboard_init(&seat->keyboard);
2488 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2489
2490 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002491}
2492
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002493WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002494weston_seat_init_pointer(struct weston_seat *seat)
2495{
2496 if (seat->has_pointer)
2497 return;
2498
2499 wl_pointer_init(&seat->pointer);
2500 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2501
2502 seat->has_pointer = 1;
2503}
2504
2505WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002506weston_seat_init_touch(struct weston_seat *seat)
2507{
2508 if (seat->has_touch)
2509 return;
2510
2511 wl_touch_init(&seat->touch);
2512 wl_seat_set_touch(&seat->seat, &seat->touch);
2513
2514 seat->has_touch = 1;
2515}
2516
2517WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002518weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002519{
Daniel Stone37816df2012-05-16 18:45:18 +01002520 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002521 seat->has_pointer = 0;
2522 seat->has_keyboard = 0;
2523 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002524
Daniel Stone37816df2012-05-16 18:45:18 +01002525 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2526 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002527
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002528 seat->sprite = NULL;
2529 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002530
Daniel Stone37816df2012-05-16 18:45:18 +01002531 seat->compositor = ec;
2532 seat->hotspot_x = 16;
2533 seat->hotspot_y = 16;
2534 seat->modifier_state = 0;
2535 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002536
Daniel Stone37816df2012-05-16 18:45:18 +01002537 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002538 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002539
Daniel Stone37816df2012-05-16 18:45:18 +01002540 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002541
Daniel Stone37816df2012-05-16 18:45:18 +01002542 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2543 wl_signal_add(&seat->seat.drag_icon_signal,
2544 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002545
2546 clipboard_create(seat);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002547 wl_signal_emit(&ec->seat_created_signal, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002548}
2549
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002550WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002551weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002552{
Daniel Stone37816df2012-05-16 18:45:18 +01002553 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002554 /* The global object is destroyed at wl_display_destroy() time. */
2555
Daniel Stone37816df2012-05-16 18:45:18 +01002556 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002557 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002558
Daniel Stone74419a22012-05-30 16:32:02 +01002559 if (seat->xkb_state.state != NULL)
2560 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002561 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002562
Daniel Stone37816df2012-05-16 18:45:18 +01002563 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002564}
2565
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002566static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002567drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2568{
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002569 empty_region(&es->pending.input);
2570
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002571 weston_surface_configure(es,
2572 es->geometry.x + sx, es->geometry.y + sy,
2573 es->buffer->width, es->buffer->height);
2574}
2575
2576static int
Daniel Stone37816df2012-05-16 18:45:18 +01002577device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002578 struct weston_surface *surface)
2579{
Daniel Stone37816df2012-05-16 18:45:18 +01002580 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002581
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002582 if (surface->configure) {
2583 wl_resource_post_error(&surface->surface.resource,
2584 WL_DISPLAY_ERROR_INVALID_OBJECT,
2585 "surface->configure already set");
2586 return 0;
2587 }
2588
Daniel Stone37816df2012-05-16 18:45:18 +01002589 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002590
Daniel Stone37816df2012-05-16 18:45:18 +01002591 weston_surface_set_position(ws->drag_surface,
2592 wl_fixed_to_double(seat->pointer->x),
2593 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002594
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002595 surface->configure = drag_surface_configure;
2596
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002597 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002598 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002599
2600 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002601}
2602
2603static void
Daniel Stone37816df2012-05-16 18:45:18 +01002604device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002605{
Ander Conselvan de Oliveira5fd55802012-10-11 14:06:19 +03002606 if (weston_surface_is_mapped(seat->drag_surface))
2607 weston_surface_unmap(seat->drag_surface);
2608
Daniel Stone37816df2012-05-16 18:45:18 +01002609 seat->drag_surface->configure = NULL;
Pekka Paalanen0cbd3b52012-10-10 12:49:28 +03002610 empty_region(&seat->drag_surface->pending.input);
Daniel Stone37816df2012-05-16 18:45:18 +01002611 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2612 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002613}
2614
2615static void
Daniel Stone37816df2012-05-16 18:45:18 +01002616device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002617{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002618 struct wl_list *list;
2619
Daniel Stone37816df2012-05-16 18:45:18 +01002620 if (weston_surface_is_mapped(seat->drag_surface) ||
2621 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002622 return;
2623
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002624 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2625 list = &seat->sprite->layer_link;
2626 else
2627 list = &seat->compositor->cursor_layer.surface_list;
2628
2629 wl_list_insert(list, &seat->drag_surface->layer_link);
Ander Conselvan de Oliveira231ba172012-09-14 16:12:04 +03002630 weston_surface_update_transform(seat->drag_surface);
Daniel Stone37816df2012-05-16 18:45:18 +01002631 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002632}
2633
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002634static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002635weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01002636 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002637{
2638 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002639
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002640 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002641 return;
2642
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002643 if (seat->drag_surface && seat->seat.drag_surface &&
2644 (&seat->drag_surface->surface.resource !=
2645 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002646 /* between calls to this funcion we got a new drag_surface */
2647 surface_changed = 1;
2648
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002649 if (!seat->seat.drag_surface || surface_changed) {
2650 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002651 if (!surface_changed)
2652 return;
2653 }
2654
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002655 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002656 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002657 seat->seat.drag_surface;
2658 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002659 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002660 }
2661
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002662 /* the client may not have attached a buffer to the drag surface
2663 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002664 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002665
2666 if (!dx && !dy)
2667 return;
2668
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002669 weston_surface_set_position(seat->drag_surface,
2670 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
2671 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002672}
2673
2674WL_EXPORT void
2675weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2676{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002677 struct weston_seat *seat;
2678
2679 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002680 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002681}
2682
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002683static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002684bind_output(struct wl_client *client,
2685 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002686{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002687 struct weston_output *output = data;
2688 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002689 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002690
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002691 resource = wl_client_add_object(client,
2692 &wl_output_interface, NULL, id, data);
2693
Casey Dahlin9074db52012-04-19 22:50:09 -04002694 wl_list_insert(&output->resource_list, &resource->link);
2695 resource->destroy = unbind_resource;
2696
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002697 wl_output_send_geometry(resource,
2698 output->x,
2699 output->y,
2700 output->mm_width,
2701 output->mm_height,
2702 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002703 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04002704 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002705
2706 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002707 wl_output_send_mode(resource,
2708 mode->flags,
2709 mode->width,
2710 mode->height,
2711 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002712 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002713}
2714
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002715WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002716weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002717{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002718 struct weston_compositor *c = output->compositor;
2719
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002720 pixman_region32_fini(&output->region);
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002721 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002722 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002723
2724 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002725}
2726
Scott Moreau1bad5db2012-08-18 01:04:05 -06002727static void
2728weston_output_compute_transform(struct weston_output *output)
2729{
2730 struct weston_matrix transform;
2731 int flip;
2732
2733 weston_matrix_init(&transform);
2734
2735 switch(output->transform) {
2736 case WL_OUTPUT_TRANSFORM_FLIPPED:
2737 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2738 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2739 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2740 flip = -1;
2741 break;
2742 default:
2743 flip = 1;
2744 break;
2745 }
2746
2747 switch(output->transform) {
2748 case WL_OUTPUT_TRANSFORM_NORMAL:
2749 case WL_OUTPUT_TRANSFORM_FLIPPED:
2750 transform.d[0] = flip;
2751 transform.d[1] = 0;
2752 transform.d[4] = 0;
2753 transform.d[5] = 1;
2754 break;
2755 case WL_OUTPUT_TRANSFORM_90:
2756 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2757 transform.d[0] = 0;
2758 transform.d[1] = -flip;
2759 transform.d[4] = 1;
2760 transform.d[5] = 0;
2761 break;
2762 case WL_OUTPUT_TRANSFORM_180:
2763 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2764 transform.d[0] = -flip;
2765 transform.d[1] = 0;
2766 transform.d[4] = 0;
2767 transform.d[5] = -1;
2768 break;
2769 case WL_OUTPUT_TRANSFORM_270:
2770 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2771 transform.d[0] = 0;
2772 transform.d[1] = flip;
2773 transform.d[4] = -1;
2774 transform.d[5] = 0;
2775 break;
2776 default:
2777 break;
2778 }
2779
2780 weston_matrix_multiply(&output->matrix, &transform);
2781}
2782
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002783WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002784weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002785{
Scott Moreau850ca422012-05-21 15:21:25 -06002786 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002787 struct weston_matrix camera;
2788 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002789
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002790 weston_matrix_init(&output->matrix);
2791 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002792 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
2793 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002794
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002795 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002796 2.0 / (output->width + output->border.left + output->border.right),
2797 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
2798
2799 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06002800
Scott Moreauccbf29d2012-02-22 14:21:41 -07002801 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002802 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002803 weston_matrix_init(&camera);
2804 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002805 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002806 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04002807 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002808 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002809 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002810 weston_matrix_multiply(&output->matrix, &modelview);
2811 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002812
Scott Moreauccbf29d2012-02-22 14:21:41 -07002813 output->dirty = 0;
2814}
2815
Scott Moreau1bad5db2012-08-18 01:04:05 -06002816static void
2817weston_output_transform_init(struct weston_output *output, uint32_t transform)
2818{
2819 output->transform = transform;
2820
2821 switch (transform) {
2822 case WL_OUTPUT_TRANSFORM_90:
2823 case WL_OUTPUT_TRANSFORM_270:
2824 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
2825 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
2826 /* Swap width and height */
2827 output->width = output->current->height;
2828 output->height = output->current->width;
2829 break;
2830 case WL_OUTPUT_TRANSFORM_NORMAL:
2831 case WL_OUTPUT_TRANSFORM_180:
2832 case WL_OUTPUT_TRANSFORM_FLIPPED:
2833 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
2834 output->width = output->current->width;
2835 output->height = output->current->height;
2836 break;
2837 default:
2838 break;
2839 }
2840}
2841
Scott Moreauccbf29d2012-02-22 14:21:41 -07002842WL_EXPORT void
2843weston_output_move(struct weston_output *output, int x, int y)
2844{
2845 output->x = x;
2846 output->y = y;
2847
Ander Conselvan de Oliveirab8fcca92012-11-16 17:23:52 +02002848 pixman_region32_init(&output->previous_damage);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002849 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002850 output->width,
2851 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002852}
2853
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002854WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002855weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06002856 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002857{
2858 output->compositor = c;
2859 output->x = x;
2860 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002861 output->border.top = 0;
2862 output->border.bottom = 0;
2863 output->border.left = 0;
2864 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002865 output->mm_width = width;
2866 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002867 output->dirty = 1;
2868
Scott Moreau1bad5db2012-08-18 01:04:05 -06002869 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06002870 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002871
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002872 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002873 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002874
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002875 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002876 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002877 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002878
Casey Dahlin58ba1372012-04-19 22:50:08 -04002879 output->id = ffs(~output->compositor->output_id_pool) - 1;
2880 output->compositor->output_id_pool |= 1 << output->id;
2881
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002882 output->global =
2883 wl_display_add_global(c->wl_display, &wl_output_interface,
2884 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002885}
2886
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002887static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002888compositor_bind(struct wl_client *client,
2889 void *data, uint32_t version, uint32_t id)
2890{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002891 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002892
2893 wl_client_add_object(client, &wl_compositor_interface,
2894 &compositor_interface, id, compositor);
2895}
2896
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002897static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002898log_uname(void)
2899{
2900 struct utsname usys;
2901
2902 uname(&usys);
2903
2904 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2905 usys.version, usys.machine);
2906}
2907
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002908WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002909weston_compositor_init(struct weston_compositor *ec,
2910 struct wl_display *display,
2911 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002912 char *argv[],
2913 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002914{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002915 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002916 struct xkb_rule_names xkb_names;
2917 const struct config_key keyboard_config_keys[] = {
2918 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2919 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2920 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2921 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2922 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2923 };
2924 const struct config_section cs[] = {
2925 { "keyboard",
2926 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2927 };
2928
2929 memset(&xkb_names, 0, sizeof(xkb_names));
2930 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002931
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002932 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002933 wl_signal_init(&ec->destroy_signal);
2934 wl_signal_init(&ec->activate_signal);
Tiago Vignatti1d01b012012-09-27 17:48:36 +03002935 wl_signal_init(&ec->kill_signal);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002936 wl_signal_init(&ec->lock_signal);
2937 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02002938 wl_signal_init(&ec->show_input_panel_signal);
2939 wl_signal_init(&ec->hide_input_panel_signal);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002940 wl_signal_init(&ec->seat_created_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002941 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002942
Casey Dahlin58ba1372012-04-19 22:50:08 -04002943 ec->output_id_pool = 0;
2944
Kristian Høgsberga8873122011-11-23 10:39:34 -05002945 if (!wl_display_add_global(display, &wl_compositor_interface,
2946 ec, compositor_bind))
2947 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002948
Daniel Stone725c2c32012-06-22 14:04:36 +01002949 wl_list_init(&ec->surface_list);
2950 wl_list_init(&ec->layer_list);
2951 wl_list_init(&ec->seat_list);
2952 wl_list_init(&ec->output_list);
2953 wl_list_init(&ec->key_binding_list);
2954 wl_list_init(&ec->button_binding_list);
2955 wl_list_init(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02002956 wl_list_init(&ec->debug_binding_list);
Daniel Stone725c2c32012-06-22 14:04:36 +01002957 wl_list_init(&ec->fade.animation.link);
2958
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002959 weston_plane_init(&ec->primary_plane, 0, 0);
2960
Daniel Stone725c2c32012-06-22 14:04:36 +01002961 weston_compositor_xkb_init(ec, &xkb_names);
2962
2963 ec->ping_handler = NULL;
2964
2965 screenshooter_create(ec);
2966 text_cursor_position_notifier_create(ec);
Jan Arne Petersen674fd1d2012-11-18 19:06:42 +01002967 text_backend_init(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01002968
2969 wl_data_device_manager_init(ec->wl_display);
2970
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002971 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002972
Daniel Stone725c2c32012-06-22 14:04:36 +01002973 loop = wl_display_get_event_loop(ec->wl_display);
2974 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
2975 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
2976
2977 ec->input_loop = wl_event_loop_create();
2978
Kristian Høgsberg861a50c2012-09-05 22:02:22 -04002979 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
2980 ec->fade.animation.frame = fade_frame;
2981
2982 weston_layer_init(&ec->fade_layer, &ec->layer_list);
2983 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
2984
2985 weston_compositor_schedule_repaint(ec);
2986
Daniel Stone725c2c32012-06-22 14:04:36 +01002987 return 0;
2988}
2989
Benjamin Franzkeb8263022011-08-30 11:32:47 +02002990WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002991weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07002992{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002993 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07002994
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002995 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02002996 if (ec->input_loop_source)
2997 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02002998
Matt Roper361d2ad2011-08-29 13:52:23 -07002999 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003000 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003001 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003002
Daniel Stone325fc2d2012-05-30 16:31:58 +01003003 weston_binding_list_destroy_all(&ec->key_binding_list);
3004 weston_binding_list_destroy_all(&ec->button_binding_list);
3005 weston_binding_list_destroy_all(&ec->axis_binding_list);
Ander Conselvan de Oliveirac509d2b2012-11-08 17:20:45 +02003006 weston_binding_list_destroy_all(&ec->debug_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003007
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003008 weston_plane_release(&ec->primary_plane);
3009
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003010 wl_array_release(&ec->vertices);
3011 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003012 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003013
3014 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003015}
3016
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003017static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003018{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003019 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003020
Martin Minarik6d118362012-06-07 18:01:59 +02003021 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003022 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003023
3024 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003025}
3026
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003027static void
3028on_segv_signal(int s, siginfo_t *siginfo, void *context)
3029{
3030 void *buffer[32];
3031 int i, count;
3032 Dl_info info;
3033
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003034 /* This SIGSEGV handler will do a best-effort backtrace, and
3035 * then call the backend restore function, which will switch
3036 * back to the vt we launched from or ungrab X etc and then
3037 * raise SIGTRAP. If we run weston under gdb from X or a
3038 * different vt, and tell gdb "handle SIGSEGV nostop", this
3039 * will allow weston to switch back to gdb on crash and then
3040 * gdb will catch the crash with SIGTRAP. */
3041
Martin Minarik6d118362012-06-07 18:01:59 +02003042 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003043
3044 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3045 for (i = 0; i < count; i++) {
3046 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003047 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003048 (long) buffer[i],
3049 info.dli_sname ? info.dli_sname : "--",
3050 info.dli_fname);
3051 }
3052
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003053 segv_compositor->restore(segv_compositor);
3054
3055 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003056}
3057
3058
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003059static void *
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003060load_module(const char *name, const char *entrypoint)
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003061{
3062 char path[PATH_MAX];
3063 void *module, *init;
3064
3065 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003066 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003067 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003068 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003069
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003070 module = dlopen(path, RTLD_NOW | RTLD_NOLOAD);
3071 if (module) {
3072 weston_log("Module '%s' already loaded\n", path);
3073 dlclose(module);
3074 return NULL;
3075 }
3076
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003077 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003078 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003079 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003080 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003081 return NULL;
3082 }
3083
3084 init = dlsym(module, entrypoint);
3085 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003086 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003087 return NULL;
3088 }
3089
3090 return init;
3091}
3092
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003093static int
3094load_modules(struct weston_compositor *ec, const char *modules)
3095{
3096 const char *p, *end;
3097 char buffer[256];
3098 int (*module_init)(struct weston_compositor *ec);
3099
3100 if (modules == NULL)
3101 return 0;
3102
3103 p = modules;
3104 while (*p) {
3105 end = strchrnul(p, ',');
3106 snprintf(buffer, sizeof buffer, "%.*s", (int) (end - p), p);
3107 module_init = load_module(buffer, "module_init");
3108 if (module_init)
3109 module_init(ec);
3110 p = end;
3111 while (*p == ',')
3112 p++;
3113
3114 }
3115
3116 return 0;
3117}
3118
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003119static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003120 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3121
3122static const char xdg_wrong_message[] =
3123 "fatal: environment variable XDG_RUNTIME_DIR\n"
3124 "is set to \"%s\", which is not a directory.\n";
3125
3126static const char xdg_wrong_mode_message[] =
3127 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3128 "correctly. Unix access mode must be 0700 but is %o,\n"
3129 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003130 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003131
3132static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003133 "Refer to your distribution on how to get it, or\n"
3134 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3135 "on how to implement it.\n";
3136
Martin Minarik37032f82012-06-18 20:15:18 +02003137static void
3138verify_xdg_runtime_dir(void)
3139{
3140 char *dir = getenv("XDG_RUNTIME_DIR");
3141 struct stat s;
3142
3143 if (!dir) {
3144 weston_log(xdg_error_message);
3145 weston_log_continue(xdg_detail_message);
3146 exit(EXIT_FAILURE);
3147 }
3148
3149 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3150 weston_log(xdg_wrong_message, dir);
3151 weston_log_continue(xdg_detail_message);
3152 exit(EXIT_FAILURE);
3153 }
3154
3155 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3156 weston_log(xdg_wrong_mode_message,
3157 dir, s.st_mode & 0777, s.st_uid);
3158 weston_log_continue(xdg_detail_message);
3159 }
3160}
3161
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003162static int
3163usage(int error_code)
3164{
3165 fprintf(stderr,
3166 "Usage: weston [OPTIONS]\n\n"
3167 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3168 "Weston supports multiple backends, and depending on which backend is in use\n"
3169 "different options will be accepted.\n\n"
3170
3171
3172 "Core options:\n\n"
3173 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3174 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3175 " -S, --socket=NAME\tName of socket to listen on\n"
3176 " -i, --idle-time=SECS\tIdle time in seconds\n"
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003177 " --modules\t\tLoad the comma-separated list of modules\n"
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003178 " --log==FILE\t\tLog to the given file\n"
3179 " -h, --help\t\tThis help message\n\n");
3180
3181 fprintf(stderr,
3182 "Options for drm-backend.so:\n\n"
3183 " --connector=ID\tBring up only this connector\n"
3184 " --seat=SEAT\t\tThe seat that weston should run on\n"
3185 " --tty=TTY\t\tThe tty to use\n"
3186 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3187
3188 fprintf(stderr,
3189 "Options for x11-backend.so:\n\n"
3190 " --width=WIDTH\t\tWidth of X window\n"
3191 " --height=HEIGHT\tHeight of X window\n"
3192 " --fullscreen\t\tRun in fullscreen mode\n"
3193 " --output-count=COUNT\tCreate multiple outputs\n"
3194 " --no-input\t\tDont create input devices\n\n");
3195
3196 fprintf(stderr,
3197 "Options for wayland-backend.so:\n\n"
3198 " --width=WIDTH\t\tWidth of Wayland surface\n"
3199 " --height=HEIGHT\tHeight of Wayland surface\n"
3200 " --display=DISPLAY\tWayland display to connect to\n\n");
3201
3202 exit(error_code);
3203}
3204
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003205int main(int argc, char *argv[])
3206{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003207 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003208 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003209 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003210 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003211 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003212 struct sigaction segv_action;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003213 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003214 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003215 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003216 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003217 char *backend = NULL;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003218 const char *modules = "desktop-shell.so", *option_modules = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003219 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003220 int32_t idle_time = 300;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003221 int32_t help = 0;
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003222 char *socket_name = "wayland-0";
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003223 char *config_file;
3224
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003225 const struct config_key core_config_keys[] = {
3226 { "modules", CONFIG_KEY_STRING, &modules },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003227 };
3228
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003229 const struct config_section cs[] = {
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003230 { "core",
3231 core_config_keys, ARRAY_LENGTH(core_config_keys) },
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003232 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003233
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003234 const struct weston_option core_options[] = {
3235 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3236 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3237 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003238 { WESTON_OPTION_STRING, "modules", 0, &option_modules },
Martin Minarik19e6f262012-06-07 13:08:46 +02003239 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003240 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003241 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003242
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003243 argc = parse_options(core_options,
3244 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003245
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003246 if (help)
3247 usage(EXIT_SUCCESS);
3248
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003249 weston_log_file_open(log);
3250
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003251 weston_log("%s\n"
3252 STAMP_SPACE "%s\n"
3253 STAMP_SPACE "Bug reports to: %s\n"
3254 STAMP_SPACE "Build: %s\n",
3255 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003256 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003257 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003258
Martin Minarik37032f82012-06-18 20:15:18 +02003259 verify_xdg_runtime_dir();
3260
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003261 display = wl_display_create();
3262
Tiago Vignatti2116b892011-08-08 05:52:59 -07003263 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003264 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3265 display);
3266 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3267 display);
3268 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3269 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003270
3271 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003272 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3273 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003274
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003275 if (!backend) {
3276 if (getenv("WAYLAND_DISPLAY"))
3277 backend = "wayland-backend.so";
3278 else if (getenv("DISPLAY"))
3279 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003280 else
Pekka Paalanena51e6fa2012-11-07 12:25:12 +02003281 backend = WESTON_NATIVE_BACKEND;
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003282 }
3283
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003284 config_file = config_file_path("weston.ini");
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003285 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), NULL);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003286
Kristian Høgsberga4624f62012-09-11 14:08:26 -04003287 backend_init = load_module(backend, "backend_init");
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003288 if (!backend_init)
3289 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003290
Daniel Stonec1be8e52012-06-01 11:14:02 -04003291 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003292 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003293 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003294 exit(EXIT_FAILURE);
3295 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003296
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003297 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3298 segv_action.sa_sigaction = on_segv_signal;
3299 sigemptyset(&segv_action.sa_mask);
3300 sigaction(SIGSEGV, &segv_action, NULL);
3301 segv_compositor = ec;
3302
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003303 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003304 weston_log("fatal: unhandled option: %s\n", argv[i]);
3305 if (argv[1]) {
3306 ret = EXIT_FAILURE;
3307 goto out;
3308 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003309
Daniel Stonec1be8e52012-06-01 11:14:02 -04003310 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003311
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003312 ec->option_idle_time = idle_time;
3313 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003314
Kristian Høgsbergeb00e2e2012-09-11 14:29:47 -04003315 setenv("WAYLAND_DISPLAY", socket_name, 1);
3316
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003317 if (load_modules(ec, modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003318 goto out;
Kristian Høgsberga6813d22012-09-12 12:21:01 -04003319 if (load_modules(ec, option_modules) < 0)
Pekka Paalanen33616392012-07-30 16:56:57 +03003320 goto out;
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003321
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003322 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003323 weston_log("fatal: failed to add socket: %m\n");
3324 ret = EXIT_FAILURE;
3325 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003326 }
3327
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003328 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003329 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003330
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003331 wl_display_run(display);
3332
3333 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003334 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003335 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003336
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003337 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003338
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003339 for (i = ARRAY_LENGTH(signals); i;)
3340 wl_event_source_remove(signals[--i]);
3341
Daniel Stone855028f2012-05-01 20:37:10 +01003342 weston_compositor_xkb_destroy(ec);
3343
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003344 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003345 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003346
Martin Minarik19e6f262012-06-07 13:08:46 +02003347 weston_log_file_close();
3348
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003349 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003350}