blob: 684fcd79a2b8e67016381df39c373bdb368a5f5e [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 Paalanendf1fd362012-08-06 14:57:03 +0300143 weston_log("launching '%s'\n", path);
144
Pekka Paalanen51aaf642012-05-30 15:53:41 +0300145 if (os_socketpair_cloexec(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +0200146 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200147 "socketpair failed while launching '%s': %m\n",
148 path);
149 return NULL;
150 }
151
152 pid = fork();
153 if (pid == -1) {
154 close(sv[0]);
155 close(sv[1]);
Martin Minarik6d118362012-06-07 18:01:59 +0200156 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200157 "fork failed while launching '%s': %m\n", path);
158 return NULL;
159 }
160
161 if (pid == 0) {
162 child_client_exec(sv[1], path);
163 exit(-1);
164 }
165
166 close(sv[1]);
167
168 client = wl_client_create(compositor->wl_display, sv[0]);
169 if (!client) {
170 close(sv[0]);
Martin Minarik6d118362012-06-07 18:01:59 +0200171 weston_log("weston_client_launch: "
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200172 "wl_client_create failed while launching '%s'.\n",
173 path);
174 return NULL;
175 }
176
177 proc->pid = pid;
178 proc->cleanup = cleanup;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500179 weston_watch_process(proc);
Pekka Paalanen409ef0a2011-12-02 15:30:21 +0200180
181 return client;
182}
183
184static void
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400185update_shm_texture(struct weston_surface *surface);
186
187static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400188surface_handle_buffer_destroy(struct wl_listener *listener, void *data)
Benjamin Franzke06286262011-05-06 19:12:33 +0200189{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500190 struct weston_surface *es =
191 container_of(listener, struct weston_surface,
192 buffer_destroy_listener);
Benjamin Franzke06286262011-05-06 19:12:33 +0200193
Kristian Høgsberga85b4fb2012-06-20 09:24:35 -0400194 if (es->buffer && wl_buffer_is_shm(es->buffer))
195 update_shm_texture(es);
196
Kristian Høgsberg24596942011-10-24 17:51:02 -0400197 es->buffer = NULL;
Benjamin Franzke06286262011-05-06 19:12:33 +0200198}
199
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500200static const pixman_region32_data_t undef_region_data;
201
202static void
203undef_region(pixman_region32_t *region)
204{
Kristian Høgsberg66a099b2012-05-29 16:49:45 -0400205 if (region->data != &undef_region_data)
206 pixman_region32_fini(region);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500207 region->data = (pixman_region32_data_t *) &undef_region_data;
208}
209
210static int
211region_is_undefined(pixman_region32_t *region)
212{
213 return region->data == &undef_region_data;
214}
215
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +0200216static void
217empty_region(pixman_region32_t *region)
218{
219 if (!region_is_undefined(region))
220 pixman_region32_fini(region);
221
222 pixman_region32_init(region);
223}
224
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500225WL_EXPORT struct weston_surface *
Kristian Høgsberg18c93002012-01-27 11:58:31 -0500226weston_surface_create(struct weston_compositor *compositor)
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500227{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500228 struct weston_surface *surface;
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400229
Pekka Paalanen56cdea92011-11-23 16:14:12 +0200230 surface = calloc(1, sizeof *surface);
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400231 if (surface == NULL)
232 return NULL;
233
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400234 wl_signal_init(&surface->surface.resource.destroy_signal);
Kristian Høgsberg48891542012-02-23 17:38:33 -0500235
Kristian Høgsbergf6b14712011-01-06 15:32:14 -0500236 wl_list_init(&surface->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500237 wl_list_init(&surface->layer_link);
Kristian Høgsbergc551bd22010-12-06 16:43:16 -0500238
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400239 surface->surface.resource.client = NULL;
Kristian Høgsberg8244b442011-06-23 15:44:14 -0400240
Kristian Høgsberg1a208d52009-02-10 14:20:26 -0500241 surface->compositor = compositor;
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400242 surface->alpha = 1.0;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400243 surface->blend = 1;
Kristian Høgsberg0b0c2bb2012-05-15 23:16:53 -0400244 surface->opaque_rect[0] = 0.0;
245 surface->opaque_rect[1] = 0.0;
246 surface->opaque_rect[2] = 0.0;
247 surface->opaque_rect[3] = 0.0;
Juan Zhao46436612012-03-20 01:48:46 +0800248 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400249
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200250 surface->num_textures = 0;
251 surface->num_images = 0;
252
Benjamin Franzke06286262011-05-06 19:12:33 +0200253 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400254 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400255 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200256
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400257 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500258 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500259 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500260 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200261 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500262 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400263
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400264 surface->buffer_destroy_listener.notify =
265 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200266
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200267 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200268 wl_list_insert(&surface->geometry.transformation_list,
269 &surface->transform.position.link);
270 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200271 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200272 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500273
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400274 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500275}
276
Alex Wu8811bf92012-02-28 18:07:54 +0800277WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500278weston_surface_set_color(struct weston_surface *surface,
279 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
280{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500281 surface->color[0] = red;
282 surface->color[1] = green;
283 surface->color[2] = blue;
284 surface->color[3] = alpha;
285 surface->shader = &surface->compositor->solid_shader;
286}
287
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400288WL_EXPORT void
289weston_surface_to_global_float(struct weston_surface *surface,
290 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200291{
292 if (surface->transform.enabled) {
293 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
294
295 weston_matrix_transform(&surface->transform.matrix, &v);
296
297 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200298 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200299 "weston_surface_to_global(), divisor = %g\n",
300 v.f[3]);
301 *x = 0;
302 *y = 0;
303 return;
304 }
305
306 *x = v.f[0] / v.f[3];
307 *y = v.f[1] / v.f[3];
308 } else {
309 *x = sx + surface->geometry.x;
310 *y = sy + surface->geometry.y;
311 }
312}
313
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500314WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400315weston_surface_move_to_plane(struct weston_surface *surface,
316 struct weston_plane *plane)
317{
318 if (surface->plane == plane)
319 return;
320
321 weston_surface_damage_below(surface);
322 surface->plane = plane;
323 weston_surface_damage(surface);
324}
325
326WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500327weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200328{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500329 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200330
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500331 pixman_region32_init(&damage);
332 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
333 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400334 pixman_region32_union(&surface->plane->damage,
335 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500336 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200337}
338
339static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200340surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
341 int32_t width, int32_t height,
342 pixman_region32_t *bbox)
343{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200344 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
345 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200346 int32_t s[4][2] = {
347 { sx, sy },
348 { sx, sy + height },
349 { sx + width, sy },
350 { sx + width, sy + height }
351 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200352 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200353 int i;
354
355 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200356 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400357 weston_surface_to_global_float(surface,
358 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200359 if (x < min_x)
360 min_x = x;
361 if (x > max_x)
362 max_x = x;
363 if (y < min_y)
364 min_y = y;
365 if (y > max_y)
366 max_y = y;
367 }
368
Pekka Paalanen219b9822012-02-08 15:38:37 +0200369 int_x = floorf(min_x);
370 int_y = floorf(min_y);
371 pixman_region32_init_rect(bbox, int_x, int_y,
372 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200373}
374
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200375static void
376weston_surface_update_transform_disable(struct weston_surface *surface)
377{
378 surface->transform.enabled = 0;
379
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200380 /* round off fractions when not transformed */
381 surface->geometry.x = roundf(surface->geometry.x);
382 surface->geometry.y = roundf(surface->geometry.y);
383
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200384 pixman_region32_init_rect(&surface->transform.boundingbox,
385 surface->geometry.x,
386 surface->geometry.y,
387 surface->geometry.width,
388 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500389
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400390 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500391 pixman_region32_copy(&surface->transform.opaque,
392 &surface->opaque);
393 pixman_region32_translate(&surface->transform.opaque,
394 surface->geometry.x,
395 surface->geometry.y);
396 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200397}
398
399static int
400weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200401{
402 struct weston_matrix *matrix = &surface->transform.matrix;
403 struct weston_matrix *inverse = &surface->transform.inverse;
404 struct weston_transform *tform;
405
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200406 surface->transform.enabled = 1;
407
408 /* Otherwise identity matrix, but with x and y translation. */
409 surface->transform.position.matrix.d[12] = surface->geometry.x;
410 surface->transform.position.matrix.d[13] = surface->geometry.y;
411
412 weston_matrix_init(matrix);
413 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
414 weston_matrix_multiply(matrix, &tform->matrix);
415
416 if (weston_matrix_invert(inverse, matrix) < 0) {
417 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200418 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200419 " transformation not invertible.\n", surface);
420 return -1;
421 }
422
423 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
424 surface->geometry.height,
425 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500426
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200427 return 0;
428}
429
430WL_EXPORT void
431weston_surface_update_transform(struct weston_surface *surface)
432{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200433 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200434 return;
435
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200436 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200437
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500438 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200439
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200440 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500441 pixman_region32_fini(&surface->transform.opaque);
442 pixman_region32_init(&surface->transform.opaque);
443
444 if (region_is_undefined(&surface->input))
445 pixman_region32_init_rect(&surface->input, 0, 0,
446 surface->geometry.width,
447 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200448
Pekka Paalanencd403622012-01-25 13:37:39 +0200449 /* transform.position is always in transformation_list */
450 if (surface->geometry.transformation_list.next ==
451 &surface->transform.position.link &&
452 surface->geometry.transformation_list.prev ==
453 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200454 weston_surface_update_transform_disable(surface);
455 } else {
456 if (weston_surface_update_transform_enable(surface) < 0)
457 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200458 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200459
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400460 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200461
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300462 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200463 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200464}
465
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200466WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100467weston_surface_to_global_fixed(struct weston_surface *surface,
468 wl_fixed_t sx, wl_fixed_t sy,
469 wl_fixed_t *x, wl_fixed_t *y)
470{
471 GLfloat xf, yf;
472
473 weston_surface_to_global_float(surface,
474 wl_fixed_to_double(sx),
475 wl_fixed_to_double(sy),
476 &xf, &yf);
477 *x = wl_fixed_from_double(xf);
478 *y = wl_fixed_from_double(yf);
479}
480
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200481static void
482surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100483 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200484{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200485 if (surface->transform.enabled) {
486 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
487
488 weston_matrix_transform(&surface->transform.inverse, &v);
489
490 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200491 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200492 "weston_surface_from_global(), divisor = %g\n",
493 v.f[3]);
494 *sx = 0;
495 *sy = 0;
496 return;
497 }
498
Pekka Paalanencd403622012-01-25 13:37:39 +0200499 *sx = v.f[0] / v.f[3];
500 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200501 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200502 *sx = x - surface->geometry.x;
503 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200504 }
505}
506
507WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100508weston_surface_from_global_fixed(struct weston_surface *surface,
509 wl_fixed_t x, wl_fixed_t y,
510 wl_fixed_t *sx, wl_fixed_t *sy)
511{
512 GLfloat sxf, syf;
513
Daniel Stonebd3489b2012-05-08 17:17:53 +0100514 surface_from_global_float(surface,
515 wl_fixed_to_double(x),
516 wl_fixed_to_double(y),
517 &sxf, &syf);
518 *sx = wl_fixed_from_double(sxf);
519 *sy = wl_fixed_from_double(syf);
520}
521
522WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200523weston_surface_from_global(struct weston_surface *surface,
524 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
525{
526 GLfloat sxf, syf;
527
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200528 surface_from_global_float(surface, x, y, &sxf, &syf);
529 *sx = floorf(sxf);
530 *sy = floorf(syf);
531}
532
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400533WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400534weston_surface_schedule_repaint(struct weston_surface *surface)
535{
536 struct weston_output *output;
537
538 wl_list_for_each(output, &surface->compositor->output_list, link)
539 if (surface->output_mask & (1 << output->id))
540 weston_output_schedule_repaint(output);
541}
542
543WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500544weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500545{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400546 pixman_region32_union_rect(&surface->damage, &surface->damage,
547 0, 0, surface->geometry.width,
548 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200549
Kristian Høgsberg98238702012-08-03 16:29:12 -0400550 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500551}
552
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400553WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500554weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200555 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400556{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200557 surface->geometry.x = x;
558 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200559 surface->geometry.width = width;
560 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200561 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400562}
563
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200564WL_EXPORT void
565weston_surface_set_position(struct weston_surface *surface,
566 GLfloat x, GLfloat y)
567{
568 surface->geometry.x = x;
569 surface->geometry.y = y;
570 surface->geometry.dirty = 1;
571}
572
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300573WL_EXPORT int
574weston_surface_is_mapped(struct weston_surface *surface)
575{
576 if (surface->output)
577 return 1;
578 else
579 return 0;
580}
581
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400582WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500583weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500584{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400585 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500586
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400587 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500588
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400589 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500590}
591
Tiago Vignatti9d393522012-02-10 16:26:19 +0200592static struct weston_surface *
593weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100594 wl_fixed_t x, wl_fixed_t y,
595 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200596{
597 struct weston_surface *surface;
598
599 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100600 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500601 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100602 wl_fixed_to_int(*sx),
603 wl_fixed_to_int(*sy),
604 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200605 return surface;
606 }
607
608 return NULL;
609}
610
611static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400612weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500613{
Scott Moreau447013d2012-02-18 05:05:29 -0700614 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500615 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400616 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500617
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400618 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100619 return;
620
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400621 surface = weston_compositor_pick_surface(seat->compositor,
622 pointer->x,
623 pointer->y,
624 &pointer->current_x,
625 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500626
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400627 if (&surface->surface != pointer->current) {
628 interface = pointer->grab->interface;
629 pointer->current = &surface->surface;
630 interface->focus(pointer->grab, &surface->surface,
631 pointer->current_x,
632 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500633 }
634
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400635 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500636 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100637 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400638 pointer->x,
639 pointer->y,
640 &pointer->grab->x,
641 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500642}
643
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400644static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500645weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400646{
Daniel Stone37816df2012-05-16 18:45:18 +0100647 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400648
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500649 if (!compositor->focus)
650 return;
651
Daniel Stone37816df2012-05-16 18:45:18 +0100652 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400653 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400654}
655
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400656WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500657weston_surface_unmap(struct weston_surface *surface)
658{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100659 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500660
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500661 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500662 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500663 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500664
Daniel Stone4dab5db2012-05-30 16:31:53 +0100665 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100666 if (seat->seat.keyboard &&
667 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100668 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100669 if (seat->seat.pointer &&
670 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100671 wl_pointer_set_focus(seat->seat.pointer,
672 NULL,
673 wl_fixed_from_int(0),
674 wl_fixed_from_int(0));
675 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500676
Kristian Høgsberg98238702012-08-03 16:29:12 -0400677 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500678}
679
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400680struct weston_frame_callback {
681 struct wl_resource resource;
682 struct wl_list link;
683};
684
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500685static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400686destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500687{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200688 int i;
689
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500690 struct weston_surface *surface =
691 container_of(resource,
692 struct weston_surface, surface.resource);
693 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400694 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400695
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300696 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500697 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400698
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200699 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200700
Benjamin Franzke06286262011-05-06 19:12:33 +0200701 if (surface->buffer)
702 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400703
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200704 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400705 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200706 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200707
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200708 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200709 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500710 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500711 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500712 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500713 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200714
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400715 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
716 wl_resource_destroy(&cb->resource);
717
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400718 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500719}
720
Alex Wu8811bf92012-02-28 18:07:54 +0800721WL_EXPORT void
722weston_surface_destroy(struct weston_surface *surface)
723{
724 /* Not a valid way to destroy a client surface */
725 assert(surface->surface.resource.client == NULL);
726
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400727 wl_signal_emit(&surface->surface.resource.destroy_signal,
728 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800729 destroy_surface(&surface->surface.resource);
730}
731
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100732static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200733ensure_textures(struct weston_surface *es, int num_textures)
734{
735 int i;
736
737 if (num_textures <= es->num_textures)
738 return;
739
740 for (i = es->num_textures; i < num_textures; i++) {
741 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500742 glBindTexture(es->target, es->textures[i]);
743 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200744 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500745 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200746 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
747 }
748 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500749 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200750}
751
752static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400753weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100754{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500755 struct weston_surface *es = (struct weston_surface *) surface;
756 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400757 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200758 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100759
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300760 if (es->buffer) {
761 weston_buffer_post_release(es->buffer);
762 wl_list_remove(&es->buffer_destroy_listener.link);
763 }
764
765 es->buffer = buffer;
766
767 if (!buffer) {
768 if (weston_surface_is_mapped(es))
769 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200770 for (i = 0; i < es->num_images; i++) {
771 ec->destroy_image(ec->egl_display, es->images[i]);
772 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400773 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200774 es->num_images = 0;
775 glDeleteTextures(es->num_textures, es->textures);
776 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300777 return;
778 }
779
780 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400781 wl_signal_add(&es->buffer->resource.destroy_signal,
782 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300783
784 if (es->geometry.width != buffer->width ||
785 es->geometry.height != buffer->height) {
786 undef_region(&es->input);
787 pixman_region32_fini(&es->opaque);
788 pixman_region32_init(&es->opaque);
789 }
790
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100791 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200792 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
Gwenole Beauchesne6d030492012-04-20 11:15:51 +0200793 es->shader = &ec->texture_shader_rgba;
Rob Clarke3b95912012-08-31 16:42:18 -0500794 es->target = GL_TEXTURE_2D;
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200795
796 ensure_textures(es, 1);
797 glBindTexture(GL_TEXTURE_2D, es->textures[0]);
Kristian Høgsbergfb6de222012-03-27 17:10:13 -0400798 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
799 es->pitch, es->buffer->height, 0,
800 GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL);
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400801 if (wl_shm_buffer_get_format(buffer) == WL_SHM_FORMAT_XRGB8888)
802 es->blend = 0;
803 else
804 es->blend = 1;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200805 } else if (ec->query_buffer(ec->egl_display, buffer,
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400806 EGL_TEXTURE_FORMAT, &format)) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200807 for (i = 0; i < es->num_images; i++)
Kristian Høgsberg971cbc82012-07-17 14:21:25 -0400808 ec->destroy_image(ec->egl_display, es->images[i]);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200809 es->num_images = 0;
Rob Clarke3b95912012-08-31 16:42:18 -0500810 es->target = GL_TEXTURE_2D;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400811 switch (format) {
812 case EGL_TEXTURE_RGB:
813 case EGL_TEXTURE_RGBA:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200814 default:
815 num_planes = 1;
816 es->shader = &ec->texture_shader_rgba;
817 break;
Rob Clarke3b95912012-08-31 16:42:18 -0500818 case EGL_TEXTURE_EXTERNAL_WL:
819 num_planes = 1;
820 es->target = GL_TEXTURE_EXTERNAL_OES;
821 es->shader = &ec->texture_shader_egl_external;
822 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400823 case EGL_TEXTURE_Y_UV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200824 num_planes = 2;
825 es->shader = &ec->texture_shader_y_uv;
826 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400827 case EGL_TEXTURE_Y_U_V_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200828 num_planes = 3;
829 es->shader = &ec->texture_shader_y_u_v;
830 break;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400831 case EGL_TEXTURE_Y_XUXV_WL:
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200832 num_planes = 2;
833 es->shader = &ec->texture_shader_y_xuxv;
834 break;
835 }
836
837 ensure_textures(es, num_planes);
838 for (i = 0; i < num_planes; i++) {
839 attribs[0] = EGL_WAYLAND_PLANE_WL;
840 attribs[1] = i;
841 attribs[2] = EGL_NONE;
842 es->images[i] = ec->create_image(ec->egl_display,
843 NULL,
844 EGL_WAYLAND_BUFFER_WL,
845 buffer, attribs);
Rob Clark48cd58b2012-08-13 17:39:17 -0500846 if (!es->images[i]) {
847 weston_log("failed to create img for plane %d\n", i);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200848 continue;
Rob Clark48cd58b2012-08-13 17:39:17 -0500849 }
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200850 es->num_images++;
851
852 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -0500853 glBindTexture(es->target, es->textures[i]);
854 ec->image_target_texture_2d(es->target,
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200855 es->images[i]);
856 }
Kristian Høgsbergf389cac2011-08-31 16:21:38 -0400857
Ander Conselvan de Oliveira9390ae32012-03-27 17:36:38 +0300858 es->pitch = buffer->width;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200859 } else {
Rob Clark48cd58b2012-08-13 17:39:17 -0500860 weston_log("unhandled buffer type!\n");
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100861 }
862}
863
Rob Clark0e5a2d02012-08-30 16:47:18 -0500864
865#define max(a, b) (((a) > (b)) ? (a) : (b))
866#define min(a, b) (((a) > (b)) ? (b) : (a))
867#define clip(x, a, b) min(max(x, a), b)
868#define sign(x) ((x) >= 0)
869
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400870static int
Rob Clark0e5a2d02012-08-30 16:47:18 -0500871calculate_edges(struct weston_surface *es, pixman_box32_t *rect,
872 pixman_box32_t *surf_rect, GLfloat *ex, GLfloat *ey)
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -0500873{
Rob Clark0e5a2d02012-08-30 16:47:18 -0500874 int i, n = 0;
875 GLfloat min_x, max_x, min_y, max_y;
876 GLfloat x[4] = {
877 surf_rect->x1, surf_rect->x2, surf_rect->x2, surf_rect->x1,
878 };
879 GLfloat y[4] = {
880 surf_rect->y1, surf_rect->y1, surf_rect->y2, surf_rect->y2,
881 };
882 GLfloat cx1 = rect->x1;
883 GLfloat cx2 = rect->x2;
884 GLfloat cy1 = rect->y1;
885 GLfloat cy2 = rect->y2;
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500886
Rob Clark0e5a2d02012-08-30 16:47:18 -0500887 GLfloat dist_squared(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
888 {
889 GLfloat dx = (x1 - x2);
890 GLfloat dy = (y1 - y2);
891 return dx * dx + dy * dy;
892 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -0400893
Rob Clark0e5a2d02012-08-30 16:47:18 -0500894 void append_vertex(GLfloat x, GLfloat y)
895 {
896 /* don't emit duplicate vertices: */
897 if ((n > 0) && (ex[n-1] == x) && (ey[n-1] == y))
898 return;
899 ex[n] = x;
900 ey[n] = y;
901 n++;
902 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500903
Rob Clark0e5a2d02012-08-30 16:47:18 -0500904 /* transform surface to screen space: */
905 for (i = 0; i < 4; i++)
906 weston_surface_to_global_float(es, x[i], y[i], &x[i], &y[i]);
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500907
Rob Clark0e5a2d02012-08-30 16:47:18 -0500908 /* find bounding box: */
909 min_x = max_x = x[0];
910 min_y = max_y = y[0];
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -0500911
Rob Clark0e5a2d02012-08-30 16:47:18 -0500912 for (i = 1; i < 4; i++) {
913 min_x = min(min_x, x[i]);
914 max_x = max(max_x, x[i]);
915 min_y = min(min_y, y[i]);
916 max_y = max(max_y, y[i]);
917 }
Kristian Høgsberg525e4c02011-02-14 10:39:54 -0500918
Rob Clark0e5a2d02012-08-30 16:47:18 -0500919 /* First, simple bounding box check to discard early transformed
920 * surface rects that do not intersect with the clip region:
921 */
922 if ((min_x > cx2) || (max_x < cx1) ||
923 (min_y > cy2) || (max_y < cy1))
924 return 0;
925
926 /* Simple case, bounding box edges are parallel to surface edges,
927 * there will be only four edges. We just need to clip the surface
928 * vertices to the clip rect bounds:
929 */
930 if (!es->transform.enabled) {
931 for (i = 0; i < 4; i++) {
932 ex[n] = clip(x[i], cx1, cx2);
933 ey[n] = clip(y[i], cy1, cy2);
934 n++;
935 }
936 return 4;
937 }
938
939 /* Hard case, transformation applied. We need to find the vertices
940 * of the shape that is the intersection of the clip rect and
941 * transformed surface. This can be anything from 3 to 8 sides.
942 *
943 * Observation: all the resulting vertices will be the intersection
944 * points of the transformed surface and the clip rect, plus the
945 * vertices of the clip rect which are enclosed by the transformed
946 * surface and the vertices of the transformed surface which are
947 * enclosed by the clip rect.
948 *
949 * Observation: there will be zero, one, or two resulting vertices
950 * for each edge of the src rect.
951 *
952 * Loop over four edges of the transformed rect:
953 */
954 for (i = 0; i < 4; i++) {
955 GLfloat x1, y1, x2, y2;
956 int last_n = n;
957
958 x1 = x[i];
959 y1 = y[i];
960
961 /* if this vertex is contained in the clip rect, use it as-is: */
962 if ((cx1 <= x1) && (x1 <= cx2) &&
963 (cy1 <= y1) && (y1 <= cy2))
964 append_vertex(x1, y1);
965
966 /* for remaining, we consider the point as part of a line: */
967 x2 = x[(i+1) % 4];
968 y2 = y[(i+1) % 4];
969
970 if (x1 == x2) {
971 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
972 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
973 } else if (y1 == y2) {
974 append_vertex(clip(x1, cx1, cx2), clip(y1, cy1, cy2));
975 append_vertex(clip(x2, cx1, cx2), clip(y2, cy1, cy2));
976 } else {
977 GLfloat m, c, p;
978 GLfloat tx[2], ty[2];
979 int tn = 0;
980
981 int intersect_horiz(GLfloat y, GLfloat *p)
982 {
983 GLfloat x;
984
985 /* if y does not lie between y1 and y2, no
986 * intersection possible
987 */
988 if (sign(y-y1) == sign(y-y2))
989 return 0;
990
991 x = (y - c) / m;
992
993 /* if x does not lie between cx1 and cx2, no
994 * intersection:
995 */
996 if (sign(x-cx1) == sign(x-cx2))
997 return 0;
998
999 *p = x;
1000 return 1;
1001 }
1002
1003 int intersect_vert(GLfloat x, GLfloat *p)
1004 {
1005 GLfloat y;
1006
1007 if (sign(x-x1) == sign(x-x2))
1008 return 0;
1009
1010 y = m * x + c;
1011
1012 if (sign(y-cy1) == sign(y-cy2))
1013 return 0;
1014
1015 *p = y;
1016 return 1;
1017 }
1018
1019 /* y = mx + c */
1020 m = (y2 - y1) / (x2 - x1);
1021 c = y1 - m * x1;
1022
1023 /* check for up to two intersections with the four edges
1024 * of the clip rect. Note that we don't know the orientation
1025 * of the transformed surface wrt. the clip rect. So if when
1026 * there are two intersection points, we need to put the one
1027 * closest to x1,y1 first:
1028 */
1029
1030 /* check top clip rect edge: */
1031 if (intersect_horiz(cy1, &p)) {
1032 ty[tn] = cy1;
1033 tx[tn] = p;
1034 tn++;
1035 }
1036
1037 /* check right clip rect edge: */
1038 if (intersect_vert(cx2, &p)) {
1039 ty[tn] = p;
1040 tx[tn] = cx2;
1041 tn++;
1042 if (tn == 2)
1043 goto edge_check_done;
1044 }
1045
1046 /* check bottom clip rect edge: */
1047 if (intersect_horiz(cy2, &p)) {
1048 ty[tn] = cy2;
1049 tx[tn] = p;
1050 tn++;
1051 if (tn == 2)
1052 goto edge_check_done;
1053 }
1054
1055 /* check left clip rect edge: */
1056 if (intersect_vert(cx1, &p)) {
1057 ty[tn] = p;
1058 tx[tn] = cx1;
1059 tn++;
1060 }
1061
1062edge_check_done:
1063 if (tn == 1) {
1064 append_vertex(tx[0], ty[0]);
1065 } else if (tn == 2) {
1066 if (dist_squared(x1, y1, tx[0], ty[0]) <
1067 dist_squared(x1, y1, tx[1], ty[1])) {
1068 append_vertex(tx[0], ty[0]);
1069 append_vertex(tx[1], ty[1]);
1070 } else {
1071 append_vertex(tx[1], ty[1]);
1072 append_vertex(tx[0], ty[0]);
1073 }
1074 }
1075
1076 if (n == last_n) {
1077 GLfloat best_x=0, best_y=0;
1078 uint32_t d, best_d = (unsigned int)-1; /* distance squared */
1079 uint32_t max_d = dist_squared(x2, y2,
1080 x[(i+2) % 4], y[(i+2) % 4]);
1081
1082 /* if there are no vertices on this line, it could be that
1083 * there is a vertex of the clip rect that is enclosed by
1084 * the transformed surface. Find the vertex of the clip
1085 * rect that is reached by the shortest line perpendicular
1086 * to the current edge, if any.
1087 *
1088 * slope of perpendicular is 1/m, so
1089 *
1090 * cy = -cx/m + c2
1091 * c2 = cy + cx/m
1092 *
1093 */
1094
1095 int perp_intersect(GLfloat cx, GLfloat cy, uint32_t *d)
1096 {
1097 GLfloat c2 = cy + cx/m;
1098 GLfloat x = (c2 - c) / (m + 1/m);
1099
1100 /* if the x position of the intersection of the
1101 * perpendicular with the transformed edge does
1102 * not lie within the bounds of the edge, then
1103 * no intersection:
1104 */
1105 if (sign(x-x1) == sign(x-x2))
1106 return 0;
1107
1108 *d = dist_squared(cx, cy, x, (m * x) + c);
1109
1110 /* if intersection distance is further away than
1111 * opposite edge of surface region, it is invalid:
1112 */
1113 if (*d > max_d)
1114 return 0;
1115
1116 return 1;
1117 }
1118
1119 if (perp_intersect(cx1, cy1, &d)) {
1120 best_x = cx1;
1121 best_y = cy1;
1122 best_d = d;
1123 }
1124
1125 if (perp_intersect(cx1, cy2, &d) && (d < best_d)) {
1126 best_x = cx1;
1127 best_y = cy2;
1128 best_d = d;
1129 }
1130
1131 if (perp_intersect(cx2, cy2, &d) && (d < best_d)) {
1132 best_x = cx2;
1133 best_y = cy2;
1134 best_d = d;
1135 }
1136
1137 if (perp_intersect(cx2, cy1, &d) && (d < best_d)) {
1138 best_x = cx2;
1139 best_y = cy1;
1140 best_d = d;
1141 }
1142
1143 if (best_d != (unsigned int)-1) // XXX can this happen?
1144 append_vertex(best_x, best_y);
1145 }
1146 }
1147
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001148 }
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05001149
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001150 return n;
1151}
1152
Rob Clark0e5a2d02012-08-30 16:47:18 -05001153static int
1154texture_region(struct weston_surface *es, pixman_region32_t *region,
1155 pixman_region32_t *surf_region)
1156{
1157 struct weston_compositor *ec = es->compositor;
1158 GLfloat *v, inv_width, inv_height;
1159 unsigned int *vtxcnt, nvtx = 0;
1160 pixman_box32_t *rects, *surf_rects;
1161 int i, j, k, nrects, nsurf;
1162
1163 rects = pixman_region32_rectangles(region, &nrects);
1164 surf_rects = pixman_region32_rectangles(surf_region, &nsurf);
1165
1166 /* worst case we can have 10 vertices per rect (ie. clipped into
1167 * an octagon):
1168 */
1169 v = wl_array_add(&ec->vertices, nrects * nsurf * 10 * 4 * sizeof *v);
1170 vtxcnt = wl_array_add(&ec->vtxcnt, nrects * nsurf * sizeof *vtxcnt);
1171
1172 inv_width = 1.0 / es->pitch;
1173 inv_height = 1.0 / es->geometry.height;
1174
1175 for (i = 0; i < nrects; i++) {
1176 pixman_box32_t *rect = &rects[i];
1177 for (j = 0; j < nsurf; j++) {
1178 pixman_box32_t *surf_rect = &surf_rects[j];
1179 GLfloat cx, cy;
1180 GLfloat ex[8], ey[8]; /* edge points in screen space */
1181 int n;
1182
1183 void emit_vertex(GLfloat gx, GLfloat gy)
1184 {
1185 GLfloat sx, sy;
1186 surface_from_global_float(es, gx, gy, &sx, &sy);
1187 /* In groups of 4 attributes, first two are 'position', 2nd two
1188 * are 'texcoord'.
1189 */
1190 *(v++) = gx;
1191 *(v++) = gy;
1192 *(v++) = sx * inv_width;
1193 *(v++) = sy * inv_height;
1194 }
1195
1196 /* The transformed surface, after clipping to the clip region,
1197 * can have as many as eight sides, emitted as a triangle-fan.
1198 * The first vertex is the center, followed by each corner.
1199 *
1200 * If a corner of the transformed surface falls outside of the
1201 * clip region, instead of emitting one vertex for the corner
1202 * of the surface, up to two are emitted for two corresponding
1203 * intersection point(s) between the surface and the clip region.
1204 *
1205 * To do this, we first calculate the (up to eight) points that
1206 * form the intersection of the clip rect and the transformed
1207 * surface. After that we calculate the average to determine
1208 * the center point, and emit the center and edge vertices of
1209 * the fan.
1210 */
1211 n = calculate_edges(es, rect, surf_rect, ex, ey);
1212 if (n < 3)
1213 continue;
1214
1215 /* calculate/emit center point: */
1216 cx = 0;
1217 cy = 0;
1218 for (k = 0; k < n; k++) {
1219 cx += ex[k];
1220 cy += ey[k];
1221 }
1222 cx /= n;
1223 cy /= n;
1224 emit_vertex(cx, cy);
1225
1226 /* then emit edge points: */
1227 for (k = 0; k < n; k++)
1228 emit_vertex(ex[k], ey[k]);
1229
1230 /* and close the fan: */
1231 emit_vertex(ex[0], ey[0]);
1232
1233 vtxcnt[nvtx++] = n + 2;
1234 }
1235 }
1236
1237 return nvtx;
1238}
1239
1240static void
1241repaint_region(struct weston_surface *es, pixman_region32_t *region,
1242 pixman_region32_t *surf_region)
1243{
1244 struct weston_compositor *ec = es->compositor;
1245 GLfloat *v;
1246 unsigned int *vtxcnt;
1247 int i, first, nfans;
1248
1249 /* The final region to be painted is the intersection of
1250 * 'region' and 'surf_region'. However, 'region' is in the global
1251 * coordinates, and 'surf_region' is in the surface-local
1252 * corodinates. texture_region() will iterate over all pairs of
1253 * rectangles from both regions, compute the intersection
1254 * polygon for each pair, and store it as a triangle fan if
1255 * it has a non-zero area.
1256 */
1257 nfans = texture_region(es, region, surf_region);
1258
1259 v = ec->vertices.data;
1260 vtxcnt = ec->vtxcnt.data;
1261
1262 /* position: */
1263 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
1264 glEnableVertexAttribArray(0);
1265
1266 /* texcoord: */
1267 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
1268 glEnableVertexAttribArray(1);
1269
1270 for (i = 0, first = 0; i < nfans; i++) {
1271 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
1272 first += vtxcnt[i];
1273 }
1274
1275 glDisableVertexAttribArray(1);
1276 glDisableVertexAttribArray(0);
1277
1278 ec->vertices.size = 0;
1279 ec->vtxcnt.size = 0;
1280}
1281
1282
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001283WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001284weston_surface_draw(struct weston_surface *es, struct weston_output *output,
1285 pixman_region32_t *damage)
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001286{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001287 struct weston_compositor *ec = es->compositor;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001288 /* repaint bounding region in global coordinates: */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001289 pixman_region32_t repaint;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001290 /* regions of surface to draw opaque/blended in surface coordinates: */
1291 pixman_region32_t surface_opaque, surface_blend;
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001292 GLint filter;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001293 int i;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001294
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001295 pixman_region32_init(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001296 pixman_region32_init(&surface_opaque);
1297 pixman_region32_init(&surface_blend);
1298
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001299 pixman_region32_intersect(&repaint,
1300 &es->transform.boundingbox, damage);
1301 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001302
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001303 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001304 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001305
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001306 pixman_region32_subtract(&ec->primary_plane.damage,
1307 &ec->primary_plane.damage, &repaint);
1308
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001309 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001310 if (es->blend || es->alpha < 1.0) {
1311 /* blended region is whole surface minus opaque region: */
1312 pixman_region32_init_rect(&surface_blend, 0, 0,
1313 es->geometry.width, es->geometry.height);
1314 pixman_region32_init(&surface_opaque);
1315 pixman_region32_copy(&surface_opaque, &es->opaque);
1316 pixman_region32_subtract(&surface_blend, &surface_blend,
1317 &surface_opaque);
1318 } else {
1319 /* whole surface is opaque: */
1320 pixman_region32_init_rect(&surface_opaque, 0, 0,
1321 es->geometry.width, es->geometry.height);
1322 pixman_region32_init(&surface_blend);
1323 }
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001324
Kristian Høgsberg07632622012-01-25 23:02:06 -05001325 if (ec->current_shader != es->shader) {
1326 glUseProgram(es->shader->program);
Kristian Høgsberg07632622012-01-25 23:02:06 -05001327 ec->current_shader = es->shader;
1328 }
1329
Kristian Høgsberg0452abc2012-01-31 15:38:36 -05001330 glUniformMatrix4fv(es->shader->proj_uniform,
1331 1, GL_FALSE, output->matrix.d);
Pekka Paalanenf55f5442012-02-02 16:49:05 +02001332 glUniform4fv(es->shader->color_uniform, 1, es->color);
Kristian Høgsberga416fa12012-05-21 14:06:52 -04001333 glUniform1f(es->shader->alpha_uniform, es->alpha);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001334
Scott Moreauccbf29d2012-02-22 14:21:41 -07001335 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001336 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001337 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001338 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001339
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001340 for (i = 0; i < es->num_textures; i++) {
1341 glUniform1i(es->shader->tex_uniforms[i], i);
1342 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001343 glBindTexture(es->target, es->textures[i]);
1344 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1345 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001346 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001347
Rob Clark0e5a2d02012-08-30 16:47:18 -05001348 if (pixman_region32_not_empty(&surface_opaque)) {
1349 glDisable(GL_BLEND);
1350 repaint_region(es, &repaint, &surface_opaque);
1351 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001352
Rob Clark0e5a2d02012-08-30 16:47:18 -05001353 if (pixman_region32_not_empty(&surface_blend)) {
1354 glEnable(GL_BLEND);
1355 repaint_region(es, &repaint, &surface_blend);
1356 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001357
1358out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001359 pixman_region32_fini(&repaint);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001360 pixman_region32_fini(&surface_opaque);
1361 pixman_region32_fini(&surface_blend);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001362}
1363
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001364WL_EXPORT void
1365weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001366{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001367 wl_list_remove(&surface->layer_link);
1368 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001369 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001370 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001371}
1372
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001373WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001374weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001375{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001376 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001377
1378 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001379 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001380}
1381
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001382WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001383weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001384{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001385 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001386 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001387
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001388 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001389 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001390}
1391
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001392WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001393weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001394{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001395 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001396
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001397 pixman_region32_union(&compositor->primary_plane.damage,
1398 &compositor->primary_plane.damage,
1399 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001400 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001401}
1402
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001403static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404fade_frame(struct weston_animation *animation,
1405 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001406{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001407 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001408 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001409 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001410 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001411
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001412 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001413 compositor->fade.spring.timestamp = msecs;
1414
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001415 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001417 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1418 compositor->fade.spring.current);
1419 weston_surface_damage(surface);
1420
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001421 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001422 compositor->fade.spring.current =
1423 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001424 wl_list_remove(&animation->link);
1425 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001426
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001427 if (compositor->fade.spring.current < 0.001) {
1428 destroy_surface(&surface->surface.resource);
1429 compositor->fade.surface = NULL;
1430 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001431 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001432 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001433 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001434 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001435}
1436
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001437static void
1438update_shm_texture(struct weston_surface *surface)
1439{
1440#ifdef GL_UNPACK_ROW_LENGTH
1441 pixman_box32_t *rectangles;
1442 void *data;
1443 int i, n;
1444#endif
1445
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001446 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001447
1448 if (!surface->compositor->has_unpack_subimage) {
1449 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1450 surface->pitch, surface->buffer->height, 0,
1451 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1452 wl_shm_buffer_get_data(surface->buffer));
1453
1454 return;
1455 }
1456
1457#ifdef GL_UNPACK_ROW_LENGTH
1458 /* Mesa does not define GL_EXT_unpack_subimage */
1459 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1460 data = wl_shm_buffer_get_data(surface->buffer);
1461 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1462 for (i = 0; i < n; i++) {
1463 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1464 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1465 glTexSubImage2D(GL_TEXTURE_2D, 0,
1466 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001467 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001468 rectangles[i].y2 - rectangles[i].y1,
1469 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1470 }
1471#endif
1472}
1473
1474static void
1475surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001476 pixman_region32_t *opaque)
1477{
1478 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1479 update_shm_texture(surface);
1480
1481 if (surface->transform.enabled) {
1482 pixman_box32_t *extents;
1483
1484 extents = pixman_region32_extents(&surface->damage);
1485 surface_compute_bbox(surface, extents->x1, extents->y1,
1486 extents->x2 - extents->x1,
1487 extents->y2 - extents->y1,
1488 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001489 pixman_region32_translate(&surface->damage,
1490 -surface->plane->x,
1491 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001492 } else {
1493 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001494 surface->geometry.x - surface->plane->x,
1495 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001496 }
1497
1498 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001499 pixman_region32_union(&surface->plane->damage,
1500 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001501 empty_region(&surface->damage);
1502 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001503 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001504}
1505
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001506static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001507weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001508{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001509 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001510 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001511 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001512 struct weston_animation *animation, *next;
1513 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001514 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001515 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001516 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001517
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001518 weston_compositor_update_drag_surfaces(ec);
1519
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001520 width = output->current->width +
1521 output->border.left + output->border.right;
1522 height = output->current->height +
1523 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001524
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001525 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001526
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001527 /* Rebuild the surface list and update surface transforms up front. */
1528 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001529 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001530 wl_list_for_each(layer, &ec->layer_list, link) {
1531 wl_list_for_each(es, &layer->surface_list, layer_link) {
1532 weston_surface_update_transform(es);
1533 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001534 if (es->output == output) {
1535 wl_list_insert_list(&frame_callback_list,
1536 &es->frame_callback_list);
1537 wl_list_init(&es->frame_callback_list);
1538 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001539 }
1540 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001541
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001542 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001543 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001544 else
1545 wl_list_for_each(es, &ec->surface_list, link)
1546 weston_surface_move_to_plane(es, &ec->primary_plane);
1547
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001548 pixman_region32_init(&opaque);
1549
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001550 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001551 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001552
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001553 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001554
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001555 pixman_region32_init(&output_damage);
1556
1557 pixman_region32_init(&new_damage);
1558 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1559
1560 pixman_region32_union(&ec->primary_plane.damage,
1561 &ec->primary_plane.damage,
1562 &output->previous_damage);
1563
1564 pixman_region32_intersect(&output->previous_damage,
1565 &new_damage, &output->region);
1566
1567 pixman_region32_intersect(&output_damage,
1568 &ec->primary_plane.damage, &output->region);
1569
1570 pixman_region32_fini(&new_damage);
1571
Scott Moreauccbf29d2012-02-22 14:21:41 -07001572 if (output->dirty)
1573 weston_output_update_matrix(output);
1574
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001575 output->repaint(output, &output_damage);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001576
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001577 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001578
Kristian Høgsbergef044142011-06-21 15:02:12 -04001579 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001580
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001581 weston_compositor_repick(ec);
1582 wl_event_loop_dispatch(ec->input_loop, 0);
1583
Jonas Ådahldb773762012-06-13 00:01:21 +02001584 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001585 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001586 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001587 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001588 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001589
Scott Moreaud64cf212012-06-08 19:40:54 -06001590 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001591 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001592 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001593 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001594}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001595
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001596static int
1597weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001598{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001599 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001600
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001601 wl_event_loop_dispatch(compositor->input_loop, 0);
1602
1603 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001604}
1605
1606WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001607weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001608{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001609 struct weston_compositor *compositor = output->compositor;
1610 struct wl_event_loop *loop =
1611 wl_display_get_event_loop(compositor->wl_display);
1612 int fd;
1613
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001614 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001615 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001616 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001617 return;
1618 }
1619
1620 output->repaint_scheduled = 0;
1621 if (compositor->input_loop_source)
1622 return;
1623
1624 fd = wl_event_loop_get_fd(compositor->input_loop);
1625 compositor->input_loop_source =
1626 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1627 weston_compositor_read_input, compositor);
1628}
1629
1630static void
1631idle_repaint(void *data)
1632{
1633 struct weston_output *output = data;
1634
1635 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001636}
1637
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001638WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001639weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1640{
1641 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001642 if (below != NULL)
1643 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001644}
1645
1646WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001647weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001648{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001649 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001650 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001651
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001652 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001653 return;
1654
Kristian Høgsbergef044142011-06-21 15:02:12 -04001655 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001656 output->repaint_needed = 1;
1657 if (output->repaint_scheduled)
1658 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001659
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001660 wl_event_loop_add_idle(loop, idle_repaint, output);
1661 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001662
1663 if (compositor->input_loop_source) {
1664 wl_event_source_remove(compositor->input_loop_source);
1665 compositor->input_loop_source = NULL;
1666 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001667}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001668
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001669WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001670weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1671{
1672 struct weston_output *output;
1673
1674 wl_list_for_each(output, &compositor->output_list, link)
1675 weston_output_schedule_repaint(output);
1676}
1677
1678WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001679weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001680{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001681 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001682 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001683
Scott Moreau9d1b1122012-06-08 19:40:53 -06001684 output = container_of(compositor->output_list.next,
1685 struct weston_output, link);
1686
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001687 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001688 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001689 return;
1690
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001691 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001692 surface = weston_surface_create(compositor);
1693 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001694 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001695 wl_list_insert(&compositor->fade_layer.surface_list,
1696 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001697 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001698 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001699 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001700 }
1701
1702 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001703 if (wl_list_empty(&compositor->fade.animation.link)) {
1704 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001705 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001706 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001707 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001708}
1709
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001710static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001711surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001712{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001713 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001714}
1715
Casey Dahlin96d8a752012-04-19 22:50:07 -04001716static struct wl_resource *
1717find_resource_for_client(struct wl_list *list, struct wl_client *client)
1718{
1719 struct wl_resource *r;
1720
1721 wl_list_for_each(r, list, link) {
1722 if (r->client == client)
1723 return r;
1724 }
1725
1726 return NULL;
1727}
1728
Casey Dahlin9074db52012-04-19 22:50:09 -04001729static void
1730weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1731{
1732 uint32_t different = es->output_mask ^ mask;
1733 uint32_t entered = mask & different;
1734 uint32_t left = es->output_mask & different;
1735 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001736 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001737 struct wl_client *client = es->surface.resource.client;
1738
Scott Moreaua5021522012-08-03 17:11:51 -06001739 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001740 if (es->surface.resource.client == NULL)
1741 return;
1742 if (different == 0)
1743 return;
1744
Casey Dahlin9074db52012-04-19 22:50:09 -04001745 wl_list_for_each(output, &es->compositor->output_list, link) {
1746 if (1 << output->id & different)
1747 resource =
1748 find_resource_for_client(&output->resource_list,
1749 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001750 if (resource == NULL)
1751 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001752 if (1 << output->id & entered)
1753 wl_surface_send_enter(&es->surface.resource, resource);
1754 if (1 << output->id & left)
1755 wl_surface_send_leave(&es->surface.resource, resource);
1756 }
1757}
1758
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001759WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001760weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001761{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001762 struct weston_compositor *ec = es->compositor;
1763 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001764 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001765 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001766 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001767
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001768 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001769 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001770 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001771 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001772 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001773 pixman_region32_intersect(&region, &es->transform.boundingbox,
1774 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001775
1776 e = pixman_region32_extents(&region);
1777 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1778
Casey Dahlin9074db52012-04-19 22:50:09 -04001779 if (area > 0)
1780 mask |= 1 << output->id;
1781
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001782 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001783 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001784 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001785 }
1786 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001787 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001788
1789 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001790 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001791}
1792
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001793static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001794surface_attach(struct wl_client *client,
1795 struct wl_resource *resource,
1796 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1797{
1798 struct weston_surface *es = resource->data;
1799 struct wl_buffer *buffer = NULL;
1800
1801 if (buffer_resource)
1802 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001803
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001804 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001805
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001806 if (buffer && es->configure)
1807 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001808}
1809
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001810static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001811surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001812 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001813 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001814{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001815 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001816
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001817 pixman_region32_union_rect(&es->damage, &es->damage,
1818 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001819 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001820}
1821
Kristian Høgsberg33418202011-08-16 23:01:28 -04001822static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001823destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001824{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001825 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001826
1827 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001828 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001829}
1830
1831static void
1832surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001833 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001834{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001835 struct weston_frame_callback *cb;
1836 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001837
1838 cb = malloc(sizeof *cb);
1839 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001840 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001841 return;
1842 }
1843
1844 cb->resource.object.interface = &wl_callback_interface;
1845 cb->resource.object.id = callback;
1846 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001847 cb->resource.client = client;
1848 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001849
1850 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001851 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001852}
1853
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001854static void
1855surface_set_opaque_region(struct wl_client *client,
1856 struct wl_resource *resource,
1857 struct wl_resource *region_resource)
1858{
1859 struct weston_surface *surface = resource->data;
1860 struct weston_region *region;
1861
1862 pixman_region32_fini(&surface->opaque);
1863
1864 if (region_resource) {
1865 region = region_resource->data;
1866 pixman_region32_init_rect(&surface->opaque, 0, 0,
1867 surface->geometry.width,
1868 surface->geometry.height);
1869 pixman_region32_intersect(&surface->opaque,
1870 &surface->opaque, &region->region);
1871 } else {
1872 pixman_region32_init(&surface->opaque);
1873 }
1874
1875 surface->geometry.dirty = 1;
1876}
1877
1878static void
1879surface_set_input_region(struct wl_client *client,
1880 struct wl_resource *resource,
1881 struct wl_resource *region_resource)
1882{
1883 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001884 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001885
1886 if (region_resource) {
1887 region = region_resource->data;
1888 pixman_region32_init_rect(&surface->input, 0, 0,
1889 surface->geometry.width,
1890 surface->geometry.height);
1891 pixman_region32_intersect(&surface->input,
1892 &surface->input, &region->region);
1893 } else {
1894 pixman_region32_init_rect(&surface->input, 0, 0,
1895 surface->geometry.width,
1896 surface->geometry.height);
1897 }
1898
Kristian Høgsberg98238702012-08-03 16:29:12 -04001899 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001900}
1901
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001902static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001903 surface_destroy,
1904 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001905 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001906 surface_frame,
1907 surface_set_opaque_region,
1908 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001909};
1910
1911static void
1912compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001913 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001914{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001915 struct weston_compositor *ec = resource->data;
1916 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001917
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001918 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001919 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001920 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001921 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001922 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001923
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001924 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001925
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001926 surface->surface.resource.object.id = id;
1927 surface->surface.resource.object.interface = &wl_surface_interface;
1928 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001929 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001930 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001931
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001932 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001933}
1934
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001935static void
1936destroy_region(struct wl_resource *resource)
1937{
1938 struct weston_region *region =
1939 container_of(resource, struct weston_region, resource);
1940
1941 pixman_region32_fini(&region->region);
1942 free(region);
1943}
1944
1945static void
1946region_destroy(struct wl_client *client, struct wl_resource *resource)
1947{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001948 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001949}
1950
1951static void
1952region_add(struct wl_client *client, struct wl_resource *resource,
1953 int32_t x, int32_t y, int32_t width, int32_t height)
1954{
1955 struct weston_region *region = resource->data;
1956
1957 pixman_region32_union_rect(&region->region, &region->region,
1958 x, y, width, height);
1959}
1960
1961static void
1962region_subtract(struct wl_client *client, struct wl_resource *resource,
1963 int32_t x, int32_t y, int32_t width, int32_t height)
1964{
1965 struct weston_region *region = resource->data;
1966 pixman_region32_t rect;
1967
1968 pixman_region32_init_rect(&rect, x, y, width, height);
1969 pixman_region32_subtract(&region->region, &region->region, &rect);
1970 pixman_region32_fini(&rect);
1971}
1972
1973static const struct wl_region_interface region_interface = {
1974 region_destroy,
1975 region_add,
1976 region_subtract
1977};
1978
1979static void
1980compositor_create_region(struct wl_client *client,
1981 struct wl_resource *resource, uint32_t id)
1982{
1983 struct weston_region *region;
1984
1985 region = malloc(sizeof *region);
1986 if (region == NULL) {
1987 wl_resource_post_no_memory(resource);
1988 return;
1989 }
1990
1991 region->resource.destroy = destroy_region;
1992
1993 region->resource.object.id = id;
1994 region->resource.object.interface = &wl_region_interface;
1995 region->resource.object.implementation =
1996 (void (**)(void)) &region_interface;
1997 region->resource.data = region;
1998
1999 pixman_region32_init(&region->region);
2000
2001 wl_client_add_resource(client, &region->resource);
2002}
2003
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002004static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002005 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002006 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002007};
2008
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002009WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002010weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002011{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002012 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2013 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002014
2015 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002016 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002017}
2018
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002019static void
2020weston_compositor_dpms_on(struct weston_compositor *compositor)
2021{
2022 struct weston_output *output;
2023
2024 wl_list_for_each(output, &compositor->output_list, link)
2025 if (output->set_dpms)
2026 output->set_dpms(output, WESTON_DPMS_ON);
2027}
2028
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002029WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002030weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002031{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002032 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2033 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002034 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002035 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002036 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002037 }
2038}
2039
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002040static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002041weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002042{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002043 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002044 compositor->idle_inhibit++;
2045}
2046
2047static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002048weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002049{
2050 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002051 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002052}
2053
2054static int
2055idle_handler(void *data)
2056{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002057 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002058
2059 if (compositor->idle_inhibit)
2060 return 1;
2061
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002062 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002063
2064 return 1;
2065}
2066
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002067WL_EXPORT void
2068weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2069{
2070 pixman_region32_init(&plane->damage);
2071 plane->x = x;
2072 plane->y = y;
2073}
2074
2075WL_EXPORT void
2076weston_plane_release(struct weston_plane *plane)
2077{
2078 pixman_region32_fini(&plane->damage);
2079}
2080
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002081static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002082weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002083
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002084static void
Daniel Stone37816df2012-05-16 18:45:18 +01002085clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002086{
Daniel Stone37816df2012-05-16 18:45:18 +01002087 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002088 struct weston_output *output, *prev = NULL;
2089 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002090
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002091 x = wl_fixed_to_int(*fx);
2092 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002093 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2094 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002095
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002096 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002097 if (pixman_region32_contains_point(&output->region,
2098 x, y, NULL))
2099 valid = 1;
2100 if (pixman_region32_contains_point(&output->region,
2101 old_x, old_y, NULL))
2102 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002103 }
Daniel Stone37816df2012-05-16 18:45:18 +01002104
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002105 if (!valid) {
2106 if (x < prev->x)
2107 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002108 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002109 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002110 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002111 if (y < prev->y)
2112 *fy = wl_fixed_from_int(prev->y);
2113 else if (y >= prev->y + prev->current->height)
2114 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002115 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002116 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002117}
2118
2119WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002120notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002121{
2122 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002123 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002124 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002125 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002126 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002127
2128 weston_compositor_activity(ec);
2129
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002130 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002131
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002132 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002133
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002134 pointer->x = x;
2135 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002136
2137 ix = wl_fixed_to_int(x);
2138 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002139
Scott Moreauccbf29d2012-02-22 14:21:41 -07002140 wl_list_for_each(output, &ec->output_list, link)
2141 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002142 pixman_region32_contains_point(&output->region,
2143 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002144 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002145
Daniel Stone37816df2012-05-16 18:45:18 +01002146 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002147 interface = pointer->grab->interface;
2148 interface->motion(pointer->grab, time,
2149 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002150
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002151 if (seat->sprite) {
2152 weston_surface_set_position(seat->sprite,
2153 ix - seat->hotspot_x,
2154 iy - seat->hotspot_y);
2155 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002156 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002157}
2158
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002159WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002160weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002161 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002162{
Daniel Stone37816df2012-05-16 18:45:18 +01002163 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002164
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002165 if (seat->seat.keyboard) {
2166 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2167 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002168 }
2169
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002170 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002171}
2172
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002173WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002174notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002175 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002176{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002177 struct weston_compositor *compositor = seat->compositor;
2178 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002179 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002180 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002181 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002182
Daniel Stone4dbadb12012-05-30 16:31:51 +01002183 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002184 if (compositor->ping_handler && focus)
2185 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002186 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002187 if (pointer->button_count == 0) {
2188 pointer->grab_button = button;
2189 pointer->grab_time = time;
2190 pointer->grab_x = pointer->x;
2191 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002192 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002193 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002194 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002195 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002196 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002197 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002198
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002199 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002200 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002201
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002202 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002203
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002204 if (pointer->button_count == 1)
2205 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002206 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002207}
2208
Scott Moreau210d0792012-03-22 10:47:01 -06002209WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002210notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002211 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002212{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002213 struct weston_compositor *compositor = seat->compositor;
2214 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002215 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002216 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002217 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2218
2219 if (compositor->ping_handler && focus)
2220 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002221
2222 weston_compositor_activity(compositor);
2223
Scott Moreau6a3633d2012-03-20 08:47:59 -06002224 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002225 weston_compositor_run_axis_binding(compositor, seat,
2226 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002227 else
2228 return;
2229
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002230 if (pointer->focus_resource)
2231 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002232 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002233}
2234
Daniel Stone05d58682012-06-22 13:21:38 +01002235WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002236notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002237{
Daniel Stone05d58682012-06-22 13:21:38 +01002238 struct wl_keyboard *keyboard = &seat->keyboard;
2239 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002240 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002241 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002242 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002243 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002244
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002245 /* Serialize and update our internal state, checking to see if it's
2246 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002247 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2248 XKB_STATE_DEPRESSED);
2249 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2250 XKB_STATE_LATCHED);
2251 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2252 XKB_STATE_LOCKED);
2253 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002254 XKB_STATE_EFFECTIVE);
2255
Daniel Stone71c38772012-06-22 13:21:30 +01002256 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2257 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2258 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2259 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002260 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002261
Daniel Stone71c38772012-06-22 13:21:30 +01002262 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2263 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2264 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2265 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002266
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002267 /* And update the modifier_state for bindings. */
2268 mods_lookup = mods_depressed | mods_latched;
2269 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002270 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002271 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002272 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002273 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002274 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002275 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002276 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2277 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002278
Daniel Stone8c8164f2012-05-30 16:31:45 +01002279 /* Finally, notify the compositor that LEDs have changed. */
2280 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002281 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002282 leds |= LED_NUM_LOCK;
2283 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002284 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002285 leds |= LED_CAPS_LOCK;
2286 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002287 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002288 leds |= LED_SCROLL_LOCK;
2289 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002290 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002291 seat->xkb_state.leds = leds;
2292
Daniel Stone05d58682012-06-22 13:21:38 +01002293 if (changed) {
2294 grab->interface->modifiers(grab,
2295 serial,
2296 keyboard->modifiers.mods_depressed,
2297 keyboard->modifiers.mods_latched,
2298 keyboard->modifiers.mods_locked,
2299 keyboard->modifiers.group);
2300 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002301}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002302
Daniel Stone05d58682012-06-22 13:21:38 +01002303static void
2304update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002305 enum wl_keyboard_key_state state)
2306{
2307 enum xkb_key_direction direction;
2308
2309 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2310 direction = XKB_KEY_DOWN;
2311 else
2312 direction = XKB_KEY_UP;
2313
2314 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2315 * broken keycode system, which starts at 8. */
2316 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2317
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002318 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002319}
2320
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002321WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002322notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002323 enum wl_keyboard_key_state state,
2324 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002325{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002326 struct weston_compositor *compositor = seat->compositor;
2327 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002328 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002329 (struct weston_surface *) keyboard->focus;
2330 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002331 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002332 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002333
Daniel Stonec9785ea2012-05-30 16:31:52 +01002334 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002335 if (compositor->ping_handler && focus)
2336 compositor->ping_handler(focus, serial);
2337
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002338 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002339 keyboard->grab_key = key;
2340 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002341 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002342 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002343 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002344
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002345 end = keyboard->keys.data + keyboard->keys.size;
2346 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002347 if (*k == key) {
2348 /* Ignore server-generated repeats. */
2349 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2350 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002351 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002352 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002353 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002354 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002355 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002356 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002357 *k = key;
2358 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002359
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002360 if (grab == &keyboard->default_grab) {
2361 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002362 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002363 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002364 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002365
Daniel Stone7ace3902012-05-30 16:31:40 +01002366 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002367
2368 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002369 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002370 wl_display_get_serial(compositor->wl_display),
2371 key,
2372 state);
2373 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002374}
2375
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002376WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002377notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002378 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002379{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002380 struct weston_compositor *compositor = seat->compositor;
2381 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002382
2383 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002384 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002385 x - pointer->x,
2386 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002387
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002388 pointer->x = x;
2389 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002390 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002391 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002392 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002393 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002394 /* FIXME: We should call wl_pointer_set_focus(seat,
2395 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002396 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002397}
2398
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002399static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002400destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002401{
Daniel Stone37816df2012-05-16 18:45:18 +01002402 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002403
Daniel Stone37816df2012-05-16 18:45:18 +01002404 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002405 saved_kbd_focus_listener);
2406
Daniel Stone37816df2012-05-16 18:45:18 +01002407 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002408}
2409
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002410WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002411notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002412 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002413{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002414 struct weston_compositor *compositor = seat->compositor;
2415 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002416 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002417 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002418
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002419 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002420 wl_array_copy(&keyboard->keys, keys);
2421 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002422 weston_compositor_idle_inhibit(compositor);
2423 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002424 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002425 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002426 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002427
Daniel Stoneef172672012-06-22 13:21:32 +01002428 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002429 wl_array_for_each(k, &keyboard->keys) {
2430 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002431 WL_KEYBOARD_KEY_STATE_PRESSED);
2432 }
2433
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002434 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002435
2436 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002437 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2438 wl_keyboard_set_focus(keyboard, surface);
2439 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002440 }
2441}
2442
2443WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002444notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002445{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002446 struct weston_compositor *compositor = seat->compositor;
2447 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002448 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002449
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002450 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002451 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002452 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002453 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002454 WL_KEYBOARD_KEY_STATE_RELEASED);
2455 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002456
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002457 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002458
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002459 if (keyboard->focus) {
2460 seat->saved_kbd_focus = keyboard->focus;
2461 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002462 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002463 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2464 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002465 }
2466
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002467 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002468 /* FIXME: We really need keyboard grab cancel here to
2469 * let the grab shut down properly. As it is we leak
2470 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002471 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002472}
2473
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002474static void
Daniel Stone37816df2012-05-16 18:45:18 +01002475touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002476{
Daniel Stone37816df2012-05-16 18:45:18 +01002477 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002478 struct wl_resource *resource;
2479
Pekka Paalanen56464252012-07-31 13:21:08 +03002480 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002481 return;
2482
Pekka Paalanen56464252012-07-31 13:21:08 +03002483 if (seat->touch->focus_resource)
2484 wl_list_remove(&seat->touch->focus_listener.link);
2485 seat->touch->focus = NULL;
2486 seat->touch->focus_resource = NULL;
2487
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002488 if (surface) {
2489 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002490 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002491 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002492 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002493 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002494 return;
2495 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002496
Daniel Stone37816df2012-05-16 18:45:18 +01002497 seat->touch->focus = surface;
2498 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002499 wl_signal_add(&resource->destroy_signal,
2500 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002501 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002502}
2503
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002504/**
2505 * notify_touch - emulates button touches and notifies surfaces accordingly.
2506 *
2507 * It assumes always the correct cycle sequence until it gets here: touch_down
2508 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2509 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002510 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002511 */
2512WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002513notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002514 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002515{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002516 struct weston_compositor *ec = seat->compositor;
2517 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002518 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002519 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002520 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002521
2522 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002523 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002524 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002525
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002526 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002527
2528 /* the first finger down picks the surface, and all further go
2529 * to that surface for the remainder of the touch session i.e.
2530 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002531 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002532 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002533 touch_set_focus(seat, &es->surface);
2534 } else if (touch->focus) {
2535 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002536 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002537 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002538
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002539 if (touch->focus_resource && touch->focus)
2540 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002541 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002542 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002543 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002544 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002545 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002546 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002547 if (!es)
2548 break;
2549
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002550 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002551 if (touch->focus_resource)
2552 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002553 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002554 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002555 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002556 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002557 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002558
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002559 if (touch->focus_resource)
2560 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002561 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002562 if (seat->num_tp == 0)
2563 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002564 break;
2565 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002566}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002567
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002568static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002569pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2570{
2571 struct weston_seat *seat = container_of(listener, struct weston_seat,
2572 sprite_destroy_listener);
2573
2574 seat->sprite = NULL;
2575}
2576
2577static void
2578pointer_cursor_surface_configure(struct weston_surface *es,
2579 int32_t dx, int32_t dy)
2580{
2581 struct weston_seat *seat = es->private;
2582 int x, y;
2583
2584 assert(es == seat->sprite);
2585
2586 seat->hotspot_x -= dx;
2587 seat->hotspot_y -= dy;
2588
2589 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2590 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2591
2592 weston_surface_configure(seat->sprite, x, y,
2593 es->buffer->width, es->buffer->height);
2594
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002595 empty_region(&es->input);
2596
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002597 if (!weston_surface_is_mapped(es)) {
2598 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2599 &es->layer_link);
2600 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002601 }
2602}
2603
2604static void
2605pointer_unmap_sprite(struct weston_seat *seat)
2606{
2607 if (weston_surface_is_mapped(seat->sprite))
2608 weston_surface_unmap(seat->sprite);
2609
2610 wl_list_remove(&seat->sprite_destroy_listener.link);
2611 seat->sprite->configure = NULL;
2612 seat->sprite->private = NULL;
2613 seat->sprite = NULL;
2614}
2615
2616static void
2617pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2618 uint32_t serial, struct wl_resource *surface_resource,
2619 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002620{
Daniel Stone37816df2012-05-16 18:45:18 +01002621 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002622 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002623
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002624 if (surface_resource)
2625 surface = container_of(surface_resource->data,
2626 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002627
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002628 if (seat->seat.pointer->focus == NULL)
2629 return;
2630 if (seat->seat.pointer->focus->resource.client != client)
2631 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002632 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002633 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002634
2635 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002636 if (surface->configure) {
2637 wl_resource_post_error(&surface->surface.resource,
2638 WL_DISPLAY_ERROR_INVALID_OBJECT,
2639 "surface->configure already "
2640 "set");
2641 return;
2642 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002643 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002644
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002645 if (seat->sprite)
2646 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002647
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002648 if (!surface)
2649 return;
2650
2651 wl_signal_add(&surface->surface.resource.destroy_signal,
2652 &seat->sprite_destroy_listener);
2653
2654 surface->configure = pointer_cursor_surface_configure;
2655 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002656 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002657 seat->hotspot_x = x;
2658 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002659
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002660 if (surface->buffer)
2661 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002662}
2663
Daniel Stone37816df2012-05-16 18:45:18 +01002664static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002665 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002666};
2667
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002668static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002669handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002670{
Daniel Stone37816df2012-05-16 18:45:18 +01002671 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002672
Daniel Stone37816df2012-05-16 18:45:18 +01002673 seat = container_of(listener, struct weston_seat,
2674 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002675
Daniel Stone37816df2012-05-16 18:45:18 +01002676 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002677}
2678
Casey Dahlin9074db52012-04-19 22:50:09 -04002679static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002680{
2681 wl_list_remove(&resource->link);
2682 free(resource);
2683}
2684
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002685static void
Daniel Stone37816df2012-05-16 18:45:18 +01002686seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2687 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002688{
Daniel Stone37816df2012-05-16 18:45:18 +01002689 struct weston_seat *seat = resource->data;
2690 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002691
Daniel Stone37816df2012-05-16 18:45:18 +01002692 if (!seat->seat.pointer)
2693 return;
2694
2695 cr = wl_client_add_object(client, &wl_pointer_interface,
2696 &pointer_interface, id, seat);
2697 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002698 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002699
2700 if (seat->seat.pointer->focus &&
2701 seat->seat.pointer->focus->resource.client == client) {
2702 struct weston_surface *surface;
2703 wl_fixed_t sx, sy;
2704
2705 surface = (struct weston_surface *) seat->seat.pointer->focus;
2706 weston_surface_from_global_fixed(surface,
2707 seat->seat.pointer->x,
2708 seat->seat.pointer->y,
2709 &sx,
2710 &sy);
2711 wl_pointer_set_focus(seat->seat.pointer,
2712 seat->seat.pointer->focus,
2713 sx,
2714 sy);
2715 }
Daniel Stone37816df2012-05-16 18:45:18 +01002716}
2717
2718static void
2719seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2720 uint32_t id)
2721{
2722 struct weston_seat *seat = resource->data;
2723 struct wl_resource *cr;
2724
2725 if (!seat->seat.keyboard)
2726 return;
2727
2728 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2729 seat);
2730 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002731 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002732
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002733 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2734 seat->xkb_info.keymap_fd,
2735 seat->xkb_info.keymap_size);
2736
Daniel Stone17b13bd2012-05-30 16:31:39 +01002737 if (seat->seat.keyboard->focus &&
2738 seat->seat.keyboard->focus->resource.client == client) {
2739 wl_keyboard_set_focus(seat->seat.keyboard,
2740 seat->seat.keyboard->focus);
2741 }
Daniel Stone37816df2012-05-16 18:45:18 +01002742}
2743
2744static void
2745seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2746 uint32_t id)
2747{
2748 struct weston_seat *seat = resource->data;
2749 struct wl_resource *cr;
2750
2751 if (!seat->seat.touch)
2752 return;
2753
2754 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2755 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002756 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002757}
2758
2759static const struct wl_seat_interface seat_interface = {
2760 seat_get_pointer,
2761 seat_get_keyboard,
2762 seat_get_touch,
2763};
2764
2765static void
2766bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2767{
2768 struct wl_seat *seat = data;
2769 struct wl_resource *resource;
2770 enum wl_seat_capability caps = 0;
2771
2772 resource = wl_client_add_object(client, &wl_seat_interface,
2773 &seat_interface, id, data);
2774 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002775 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002776
2777 if (seat->pointer)
2778 caps |= WL_SEAT_CAPABILITY_POINTER;
2779 if (seat->keyboard)
2780 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2781 if (seat->touch)
2782 caps |= WL_SEAT_CAPABILITY_TOUCH;
2783
2784 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002785}
2786
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002787static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002788device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002789{
Daniel Stone37816df2012-05-16 18:45:18 +01002790 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002791
Daniel Stone37816df2012-05-16 18:45:18 +01002792 seat = container_of(listener, struct weston_seat,
2793 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002794
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002795 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002796}
2797
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002798static void weston_compositor_xkb_init(struct weston_compositor *ec,
2799 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002800{
Daniel Stonee379da92012-05-30 16:32:04 +01002801 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002802 ec->xkb_context = xkb_context_new(0);
2803 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002804 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002805 exit(1);
2806 }
Daniel Stone994679a2012-05-30 16:31:43 +01002807 }
2808
2809 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002810 ec->xkb_names = *names;
2811 if (!ec->xkb_names.rules)
2812 ec->xkb_names.rules = strdup("evdev");
2813 if (!ec->xkb_names.model)
2814 ec->xkb_names.model = strdup("pc105");
2815 if (!ec->xkb_names.layout)
2816 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002817}
2818
Daniel Stonee379da92012-05-30 16:32:04 +01002819static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2820{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002821 if (xkb_info->keymap)
2822 xkb_map_unref(xkb_info->keymap);
2823
2824 if (xkb_info->keymap_area)
2825 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2826 if (xkb_info->keymap_fd >= 0)
2827 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002828}
2829
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002830static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2831{
Daniel Stonee379da92012-05-30 16:32:04 +01002832 free((char *) ec->xkb_names.rules);
2833 free((char *) ec->xkb_names.model);
2834 free((char *) ec->xkb_names.layout);
2835 free((char *) ec->xkb_names.variant);
2836 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002837
Daniel Stonee379da92012-05-30 16:32:04 +01002838 xkb_info_destroy(&ec->xkb_info);
2839 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002840}
2841
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002842static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002843weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002844{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002845 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002846
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002847 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2848 XKB_MOD_NAME_SHIFT);
2849 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2850 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002851 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2852 XKB_MOD_NAME_CTRL);
2853 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2854 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002855 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2856 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002857 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2858 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002859 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002860
2861 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2862 XKB_LED_NAME_NUM);
2863 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2864 XKB_LED_NAME_CAPS);
2865 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2866 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002867
2868 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2869 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002870 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002871 exit(EXIT_FAILURE);
2872 }
2873 xkb_info->keymap_size = strlen(keymap_str) + 1;
2874
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002875 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002876 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002877 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002878 (unsigned long) xkb_info->keymap_size);
2879 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002880 }
2881
2882 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2883 PROT_READ | PROT_WRITE,
2884 MAP_SHARED, xkb_info->keymap_fd, 0);
2885 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002886 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002887 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002888 goto err_dev_zero;
2889 }
2890 strcpy(xkb_info->keymap_area, keymap_str);
2891 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002892
2893 return;
2894
2895err_dev_zero:
2896 close(xkb_info->keymap_fd);
2897 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002898err_keymap_str:
2899 free(keymap_str);
2900 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002901}
2902
2903static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002904weston_compositor_build_global_keymap(struct weston_compositor *ec)
2905{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002906 if (ec->xkb_info.keymap != NULL)
2907 return;
2908
Daniel Stonee379da92012-05-30 16:32:04 +01002909 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002910 &ec->xkb_names,
2911 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002912 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002913 weston_log("failed to compile global XKB keymap\n");
2914 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002915 "options %s",
2916 ec->xkb_names.rules, ec->xkb_names.model,
2917 ec->xkb_names.layout, ec->xkb_names.variant,
2918 ec->xkb_names.options);
2919 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002920 }
2921
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002922 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002923}
2924
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002925WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002926weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002927{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002928 if (seat->has_keyboard)
2929 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002930
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002931 if (keymap != NULL) {
2932 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002933 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002934 }
2935 else {
2936 weston_compositor_build_global_keymap(seat->compositor);
2937 seat->xkb_info = seat->compositor->xkb_info;
2938 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2939 }
Daniel Stoned65b9092012-05-30 16:32:05 +01002940
2941 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
2942 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002943 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01002944 exit(1);
2945 }
2946
Daniel Stone71c38772012-06-22 13:21:30 +01002947 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01002948
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002949 wl_keyboard_init(&seat->keyboard);
2950 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
2951
2952 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01002953}
2954
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002955WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002956weston_seat_init_pointer(struct weston_seat *seat)
2957{
2958 if (seat->has_pointer)
2959 return;
2960
2961 wl_pointer_init(&seat->pointer);
2962 wl_seat_set_pointer(&seat->seat, &seat->pointer);
2963
2964 seat->has_pointer = 1;
2965}
2966
2967WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01002968weston_seat_init_touch(struct weston_seat *seat)
2969{
2970 if (seat->has_touch)
2971 return;
2972
2973 wl_touch_init(&seat->touch);
2974 wl_seat_set_touch(&seat->seat, &seat->touch);
2975
2976 seat->has_touch = 1;
2977}
2978
2979WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01002980weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002981{
Daniel Stone37816df2012-05-16 18:45:18 +01002982 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01002983 seat->has_pointer = 0;
2984 seat->has_keyboard = 0;
2985 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05002986
Daniel Stone37816df2012-05-16 18:45:18 +01002987 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
2988 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04002989
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002990 seat->sprite = NULL;
2991 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04002992
Daniel Stone37816df2012-05-16 18:45:18 +01002993 seat->compositor = ec;
2994 seat->hotspot_x = 16;
2995 seat->hotspot_y = 16;
2996 seat->modifier_state = 0;
2997 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05002998
Daniel Stone37816df2012-05-16 18:45:18 +01002999 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003000 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02003001
Daniel Stone37816df2012-05-16 18:45:18 +01003002 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03003003
Daniel Stone37816df2012-05-16 18:45:18 +01003004 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
3005 wl_signal_add(&seat->seat.drag_icon_signal,
3006 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04003007
3008 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02003009 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003010}
3011
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003012WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003013weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003014{
Daniel Stone37816df2012-05-16 18:45:18 +01003015 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003016 /* The global object is destroyed at wl_display_destroy() time. */
3017
Daniel Stone37816df2012-05-16 18:45:18 +01003018 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003019 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003020
Daniel Stone74419a22012-05-30 16:32:02 +01003021 if (seat->xkb_state.state != NULL)
3022 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003023 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003024
Daniel Stone37816df2012-05-16 18:45:18 +01003025 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003026}
3027
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003028static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003029drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3030{
3031 weston_surface_configure(es,
3032 es->geometry.x + sx, es->geometry.y + sy,
3033 es->buffer->width, es->buffer->height);
3034}
3035
3036static int
Daniel Stone37816df2012-05-16 18:45:18 +01003037device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003038 struct weston_surface *surface)
3039{
Daniel Stone37816df2012-05-16 18:45:18 +01003040 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003041
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003042 if (surface->configure) {
3043 wl_resource_post_error(&surface->surface.resource,
3044 WL_DISPLAY_ERROR_INVALID_OBJECT,
3045 "surface->configure already set");
3046 return 0;
3047 }
3048
Daniel Stone37816df2012-05-16 18:45:18 +01003049 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003050
Daniel Stone37816df2012-05-16 18:45:18 +01003051 weston_surface_set_position(ws->drag_surface,
3052 wl_fixed_to_double(seat->pointer->x),
3053 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003054
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003055 surface->configure = drag_surface_configure;
3056
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003057 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003058 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003059
3060 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003061}
3062
3063static void
Daniel Stone37816df2012-05-16 18:45:18 +01003064device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003065{
Daniel Stone37816df2012-05-16 18:45:18 +01003066 seat->drag_surface->configure = NULL;
3067 undef_region(&seat->drag_surface->input);
3068 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3069 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003070}
3071
3072static void
Daniel Stone37816df2012-05-16 18:45:18 +01003073device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003074{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003075 struct wl_list *list;
3076
Daniel Stone37816df2012-05-16 18:45:18 +01003077 if (weston_surface_is_mapped(seat->drag_surface) ||
3078 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003079 return;
3080
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003081 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3082 list = &seat->sprite->layer_link;
3083 else
3084 list = &seat->compositor->cursor_layer.surface_list;
3085
3086 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003087 weston_surface_assign_output(seat->drag_surface);
3088 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003089}
3090
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003091static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003092weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003093 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003094{
3095 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003096
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003097 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003098 return;
3099
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003100 if (seat->drag_surface && seat->seat.drag_surface &&
3101 (&seat->drag_surface->surface.resource !=
3102 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003103 /* between calls to this funcion we got a new drag_surface */
3104 surface_changed = 1;
3105
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003106 if (!seat->seat.drag_surface || surface_changed) {
3107 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003108 if (!surface_changed)
3109 return;
3110 }
3111
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003112 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003113 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003114 seat->seat.drag_surface;
3115 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003116 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003117 }
3118
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003119 /* the client may not have attached a buffer to the drag surface
3120 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003121 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003122
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003123 /* the client may have attached a buffer with a different size to
3124 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003125 if (region_is_undefined(&seat->drag_surface->input))
3126 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003127
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003128 if (!dx && !dy)
3129 return;
3130
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003131 weston_surface_set_position(seat->drag_surface,
3132 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3133 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003134}
3135
3136WL_EXPORT void
3137weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3138{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003139 struct weston_seat *seat;
3140
3141 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003142 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003143}
3144
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003145static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003146bind_output(struct wl_client *client,
3147 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003148{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003149 struct weston_output *output = data;
3150 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003151 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003152
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003153 resource = wl_client_add_object(client,
3154 &wl_output_interface, NULL, id, data);
3155
Casey Dahlin9074db52012-04-19 22:50:09 -04003156 wl_list_insert(&output->resource_list, &resource->link);
3157 resource->destroy = unbind_resource;
3158
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003159 wl_output_send_geometry(resource,
3160 output->x,
3161 output->y,
3162 output->mm_width,
3163 output->mm_height,
3164 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003165 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003166 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003167
3168 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003169 wl_output_send_mode(resource,
3170 mode->flags,
3171 mode->width,
3172 mode->height,
3173 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003174 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003175}
3176
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003177static const char vertex_shader[] =
3178 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003179 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003180 "attribute vec2 texcoord;\n"
3181 "varying vec2 v_texcoord;\n"
3182 "void main()\n"
3183 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003184 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003185 " v_texcoord = texcoord;\n"
3186 "}\n";
3187
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003188/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003189#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003190 " y *= alpha;\n" \
3191 " u *= alpha;\n" \
3192 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003193 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3194 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3195 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003196 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003197
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003198static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003199 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003200 "varying vec2 v_texcoord;\n"
3201 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003202 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003203 "void main()\n"
3204 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003205 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003206 "}\n";
3207
Rob Clarke3b95912012-08-31 16:42:18 -05003208static const char texture_fragment_shader_egl_external[] =
3209 "#extension GL_OES_EGL_image_external : require\n"
3210 "precision mediump float;\n"
3211 "varying vec2 v_texcoord;\n"
3212 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003213 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003214 "void main()\n"
3215 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003216 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003217 "}\n";
3218
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003219static const char texture_fragment_shader_y_uv[] =
3220 "precision mediump float;\n"
3221 "uniform sampler2D tex;\n"
3222 "uniform sampler2D tex1;\n"
3223 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003224 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003225 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003226 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3227 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3228 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3229 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003230 "}\n";
3231
3232static const char texture_fragment_shader_y_u_v[] =
3233 "precision mediump float;\n"
3234 "uniform sampler2D tex;\n"
3235 "uniform sampler2D tex1;\n"
3236 "uniform sampler2D tex2;\n"
3237 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003238 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003239 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003240 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3241 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3242 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3243 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003244 "}\n";
3245
3246static const char texture_fragment_shader_y_xuxv[] =
3247 "precision mediump float;\n"
3248 "uniform sampler2D tex;\n"
3249 "uniform sampler2D tex1;\n"
3250 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003251 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003252 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003253 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3254 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3255 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3256 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003257 "}\n";
3258
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003259static const char solid_fragment_shader[] =
3260 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003261 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003262 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003263 "void main()\n"
3264 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003265 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003266 "}\n";
3267
Kristian Høgsberga9468212010-06-14 21:03:11 -04003268static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003269compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003270{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003271 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003272 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003273 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003274
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003275 s = glCreateShader(type);
3276 glShaderSource(s, 1, &source, NULL);
3277 glCompileShader(s);
3278 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003279 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003280 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003281 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003282 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003283 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003284
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003285 return s;
3286}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003287
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003288static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003289weston_shader_init(struct weston_shader *shader,
3290 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003291{
3292 char msg[512];
3293 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003294
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003295 shader->vertex_shader =
3296 compile_shader(GL_VERTEX_SHADER, vertex_source);
3297 shader->fragment_shader =
3298 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3299
3300 shader->program = glCreateProgram();
3301 glAttachShader(shader->program, shader->vertex_shader);
3302 glAttachShader(shader->program, shader->fragment_shader);
3303 glBindAttribLocation(shader->program, 0, "position");
3304 glBindAttribLocation(shader->program, 1, "texcoord");
3305
3306 glLinkProgram(shader->program);
3307 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003308 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003309 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003310 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003311 return -1;
3312 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003313
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003314 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003315 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3316 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3317 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003318 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003319 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003320
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003321 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003322}
3323
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003324WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003325weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003326{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003327 struct weston_compositor *c = output->compositor;
3328
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003329 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003330 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003331 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003332
3333 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003334}
3335
Scott Moreau1bad5db2012-08-18 01:04:05 -06003336static void
3337weston_output_compute_transform(struct weston_output *output)
3338{
3339 struct weston_matrix transform;
3340 int flip;
3341
3342 weston_matrix_init(&transform);
3343
3344 switch(output->transform) {
3345 case WL_OUTPUT_TRANSFORM_FLIPPED:
3346 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3347 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3348 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3349 flip = -1;
3350 break;
3351 default:
3352 flip = 1;
3353 break;
3354 }
3355
3356 switch(output->transform) {
3357 case WL_OUTPUT_TRANSFORM_NORMAL:
3358 case WL_OUTPUT_TRANSFORM_FLIPPED:
3359 transform.d[0] = flip;
3360 transform.d[1] = 0;
3361 transform.d[4] = 0;
3362 transform.d[5] = 1;
3363 break;
3364 case WL_OUTPUT_TRANSFORM_90:
3365 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3366 transform.d[0] = 0;
3367 transform.d[1] = -flip;
3368 transform.d[4] = 1;
3369 transform.d[5] = 0;
3370 break;
3371 case WL_OUTPUT_TRANSFORM_180:
3372 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3373 transform.d[0] = -flip;
3374 transform.d[1] = 0;
3375 transform.d[4] = 0;
3376 transform.d[5] = -1;
3377 break;
3378 case WL_OUTPUT_TRANSFORM_270:
3379 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3380 transform.d[0] = 0;
3381 transform.d[1] = flip;
3382 transform.d[4] = -1;
3383 transform.d[5] = 0;
3384 break;
3385 default:
3386 break;
3387 }
3388
3389 weston_matrix_multiply(&output->matrix, &transform);
3390}
3391
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003392WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003393weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003394{
Scott Moreau850ca422012-05-21 15:21:25 -06003395 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003396 struct weston_matrix camera;
3397 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003398
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003399 weston_matrix_init(&output->matrix);
3400 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003401 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3402 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003403
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003404 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003405 2.0 / (output->width + output->border.left + output->border.right),
3406 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3407
3408 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003409
Scott Moreauccbf29d2012-02-22 14:21:41 -07003410 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003411 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003412 weston_matrix_init(&camera);
3413 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003414 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003415 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003416 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003417 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003418 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003419 weston_matrix_multiply(&output->matrix, &modelview);
3420 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003421
Scott Moreauccbf29d2012-02-22 14:21:41 -07003422 output->dirty = 0;
3423}
3424
Scott Moreau1bad5db2012-08-18 01:04:05 -06003425static void
3426weston_output_transform_init(struct weston_output *output, uint32_t transform)
3427{
3428 output->transform = transform;
3429
3430 switch (transform) {
3431 case WL_OUTPUT_TRANSFORM_90:
3432 case WL_OUTPUT_TRANSFORM_270:
3433 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3434 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3435 /* Swap width and height */
3436 output->width = output->current->height;
3437 output->height = output->current->width;
3438 break;
3439 case WL_OUTPUT_TRANSFORM_NORMAL:
3440 case WL_OUTPUT_TRANSFORM_180:
3441 case WL_OUTPUT_TRANSFORM_FLIPPED:
3442 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3443 output->width = output->current->width;
3444 output->height = output->current->height;
3445 break;
3446 default:
3447 break;
3448 }
3449}
3450
Scott Moreauccbf29d2012-02-22 14:21:41 -07003451WL_EXPORT void
3452weston_output_move(struct weston_output *output, int x, int y)
3453{
3454 output->x = x;
3455 output->y = y;
3456
3457 pixman_region32_init(&output->previous_damage);
3458 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003459 output->width,
3460 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003461}
3462
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003463WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003464weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003465 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003466{
3467 output->compositor = c;
3468 output->x = x;
3469 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003470 output->border.top = 0;
3471 output->border.bottom = 0;
3472 output->border.left = 0;
3473 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003474 output->mm_width = width;
3475 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003476 output->dirty = 1;
3477
Scott Moreau1bad5db2012-08-18 01:04:05 -06003478 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3479 output->disable_planes++;
3480
3481 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003482 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003483
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003484 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003485 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003486
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003487 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003488 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003489 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003490
Casey Dahlin58ba1372012-04-19 22:50:08 -04003491 output->id = ffs(~output->compositor->output_id_pool) - 1;
3492 output->compositor->output_id_pool |= 1 << output->id;
3493
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003494 output->global =
3495 wl_display_add_global(c->wl_display, &wl_output_interface,
3496 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003497}
3498
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003499static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003500compositor_bind(struct wl_client *client,
3501 void *data, uint32_t version, uint32_t id)
3502{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003503 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003504
3505 wl_client_add_object(client, &wl_compositor_interface,
3506 &compositor_interface, id, compositor);
3507}
3508
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003509static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003510log_uname(void)
3511{
3512 struct utsname usys;
3513
3514 uname(&usys);
3515
3516 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3517 usys.version, usys.machine);
3518}
3519
3520static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003521log_extensions(const char *name, const char *extensions)
3522{
3523 const char *p, *end;
3524 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003525 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003526
3527 l = weston_log("%s:", name);
3528 p = extensions;
3529 while (*p) {
3530 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003531 len = end - p;
3532 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003533 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003534 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003535 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003536 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003537 for (p = end; isspace(*p); p++)
3538 ;
3539 }
3540 weston_log_continue("\n");
3541}
3542
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003543static void
3544log_egl_gl_info(EGLDisplay egldpy)
3545{
3546 const char *str;
3547
3548 str = eglQueryString(egldpy, EGL_VERSION);
3549 weston_log("EGL version: %s\n", str ? str : "(null)");
3550
3551 str = eglQueryString(egldpy, EGL_VENDOR);
3552 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3553
3554 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3555 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3556
3557 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3558 log_extensions("EGL extensions", str ? str : "(null)");
3559
3560 str = (char *)glGetString(GL_VERSION);
3561 weston_log("GL version: %s\n", str ? str : "(null)");
3562
3563 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3564 weston_log("GLSL version: %s\n", str ? str : "(null)");
3565
3566 str = (char *)glGetString(GL_VENDOR);
3567 weston_log("GL vendor: %s\n", str ? str : "(null)");
3568
3569 str = (char *)glGetString(GL_RENDERER);
3570 weston_log("GL renderer: %s\n", str ? str : "(null)");
3571
3572 str = (char *)glGetString(GL_EXTENSIONS);
3573 log_extensions("GL extensions", str ? str : "(null)");
3574}
3575
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003576WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003577weston_compositor_init(struct weston_compositor *ec,
3578 struct wl_display *display,
3579 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003580 char *argv[],
3581 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003582{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003583 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003584 struct xkb_rule_names xkb_names;
3585 const struct config_key keyboard_config_keys[] = {
3586 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3587 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3588 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3589 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3590 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3591 };
3592 const struct config_section cs[] = {
3593 { "keyboard",
3594 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3595 };
3596
3597 memset(&xkb_names, 0, sizeof(xkb_names));
3598 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003599
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003600 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003601 wl_signal_init(&ec->destroy_signal);
3602 wl_signal_init(&ec->activate_signal);
3603 wl_signal_init(&ec->lock_signal);
3604 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003605 wl_signal_init(&ec->show_input_panel_signal);
3606 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003607 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003608
Casey Dahlin58ba1372012-04-19 22:50:08 -04003609 ec->output_id_pool = 0;
3610
Kristian Høgsberga8873122011-11-23 10:39:34 -05003611 if (!wl_display_add_global(display, &wl_compositor_interface,
3612 ec, compositor_bind))
3613 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003614
Daniel Stone725c2c32012-06-22 14:04:36 +01003615 wl_list_init(&ec->surface_list);
3616 wl_list_init(&ec->layer_list);
3617 wl_list_init(&ec->seat_list);
3618 wl_list_init(&ec->output_list);
3619 wl_list_init(&ec->key_binding_list);
3620 wl_list_init(&ec->button_binding_list);
3621 wl_list_init(&ec->axis_binding_list);
3622 wl_list_init(&ec->fade.animation.link);
3623
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003624 weston_plane_init(&ec->primary_plane, 0, 0);
3625
Daniel Stone725c2c32012-06-22 14:04:36 +01003626 weston_compositor_xkb_init(ec, &xkb_names);
3627
3628 ec->ping_handler = NULL;
3629
3630 screenshooter_create(ec);
3631 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003632
3633 wl_data_device_manager_init(ec->wl_display);
3634
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003635 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003636
Daniel Stone725c2c32012-06-22 14:04:36 +01003637 loop = wl_display_get_event_loop(ec->wl_display);
3638 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3639 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3640
3641 ec->input_loop = wl_event_loop_create();
3642
3643 return 0;
3644}
3645
3646WL_EXPORT int
3647weston_compositor_init_gl(struct weston_compositor *ec)
3648{
3649 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003650 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003651
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003652 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003653
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003654 ec->image_target_texture_2d =
3655 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3656 ec->image_target_renderbuffer_storage = (void *)
3657 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3658 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3659 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3660 ec->bind_display =
3661 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3662 ec->unbind_display =
3663 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003664 ec->query_buffer =
3665 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003666
3667 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003668 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003669 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003670 return -1;
3671 }
3672
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003673 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003674 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003675 return -1;
3676 }
3677
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003678 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3679 ec->read_format = GL_BGRA_EXT;
3680 else
3681 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003682
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003683 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3684 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003685
Rob Clarke3b95912012-08-31 16:42:18 -05003686 if (strstr(extensions, "GL_OES_EGL_image_external"))
3687 has_egl_image_external = 1;
3688
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003689 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003690 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003691 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003692 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003693 return -1;
3694 }
3695
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003696 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3697 ec->has_bind_display = 1;
3698 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003699 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003700
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003701 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003702 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003703
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003704 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3705 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3706
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003707 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003708
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003709 if (weston_shader_init(&ec->texture_shader_rgba,
3710 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003711 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003712 if (has_egl_image_external &&
3713 weston_shader_init(&ec->texture_shader_egl_external,
3714 vertex_shader, texture_fragment_shader_egl_external) < 0)
3715 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003716 if (weston_shader_init(&ec->texture_shader_y_uv,
3717 vertex_shader, texture_fragment_shader_y_uv) < 0)
3718 return -1;
3719 if (weston_shader_init(&ec->texture_shader_y_u_v,
3720 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3721 return -1;
3722 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3723 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3724 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003725 if (weston_shader_init(&ec->solid_shader,
3726 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003727 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003728
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003729 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003730
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003731 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003732}
3733
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003734WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003735weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003736{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003737 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003738
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003739 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003740 if (ec->input_loop_source)
3741 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003742
Matt Roper361d2ad2011-08-29 13:52:23 -07003743 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003744 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003745 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003746
Daniel Stone325fc2d2012-05-30 16:31:58 +01003747 weston_binding_list_destroy_all(&ec->key_binding_list);
3748 weston_binding_list_destroy_all(&ec->button_binding_list);
3749 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003750
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003751 weston_plane_release(&ec->primary_plane);
3752
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003753 wl_array_release(&ec->vertices);
3754 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003755 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003756
3757 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003758}
3759
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003760static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003761{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003762 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003763
Martin Minarik6d118362012-06-07 18:01:59 +02003764 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003765 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003766
3767 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003768}
3769
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003770static void
3771on_segv_signal(int s, siginfo_t *siginfo, void *context)
3772{
3773 void *buffer[32];
3774 int i, count;
3775 Dl_info info;
3776
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003777 /* This SIGSEGV handler will do a best-effort backtrace, and
3778 * then call the backend restore function, which will switch
3779 * back to the vt we launched from or ungrab X etc and then
3780 * raise SIGTRAP. If we run weston under gdb from X or a
3781 * different vt, and tell gdb "handle SIGSEGV nostop", this
3782 * will allow weston to switch back to gdb on crash and then
3783 * gdb will catch the crash with SIGTRAP. */
3784
Martin Minarik6d118362012-06-07 18:01:59 +02003785 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003786
3787 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3788 for (i = 0; i < count; i++) {
3789 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003790 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003791 (long) buffer[i],
3792 info.dli_sname ? info.dli_sname : "--",
3793 info.dli_fname);
3794 }
3795
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003796 segv_compositor->restore(segv_compositor);
3797
3798 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003799}
3800
3801
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003802static void *
3803load_module(const char *name, const char *entrypoint, void **handle)
3804{
3805 char path[PATH_MAX];
3806 void *module, *init;
3807
3808 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003809 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003810 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003811 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003812
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003813 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003814 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003815 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003816 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003817 return NULL;
3818 }
3819
3820 init = dlsym(module, entrypoint);
3821 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003822 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003823 return NULL;
3824 }
3825
3826 return init;
3827}
3828
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003829static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003830 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3831
3832static const char xdg_wrong_message[] =
3833 "fatal: environment variable XDG_RUNTIME_DIR\n"
3834 "is set to \"%s\", which is not a directory.\n";
3835
3836static const char xdg_wrong_mode_message[] =
3837 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3838 "correctly. Unix access mode must be 0700 but is %o,\n"
3839 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003840 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003841
3842static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003843 "Refer to your distribution on how to get it, or\n"
3844 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3845 "on how to implement it.\n";
3846
Martin Minarik37032f82012-06-18 20:15:18 +02003847static void
3848verify_xdg_runtime_dir(void)
3849{
3850 char *dir = getenv("XDG_RUNTIME_DIR");
3851 struct stat s;
3852
3853 if (!dir) {
3854 weston_log(xdg_error_message);
3855 weston_log_continue(xdg_detail_message);
3856 exit(EXIT_FAILURE);
3857 }
3858
3859 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3860 weston_log(xdg_wrong_message, dir);
3861 weston_log_continue(xdg_detail_message);
3862 exit(EXIT_FAILURE);
3863 }
3864
3865 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3866 weston_log(xdg_wrong_mode_message,
3867 dir, s.st_mode & 0777, s.st_uid);
3868 weston_log_continue(xdg_detail_message);
3869 }
3870}
3871
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003872static int
3873usage(int error_code)
3874{
3875 fprintf(stderr,
3876 "Usage: weston [OPTIONS]\n\n"
3877 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3878 "Weston supports multiple backends, and depending on which backend is in use\n"
3879 "different options will be accepted.\n\n"
3880
3881
3882 "Core options:\n\n"
3883 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3884 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3885 " -S, --socket=NAME\tName of socket to listen on\n"
3886 " -i, --idle-time=SECS\tIdle time in seconds\n"
3887 " --xserver\t\tEnable X server integration\n"
3888 " --module\t\tLoad the specified module\n"
3889 " --log==FILE\t\tLog to the given file\n"
3890 " -h, --help\t\tThis help message\n\n");
3891
3892 fprintf(stderr,
3893 "Options for drm-backend.so:\n\n"
3894 " --connector=ID\tBring up only this connector\n"
3895 " --seat=SEAT\t\tThe seat that weston should run on\n"
3896 " --tty=TTY\t\tThe tty to use\n"
3897 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3898
3899 fprintf(stderr,
3900 "Options for x11-backend.so:\n\n"
3901 " --width=WIDTH\t\tWidth of X window\n"
3902 " --height=HEIGHT\tHeight of X window\n"
3903 " --fullscreen\t\tRun in fullscreen mode\n"
3904 " --output-count=COUNT\tCreate multiple outputs\n"
3905 " --no-input\t\tDont create input devices\n\n");
3906
3907 fprintf(stderr,
3908 "Options for wayland-backend.so:\n\n"
3909 " --width=WIDTH\t\tWidth of Wayland surface\n"
3910 " --height=HEIGHT\tHeight of Wayland surface\n"
3911 " --display=DISPLAY\tWayland display to connect to\n\n");
3912
3913 exit(error_code);
3914}
3915
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003916int main(int argc, char *argv[])
3917{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003918 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003919 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003920 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003921 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003922 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003923 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003924 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003925 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003926 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003927 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003928 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003929 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003930 char *backend = NULL;
3931 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003932 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02003933 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003934 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06003935 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003936 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003937 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003938 char *config_file;
3939
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003940 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003941 { "type", CONFIG_KEY_STRING, &shell },
3942 };
3943
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04003944 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02003945 { "shell",
3946 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
3947 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003948
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003949 const struct weston_option core_options[] = {
3950 { WESTON_OPTION_STRING, "backend", 'B', &backend },
3951 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
3952 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003953 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003954 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02003955 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003956 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04003957 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05003958
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04003959 argc = parse_options(core_options,
3960 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003961
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003962 if (help)
3963 usage(EXIT_SUCCESS);
3964
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02003965 weston_log_file_open(log);
3966
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03003967 weston_log("%s\n"
3968 STAMP_SPACE "%s\n"
3969 STAMP_SPACE "Bug reports to: %s\n"
3970 STAMP_SPACE "Build: %s\n",
3971 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04003972 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03003973 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04003974
Martin Minarik37032f82012-06-18 20:15:18 +02003975 verify_xdg_runtime_dir();
3976
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003977 display = wl_display_create();
3978
Tiago Vignatti2116b892011-08-08 05:52:59 -07003979 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003980 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
3981 display);
3982 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
3983 display);
3984 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
3985 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07003986
3987 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003988 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
3989 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05003990
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003991 if (!backend) {
3992 if (getenv("WAYLAND_DISPLAY"))
3993 backend = "wayland-backend.so";
3994 else if (getenv("DISPLAY"))
3995 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003996 else
3997 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04003998 }
3999
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004000 config_file = config_file_path("weston.ini");
4001 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004002
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004003 backend_init = load_module(backend, "backend_init", &backend_module);
4004 if (!backend_init)
4005 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004006
Daniel Stonec1be8e52012-06-01 11:14:02 -04004007 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004008 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004009 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004010 exit(EXIT_FAILURE);
4011 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004012
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004013 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4014 segv_action.sa_sigaction = on_segv_signal;
4015 sigemptyset(&segv_action.sa_mask);
4016 sigaction(SIGSEGV, &segv_action, NULL);
4017 segv_compositor = ec;
4018
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004019 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004020 weston_log("fatal: unhandled option: %s\n", argv[i]);
4021 if (argv[1]) {
4022 ret = EXIT_FAILURE;
4023 goto out;
4024 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004025
Daniel Stonec1be8e52012-06-01 11:14:02 -04004026 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004027
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004028 ec->option_idle_time = idle_time;
4029 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004030
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004031 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004032 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004033 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004034 "weston_xserver_init",
4035 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004036 if (module_init && module_init(ec) < 0) {
4037 ret = EXIT_FAILURE;
4038 goto out;
4039 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004040
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004041 if (socket_name)
4042 setenv("WAYLAND_DISPLAY", socket_name, 1);
4043
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004044 if (!shell)
4045 shell = "desktop-shell.so";
4046 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004047 if (!module_init || module_init(ec) < 0) {
4048 ret = EXIT_FAILURE;
4049 goto out;
4050 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004051
4052
4053 module_init = NULL;
4054 if (module)
4055 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004056 if (module_init && module_init(ec) < 0) {
4057 ret = EXIT_FAILURE;
4058 goto out;
4059 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004060
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004061 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004062 weston_log("fatal: failed to add socket: %m\n");
4063 ret = EXIT_FAILURE;
4064 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004065 }
4066
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004067 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004068 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004069
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004070 wl_display_run(display);
4071
4072 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004073 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004074 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004075
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004076 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004077
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004078 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004079 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004080
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004081 for (i = ARRAY_LENGTH(signals); i;)
4082 wl_event_source_remove(signals[--i]);
4083
Daniel Stone855028f2012-05-01 20:37:10 +01004084 weston_compositor_xkb_destroy(ec);
4085
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004086 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004087 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004088
Martin Minarik19e6f262012-06-07 13:08:46 +02004089 weston_log_file_close();
4090
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004091 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004092}