blob: 48b26a5aeed9cfebfaff412cf42879b6f886f5be [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>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040042#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050043#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040044#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040045#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040046#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050047#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040048#include <sys/time.h>
49#include <time.h>
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -040050#include <ctype.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"
Martin Minarik19e6f262012-06-07 13:08:46 +020055#include "log.h"
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040056#include "git-version.h"
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -050057
Kristian Høgsberg27da5382011-06-21 17:32:25 -040058static struct wl_list child_process_list;
Kristian Høgsberg0690da62012-01-16 11:53:54 -050059static jmp_buf segv_jmp_buf;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040060
61static int
62sigchld_handler(int signal_number, void *data)
63{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050064 struct weston_process *p;
Kristian Høgsberg27da5382011-06-21 17:32:25 -040065 int status;
66 pid_t pid;
67
Bill Spitzak027b9622012-03-17 15:22:03 -070068 pid = waitpid(-1, &status, WNOHANG);
69 if (!pid)
70 return 1;
71
Kristian Høgsberg27da5382011-06-21 17:32:25 -040072 wl_list_for_each(p, &child_process_list, link) {
73 if (p->pid == pid)
74 break;
75 }
76
77 if (&p->link == &child_process_list) {
Martin Minarik6d118362012-06-07 18:01:59 +020078 weston_log("unknown child process exited\n");
Kristian Høgsberg27da5382011-06-21 17:32:25 -040079 return 1;
80 }
81
82 wl_list_remove(&p->link);
83 p->cleanup(p, status);
84
85 return 1;
86}
87
Alex Wu2dda6042012-04-17 17:20:47 +080088WL_EXPORT int
89weston_output_switch_mode(struct weston_output *output, struct weston_mode *mode)
90{
91 if (!output->switch_mode)
92 return -1;
93
94 return output->switch_mode(output, mode);
95}
96
Kristian Høgsberg27da5382011-06-21 17:32:25 -040097WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050098weston_watch_process(struct weston_process *process)
Kristian Høgsberg27da5382011-06-21 17:32:25 -040099{
100 wl_list_insert(&child_process_list, &process->link);
101}
102
Benjamin Franzke06286262011-05-06 19:12:33 +0200103static void
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200104child_client_exec(int sockfd, const char *path)
105{
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500106 int clientfd;
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200107 char s[32];
Pekka Paalanenc47ddfd2011-12-08 10:44:56 +0200108 sigset_t allsigs;
109
110 /* do not give our signal mask to the new process */
111 sigfillset(&allsigs);
112 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200113
Kristian Høgsbergeb764842012-01-31 22:17:25 -0500114 /* Launch clients as the user. */
115 seteuid(getuid());
116
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500117 /* SOCK_CLOEXEC closes both ends, so we dup the fd to get a
118 * non-CLOEXEC fd to pass through exec. */
119 clientfd = dup(sockfd);
120 if (clientfd == -1) {
Martin Minarik6d118362012-06-07 18:01:59 +0200121 weston_log("compositor: dup failed: %m\n");
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500122 return;
123 }
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200124
Kristian Høgsbergd42b0c92011-12-08 10:19:40 -0500125 snprintf(s, sizeof s, "%d", clientfd);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200126 setenv("WAYLAND_SOCKET", s, 1);
127
128 if (execl(path, path, NULL) < 0)
Martin Minarik6d118362012-06-07 18:01:59 +0200129 weston_log("compositor: executing '%s' failed: %m\n",
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200130 path);
131}
132
133WL_EXPORT struct wl_client *
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500134weston_client_launch(struct weston_compositor *compositor,
135 struct weston_process *proc,
136 const char *path,
137 weston_process_cleanup_func_t cleanup)
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200138{
139 int sv[2];
140 pid_t pid;
141 struct wl_client *client;
142
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300143 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200144 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200145 "socketpair failed while launching '%s': %m\n",
146 path);
147 return NULL;
148 }
149
150 pid = fork();
151 if (pid == -1) {
152 close(sv[0]);
153 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200154 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200155 "fork failed while launching '%s': %m\n", path);
156 return NULL;
157 }
158
159 if (pid == 0) {
160 child_client_exec(sv[1], path);
161 exit(-1);
162 }
163
164 close(sv[1]);
165
166 client = wl_client_create(compositor->wl_display, sv[0]);
167 if (!client) {
168 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200169 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200170 "wl_client_create failed while launching '%s'.\n",
171 path);
172 return NULL;
173 }
174
175 proc->pid = pid;
176 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500177 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200178
179 return client;
180}
181
182static void
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400183update_shm_texture(struct weston_surface *surface);
184
185static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400186surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200187{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500188 struct weston_surface *es =
189 container_of(listener, struct weston_surface,
190 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200191
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400192 if (es->buffer && wl_buffer_is_shm(es->buffer))
193 update_shm_texture(es);
194
Kristian Høgsberg24596942011-10-24 17:51:02 -0400195 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200196}
197
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500198static const pixman_region32_data_t undef_region_data;
199
200static void
201undef_region(pixman_region32_t *region)
202{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400203 if (region->data != &undef_region_data)
204 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500205 region->data = (pixman_region32_data_t *) &undef_region_data;
206}
207
208static int
209region_is_undefined(pixman_region32_t *region)
210{
211 return region->data == &undef_region_data;
212}
213
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200214static void
215empty_region(pixman_region32_t *region)
216{
217 if (!region_is_undefined(region))
218 pixman_region32_fini(region);
219
220 pixman_region32_init(region);
221}
222
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500223WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500224weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500225{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500226 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400227
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200228 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229 if (surface == NULL)
230 return NULL;
231
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400232 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500233
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500234 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500235 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500236
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400237 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400238
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500239 surface->compositor = compositor;
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200240 surface->image = EGL_NO_IMAGE_KHR;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400241 surface->alpha = 1.0;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400242 surface->blend = 1;
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -0400243 surface->opaque_rect[0] = 0.0;
244 surface->opaque_rect[1] = 0.0;
245 surface->opaque_rect[2] = 0.0;
246 surface->opaque_rect[3] = 0.0;
Juan Zhao46436612012-03-20 01:48:46 +0800247 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400248
Benjamin Franzke06286262011-05-06 19:12:33 +0200249 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400250 surface->output = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200251
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400252 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500253 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500254 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500255 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200256 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500257 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400258
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400259 surface->buffer_destroy_listener.notify =
260 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200261
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200262 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200263 wl_list_insert(&surface->geometry.transformation_list,
264 &surface->transform.position.link);
265 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200266 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200267 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500268
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400269 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500270}
271
Alex Wu8811bf92012-02-28 18:07:54 +0800272WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500273weston_surface_set_color(struct weston_surface *surface,
274 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
275{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500276 surface->color[0] = red;
277 surface->color[1] = green;
278 surface->color[2] = blue;
279 surface->color[3] = alpha;
280 surface->shader = &surface->compositor->solid_shader;
281}
282
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400283WL_EXPORT void
284weston_surface_to_global_float(struct weston_surface *surface,
285 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200286{
287 if (surface->transform.enabled) {
288 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
289
290 weston_matrix_transform(&surface->transform.matrix, &v);
291
292 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200293 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200294 "weston_surface_to_global(), divisor = %g\n",
295 v.f[3]);
296 *x = 0;
297 *y = 0;
298 return;
299 }
300
301 *x = v.f[0] / v.f[3];
302 *y = v.f[1] / v.f[3];
303 } else {
304 *x = sx + surface->geometry.x;
305 *y = sy + surface->geometry.y;
306 }
307}
308
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500309WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500310weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200311{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500312 struct weston_compositor *compositor = surface->compositor;
313 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200314
Kristian Høgsbergd553bfc2012-06-18 22:37:35 -0400315 if (surface->plane != WESTON_PLANE_PRIMARY)
316 return;
317
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500318 pixman_region32_init(&damage);
319 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
320 &surface->clip);
321 pixman_region32_union(&compositor->damage,
322 &compositor->damage, &damage);
323 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200324}
325
326static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200327surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
328 int32_t width, int32_t height,
329 pixman_region32_t *bbox)
330{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200331 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
332 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200333 int32_t s[4][2] = {
334 { sx, sy },
335 { sx, sy + height },
336 { sx + width, sy },
337 { sx + width, sy + height }
338 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200339 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200340 int i;
341
342 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200343 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400344 weston_surface_to_global_float(surface,
345 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200346 if (x < min_x)
347 min_x = x;
348 if (x > max_x)
349 max_x = x;
350 if (y < min_y)
351 min_y = y;
352 if (y > max_y)
353 max_y = y;
354 }
355
Pekka Paalanen219b9822012-02-08 15:38:37 +0200356 int_x = floorf(min_x);
357 int_y = floorf(min_y);
358 pixman_region32_init_rect(bbox, int_x, int_y,
359 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200360}
361
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200362static void
363weston_surface_update_transform_disable(struct weston_surface *surface)
364{
365 surface->transform.enabled = 0;
366
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200367 /* round off fractions when not transformed */
368 surface->geometry.x = roundf(surface->geometry.x);
369 surface->geometry.y = roundf(surface->geometry.y);
370
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200371 pixman_region32_init_rect(&surface->transform.boundingbox,
372 surface->geometry.x,
373 surface->geometry.y,
374 surface->geometry.width,
375 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500376
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400377 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500378 pixman_region32_copy(&surface->transform.opaque,
379 &surface->opaque);
380 pixman_region32_translate(&surface->transform.opaque,
381 surface->geometry.x,
382 surface->geometry.y);
383 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200384}
385
386static int
387weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200388{
389 struct weston_matrix *matrix = &surface->transform.matrix;
390 struct weston_matrix *inverse = &surface->transform.inverse;
391 struct weston_transform *tform;
392
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200393 surface->transform.enabled = 1;
394
395 /* Otherwise identity matrix, but with x and y translation. */
396 surface->transform.position.matrix.d[12] = surface->geometry.x;
397 surface->transform.position.matrix.d[13] = surface->geometry.y;
398
399 weston_matrix_init(matrix);
400 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
401 weston_matrix_multiply(matrix, &tform->matrix);
402
403 if (weston_matrix_invert(inverse, matrix) < 0) {
404 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200405 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200406 " transformation not invertible.\n", surface);
407 return -1;
408 }
409
410 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
411 surface->geometry.height,
412 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500413
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200414 return 0;
415}
416
417WL_EXPORT void
418weston_surface_update_transform(struct weston_surface *surface)
419{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200420 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200421 return;
422
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200423 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200424
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500425 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200426
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200427 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500428 pixman_region32_fini(&surface->transform.opaque);
429 pixman_region32_init(&surface->transform.opaque);
430
431 if (region_is_undefined(&surface->input))
432 pixman_region32_init_rect(&surface->input, 0, 0,
433 surface->geometry.width,
434 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200435
Pekka Paalanencd403622012-01-25 13:37:39 +0200436 /* transform.position is always in transformation_list */
437 if (surface->geometry.transformation_list.next ==
438 &surface->transform.position.link &&
439 surface->geometry.transformation_list.prev ==
440 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200441 weston_surface_update_transform_disable(surface);
442 } else {
443 if (weston_surface_update_transform_enable(surface) < 0)
444 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200445 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200446
447 /* weston_surface_damage() without update */
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400448 pixman_region32_union_rect(&surface->damage, &surface->damage,
449 0, 0, surface->geometry.width,
450 surface->geometry.height);
Pekka Paalanen96516782012-02-09 15:32:15 +0200451
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300452 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200453 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200454}
455
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200456WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100457weston_surface_to_global_fixed(struct weston_surface *surface,
458 wl_fixed_t sx, wl_fixed_t sy,
459 wl_fixed_t *x, wl_fixed_t *y)
460{
461 GLfloat xf, yf;
462
463 weston_surface_to_global_float(surface,
464 wl_fixed_to_double(sx),
465 wl_fixed_to_double(sy),
466 &xf, &yf);
467 *x = wl_fixed_from_double(xf);
468 *y = wl_fixed_from_double(yf);
469}
470
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200471static void
472surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100473 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200474{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200475 if (surface->transform.enabled) {
476 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
477
478 weston_matrix_transform(&surface->transform.inverse, &v);
479
480 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200481 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200482 "weston_surface_from_global(), divisor = %g\n",
483 v.f[3]);
484 *sx = 0;
485 *sy = 0;
486 return;
487 }
488
Pekka Paalanencd403622012-01-25 13:37:39 +0200489 *sx = v.f[0] / v.f[3];
490 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200491 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200492 *sx = x - surface->geometry.x;
493 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200494 }
495}
496
497WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100498weston_surface_from_global_fixed(struct weston_surface *surface,
499 wl_fixed_t x, wl_fixed_t y,
500 wl_fixed_t *sx, wl_fixed_t *sy)
501{
502 GLfloat sxf, syf;
503
Daniel Stonebd3489b2012-05-08 17:17:53 +0100504 surface_from_global_float(surface,
505 wl_fixed_to_double(x),
506 wl_fixed_to_double(y),
507 &sxf, &syf);
508 *sx = wl_fixed_from_double(sxf);
509 *sy = wl_fixed_from_double(syf);
510}
511
512WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200513weston_surface_from_global(struct weston_surface *surface,
514 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
515{
516 GLfloat sxf, syf;
517
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200518 surface_from_global_float(surface, x, y, &sxf, &syf);
519 *sx = floorf(sxf);
520 *sy = floorf(syf);
521}
522
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400523WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500524weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500525{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400526 pixman_region32_union_rect(&surface->damage, &surface->damage,
527 0, 0, surface->geometry.width,
528 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200529
530 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500531}
532
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400533WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500534weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200535 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400536{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200537 surface->geometry.x = x;
538 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200539 surface->geometry.width = width;
540 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200541 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400542}
543
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200544WL_EXPORT void
545weston_surface_set_position(struct weston_surface *surface,
546 GLfloat x, GLfloat y)
547{
548 surface->geometry.x = x;
549 surface->geometry.y = y;
550 surface->geometry.dirty = 1;
551}
552
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300553WL_EXPORT int
554weston_surface_is_mapped(struct weston_surface *surface)
555{
556 if (surface->output)
557 return 1;
558 else
559 return 0;
560}
561
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400562WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500563weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500564{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400565 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500566
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400567 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500568
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400569 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500570}
571
Tiago Vignatti9d393522012-02-10 16:26:19 +0200572static struct weston_surface *
573weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100574 wl_fixed_t x, wl_fixed_t y,
575 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200576{
577 struct weston_surface *surface;
578
579 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100580 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500581 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100582 wl_fixed_to_int(*sx),
583 wl_fixed_to_int(*sy),
584 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200585 return surface;
586 }
587
588 return NULL;
589}
590
591static void
Daniel Stone37816df2012-05-16 18:45:18 +0100592weston_device_repick(struct wl_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500593{
Daniel Stone37816df2012-05-16 18:45:18 +0100594 struct weston_seat *ws = (struct weston_seat *) seat;
Scott Moreau447013d2012-02-18 05:05:29 -0700595 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500596 struct weston_surface *surface, *focus;
597
Daniel Stonee9e92d22012-06-04 11:40:47 +0100598 if (!seat->pointer)
599 return;
600
Daniel Stone37816df2012-05-16 18:45:18 +0100601 surface = weston_compositor_pick_surface(ws->compositor,
602 seat->pointer->x,
603 seat->pointer->y,
604 &seat->pointer->current_x,
605 &seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500606
Daniel Stone37816df2012-05-16 18:45:18 +0100607 if (&surface->surface != seat->pointer->current) {
608 interface = seat->pointer->grab->interface;
Kristian Høgsberg4e99ac42012-06-04 16:12:38 -0400609 seat->pointer->current = &surface->surface;
Daniel Stone37816df2012-05-16 18:45:18 +0100610 interface->focus(seat->pointer->grab, &surface->surface,
611 seat->pointer->current_x,
612 seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500613 }
614
Daniel Stone37816df2012-05-16 18:45:18 +0100615 focus = (struct weston_surface *) seat->pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500616 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100617 weston_surface_from_global_fixed(focus,
618 seat->pointer->x,
619 seat->pointer->y,
620 &seat->pointer->grab->x,
621 &seat->pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500622}
623
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400624static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500625weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400626{
Daniel Stone37816df2012-05-16 18:45:18 +0100627 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400628
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500629 if (!compositor->focus)
630 return;
631
Daniel Stone37816df2012-05-16 18:45:18 +0100632 wl_list_for_each(seat, &compositor->seat_list, link)
633 weston_device_repick(&seat->seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400634}
635
636static void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500637weston_surface_unmap(struct weston_surface *surface)
638{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100639 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500640
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500641 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500642 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500643 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500644
Daniel Stone4dab5db2012-05-30 16:31:53 +0100645 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100646 if (seat->seat.keyboard &&
647 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100648 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100649 if (seat->seat.pointer &&
650 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100651 wl_pointer_set_focus(seat->seat.pointer,
652 NULL,
653 wl_fixed_from_int(0),
654 wl_fixed_from_int(0));
655 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500656
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500657 weston_compositor_schedule_repaint(surface->compositor);
658}
659
660static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400661destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500662{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500663 struct weston_surface *surface =
664 container_of(resource,
665 struct weston_surface, surface.resource);
666 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400667
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300668 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500669 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400670
Kristian Høgsberg12bbf812012-02-17 12:15:27 -0500671 if (surface->texture)
672 glDeleteTextures(1, &surface->texture);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200673
Benjamin Franzke06286262011-05-06 19:12:33 +0200674 if (surface->buffer)
675 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400676
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200677 if (surface->image != EGL_NO_IMAGE_KHR)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400678 compositor->destroy_image(compositor->egl_display,
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400679 surface->image);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200680
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200681 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200682 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500683 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500684 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500685 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500686 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200687
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400688 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500689}
690
Alex Wu8811bf92012-02-28 18:07:54 +0800691WL_EXPORT void
692weston_surface_destroy(struct weston_surface *surface)
693{
694 /* Not a valid way to destroy a client surface */
695 assert(surface->surface.resource.client == NULL);
696
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400697 wl_signal_emit(&surface->surface.resource.destroy_signal,
698 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800699 destroy_surface(&surface->surface.resource);
700}
701
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100702static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400703weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100704{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500705 struct weston_surface *es = (struct weston_surface *) surface;
706 struct weston_compositor *ec = es->compositor;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100707
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300708 if (es->buffer) {
709 weston_buffer_post_release(es->buffer);
710 wl_list_remove(&es->buffer_destroy_listener.link);
711 }
712
713 es->buffer = buffer;
714
715 if (!buffer) {
716 if (weston_surface_is_mapped(es))
717 weston_surface_unmap(es);
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400718 if (es->image != EGL_NO_IMAGE_KHR) {
719 ec->destroy_image(ec->egl_display, es->image);
720 es->image = NULL;
721 }
722 if (es->texture) {
723 glDeleteTextures(1, &es->texture);
724 es->texture = 0;
725 }
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300726 return;
727 }
728
729 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400730 wl_signal_add(&es->buffer->resource.destroy_signal,
731 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300732
733 if (es->geometry.width != buffer->width ||
734 es->geometry.height != buffer->height) {
735 undef_region(&es->input);
736 pixman_region32_fini(&es->opaque);
737 pixman_region32_init(&es->opaque);
738 }
739
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500740 if (!es->texture) {
741 glGenTextures(1, &es->texture);
742 glBindTexture(GL_TEXTURE_2D, es->texture);
743 glTexParameteri(GL_TEXTURE_2D,
744 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
745 glTexParameteri(GL_TEXTURE_2D,
746 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
747 es->shader = &ec->texture_shader;
748 } else {
749 glBindTexture(GL_TEXTURE_2D, es->texture);
750 }
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100751
752 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200753 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400754 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
755 es->pitch, es->buffer->height, 0,
756 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400757 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
758 es->blend = 0;
759 else
760 es->blend = 1;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100761 } else {
Benjamin Franzkefb4b5a22011-06-21 10:44:37 +0200762 if (es->image != EGL_NO_IMAGE_KHR)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400763 ec->destroy_image(ec->egl_display, es->image);
764 es->image = ec->create_image(ec->egl_display, NULL,
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400765 EGL_WAYLAND_BUFFER_WL,
766 buffer, NULL);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300767
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -0400768 ec->image_target_texture_2d(GL_TEXTURE_2D, es->image);
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400769
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300770 es->pitch = buffer->width;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100771 }
772}
773
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400774static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500775texture_region(struct weston_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500776{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500777 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500778 GLfloat *v, inv_width, inv_height;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200779 GLfloat sx, sy;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500780 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400781 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500782 int i, n;
783
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400784 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500785 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
786 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200787 inv_width = 1.0 / es->pitch;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200788 inv_height = 1.0 / es->geometry.height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400789
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500790 for (i = 0; i < n; i++, v += 16, p += 6) {
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200791 surface_from_global_float(es, rectangles[i].x1,
792 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500793 v[ 0] = rectangles[i].x1;
794 v[ 1] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200795 v[ 2] = sx * inv_width;
796 v[ 3] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500797
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200798 surface_from_global_float(es, rectangles[i].x1,
799 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500800 v[ 4] = rectangles[i].x1;
801 v[ 5] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200802 v[ 6] = sx * inv_width;
803 v[ 7] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500804
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200805 surface_from_global_float(es, rectangles[i].x2,
806 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500807 v[ 8] = rectangles[i].x2;
808 v[ 9] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200809 v[10] = sx * inv_width;
810 v[11] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500811
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200812 surface_from_global_float(es, rectangles[i].x2,
813 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500814 v[12] = rectangles[i].x2;
815 v[13] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200816 v[14] = sx * inv_width;
817 v[15] = sy * inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500818
819 p[0] = i * 4 + 0;
820 p[1] = i * 4 + 1;
821 p[2] = i * 4 + 2;
822 p[3] = i * 4 + 2;
823 p[4] = i * 4 + 1;
824 p[5] = i * 4 + 3;
825 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500826
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400827 return n;
828}
829
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500830WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500831weston_surface_draw(struct weston_surface *es, struct weston_output *output,
832 pixman_region32_t *damage)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400833{
Scott Moreau9fb98242012-05-22 10:18:50 -0600834 GLfloat surface_rect[4] = { 0.0, 1.0, 0.0, 1.0 };
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500835 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400836 GLfloat *v;
837 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400838 GLint filter;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400839 int n;
840
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200841 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500842 pixman_region32_intersect(&repaint,
843 &es->transform.boundingbox, damage);
844 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +0200845
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400846 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200847 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400848
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500849 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Scott Moreau9fb98242012-05-22 10:18:50 -0600850 if (es->blend || es->alpha < 1.0)
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400851 glEnable(GL_BLEND);
852 else
853 glDisable(GL_BLEND);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400854
Kristian Høgsberg07632622012-01-25 23:02:06 -0500855 if (ec->current_shader != es->shader) {
856 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -0500857 ec->current_shader = es->shader;
858 }
859
Kristian Høgsberg0452abc2012-01-31 15:38:36 -0500860 glUniformMatrix4fv(es->shader->proj_uniform,
861 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200862 glUniform1i(es->shader->tex_uniform, 0);
863 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400864 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200865 glUniform1f(es->shader->texwidth_uniform,
866 (GLfloat)es->geometry.width / es->pitch);
Scott Moreau9fb98242012-05-22 10:18:50 -0600867 if (es->blend)
868 glUniform4fv(es->shader->opaque_uniform, 1, es->opaque_rect);
869 else
870 glUniform4fv(es->shader->opaque_uniform, 1, surface_rect);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200871
Scott Moreauccbf29d2012-02-22 14:21:41 -0700872 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400873 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200874 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200875 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200876
877 n = texture_region(es, &repaint);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400878
Kristian Høgsbergaa5b5be2008-11-21 21:31:54 -0500879 glBindTexture(GL_TEXTURE_2D, es->texture);
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400880 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
881 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
882
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500883 v = ec->vertices.data;
884 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
885 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400886 glEnableVertexAttribArray(0);
887 glEnableVertexAttribArray(1);
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200888
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500889 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
890
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200891 glDisableVertexAttribArray(1);
892 glDisableVertexAttribArray(0);
893
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500894 ec->vertices.size = 0;
895 ec->indices.size = 0;
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200896
897out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500898 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500899}
900
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500901WL_EXPORT void
902weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400903{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500904 wl_list_remove(&surface->layer_link);
905 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500906 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500907 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400908}
909
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400910WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500911weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400912{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500913 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400914
915 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500916 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400917}
918
Kristian Høgsberg9f404b72012-01-26 00:11:01 -0500919WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500920weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +0200921{
Kristian Høgsberg24596942011-10-24 17:51:02 -0400922 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +0200923 return;
Benjamin Franzke06286262011-05-06 19:12:33 +0200924
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400925 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -0500926 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +0200927}
928
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400929WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500930weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400931{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500932 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400933
Kristian Høgsberg944236a2012-02-28 23:07:47 -0500934 pixman_region32_union(&compositor->damage,
935 &compositor->damage, &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -0400936 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400937}
938
Kristian Høgsberg01f941b2009-05-27 17:47:15 -0400939static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500940fade_frame(struct weston_animation *animation,
941 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400942{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500943 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400944 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500945 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500946 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400947
Scott Moreau94b0b0c2012-06-14 01:01:56 -0600948 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -0600949 compositor->fade.spring.timestamp = msecs;
950
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500951 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500952 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500953 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
954 compositor->fade.spring.current);
955 weston_surface_damage(surface);
956
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500957 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -0400958 compositor->fade.spring.current =
959 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400960 wl_list_remove(&animation->link);
961 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200962
Kristian Høgsberg681f9f42012-01-26 10:55:10 -0500963 if (compositor->fade.spring.current < 0.001) {
964 destroy_surface(&surface->surface.resource);
965 compositor->fade.surface = NULL;
966 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500967 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400968 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +0200969 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400970 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -0400971}
972
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400973static void
974update_shm_texture(struct weston_surface *surface)
975{
976#ifdef GL_UNPACK_ROW_LENGTH
977 pixman_box32_t *rectangles;
978 void *data;
979 int i, n;
980#endif
981
982 glBindTexture(GL_TEXTURE_2D, surface->texture);
983
984 if (!surface->compositor->has_unpack_subimage) {
985 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
986 surface->pitch, surface->buffer->height, 0,
987 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
988 wl_shm_buffer_get_data(surface->buffer));
989
990 return;
991 }
992
993#ifdef GL_UNPACK_ROW_LENGTH
994 /* Mesa does not define GL_EXT_unpack_subimage */
995 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
996 data = wl_shm_buffer_get_data(surface->buffer);
997 rectangles = pixman_region32_rectangles(&surface->damage, &n);
998 for (i = 0; i < n; i++) {
999 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1000 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1001 glTexSubImage2D(GL_TEXTURE_2D, 0,
1002 rectangles[i].x1, rectangles[i].y1,
1003 rectangles[i].x2 - rectangles[i].y1,
1004 rectangles[i].y2 - rectangles[i].y1,
1005 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1006 }
1007#endif
1008}
1009
1010static void
1011surface_accumulate_damage(struct weston_surface *surface,
1012 pixman_region32_t *new_damage,
1013 pixman_region32_t *opaque)
1014{
1015 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1016 update_shm_texture(surface);
1017
1018 if (surface->transform.enabled) {
1019 pixman_box32_t *extents;
1020
1021 extents = pixman_region32_extents(&surface->damage);
1022 surface_compute_bbox(surface, extents->x1, extents->y1,
1023 extents->x2 - extents->x1,
1024 extents->y2 - extents->y1,
1025 &surface->damage);
1026 } else {
1027 pixman_region32_translate(&surface->damage,
1028 surface->geometry.x,
1029 surface->geometry.y);
1030 }
1031
1032 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
1033 pixman_region32_union(new_damage, new_damage, &surface->damage);
1034 empty_region(&surface->damage);
1035 pixman_region32_copy(&surface->clip, opaque);
1036 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
1037}
1038
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001039struct weston_frame_callback {
1040 struct wl_resource resource;
1041 struct wl_list link;
1042};
1043
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001044static void
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001045weston_output_repaint(struct weston_output *output, int 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;
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001053 pixman_region32_t opaque, new_damage, output_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001054 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001055
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001056 weston_compositor_update_drag_surfaces(ec);
1057
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001058 width = output->current->width +
1059 output->border.left + output->border.right;
1060 height = output->current->height +
1061 output->border.top + output->border.bottom;
1062 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001063
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001064 /* Rebuild the surface list and update surface transforms up front. */
1065 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001066 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001067 wl_list_for_each(layer, &ec->layer_list, link) {
1068 wl_list_for_each(es, &layer->surface_list, layer_link) {
1069 weston_surface_update_transform(es);
1070 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001071 if (es->output == output) {
1072 wl_list_insert_list(&frame_callback_list,
1073 &es->frame_callback_list);
1074 wl_list_init(&es->frame_callback_list);
1075 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001076 }
1077 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001078
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001079 if (output->assign_planes)
1080 /*
1081 * This will queue flips for the fbs and sprites where
1082 * applicable and clear the damage for those surfaces.
1083 * The repaint loop below will repaint everything
1084 * else.
1085 */
1086 output->assign_planes(output);
1087
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001088 pixman_region32_init(&new_damage);
1089 pixman_region32_init(&opaque);
1090
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001091 wl_list_for_each(es, &ec->surface_list, link)
1092 surface_accumulate_damage(es, &new_damage, &opaque);
Kristian Høgsberga82c4862012-01-26 01:03:58 -05001093
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001094 pixman_region32_union(&ec->damage, &ec->damage, &new_damage);
1095
1096 pixman_region32_init(&output_damage);
1097 pixman_region32_union(&output_damage,
1098 &ec->damage, &output->previous_damage);
1099 pixman_region32_copy(&output->previous_damage, &ec->damage);
1100 pixman_region32_intersect(&output_damage,
1101 &output_damage, &output->region);
1102 pixman_region32_subtract(&ec->damage, &ec->damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001103
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001104 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001105 pixman_region32_fini(&new_damage);
1106
Scott Moreauccbf29d2012-02-22 14:21:41 -07001107 if (output->dirty)
1108 weston_output_update_matrix(output);
1109
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001110 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001111
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001112 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001113
Kristian Høgsbergef044142011-06-21 15:02:12 -04001114 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001115
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001116 weston_compositor_repick(ec);
1117 wl_event_loop_dispatch(ec->input_loop, 0);
1118
Jonas Ådahldb773762012-06-13 00:01:21 +02001119 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001120 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001121 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001122 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001123 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001124
Scott Moreaud64cf212012-06-08 19:40:54 -06001125 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001126 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001127 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001128 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001129}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001130
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001131static int
1132weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001133{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001134 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001135
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001136 wl_event_loop_dispatch(compositor->input_loop, 0);
1137
1138 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001139}
1140
1141WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001142weston_output_finish_frame(struct weston_output *output, int msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001143{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001144 struct weston_compositor *compositor = output->compositor;
1145 struct wl_event_loop *loop =
1146 wl_display_get_event_loop(compositor->wl_display);
1147 int fd;
1148
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001149 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001150 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001151 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001152 return;
1153 }
1154
1155 output->repaint_scheduled = 0;
1156 if (compositor->input_loop_source)
1157 return;
1158
1159 fd = wl_event_loop_get_fd(compositor->input_loop);
1160 compositor->input_loop_source =
1161 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1162 weston_compositor_read_input, compositor);
1163}
1164
1165static void
1166idle_repaint(void *data)
1167{
1168 struct weston_output *output = data;
1169
1170 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001171}
1172
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001173WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001174weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1175{
1176 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001177 if (below != NULL)
1178 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001179}
1180
1181WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001182weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001183{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001184 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001185 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001186
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001187 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001188 return;
1189
Kristian Høgsbergef044142011-06-21 15:02:12 -04001190 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001191 output->repaint_needed = 1;
1192 if (output->repaint_scheduled)
1193 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001194
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001195 wl_event_loop_add_idle(loop, idle_repaint, output);
1196 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001197
1198 if (compositor->input_loop_source) {
1199 wl_event_source_remove(compositor->input_loop_source);
1200 compositor->input_loop_source = NULL;
1201 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001202}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001203
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001204WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001205weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1206{
1207 struct weston_output *output;
1208
1209 wl_list_for_each(output, &compositor->output_list, link)
1210 weston_output_schedule_repaint(output);
1211}
1212
1213WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001214weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001215{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001216 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001217 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001218
Scott Moreau9d1b1122012-06-08 19:40:53 -06001219 output = container_of(compositor->output_list.next,
1220 struct weston_output, link);
1221
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001222 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001223 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001224 return;
1225
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001226 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001227 surface = weston_surface_create(compositor);
1228 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001229 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001230 wl_list_insert(&compositor->fade_layer.surface_list,
1231 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001232 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001233 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001234 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001235 }
1236
1237 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001238 if (wl_list_empty(&compositor->fade.animation.link)) {
1239 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001240 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001241 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001242 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001243}
1244
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001245static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001246surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001247{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001248 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001249}
1250
Casey Dahlin96d8a752012-04-19 22:50:07 -04001251static struct wl_resource *
1252find_resource_for_client(struct wl_list *list, struct wl_client *client)
1253{
1254 struct wl_resource *r;
1255
1256 wl_list_for_each(r, list, link) {
1257 if (r->client == client)
1258 return r;
1259 }
1260
1261 return NULL;
1262}
1263
Casey Dahlin9074db52012-04-19 22:50:09 -04001264static void
1265weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1266{
1267 uint32_t different = es->output_mask ^ mask;
1268 uint32_t entered = mask & different;
1269 uint32_t left = es->output_mask & different;
1270 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001271 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001272 struct wl_client *client = es->surface.resource.client;
1273
1274 if (es->surface.resource.client == NULL)
1275 return;
1276 if (different == 0)
1277 return;
1278
1279 es->output_mask = mask;
1280 wl_list_for_each(output, &es->compositor->output_list, link) {
1281 if (1 << output->id & different)
1282 resource =
1283 find_resource_for_client(&output->resource_list,
1284 client);
1285 if (1 << output->id & entered)
1286 wl_surface_send_enter(&es->surface.resource, resource);
1287 if (1 << output->id & left)
1288 wl_surface_send_leave(&es->surface.resource, resource);
1289 }
1290}
1291
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001292WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001293weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001294{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001295 struct weston_compositor *ec = es->compositor;
1296 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001297 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001298 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001299 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001300
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001301 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001302 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001303 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001304 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001305 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001306 pixman_region32_intersect(&region, &es->transform.boundingbox,
1307 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001308
1309 e = pixman_region32_extents(&region);
1310 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1311
Casey Dahlin9074db52012-04-19 22:50:09 -04001312 if (area > 0)
1313 mask |= 1 << output->id;
1314
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001315 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001316 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001317 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001318 }
1319 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001320 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001321
1322 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001323 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001324}
1325
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001326static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001327surface_attach(struct wl_client *client,
1328 struct wl_resource *resource,
1329 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1330{
1331 struct weston_surface *es = resource->data;
1332 struct wl_buffer *buffer = NULL;
1333
1334 if (buffer_resource)
1335 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001336
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001337 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001338
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001339 if (buffer && es->configure)
1340 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001341}
1342
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001343static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001344surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001345 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001346 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001347{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001348 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001349
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001350 pixman_region32_union_rect(&es->damage, &es->damage,
1351 x, y, width, height);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04001352
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001353 weston_compositor_schedule_repaint(es->compositor);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001354}
1355
Kristian Høgsberg33418202011-08-16 23:01:28 -04001356static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001357destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001358{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001359 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001360
1361 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001362 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001363}
1364
1365static void
1366surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001367 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001368{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001369 struct weston_frame_callback *cb;
1370 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001371
1372 cb = malloc(sizeof *cb);
1373 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001374 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001375 return;
1376 }
1377
1378 cb->resource.object.interface = &wl_callback_interface;
1379 cb->resource.object.id = callback;
1380 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001381 cb->resource.client = client;
1382 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001383
1384 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001385 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001386}
1387
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001388static void
1389surface_set_opaque_region(struct wl_client *client,
1390 struct wl_resource *resource,
1391 struct wl_resource *region_resource)
1392{
1393 struct weston_surface *surface = resource->data;
1394 struct weston_region *region;
1395
1396 pixman_region32_fini(&surface->opaque);
1397
1398 if (region_resource) {
1399 region = region_resource->data;
1400 pixman_region32_init_rect(&surface->opaque, 0, 0,
1401 surface->geometry.width,
1402 surface->geometry.height);
1403 pixman_region32_intersect(&surface->opaque,
1404 &surface->opaque, &region->region);
1405 } else {
1406 pixman_region32_init(&surface->opaque);
1407 }
1408
1409 surface->geometry.dirty = 1;
1410}
1411
1412static void
1413surface_set_input_region(struct wl_client *client,
1414 struct wl_resource *resource,
1415 struct wl_resource *region_resource)
1416{
1417 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001418 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001419
1420 if (region_resource) {
1421 region = region_resource->data;
1422 pixman_region32_init_rect(&surface->input, 0, 0,
1423 surface->geometry.width,
1424 surface->geometry.height);
1425 pixman_region32_intersect(&surface->input,
1426 &surface->input, &region->region);
1427 } else {
1428 pixman_region32_init_rect(&surface->input, 0, 0,
1429 surface->geometry.width,
1430 surface->geometry.height);
1431 }
1432
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001433 weston_compositor_schedule_repaint(surface->compositor);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001434}
1435
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001436static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001437 surface_destroy,
1438 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001439 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001440 surface_frame,
1441 surface_set_opaque_region,
1442 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001443};
1444
1445static void
1446compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001447 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001448{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001449 struct weston_compositor *ec = resource->data;
1450 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001451
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001452 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001453 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001454 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001455 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001456 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001457
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001458 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001459
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001460 surface->surface.resource.object.id = id;
1461 surface->surface.resource.object.interface = &wl_surface_interface;
1462 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001463 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001464 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001465
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001466 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001467}
1468
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001469static void
1470destroy_region(struct wl_resource *resource)
1471{
1472 struct weston_region *region =
1473 container_of(resource, struct weston_region, resource);
1474
1475 pixman_region32_fini(&region->region);
1476 free(region);
1477}
1478
1479static void
1480region_destroy(struct wl_client *client, struct wl_resource *resource)
1481{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001482 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001483}
1484
1485static void
1486region_add(struct wl_client *client, struct wl_resource *resource,
1487 int32_t x, int32_t y, int32_t width, int32_t height)
1488{
1489 struct weston_region *region = resource->data;
1490
1491 pixman_region32_union_rect(&region->region, &region->region,
1492 x, y, width, height);
1493}
1494
1495static void
1496region_subtract(struct wl_client *client, struct wl_resource *resource,
1497 int32_t x, int32_t y, int32_t width, int32_t height)
1498{
1499 struct weston_region *region = resource->data;
1500 pixman_region32_t rect;
1501
1502 pixman_region32_init_rect(&rect, x, y, width, height);
1503 pixman_region32_subtract(&region->region, &region->region, &rect);
1504 pixman_region32_fini(&rect);
1505}
1506
1507static const struct wl_region_interface region_interface = {
1508 region_destroy,
1509 region_add,
1510 region_subtract
1511};
1512
1513static void
1514compositor_create_region(struct wl_client *client,
1515 struct wl_resource *resource, uint32_t id)
1516{
1517 struct weston_region *region;
1518
1519 region = malloc(sizeof *region);
1520 if (region == NULL) {
1521 wl_resource_post_no_memory(resource);
1522 return;
1523 }
1524
1525 region->resource.destroy = destroy_region;
1526
1527 region->resource.object.id = id;
1528 region->resource.object.interface = &wl_region_interface;
1529 region->resource.object.implementation =
1530 (void (**)(void)) &region_interface;
1531 region->resource.data = region;
1532
1533 pixman_region32_init(&region->region);
1534
1535 wl_client_add_resource(client, &region->resource);
1536}
1537
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001538static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001539 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001540 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001541};
1542
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001543WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001544weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001545{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001546 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1547 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001548
1549 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001550 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001551}
1552
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001553static void
1554weston_compositor_dpms_on(struct weston_compositor *compositor)
1555{
1556 struct weston_output *output;
1557
1558 wl_list_for_each(output, &compositor->output_list, link)
1559 if (output->set_dpms)
1560 output->set_dpms(output, WESTON_DPMS_ON);
1561}
1562
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001563WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001564weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001565{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001566 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1567 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001568 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001569 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001570 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001571 }
1572}
1573
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001574static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001575weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001576{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001577 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001578 compositor->idle_inhibit++;
1579}
1580
1581static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001582weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001583{
1584 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001585 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001586}
1587
1588static int
1589idle_handler(void *data)
1590{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001591 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001592
1593 if (compositor->idle_inhibit)
1594 return 1;
1595
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001596 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001597
1598 return 1;
1599}
1600
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001601static void
Daniel Stone37816df2012-05-16 18:45:18 +01001602weston_seat_update_drag_surface(struct wl_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001603
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001604static void
Daniel Stone37816df2012-05-16 18:45:18 +01001605clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001606{
Daniel Stone37816df2012-05-16 18:45:18 +01001607 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001608 struct weston_output *output, *prev = NULL;
1609 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001610
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001611 x = wl_fixed_to_int(*fx);
1612 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001613 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1614 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001615
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001616 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001617 if (pixman_region32_contains_point(&output->region,
1618 x, y, NULL))
1619 valid = 1;
1620 if (pixman_region32_contains_point(&output->region,
1621 old_x, old_y, NULL))
1622 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001623 }
Daniel Stone37816df2012-05-16 18:45:18 +01001624
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001625 if (!valid) {
1626 if (x < prev->x)
1627 *fx = wl_fixed_from_int(prev->x);
1628 else if (x >= prev->x + prev->current->width)
1629 *fx = wl_fixed_from_int(prev->x +
1630 prev->current->width - 1);
1631 if (y < prev->y)
1632 *fy = wl_fixed_from_int(prev->y);
1633 else if (y >= prev->y + prev->current->height)
1634 *fy = wl_fixed_from_int(prev->y +
1635 prev->current->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001636 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001637}
1638
1639WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001640notify_motion(struct wl_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001641{
1642 const struct wl_pointer_grab_interface *interface;
Daniel Stone37816df2012-05-16 18:45:18 +01001643 struct weston_seat *ws = (struct weston_seat *) seat;
1644 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001645 struct weston_output *output;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001646 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001647
1648 weston_compositor_activity(ec);
1649
Daniel Stone37816df2012-05-16 18:45:18 +01001650 clip_pointer_motion(ws, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001651
Daniel Stone37816df2012-05-16 18:45:18 +01001652 weston_seat_update_drag_surface(seat,
1653 x - seat->pointer->x,
1654 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001655
Daniel Stone37816df2012-05-16 18:45:18 +01001656 seat->pointer->x = x;
1657 seat->pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001658
1659 ix = wl_fixed_to_int(x);
1660 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001661
Scott Moreauccbf29d2012-02-22 14:21:41 -07001662 wl_list_for_each(output, &ec->output_list, link)
1663 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001664 pixman_region32_contains_point(&output->region,
1665 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001666 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001667
Daniel Stone37816df2012-05-16 18:45:18 +01001668 weston_device_repick(seat);
1669 interface = seat->pointer->grab->interface;
1670 interface->motion(seat->pointer->grab, time,
1671 seat->pointer->grab->x, seat->pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001672
Daniel Stone37816df2012-05-16 18:45:18 +01001673 if (ws->sprite) {
1674 weston_surface_set_position(ws->sprite,
1675 ix - ws->hotspot_x,
1676 iy - ws->hotspot_y);
Pekka Paalanenf8c6aae2012-02-13 11:01:59 +02001677 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001678 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001679}
1680
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001681WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001682weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001683 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001684{
Daniel Stone37816df2012-05-16 18:45:18 +01001685 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001686
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001687 if (seat->seat.keyboard) {
1688 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1689 wl_data_device_set_keyboard_focus(&seat->seat);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001690
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001691 if (seat->seat.keyboard->focus_resource) {
1692 wl_keyboard_send_modifiers(
1693 seat->seat.keyboard->focus_resource,
1694 wl_display_next_serial(compositor->wl_display),
1695 seat->xkb_state.mods_depressed,
1696 seat->xkb_state.mods_latched,
1697 seat->xkb_state.mods_locked,
1698 seat->xkb_state.group);
1699 }
Daniel Stone351eb612012-05-31 15:27:47 -04001700 }
1701
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001702 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001703}
1704
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001705WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001706notify_button(struct wl_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001707 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001708{
Daniel Stone37816df2012-05-16 18:45:18 +01001709 struct weston_seat *ws = (struct weston_seat *) seat;
1710 struct weston_compositor *compositor = ws->compositor;
1711 struct weston_surface *focus =
1712 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001713 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001714
Daniel Stone4dbadb12012-05-30 16:31:51 +01001715 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001716 if (compositor->ping_handler && focus)
1717 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001718 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001719 if (seat->pointer->button_count == 0) {
1720 seat->pointer->grab_button = button;
1721 seat->pointer->grab_time = time;
1722 seat->pointer->grab_x = seat->pointer->x;
1723 seat->pointer->grab_y = seat->pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001724 }
Daniel Stone37816df2012-05-16 18:45:18 +01001725 seat->pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001726 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001727 weston_compositor_idle_release(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001728 seat->pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001729 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001730
Daniel Stone325fc2d2012-05-30 16:31:58 +01001731 weston_compositor_run_button_binding(compositor, ws, time, button,
1732 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001733
Daniel Stone37816df2012-05-16 18:45:18 +01001734 seat->pointer->grab->interface->button(seat->pointer->grab, time,
1735 button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001736
Daniel Stone37816df2012-05-16 18:45:18 +01001737 if (seat->pointer->button_count == 1)
1738 seat->pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001739 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001740}
1741
Scott Moreau210d0792012-03-22 10:47:01 -06001742WL_EXPORT void
Daniel Stone878f0b72012-05-30 16:31:57 +01001743notify_axis(struct wl_seat *seat, uint32_t time, uint32_t axis,
1744 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001745{
Daniel Stone37816df2012-05-16 18:45:18 +01001746 struct weston_seat *ws = (struct weston_seat *) seat;
1747 struct weston_compositor *compositor = ws->compositor;
1748 struct weston_surface *focus =
1749 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001750 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1751
1752 if (compositor->ping_handler && focus)
1753 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001754
1755 weston_compositor_activity(compositor);
1756
Scott Moreau6a3633d2012-03-20 08:47:59 -06001757 if (value)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001758 weston_compositor_run_axis_binding(compositor, ws, time, axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001759 value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001760 else
1761 return;
1762
Daniel Stone37816df2012-05-16 18:45:18 +01001763 if (seat->pointer->focus_resource)
Daniel Stone878f0b72012-05-30 16:31:57 +01001764 wl_pointer_send_axis(seat->pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001765 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001766}
1767
Daniel Stone7ace3902012-05-30 16:31:40 +01001768static int
Daniel Stone37816df2012-05-16 18:45:18 +01001769update_modifier_state(struct weston_seat *seat, uint32_t key, uint32_t state)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001770{
Daniel Stone7ace3902012-05-30 16:31:40 +01001771 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001772 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001773 enum weston_led leds = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001774 int ret = 0;
1775
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001776 /* First update the XKB state object with the keypress. */
Daniel Stone994679a2012-05-30 16:31:43 +01001777 xkb_state_update_key(seat->xkb_state.state, key + 8,
Daniel Stone7ace3902012-05-30 16:31:40 +01001778 state ? XKB_KEY_DOWN : XKB_KEY_UP);
1779
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001780 /* Serialize and update our internal state, checking to see if it's
1781 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001782 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1783 XKB_STATE_DEPRESSED);
1784 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1785 XKB_STATE_LATCHED);
1786 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1787 XKB_STATE_LOCKED);
1788 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001789 XKB_STATE_EFFECTIVE);
1790
Daniel Stone994679a2012-05-30 16:31:43 +01001791 if (mods_depressed != seat->xkb_state.mods_depressed ||
1792 mods_latched != seat->xkb_state.mods_latched ||
1793 mods_locked != seat->xkb_state.mods_locked ||
1794 group != seat->xkb_state.group)
Daniel Stone7ace3902012-05-30 16:31:40 +01001795 ret = 1;
1796
Daniel Stone994679a2012-05-30 16:31:43 +01001797 seat->xkb_state.mods_depressed = mods_depressed;
1798 seat->xkb_state.mods_latched = mods_latched;
1799 seat->xkb_state.mods_locked = mods_locked;
1800 seat->xkb_state.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001801
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001802 /* And update the modifier_state for bindings. */
1803 mods_lookup = mods_depressed | mods_latched;
1804 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001805 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001806 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001807 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001808 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001809 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001810 seat->modifier_state |= MODIFIER_SUPER;
Daniel Stone7ace3902012-05-30 16:31:40 +01001811
Daniel Stone8c8164f2012-05-30 16:31:45 +01001812 /* Finally, notify the compositor that LEDs have changed. */
1813 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001814 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001815 leds |= LED_NUM_LOCK;
1816 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001817 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001818 leds |= LED_CAPS_LOCK;
1819 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001820 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001821 leds |= LED_SCROLL_LOCK;
1822 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001823 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001824 seat->xkb_state.leds = leds;
1825
Daniel Stone7ace3902012-05-30 16:31:40 +01001826 return ret;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001827}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001828
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001829WL_EXPORT void
Daniel Stonec9785ea2012-05-30 16:31:52 +01001830notify_key(struct wl_seat *seat, uint32_t time, uint32_t key,
1831 enum wl_keyboard_key_state state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001832{
Daniel Stone37816df2012-05-16 18:45:18 +01001833 struct weston_seat *ws = (struct weston_seat *) seat;
1834 struct weston_compositor *compositor = ws->compositor;
1835 struct weston_surface *focus =
1836 (struct weston_surface *) seat->pointer->focus;
Daniel Stone7ace3902012-05-30 16:31:40 +01001837 struct wl_keyboard_grab *grab = seat->keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001838 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001839 uint32_t *k, *end;
Daniel Stone7ace3902012-05-30 16:31:40 +01001840 int mods;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001841
Daniel Stonec9785ea2012-05-30 16:31:52 +01001842 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001843 if (compositor->ping_handler && focus)
1844 compositor->ping_handler(focus, serial);
1845
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001846 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001847 seat->keyboard->grab_key = key;
1848 seat->keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001849 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001851 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001852
Daniel Stone7ace3902012-05-30 16:31:40 +01001853 mods = update_modifier_state(ws, key, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001854 end = seat->keyboard->keys.data + seat->keyboard->keys.size;
1855 for (k = seat->keyboard->keys.data; k < end; k++) {
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001856 if (*k == key)
1857 *k = *--end;
1858 }
Daniel Stone37816df2012-05-16 18:45:18 +01001859 seat->keyboard->keys.size = (void *) end - seat->keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001860 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Daniel Stone37816df2012-05-16 18:45:18 +01001861 k = wl_array_add(&seat->keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001862 *k = key;
1863 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001864
Scott Moreaubefa6532012-06-11 14:59:31 -06001865 if (grab == &seat->keyboard->default_grab) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01001866 weston_compositor_run_key_binding(compositor, ws, time, key,
1867 state);
Scott Moreaubefa6532012-06-11 14:59:31 -06001868 grab = seat->keyboard->grab;
1869 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001870
Daniel Stone7ace3902012-05-30 16:31:40 +01001871 grab->interface->key(grab, time, key, state);
Daniel Stone351eb612012-05-31 15:27:47 -04001872 if (mods)
1873 grab->interface->modifiers(grab,
1874 wl_display_get_serial(compositor->wl_display),
Daniel Stone994679a2012-05-30 16:31:43 +01001875 ws->xkb_state.mods_depressed,
1876 ws->xkb_state.mods_latched,
1877 ws->xkb_state.mods_locked,
1878 ws->xkb_state.group);
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001879}
1880
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001881WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001882notify_pointer_focus(struct wl_seat *seat, struct weston_output *output,
1883 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001884{
Daniel Stone37816df2012-05-16 18:45:18 +01001885 struct weston_seat *ws = (struct weston_seat *) seat;
1886 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001887
1888 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01001889 weston_seat_update_drag_surface(seat,
1890 x - seat->pointer->x,
1891 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001892
Daniel Stone37816df2012-05-16 18:45:18 +01001893 seat->pointer->x = x;
1894 seat->pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001895 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001896 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001897 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001898 compositor->focus = 0;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001899 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001900 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001901}
1902
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001903static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001904destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001905{
Daniel Stone37816df2012-05-16 18:45:18 +01001906 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001907
Daniel Stone37816df2012-05-16 18:45:18 +01001908 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001909 saved_kbd_focus_listener);
1910
Daniel Stone37816df2012-05-16 18:45:18 +01001911 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001912}
1913
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001914WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001915notify_keyboard_focus(struct wl_seat *seat, struct wl_array *keys)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001916{
Daniel Stone37816df2012-05-16 18:45:18 +01001917 struct weston_seat *ws = (struct weston_seat *) seat;
1918 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001919 struct wl_surface *surface;
Kristian Høgsbergd3c02752012-03-04 22:35:47 -05001920 uint32_t *k;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05001921
Kristian Høgsberg31dd6b82012-04-09 22:10:00 -04001922 if (keys) {
Daniel Stone37816df2012-05-16 18:45:18 +01001923 wl_array_copy(&seat->keyboard->keys, keys);
1924 ws->modifier_state = 0;
1925 wl_array_for_each(k, &seat->keyboard->keys) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001926 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001927 update_modifier_state(ws, *k, 1);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001928 }
1929
Daniel Stone37816df2012-05-16 18:45:18 +01001930 surface = ws->saved_kbd_focus;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001931
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001932 if (surface) {
Daniel Stone37816df2012-05-16 18:45:18 +01001933 wl_list_remove(&ws->saved_kbd_focus_listener.link);
1934 wl_keyboard_set_focus(ws->seat.keyboard, surface);
Daniel Stone351eb612012-05-31 15:27:47 -04001935
1936 if (seat->keyboard->focus_resource) {
1937 wl_keyboard_send_modifiers(seat->keyboard->focus_resource,
1938 wl_display_next_serial(compositor->wl_display),
Daniel Stone994679a2012-05-30 16:31:43 +01001939 ws->xkb_state.mods_depressed,
1940 ws->xkb_state.mods_latched,
1941 ws->xkb_state.mods_locked,
1942 ws->xkb_state.group);
Daniel Stone351eb612012-05-31 15:27:47 -04001943 }
Daniel Stone37816df2012-05-16 18:45:18 +01001944 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001945 }
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001946 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01001947 wl_array_for_each(k, &seat->keyboard->keys)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001948 weston_compositor_idle_release(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001949
Daniel Stone37816df2012-05-16 18:45:18 +01001950 ws->modifier_state = 0;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001951
Daniel Stone37816df2012-05-16 18:45:18 +01001952 surface = ws->seat.keyboard->focus;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001953
Kristian Høgsbergafee2212012-03-20 17:28:20 -04001954 if (surface) {
Daniel Stone37816df2012-05-16 18:45:18 +01001955 ws->saved_kbd_focus = surface;
1956 ws->saved_kbd_focus_listener.notify =
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001957 destroy_device_saved_kbd_focus;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001958 wl_signal_add(&surface->resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01001959 &ws->saved_kbd_focus_listener);
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02001960 }
1961
Daniel Stone37816df2012-05-16 18:45:18 +01001962 wl_keyboard_set_focus(ws->seat.keyboard, NULL);
Kristian Høgsberg035dd9c2012-04-10 00:33:40 -04001963 /* FIXME: We really need keyboard grab cancel here to
1964 * let the grab shut down properly. As it is we leak
1965 * the grab data. */
Daniel Stone37816df2012-05-16 18:45:18 +01001966 wl_keyboard_end_grab(ws->seat.keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05001967 }
1968}
1969
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001970static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001971lose_touch_focus_resource(struct wl_listener *listener, void *data)
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001972{
Daniel Stone37816df2012-05-16 18:45:18 +01001973 struct weston_seat *seat = container_of(listener, struct weston_seat,
1974 touch_focus_resource_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001975
Daniel Stone37816df2012-05-16 18:45:18 +01001976 seat->touch_focus_resource = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001977}
1978
1979static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04001980lose_touch_focus(struct wl_listener *listener, void *data)
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001981{
Daniel Stone37816df2012-05-16 18:45:18 +01001982 struct weston_seat *seat = container_of(listener, struct weston_seat,
1983 touch_focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001984
Daniel Stone37816df2012-05-16 18:45:18 +01001985 seat->touch_focus = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001986}
1987
1988static void
Daniel Stone37816df2012-05-16 18:45:18 +01001989touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001990{
Daniel Stone37816df2012-05-16 18:45:18 +01001991 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001992 struct wl_resource *resource;
1993
Daniel Stone37816df2012-05-16 18:45:18 +01001994 if (ws->touch_focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02001995 return;
1996
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05001997 if (surface) {
1998 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01001999 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002000 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002001 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002002 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002003 return;
2004 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002005
Daniel Stone37816df2012-05-16 18:45:18 +01002006 ws->touch_focus_resource_listener.notify =
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002007 lose_touch_focus_resource;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002008 wl_signal_add(&resource->destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002009 &ws->touch_focus_resource_listener);
2010 ws->touch_focus_listener.notify = lose_touch_focus;
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002011 wl_signal_add(&surface->resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002012 &ws->touch_focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002013
Daniel Stone37816df2012-05-16 18:45:18 +01002014 seat->touch->focus = surface;
2015 seat->touch->focus_resource = resource;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002016 } else {
Daniel Stone37816df2012-05-16 18:45:18 +01002017 if (seat->touch->focus)
2018 wl_list_remove(&ws->touch_focus_listener.link);
2019 if (seat->touch->focus_resource)
2020 wl_list_remove(&ws->touch_focus_resource_listener.link);
2021 seat->touch->focus = NULL;
2022 seat->touch->focus_resource = NULL;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002023 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002024}
2025
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002026/**
2027 * notify_touch - emulates button touches and notifies surfaces accordingly.
2028 *
2029 * It assumes always the correct cycle sequence until it gets here: touch_down
2030 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2031 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002032 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002033 */
2034WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002035notify_touch(struct wl_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002036 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002037{
Daniel Stone37816df2012-05-16 18:45:18 +01002038 struct weston_seat *ws = (struct weston_seat *) seat;
2039 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002040 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002041 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002042 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002043
2044 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002045 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002046 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002047
Daniel Stone37816df2012-05-16 18:45:18 +01002048 ws->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002049
2050 /* the first finger down picks the surface, and all further go
2051 * to that surface for the remainder of the touch session i.e.
2052 * until all touch points are up again. */
Daniel Stone37816df2012-05-16 18:45:18 +01002053 if (ws->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002054 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01002055 touch_set_focus(ws, &es->surface);
2056 } else if (ws->touch_focus) {
2057 es = (struct weston_surface *) ws->touch_focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002058 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002059 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002060
Daniel Stone37816df2012-05-16 18:45:18 +01002061 if (ws->touch_focus_resource && ws->touch_focus)
2062 wl_touch_send_down(ws->touch_focus_resource,
2063 serial, time,
2064 &ws->touch_focus->resource,
2065 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002066 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002067 case WL_TOUCH_MOTION:
2068 es = (struct weston_surface *) ws->touch_focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002069 if (!es)
2070 break;
2071
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002072 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01002073 if (ws->touch_focus_resource)
2074 wl_touch_send_motion(ws->touch_focus_resource,
2075 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002076 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002077 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002078 weston_compositor_idle_release(ec);
Daniel Stone37816df2012-05-16 18:45:18 +01002079 ws->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002080
Daniel Stone37816df2012-05-16 18:45:18 +01002081 if (ws->touch_focus_resource)
2082 wl_touch_send_up(ws->touch_focus_resource,
2083 serial, time, touch_id);
2084 if (ws->num_tp == 0)
2085 touch_set_focus(ws, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002086 break;
2087 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002088}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002089
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002090static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002091pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2092{
2093 struct weston_seat *seat = container_of(listener, struct weston_seat,
2094 sprite_destroy_listener);
2095
2096 seat->sprite = NULL;
2097}
2098
2099static void
2100pointer_cursor_surface_configure(struct weston_surface *es,
2101 int32_t dx, int32_t dy)
2102{
2103 struct weston_seat *seat = es->private;
2104 int x, y;
2105
2106 assert(es == seat->sprite);
2107
2108 seat->hotspot_x -= dx;
2109 seat->hotspot_y -= dy;
2110
2111 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2112 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2113
2114 weston_surface_configure(seat->sprite, x, y,
2115 es->buffer->width, es->buffer->height);
2116
2117 if (!weston_surface_is_mapped(es)) {
2118 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2119 &es->layer_link);
2120 weston_surface_assign_output(es);
2121 empty_region(&es->input);
2122 }
2123}
2124
2125static void
2126pointer_unmap_sprite(struct weston_seat *seat)
2127{
2128 if (weston_surface_is_mapped(seat->sprite))
2129 weston_surface_unmap(seat->sprite);
2130
2131 wl_list_remove(&seat->sprite_destroy_listener.link);
2132 seat->sprite->configure = NULL;
2133 seat->sprite->private = NULL;
2134 seat->sprite = NULL;
2135}
2136
2137static void
2138pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2139 uint32_t serial, struct wl_resource *surface_resource,
2140 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002141{
Daniel Stone37816df2012-05-16 18:45:18 +01002142 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002143 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002144
Daniel Stone37816df2012-05-16 18:45:18 +01002145 if (serial < seat->seat.pointer->focus_serial)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002146 return;
Daniel Stone37816df2012-05-16 18:45:18 +01002147 if (seat->seat.pointer->focus == NULL)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002148 return;
Daniel Stone37816df2012-05-16 18:45:18 +01002149 if (seat->seat.pointer->focus->resource.client != client)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002150 return;
2151
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002152 if (surface_resource)
2153 surface = container_of(surface_resource->data,
2154 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002155
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002156 if (surface && surface != seat->sprite && surface->configure) {
2157 wl_resource_post_error(&surface->surface.resource,
2158 WL_DISPLAY_ERROR_INVALID_OBJECT,
2159 "surface->configure already set");
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002160 return;
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002161 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002162
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002163 if (seat->sprite)
2164 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002165
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002166 if (!surface)
2167 return;
2168
2169 wl_signal_add(&surface->surface.resource.destroy_signal,
2170 &seat->sprite_destroy_listener);
2171
2172 surface->configure = pointer_cursor_surface_configure;
2173 surface->private = seat;
2174 empty_region(&surface->input);
2175
2176 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002177 seat->hotspot_x = x;
2178 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002179
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002180 weston_surface_set_position(surface,
2181 wl_fixed_to_int(seat->seat.pointer->x) - x,
2182 wl_fixed_to_int(seat->seat.pointer->y) - y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002183}
2184
Daniel Stone37816df2012-05-16 18:45:18 +01002185static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002186 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002187};
2188
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002189static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002190handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002191{
Daniel Stone37816df2012-05-16 18:45:18 +01002192 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002193
Daniel Stone37816df2012-05-16 18:45:18 +01002194 seat = container_of(listener, struct weston_seat,
2195 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002196
Daniel Stone37816df2012-05-16 18:45:18 +01002197 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002198}
2199
Casey Dahlin9074db52012-04-19 22:50:09 -04002200static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002201{
2202 wl_list_remove(&resource->link);
2203 free(resource);
2204}
2205
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002206static void
Daniel Stone37816df2012-05-16 18:45:18 +01002207seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2208 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002209{
Daniel Stone37816df2012-05-16 18:45:18 +01002210 struct weston_seat *seat = resource->data;
2211 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002212
Daniel Stone37816df2012-05-16 18:45:18 +01002213 if (!seat->seat.pointer)
2214 return;
2215
2216 cr = wl_client_add_object(client, &wl_pointer_interface,
2217 &pointer_interface, id, seat);
2218 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002219 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002220
2221 if (seat->seat.pointer->focus &&
2222 seat->seat.pointer->focus->resource.client == client) {
2223 struct weston_surface *surface;
2224 wl_fixed_t sx, sy;
2225
2226 surface = (struct weston_surface *) seat->seat.pointer->focus;
2227 weston_surface_from_global_fixed(surface,
2228 seat->seat.pointer->x,
2229 seat->seat.pointer->y,
2230 &sx,
2231 &sy);
2232 wl_pointer_set_focus(seat->seat.pointer,
2233 seat->seat.pointer->focus,
2234 sx,
2235 sy);
2236 }
Daniel Stone37816df2012-05-16 18:45:18 +01002237}
2238
2239static void
2240seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2241 uint32_t id)
2242{
2243 struct weston_seat *seat = resource->data;
2244 struct wl_resource *cr;
2245
2246 if (!seat->seat.keyboard)
2247 return;
2248
2249 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2250 seat);
2251 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002252 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002253
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002254 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2255 seat->xkb_info.keymap_fd,
2256 seat->xkb_info.keymap_size);
2257
Daniel Stone17b13bd2012-05-30 16:31:39 +01002258 if (seat->seat.keyboard->focus &&
2259 seat->seat.keyboard->focus->resource.client == client) {
2260 wl_keyboard_set_focus(seat->seat.keyboard,
2261 seat->seat.keyboard->focus);
2262 }
Daniel Stone37816df2012-05-16 18:45:18 +01002263}
2264
2265static void
2266seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2267 uint32_t id)
2268{
2269 struct weston_seat *seat = resource->data;
2270 struct wl_resource *cr;
2271
2272 if (!seat->seat.touch)
2273 return;
2274
2275 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2276 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002277 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002278}
2279
2280static const struct wl_seat_interface seat_interface = {
2281 seat_get_pointer,
2282 seat_get_keyboard,
2283 seat_get_touch,
2284};
2285
2286static void
2287bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2288{
2289 struct wl_seat *seat = data;
2290 struct wl_resource *resource;
2291 enum wl_seat_capability caps = 0;
2292
2293 resource = wl_client_add_object(client, &wl_seat_interface,
2294 &seat_interface, id, data);
2295 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002296 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002297
2298 if (seat->pointer)
2299 caps |= WL_SEAT_CAPABILITY_POINTER;
2300 if (seat->keyboard)
2301 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2302 if (seat->touch)
2303 caps |= WL_SEAT_CAPABILITY_TOUCH;
2304
2305 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002306}
2307
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002308static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002309device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002310{
Daniel Stone37816df2012-05-16 18:45:18 +01002311 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002312
Daniel Stone37816df2012-05-16 18:45:18 +01002313 seat = container_of(listener, struct weston_seat,
2314 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002315
Daniel Stone37816df2012-05-16 18:45:18 +01002316 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002317}
2318
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002319static void weston_compositor_xkb_init(struct weston_compositor *ec,
2320 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002321{
Daniel Stonee379da92012-05-30 16:32:04 +01002322 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002323 ec->xkb_context = xkb_context_new(0);
2324 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002325 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002326 exit(1);
2327 }
Daniel Stone994679a2012-05-30 16:31:43 +01002328 }
2329
2330 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002331 ec->xkb_names = *names;
2332 if (!ec->xkb_names.rules)
2333 ec->xkb_names.rules = strdup("evdev");
2334 if (!ec->xkb_names.model)
2335 ec->xkb_names.model = strdup("pc105");
2336 if (!ec->xkb_names.layout)
2337 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002338}
2339
Daniel Stonee379da92012-05-30 16:32:04 +01002340static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2341{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002342 if (xkb_info->keymap)
2343 xkb_map_unref(xkb_info->keymap);
2344
2345 if (xkb_info->keymap_area)
2346 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2347 if (xkb_info->keymap_fd >= 0)
2348 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002349}
2350
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002351static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2352{
Daniel Stonee379da92012-05-30 16:32:04 +01002353 free((char *) ec->xkb_names.rules);
2354 free((char *) ec->xkb_names.model);
2355 free((char *) ec->xkb_names.layout);
2356 free((char *) ec->xkb_names.variant);
2357 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002358
Daniel Stonee379da92012-05-30 16:32:04 +01002359 xkb_info_destroy(&ec->xkb_info);
2360 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002361}
2362
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002363static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002364weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002365{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002366 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002367
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002368 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2369 XKB_MOD_NAME_CTRL);
2370 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2371 XKB_MOD_NAME_ALT);
2372 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2373 XKB_MOD_NAME_LOGO);
2374
2375 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2376 XKB_LED_NAME_NUM);
2377 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2378 XKB_LED_NAME_CAPS);
2379 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2380 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002381
2382 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2383 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002384 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002385 exit(EXIT_FAILURE);
2386 }
2387 xkb_info->keymap_size = strlen(keymap_str) + 1;
2388
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002389 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002390 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002391 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002392 (unsigned long) xkb_info->keymap_size);
2393 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002394 }
2395
2396 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2397 PROT_READ | PROT_WRITE,
2398 MAP_SHARED, xkb_info->keymap_fd, 0);
2399 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002400 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002401 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002402 goto err_dev_zero;
2403 }
2404 strcpy(xkb_info->keymap_area, keymap_str);
2405 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002406
2407 return;
2408
2409err_dev_zero:
2410 close(xkb_info->keymap_fd);
2411 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002412err_keymap_str:
2413 free(keymap_str);
2414 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002415}
2416
2417static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002418weston_compositor_build_global_keymap(struct weston_compositor *ec)
2419{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002420 if (ec->xkb_info.keymap != NULL)
2421 return;
2422
Daniel Stonee379da92012-05-30 16:32:04 +01002423 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002424 &ec->xkb_names,
2425 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002426 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002427 weston_log("failed to compile global XKB keymap\n");
2428 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002429 "options %s",
2430 ec->xkb_names.rules, ec->xkb_names.model,
2431 ec->xkb_names.layout, ec->xkb_names.variant,
2432 ec->xkb_names.options);
2433 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002434 }
2435
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002436 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002437}
2438
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002439WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002440weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002441{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002442 if (seat->has_keyboard)
2443 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002444
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002445 if (keymap != NULL) {
2446 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002447 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002448 }
2449 else {
2450 weston_compositor_build_global_keymap(seat->compositor);
2451 seat->xkb_info = seat->compositor->xkb_info;
2452 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2453 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002454
2455 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2456 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002457 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002458 exit(1);
2459 }
2460
2461 seat->xkb_state.mods_depressed = 0;
2462 seat->xkb_state.mods_latched = 0;
2463 seat->xkb_state.mods_locked = 0;
2464 seat->xkb_state.group = 0;
2465
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002466 wl_keyboard_init(&seat->keyboard);
2467 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2468
2469 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002470}
2471
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002472WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002473weston_seat_init_pointer(struct weston_seat *seat)
2474{
2475 if (seat->has_pointer)
2476 return;
2477
2478 wl_pointer_init(&seat->pointer);
2479 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2480
2481 seat->has_pointer = 1;
2482}
2483
2484WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002485weston_seat_init_touch(struct weston_seat *seat)
2486{
2487 if (seat->has_touch)
2488 return;
2489
2490 wl_touch_init(&seat->touch);
2491 wl_seat_set_touch(&seat->seat, &seat->touch);
2492
2493 seat->has_touch = 1;
2494}
2495
2496WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002497weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002498{
Daniel Stone37816df2012-05-16 18:45:18 +01002499 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002500 seat->has_pointer = 0;
2501 seat->has_keyboard = 0;
2502 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002503
Daniel Stone37816df2012-05-16 18:45:18 +01002504 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2505 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002506
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002507 seat->sprite = NULL;
2508 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002509
Daniel Stone37816df2012-05-16 18:45:18 +01002510 seat->compositor = ec;
2511 seat->hotspot_x = 16;
2512 seat->hotspot_y = 16;
2513 seat->modifier_state = 0;
2514 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002515
Daniel Stone37816df2012-05-16 18:45:18 +01002516 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002517 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002518
Daniel Stone37816df2012-05-16 18:45:18 +01002519 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002520
Daniel Stone37816df2012-05-16 18:45:18 +01002521 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2522 wl_signal_add(&seat->seat.drag_icon_signal,
2523 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002524
2525 clipboard_create(seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002526}
2527
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002528WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002529weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002530{
Daniel Stone37816df2012-05-16 18:45:18 +01002531 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002532 /* The global object is destroyed at wl_display_destroy() time. */
2533
Daniel Stone37816df2012-05-16 18:45:18 +01002534 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002535 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002536
Daniel Stone74419a22012-05-30 16:32:02 +01002537 if (seat->xkb_state.state != NULL)
2538 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002539 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002540
Daniel Stone37816df2012-05-16 18:45:18 +01002541 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002542}
2543
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002544static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002545drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2546{
2547 weston_surface_configure(es,
2548 es->geometry.x + sx, es->geometry.y + sy,
2549 es->buffer->width, es->buffer->height);
2550}
2551
2552static int
Daniel Stone37816df2012-05-16 18:45:18 +01002553device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002554 struct weston_surface *surface)
2555{
Daniel Stone37816df2012-05-16 18:45:18 +01002556 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002557
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002558 if (surface->configure) {
2559 wl_resource_post_error(&surface->surface.resource,
2560 WL_DISPLAY_ERROR_INVALID_OBJECT,
2561 "surface->configure already set");
2562 return 0;
2563 }
2564
Daniel Stone37816df2012-05-16 18:45:18 +01002565 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002566
Daniel Stone37816df2012-05-16 18:45:18 +01002567 weston_surface_set_position(ws->drag_surface,
2568 wl_fixed_to_double(seat->pointer->x),
2569 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002570
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002571 surface->configure = drag_surface_configure;
2572
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002573 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002574 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002575
2576 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002577}
2578
2579static void
Daniel Stone37816df2012-05-16 18:45:18 +01002580device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002581{
Daniel Stone37816df2012-05-16 18:45:18 +01002582 seat->drag_surface->configure = NULL;
2583 undef_region(&seat->drag_surface->input);
2584 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2585 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002586}
2587
2588static void
Daniel Stone37816df2012-05-16 18:45:18 +01002589device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002590{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002591 struct wl_list *list;
2592
Daniel Stone37816df2012-05-16 18:45:18 +01002593 if (weston_surface_is_mapped(seat->drag_surface) ||
2594 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002595 return;
2596
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002597 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2598 list = &seat->sprite->layer_link;
2599 else
2600 list = &seat->compositor->cursor_layer.surface_list;
2601
2602 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01002603 weston_surface_assign_output(seat->drag_surface);
2604 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002605}
2606
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002607static void
Daniel Stone37816df2012-05-16 18:45:18 +01002608weston_seat_update_drag_surface(struct wl_seat *seat,
2609 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002610{
2611 int surface_changed = 0;
Daniel Stone37816df2012-05-16 18:45:18 +01002612 struct weston_seat *ws = (struct weston_seat *) seat;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002613
Daniel Stone37816df2012-05-16 18:45:18 +01002614 if (!ws->drag_surface && !seat->drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002615 return;
2616
Daniel Stone37816df2012-05-16 18:45:18 +01002617 if (ws->drag_surface && seat->drag_surface &&
2618 (&ws->drag_surface->surface.resource !=
2619 &seat->drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002620 /* between calls to this funcion we got a new drag_surface */
2621 surface_changed = 1;
2622
Daniel Stone37816df2012-05-16 18:45:18 +01002623 if (!seat->drag_surface || surface_changed) {
2624 device_release_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002625 if (!surface_changed)
2626 return;
2627 }
2628
Daniel Stone37816df2012-05-16 18:45:18 +01002629 if (!ws->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002630 struct weston_surface *surface = (struct weston_surface *)
Daniel Stone37816df2012-05-16 18:45:18 +01002631 seat->drag_surface;
2632 if (!device_setup_new_drag_surface(ws, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002633 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002634 }
2635
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002636 /* the client may not have attached a buffer to the drag surface
2637 * when we setup it up, so check if map is needed on every update */
Daniel Stone37816df2012-05-16 18:45:18 +01002638 device_map_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002639
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002640 /* the client may have attached a buffer with a different size to
2641 * the drag surface, causing the input region to be reset */
Daniel Stone37816df2012-05-16 18:45:18 +01002642 if (region_is_undefined(&ws->drag_surface->input))
2643 empty_region(&ws->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002644
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002645 if (!dx && !dy)
2646 return;
2647
Daniel Stone37816df2012-05-16 18:45:18 +01002648 weston_surface_set_position(ws->drag_surface,
2649 ws->drag_surface->geometry.x + wl_fixed_to_double(dx),
2650 ws->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002651}
2652
2653WL_EXPORT void
2654weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2655{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002656 struct weston_seat *seat;
2657
2658 wl_list_for_each(seat, &compositor->seat_list, link)
2659 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002660}
2661
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002662static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002663bind_output(struct wl_client *client,
2664 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002665{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002666 struct weston_output *output = data;
2667 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002668 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002669
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002670 resource = wl_client_add_object(client,
2671 &wl_output_interface, NULL, id, data);
2672
Casey Dahlin9074db52012-04-19 22:50:09 -04002673 wl_list_insert(&output->resource_list, &resource->link);
2674 resource->destroy = unbind_resource;
2675
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002676 wl_output_send_geometry(resource,
2677 output->x,
2678 output->y,
2679 output->mm_width,
2680 output->mm_height,
2681 output->subpixel,
2682 output->make, output->model);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002683
2684 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002685 wl_output_send_mode(resource,
2686 mode->flags,
2687 mode->width,
2688 mode->height,
2689 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002690 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002691}
2692
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002693static const char vertex_shader[] =
2694 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002695 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002696 "attribute vec2 texcoord;\n"
2697 "varying vec2 v_texcoord;\n"
2698 "void main()\n"
2699 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002700 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002701 " v_texcoord = texcoord;\n"
2702 "}\n";
2703
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002704static const char texture_fragment_shader[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05002705 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002706 "varying vec2 v_texcoord;\n"
2707 "uniform sampler2D tex;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002708 "uniform float alpha;\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002709 "uniform float texwidth;\n"
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -04002710 "uniform vec4 opaque;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002711 "void main()\n"
2712 "{\n"
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02002713 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n"
2714 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n"
2715 " discard;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002716 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -04002717 " if (opaque.x <= v_texcoord.x && v_texcoord.x < opaque.y &&\n"
2718 " opaque.z <= v_texcoord.y && v_texcoord.y < opaque.w)\n"
2719 " gl_FragColor.a = 1.0;\n"
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002720 " gl_FragColor = alpha * gl_FragColor;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002721 "}\n";
2722
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002723static const char solid_fragment_shader[] =
2724 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002725 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002726 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002727 "void main()\n"
2728 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002729 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002730 "}\n";
2731
Kristian Høgsberga9468212010-06-14 21:03:11 -04002732static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002733compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002734{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002735 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002736 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002737 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002738
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002739 s = glCreateShader(type);
2740 glShaderSource(s, 1, &source, NULL);
2741 glCompileShader(s);
2742 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002743 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002744 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02002745 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002746 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002747 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002748
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002749 return s;
2750}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002751
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002752static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002753weston_shader_init(struct weston_shader *shader,
2754 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002755{
2756 char msg[512];
2757 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002758
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002759 shader->vertex_shader =
2760 compile_shader(GL_VERTEX_SHADER, vertex_source);
2761 shader->fragment_shader =
2762 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2763
2764 shader->program = glCreateProgram();
2765 glAttachShader(shader->program, shader->vertex_shader);
2766 glAttachShader(shader->program, shader->fragment_shader);
2767 glBindAttribLocation(shader->program, 0, "position");
2768 glBindAttribLocation(shader->program, 1, "texcoord");
2769
2770 glLinkProgram(shader->program);
2771 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002772 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002773 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02002774 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002775 return -1;
2776 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002777
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002778 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
2779 shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002780 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002781 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Scott Moreau9fb98242012-05-22 10:18:50 -06002782 shader->texwidth_uniform = glGetUniformLocation(shader->program, "texwidth");
2783 shader->opaque_uniform = glGetUniformLocation(shader->program, "opaque");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002784
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002785 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002786}
2787
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002788WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002789weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002790{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002791 struct weston_compositor *c = output->compositor;
2792
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002793 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002794 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002795 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002796
2797 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002798}
2799
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002800WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002801weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002802{
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002803 int flip;
Scott Moreau850ca422012-05-21 15:21:25 -06002804 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002805 struct weston_matrix camera;
2806 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002807
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002808 weston_matrix_init(&output->matrix);
2809 weston_matrix_translate(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002810 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2811 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002812
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002813 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002814 weston_matrix_scale(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002815 2.0 / (output->current->width + output->border.left + output->border.right),
2816 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
Scott Moreau850ca422012-05-21 15:21:25 -06002817
Scott Moreauccbf29d2012-02-22 14:21:41 -07002818 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002819 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002820 weston_matrix_init(&camera);
2821 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002822 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002823 weston_matrix_translate(&camera, output->zoom.trans_x,
2824 flip * output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002825 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002826 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002827 weston_matrix_multiply(&output->matrix, &modelview);
2828 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002829
Scott Moreauccbf29d2012-02-22 14:21:41 -07002830 output->dirty = 0;
2831}
2832
2833WL_EXPORT void
2834weston_output_move(struct weston_output *output, int x, int y)
2835{
2836 output->x = x;
2837 output->y = y;
2838
2839 pixman_region32_init(&output->previous_damage);
2840 pixman_region32_init_rect(&output->region, x, y,
2841 output->current->width,
2842 output->current->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002843}
2844
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002845WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002846weston_output_init(struct weston_output *output, struct weston_compositor *c,
2847 int x, int y, int width, int height, uint32_t flags)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002848{
2849 output->compositor = c;
2850 output->x = x;
2851 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002852 output->border.top = 0;
2853 output->border.bottom = 0;
2854 output->border.left = 0;
2855 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002856 output->mm_width = width;
2857 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002858 output->dirty = 1;
2859
Scott Moreau429490d2012-06-17 18:10:59 -06002860 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002861
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002862 output->flags = flags;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002863 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02002864 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002865
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04002866 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06002867 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04002868 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02002869
Casey Dahlin58ba1372012-04-19 22:50:08 -04002870 output->id = ffs(~output->compositor->output_id_pool) - 1;
2871 output->compositor->output_id_pool |= 1 << output->id;
2872
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002873 output->global =
2874 wl_display_add_global(c->wl_display, &wl_output_interface,
2875 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002876}
2877
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01002878static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05002879compositor_bind(struct wl_client *client,
2880 void *data, uint32_t version, uint32_t id)
2881{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002882 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05002883
2884 wl_client_add_object(client, &wl_compositor_interface,
2885 &compositor_interface, id, compositor);
2886}
2887
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002888static void
Martin Minarikf12c2872012-06-11 00:57:39 +02002889log_uname(void)
2890{
2891 struct utsname usys;
2892
2893 uname(&usys);
2894
2895 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
2896 usys.version, usys.machine);
2897}
2898
2899static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002900log_extensions(const char *name, const char *extensions)
2901{
2902 const char *p, *end;
2903 int l;
2904
2905 l = weston_log("%s:", name);
2906 p = extensions;
2907 while (*p) {
2908 end = strchrnul(p, ' ');
2909 if (l + (end - p) > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03002910 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
2911 end - p, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002912 else
2913 l += weston_log_continue(" %.*s", end - p, p);
2914 for (p = end; isspace(*p); p++)
2915 ;
2916 }
2917 weston_log_continue("\n");
2918}
2919
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03002920static void
2921log_egl_gl_info(EGLDisplay egldpy)
2922{
2923 const char *str;
2924
2925 str = eglQueryString(egldpy, EGL_VERSION);
2926 weston_log("EGL version: %s\n", str ? str : "(null)");
2927
2928 str = eglQueryString(egldpy, EGL_VENDOR);
2929 weston_log("EGL vendor: %s\n", str ? str : "(null)");
2930
2931 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
2932 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
2933
2934 str = eglQueryString(egldpy, EGL_EXTENSIONS);
2935 log_extensions("EGL extensions", str ? str : "(null)");
2936
2937 str = (char *)glGetString(GL_VERSION);
2938 weston_log("GL version: %s\n", str ? str : "(null)");
2939
2940 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
2941 weston_log("GLSL version: %s\n", str ? str : "(null)");
2942
2943 str = (char *)glGetString(GL_VENDOR);
2944 weston_log("GL vendor: %s\n", str ? str : "(null)");
2945
2946 str = (char *)glGetString(GL_RENDERER);
2947 weston_log("GL renderer: %s\n", str ? str : "(null)");
2948
2949 str = (char *)glGetString(GL_EXTENSIONS);
2950 log_extensions("GL extensions", str ? str : "(null)");
2951}
2952
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002953WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04002954weston_compositor_init(struct weston_compositor *ec,
2955 struct wl_display *display,
2956 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04002957 char *argv[],
2958 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04002959{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05002960 struct wl_event_loop *loop;
Kristian Høgsbergd711d0c2011-01-14 17:28:21 -05002961 const char *extensions;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01002962 struct xkb_rule_names xkb_names;
2963 const struct config_key keyboard_config_keys[] = {
2964 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
2965 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
2966 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
2967 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
2968 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
2969 };
2970 const struct config_section cs[] = {
2971 { "keyboard",
2972 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
2973 };
2974
2975 memset(&xkb_names, 0, sizeof(xkb_names));
2976 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002977
Kristian Høgsbergf9212892008-10-11 18:40:23 -04002978 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002979 wl_signal_init(&ec->destroy_signal);
2980 wl_signal_init(&ec->activate_signal);
2981 wl_signal_init(&ec->lock_signal);
2982 wl_signal_init(&ec->unlock_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01002983 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002984
Casey Dahlin58ba1372012-04-19 22:50:08 -04002985 ec->output_id_pool = 0;
2986
Kristian Høgsberga8873122011-11-23 10:39:34 -05002987 if (!wl_display_add_global(display, &wl_compositor_interface,
2988 ec, compositor_bind))
2989 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05002990
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002991 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002992
Kristian Høgsberg362b6722012-06-18 15:13:51 -04002993 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04002994
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04002995 ec->image_target_texture_2d =
2996 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
2997 ec->image_target_renderbuffer_storage = (void *)
2998 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
2999 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3000 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3001 ec->bind_display =
3002 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3003 ec->unbind_display =
3004 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
3005
3006 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003007 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003008 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003009 return -1;
3010 }
3011
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003012 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003013 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003014 return -1;
3015 }
3016
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003017 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3018 ec->read_format = GL_BGRA_EXT;
3019 else
3020 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003021
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003022 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3023 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003024
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003025 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003026 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003027 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003028 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003029 return -1;
3030 }
3031
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003032 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3033 ec->has_bind_display = 1;
3034 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003035 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003036
Kristian Høgsberg201a9042008-12-10 00:40:50 -05003037 wl_list_init(&ec->surface_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003038 wl_list_init(&ec->layer_list);
Daniel Stone37816df2012-05-16 18:45:18 +01003039 wl_list_init(&ec->seat_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003040 wl_list_init(&ec->output_list);
Daniel Stone325fc2d2012-05-30 16:31:58 +01003041 wl_list_init(&ec->key_binding_list);
3042 wl_list_init(&ec->button_binding_list);
3043 wl_list_init(&ec->axis_binding_list);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003044 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003045 ec->fade.animation.frame = fade_frame;
3046 wl_list_init(&ec->fade.animation.link);
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003047
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003048 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3049 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3050
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003051 screenshooter_create(ec);
Scott Moreau7a1b32a2012-05-27 14:25:02 -06003052 text_cursor_position_notifier_create(ec);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003053
Scott Moreauff1db4a2012-04-17 19:06:18 -06003054 ec->ping_handler = NULL;
3055
Kristian Høgsbergdade6492012-01-04 21:47:30 -05003056 wl_data_device_manager_init(ec->wl_display);
Kristian Høgsberg47fe08a2011-10-28 12:26:06 -04003057
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003058 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003059
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003060 if (weston_shader_init(&ec->texture_shader,
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003061 vertex_shader, texture_fragment_shader) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003062 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003063 if (weston_shader_init(&ec->solid_shader,
3064 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003065 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003066
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003067 weston_compositor_xkb_init(ec, &xkb_names);
3068
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003069 loop = wl_display_get_event_loop(ec->wl_display);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003070 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
Pekka Paalanen7296e792011-12-07 16:22:00 +02003071 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04003072
Kristian Høgsberg7ea10862012-03-05 17:49:30 -05003073 ec->input_loop = wl_event_loop_create();
3074
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003075 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003076
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003077 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003078}
3079
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003080WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003081weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003082{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003083 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003084
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003085 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003086 if (ec->input_loop_source)
3087 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003088
Matt Roper361d2ad2011-08-29 13:52:23 -07003089 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003090 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003091 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003092
Daniel Stone325fc2d2012-05-30 16:31:58 +01003093 weston_binding_list_destroy_all(&ec->key_binding_list);
3094 weston_binding_list_destroy_all(&ec->button_binding_list);
3095 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003096
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003097 wl_array_release(&ec->vertices);
3098 wl_array_release(&ec->indices);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003099
3100 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003101}
3102
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003103static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003104{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003105 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003106
Martin Minarik6d118362012-06-07 18:01:59 +02003107 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003108 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003109
3110 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003111}
3112
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003113static void
3114on_segv_signal(int s, siginfo_t *siginfo, void *context)
3115{
3116 void *buffer[32];
3117 int i, count;
3118 Dl_info info;
3119
Martin Minarik6d118362012-06-07 18:01:59 +02003120 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003121
3122 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3123 for (i = 0; i < count; i++) {
3124 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003125 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003126 (long) buffer[i],
3127 info.dli_sname ? info.dli_sname : "--",
3128 info.dli_fname);
3129 }
3130
3131 longjmp(segv_jmp_buf, 1);
3132}
3133
3134
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003135static void *
3136load_module(const char *name, const char *entrypoint, void **handle)
3137{
3138 char path[PATH_MAX];
3139 void *module, *init;
3140
3141 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003142 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003143 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003144 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003145
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003146 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003147 module = dlopen(path, RTLD_LAZY);
3148 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003149 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003150 return NULL;
3151 }
3152
3153 init = dlsym(module, entrypoint);
3154 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003155 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003156 return NULL;
3157 }
3158
3159 return init;
3160}
3161
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003162static const char xdg_error_message[] =
3163 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n"
3164 "Refer to your distribution on how to get it, or\n"
3165 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3166 "on how to implement it.\n";
3167
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003168int main(int argc, char *argv[])
3169{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003170 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003171 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003172 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003173 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003174 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003175 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003176 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003177 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003178 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003179 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003180 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003181 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003182 char *backend = NULL;
3183 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003184 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003185 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003186 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003187 int32_t xserver = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003188 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003189 char *config_file;
3190
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003191 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003192 { "type", CONFIG_KEY_STRING, &shell },
3193 };
3194
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003195 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003196 { "shell",
3197 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3198 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003199
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003200 const struct weston_option core_options[] = {
3201 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3202 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3203 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003204 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003205 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003206 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003207 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003208
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003209 argc = parse_options(core_options,
3210 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003211
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003212 weston_log_file_open(log);
3213
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003214 if (!getenv("XDG_RUNTIME_DIR")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003215 weston_log(xdg_error_message);
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003216 exit(EXIT_FAILURE);
3217 }
3218
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003219 weston_log("%s\n"
3220 STAMP_SPACE "%s\n"
3221 STAMP_SPACE "Bug reports to: %s\n"
3222 STAMP_SPACE "Build: %s\n",
3223 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003224 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003225 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003226
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003227 display = wl_display_create();
3228
Tiago Vignatti2116b892011-08-08 05:52:59 -07003229 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003230 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3231 display);
3232 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3233 display);
3234 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3235 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003236
3237 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003238 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3239 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003240
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003241 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3242 segv_action.sa_sigaction = on_segv_signal;
Pekka Paalanen8423a892012-01-19 16:48:37 +02003243 sigemptyset(&segv_action.sa_mask);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003244 sigaction(SIGSEGV, &segv_action, NULL);
3245
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003246 if (!backend) {
3247 if (getenv("WAYLAND_DISPLAY"))
3248 backend = "wayland-backend.so";
3249 else if (getenv("DISPLAY"))
3250 backend = "x11-backend.so";
3251 else if (getenv("OPENWFD"))
3252 backend = "openwfd-backend.so";
3253 else
3254 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003255 }
3256
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003257 config_file = config_file_path("weston.ini");
3258 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003259
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003260 backend_init = load_module(backend, "backend_init", &backend_module);
3261 if (!backend_init)
3262 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003263
Daniel Stonec1be8e52012-06-01 11:14:02 -04003264 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003265 if (ec == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003266 weston_log("failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003267 exit(EXIT_FAILURE);
3268 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003269
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003270 for (i = 1; argv[i]; i++)
Martin Minarik6d118362012-06-07 18:01:59 +02003271 weston_log("unhandled option: %s\n", argv[i]);
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003272 if (argv[1])
3273 exit(EXIT_FAILURE);
3274
Daniel Stonec1be8e52012-06-01 11:14:02 -04003275 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003276
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003277 ec->option_idle_time = idle_time;
3278 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003279
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003280 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003281 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03003282 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003283 "weston_xserver_init",
3284 &xserver_module);
3285 if (module_init && module_init(ec) < 0)
3286 exit(EXIT_FAILURE);
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003287
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003288 if (!shell)
3289 shell = "desktop-shell.so";
3290 module_init = load_module(shell, "shell_init", &shell_module);
3291 if (!module_init || module_init(ec) < 0)
3292 exit(EXIT_FAILURE);
3293
3294
3295 module_init = NULL;
3296 if (module)
3297 module_init = load_module(module, "module_init", NULL);
3298 if (module_init && module_init(ec) < 0)
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003299 exit(EXIT_FAILURE);
3300
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003301 if (wl_display_add_socket(display, socket_name)) {
Martin Minarik6d118362012-06-07 18:01:59 +02003302 weston_log("failed to add socket: %m\n");
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003303 exit(EXIT_FAILURE);
3304 }
3305
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003306 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003307 weston_compositor_wake(ec);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003308 if (setjmp(segv_jmp_buf) == 0)
3309 wl_display_run(display);
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003310 else
3311 ret = EXIT_FAILURE;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003312
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003313 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003314 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003315
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003316 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003317
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003318 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003319 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003320
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003321 for (i = ARRAY_LENGTH(signals); i;)
3322 wl_event_source_remove(signals[--i]);
3323
Daniel Stone855028f2012-05-01 20:37:10 +01003324 weston_compositor_xkb_destroy(ec);
3325
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003326 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003327 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003328
Martin Minarik19e6f262012-06-07 13:08:46 +02003329 weston_log_file_close();
3330
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003331 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003332}