blob: faed8f262a3b23df850ab93ff6396a01186930d7 [file] [log] [blame]
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Intel Corporation
3 * Copyright © 2008-2011 Kristian Høgsberg
Pekka Paalanend581a8f2012-01-27 16:25:16 +02004 * Copyright © 2012 Collabora, Ltd.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -05005 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04006 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050015 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040016 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Kristian Høgsbergffd710e2008-12-02 15:15:01 -050023 */
24
Kristian Høgsberg06bc2642010-12-01 09:50:16 -050025#define _GNU_SOURCE
26
Kristian Høgsberga9410222011-01-14 17:22:35 -050027#include "config.h"
28
Daniel Stoneb7452fe2012-06-01 12:14:06 +010029#include <fcntl.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040030#include <stdio.h>
31#include <string.h>
32#include <stdlib.h>
33#include <stdint.h>
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +010034#include <limits.h>
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -050035#include <stdarg.h>
Benjamin Franzke6f5fc692011-06-21 19:34:19 +020036#include <assert.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040037#include <sys/ioctl.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010038#include <sys/mman.h>
Kristian Høgsberg27da5382011-06-21 17:32:25 -040039#include <sys/wait.h>
Pekka Paalanen409ef0a2011-12-02 15:30:21 +020040#include <sys/socket.h>
Martin Minarikf12c2872012-06-11 00:57:39 +020041#include <sys/utsname.h>
Martin Minarik37032f82012-06-18 20:15:18 +020042#include <sys/stat.h>
Kristian Høgsberg16eb6752008-10-08 22:51:32 -040043#include <unistd.h>
Kristian Høgsberg54879822008-11-23 17:07:32 -050044#include <math.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040045#include <linux/input.h>
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -040046#include <dlfcn.h>
Kristian Høgsberg85449032011-05-02 12:11:07 -040047#include <signal.h>
Kristian Høgsberg0690da62012-01-16 11:53:54 -050048#include <setjmp.h>
Kristian Høgsberga411c8b2012-06-08 16:16:52 -040049#include <sys/time.h>
50#include <time.h>
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -040051#include <ctype.h>
Kristian Høgsberg890bc052008-12-30 14:31:33 -050052
Pekka Paalanen50719bc2011-11-22 14:18:50 +020053#include <wayland-server.h>
Kristian Høgsberg82863022010-06-04 21:52:02 -040054#include "compositor.h"
Pekka Paalanen51aaf642012-05-30 15:53:41 +030055#include "../shared/os-compatibility.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øgsberg7b884bc2012-07-31 14:32:01 -040059static struct weston_compositor *segv_compositor;
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;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400240 surface->alpha = 1.0;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400241 surface->blend = 1;
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -0400242 surface->opaque_rect[0] = 0.0;
243 surface->opaque_rect[1] = 0.0;
244 surface->opaque_rect[2] = 0.0;
245 surface->opaque_rect[3] = 0.0;
Juan Zhao46436612012-03-20 01:48:46 +0800246 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400247
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200248 surface->num_textures = 0;
249 surface->num_images = 0;
250
Benjamin Franzke06286262011-05-06 19:12:33 +0200251 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400252 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400253 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200254
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400255 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500256 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500257 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500258 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200259 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500260 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400261
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400262 surface->buffer_destroy_listener.notify =
263 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200264
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200265 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200266 wl_list_insert(&surface->geometry.transformation_list,
267 &surface->transform.position.link);
268 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200269 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200270 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500271
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400272 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500273}
274
Alex Wu8811bf92012-02-28 18:07:54 +0800275WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500276weston_surface_set_color(struct weston_surface *surface,
277 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
278{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500279 surface->color[0] = red;
280 surface->color[1] = green;
281 surface->color[2] = blue;
282 surface->color[3] = alpha;
283 surface->shader = &surface->compositor->solid_shader;
284}
285
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400286WL_EXPORT void
287weston_surface_to_global_float(struct weston_surface *surface,
288 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200289{
290 if (surface->transform.enabled) {
291 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
292
293 weston_matrix_transform(&surface->transform.matrix, &v);
294
295 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200296 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200297 "weston_surface_to_global(), divisor = %g\n",
298 v.f[3]);
299 *x = 0;
300 *y = 0;
301 return;
302 }
303
304 *x = v.f[0] / v.f[3];
305 *y = v.f[1] / v.f[3];
306 } else {
307 *x = sx + surface->geometry.x;
308 *y = sy + surface->geometry.y;
309 }
310}
311
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500312WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400313weston_surface_move_to_plane(struct weston_surface *surface,
314 struct weston_plane *plane)
315{
316 if (surface->plane == plane)
317 return;
318
319 weston_surface_damage_below(surface);
320 surface->plane = plane;
321 weston_surface_damage(surface);
322}
323
324WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500325weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200326{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500327 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500329 pixman_region32_init(&damage);
330 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
331 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400332 pixman_region32_union(&surface->plane->damage,
333 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500334 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200335}
336
337static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200338surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
339 int32_t width, int32_t height,
340 pixman_region32_t *bbox)
341{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200342 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
343 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200344 int32_t s[4][2] = {
345 { sx, sy },
346 { sx, sy + height },
347 { sx + width, sy },
348 { sx + width, sy + height }
349 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200350 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200351 int i;
352
353 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200354 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400355 weston_surface_to_global_float(surface,
356 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200357 if (x < min_x)
358 min_x = x;
359 if (x > max_x)
360 max_x = x;
361 if (y < min_y)
362 min_y = y;
363 if (y > max_y)
364 max_y = y;
365 }
366
Pekka Paalanen219b9822012-02-08 15:38:37 +0200367 int_x = floorf(min_x);
368 int_y = floorf(min_y);
369 pixman_region32_init_rect(bbox, int_x, int_y,
370 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200371}
372
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200373static void
374weston_surface_update_transform_disable(struct weston_surface *surface)
375{
376 surface->transform.enabled = 0;
377
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200378 /* round off fractions when not transformed */
379 surface->geometry.x = roundf(surface->geometry.x);
380 surface->geometry.y = roundf(surface->geometry.y);
381
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200382 pixman_region32_init_rect(&surface->transform.boundingbox,
383 surface->geometry.x,
384 surface->geometry.y,
385 surface->geometry.width,
386 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500387
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400388 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500389 pixman_region32_copy(&surface->transform.opaque,
390 &surface->opaque);
391 pixman_region32_translate(&surface->transform.opaque,
392 surface->geometry.x,
393 surface->geometry.y);
394 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200395}
396
397static int
398weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200399{
400 struct weston_matrix *matrix = &surface->transform.matrix;
401 struct weston_matrix *inverse = &surface->transform.inverse;
402 struct weston_transform *tform;
403
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200404 surface->transform.enabled = 1;
405
406 /* Otherwise identity matrix, but with x and y translation. */
407 surface->transform.position.matrix.d[12] = surface->geometry.x;
408 surface->transform.position.matrix.d[13] = surface->geometry.y;
409
410 weston_matrix_init(matrix);
411 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
412 weston_matrix_multiply(matrix, &tform->matrix);
413
414 if (weston_matrix_invert(inverse, matrix) < 0) {
415 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200416 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200417 " transformation not invertible.\n", surface);
418 return -1;
419 }
420
421 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
422 surface->geometry.height,
423 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500424
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200425 return 0;
426}
427
428WL_EXPORT void
429weston_surface_update_transform(struct weston_surface *surface)
430{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200431 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200432 return;
433
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200434 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200435
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500436 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200437
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200438 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500439 pixman_region32_fini(&surface->transform.opaque);
440 pixman_region32_init(&surface->transform.opaque);
441
442 if (region_is_undefined(&surface->input))
443 pixman_region32_init_rect(&surface->input, 0, 0,
444 surface->geometry.width,
445 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200446
Pekka Paalanencd403622012-01-25 13:37:39 +0200447 /* transform.position is always in transformation_list */
448 if (surface->geometry.transformation_list.next ==
449 &surface->transform.position.link &&
450 surface->geometry.transformation_list.prev ==
451 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200452 weston_surface_update_transform_disable(surface);
453 } else {
454 if (weston_surface_update_transform_enable(surface) < 0)
455 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200456 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200457
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400458 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200459
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300460 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200461 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200462}
463
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200464WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100465weston_surface_to_global_fixed(struct weston_surface *surface,
466 wl_fixed_t sx, wl_fixed_t sy,
467 wl_fixed_t *x, wl_fixed_t *y)
468{
469 GLfloat xf, yf;
470
471 weston_surface_to_global_float(surface,
472 wl_fixed_to_double(sx),
473 wl_fixed_to_double(sy),
474 &xf, &yf);
475 *x = wl_fixed_from_double(xf);
476 *y = wl_fixed_from_double(yf);
477}
478
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200479static void
480surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100481 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200482{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200483 if (surface->transform.enabled) {
484 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
485
486 weston_matrix_transform(&surface->transform.inverse, &v);
487
488 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200489 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200490 "weston_surface_from_global(), divisor = %g\n",
491 v.f[3]);
492 *sx = 0;
493 *sy = 0;
494 return;
495 }
496
Pekka Paalanencd403622012-01-25 13:37:39 +0200497 *sx = v.f[0] / v.f[3];
498 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200499 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200500 *sx = x - surface->geometry.x;
501 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200502 }
503}
504
505WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100506weston_surface_from_global_fixed(struct weston_surface *surface,
507 wl_fixed_t x, wl_fixed_t y,
508 wl_fixed_t *sx, wl_fixed_t *sy)
509{
510 GLfloat sxf, syf;
511
Daniel Stonebd3489b2012-05-08 17:17:53 +0100512 surface_from_global_float(surface,
513 wl_fixed_to_double(x),
514 wl_fixed_to_double(y),
515 &sxf, &syf);
516 *sx = wl_fixed_from_double(sxf);
517 *sy = wl_fixed_from_double(syf);
518}
519
520WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200521weston_surface_from_global(struct weston_surface *surface,
522 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
523{
524 GLfloat sxf, syf;
525
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200526 surface_from_global_float(surface, x, y, &sxf, &syf);
527 *sx = floorf(sxf);
528 *sy = floorf(syf);
529}
530
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400531WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400532weston_surface_schedule_repaint(struct weston_surface *surface)
533{
534 struct weston_output *output;
535
536 wl_list_for_each(output, &surface->compositor->output_list, link)
537 if (surface->output_mask & (1 << output->id))
538 weston_output_schedule_repaint(output);
539}
540
541WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500542weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500543{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400544 pixman_region32_union_rect(&surface->damage, &surface->damage,
545 0, 0, surface->geometry.width,
546 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200547
Kristian Høgsberg98238702012-08-03 16:29:12 -0400548 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500549}
550
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400551WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500552weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200553 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400554{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200555 surface->geometry.x = x;
556 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200557 surface->geometry.width = width;
558 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200559 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400560}
561
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200562WL_EXPORT void
563weston_surface_set_position(struct weston_surface *surface,
564 GLfloat x, GLfloat y)
565{
566 surface->geometry.x = x;
567 surface->geometry.y = y;
568 surface->geometry.dirty = 1;
569}
570
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300571WL_EXPORT int
572weston_surface_is_mapped(struct weston_surface *surface)
573{
574 if (surface->output)
575 return 1;
576 else
577 return 0;
578}
579
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400580WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500581weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500582{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400583 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500584
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500586
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500588}
589
Tiago Vignatti9d393522012-02-10 16:26:19 +0200590static struct weston_surface *
591weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100592 wl_fixed_t x, wl_fixed_t y,
593 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200594{
595 struct weston_surface *surface;
596
597 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100598 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500599 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100600 wl_fixed_to_int(*sx),
601 wl_fixed_to_int(*sy),
602 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200603 return surface;
604 }
605
606 return NULL;
607}
608
609static void
Daniel Stone37816df2012-05-16 18:45:18 +0100610weston_device_repick(struct wl_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500611{
Daniel Stone37816df2012-05-16 18:45:18 +0100612 struct weston_seat *ws = (struct weston_seat *) seat;
Scott Moreau447013d2012-02-18 05:05:29 -0700613 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500614 struct weston_surface *surface, *focus;
615
Daniel Stonee9e92d22012-06-04 11:40:47 +0100616 if (!seat->pointer)
617 return;
618
Daniel Stone37816df2012-05-16 18:45:18 +0100619 surface = weston_compositor_pick_surface(ws->compositor,
620 seat->pointer->x,
621 seat->pointer->y,
622 &seat->pointer->current_x,
623 &seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500624
Daniel Stone37816df2012-05-16 18:45:18 +0100625 if (&surface->surface != seat->pointer->current) {
626 interface = seat->pointer->grab->interface;
Kristian Høgsberg4e99ac42012-06-04 16:12:38 -0400627 seat->pointer->current = &surface->surface;
Daniel Stone37816df2012-05-16 18:45:18 +0100628 interface->focus(seat->pointer->grab, &surface->surface,
629 seat->pointer->current_x,
630 seat->pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500631 }
632
Daniel Stone37816df2012-05-16 18:45:18 +0100633 focus = (struct weston_surface *) seat->pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500634 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100635 weston_surface_from_global_fixed(focus,
636 seat->pointer->x,
637 seat->pointer->y,
638 &seat->pointer->grab->x,
639 &seat->pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500640}
641
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400642static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500643weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400644{
Daniel Stone37816df2012-05-16 18:45:18 +0100645 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400646
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500647 if (!compositor->focus)
648 return;
649
Daniel Stone37816df2012-05-16 18:45:18 +0100650 wl_list_for_each(seat, &compositor->seat_list, link)
651 weston_device_repick(&seat->seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400652}
653
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400654WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500655weston_surface_unmap(struct weston_surface *surface)
656{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100657 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500658
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500659 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500660 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500661 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500662
Daniel Stone4dab5db2012-05-30 16:31:53 +0100663 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100664 if (seat->seat.keyboard &&
665 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100666 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100667 if (seat->seat.pointer &&
668 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100669 wl_pointer_set_focus(seat->seat.pointer,
670 NULL,
671 wl_fixed_from_int(0),
672 wl_fixed_from_int(0));
673 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500674
Kristian Høgsberg98238702012-08-03 16:29:12 -0400675 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500676}
677
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400678struct weston_frame_callback {
679 struct wl_resource resource;
680 struct wl_list link;
681};
682
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500683static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400684destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500685{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200686 int i;
687
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500688 struct weston_surface *surface =
689 container_of(resource,
690 struct weston_surface, surface.resource);
691 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400692 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400693
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300694 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500695 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400696
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200697 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200698
Benjamin Franzke06286262011-05-06 19:12:33 +0200699 if (surface->buffer)
700 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400701
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200702 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400703 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200704 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200705
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200706 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200707 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500708 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500709 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500710 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500711 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200712
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400713 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
714 wl_resource_destroy(&cb->resource);
715
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400716 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500717}
718
Alex Wu8811bf92012-02-28 18:07:54 +0800719WL_EXPORT void
720weston_surface_destroy(struct weston_surface *surface)
721{
722 /* Not a valid way to destroy a client surface */
723 assert(surface->surface.resource.client == NULL);
724
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400725 wl_signal_emit(&surface->surface.resource.destroy_signal,
726 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800727 destroy_surface(&surface->surface.resource);
728}
729
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100730static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200731ensure_textures(struct weston_surface *es, int num_textures)
732{
733 int i;
734
735 if (num_textures <= es->num_textures)
736 return;
737
738 for (i = es->num_textures; i < num_textures; i++) {
739 glGenTextures(1, &es->textures[i]);
740 glBindTexture(GL_TEXTURE_2D, es->textures[i]);
741 glTexParameteri(GL_TEXTURE_2D,
742 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
743 glTexParameteri(GL_TEXTURE_2D,
744 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
745 }
746 es->num_textures = num_textures;
747 glBindTexture(GL_TEXTURE_2D, 0);
748}
749
750static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400751weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100752{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500753 struct weston_surface *es = (struct weston_surface *) surface;
754 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400755 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200756 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100757
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300758 if (es->buffer) {
759 weston_buffer_post_release(es->buffer);
760 wl_list_remove(&es->buffer_destroy_listener.link);
761 }
762
763 es->buffer = buffer;
764
765 if (!buffer) {
766 if (weston_surface_is_mapped(es))
767 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200768 for (i = 0; i < es->num_images; i++) {
769 ec->destroy_image(ec->egl_display, es->images[i]);
770 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400771 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200772 es->num_images = 0;
773 glDeleteTextures(es->num_textures, es->textures);
774 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300775 return;
776 }
777
778 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400779 wl_signal_add(&es->buffer->resource.destroy_signal,
780 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300781
782 if (es->geometry.width != buffer->width ||
783 es->geometry.height != buffer->height) {
784 undef_region(&es->input);
785 pixman_region32_fini(&es->opaque);
786 pixman_region32_init(&es->opaque);
787 }
788
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100789 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200790 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Gwenole Beauchesne6d030492012-04-20 11:15:51 +0200791 es->shader = &ec->texture_shader_rgba;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200792
793 ensure_textures(es, 1);
794 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400795 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
796 es->pitch, es->buffer->height, 0,
797 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400798 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
799 es->blend = 0;
800 else
801 es->blend = 1;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200802 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400803 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200804 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400805 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200806 es->num_images = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300807
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400808 switch (format) {
809 case EGL_TEXTURE_RGB:
810 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200811 default:
812 num_planes = 1;
813 es->shader = &ec->texture_shader_rgba;
814 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400815 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200816 num_planes = 2;
817 es->shader = &ec->texture_shader_y_uv;
818 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400819 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200820 num_planes = 3;
821 es->shader = &ec->texture_shader_y_u_v;
822 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400823 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200824 num_planes = 2;
825 es->shader = &ec->texture_shader_y_xuxv;
826 break;
827 }
828
829 ensure_textures(es, num_planes);
830 for (i = 0; i < num_planes; i++) {
831 attribs[0] = EGL_WAYLAND_PLANE_WL;
832 attribs[1] = i;
833 attribs[2] = EGL_NONE;
834 es->images[i] = ec->create_image(ec->egl_display,
835 NULL,
836 EGL_WAYLAND_BUFFER_WL,
837 buffer, attribs);
838 if (!es->images[i])
839 continue;
840 es->num_images++;
841
842 glActiveTexture(GL_TEXTURE0 + i);
843 glBindTexture(GL_TEXTURE_2D, es->textures[i]);
844 ec->image_target_texture_2d(GL_TEXTURE_2D,
845 es->images[i]);
846 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400847
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300848 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200849 } else {
850 /* unhandled buffer type */
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100851 }
852}
853
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400854static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500855texture_region(struct weston_surface *es, pixman_region32_t *region)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500856{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500857 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500858 GLfloat *v, inv_width, inv_height;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200859 GLfloat sx, sy;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500860 pixman_box32_t *rectangles;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400861 unsigned int *p;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500862 int i, n;
863
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400864 rectangles = pixman_region32_rectangles(region, &n);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500865 v = wl_array_add(&ec->vertices, n * 16 * sizeof *v);
866 p = wl_array_add(&ec->indices, n * 6 * sizeof *p);
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200867 inv_width = 1.0 / es->pitch;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200868 inv_height = 1.0 / es->geometry.height;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400869
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500870 for (i = 0; i < n; i++, v += 16, p += 6) {
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200871 surface_from_global_float(es, rectangles[i].x1,
872 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500873 v[ 0] = rectangles[i].x1;
874 v[ 1] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200875 v[ 2] = sx * inv_width;
876 v[ 3] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500877
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200878 surface_from_global_float(es, rectangles[i].x1,
879 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500880 v[ 4] = rectangles[i].x1;
881 v[ 5] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200882 v[ 6] = sx * inv_width;
883 v[ 7] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500884
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200885 surface_from_global_float(es, rectangles[i].x2,
886 rectangles[i].y1, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500887 v[ 8] = rectangles[i].x2;
888 v[ 9] = rectangles[i].y1;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200889 v[10] = sx * inv_width;
890 v[11] = sy * inv_height;
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500891
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200892 surface_from_global_float(es, rectangles[i].x2,
893 rectangles[i].y2, &sx, &sy);
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500894 v[12] = rectangles[i].x2;
895 v[13] = rectangles[i].y2;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200896 v[14] = sx * inv_width;
897 v[15] = sy * inv_height;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500898
899 p[0] = i * 4 + 0;
900 p[1] = i * 4 + 1;
901 p[2] = i * 4 + 2;
902 p[3] = i * 4 + 2;
903 p[4] = i * 4 + 1;
904 p[5] = i * 4 + 3;
905 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500906
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400907 return n;
908}
909
Kristian Høgsberg68c479a2012-01-25 23:32:28 -0500910WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500911weston_surface_draw(struct weston_surface *es, struct weston_output *output,
912 pixman_region32_t *damage)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400913{
Scott Moreau9fb98242012-05-22 10:18:50 -0600914 GLfloat surface_rect[4] = { 0.0, 1.0, 0.0, 1.0 };
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500915 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400916 GLfloat *v;
917 pixman_region32_t repaint;
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400918 GLint filter;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200919 int i, n;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400920
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200921 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500922 pixman_region32_intersect(&repaint,
923 &es->transform.boundingbox, damage);
924 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +0200925
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400926 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200927 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400928
Kristian Høgsberg101cb652012-02-17 10:45:16 -0500929 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Scott Moreau9fb98242012-05-22 10:18:50 -0600930 if (es->blend || es->alpha < 1.0)
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400931 glEnable(GL_BLEND);
932 else
933 glDisable(GL_BLEND);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400934
Kristian Høgsberg07632622012-01-25 23:02:06 -0500935 if (ec->current_shader != es->shader) {
936 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -0500937 ec->current_shader = es->shader;
938 }
939
Kristian Høgsberg0452abc2012-01-31 15:38:36 -0500940 glUniformMatrix4fv(es->shader->proj_uniform,
941 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200942 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400943 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanenf55f5442012-02-02 16:49:05 +0200944 glUniform1f(es->shader->texwidth_uniform,
945 (GLfloat)es->geometry.width / es->pitch);
Scott Moreau9fb98242012-05-22 10:18:50 -0600946 if (es->blend)
947 glUniform4fv(es->shader->opaque_uniform, 1, es->opaque_rect);
948 else
949 glUniform4fv(es->shader->opaque_uniform, 1, surface_rect);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200950
Scott Moreauccbf29d2012-02-22 14:21:41 -0700951 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400952 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200953 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +0200954 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200955
956 n = texture_region(es, &repaint);
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400957
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200958 for (i = 0; i < es->num_textures; i++) {
959 glUniform1i(es->shader->tex_uniforms[i], i);
960 glActiveTexture(GL_TEXTURE0 + i);
961 glBindTexture(GL_TEXTURE_2D, es->textures[i]);
962 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
963 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
964 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -0400965
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500966 v = ec->vertices.data;
967 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
968 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400969 glEnableVertexAttribArray(0);
970 glEnableVertexAttribArray(1);
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200971
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500972 glDrawElements(GL_TRIANGLES, n * 6, GL_UNSIGNED_INT, ec->indices.data);
973
Pekka Paalanen3df327f2012-01-24 15:53:35 +0200974 glDisableVertexAttribArray(1);
975 glDisableVertexAttribArray(0);
976
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500977 ec->vertices.size = 0;
978 ec->indices.size = 0;
Pekka Paalanena9f8a212012-01-26 11:28:08 +0200979
980out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500981 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500982}
983
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500984WL_EXPORT void
985weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400986{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500987 wl_list_remove(&surface->layer_link);
988 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500989 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500990 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -0400991}
992
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400993WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500994weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400995{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500996 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -0400997
998 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500999 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001000}
1001
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001002WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001003weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001004{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001005 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001006 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001007
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001008 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001009 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001010}
1011
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001012WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001013weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001014{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001015 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001016
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001017 pixman_region32_union(&compositor->primary_plane.damage,
1018 &compositor->primary_plane.damage,
1019 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001020 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001021}
1022
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001023static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001024fade_frame(struct weston_animation *animation,
1025 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001026{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001027 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001028 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001029 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001030 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001031
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001032 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001033 compositor->fade.spring.timestamp = msecs;
1034
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001035 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001036 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001037 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1038 compositor->fade.spring.current);
1039 weston_surface_damage(surface);
1040
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001041 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001042 compositor->fade.spring.current =
1043 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001044 wl_list_remove(&animation->link);
1045 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001046
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001047 if (compositor->fade.spring.current < 0.001) {
1048 destroy_surface(&surface->surface.resource);
1049 compositor->fade.surface = NULL;
1050 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001051 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001052 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001053 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001054 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001055}
1056
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001057static void
1058update_shm_texture(struct weston_surface *surface)
1059{
1060#ifdef GL_UNPACK_ROW_LENGTH
1061 pixman_box32_t *rectangles;
1062 void *data;
1063 int i, n;
1064#endif
1065
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001066 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001067
1068 if (!surface->compositor->has_unpack_subimage) {
1069 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1070 surface->pitch, surface->buffer->height, 0,
1071 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1072 wl_shm_buffer_get_data(surface->buffer));
1073
1074 return;
1075 }
1076
1077#ifdef GL_UNPACK_ROW_LENGTH
1078 /* Mesa does not define GL_EXT_unpack_subimage */
1079 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1080 data = wl_shm_buffer_get_data(surface->buffer);
1081 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1082 for (i = 0; i < n; i++) {
1083 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1084 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1085 glTexSubImage2D(GL_TEXTURE_2D, 0,
1086 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001087 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001088 rectangles[i].y2 - rectangles[i].y1,
1089 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1090 }
1091#endif
1092}
1093
1094static void
1095surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001096 pixman_region32_t *opaque)
1097{
1098 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1099 update_shm_texture(surface);
1100
1101 if (surface->transform.enabled) {
1102 pixman_box32_t *extents;
1103
1104 extents = pixman_region32_extents(&surface->damage);
1105 surface_compute_bbox(surface, extents->x1, extents->y1,
1106 extents->x2 - extents->x1,
1107 extents->y2 - extents->y1,
1108 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001109 pixman_region32_translate(&surface->damage,
1110 -surface->plane->x,
1111 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001112 } else {
1113 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001114 surface->geometry.x - surface->plane->x,
1115 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001116 }
1117
1118 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001119 pixman_region32_union(&surface->plane->damage,
1120 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001121 empty_region(&surface->damage);
1122 pixman_region32_copy(&surface->clip, opaque);
1123 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
1124}
1125
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001126static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001127weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001128{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001129 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001130 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001131 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001132 struct weston_animation *animation, *next;
1133 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001134 struct wl_list frame_callback_list;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001135 pixman_region32_t opaque, output_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001136 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001137
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001138 weston_compositor_update_drag_surfaces(ec);
1139
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001140 width = output->current->width +
1141 output->border.left + output->border.right;
1142 height = output->current->height +
1143 output->border.top + output->border.bottom;
1144 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001145
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001146 /* Rebuild the surface list and update surface transforms up front. */
1147 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001148 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001149 wl_list_for_each(layer, &ec->layer_list, link) {
1150 wl_list_for_each(es, &layer->surface_list, layer_link) {
1151 weston_surface_update_transform(es);
1152 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001153 if (es->output == output) {
1154 wl_list_insert_list(&frame_callback_list,
1155 &es->frame_callback_list);
1156 wl_list_init(&es->frame_callback_list);
1157 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001158 }
1159 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001160
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001161 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001162 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001163 else
1164 wl_list_for_each(es, &ec->surface_list, link)
1165 weston_surface_move_to_plane(es, &ec->primary_plane);
1166
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001167
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001168 pixman_region32_init(&opaque);
1169
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001170 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001171 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001172
1173 pixman_region32_init(&output_damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001174 pixman_region32_union(&output_damage, &ec->primary_plane.damage,
1175 &output->previous_damage);
1176 pixman_region32_copy(&output->previous_damage,
1177 &ec->primary_plane.damage);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001178 pixman_region32_intersect(&output_damage,
1179 &output_damage, &output->region);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001180 pixman_region32_subtract(&ec->primary_plane.damage,
1181 &ec->primary_plane.damage, &output->region);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05001182
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001183 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001184
Scott Moreauccbf29d2012-02-22 14:21:41 -07001185 if (output->dirty)
1186 weston_output_update_matrix(output);
1187
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001188 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001189
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001190 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001191
Kristian Høgsbergef044142011-06-21 15:02:12 -04001192 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001193
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001194 weston_compositor_repick(ec);
1195 wl_event_loop_dispatch(ec->input_loop, 0);
1196
Jonas Ådahldb773762012-06-13 00:01:21 +02001197 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001198 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001199 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001200 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001201 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001202
Scott Moreaud64cf212012-06-08 19:40:54 -06001203 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001204 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001205 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001206 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001207}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001208
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001209static int
1210weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001211{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001212 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001213
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001214 wl_event_loop_dispatch(compositor->input_loop, 0);
1215
1216 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001217}
1218
1219WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001220weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001221{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001222 struct weston_compositor *compositor = output->compositor;
1223 struct wl_event_loop *loop =
1224 wl_display_get_event_loop(compositor->wl_display);
1225 int fd;
1226
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001227 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001228 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001229 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001230 return;
1231 }
1232
1233 output->repaint_scheduled = 0;
1234 if (compositor->input_loop_source)
1235 return;
1236
1237 fd = wl_event_loop_get_fd(compositor->input_loop);
1238 compositor->input_loop_source =
1239 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1240 weston_compositor_read_input, compositor);
1241}
1242
1243static void
1244idle_repaint(void *data)
1245{
1246 struct weston_output *output = data;
1247
1248 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001249}
1250
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001251WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001252weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1253{
1254 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001255 if (below != NULL)
1256 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001257}
1258
1259WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001260weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001261{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001262 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001263 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001264
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001265 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001266 return;
1267
Kristian Høgsbergef044142011-06-21 15:02:12 -04001268 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001269 output->repaint_needed = 1;
1270 if (output->repaint_scheduled)
1271 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001272
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001273 wl_event_loop_add_idle(loop, idle_repaint, output);
1274 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001275
1276 if (compositor->input_loop_source) {
1277 wl_event_source_remove(compositor->input_loop_source);
1278 compositor->input_loop_source = NULL;
1279 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001280}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001281
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001282WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001283weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1284{
1285 struct weston_output *output;
1286
1287 wl_list_for_each(output, &compositor->output_list, link)
1288 weston_output_schedule_repaint(output);
1289}
1290
1291WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001292weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001293{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001294 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001295 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001296
Scott Moreau9d1b1122012-06-08 19:40:53 -06001297 output = container_of(compositor->output_list.next,
1298 struct weston_output, link);
1299
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001300 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001301 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001302 return;
1303
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001304 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001305 surface = weston_surface_create(compositor);
1306 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001307 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001308 wl_list_insert(&compositor->fade_layer.surface_list,
1309 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001310 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001311 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001312 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001313 }
1314
1315 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001316 if (wl_list_empty(&compositor->fade.animation.link)) {
1317 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001318 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001319 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001320 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001321}
1322
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001323static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001324surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001325{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001326 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001327}
1328
Casey Dahlin96d8a752012-04-19 22:50:07 -04001329static struct wl_resource *
1330find_resource_for_client(struct wl_list *list, struct wl_client *client)
1331{
1332 struct wl_resource *r;
1333
1334 wl_list_for_each(r, list, link) {
1335 if (r->client == client)
1336 return r;
1337 }
1338
1339 return NULL;
1340}
1341
Casey Dahlin9074db52012-04-19 22:50:09 -04001342static void
1343weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1344{
1345 uint32_t different = es->output_mask ^ mask;
1346 uint32_t entered = mask & different;
1347 uint32_t left = es->output_mask & different;
1348 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001349 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001350 struct wl_client *client = es->surface.resource.client;
1351
1352 if (es->surface.resource.client == NULL)
1353 return;
1354 if (different == 0)
1355 return;
1356
1357 es->output_mask = mask;
1358 wl_list_for_each(output, &es->compositor->output_list, link) {
1359 if (1 << output->id & different)
1360 resource =
1361 find_resource_for_client(&output->resource_list,
1362 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001363 if (resource == NULL)
1364 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001365 if (1 << output->id & entered)
1366 wl_surface_send_enter(&es->surface.resource, resource);
1367 if (1 << output->id & left)
1368 wl_surface_send_leave(&es->surface.resource, resource);
1369 }
1370}
1371
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001372WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001373weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001374{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001375 struct weston_compositor *ec = es->compositor;
1376 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001377 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001378 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001379 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001380
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001381 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001382 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001383 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001384 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001385 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001386 pixman_region32_intersect(&region, &es->transform.boundingbox,
1387 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001388
1389 e = pixman_region32_extents(&region);
1390 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1391
Casey Dahlin9074db52012-04-19 22:50:09 -04001392 if (area > 0)
1393 mask |= 1 << output->id;
1394
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001395 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001396 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001397 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001398 }
1399 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001400 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001401
1402 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001403 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001404}
1405
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001406static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001407surface_attach(struct wl_client *client,
1408 struct wl_resource *resource,
1409 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1410{
1411 struct weston_surface *es = resource->data;
1412 struct wl_buffer *buffer = NULL;
1413
1414 if (buffer_resource)
1415 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001416
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001417 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001418
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001419 if (buffer && es->configure)
1420 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001421}
1422
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001423static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001424surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001425 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001426 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001427{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001428 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001429
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001430 pixman_region32_union_rect(&es->damage, &es->damage,
1431 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001432 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001433}
1434
Kristian Høgsberg33418202011-08-16 23:01:28 -04001435static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001436destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001437{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001438 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001439
1440 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001441 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001442}
1443
1444static void
1445surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001446 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001447{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001448 struct weston_frame_callback *cb;
1449 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001450
1451 cb = malloc(sizeof *cb);
1452 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001453 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001454 return;
1455 }
1456
1457 cb->resource.object.interface = &wl_callback_interface;
1458 cb->resource.object.id = callback;
1459 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001460 cb->resource.client = client;
1461 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001462
1463 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001464 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001465}
1466
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001467static void
1468surface_set_opaque_region(struct wl_client *client,
1469 struct wl_resource *resource,
1470 struct wl_resource *region_resource)
1471{
1472 struct weston_surface *surface = resource->data;
1473 struct weston_region *region;
1474
1475 pixman_region32_fini(&surface->opaque);
1476
1477 if (region_resource) {
1478 region = region_resource->data;
1479 pixman_region32_init_rect(&surface->opaque, 0, 0,
1480 surface->geometry.width,
1481 surface->geometry.height);
1482 pixman_region32_intersect(&surface->opaque,
1483 &surface->opaque, &region->region);
1484 } else {
1485 pixman_region32_init(&surface->opaque);
1486 }
1487
1488 surface->geometry.dirty = 1;
1489}
1490
1491static void
1492surface_set_input_region(struct wl_client *client,
1493 struct wl_resource *resource,
1494 struct wl_resource *region_resource)
1495{
1496 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001497 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001498
1499 if (region_resource) {
1500 region = region_resource->data;
1501 pixman_region32_init_rect(&surface->input, 0, 0,
1502 surface->geometry.width,
1503 surface->geometry.height);
1504 pixman_region32_intersect(&surface->input,
1505 &surface->input, &region->region);
1506 } else {
1507 pixman_region32_init_rect(&surface->input, 0, 0,
1508 surface->geometry.width,
1509 surface->geometry.height);
1510 }
1511
Kristian Høgsberg98238702012-08-03 16:29:12 -04001512 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001513}
1514
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001515static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001516 surface_destroy,
1517 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001518 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001519 surface_frame,
1520 surface_set_opaque_region,
1521 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001522};
1523
1524static void
1525compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001526 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001527{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001528 struct weston_compositor *ec = resource->data;
1529 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001530
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001531 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001532 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001533 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001534 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001535 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001536
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001537 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001538
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001539 surface->surface.resource.object.id = id;
1540 surface->surface.resource.object.interface = &wl_surface_interface;
1541 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001542 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001543 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001544
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001545 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001546}
1547
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001548static void
1549destroy_region(struct wl_resource *resource)
1550{
1551 struct weston_region *region =
1552 container_of(resource, struct weston_region, resource);
1553
1554 pixman_region32_fini(&region->region);
1555 free(region);
1556}
1557
1558static void
1559region_destroy(struct wl_client *client, struct wl_resource *resource)
1560{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001561 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001562}
1563
1564static void
1565region_add(struct wl_client *client, struct wl_resource *resource,
1566 int32_t x, int32_t y, int32_t width, int32_t height)
1567{
1568 struct weston_region *region = resource->data;
1569
1570 pixman_region32_union_rect(&region->region, &region->region,
1571 x, y, width, height);
1572}
1573
1574static void
1575region_subtract(struct wl_client *client, struct wl_resource *resource,
1576 int32_t x, int32_t y, int32_t width, int32_t height)
1577{
1578 struct weston_region *region = resource->data;
1579 pixman_region32_t rect;
1580
1581 pixman_region32_init_rect(&rect, x, y, width, height);
1582 pixman_region32_subtract(&region->region, &region->region, &rect);
1583 pixman_region32_fini(&rect);
1584}
1585
1586static const struct wl_region_interface region_interface = {
1587 region_destroy,
1588 region_add,
1589 region_subtract
1590};
1591
1592static void
1593compositor_create_region(struct wl_client *client,
1594 struct wl_resource *resource, uint32_t id)
1595{
1596 struct weston_region *region;
1597
1598 region = malloc(sizeof *region);
1599 if (region == NULL) {
1600 wl_resource_post_no_memory(resource);
1601 return;
1602 }
1603
1604 region->resource.destroy = destroy_region;
1605
1606 region->resource.object.id = id;
1607 region->resource.object.interface = &wl_region_interface;
1608 region->resource.object.implementation =
1609 (void (**)(void)) &region_interface;
1610 region->resource.data = region;
1611
1612 pixman_region32_init(&region->region);
1613
1614 wl_client_add_resource(client, &region->resource);
1615}
1616
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001617static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001618 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001619 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001620};
1621
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001622WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001623weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001624{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001625 compositor->state = WESTON_COMPOSITOR_ACTIVE;
1626 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001627
1628 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02001629 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001630}
1631
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001632static void
1633weston_compositor_dpms_on(struct weston_compositor *compositor)
1634{
1635 struct weston_output *output;
1636
1637 wl_list_for_each(output, &compositor->output_list, link)
1638 if (output->set_dpms)
1639 output->set_dpms(output, WESTON_DPMS_ON);
1640}
1641
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001642WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001643weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001644{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001645 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
1646 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001647 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001648 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001649 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02001650 }
1651}
1652
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001653static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001654weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001655{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001656 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001657 compositor->idle_inhibit++;
1658}
1659
1660static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001661weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001662{
1663 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001664 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001665}
1666
1667static int
1668idle_handler(void *data)
1669{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001670 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001671
1672 if (compositor->idle_inhibit)
1673 return 1;
1674
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001675 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001676
1677 return 1;
1678}
1679
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001680WL_EXPORT void
1681weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
1682{
1683 pixman_region32_init(&plane->damage);
1684 plane->x = x;
1685 plane->y = y;
1686}
1687
1688WL_EXPORT void
1689weston_plane_release(struct weston_plane *plane)
1690{
1691 pixman_region32_fini(&plane->damage);
1692}
1693
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001694static void
Daniel Stone37816df2012-05-16 18:45:18 +01001695weston_seat_update_drag_surface(struct wl_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001696
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001697static void
Daniel Stone37816df2012-05-16 18:45:18 +01001698clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001699{
Daniel Stone37816df2012-05-16 18:45:18 +01001700 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001701 struct weston_output *output, *prev = NULL;
1702 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001703
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001704 x = wl_fixed_to_int(*fx);
1705 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01001706 old_x = wl_fixed_to_int(seat->seat.pointer->x);
1707 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001708
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001709 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001710 if (pixman_region32_contains_point(&output->region,
1711 x, y, NULL))
1712 valid = 1;
1713 if (pixman_region32_contains_point(&output->region,
1714 old_x, old_y, NULL))
1715 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001716 }
Daniel Stone37816df2012-05-16 18:45:18 +01001717
Kristian Høgsberg34829de2012-05-09 22:21:14 -04001718 if (!valid) {
1719 if (x < prev->x)
1720 *fx = wl_fixed_from_int(prev->x);
1721 else if (x >= prev->x + prev->current->width)
1722 *fx = wl_fixed_from_int(prev->x +
1723 prev->current->width - 1);
1724 if (y < prev->y)
1725 *fy = wl_fixed_from_int(prev->y);
1726 else if (y >= prev->y + prev->current->height)
1727 *fy = wl_fixed_from_int(prev->y +
1728 prev->current->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001729 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001730}
1731
1732WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001733notify_motion(struct wl_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001734{
1735 const struct wl_pointer_grab_interface *interface;
Daniel Stone37816df2012-05-16 18:45:18 +01001736 struct weston_seat *ws = (struct weston_seat *) seat;
1737 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001738 struct weston_output *output;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001739 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001740
1741 weston_compositor_activity(ec);
1742
Daniel Stone37816df2012-05-16 18:45:18 +01001743 clip_pointer_motion(ws, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04001744
Daniel Stone37816df2012-05-16 18:45:18 +01001745 weston_seat_update_drag_surface(seat,
1746 x - seat->pointer->x,
1747 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001748
Daniel Stone37816df2012-05-16 18:45:18 +01001749 seat->pointer->x = x;
1750 seat->pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001751
1752 ix = wl_fixed_to_int(x);
1753 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05001754
Scott Moreauccbf29d2012-02-22 14:21:41 -07001755 wl_list_for_each(output, &ec->output_list, link)
1756 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001757 pixman_region32_contains_point(&output->region,
1758 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06001759 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07001760
Daniel Stone37816df2012-05-16 18:45:18 +01001761 weston_device_repick(seat);
1762 interface = seat->pointer->grab->interface;
1763 interface->motion(seat->pointer->grab, time,
1764 seat->pointer->grab->x, seat->pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001765
Daniel Stone37816df2012-05-16 18:45:18 +01001766 if (ws->sprite) {
1767 weston_surface_set_position(ws->sprite,
1768 ix - ws->hotspot_x,
1769 iy - ws->hotspot_y);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001770 weston_surface_schedule_repaint(ws->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05001771 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05001772}
1773
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001774WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001775weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01001776 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001777{
Daniel Stone37816df2012-05-16 18:45:18 +01001778 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001779
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03001780 if (seat->seat.keyboard) {
1781 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
1782 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04001783 }
1784
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001785 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05001786}
1787
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001788WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001789notify_button(struct wl_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001790 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001791{
Daniel Stone37816df2012-05-16 18:45:18 +01001792 struct weston_seat *ws = (struct weston_seat *) seat;
1793 struct weston_compositor *compositor = ws->compositor;
1794 struct weston_surface *focus =
1795 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001796 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05001797
Daniel Stone4dbadb12012-05-30 16:31:51 +01001798 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001799 if (compositor->ping_handler && focus)
1800 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001801 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001802 if (seat->pointer->button_count == 0) {
1803 seat->pointer->grab_button = button;
1804 seat->pointer->grab_time = time;
1805 seat->pointer->grab_x = seat->pointer->x;
1806 seat->pointer->grab_y = seat->pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001807 }
Daniel Stone37816df2012-05-16 18:45:18 +01001808 seat->pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001809 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001810 weston_compositor_idle_release(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001811 seat->pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05001812 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001813
Daniel Stone325fc2d2012-05-30 16:31:58 +01001814 weston_compositor_run_button_binding(compositor, ws, time, button,
1815 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05001816
Daniel Stone37816df2012-05-16 18:45:18 +01001817 seat->pointer->grab->interface->button(seat->pointer->grab, time,
1818 button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05001819
Daniel Stone37816df2012-05-16 18:45:18 +01001820 if (seat->pointer->button_count == 1)
1821 seat->pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001822 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05001823}
1824
Scott Moreau210d0792012-03-22 10:47:01 -06001825WL_EXPORT void
Daniel Stone878f0b72012-05-30 16:31:57 +01001826notify_axis(struct wl_seat *seat, uint32_t time, uint32_t axis,
1827 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06001828{
Daniel Stone37816df2012-05-16 18:45:18 +01001829 struct weston_seat *ws = (struct weston_seat *) seat;
1830 struct weston_compositor *compositor = ws->compositor;
1831 struct weston_surface *focus =
1832 (struct weston_surface *) seat->pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001833 uint32_t serial = wl_display_next_serial(compositor->wl_display);
1834
1835 if (compositor->ping_handler && focus)
1836 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06001837
1838 weston_compositor_activity(compositor);
1839
Scott Moreau6a3633d2012-03-20 08:47:59 -06001840 if (value)
Daniel Stone325fc2d2012-05-30 16:31:58 +01001841 weston_compositor_run_axis_binding(compositor, ws, time, axis,
Daniel Stone0c1e46e2012-05-30 16:31:59 +01001842 value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06001843 else
1844 return;
1845
Daniel Stone37816df2012-05-16 18:45:18 +01001846 if (seat->pointer->focus_resource)
Daniel Stone878f0b72012-05-30 16:31:57 +01001847 wl_pointer_send_axis(seat->pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01001848 value);
Scott Moreau210d0792012-03-22 10:47:01 -06001849}
1850
Daniel Stone05d58682012-06-22 13:21:38 +01001851WL_EXPORT void
1852notify_modifiers(struct wl_seat *wl_seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05001853{
Daniel Stone05d58682012-06-22 13:21:38 +01001854 struct weston_seat *seat = (struct weston_seat *) wl_seat;
1855 struct wl_keyboard *keyboard = &seat->keyboard;
1856 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01001857 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001858 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01001859 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01001860 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01001861
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001862 /* Serialize and update our internal state, checking to see if it's
1863 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01001864 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
1865 XKB_STATE_DEPRESSED);
1866 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
1867 XKB_STATE_LATCHED);
1868 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
1869 XKB_STATE_LOCKED);
1870 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01001871 XKB_STATE_EFFECTIVE);
1872
Daniel Stone71c38772012-06-22 13:21:30 +01001873 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
1874 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
1875 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
1876 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01001877 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01001878
Daniel Stone71c38772012-06-22 13:21:30 +01001879 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
1880 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
1881 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
1882 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05001883
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001884 /* And update the modifier_state for bindings. */
1885 mods_lookup = mods_depressed | mods_latched;
1886 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01001887 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001888 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01001889 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001890 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01001891 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01001892 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04001893 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
1894 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01001895
Daniel Stone8c8164f2012-05-30 16:31:45 +01001896 /* Finally, notify the compositor that LEDs have changed. */
1897 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001898 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001899 leds |= LED_NUM_LOCK;
1900 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001901 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001902 leds |= LED_CAPS_LOCK;
1903 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01001904 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01001905 leds |= LED_SCROLL_LOCK;
1906 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01001907 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01001908 seat->xkb_state.leds = leds;
1909
Daniel Stone05d58682012-06-22 13:21:38 +01001910 if (changed) {
1911 grab->interface->modifiers(grab,
1912 serial,
1913 keyboard->modifiers.mods_depressed,
1914 keyboard->modifiers.mods_latched,
1915 keyboard->modifiers.mods_locked,
1916 keyboard->modifiers.group);
1917 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001918}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04001919
Daniel Stone05d58682012-06-22 13:21:38 +01001920static void
1921update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001922 enum wl_keyboard_key_state state)
1923{
1924 enum xkb_key_direction direction;
1925
1926 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1927 direction = XKB_KEY_DOWN;
1928 else
1929 direction = XKB_KEY_UP;
1930
1931 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
1932 * broken keycode system, which starts at 8. */
1933 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
1934
Daniel Stone05d58682012-06-22 13:21:38 +01001935 notify_modifiers(&seat->seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01001936}
1937
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001938WL_EXPORT void
Daniel Stonec9785ea2012-05-30 16:31:52 +01001939notify_key(struct wl_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001940 enum wl_keyboard_key_state state,
1941 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001942{
Daniel Stone37816df2012-05-16 18:45:18 +01001943 struct weston_seat *ws = (struct weston_seat *) seat;
1944 struct weston_compositor *compositor = ws->compositor;
1945 struct weston_surface *focus =
Kristian Høgsberg8fe8d242012-06-22 16:57:21 -04001946 (struct weston_surface *) seat->keyboard->focus;
Daniel Stone7ace3902012-05-30 16:31:40 +01001947 struct wl_keyboard_grab *grab = seat->keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06001948 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001949 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04001950
Daniel Stonec9785ea2012-05-30 16:31:52 +01001951 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06001952 if (compositor->ping_handler && focus)
1953 compositor->ping_handler(focus, serial);
1954
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001955 weston_compositor_idle_inhibit(compositor);
Daniel Stone37816df2012-05-16 18:45:18 +01001956 seat->keyboard->grab_key = key;
1957 seat->keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07001958 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001959 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07001960 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001961
Daniel Stone37816df2012-05-16 18:45:18 +01001962 end = seat->keyboard->keys.data + seat->keyboard->keys.size;
1963 for (k = seat->keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01001964 if (*k == key) {
1965 /* Ignore server-generated repeats. */
1966 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
1967 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001968 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01001969 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001970 }
Daniel Stone37816df2012-05-16 18:45:18 +01001971 seat->keyboard->keys.size = (void *) end - seat->keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01001972 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Daniel Stone37816df2012-05-16 18:45:18 +01001973 k = wl_array_add(&seat->keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05001974 *k = key;
1975 }
Ray Strodee96dcb82008-12-20 02:00:49 -05001976
Scott Moreaubefa6532012-06-11 14:59:31 -06001977 if (grab == &seat->keyboard->default_grab) {
Daniel Stone325fc2d2012-05-30 16:31:58 +01001978 weston_compositor_run_key_binding(compositor, ws, time, key,
1979 state);
Scott Moreaubefa6532012-06-11 14:59:31 -06001980 grab = seat->keyboard->grab;
1981 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05001982
Daniel Stone7ace3902012-05-30 16:31:40 +01001983 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01001984
1985 if (update_state == STATE_UPDATE_AUTOMATIC) {
1986 update_modifier_state(ws,
1987 wl_display_get_serial(compositor->wl_display),
1988 key,
1989 state);
1990 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04001991}
1992
Kristian Høgsberg1c562182011-05-02 22:09:20 -04001993WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01001994notify_pointer_focus(struct wl_seat *seat, struct weston_output *output,
1995 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001996{
Daniel Stone37816df2012-05-16 18:45:18 +01001997 struct weston_seat *ws = (struct weston_seat *) seat;
1998 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05001999
2000 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002001 weston_seat_update_drag_surface(seat,
2002 x - seat->pointer->x,
2003 y - seat->pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002004
Daniel Stone37816df2012-05-16 18:45:18 +01002005 seat->pointer->x = x;
2006 seat->pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002007 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002008 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002009 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002010 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002011 /* FIXME: We should call wl_pointer_set_focus(seat,
2012 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002013 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002014}
2015
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002016static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002017destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002018{
Daniel Stone37816df2012-05-16 18:45:18 +01002019 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002020
Daniel Stone37816df2012-05-16 18:45:18 +01002021 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002022 saved_kbd_focus_listener);
2023
Daniel Stone37816df2012-05-16 18:45:18 +01002024 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002025}
2026
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002027WL_EXPORT void
Daniel Stoned6da09e2012-06-22 13:21:29 +01002028notify_keyboard_focus_in(struct wl_seat *seat, struct wl_array *keys,
2029 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002030{
Daniel Stone37816df2012-05-16 18:45:18 +01002031 struct weston_seat *ws = (struct weston_seat *) seat;
2032 struct weston_compositor *compositor = ws->compositor;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002033 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002034 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002035
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002036 serial = wl_display_next_serial(compositor->wl_display);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002037 wl_array_copy(&seat->keyboard->keys, keys);
2038 wl_array_for_each(k, &seat->keyboard->keys) {
2039 weston_compositor_idle_inhibit(compositor);
2040 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002041 update_modifier_state(ws, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002042 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002043 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002044
Daniel Stoneef172672012-06-22 13:21:32 +01002045 /* Run key bindings after we've updated the state. */
2046 wl_array_for_each(k, &seat->keyboard->keys) {
2047 weston_compositor_run_key_binding(compositor, ws, 0, *k,
2048 WL_KEYBOARD_KEY_STATE_PRESSED);
2049 }
2050
Daniel Stoned6da09e2012-06-22 13:21:29 +01002051 surface = ws->saved_kbd_focus;
2052
2053 if (surface) {
2054 wl_list_remove(&ws->saved_kbd_focus_listener.link);
2055 wl_keyboard_set_focus(ws->seat.keyboard, surface);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002056 ws->saved_kbd_focus = NULL;
2057 }
2058}
2059
2060WL_EXPORT void
2061notify_keyboard_focus_out(struct wl_seat *seat)
2062{
2063 struct weston_seat *ws = (struct weston_seat *) seat;
2064 struct weston_compositor *compositor = ws->compositor;
2065 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002066 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002067
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002068 serial = wl_display_next_serial(compositor->wl_display);
2069 wl_array_for_each(k, &seat->keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002070 weston_compositor_idle_release(compositor);
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002071 update_modifier_state(ws, serial, *k,
2072 WL_KEYBOARD_KEY_STATE_RELEASED);
2073 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002074
2075 ws->modifier_state = 0;
2076
2077 surface = ws->seat.keyboard->focus;
2078
2079 if (surface) {
2080 ws->saved_kbd_focus = surface;
2081 ws->saved_kbd_focus_listener.notify =
2082 destroy_device_saved_kbd_focus;
2083 wl_signal_add(&surface->resource.destroy_signal,
2084 &ws->saved_kbd_focus_listener);
2085 }
2086
2087 wl_keyboard_set_focus(ws->seat.keyboard, NULL);
2088 /* FIXME: We really need keyboard grab cancel here to
2089 * let the grab shut down properly. As it is we leak
2090 * the grab data. */
2091 wl_keyboard_end_grab(ws->seat.keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002092}
2093
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002094static void
Daniel Stone37816df2012-05-16 18:45:18 +01002095touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002096{
Daniel Stone37816df2012-05-16 18:45:18 +01002097 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002098 struct wl_resource *resource;
2099
Pekka Paalanen56464252012-07-31 13:21:08 +03002100 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002101 return;
2102
Pekka Paalanen56464252012-07-31 13:21:08 +03002103 if (seat->touch->focus_resource)
2104 wl_list_remove(&seat->touch->focus_listener.link);
2105 seat->touch->focus = NULL;
2106 seat->touch->focus_resource = NULL;
2107
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002108 if (surface) {
2109 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002110 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002111 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002112 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002113 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002114 return;
2115 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002116
Daniel Stone37816df2012-05-16 18:45:18 +01002117 seat->touch->focus = surface;
2118 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002119 wl_signal_add(&resource->destroy_signal,
2120 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002121 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002122}
2123
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002124/**
2125 * notify_touch - emulates button touches and notifies surfaces accordingly.
2126 *
2127 * It assumes always the correct cycle sequence until it gets here: touch_down
2128 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2129 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002130 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002131 */
2132WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002133notify_touch(struct wl_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002134 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002135{
Daniel Stone37816df2012-05-16 18:45:18 +01002136 struct weston_seat *ws = (struct weston_seat *) seat;
2137 struct weston_compositor *ec = ws->compositor;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002138 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002139 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002140 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002141
2142 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002143 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002144 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002145
Daniel Stone37816df2012-05-16 18:45:18 +01002146 ws->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002147
2148 /* the first finger down picks the surface, and all further go
2149 * to that surface for the remainder of the touch session i.e.
2150 * until all touch points are up again. */
Daniel Stone37816df2012-05-16 18:45:18 +01002151 if (ws->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002152 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Daniel Stone37816df2012-05-16 18:45:18 +01002153 touch_set_focus(ws, &es->surface);
Pekka Paalanen56464252012-07-31 13:21:08 +03002154 } else if (seat->touch->focus) {
2155 es = (struct weston_surface *)seat->touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002156 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002157 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002158
Pekka Paalanen56464252012-07-31 13:21:08 +03002159 if (seat->touch->focus_resource && seat->touch->focus)
2160 wl_touch_send_down(seat->touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002161 serial, time,
Pekka Paalanen56464252012-07-31 13:21:08 +03002162 &seat->touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002163 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002164 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002165 case WL_TOUCH_MOTION:
Pekka Paalanen56464252012-07-31 13:21:08 +03002166 es = (struct weston_surface *)seat->touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002167 if (!es)
2168 break;
2169
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002170 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Pekka Paalanen56464252012-07-31 13:21:08 +03002171 if (seat->touch->focus_resource)
2172 wl_touch_send_motion(seat->touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002173 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002174 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002175 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002176 weston_compositor_idle_release(ec);
Daniel Stone37816df2012-05-16 18:45:18 +01002177 ws->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002178
Pekka Paalanen56464252012-07-31 13:21:08 +03002179 if (seat->touch->focus_resource)
2180 wl_touch_send_up(seat->touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002181 serial, time, touch_id);
2182 if (ws->num_tp == 0)
2183 touch_set_focus(ws, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002184 break;
2185 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002186}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002187
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002188static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002189pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2190{
2191 struct weston_seat *seat = container_of(listener, struct weston_seat,
2192 sprite_destroy_listener);
2193
2194 seat->sprite = NULL;
2195}
2196
2197static void
2198pointer_cursor_surface_configure(struct weston_surface *es,
2199 int32_t dx, int32_t dy)
2200{
2201 struct weston_seat *seat = es->private;
2202 int x, y;
2203
2204 assert(es == seat->sprite);
2205
2206 seat->hotspot_x -= dx;
2207 seat->hotspot_y -= dy;
2208
2209 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2210 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2211
2212 weston_surface_configure(seat->sprite, x, y,
2213 es->buffer->width, es->buffer->height);
2214
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002215 empty_region(&es->input);
2216
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002217 if (!weston_surface_is_mapped(es)) {
2218 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2219 &es->layer_link);
2220 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002221 }
2222}
2223
2224static void
2225pointer_unmap_sprite(struct weston_seat *seat)
2226{
2227 if (weston_surface_is_mapped(seat->sprite))
2228 weston_surface_unmap(seat->sprite);
2229
2230 wl_list_remove(&seat->sprite_destroy_listener.link);
2231 seat->sprite->configure = NULL;
2232 seat->sprite->private = NULL;
2233 seat->sprite = NULL;
2234}
2235
2236static void
2237pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2238 uint32_t serial, struct wl_resource *surface_resource,
2239 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002240{
Daniel Stone37816df2012-05-16 18:45:18 +01002241 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002242 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002243
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002244 if (surface_resource)
2245 surface = container_of(surface_resource->data,
2246 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002247
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002248 if (seat->seat.pointer->focus == NULL)
2249 return;
2250 if (seat->seat.pointer->focus->resource.client != client)
2251 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002252 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002253 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002254
2255 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002256 if (surface->configure) {
2257 wl_resource_post_error(&surface->surface.resource,
2258 WL_DISPLAY_ERROR_INVALID_OBJECT,
2259 "surface->configure already "
2260 "set");
2261 return;
2262 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002263 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002264
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002265 if (seat->sprite)
2266 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002267
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002268 if (!surface)
2269 return;
2270
2271 wl_signal_add(&surface->surface.resource.destroy_signal,
2272 &seat->sprite_destroy_listener);
2273
2274 surface->configure = pointer_cursor_surface_configure;
2275 surface->private = seat;
2276 empty_region(&surface->input);
2277
2278 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002279 seat->hotspot_x = x;
2280 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002281
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002282 weston_surface_set_position(surface,
2283 wl_fixed_to_int(seat->seat.pointer->x) - x,
2284 wl_fixed_to_int(seat->seat.pointer->y) - y);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002285}
2286
Daniel Stone37816df2012-05-16 18:45:18 +01002287static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002288 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002289};
2290
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002291static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002292handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002293{
Daniel Stone37816df2012-05-16 18:45:18 +01002294 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002295
Daniel Stone37816df2012-05-16 18:45:18 +01002296 seat = container_of(listener, struct weston_seat,
2297 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002298
Daniel Stone37816df2012-05-16 18:45:18 +01002299 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002300}
2301
Casey Dahlin9074db52012-04-19 22:50:09 -04002302static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002303{
2304 wl_list_remove(&resource->link);
2305 free(resource);
2306}
2307
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002308static void
Daniel Stone37816df2012-05-16 18:45:18 +01002309seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2310 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002311{
Daniel Stone37816df2012-05-16 18:45:18 +01002312 struct weston_seat *seat = resource->data;
2313 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002314
Daniel Stone37816df2012-05-16 18:45:18 +01002315 if (!seat->seat.pointer)
2316 return;
2317
2318 cr = wl_client_add_object(client, &wl_pointer_interface,
2319 &pointer_interface, id, seat);
2320 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002321 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002322
2323 if (seat->seat.pointer->focus &&
2324 seat->seat.pointer->focus->resource.client == client) {
2325 struct weston_surface *surface;
2326 wl_fixed_t sx, sy;
2327
2328 surface = (struct weston_surface *) seat->seat.pointer->focus;
2329 weston_surface_from_global_fixed(surface,
2330 seat->seat.pointer->x,
2331 seat->seat.pointer->y,
2332 &sx,
2333 &sy);
2334 wl_pointer_set_focus(seat->seat.pointer,
2335 seat->seat.pointer->focus,
2336 sx,
2337 sy);
2338 }
Daniel Stone37816df2012-05-16 18:45:18 +01002339}
2340
2341static void
2342seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2343 uint32_t id)
2344{
2345 struct weston_seat *seat = resource->data;
2346 struct wl_resource *cr;
2347
2348 if (!seat->seat.keyboard)
2349 return;
2350
2351 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2352 seat);
2353 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002354 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002355
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002356 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2357 seat->xkb_info.keymap_fd,
2358 seat->xkb_info.keymap_size);
2359
Daniel Stone17b13bd2012-05-30 16:31:39 +01002360 if (seat->seat.keyboard->focus &&
2361 seat->seat.keyboard->focus->resource.client == client) {
2362 wl_keyboard_set_focus(seat->seat.keyboard,
2363 seat->seat.keyboard->focus);
2364 }
Daniel Stone37816df2012-05-16 18:45:18 +01002365}
2366
2367static void
2368seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2369 uint32_t id)
2370{
2371 struct weston_seat *seat = resource->data;
2372 struct wl_resource *cr;
2373
2374 if (!seat->seat.touch)
2375 return;
2376
2377 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2378 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002379 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002380}
2381
2382static const struct wl_seat_interface seat_interface = {
2383 seat_get_pointer,
2384 seat_get_keyboard,
2385 seat_get_touch,
2386};
2387
2388static void
2389bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2390{
2391 struct wl_seat *seat = data;
2392 struct wl_resource *resource;
2393 enum wl_seat_capability caps = 0;
2394
2395 resource = wl_client_add_object(client, &wl_seat_interface,
2396 &seat_interface, id, data);
2397 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002398 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002399
2400 if (seat->pointer)
2401 caps |= WL_SEAT_CAPABILITY_POINTER;
2402 if (seat->keyboard)
2403 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2404 if (seat->touch)
2405 caps |= WL_SEAT_CAPABILITY_TOUCH;
2406
2407 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002408}
2409
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002410static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002411device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002412{
Daniel Stone37816df2012-05-16 18:45:18 +01002413 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002414
Daniel Stone37816df2012-05-16 18:45:18 +01002415 seat = container_of(listener, struct weston_seat,
2416 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002417
Daniel Stone37816df2012-05-16 18:45:18 +01002418 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002419}
2420
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002421static void weston_compositor_xkb_init(struct weston_compositor *ec,
2422 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002423{
Daniel Stonee379da92012-05-30 16:32:04 +01002424 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002425 ec->xkb_context = xkb_context_new(0);
2426 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002427 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002428 exit(1);
2429 }
Daniel Stone994679a2012-05-30 16:31:43 +01002430 }
2431
2432 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002433 ec->xkb_names = *names;
2434 if (!ec->xkb_names.rules)
2435 ec->xkb_names.rules = strdup("evdev");
2436 if (!ec->xkb_names.model)
2437 ec->xkb_names.model = strdup("pc105");
2438 if (!ec->xkb_names.layout)
2439 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002440}
2441
Daniel Stonee379da92012-05-30 16:32:04 +01002442static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2443{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002444 if (xkb_info->keymap)
2445 xkb_map_unref(xkb_info->keymap);
2446
2447 if (xkb_info->keymap_area)
2448 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2449 if (xkb_info->keymap_fd >= 0)
2450 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002451}
2452
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002453static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2454{
Daniel Stonee379da92012-05-30 16:32:04 +01002455 free((char *) ec->xkb_names.rules);
2456 free((char *) ec->xkb_names.model);
2457 free((char *) ec->xkb_names.layout);
2458 free((char *) ec->xkb_names.variant);
2459 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002460
Daniel Stonee379da92012-05-30 16:32:04 +01002461 xkb_info_destroy(&ec->xkb_info);
2462 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002463}
2464
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002465static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002466weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002467{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002468 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002469
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002470 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2471 XKB_MOD_NAME_SHIFT);
2472 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2473 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002474 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2475 XKB_MOD_NAME_CTRL);
2476 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2477 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002478 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2479 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002480 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2481 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002482 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002483
2484 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2485 XKB_LED_NAME_NUM);
2486 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2487 XKB_LED_NAME_CAPS);
2488 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2489 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002490
2491 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2492 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002493 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002494 exit(EXIT_FAILURE);
2495 }
2496 xkb_info->keymap_size = strlen(keymap_str) + 1;
2497
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002498 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002499 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002500 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002501 (unsigned long) xkb_info->keymap_size);
2502 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002503 }
2504
2505 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2506 PROT_READ | PROT_WRITE,
2507 MAP_SHARED, xkb_info->keymap_fd, 0);
2508 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002509 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002510 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002511 goto err_dev_zero;
2512 }
2513 strcpy(xkb_info->keymap_area, keymap_str);
2514 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002515
2516 return;
2517
2518err_dev_zero:
2519 close(xkb_info->keymap_fd);
2520 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002521err_keymap_str:
2522 free(keymap_str);
2523 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002524}
2525
2526static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002527weston_compositor_build_global_keymap(struct weston_compositor *ec)
2528{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002529 if (ec->xkb_info.keymap != NULL)
2530 return;
2531
Daniel Stonee379da92012-05-30 16:32:04 +01002532 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002533 &ec->xkb_names,
2534 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002535 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002536 weston_log("failed to compile global XKB keymap\n");
2537 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002538 "options %s",
2539 ec->xkb_names.rules, ec->xkb_names.model,
2540 ec->xkb_names.layout, ec->xkb_names.variant,
2541 ec->xkb_names.options);
2542 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002543 }
2544
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002545 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002546}
2547
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002548WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002549weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002550{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002551 if (seat->has_keyboard)
2552 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002553
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002554 if (keymap != NULL) {
2555 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002556 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002557 }
2558 else {
2559 weston_compositor_build_global_keymap(seat->compositor);
2560 seat->xkb_info = seat->compositor->xkb_info;
2561 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2562 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002563
2564 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2565 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002566 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002567 exit(1);
2568 }
2569
Daniel Stone71c38772012-06-22 13:21:30 +01002570 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002571
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002572 wl_keyboard_init(&seat->keyboard);
2573 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2574
2575 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002576}
2577
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002578WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002579weston_seat_init_pointer(struct weston_seat *seat)
2580{
2581 if (seat->has_pointer)
2582 return;
2583
2584 wl_pointer_init(&seat->pointer);
2585 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2586
2587 seat->has_pointer = 1;
2588}
2589
2590WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002591weston_seat_init_touch(struct weston_seat *seat)
2592{
2593 if (seat->has_touch)
2594 return;
2595
2596 wl_touch_init(&seat->touch);
2597 wl_seat_set_touch(&seat->seat, &seat->touch);
2598
2599 seat->has_touch = 1;
2600}
2601
2602WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002603weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002604{
Daniel Stone37816df2012-05-16 18:45:18 +01002605 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002606 seat->has_pointer = 0;
2607 seat->has_keyboard = 0;
2608 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002609
Daniel Stone37816df2012-05-16 18:45:18 +01002610 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2611 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002612
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002613 seat->sprite = NULL;
2614 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002615
Daniel Stone37816df2012-05-16 18:45:18 +01002616 seat->compositor = ec;
2617 seat->hotspot_x = 16;
2618 seat->hotspot_y = 16;
2619 seat->modifier_state = 0;
2620 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002621
Daniel Stone37816df2012-05-16 18:45:18 +01002622 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002623 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002624
Daniel Stone37816df2012-05-16 18:45:18 +01002625 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002626
Daniel Stone37816df2012-05-16 18:45:18 +01002627 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
2628 wl_signal_add(&seat->seat.drag_icon_signal,
2629 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04002630
2631 clipboard_create(seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002632}
2633
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002634WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002635weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002636{
Daniel Stone37816df2012-05-16 18:45:18 +01002637 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002638 /* The global object is destroyed at wl_display_destroy() time. */
2639
Daniel Stone37816df2012-05-16 18:45:18 +01002640 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002641 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002642
Daniel Stone74419a22012-05-30 16:32:02 +01002643 if (seat->xkb_state.state != NULL)
2644 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01002645 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002646
Daniel Stone37816df2012-05-16 18:45:18 +01002647 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02002648}
2649
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002650static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002651drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
2652{
2653 weston_surface_configure(es,
2654 es->geometry.x + sx, es->geometry.y + sy,
2655 es->buffer->width, es->buffer->height);
2656}
2657
2658static int
Daniel Stone37816df2012-05-16 18:45:18 +01002659device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002660 struct weston_surface *surface)
2661{
Daniel Stone37816df2012-05-16 18:45:18 +01002662 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002663
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002664 if (surface->configure) {
2665 wl_resource_post_error(&surface->surface.resource,
2666 WL_DISPLAY_ERROR_INVALID_OBJECT,
2667 "surface->configure already set");
2668 return 0;
2669 }
2670
Daniel Stone37816df2012-05-16 18:45:18 +01002671 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002672
Daniel Stone37816df2012-05-16 18:45:18 +01002673 weston_surface_set_position(ws->drag_surface,
2674 wl_fixed_to_double(seat->pointer->x),
2675 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002676
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002677 surface->configure = drag_surface_configure;
2678
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002679 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01002680 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002681
2682 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002683}
2684
2685static void
Daniel Stone37816df2012-05-16 18:45:18 +01002686device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002687{
Daniel Stone37816df2012-05-16 18:45:18 +01002688 seat->drag_surface->configure = NULL;
2689 undef_region(&seat->drag_surface->input);
2690 wl_list_remove(&seat->drag_surface_destroy_listener.link);
2691 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002692}
2693
2694static void
Daniel Stone37816df2012-05-16 18:45:18 +01002695device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002696{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002697 struct wl_list *list;
2698
Daniel Stone37816df2012-05-16 18:45:18 +01002699 if (weston_surface_is_mapped(seat->drag_surface) ||
2700 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002701 return;
2702
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002703 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
2704 list = &seat->sprite->layer_link;
2705 else
2706 list = &seat->compositor->cursor_layer.surface_list;
2707
2708 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01002709 weston_surface_assign_output(seat->drag_surface);
2710 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002711}
2712
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002713static void
Daniel Stone37816df2012-05-16 18:45:18 +01002714weston_seat_update_drag_surface(struct wl_seat *seat,
2715 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002716{
2717 int surface_changed = 0;
Daniel Stone37816df2012-05-16 18:45:18 +01002718 struct weston_seat *ws = (struct weston_seat *) seat;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002719
Daniel Stone37816df2012-05-16 18:45:18 +01002720 if (!ws->drag_surface && !seat->drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002721 return;
2722
Daniel Stone37816df2012-05-16 18:45:18 +01002723 if (ws->drag_surface && seat->drag_surface &&
2724 (&ws->drag_surface->surface.resource !=
2725 &seat->drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002726 /* between calls to this funcion we got a new drag_surface */
2727 surface_changed = 1;
2728
Daniel Stone37816df2012-05-16 18:45:18 +01002729 if (!seat->drag_surface || surface_changed) {
2730 device_release_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002731 if (!surface_changed)
2732 return;
2733 }
2734
Daniel Stone37816df2012-05-16 18:45:18 +01002735 if (!ws->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002736 struct weston_surface *surface = (struct weston_surface *)
Daniel Stone37816df2012-05-16 18:45:18 +01002737 seat->drag_surface;
2738 if (!device_setup_new_drag_surface(ws, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03002739 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002740 }
2741
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02002742 /* the client may not have attached a buffer to the drag surface
2743 * when we setup it up, so check if map is needed on every update */
Daniel Stone37816df2012-05-16 18:45:18 +01002744 device_map_drag_surface(ws);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002745
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002746 /* the client may have attached a buffer with a different size to
2747 * the drag surface, causing the input region to be reset */
Daniel Stone37816df2012-05-16 18:45:18 +01002748 if (region_is_undefined(&ws->drag_surface->input))
2749 empty_region(&ws->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02002750
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002751 if (!dx && !dy)
2752 return;
2753
Daniel Stone37816df2012-05-16 18:45:18 +01002754 weston_surface_set_position(ws->drag_surface,
2755 ws->drag_surface->geometry.x + wl_fixed_to_double(dx),
2756 ws->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002757}
2758
2759WL_EXPORT void
2760weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
2761{
Daniel Stone4dab5db2012-05-30 16:31:53 +01002762 struct weston_seat *seat;
2763
2764 wl_list_for_each(seat, &compositor->seat_list, link)
2765 weston_seat_update_drag_surface(&seat->seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002766}
2767
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002768static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002769bind_output(struct wl_client *client,
2770 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05002771{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002772 struct weston_output *output = data;
2773 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002774 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002775
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04002776 resource = wl_client_add_object(client,
2777 &wl_output_interface, NULL, id, data);
2778
Casey Dahlin9074db52012-04-19 22:50:09 -04002779 wl_list_insert(&output->resource_list, &resource->link);
2780 resource->destroy = unbind_resource;
2781
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002782 wl_output_send_geometry(resource,
2783 output->x,
2784 output->y,
2785 output->mm_width,
2786 output->mm_height,
2787 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04002788 output->make, output->model,
2789 WL_OUTPUT_TRANSFORM_NORMAL);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002790
2791 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05002792 wl_output_send_mode(resource,
2793 mode->flags,
2794 mode->width,
2795 mode->height,
2796 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04002797 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05002798}
2799
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002800static const char vertex_shader[] =
2801 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002802 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002803 "attribute vec2 texcoord;\n"
2804 "varying vec2 v_texcoord;\n"
2805 "void main()\n"
2806 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05002807 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002808 " v_texcoord = texcoord;\n"
2809 "}\n";
2810
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02002811/* Declare common fragment shader uniforms */
2812#define FRAGMENT_SHADER_UNIFORMS \
2813 "uniform float alpha;\n" \
2814 "uniform float texwidth;\n" \
2815 "uniform vec4 opaque;\n"
2816
2817/* Common fragment shader init code (check texture bounds) */
2818#define FRAGMENT_SHADER_INIT \
2819 " if (v_texcoord.x < 0.0 || v_texcoord.x > texwidth ||\n" \
2820 " v_texcoord.y < 0.0 || v_texcoord.y > 1.0)\n" \
2821 " discard;\n"
2822
2823#define FRAGMENT_SHADER_EXIT \
2824 " if (opaque.x <= v_texcoord.x && v_texcoord.x < opaque.y &&\n" \
2825 " opaque.z <= v_texcoord.y && v_texcoord.y < opaque.w)\n" \
2826 " gl_FragColor.a = 1.0;\n" \
2827 " gl_FragColor = alpha * gl_FragColor;\n"
2828
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02002829#define FRAGMENT_CONVERT_YUV \
2830 " gl_FragColor.r = y + 1.59602678 * v;\n" \
2831 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
2832 " gl_FragColor.b = y + 2.01723214 * u;\n" \
2833 " gl_FragColor.a = 1.0;\n"
2834
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02002835static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05002836 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002837 "varying vec2 v_texcoord;\n"
2838 "uniform sampler2D tex;\n"
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02002839 FRAGMENT_SHADER_UNIFORMS
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002840 "void main()\n"
2841 "{\n"
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02002842 FRAGMENT_SHADER_INIT
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002843 " gl_FragColor = texture2D(tex, v_texcoord)\n;"
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02002844 FRAGMENT_SHADER_EXIT
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002845 "}\n";
2846
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02002847static const char texture_fragment_shader_y_uv[] =
2848 "precision mediump float;\n"
2849 "uniform sampler2D tex;\n"
2850 "uniform sampler2D tex1;\n"
2851 "varying vec2 v_texcoord;\n"
2852 FRAGMENT_SHADER_UNIFORMS
2853 "void main() {\n"
2854 FRAGMENT_SHADER_INIT
2855 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2856 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
2857 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
2858 FRAGMENT_CONVERT_YUV
2859 FRAGMENT_SHADER_EXIT
2860 "}\n";
2861
2862static const char texture_fragment_shader_y_u_v[] =
2863 "precision mediump float;\n"
2864 "uniform sampler2D tex;\n"
2865 "uniform sampler2D tex1;\n"
2866 "uniform sampler2D tex2;\n"
2867 "varying vec2 v_texcoord;\n"
2868 FRAGMENT_SHADER_UNIFORMS
2869 "void main() {\n"
2870 FRAGMENT_SHADER_INIT
2871 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2872 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
2873 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
2874 FRAGMENT_CONVERT_YUV
2875 FRAGMENT_SHADER_EXIT
2876 "}\n";
2877
2878static const char texture_fragment_shader_y_xuxv[] =
2879 "precision mediump float;\n"
2880 "uniform sampler2D tex;\n"
2881 "uniform sampler2D tex1;\n"
2882 "varying vec2 v_texcoord;\n"
2883 FRAGMENT_SHADER_UNIFORMS
2884 "void main() {\n"
2885 FRAGMENT_SHADER_INIT
2886 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
2887 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
2888 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
2889 FRAGMENT_CONVERT_YUV
2890 FRAGMENT_SHADER_EXIT
2891 "}\n";
2892
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002893static const char solid_fragment_shader[] =
2894 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002895 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002896 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002897 "void main()\n"
2898 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04002899 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04002900 "}\n";
2901
Kristian Høgsberga9468212010-06-14 21:03:11 -04002902static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002903compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002904{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002905 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002906 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002907 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002908
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002909 s = glCreateShader(type);
2910 glShaderSource(s, 1, &source, NULL);
2911 glCompileShader(s);
2912 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002913 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002914 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02002915 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002916 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002917 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002918
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002919 return s;
2920}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002921
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002922static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002923weston_shader_init(struct weston_shader *shader,
2924 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002925{
2926 char msg[512];
2927 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002928
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002929 shader->vertex_shader =
2930 compile_shader(GL_VERTEX_SHADER, vertex_source);
2931 shader->fragment_shader =
2932 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
2933
2934 shader->program = glCreateProgram();
2935 glAttachShader(shader->program, shader->vertex_shader);
2936 glAttachShader(shader->program, shader->fragment_shader);
2937 glBindAttribLocation(shader->program, 0, "position");
2938 glBindAttribLocation(shader->program, 1, "texcoord");
2939
2940 glLinkProgram(shader->program);
2941 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002942 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002943 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02002944 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002945 return -1;
2946 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002947
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04002948 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02002949 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
2950 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
2951 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05002952 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02002953 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Scott Moreau9fb98242012-05-22 10:18:50 -06002954 shader->texwidth_uniform = glGetUniformLocation(shader->program, "texwidth");
2955 shader->opaque_uniform = glGetUniformLocation(shader->program, "opaque");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002956
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04002957 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04002958}
2959
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002960WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002961weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04002962{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002963 struct weston_compositor *c = output->compositor;
2964
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04002965 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04002966 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04002967 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02002968
2969 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002970}
2971
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002972WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07002973weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002974{
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002975 int flip;
Scott Moreau850ca422012-05-21 15:21:25 -06002976 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07002977 struct weston_matrix camera;
2978 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05002979
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002980 weston_matrix_init(&output->matrix);
2981 weston_matrix_translate(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002982 -(output->x + (output->border.right + output->current->width - output->border.left) / 2.0),
2983 -(output->y + (output->border.bottom + output->current->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01002984
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01002985 flip = (output->flags & WL_OUTPUT_FLIPPED) ? -1 : 1;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002986 weston_matrix_scale(&output->matrix,
Kristian Høgsberg546a8122012-02-01 07:45:51 -05002987 2.0 / (output->current->width + output->border.left + output->border.right),
2988 flip * 2.0 / (output->current->height + output->border.top + output->border.bottom), 1);
Scott Moreau850ca422012-05-21 15:21:25 -06002989
Scott Moreauccbf29d2012-02-22 14:21:41 -07002990 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06002991 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002992 weston_matrix_init(&camera);
2993 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06002994 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06002995 weston_matrix_translate(&camera, output->zoom.trans_x,
2996 flip * output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002997 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06002998 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002999 weston_matrix_multiply(&output->matrix, &modelview);
3000 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003001
Scott Moreauccbf29d2012-02-22 14:21:41 -07003002 output->dirty = 0;
3003}
3004
3005WL_EXPORT void
3006weston_output_move(struct weston_output *output, int x, int y)
3007{
3008 output->x = x;
3009 output->y = y;
3010
3011 pixman_region32_init(&output->previous_damage);
3012 pixman_region32_init_rect(&output->region, x, y,
3013 output->current->width,
3014 output->current->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003015}
3016
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003017WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003018weston_output_init(struct weston_output *output, struct weston_compositor *c,
3019 int x, int y, int width, int height, uint32_t flags)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003020{
3021 output->compositor = c;
3022 output->x = x;
3023 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003024 output->border.top = 0;
3025 output->border.bottom = 0;
3026 output->border.left = 0;
3027 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003028 output->mm_width = width;
3029 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003030 output->dirty = 1;
3031
Scott Moreau429490d2012-06-17 18:10:59 -06003032 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003033
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003034 output->flags = flags;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003035 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003036 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003037
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003038 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003039 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003040 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003041
Casey Dahlin58ba1372012-04-19 22:50:08 -04003042 output->id = ffs(~output->compositor->output_id_pool) - 1;
3043 output->compositor->output_id_pool |= 1 << output->id;
3044
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003045 output->global =
3046 wl_display_add_global(c->wl_display, &wl_output_interface,
3047 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003048}
3049
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003050static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003051compositor_bind(struct wl_client *client,
3052 void *data, uint32_t version, uint32_t id)
3053{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003054 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003055
3056 wl_client_add_object(client, &wl_compositor_interface,
3057 &compositor_interface, id, compositor);
3058}
3059
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003060static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003061log_uname(void)
3062{
3063 struct utsname usys;
3064
3065 uname(&usys);
3066
3067 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3068 usys.version, usys.machine);
3069}
3070
3071static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003072log_extensions(const char *name, const char *extensions)
3073{
3074 const char *p, *end;
3075 int l;
3076
3077 l = weston_log("%s:", name);
3078 p = extensions;
3079 while (*p) {
3080 end = strchrnul(p, ' ');
3081 if (l + (end - p) > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003082 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
3083 end - p, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003084 else
3085 l += weston_log_continue(" %.*s", end - p, p);
3086 for (p = end; isspace(*p); p++)
3087 ;
3088 }
3089 weston_log_continue("\n");
3090}
3091
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003092static void
3093log_egl_gl_info(EGLDisplay egldpy)
3094{
3095 const char *str;
3096
3097 str = eglQueryString(egldpy, EGL_VERSION);
3098 weston_log("EGL version: %s\n", str ? str : "(null)");
3099
3100 str = eglQueryString(egldpy, EGL_VENDOR);
3101 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3102
3103 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3104 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3105
3106 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3107 log_extensions("EGL extensions", str ? str : "(null)");
3108
3109 str = (char *)glGetString(GL_VERSION);
3110 weston_log("GL version: %s\n", str ? str : "(null)");
3111
3112 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3113 weston_log("GLSL version: %s\n", str ? str : "(null)");
3114
3115 str = (char *)glGetString(GL_VENDOR);
3116 weston_log("GL vendor: %s\n", str ? str : "(null)");
3117
3118 str = (char *)glGetString(GL_RENDERER);
3119 weston_log("GL renderer: %s\n", str ? str : "(null)");
3120
3121 str = (char *)glGetString(GL_EXTENSIONS);
3122 log_extensions("GL extensions", str ? str : "(null)");
3123}
3124
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003125WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003126weston_compositor_init(struct weston_compositor *ec,
3127 struct wl_display *display,
3128 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003129 char *argv[],
3130 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003131{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003132 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003133 struct xkb_rule_names xkb_names;
3134 const struct config_key keyboard_config_keys[] = {
3135 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3136 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3137 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3138 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3139 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3140 };
3141 const struct config_section cs[] = {
3142 { "keyboard",
3143 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3144 };
3145
3146 memset(&xkb_names, 0, sizeof(xkb_names));
3147 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003148
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003149 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003150 wl_signal_init(&ec->destroy_signal);
3151 wl_signal_init(&ec->activate_signal);
3152 wl_signal_init(&ec->lock_signal);
3153 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003154 wl_signal_init(&ec->show_input_panel_signal);
3155 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003156 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003157
Casey Dahlin58ba1372012-04-19 22:50:08 -04003158 ec->output_id_pool = 0;
3159
Kristian Høgsberga8873122011-11-23 10:39:34 -05003160 if (!wl_display_add_global(display, &wl_compositor_interface,
3161 ec, compositor_bind))
3162 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003163
Daniel Stone725c2c32012-06-22 14:04:36 +01003164 wl_list_init(&ec->surface_list);
3165 wl_list_init(&ec->layer_list);
3166 wl_list_init(&ec->seat_list);
3167 wl_list_init(&ec->output_list);
3168 wl_list_init(&ec->key_binding_list);
3169 wl_list_init(&ec->button_binding_list);
3170 wl_list_init(&ec->axis_binding_list);
3171 wl_list_init(&ec->fade.animation.link);
3172
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003173 weston_plane_init(&ec->primary_plane, 0, 0);
3174
Daniel Stone725c2c32012-06-22 14:04:36 +01003175 weston_compositor_xkb_init(ec, &xkb_names);
3176
3177 ec->ping_handler = NULL;
3178
3179 screenshooter_create(ec);
3180 text_cursor_position_notifier_create(ec);
3181 input_method_create(ec);
3182
3183 wl_data_device_manager_init(ec->wl_display);
3184
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003185 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003186
Daniel Stone725c2c32012-06-22 14:04:36 +01003187 loop = wl_display_get_event_loop(ec->wl_display);
3188 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3189 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3190
3191 ec->input_loop = wl_event_loop_create();
3192
3193 return 0;
3194}
3195
3196WL_EXPORT int
3197weston_compositor_init_gl(struct weston_compositor *ec)
3198{
3199 const char *extensions;
3200
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003201 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003202
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003203 ec->image_target_texture_2d =
3204 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3205 ec->image_target_renderbuffer_storage = (void *)
3206 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3207 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3208 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3209 ec->bind_display =
3210 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3211 ec->unbind_display =
3212 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003213 ec->query_buffer =
3214 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003215
3216 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003217 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003218 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003219 return -1;
3220 }
3221
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003222 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003223 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003224 return -1;
3225 }
3226
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003227 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3228 ec->read_format = GL_BGRA_EXT;
3229 else
3230 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003231
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003232 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3233 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003234
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003235 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003236 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003237 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003238 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003239 return -1;
3240 }
3241
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003242 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3243 ec->has_bind_display = 1;
3244 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003245 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003246
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003247 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003248 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003249
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003250 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3251 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3252
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003253 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003254
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003255 if (weston_shader_init(&ec->texture_shader_rgba,
3256 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003257 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003258 if (weston_shader_init(&ec->texture_shader_y_uv,
3259 vertex_shader, texture_fragment_shader_y_uv) < 0)
3260 return -1;
3261 if (weston_shader_init(&ec->texture_shader_y_u_v,
3262 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3263 return -1;
3264 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3265 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3266 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003267 if (weston_shader_init(&ec->solid_shader,
3268 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003269 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003270
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003271 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003272
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003273 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003274}
3275
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003276WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003277weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003278{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003279 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003280
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003281 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003282 if (ec->input_loop_source)
3283 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003284
Matt Roper361d2ad2011-08-29 13:52:23 -07003285 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003286 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003287 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003288
Daniel Stone325fc2d2012-05-30 16:31:58 +01003289 weston_binding_list_destroy_all(&ec->key_binding_list);
3290 weston_binding_list_destroy_all(&ec->button_binding_list);
3291 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003292
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003293 weston_plane_release(&ec->primary_plane);
3294
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003295 wl_array_release(&ec->vertices);
3296 wl_array_release(&ec->indices);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003297
3298 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003299}
3300
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003301static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003302{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003303 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003304
Martin Minarik6d118362012-06-07 18:01:59 +02003305 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003306 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003307
3308 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003309}
3310
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003311static void
3312on_segv_signal(int s, siginfo_t *siginfo, void *context)
3313{
3314 void *buffer[32];
3315 int i, count;
3316 Dl_info info;
3317
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003318 /* This SIGSEGV handler will do a best-effort backtrace, and
3319 * then call the backend restore function, which will switch
3320 * back to the vt we launched from or ungrab X etc and then
3321 * raise SIGTRAP. If we run weston under gdb from X or a
3322 * different vt, and tell gdb "handle SIGSEGV nostop", this
3323 * will allow weston to switch back to gdb on crash and then
3324 * gdb will catch the crash with SIGTRAP. */
3325
Martin Minarik6d118362012-06-07 18:01:59 +02003326 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003327
3328 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3329 for (i = 0; i < count; i++) {
3330 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003331 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003332 (long) buffer[i],
3333 info.dli_sname ? info.dli_sname : "--",
3334 info.dli_fname);
3335 }
3336
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003337 segv_compositor->restore(segv_compositor);
3338
3339 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003340}
3341
3342
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003343static void *
3344load_module(const char *name, const char *entrypoint, void **handle)
3345{
3346 char path[PATH_MAX];
3347 void *module, *init;
3348
3349 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003350 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003351 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003352 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003353
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003354 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003355 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003356 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003357 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003358 return NULL;
3359 }
3360
3361 init = dlsym(module, entrypoint);
3362 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003363 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003364 return NULL;
3365 }
3366
3367 return init;
3368}
3369
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003370static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003371 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3372
3373static const char xdg_wrong_message[] =
3374 "fatal: environment variable XDG_RUNTIME_DIR\n"
3375 "is set to \"%s\", which is not a directory.\n";
3376
3377static const char xdg_wrong_mode_message[] =
3378 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3379 "correctly. Unix access mode must be 0700 but is %o,\n"
3380 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003381 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003382
3383static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003384 "Refer to your distribution on how to get it, or\n"
3385 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3386 "on how to implement it.\n";
3387
Martin Minarik37032f82012-06-18 20:15:18 +02003388static void
3389verify_xdg_runtime_dir(void)
3390{
3391 char *dir = getenv("XDG_RUNTIME_DIR");
3392 struct stat s;
3393
3394 if (!dir) {
3395 weston_log(xdg_error_message);
3396 weston_log_continue(xdg_detail_message);
3397 exit(EXIT_FAILURE);
3398 }
3399
3400 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3401 weston_log(xdg_wrong_message, dir);
3402 weston_log_continue(xdg_detail_message);
3403 exit(EXIT_FAILURE);
3404 }
3405
3406 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3407 weston_log(xdg_wrong_mode_message,
3408 dir, s.st_mode & 0777, s.st_uid);
3409 weston_log_continue(xdg_detail_message);
3410 }
3411}
3412
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003413static int
3414usage(int error_code)
3415{
3416 fprintf(stderr,
3417 "Usage: weston [OPTIONS]\n\n"
3418 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3419 "Weston supports multiple backends, and depending on which backend is in use\n"
3420 "different options will be accepted.\n\n"
3421
3422
3423 "Core options:\n\n"
3424 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3425 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3426 " -S, --socket=NAME\tName of socket to listen on\n"
3427 " -i, --idle-time=SECS\tIdle time in seconds\n"
3428 " --xserver\t\tEnable X server integration\n"
3429 " --module\t\tLoad the specified module\n"
3430 " --log==FILE\t\tLog to the given file\n"
3431 " -h, --help\t\tThis help message\n\n");
3432
3433 fprintf(stderr,
3434 "Options for drm-backend.so:\n\n"
3435 " --connector=ID\tBring up only this connector\n"
3436 " --seat=SEAT\t\tThe seat that weston should run on\n"
3437 " --tty=TTY\t\tThe tty to use\n"
3438 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3439
3440 fprintf(stderr,
3441 "Options for x11-backend.so:\n\n"
3442 " --width=WIDTH\t\tWidth of X window\n"
3443 " --height=HEIGHT\tHeight of X window\n"
3444 " --fullscreen\t\tRun in fullscreen mode\n"
3445 " --output-count=COUNT\tCreate multiple outputs\n"
3446 " --no-input\t\tDont create input devices\n\n");
3447
3448 fprintf(stderr,
3449 "Options for wayland-backend.so:\n\n"
3450 " --width=WIDTH\t\tWidth of Wayland surface\n"
3451 " --height=HEIGHT\tHeight of Wayland surface\n"
3452 " --display=DISPLAY\tWayland display to connect to\n\n");
3453
3454 exit(error_code);
3455}
3456
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003457int main(int argc, char *argv[])
3458{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003459 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003460 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003461 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003462 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003463 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003464 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003465 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003466 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003467 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003468 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003469 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003470 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003471 char *backend = NULL;
3472 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003473 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003474 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003475 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003476 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003477 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003478 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003479 char *config_file;
3480
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003481 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003482 { "type", CONFIG_KEY_STRING, &shell },
3483 };
3484
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003485 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003486 { "shell",
3487 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3488 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003489
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003490 const struct weston_option core_options[] = {
3491 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3492 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3493 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003494 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003495 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003496 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003497 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003498 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003499
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003500 argc = parse_options(core_options,
3501 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003502
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003503 if (help)
3504 usage(EXIT_SUCCESS);
3505
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003506 weston_log_file_open(log);
3507
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003508 weston_log("%s\n"
3509 STAMP_SPACE "%s\n"
3510 STAMP_SPACE "Bug reports to: %s\n"
3511 STAMP_SPACE "Build: %s\n",
3512 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003513 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003514 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003515
Martin Minarik37032f82012-06-18 20:15:18 +02003516 verify_xdg_runtime_dir();
3517
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003518 display = wl_display_create();
3519
Tiago Vignatti2116b892011-08-08 05:52:59 -07003520 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003521 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3522 display);
3523 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3524 display);
3525 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3526 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003527
3528 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003529 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3530 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003531
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003532 if (!backend) {
3533 if (getenv("WAYLAND_DISPLAY"))
3534 backend = "wayland-backend.so";
3535 else if (getenv("DISPLAY"))
3536 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003537 else
3538 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003539 }
3540
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003541 config_file = config_file_path("weston.ini");
3542 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Kristian Høgsberg3df63842012-07-31 14:54:48 -04003543 if (socket_name)
3544 setenv("WAYLAND_DISPLAY", socket_name, 1);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003545
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003546 backend_init = load_module(backend, "backend_init", &backend_module);
3547 if (!backend_init)
3548 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003549
Daniel Stonec1be8e52012-06-01 11:14:02 -04003550 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003551 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003552 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05003553 exit(EXIT_FAILURE);
3554 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04003555
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003556 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
3557 segv_action.sa_sigaction = on_segv_signal;
3558 sigemptyset(&segv_action.sa_mask);
3559 sigaction(SIGSEGV, &segv_action, NULL);
3560 segv_compositor = ec;
3561
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003562 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03003563 weston_log("fatal: unhandled option: %s\n", argv[i]);
3564 if (argv[1]) {
3565 ret = EXIT_FAILURE;
3566 goto out;
3567 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003568
Daniel Stonec1be8e52012-06-01 11:14:02 -04003569 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01003570
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003571 ec->option_idle_time = idle_time;
3572 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02003573
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003574 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003575 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03003576 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003577 "weston_xserver_init",
3578 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03003579 if (module_init && module_init(ec) < 0) {
3580 ret = EXIT_FAILURE;
3581 goto out;
3582 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003583
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003584 if (!shell)
3585 shell = "desktop-shell.so";
3586 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03003587 if (!module_init || module_init(ec) < 0) {
3588 ret = EXIT_FAILURE;
3589 goto out;
3590 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003591
3592
3593 module_init = NULL;
3594 if (module)
3595 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03003596 if (module_init && module_init(ec) < 0) {
3597 ret = EXIT_FAILURE;
3598 goto out;
3599 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03003600
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003601 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03003602 weston_log("fatal: failed to add socket: %m\n");
3603 ret = EXIT_FAILURE;
3604 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003605 }
3606
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02003607 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02003608 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003609
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003610 wl_display_run(display);
3611
3612 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003613 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05003614 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02003615
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003616 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02003617
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003618 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003619 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05003620
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003621 for (i = ARRAY_LENGTH(signals); i;)
3622 wl_event_source_remove(signals[--i]);
3623
Daniel Stone855028f2012-05-01 20:37:10 +01003624 weston_compositor_xkb_destroy(ec);
3625
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003626 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02003627 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05003628
Martin Minarik19e6f262012-06-07 13:08:46 +02003629 weston_log_file_close();
3630
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003631 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003632}