blob: 2b963f5fbebf2b00b3d3aac66f6e658d798e6d59 [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;
Juan Zhao46436612012-03-20 01:48:46 +0800243 surface->pitch = 1;
Kristian Høgsberg27803c62010-06-06 22:23:21 -0400244
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200245 surface->num_textures = 0;
246 surface->num_images = 0;
247
Benjamin Franzke06286262011-05-06 19:12:33 +0200248 surface->buffer = NULL;
Kristian Høgsberg6f7179c2011-08-29 16:09:32 -0400249 surface->output = NULL;
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400250 surface->plane = &compositor->primary_plane;
Benjamin Franzke06286262011-05-06 19:12:33 +0200251
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400252 pixman_region32_init(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500253 pixman_region32_init(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500254 pixman_region32_init(&surface->clip);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500255 undef_region(&surface->input);
Pekka Paalanen730d87e2012-02-09 16:39:38 +0200256 pixman_region32_init(&surface->transform.opaque);
Kristian Høgsberg496433b2011-11-15 13:50:21 -0500257 wl_list_init(&surface->frame_callback_list);
Kristian Høgsberg20300ba2011-06-23 20:29:12 -0400258
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400259 surface->buffer_destroy_listener.notify =
260 surface_handle_buffer_destroy;
Benjamin Franzke06286262011-05-06 19:12:33 +0200261
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200262 wl_list_init(&surface->geometry.transformation_list);
Pekka Paalanencd403622012-01-25 13:37:39 +0200263 wl_list_insert(&surface->geometry.transformation_list,
264 &surface->transform.position.link);
265 weston_matrix_init(&surface->transform.position.matrix);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200266 pixman_region32_init(&surface->transform.boundingbox);
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200267 surface->geometry.dirty = 1;
Kristian Høgsberg1e4b86a2008-11-23 23:41:08 -0500268
Kristian Høgsberg77fb1672010-08-16 10:38:29 -0400269 return surface;
Kristian Høgsberg54879822008-11-23 17:07:32 -0500270}
271
Alex Wu8811bf92012-02-28 18:07:54 +0800272WL_EXPORT void
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500273weston_surface_set_color(struct weston_surface *surface,
274 GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha)
275{
Kristian Høgsbergbbeefb02012-01-26 10:00:23 -0500276 surface->color[0] = red;
277 surface->color[1] = green;
278 surface->color[2] = blue;
279 surface->color[3] = alpha;
280 surface->shader = &surface->compositor->solid_shader;
281}
282
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400283WL_EXPORT void
284weston_surface_to_global_float(struct weston_surface *surface,
285 GLfloat sx, GLfloat sy, GLfloat *x, GLfloat *y)
Pekka Paalanenece8a012012-02-08 15:23:15 +0200286{
287 if (surface->transform.enabled) {
288 struct weston_vector v = { { sx, sy, 0.0f, 1.0f } };
289
290 weston_matrix_transform(&surface->transform.matrix, &v);
291
292 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200293 weston_log("warning: numerical instability in "
Pekka Paalanenece8a012012-02-08 15:23:15 +0200294 "weston_surface_to_global(), divisor = %g\n",
295 v.f[3]);
296 *x = 0;
297 *y = 0;
298 return;
299 }
300
301 *x = v.f[0] / v.f[3];
302 *y = v.f[1] / v.f[3];
303 } else {
304 *x = sx + surface->geometry.x;
305 *y = sy + surface->geometry.y;
306 }
307}
308
Kristian Høgsbergd8bf90c2012-02-23 23:03:14 -0500309WL_EXPORT void
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400310weston_surface_move_to_plane(struct weston_surface *surface,
311 struct weston_plane *plane)
312{
313 if (surface->plane == plane)
314 return;
315
316 weston_surface_damage_below(surface);
317 surface->plane = plane;
318 weston_surface_damage(surface);
319}
320
321WL_EXPORT void
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500322weston_surface_damage_below(struct weston_surface *surface)
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200323{
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500324 pixman_region32_t damage;
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200325
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500326 pixman_region32_init(&damage);
327 pixman_region32_subtract(&damage, &surface->transform.boundingbox,
328 &surface->clip);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -0400329 pixman_region32_union(&surface->plane->damage,
330 &surface->plane->damage, &damage);
Kristian Høgsberg1e832122012-02-28 22:47:14 -0500331 pixman_region32_fini(&damage);
Pekka Paalanen9abf3932012-02-08 14:49:37 +0200332}
333
334static void
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200335surface_compute_bbox(struct weston_surface *surface, int32_t sx, int32_t sy,
336 int32_t width, int32_t height,
337 pixman_region32_t *bbox)
338{
Pekka Paalanen219b9822012-02-08 15:38:37 +0200339 GLfloat min_x = HUGE_VALF, min_y = HUGE_VALF;
340 GLfloat max_x = -HUGE_VALF, max_y = -HUGE_VALF;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200341 int32_t s[4][2] = {
342 { sx, sy },
343 { sx, sy + height },
344 { sx + width, sy },
345 { sx + width, sy + height }
346 };
Pekka Paalanen219b9822012-02-08 15:38:37 +0200347 GLfloat int_x, int_y;
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200348 int i;
349
Pekka Paalanen7c7d4642012-09-04 13:55:44 +0300350 if (width == 0 || height == 0) {
351 /* avoid rounding empty bbox to 1x1 */
352 pixman_region32_init(bbox);
353 return;
354 }
355
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200356 for (i = 0; i < 4; ++i) {
Pekka Paalanen219b9822012-02-08 15:38:37 +0200357 GLfloat x, y;
Kristian Høgsberge4c1a5f2012-06-18 13:17:32 -0400358 weston_surface_to_global_float(surface,
359 s[i][0], s[i][1], &x, &y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200360 if (x < min_x)
361 min_x = x;
362 if (x > max_x)
363 max_x = x;
364 if (y < min_y)
365 min_y = y;
366 if (y > max_y)
367 max_y = y;
368 }
369
Pekka Paalanen219b9822012-02-08 15:38:37 +0200370 int_x = floorf(min_x);
371 int_y = floorf(min_y);
372 pixman_region32_init_rect(bbox, int_x, int_y,
373 ceilf(max_x) - int_x, ceilf(max_y) - int_y);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200374}
375
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200376static void
377weston_surface_update_transform_disable(struct weston_surface *surface)
378{
379 surface->transform.enabled = 0;
380
Pekka Paalanencc2f8682012-02-13 10:34:04 +0200381 /* round off fractions when not transformed */
382 surface->geometry.x = roundf(surface->geometry.x);
383 surface->geometry.y = roundf(surface->geometry.y);
384
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200385 pixman_region32_init_rect(&surface->transform.boundingbox,
386 surface->geometry.x,
387 surface->geometry.y,
388 surface->geometry.width,
389 surface->geometry.height);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500390
Kristian Høgsberga416fa12012-05-21 14:06:52 -0400391 if (surface->alpha == 1.0) {
Kristian Høgsberg3b4af202012-02-28 09:19:39 -0500392 pixman_region32_copy(&surface->transform.opaque,
393 &surface->opaque);
394 pixman_region32_translate(&surface->transform.opaque,
395 surface->geometry.x,
396 surface->geometry.y);
397 }
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200398}
399
400static int
401weston_surface_update_transform_enable(struct weston_surface *surface)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200402{
403 struct weston_matrix *matrix = &surface->transform.matrix;
404 struct weston_matrix *inverse = &surface->transform.inverse;
405 struct weston_transform *tform;
406
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200407 surface->transform.enabled = 1;
408
409 /* Otherwise identity matrix, but with x and y translation. */
410 surface->transform.position.matrix.d[12] = surface->geometry.x;
411 surface->transform.position.matrix.d[13] = surface->geometry.y;
412
413 weston_matrix_init(matrix);
414 wl_list_for_each(tform, &surface->geometry.transformation_list, link)
415 weston_matrix_multiply(matrix, &tform->matrix);
416
417 if (weston_matrix_invert(inverse, matrix) < 0) {
418 /* Oops, bad total transformation, not invertible */
Martin Minarik6d118362012-06-07 18:01:59 +0200419 weston_log("error: weston_surface %p"
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200420 " transformation not invertible.\n", surface);
421 return -1;
422 }
423
424 surface_compute_bbox(surface, 0, 0, surface->geometry.width,
425 surface->geometry.height,
426 &surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500427
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200428 return 0;
429}
430
431WL_EXPORT void
432weston_surface_update_transform(struct weston_surface *surface)
433{
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200434 if (!surface->geometry.dirty)
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200435 return;
436
Pekka Paalanenbc0b7e72012-01-24 09:53:37 +0200437 surface->geometry.dirty = 0;
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200438
Kristian Høgsberg323ee042012-02-17 12:45:43 -0500439 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200440
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200441 pixman_region32_fini(&surface->transform.boundingbox);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500442 pixman_region32_fini(&surface->transform.opaque);
443 pixman_region32_init(&surface->transform.opaque);
444
445 if (region_is_undefined(&surface->input))
446 pixman_region32_init_rect(&surface->input, 0, 0,
447 surface->geometry.width,
448 surface->geometry.height);
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200449
Pekka Paalanencd403622012-01-25 13:37:39 +0200450 /* transform.position is always in transformation_list */
451 if (surface->geometry.transformation_list.next ==
452 &surface->transform.position.link &&
453 surface->geometry.transformation_list.prev ==
454 &surface->transform.position.link) {
Pekka Paalanen80fb08d2012-02-08 15:14:17 +0200455 weston_surface_update_transform_disable(surface);
456 } else {
457 if (weston_surface_update_transform_enable(surface) < 0)
458 weston_surface_update_transform_disable(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200459 }
Pekka Paalanen96516782012-02-09 15:32:15 +0200460
Kristian Høgsbergf1ea63f2012-07-18 12:02:51 -0400461 weston_surface_damage_below(surface);
Pekka Paalanen96516782012-02-09 15:32:15 +0200462
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300463 if (weston_surface_is_mapped(surface))
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +0200464 weston_surface_assign_output(surface);
Pekka Paalanen2a5cecc2012-01-20 14:24:25 +0200465}
466
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200467WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100468weston_surface_to_global_fixed(struct weston_surface *surface,
469 wl_fixed_t sx, wl_fixed_t sy,
470 wl_fixed_t *x, wl_fixed_t *y)
471{
472 GLfloat xf, yf;
473
474 weston_surface_to_global_float(surface,
475 wl_fixed_to_double(sx),
476 wl_fixed_to_double(sy),
477 &xf, &yf);
478 *x = wl_fixed_from_double(xf);
479 *y = wl_fixed_from_double(yf);
480}
481
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200482static void
483surface_from_global_float(struct weston_surface *surface,
Daniel Stonebd3489b2012-05-08 17:17:53 +0100484 GLfloat x, GLfloat y, GLfloat *sx, GLfloat *sy)
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200485{
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200486 if (surface->transform.enabled) {
487 struct weston_vector v = { { x, y, 0.0f, 1.0f } };
488
489 weston_matrix_transform(&surface->transform.inverse, &v);
490
491 if (fabsf(v.f[3]) < 1e-6) {
Martin Minarik6d118362012-06-07 18:01:59 +0200492 weston_log("warning: numerical instability in "
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200493 "weston_surface_from_global(), divisor = %g\n",
494 v.f[3]);
495 *sx = 0;
496 *sy = 0;
497 return;
498 }
499
Pekka Paalanencd403622012-01-25 13:37:39 +0200500 *sx = v.f[0] / v.f[3];
501 *sy = v.f[1] / v.f[3];
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200502 } else {
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200503 *sx = x - surface->geometry.x;
504 *sy = y - surface->geometry.y;
Pekka Paalanene0f3cb22012-01-24 09:59:29 +0200505 }
506}
507
508WL_EXPORT void
Daniel Stonebd3489b2012-05-08 17:17:53 +0100509weston_surface_from_global_fixed(struct weston_surface *surface,
510 wl_fixed_t x, wl_fixed_t y,
511 wl_fixed_t *sx, wl_fixed_t *sy)
512{
513 GLfloat sxf, syf;
514
Daniel Stonebd3489b2012-05-08 17:17:53 +0100515 surface_from_global_float(surface,
516 wl_fixed_to_double(x),
517 wl_fixed_to_double(y),
518 &sxf, &syf);
519 *sx = wl_fixed_from_double(sxf);
520 *sy = wl_fixed_from_double(syf);
521}
522
523WL_EXPORT void
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200524weston_surface_from_global(struct weston_surface *surface,
525 int32_t x, int32_t y, int32_t *sx, int32_t *sy)
526{
527 GLfloat sxf, syf;
528
Pekka Paalanen0e151bb2012-01-24 14:47:37 +0200529 surface_from_global_float(surface, x, y, &sxf, &syf);
530 *sx = floorf(sxf);
531 *sy = floorf(syf);
532}
533
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400534WL_EXPORT void
Kristian Høgsberg98238702012-08-03 16:29:12 -0400535weston_surface_schedule_repaint(struct weston_surface *surface)
536{
537 struct weston_output *output;
538
539 wl_list_for_each(output, &surface->compositor->output_list, link)
540 if (surface->output_mask & (1 << output->id))
541 weston_output_schedule_repaint(output);
542}
543
544WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500545weston_surface_damage(struct weston_surface *surface)
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500546{
Kristian Høgsberg460a79b2012-06-18 15:09:11 -0400547 pixman_region32_union_rect(&surface->damage, &surface->damage,
548 0, 0, surface->geometry.width,
549 surface->geometry.height);
Pekka Paalanen2267d452012-01-26 13:12:45 +0200550
Kristian Høgsberg98238702012-08-03 16:29:12 -0400551 weston_surface_schedule_repaint(surface);
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -0500552}
553
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400554WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500555weston_surface_configure(struct weston_surface *surface,
Pekka Paalanenddae03c2012-02-06 14:54:20 +0200556 GLfloat x, GLfloat y, int width, int height)
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400557{
Pekka Paalanenba3cf952012-01-25 16:22:05 +0200558 surface->geometry.x = x;
559 surface->geometry.y = y;
Pekka Paalanen60921e52012-01-25 15:55:43 +0200560 surface->geometry.width = width;
561 surface->geometry.height = height;
Pekka Paalanencd403622012-01-25 13:37:39 +0200562 surface->geometry.dirty = 1;
Kristian Høgsberga691aee2011-06-23 21:43:50 -0400563}
564
Pekka Paalanen8fb8d3b2012-02-13 13:03:59 +0200565WL_EXPORT void
566weston_surface_set_position(struct weston_surface *surface,
567 GLfloat x, GLfloat y)
568{
569 surface->geometry.x = x;
570 surface->geometry.y = y;
571 surface->geometry.dirty = 1;
572}
573
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300574WL_EXPORT int
575weston_surface_is_mapped(struct weston_surface *surface)
576{
577 if (surface->output)
578 return 1;
579 else
580 return 0;
581}
582
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -0400583WL_EXPORT uint32_t
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500584weston_compositor_get_time(void)
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500585{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400586 struct timeval tv;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500587
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400588 gettimeofday(&tv, NULL);
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500589
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400590 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
Kristian Høgsberg7132a9a2010-12-06 21:41:10 -0500591}
592
Tiago Vignatti9d393522012-02-10 16:26:19 +0200593static struct weston_surface *
594weston_compositor_pick_surface(struct weston_compositor *compositor,
Daniel Stone103db7f2012-05-08 17:17:55 +0100595 wl_fixed_t x, wl_fixed_t y,
596 wl_fixed_t *sx, wl_fixed_t *sy)
Tiago Vignatti9d393522012-02-10 16:26:19 +0200597{
598 struct weston_surface *surface;
599
600 wl_list_for_each(surface, &compositor->surface_list, link) {
Daniel Stone103db7f2012-05-08 17:17:55 +0100601 weston_surface_from_global_fixed(surface, x, y, sx, sy);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500602 if (pixman_region32_contains_point(&surface->input,
Daniel Stone103db7f2012-05-08 17:17:55 +0100603 wl_fixed_to_int(*sx),
604 wl_fixed_to_int(*sy),
605 NULL))
Tiago Vignatti9d393522012-02-10 16:26:19 +0200606 return surface;
607 }
608
609 return NULL;
610}
611
612static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400613weston_device_repick(struct weston_seat *seat)
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500614{
Scott Moreau447013d2012-02-18 05:05:29 -0700615 const struct wl_pointer_grab_interface *interface;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500616 struct weston_surface *surface, *focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400617 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500618
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400619 if (!pointer)
Daniel Stonee9e92d22012-06-04 11:40:47 +0100620 return;
621
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400622 surface = weston_compositor_pick_surface(seat->compositor,
623 pointer->x,
624 pointer->y,
625 &pointer->current_x,
626 &pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500627
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400628 if (&surface->surface != pointer->current) {
629 interface = pointer->grab->interface;
630 pointer->current = &surface->surface;
631 interface->focus(pointer->grab, &surface->surface,
632 pointer->current_x,
633 pointer->current_y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500634 }
635
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400636 focus = (struct weston_surface *) pointer->grab->focus;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500637 if (focus)
Daniel Stone37816df2012-05-16 18:45:18 +0100638 weston_surface_from_global_fixed(focus,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400639 pointer->x,
640 pointer->y,
641 &pointer->grab->x,
642 &pointer->grab->y);
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -0500643}
644
Kristian Høgsberg4ff5a742012-06-18 16:48:27 -0400645static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500646weston_compositor_repick(struct weston_compositor *compositor)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400647{
Daniel Stone37816df2012-05-16 18:45:18 +0100648 struct weston_seat *seat;
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400649
Kristian Høgsberg1ec0c312011-11-15 16:39:55 -0500650 if (!compositor->focus)
651 return;
652
Daniel Stone37816df2012-05-16 18:45:18 +0100653 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -0400654 weston_device_repick(seat);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400655}
656
Kristian Høgsbergaf7b1ff2012-06-26 21:19:23 -0400657WL_EXPORT void
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500658weston_surface_unmap(struct weston_surface *surface)
659{
Daniel Stone4dab5db2012-05-30 16:31:53 +0100660 struct weston_seat *seat;
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500661
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500662 weston_surface_damage_below(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500663 surface->output = NULL;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -0500664 wl_list_remove(&surface->layer_link);
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500665
Daniel Stone4dab5db2012-05-30 16:31:53 +0100666 wl_list_for_each(seat, &surface->compositor->seat_list, link) {
Daniel Stonee9e92d22012-06-04 11:40:47 +0100667 if (seat->seat.keyboard &&
668 seat->seat.keyboard->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100669 wl_keyboard_set_focus(seat->seat.keyboard, NULL);
Daniel Stonee9e92d22012-06-04 11:40:47 +0100670 if (seat->seat.pointer &&
671 seat->seat.pointer->focus == &surface->surface)
Daniel Stone4dab5db2012-05-30 16:31:53 +0100672 wl_pointer_set_focus(seat->seat.pointer,
673 NULL,
674 wl_fixed_from_int(0),
675 wl_fixed_from_int(0));
676 }
Kristian Høgsberg867dec72012-03-01 17:09:37 -0500677
Kristian Høgsberg98238702012-08-03 16:29:12 -0400678 weston_surface_schedule_repaint(surface);
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500679}
680
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400681struct weston_frame_callback {
682 struct wl_resource resource;
683 struct wl_list link;
684};
685
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500686static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -0400687destroy_surface(struct wl_resource *resource)
Kristian Høgsberg54879822008-11-23 17:07:32 -0500688{
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200689 int i;
690
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500691 struct weston_surface *surface =
692 container_of(resource,
693 struct weston_surface, surface.resource);
694 struct weston_compositor *compositor = surface->compositor;
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400695 struct weston_frame_callback *cb, *next;
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400696
Ander Conselvan de Oliveirab8ab14f2012-03-27 17:36:36 +0300697 if (weston_surface_is_mapped(surface))
Kristian Høgsberg3b5ea3b2012-02-17 12:43:56 -0500698 weston_surface_unmap(surface);
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -0400699
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200700 glDeleteTextures(surface->num_textures, surface->textures);
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200701
Benjamin Franzke06286262011-05-06 19:12:33 +0200702 if (surface->buffer)
703 wl_list_remove(&surface->buffer_destroy_listener.link);
Kristian Høgsberg3f8f39c2009-09-18 17:05:13 -0400704
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200705 for (i = 0; i < surface->num_images; i++)
Kristian Høgsberg362b6722012-06-18 15:13:51 -0400706 compositor->destroy_image(compositor->egl_display,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200707 surface->images[i]);
Benjamin Franzke1178a3c2011-04-10 16:49:52 +0200708
Pekka Paalanen6720d8f2012-01-25 15:17:40 +0200709 pixman_region32_fini(&surface->transform.boundingbox);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200710 pixman_region32_fini(&surface->damage);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500711 pixman_region32_fini(&surface->opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -0500712 pixman_region32_fini(&surface->clip);
Kristian Høgsbergdd631c12012-02-23 16:20:38 -0500713 if (!region_is_undefined(&surface->input))
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -0500714 pixman_region32_fini(&surface->input);
Pekka Paalanen402ae6d2012-01-03 10:23:24 +0200715
Kristian Høgsberg1e51fec2012-07-22 11:33:14 -0400716 wl_list_for_each_safe(cb, next, &surface->frame_callback_list, link)
717 wl_resource_destroy(&cb->resource);
718
Kristian Høgsberg4fa48732009-03-10 23:17:00 -0400719 free(surface);
Kristian Høgsberg54879822008-11-23 17:07:32 -0500720}
721
Alex Wu8811bf92012-02-28 18:07:54 +0800722WL_EXPORT void
723weston_surface_destroy(struct weston_surface *surface)
724{
725 /* Not a valid way to destroy a client surface */
726 assert(surface->surface.resource.client == NULL);
727
Kristian Høgsberg9a050af2012-06-07 18:17:42 -0400728 wl_signal_emit(&surface->surface.resource.destroy_signal,
729 &surface->surface.resource);
Alex Wu8811bf92012-02-28 18:07:54 +0800730 destroy_surface(&surface->surface.resource);
731}
732
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100733static void
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200734ensure_textures(struct weston_surface *es, int num_textures)
735{
736 int i;
737
738 if (num_textures <= es->num_textures)
739 return;
740
741 for (i = es->num_textures; i < num_textures; i++) {
742 glGenTextures(1, &es->textures[i]);
Rob Clarke3b95912012-08-31 16:42:18 -0500743 glBindTexture(es->target, es->textures[i]);
744 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200745 GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
Rob Clarke3b95912012-08-31 16:42:18 -0500746 glTexParameteri(es->target,
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200747 GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
748 }
749 es->num_textures = num_textures;
Rob Clarke3b95912012-08-31 16:42:18 -0500750 glBindTexture(es->target, 0);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200751}
752
753static void
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -0400754weston_surface_attach(struct wl_surface *surface, struct wl_buffer *buffer)
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100755{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500756 struct weston_surface *es = (struct weston_surface *) surface;
757 struct weston_compositor *ec = es->compositor;
Kristian Høgsberg905d7ce2012-07-19 14:20:20 -0400758 EGLint attribs[3], format;
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +0200759 int i, num_planes;
Benjamin Franzkefaa0a9d2011-02-21 16:24:53 +0100760
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300761 if (es->buffer) {
762 weston_buffer_post_release(es->buffer);
763 wl_list_remove(&es->buffer_destroy_listener.link);
764 }
765
766 es->buffer = buffer;
767
768 if (!buffer) {
769 if (weston_surface_is_mapped(es))
770 weston_surface_unmap(es);
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200771 for (i = 0; i < es->num_images; i++) {
772 ec->destroy_image(ec->egl_display, es->images[i]);
773 es->images[i] = NULL;
Kristian Høgsbergb8ceaaa2012-06-19 15:41:12 -0400774 }
Gwenole Beauchesne023f8552012-04-20 11:07:06 +0200775 es->num_images = 0;
776 glDeleteTextures(es->num_textures, es->textures);
777 es->num_textures = 0;
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300778 return;
779 }
780
781 buffer->busy_count++;
Kristian Høgsberg27e30522012-04-11 23:18:23 -0400782 wl_signal_add(&es->buffer->resource.destroy_signal,
783 &es->buffer_destroy_listener);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +0300784
785 if (es->geometry.width != buffer->width ||
786 es->geometry.height != buffer->height) {
787 undef_region(&es->input);
788 pixman_region32_fini(&es->opaque);
789 pixman_region32_init(&es->opaque);
790 }
791
Benjamin Franzke315b3dc2011-03-08 11:32:57 +0100792 if (wl_buffer_is_shm(buffer)) {
Benjamin Franzkefab5ec12011-04-25 19:44:47 +0200793 es->pitch = wl_shm_buffer_get_stride(buffer) / 4;
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)
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500802 es->shader = &ec->texture_shader_rgbx;
Kristian Høgsbergd6ad1222012-05-17 11:11:15 -0400803 else
Pekka Paalanen6b5585b2012-08-30 16:47:19 -0500804 es->shader = &ec->texture_shader_rgba;
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
Pekka Paalanen85a46672012-09-04 13:55:42 +03001166 /* worst case we can have 8 vertices per rect (ie. clipped into
Rob Clark0e5a2d02012-08-30 16:47:18 -05001167 * an octagon):
1168 */
Pekka Paalanen85a46672012-09-04 13:55:42 +03001169 v = wl_array_add(&ec->vertices, nrects * nsurf * 8 * 4 * sizeof *v);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001170 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];
Pekka Paalanen85a46672012-09-04 13:55:42 +03001179 GLfloat sx, sy;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001180 GLfloat ex[8], ey[8]; /* edge points in screen space */
1181 int n;
1182
Rob Clark0e5a2d02012-08-30 16:47:18 -05001183 /* The transformed surface, after clipping to the clip region,
1184 * can have as many as eight sides, emitted as a triangle-fan.
Pekka Paalanen85a46672012-09-04 13:55:42 +03001185 * The first vertex in the triangle fan can be chosen arbitrarily,
1186 * since the area is guaranteed to be convex.
Rob Clark0e5a2d02012-08-30 16:47:18 -05001187 *
1188 * If a corner of the transformed surface falls outside of the
1189 * clip region, instead of emitting one vertex for the corner
1190 * of the surface, up to two are emitted for two corresponding
1191 * intersection point(s) between the surface and the clip region.
1192 *
1193 * To do this, we first calculate the (up to eight) points that
1194 * form the intersection of the clip rect and the transformed
Pekka Paalanen85a46672012-09-04 13:55:42 +03001195 * surface.
Rob Clark0e5a2d02012-08-30 16:47:18 -05001196 */
1197 n = calculate_edges(es, rect, surf_rect, ex, ey);
1198 if (n < 3)
1199 continue;
1200
Pekka Paalanen85a46672012-09-04 13:55:42 +03001201 /* emit edge points: */
Rob Clark0e5a2d02012-08-30 16:47:18 -05001202 for (k = 0; k < n; k++) {
Pekka Paalanen85a46672012-09-04 13:55:42 +03001203 surface_from_global_float(es, ex[k], ey[k], &sx, &sy);
1204 /* position: */
1205 *(v++) = ex[k];
1206 *(v++) = ey[k];
1207 /* texcoord: */
1208 *(v++) = sx * inv_width;
1209 *(v++) = sy * inv_height;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001210 }
Rob Clark0e5a2d02012-08-30 16:47:18 -05001211
Pekka Paalanen85a46672012-09-04 13:55:42 +03001212 vtxcnt[nvtx++] = n;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001213 }
1214 }
1215
1216 return nvtx;
1217}
1218
1219static void
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001220triangle_fan_debug(struct weston_surface *surface, int first, int count)
1221{
1222 struct weston_compositor *compositor = surface->compositor;
1223 int i;
1224 GLushort *buffer;
1225 GLushort *index;
1226 int nelems;
1227 static int color_idx = 0;
1228 static const GLfloat color[][4] = {
1229 { 1.0, 0.0, 0.0, 1.0 },
1230 { 0.0, 1.0, 0.0, 1.0 },
1231 { 0.0, 0.0, 1.0, 1.0 },
1232 { 1.0, 1.0, 1.0, 1.0 },
1233 };
1234
1235 nelems = (count - 1 + count - 2) * 2;
1236
1237 buffer = malloc(sizeof(GLushort) * nelems);
1238 index = buffer;
1239
1240 for (i = 1; i < count; i++) {
1241 *index++ = first;
1242 *index++ = first + i;
1243 }
1244
1245 for (i = 2; i < count; i++) {
1246 *index++ = first + i - 1;
1247 *index++ = first + i;
1248 }
1249
1250 glUseProgram(compositor->solid_shader.program);
1251 glUniform4fv(compositor->solid_shader.color_uniform, 1,
Pekka Paalanen27a8c532012-09-04 13:55:43 +03001252 color[color_idx++ % ARRAY_LENGTH(color)]);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001253 glDrawElements(GL_LINES, nelems, GL_UNSIGNED_SHORT, buffer);
Pekka Paalanenc73bd542012-09-03 16:48:43 +03001254 glUseProgram(compositor->current_shader->program);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001255 free(buffer);
1256}
1257
1258static void
Rob Clark0e5a2d02012-08-30 16:47:18 -05001259repaint_region(struct weston_surface *es, pixman_region32_t *region,
1260 pixman_region32_t *surf_region)
1261{
1262 struct weston_compositor *ec = es->compositor;
1263 GLfloat *v;
1264 unsigned int *vtxcnt;
1265 int i, first, nfans;
1266
1267 /* The final region to be painted is the intersection of
1268 * 'region' and 'surf_region'. However, 'region' is in the global
1269 * coordinates, and 'surf_region' is in the surface-local
Pekka Paalanen85a46672012-09-04 13:55:42 +03001270 * coordinates. texture_region() will iterate over all pairs of
Rob Clark0e5a2d02012-08-30 16:47:18 -05001271 * rectangles from both regions, compute the intersection
1272 * polygon for each pair, and store it as a triangle fan if
Pekka Paalanen85a46672012-09-04 13:55:42 +03001273 * it has a non-zero area (at least 3 vertices, actually).
Rob Clark0e5a2d02012-08-30 16:47:18 -05001274 */
1275 nfans = texture_region(es, region, surf_region);
1276
1277 v = ec->vertices.data;
1278 vtxcnt = ec->vtxcnt.data;
1279
1280 /* position: */
1281 glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[0]);
1282 glEnableVertexAttribArray(0);
1283
1284 /* texcoord: */
1285 glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof *v, &v[2]);
1286 glEnableVertexAttribArray(1);
1287
1288 for (i = 0, first = 0; i < nfans; i++) {
1289 glDrawArrays(GL_TRIANGLE_FAN, first, vtxcnt[i]);
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001290 if (ec->fan_debug)
1291 triangle_fan_debug(es, first, vtxcnt[i]);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001292 first += vtxcnt[i];
1293 }
1294
1295 glDisableVertexAttribArray(1);
1296 glDisableVertexAttribArray(0);
1297
1298 ec->vertices.size = 0;
1299 ec->vtxcnt.size = 0;
1300}
1301
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001302static void
1303weston_compositor_use_shader(struct weston_compositor *compositor,
1304 struct weston_shader *shader)
1305{
1306 if (compositor->current_shader == shader)
1307 return;
1308
1309 glUseProgram(shader->program);
1310 compositor->current_shader = shader;
1311}
1312
1313static void
1314weston_shader_uniforms(struct weston_shader *shader,
1315 struct weston_surface *surface,
1316 struct weston_output *output)
1317{
1318 int i;
1319
1320 glUniformMatrix4fv(shader->proj_uniform,
1321 1, GL_FALSE, output->matrix.d);
1322 glUniform4fv(shader->color_uniform, 1, surface->color);
1323 glUniform1f(shader->alpha_uniform, surface->alpha);
1324
1325 for (i = 0; i < surface->num_textures; i++)
1326 glUniform1i(shader->tex_uniforms[i], i);
1327}
Rob Clark0e5a2d02012-08-30 16:47:18 -05001328
Kristian Høgsberg68c479a2012-01-25 23:32:28 -05001329WL_EXPORT void
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001330weston_surface_draw(struct weston_surface *es, struct weston_output *output,
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001331 pixman_region32_t *damage) /* in global coordinates */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001332{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001333 struct weston_compositor *ec = es->compositor;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001334 /* repaint bounding region in global coordinates: */
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001335 pixman_region32_t repaint;
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001336 /* non-opaque region in surface coordinates: */
1337 pixman_region32_t surface_blend;
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001338 GLint filter;
Rob Clark0e5a2d02012-08-30 16:47:18 -05001339 int i;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001340
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001341 pixman_region32_init(&repaint);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001342 pixman_region32_intersect(&repaint,
1343 &es->transform.boundingbox, damage);
1344 pixman_region32_subtract(&repaint, &repaint, &es->clip);
Pekka Paalanenc7b45f62012-02-06 12:16:07 +02001345
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001346 if (!pixman_region32_not_empty(&repaint))
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001347 goto out;
Kristian Høgsberg2e94d112011-05-02 13:47:51 -04001348
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001349 pixman_region32_subtract(&ec->primary_plane.damage,
1350 &ec->primary_plane.damage, &repaint);
1351
Kristian Høgsberg101cb652012-02-17 10:45:16 -05001352 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001353
Pekka Paalanenc73bd542012-09-03 16:48:43 +03001354 if (ec->fan_debug) {
1355 weston_compositor_use_shader(ec, &ec->solid_shader);
1356 weston_shader_uniforms(&ec->solid_shader, es, output);
1357 }
1358
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001359 weston_compositor_use_shader(ec, es->shader);
1360 weston_shader_uniforms(es->shader, es, output);
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001361
Scott Moreauccbf29d2012-02-22 14:21:41 -07001362 if (es->transform.enabled || output->zoom.active)
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001363 filter = GL_LINEAR;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001364 else
Pekka Paalanenc61eca62012-01-06 14:10:06 +02001365 filter = GL_NEAREST;
Pekka Paalanen0e151bb2012-01-24 14:47:37 +02001366
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001367 for (i = 0; i < es->num_textures; i++) {
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001368 glActiveTexture(GL_TEXTURE0 + i);
Rob Clarke3b95912012-08-31 16:42:18 -05001369 glBindTexture(es->target, es->textures[i]);
1370 glTexParameteri(es->target, GL_TEXTURE_MIN_FILTER, filter);
1371 glTexParameteri(es->target, GL_TEXTURE_MAG_FILTER, filter);
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02001372 }
Kristian Høgsberg40caded2011-06-20 19:48:16 -04001373
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001374 /* blended region is whole surface minus opaque region: */
1375 pixman_region32_init_rect(&surface_blend, 0, 0,
1376 es->geometry.width, es->geometry.height);
1377 pixman_region32_subtract(&surface_blend, &surface_blend, &es->opaque);
1378
1379 if (pixman_region32_not_empty(&es->opaque)) {
1380 if (es->shader == &ec->texture_shader_rgba) {
1381 /* Special case for RGBA textures with possibly
1382 * bad data in alpha channel: use the shader
1383 * that forces texture alpha = 1.0.
1384 * Xwayland surfaces need this.
1385 */
1386 weston_compositor_use_shader(ec, &ec->texture_shader_rgbx);
1387 weston_shader_uniforms(&ec->texture_shader_rgbx, es, output);
1388 }
1389
1390 if (es->alpha < 1.0)
1391 glEnable(GL_BLEND);
1392 else
1393 glDisable(GL_BLEND);
1394
Pekka Paalanen8a15bb82012-08-30 16:47:20 -05001395 repaint_region(es, &repaint, &es->opaque);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001396 }
Pekka Paalanen3df327f2012-01-24 15:53:35 +02001397
Rob Clark0e5a2d02012-08-30 16:47:18 -05001398 if (pixman_region32_not_empty(&surface_blend)) {
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001399 weston_compositor_use_shader(ec, es->shader);
Rob Clark0e5a2d02012-08-30 16:47:18 -05001400 glEnable(GL_BLEND);
1401 repaint_region(es, &repaint, &surface_blend);
1402 }
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001403
Pekka Paalanen2abe2e62012-09-03 16:48:42 +03001404 pixman_region32_fini(&surface_blend);
1405
Pekka Paalanena9f8a212012-01-26 11:28:08 +02001406out:
Kristian Høgsberg525e4c02011-02-14 10:39:54 -05001407 pixman_region32_fini(&repaint);
Kristian Høgsberg4c9f2c92008-11-21 19:25:44 -05001408}
1409
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001410WL_EXPORT void
1411weston_surface_restack(struct weston_surface *surface, struct wl_list *below)
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001412{
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001413 wl_list_remove(&surface->layer_link);
1414 wl_list_insert(below, &surface->layer_link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001415 weston_surface_damage_below(surface);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001416 weston_surface_damage(surface);
Kristian Høgsberg8da19ac2009-03-17 16:12:51 -04001417}
1418
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001419WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001420weston_compositor_damage_all(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001421{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001422 struct weston_output *output;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001423
1424 wl_list_for_each(output, &compositor->output_list, link)
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001425 weston_output_damage(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001426}
1427
Kristian Høgsberg9f404b72012-01-26 00:11:01 -05001428WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001429weston_buffer_post_release(struct wl_buffer *buffer)
Benjamin Franzke06286262011-05-06 19:12:33 +02001430{
Kristian Høgsberg24596942011-10-24 17:51:02 -04001431 if (--buffer->busy_count > 0)
Benjamin Franzke06286262011-05-06 19:12:33 +02001432 return;
Benjamin Franzke06286262011-05-06 19:12:33 +02001433
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001434 assert(buffer->resource.client != NULL);
Kristian Høgsbergeccef6a2011-11-17 16:46:19 -05001435 wl_resource_queue_event(&buffer->resource, WL_BUFFER_RELEASE);
Benjamin Franzke06286262011-05-06 19:12:33 +02001436}
1437
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001438WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001439weston_output_damage(struct weston_output *output)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001440{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001441 struct weston_compositor *compositor = output->compositor;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001442
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001443 pixman_region32_union(&compositor->primary_plane.damage,
1444 &compositor->primary_plane.damage,
1445 &output->region);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001446 weston_output_schedule_repaint(output);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001447}
1448
Kristian Høgsberg01f941b2009-05-27 17:47:15 -04001449static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001450fade_frame(struct weston_animation *animation,
1451 struct weston_output *output, uint32_t msecs)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001452{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001453 struct weston_compositor *compositor =
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001454 container_of(animation,
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001455 struct weston_compositor, fade.animation);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001456 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001457
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001458 if (animation->frame_counter <= 1)
Scott Moreaue2949db2012-06-11 13:09:23 -06001459 compositor->fade.spring.timestamp = msecs;
1460
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001461 surface = compositor->fade.surface;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001462 weston_spring_update(&compositor->fade.spring, msecs);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001463 weston_surface_set_color(surface, 0.0, 0.0, 0.0,
1464 compositor->fade.spring.current);
1465 weston_surface_damage(surface);
1466
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001467 if (weston_spring_done(&compositor->fade.spring)) {
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001468 compositor->fade.spring.current =
1469 compositor->fade.spring.target;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001470 wl_list_remove(&animation->link);
1471 wl_list_init(&animation->link);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001472
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001473 if (compositor->fade.spring.current < 0.001) {
1474 destroy_surface(&surface->surface.resource);
1475 compositor->fade.surface = NULL;
1476 } else if (compositor->fade.spring.current > 0.999) {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001477 compositor->state = WESTON_COMPOSITOR_SLEEPING;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04001478 wl_signal_emit(&compositor->lock_signal, compositor);
Pekka Paalanen2e097ee2011-12-02 10:39:49 +02001479 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001480 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001481}
1482
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001483static void
1484update_shm_texture(struct weston_surface *surface)
1485{
1486#ifdef GL_UNPACK_ROW_LENGTH
1487 pixman_box32_t *rectangles;
1488 void *data;
1489 int i, n;
1490#endif
1491
Gwenole Beauchesne023f8552012-04-20 11:07:06 +02001492 glBindTexture(GL_TEXTURE_2D, surface->textures[0]);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001493
1494 if (!surface->compositor->has_unpack_subimage) {
1495 glTexImage2D(GL_TEXTURE_2D, 0, GL_BGRA_EXT,
1496 surface->pitch, surface->buffer->height, 0,
1497 GL_BGRA_EXT, GL_UNSIGNED_BYTE,
1498 wl_shm_buffer_get_data(surface->buffer));
1499
1500 return;
1501 }
1502
1503#ifdef GL_UNPACK_ROW_LENGTH
1504 /* Mesa does not define GL_EXT_unpack_subimage */
1505 glPixelStorei(GL_UNPACK_ROW_LENGTH, surface->pitch);
1506 data = wl_shm_buffer_get_data(surface->buffer);
1507 rectangles = pixman_region32_rectangles(&surface->damage, &n);
1508 for (i = 0; i < n; i++) {
1509 glPixelStorei(GL_UNPACK_SKIP_PIXELS, rectangles[i].x1);
1510 glPixelStorei(GL_UNPACK_SKIP_ROWS, rectangles[i].y1);
1511 glTexSubImage2D(GL_TEXTURE_2D, 0,
1512 rectangles[i].x1, rectangles[i].y1,
Rob Bradford8f241562012-07-02 17:33:40 +01001513 rectangles[i].x2 - rectangles[i].x1,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001514 rectangles[i].y2 - rectangles[i].y1,
1515 GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
1516 }
1517#endif
1518}
1519
1520static void
1521surface_accumulate_damage(struct weston_surface *surface,
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001522 pixman_region32_t *opaque)
1523{
1524 if (surface->buffer && wl_buffer_is_shm(surface->buffer))
1525 update_shm_texture(surface);
1526
1527 if (surface->transform.enabled) {
1528 pixman_box32_t *extents;
1529
1530 extents = pixman_region32_extents(&surface->damage);
1531 surface_compute_bbox(surface, extents->x1, extents->y1,
1532 extents->x2 - extents->x1,
1533 extents->y2 - extents->y1,
1534 &surface->damage);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001535 pixman_region32_translate(&surface->damage,
1536 -surface->plane->x,
1537 -surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001538 } else {
1539 pixman_region32_translate(&surface->damage,
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001540 surface->geometry.x - surface->plane->x,
1541 surface->geometry.y - surface->plane->y);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001542 }
1543
1544 pixman_region32_subtract(&surface->damage, &surface->damage, opaque);
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001545 pixman_region32_union(&surface->plane->damage,
1546 &surface->plane->damage, &surface->damage);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001547 empty_region(&surface->damage);
1548 pixman_region32_copy(&surface->clip, opaque);
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001549 pixman_region32_union(opaque, opaque, &surface->transform.opaque);
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001550}
1551
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001552static void
Rob Bradford0fb79752012-08-02 15:36:57 +01001553weston_output_repaint(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001554{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001555 struct weston_compositor *ec = output->compositor;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001556 struct weston_surface *es;
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001557 struct weston_layer *layer;
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001558 struct weston_animation *animation, *next;
1559 struct weston_frame_callback *cb, *cnext;
Jonas Ådahldb773762012-06-13 00:01:21 +02001560 struct wl_list frame_callback_list;
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001561 pixman_region32_t opaque, output_damage, new_damage;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001562 int32_t width, height;
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001563
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02001564 weston_compositor_update_drag_surfaces(ec);
1565
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001566 width = output->current->width +
1567 output->border.left + output->border.right;
1568 height = output->current->height +
1569 output->border.top + output->border.bottom;
Scott Moreau1bad5db2012-08-18 01:04:05 -06001570
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001571 glViewport(0, 0, width, height);
Kristian Høgsbergfdec2362001-01-01 22:23:51 -05001572
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001573 /* Rebuild the surface list and update surface transforms up front. */
1574 wl_list_init(&ec->surface_list);
Jonas Ådahldb773762012-06-13 00:01:21 +02001575 wl_list_init(&frame_callback_list);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001576 wl_list_for_each(layer, &ec->layer_list, link) {
1577 wl_list_for_each(es, &layer->surface_list, layer_link) {
1578 weston_surface_update_transform(es);
1579 wl_list_insert(ec->surface_list.prev, &es->link);
Jonas Ådahldb773762012-06-13 00:01:21 +02001580 if (es->output == output) {
1581 wl_list_insert_list(&frame_callback_list,
1582 &es->frame_callback_list);
1583 wl_list_init(&es->frame_callback_list);
1584 }
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001585 }
1586 }
Pekka Paalanen15d60ef2012-01-27 14:38:33 +02001587
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001588 if (output->assign_planes && !output->disable_planes)
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001589 output->assign_planes(output);
Kristian Høgsberg79af73e2012-08-03 15:45:23 -04001590 else
1591 wl_list_for_each(es, &ec->surface_list, link)
1592 weston_surface_move_to_plane(es, &ec->primary_plane);
1593
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001594 pixman_region32_init(&opaque);
1595
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001596 wl_list_for_each(es, &ec->surface_list, link)
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04001597 surface_accumulate_damage(es, &opaque);
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001598
Kristian Høgsberg7341e9b2011-07-01 22:12:11 -04001599 pixman_region32_fini(&opaque);
Kristian Høgsberg53df1d82011-06-23 21:11:19 -04001600
Ander Conselvan de Oliveira4f521732012-08-15 14:02:05 -04001601 pixman_region32_init(&output_damage);
1602
1603 pixman_region32_init(&new_damage);
1604 pixman_region32_copy(&new_damage, &ec->primary_plane.damage);
1605
1606 pixman_region32_union(&ec->primary_plane.damage,
1607 &ec->primary_plane.damage,
1608 &output->previous_damage);
1609
1610 pixman_region32_intersect(&output->previous_damage,
1611 &new_damage, &output->region);
1612
1613 pixman_region32_intersect(&output_damage,
1614 &ec->primary_plane.damage, &output->region);
1615
1616 pixman_region32_fini(&new_damage);
1617
Scott Moreauccbf29d2012-02-22 14:21:41 -07001618 if (output->dirty)
1619 weston_output_update_matrix(output);
1620
Pekka Paalanen07c91f82012-08-30 16:47:21 -05001621 /* if debugging, redraw everything outside the damage to clean up
1622 * debug lines from the previous draw on this buffer:
1623 */
1624 if (ec->fan_debug) {
1625 pixman_region32_t undamaged;
1626 pixman_region32_init(&undamaged);
1627 pixman_region32_subtract(&undamaged, &output->region,
1628 &output_damage);
1629 ec->fan_debug = 0;
1630 output->repaint(output, &undamaged, 0);
1631 ec->fan_debug = 1;
1632 pixman_region32_fini(&undamaged);
1633 }
1634
1635 output->repaint(output, &output_damage, 1);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001636
Kristian Høgsberg6ddcdae2012-02-28 22:31:58 -05001637 pixman_region32_fini(&output_damage);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001638
Kristian Høgsbergef044142011-06-21 15:02:12 -04001639 output->repaint_needed = 0;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001640
Kristian Høgsbergaa6019e2012-03-11 16:35:16 -04001641 weston_compositor_repick(ec);
1642 wl_event_loop_dispatch(ec->input_loop, 0);
1643
Jonas Ådahldb773762012-06-13 00:01:21 +02001644 wl_list_for_each_safe(cb, cnext, &frame_callback_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05001645 wl_callback_send_done(&cb->resource, msecs);
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001646 wl_resource_destroy(&cb->resource);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001647 }
Jonas Ådahldb773762012-06-13 00:01:21 +02001648 wl_list_init(&frame_callback_list);
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05001649
Scott Moreaud64cf212012-06-08 19:40:54 -06001650 wl_list_for_each_safe(animation, next, &output->animation_list, link) {
Scott Moreaud64cf212012-06-08 19:40:54 -06001651 animation->frame_counter++;
Scott Moreau94b0b0c2012-06-14 01:01:56 -06001652 animation->frame(animation, output, msecs);
Scott Moreaud64cf212012-06-08 19:40:54 -06001653 }
Kristian Høgsbergef044142011-06-21 15:02:12 -04001654}
Kristian Høgsbergb1868472011-04-22 12:27:57 -04001655
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001656static int
1657weston_compositor_read_input(int fd, uint32_t mask, void *data)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001658{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001659 struct weston_compositor *compositor = data;
Kristian Høgsberg34f80ff2012-01-18 11:50:31 -05001660
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001661 wl_event_loop_dispatch(compositor->input_loop, 0);
1662
1663 return 1;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001664}
1665
1666WL_EXPORT void
Rob Bradford0fb79752012-08-02 15:36:57 +01001667weston_output_finish_frame(struct weston_output *output, uint32_t msecs)
Kristian Høgsbergef044142011-06-21 15:02:12 -04001668{
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001669 struct weston_compositor *compositor = output->compositor;
1670 struct wl_event_loop *loop =
1671 wl_display_get_event_loop(compositor->wl_display);
1672 int fd;
1673
Kristian Høgsberge9d04922012-06-19 23:54:26 -04001674 output->frame_time = msecs;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001675 if (output->repaint_needed) {
Kristian Høgsberg30c018b2012-01-26 08:40:37 -05001676 weston_output_repaint(output, msecs);
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001677 return;
1678 }
1679
1680 output->repaint_scheduled = 0;
1681 if (compositor->input_loop_source)
1682 return;
1683
1684 fd = wl_event_loop_get_fd(compositor->input_loop);
1685 compositor->input_loop_source =
1686 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1687 weston_compositor_read_input, compositor);
1688}
1689
1690static void
1691idle_repaint(void *data)
1692{
1693 struct weston_output *output = data;
1694
1695 weston_output_finish_frame(output, weston_compositor_get_time());
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001696}
1697
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001698WL_EXPORT void
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001699weston_layer_init(struct weston_layer *layer, struct wl_list *below)
1700{
1701 wl_list_init(&layer->surface_list);
Jonas Ådahle3cddce2012-06-13 00:01:22 +02001702 if (below != NULL)
1703 wl_list_insert(below, &layer->link);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001704}
1705
1706WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001707weston_output_schedule_repaint(struct weston_output *output)
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001708{
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001709 struct weston_compositor *compositor = output->compositor;
Kristian Høgsbergef044142011-06-21 15:02:12 -04001710 struct wl_event_loop *loop;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001711
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001712 if (compositor->state == WESTON_COMPOSITOR_SLEEPING)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04001713 return;
1714
Kristian Høgsbergef044142011-06-21 15:02:12 -04001715 loop = wl_display_get_event_loop(compositor->wl_display);
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001716 output->repaint_needed = 1;
1717 if (output->repaint_scheduled)
1718 return;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001719
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001720 wl_event_loop_add_idle(loop, idle_repaint, output);
1721 output->repaint_scheduled = 1;
Kristian Høgsberg7dbf5e22012-03-05 19:50:08 -05001722
1723 if (compositor->input_loop_source) {
1724 wl_event_source_remove(compositor->input_loop_source);
1725 compositor->input_loop_source = NULL;
1726 }
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04001727}
Kristian Høgsberg5c8c3282009-02-09 15:17:46 -05001728
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001729WL_EXPORT void
Kristian Høgsberg49952d12012-06-20 00:35:59 -04001730weston_compositor_schedule_repaint(struct weston_compositor *compositor)
1731{
1732 struct weston_output *output;
1733
1734 wl_list_for_each(output, &compositor->output_list, link)
1735 weston_output_schedule_repaint(output);
1736}
1737
1738WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001739weston_compositor_fade(struct weston_compositor *compositor, float tint)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001740{
Scott Moreau9d1b1122012-06-08 19:40:53 -06001741 struct weston_output *output;
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001742 struct weston_surface *surface;
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001743
Scott Moreau9d1b1122012-06-08 19:40:53 -06001744 output = container_of(compositor->output_list.next,
1745 struct weston_output, link);
1746
Kristian Høgsberg269c7822011-05-02 14:38:18 -04001747 compositor->fade.spring.target = tint;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001748 if (weston_spring_done(&compositor->fade.spring))
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001749 return;
1750
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001751 if (compositor->fade.surface == NULL) {
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001752 surface = weston_surface_create(compositor);
1753 weston_surface_configure(surface, 0, 0, 8192, 8192);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001754 weston_surface_set_color(surface, 0.0, 0.0, 0.0, 0.0);
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05001755 wl_list_insert(&compositor->fade_layer.surface_list,
1756 &surface->layer_link);
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02001757 weston_surface_assign_output(surface);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001758 compositor->fade.surface = surface;
Kristian Høgsberg9bc70952012-02-29 12:22:38 -05001759 pixman_region32_init(&surface->input);
Kristian Høgsberg681f9f42012-01-26 10:55:10 -05001760 }
1761
1762 weston_surface_damage(compositor->fade.surface);
Scott Moreaud64cf212012-06-08 19:40:54 -06001763 if (wl_list_empty(&compositor->fade.animation.link)) {
1764 compositor->fade.animation.frame_counter = 0;
Scott Moreau9d1b1122012-06-08 19:40:53 -06001765 wl_list_insert(output->animation_list.prev,
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001766 &compositor->fade.animation.link);
Scott Moreaud64cf212012-06-08 19:40:54 -06001767 }
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04001768}
1769
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001770static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001771surface_destroy(struct wl_client *client, struct wl_resource *resource)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001772{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04001773 wl_resource_destroy(resource);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04001774}
1775
Casey Dahlin96d8a752012-04-19 22:50:07 -04001776static struct wl_resource *
1777find_resource_for_client(struct wl_list *list, struct wl_client *client)
1778{
1779 struct wl_resource *r;
1780
1781 wl_list_for_each(r, list, link) {
1782 if (r->client == client)
1783 return r;
1784 }
1785
1786 return NULL;
1787}
1788
Casey Dahlin9074db52012-04-19 22:50:09 -04001789static void
1790weston_surface_update_output_mask(struct weston_surface *es, uint32_t mask)
1791{
1792 uint32_t different = es->output_mask ^ mask;
1793 uint32_t entered = mask & different;
1794 uint32_t left = es->output_mask & different;
1795 struct weston_output *output;
Rob Bradford8667e772012-05-18 14:13:03 +01001796 struct wl_resource *resource = NULL;
Casey Dahlin9074db52012-04-19 22:50:09 -04001797 struct wl_client *client = es->surface.resource.client;
1798
Scott Moreaua5021522012-08-03 17:11:51 -06001799 es->output_mask = mask;
Casey Dahlin9074db52012-04-19 22:50:09 -04001800 if (es->surface.resource.client == NULL)
1801 return;
1802 if (different == 0)
1803 return;
1804
Casey Dahlin9074db52012-04-19 22:50:09 -04001805 wl_list_for_each(output, &es->compositor->output_list, link) {
1806 if (1 << output->id & different)
1807 resource =
1808 find_resource_for_client(&output->resource_list,
1809 client);
Kristian Høgsbergc7814d22012-07-12 12:34:43 -04001810 if (resource == NULL)
1811 continue;
Casey Dahlin9074db52012-04-19 22:50:09 -04001812 if (1 << output->id & entered)
1813 wl_surface_send_enter(&es->surface.resource, resource);
1814 if (1 << output->id & left)
1815 wl_surface_send_leave(&es->surface.resource, resource);
1816 }
1817}
1818
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04001819WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001820weston_surface_assign_output(struct weston_surface *es)
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001821{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001822 struct weston_compositor *ec = es->compositor;
1823 struct weston_output *output, *new_output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001824 pixman_region32_t region;
Casey Dahlin9074db52012-04-19 22:50:09 -04001825 uint32_t max, area, mask;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001826 pixman_box32_t *e;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001827
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001828 new_output = NULL;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001829 max = 0;
Casey Dahlin9074db52012-04-19 22:50:09 -04001830 mask = 0;
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001831 pixman_region32_init(&region);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001832 wl_list_for_each(output, &ec->output_list, link) {
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001833 pixman_region32_intersect(&region, &es->transform.boundingbox,
1834 &output->region);
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001835
1836 e = pixman_region32_extents(&region);
1837 area = (e->x2 - e->x1) * (e->y2 - e->y1);
1838
Casey Dahlin9074db52012-04-19 22:50:09 -04001839 if (area > 0)
1840 mask |= 1 << output->id;
1841
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001842 if (area >= max) {
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001843 new_output = output;
Kristian Høgsberg4f0df042011-07-21 11:30:22 -07001844 max = area;
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001845 }
1846 }
Pekka Paalanen45f3e402012-01-26 11:34:16 +02001847 pixman_region32_fini(&region);
Kristian Høgsberg496433b2011-11-15 13:50:21 -05001848
1849 es->output = new_output;
Casey Dahlin9074db52012-04-19 22:50:09 -04001850 weston_surface_update_output_mask(es, mask);
Benjamin Franzkeec4d3422011-03-14 12:07:26 +01001851}
1852
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001853static void
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001854surface_attach(struct wl_client *client,
1855 struct wl_resource *resource,
1856 struct wl_resource *buffer_resource, int32_t sx, int32_t sy)
1857{
1858 struct weston_surface *es = resource->data;
1859 struct wl_buffer *buffer = NULL;
1860
1861 if (buffer_resource)
1862 buffer = buffer_resource->data;
Kristian Høgsberga691aee2011-06-23 21:43:50 -04001863
Kristian Høgsberg8cbfb2a2012-03-27 17:04:59 -04001864 weston_surface_attach(&es->surface, buffer);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03001865
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03001866 if (buffer && es->configure)
1867 es->configure(es, sx, sy);
Kristian Høgsbergf9212892008-10-11 18:40:23 -04001868}
1869
Kristian Høgsberg5503bf82008-11-06 10:38:17 -05001870static void
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001871surface_damage(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001872 struct wl_resource *resource,
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001873 int32_t x, int32_t y, int32_t width, int32_t height)
Kristian Høgsberg7f77bd82008-11-07 08:39:37 -05001874{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001875 struct weston_surface *es = resource->data;
Kristian Høgsberg9d69f8e2010-09-03 14:46:38 -04001876
Kristian Høgsberg460a79b2012-06-18 15:09:11 -04001877 pixman_region32_union_rect(&es->damage, &es->damage,
1878 x, y, width, height);
Kristian Høgsberg98238702012-08-03 16:29:12 -04001879 weston_surface_schedule_repaint(es);
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05001880}
1881
Kristian Høgsberg33418202011-08-16 23:01:28 -04001882static void
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001883destroy_frame_callback(struct wl_resource *resource)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001884{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001885 struct weston_frame_callback *cb = resource->data;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001886
1887 wl_list_remove(&cb->link);
Pekka Paalanen8c196452011-11-15 11:45:42 +02001888 free(cb);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001889}
1890
1891static void
1892surface_frame(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001893 struct wl_resource *resource, uint32_t callback)
Kristian Høgsberg33418202011-08-16 23:01:28 -04001894{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001895 struct weston_frame_callback *cb;
1896 struct weston_surface *es = resource->data;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001897
1898 cb = malloc(sizeof *cb);
1899 if (cb == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001900 wl_resource_post_no_memory(resource);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001901 return;
1902 }
1903
1904 cb->resource.object.interface = &wl_callback_interface;
1905 cb->resource.object.id = callback;
1906 cb->resource.destroy = destroy_frame_callback;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001907 cb->resource.client = client;
1908 cb->resource.data = cb;
Kristian Høgsberg33418202011-08-16 23:01:28 -04001909
1910 wl_client_add_resource(client, &cb->resource);
Jonas Ådahldb773762012-06-13 00:01:21 +02001911 wl_list_insert(es->frame_callback_list.prev, &cb->link);
Kristian Høgsberg33418202011-08-16 23:01:28 -04001912}
1913
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001914static void
1915surface_set_opaque_region(struct wl_client *client,
1916 struct wl_resource *resource,
1917 struct wl_resource *region_resource)
1918{
1919 struct weston_surface *surface = resource->data;
1920 struct weston_region *region;
1921
1922 pixman_region32_fini(&surface->opaque);
1923
1924 if (region_resource) {
1925 region = region_resource->data;
1926 pixman_region32_init_rect(&surface->opaque, 0, 0,
1927 surface->geometry.width,
1928 surface->geometry.height);
1929 pixman_region32_intersect(&surface->opaque,
1930 &surface->opaque, &region->region);
1931 } else {
1932 pixman_region32_init(&surface->opaque);
1933 }
1934
1935 surface->geometry.dirty = 1;
1936}
1937
1938static void
1939surface_set_input_region(struct wl_client *client,
1940 struct wl_resource *resource,
1941 struct wl_resource *region_resource)
1942{
1943 struct weston_surface *surface = resource->data;
Kristian Høgsberg010f98b2012-02-23 17:30:45 -05001944 struct weston_region *region;
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001945
1946 if (region_resource) {
1947 region = region_resource->data;
1948 pixman_region32_init_rect(&surface->input, 0, 0,
1949 surface->geometry.width,
1950 surface->geometry.height);
1951 pixman_region32_intersect(&surface->input,
1952 &surface->input, &region->region);
1953 } else {
1954 pixman_region32_init_rect(&surface->input, 0, 0,
1955 surface->geometry.width,
1956 surface->geometry.height);
1957 }
1958
Kristian Høgsberg98238702012-08-03 16:29:12 -04001959 weston_surface_schedule_repaint(surface);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001960}
1961
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04001962static const struct wl_surface_interface surface_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001963 surface_destroy,
1964 surface_attach,
Kristian Høgsberg33418202011-08-16 23:01:28 -04001965 surface_damage,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001966 surface_frame,
1967 surface_set_opaque_region,
1968 surface_set_input_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001969};
1970
1971static void
1972compositor_create_surface(struct wl_client *client,
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001973 struct wl_resource *resource, uint32_t id)
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001974{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001975 struct weston_compositor *ec = resource->data;
1976 struct weston_surface *surface;
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001977
Kristian Høgsberg18c93002012-01-27 11:58:31 -05001978 surface = weston_surface_create(ec);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001979 if (surface == NULL) {
Kristian Høgsberg9ebcf942011-09-01 09:54:57 -04001980 wl_resource_post_no_memory(resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001981 return;
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04001982 }
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001983
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001984 surface->surface.resource.destroy = destroy_surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001985
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001986 surface->surface.resource.object.id = id;
1987 surface->surface.resource.object.interface = &wl_surface_interface;
1988 surface->surface.resource.object.implementation =
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001989 (void (**)(void)) &surface_interface;
Kristian Høgsberg904055a2011-08-18 17:55:30 -04001990 surface->surface.resource.data = surface;
Kristian Høgsbergf66d0f42010-09-02 20:27:16 -04001991
Kristian Høgsbergb313b022010-12-01 17:07:41 -05001992 wl_client_add_resource(client, &surface->surface.resource);
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05001993}
1994
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05001995static void
1996destroy_region(struct wl_resource *resource)
1997{
1998 struct weston_region *region =
1999 container_of(resource, struct weston_region, resource);
2000
2001 pixman_region32_fini(&region->region);
2002 free(region);
2003}
2004
2005static void
2006region_destroy(struct wl_client *client, struct wl_resource *resource)
2007{
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002008 wl_resource_destroy(resource);
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002009}
2010
2011static void
2012region_add(struct wl_client *client, struct wl_resource *resource,
2013 int32_t x, int32_t y, int32_t width, int32_t height)
2014{
2015 struct weston_region *region = resource->data;
2016
2017 pixman_region32_union_rect(&region->region, &region->region,
2018 x, y, width, height);
2019}
2020
2021static void
2022region_subtract(struct wl_client *client, struct wl_resource *resource,
2023 int32_t x, int32_t y, int32_t width, int32_t height)
2024{
2025 struct weston_region *region = resource->data;
2026 pixman_region32_t rect;
2027
2028 pixman_region32_init_rect(&rect, x, y, width, height);
2029 pixman_region32_subtract(&region->region, &region->region, &rect);
2030 pixman_region32_fini(&rect);
2031}
2032
2033static const struct wl_region_interface region_interface = {
2034 region_destroy,
2035 region_add,
2036 region_subtract
2037};
2038
2039static void
2040compositor_create_region(struct wl_client *client,
2041 struct wl_resource *resource, uint32_t id)
2042{
2043 struct weston_region *region;
2044
2045 region = malloc(sizeof *region);
2046 if (region == NULL) {
2047 wl_resource_post_no_memory(resource);
2048 return;
2049 }
2050
2051 region->resource.destroy = destroy_region;
2052
2053 region->resource.object.id = id;
2054 region->resource.object.interface = &wl_region_interface;
2055 region->resource.object.implementation =
2056 (void (**)(void)) &region_interface;
2057 region->resource.data = region;
2058
2059 pixman_region32_init(&region->region);
2060
2061 wl_client_add_resource(client, &region->resource);
2062}
2063
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04002064static const struct wl_compositor_interface compositor_interface = {
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002065 compositor_create_surface,
Kristian Høgsberg5e7e6f22012-02-23 16:11:59 -05002066 compositor_create_region
Kristian Høgsbergd2412e22008-12-15 20:35:24 -05002067};
2068
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002069WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002070weston_compositor_wake(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002071{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002072 compositor->state = WESTON_COMPOSITOR_ACTIVE;
2073 weston_compositor_fade(compositor, 0.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002074
2075 wl_event_source_timer_update(compositor->idle_source,
Pekka Paalanen7296e792011-12-07 16:22:00 +02002076 compositor->idle_time * 1000);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002077}
2078
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002079static void
2080weston_compositor_dpms_on(struct weston_compositor *compositor)
2081{
2082 struct weston_output *output;
2083
2084 wl_list_for_each(output, &compositor->output_list, link)
2085 if (output->set_dpms)
2086 output->set_dpms(output, WESTON_DPMS_ON);
2087}
2088
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002089WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002090weston_compositor_activity(struct weston_compositor *compositor)
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002091{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002092 if (compositor->state == WESTON_COMPOSITOR_ACTIVE) {
2093 weston_compositor_wake(compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002094 } else {
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02002095 weston_compositor_dpms_on(compositor);
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002096 wl_signal_emit(&compositor->unlock_signal, compositor);
Kristian Høgsbergaf867cc2011-11-15 13:34:49 +02002097 }
2098}
2099
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002100static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002101weston_compositor_idle_inhibit(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002102{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002103 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002104 compositor->idle_inhibit++;
2105}
2106
2107static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002108weston_compositor_idle_release(struct weston_compositor *compositor)
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002109{
2110 compositor->idle_inhibit--;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002111 weston_compositor_activity(compositor);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002112}
2113
2114static int
2115idle_handler(void *data)
2116{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002117 struct weston_compositor *compositor = data;
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002118
2119 if (compositor->idle_inhibit)
2120 return 1;
2121
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002122 weston_compositor_fade(compositor, 1.0);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002123
2124 return 1;
2125}
2126
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04002127WL_EXPORT void
2128weston_plane_init(struct weston_plane *plane, int32_t x, int32_t y)
2129{
2130 pixman_region32_init(&plane->damage);
2131 plane->x = x;
2132 plane->y = y;
2133}
2134
2135WL_EXPORT void
2136weston_plane_release(struct weston_plane *plane)
2137{
2138 pixman_region32_fini(&plane->damage);
2139}
2140
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002141static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002142weston_seat_update_drag_surface(struct weston_seat *seat, int dx, int dy);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002143
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002144static void
Daniel Stone37816df2012-05-16 18:45:18 +01002145clip_pointer_motion(struct weston_seat *seat, wl_fixed_t *fx, wl_fixed_t *fy)
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002146{
Daniel Stone37816df2012-05-16 18:45:18 +01002147 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002148 struct weston_output *output, *prev = NULL;
2149 int x, y, old_x, old_y, valid = 0;
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002150
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002151 x = wl_fixed_to_int(*fx);
2152 y = wl_fixed_to_int(*fy);
Daniel Stone37816df2012-05-16 18:45:18 +01002153 old_x = wl_fixed_to_int(seat->seat.pointer->x);
2154 old_y = wl_fixed_to_int(seat->seat.pointer->y);
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002155
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002156 wl_list_for_each(output, &ec->output_list, link) {
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002157 if (pixman_region32_contains_point(&output->region,
2158 x, y, NULL))
2159 valid = 1;
2160 if (pixman_region32_contains_point(&output->region,
2161 old_x, old_y, NULL))
2162 prev = output;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002163 }
Daniel Stone37816df2012-05-16 18:45:18 +01002164
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002165 if (!valid) {
2166 if (x < prev->x)
2167 *fx = wl_fixed_from_int(prev->x);
Scott Moreau1bad5db2012-08-18 01:04:05 -06002168 else if (x >= prev->x + prev->width)
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002169 *fx = wl_fixed_from_int(prev->x +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002170 prev->width - 1);
Kristian Høgsberg34829de2012-05-09 22:21:14 -04002171 if (y < prev->y)
2172 *fy = wl_fixed_from_int(prev->y);
2173 else if (y >= prev->y + prev->current->height)
2174 *fy = wl_fixed_from_int(prev->y +
Scott Moreau1bad5db2012-08-18 01:04:05 -06002175 prev->height - 1);
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01002176 }
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002177}
2178
2179WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002180notify_motion(struct weston_seat *seat, uint32_t time, wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002181{
2182 const struct wl_pointer_grab_interface *interface;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002183 struct weston_compositor *ec = seat->compositor;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002184 struct weston_output *output;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002185 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002186 int32_t ix, iy;
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002187
2188 weston_compositor_activity(ec);
2189
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002190 clip_pointer_motion(seat, &x, &y);
Kristian Høgsberg1f376012012-05-09 11:43:11 -04002191
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002192 weston_seat_update_drag_surface(seat, x - pointer->x, y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002193
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002194 pointer->x = x;
2195 pointer->y = y;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002196
2197 ix = wl_fixed_to_int(x);
2198 iy = wl_fixed_to_int(y);
Kristian Høgsbergdb6c2f32009-02-22 21:51:24 -05002199
Scott Moreauccbf29d2012-02-22 14:21:41 -07002200 wl_list_for_each(output, &ec->output_list, link)
2201 if (output->zoom.active &&
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002202 pixman_region32_contains_point(&output->region,
2203 ix, iy, NULL))
Scott Moreau8dacaab2012-06-17 18:10:58 -06002204 weston_output_update_zoom(output, ZOOM_FOCUS_POINTER);
Scott Moreauccbf29d2012-02-22 14:21:41 -07002205
Daniel Stone37816df2012-05-16 18:45:18 +01002206 weston_device_repick(seat);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002207 interface = pointer->grab->interface;
2208 interface->motion(pointer->grab, time,
2209 pointer->grab->x, pointer->grab->y);
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002210
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002211 if (seat->sprite) {
2212 weston_surface_set_position(seat->sprite,
2213 ix - seat->hotspot_x,
2214 iy - seat->hotspot_y);
2215 weston_surface_schedule_repaint(seat->sprite);
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002216 }
Kristian Høgsberg715a0812008-12-10 10:42:04 -05002217}
2218
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04002219WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002220weston_surface_activate(struct weston_surface *surface,
Daniel Stone37816df2012-05-16 18:45:18 +01002221 struct weston_seat *seat)
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002222{
Daniel Stone37816df2012-05-16 18:45:18 +01002223 struct weston_compositor *compositor = seat->compositor;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002224
Pekka Paalanenbdc7cd02012-06-07 15:07:07 +03002225 if (seat->seat.keyboard) {
2226 wl_keyboard_set_focus(seat->seat.keyboard, &surface->surface);
2227 wl_data_device_set_keyboard_focus(&seat->seat);
Daniel Stone351eb612012-05-31 15:27:47 -04002228 }
2229
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04002230 wl_signal_emit(&compositor->activate_signal, surface);
Kristian Høgsbergc9824dd2011-02-06 16:54:59 -05002231}
2232
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002233WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002234notify_button(struct weston_seat *seat, uint32_t time, int32_t button,
Daniel Stone4dbadb12012-05-30 16:31:51 +01002235 enum wl_pointer_button_state state)
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002236{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002237 struct weston_compositor *compositor = seat->compositor;
2238 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002239 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002240 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002241 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergea081152010-12-07 08:59:51 -05002242
Daniel Stone4dbadb12012-05-30 16:31:51 +01002243 if (state == WL_POINTER_BUTTON_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002244 if (compositor->ping_handler && focus)
2245 compositor->ping_handler(focus, serial);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002246 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002247 if (pointer->button_count == 0) {
2248 pointer->grab_button = button;
2249 pointer->grab_time = time;
2250 pointer->grab_x = pointer->x;
2251 pointer->grab_y = pointer->y;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002252 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002253 pointer->button_count++;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002254 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002255 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002256 pointer->button_count--;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002257 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002258
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002259 weston_compositor_run_button_binding(compositor, seat, time, button,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002260 state);
Kristian Høgsbergb3fc7572010-12-08 11:07:57 -05002261
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002262 pointer->grab->interface->button(pointer->grab, time, button, state);
Kristian Høgsberg5a5f0072011-12-19 14:54:11 -05002263
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002264 if (pointer->button_count == 1)
2265 pointer->grab_serial =
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002266 wl_display_get_serial(compositor->wl_display);
Kristian Høgsbergeac149a2008-12-10 00:24:18 -05002267}
2268
Scott Moreau210d0792012-03-22 10:47:01 -06002269WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002270notify_axis(struct weston_seat *seat, uint32_t time, uint32_t axis,
Daniel Stone878f0b72012-05-30 16:31:57 +01002271 wl_fixed_t value)
Scott Moreau210d0792012-03-22 10:47:01 -06002272{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002273 struct weston_compositor *compositor = seat->compositor;
2274 struct wl_pointer *pointer = seat->seat.pointer;
Daniel Stone37816df2012-05-16 18:45:18 +01002275 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002276 (struct weston_surface *) pointer->focus;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002277 uint32_t serial = wl_display_next_serial(compositor->wl_display);
2278
2279 if (compositor->ping_handler && focus)
2280 compositor->ping_handler(focus, serial);
Scott Moreau210d0792012-03-22 10:47:01 -06002281
2282 weston_compositor_activity(compositor);
2283
Scott Moreau6a3633d2012-03-20 08:47:59 -06002284 if (value)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002285 weston_compositor_run_axis_binding(compositor, seat,
2286 time, axis, value);
Scott Moreau6a3633d2012-03-20 08:47:59 -06002287 else
2288 return;
2289
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002290 if (pointer->focus_resource)
2291 wl_pointer_send_axis(pointer->focus_resource, time, axis,
Daniel Stone2fce4022012-05-30 16:32:00 +01002292 value);
Scott Moreau210d0792012-03-22 10:47:01 -06002293}
2294
Daniel Stone05d58682012-06-22 13:21:38 +01002295WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002296notify_modifiers(struct weston_seat *seat, uint32_t serial)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05002297{
Daniel Stone05d58682012-06-22 13:21:38 +01002298 struct wl_keyboard *keyboard = &seat->keyboard;
2299 struct wl_keyboard_grab *grab = keyboard->grab;
Daniel Stone7ace3902012-05-30 16:31:40 +01002300 uint32_t mods_depressed, mods_latched, mods_locked, group;
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002301 uint32_t mods_lookup;
Daniel Stone8c8164f2012-05-30 16:31:45 +01002302 enum weston_led leds = 0;
Daniel Stone05d58682012-06-22 13:21:38 +01002303 int changed = 0;
Daniel Stone7ace3902012-05-30 16:31:40 +01002304
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002305 /* Serialize and update our internal state, checking to see if it's
2306 * different to the previous state. */
Daniel Stone994679a2012-05-30 16:31:43 +01002307 mods_depressed = xkb_state_serialize_mods(seat->xkb_state.state,
2308 XKB_STATE_DEPRESSED);
2309 mods_latched = xkb_state_serialize_mods(seat->xkb_state.state,
2310 XKB_STATE_LATCHED);
2311 mods_locked = xkb_state_serialize_mods(seat->xkb_state.state,
2312 XKB_STATE_LOCKED);
2313 group = xkb_state_serialize_group(seat->xkb_state.state,
Daniel Stone7ace3902012-05-30 16:31:40 +01002314 XKB_STATE_EFFECTIVE);
2315
Daniel Stone71c38772012-06-22 13:21:30 +01002316 if (mods_depressed != seat->seat.keyboard->modifiers.mods_depressed ||
2317 mods_latched != seat->seat.keyboard->modifiers.mods_latched ||
2318 mods_locked != seat->seat.keyboard->modifiers.mods_locked ||
2319 group != seat->seat.keyboard->modifiers.group)
Daniel Stone05d58682012-06-22 13:21:38 +01002320 changed = 1;
Daniel Stone7ace3902012-05-30 16:31:40 +01002321
Daniel Stone71c38772012-06-22 13:21:30 +01002322 seat->seat.keyboard->modifiers.mods_depressed = mods_depressed;
2323 seat->seat.keyboard->modifiers.mods_latched = mods_latched;
2324 seat->seat.keyboard->modifiers.mods_locked = mods_locked;
2325 seat->seat.keyboard->modifiers.group = group;
Kristian Høgsbergab909ae2001-01-01 22:24:24 -05002326
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002327 /* And update the modifier_state for bindings. */
2328 mods_lookup = mods_depressed | mods_latched;
2329 seat->modifier_state = 0;
Daniel Stone8db79692012-06-01 12:13:58 +01002330 if (mods_lookup & (1 << seat->xkb_info.ctrl_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002331 seat->modifier_state |= MODIFIER_CTRL;
Daniel Stone8db79692012-06-01 12:13:58 +01002332 if (mods_lookup & (1 << seat->xkb_info.alt_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002333 seat->modifier_state |= MODIFIER_ALT;
Daniel Stone8db79692012-06-01 12:13:58 +01002334 if (mods_lookup & (1 << seat->xkb_info.super_mod))
Daniel Stonee3f15ed2012-05-30 16:31:44 +01002335 seat->modifier_state |= MODIFIER_SUPER;
Kristian Høgsberg73694c82012-06-28 14:13:10 -04002336 if (mods_lookup & (1 << seat->xkb_info.shift_mod))
2337 seat->modifier_state |= MODIFIER_SHIFT;
Daniel Stone7ace3902012-05-30 16:31:40 +01002338
Daniel Stone8c8164f2012-05-30 16:31:45 +01002339 /* Finally, notify the compositor that LEDs have changed. */
2340 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002341 seat->xkb_info.num_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002342 leds |= LED_NUM_LOCK;
2343 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002344 seat->xkb_info.caps_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002345 leds |= LED_CAPS_LOCK;
2346 if (xkb_state_led_index_is_active(seat->xkb_state.state,
Daniel Stoned65b9092012-05-30 16:32:05 +01002347 seat->xkb_info.scroll_led))
Daniel Stone8c8164f2012-05-30 16:31:45 +01002348 leds |= LED_SCROLL_LOCK;
2349 if (leds != seat->xkb_state.leds && seat->led_update)
Daniel Stonebbf63bf2012-06-04 11:40:48 +01002350 seat->led_update(seat, leds);
Daniel Stone8c8164f2012-05-30 16:31:45 +01002351 seat->xkb_state.leds = leds;
2352
Daniel Stone05d58682012-06-22 13:21:38 +01002353 if (changed) {
2354 grab->interface->modifiers(grab,
2355 serial,
2356 keyboard->modifiers.mods_depressed,
2357 keyboard->modifiers.mods_latched,
2358 keyboard->modifiers.mods_locked,
2359 keyboard->modifiers.group);
2360 }
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002361}
Kristian Høgsberg2cbedd12009-09-18 17:29:49 -04002362
Daniel Stone05d58682012-06-22 13:21:38 +01002363static void
2364update_modifier_state(struct weston_seat *seat, uint32_t serial, uint32_t key,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002365 enum wl_keyboard_key_state state)
2366{
2367 enum xkb_key_direction direction;
2368
2369 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2370 direction = XKB_KEY_DOWN;
2371 else
2372 direction = XKB_KEY_UP;
2373
2374 /* Offset the keycode by 8, as the evdev XKB rules reflect X's
2375 * broken keycode system, which starts at 8. */
2376 xkb_state_update_key(seat->xkb_state.state, key + 8, direction);
2377
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002378 notify_modifiers(seat, serial);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002379}
2380
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002381WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002382notify_key(struct weston_seat *seat, uint32_t time, uint32_t key,
Daniel Stone1b4e11f2012-06-22 13:21:37 +01002383 enum wl_keyboard_key_state state,
2384 enum weston_key_state_update update_state)
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002385{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002386 struct weston_compositor *compositor = seat->compositor;
2387 struct wl_keyboard *keyboard = seat->seat.keyboard;
Daniel Stone37816df2012-05-16 18:45:18 +01002388 struct weston_surface *focus =
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002389 (struct weston_surface *) keyboard->focus;
2390 struct wl_keyboard_grab *grab = keyboard->grab;
Scott Moreauff1db4a2012-04-17 19:06:18 -06002391 uint32_t serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002392 uint32_t *k, *end;
Kristian Høgsbergf512d072011-04-12 17:16:00 -04002393
Daniel Stonec9785ea2012-05-30 16:31:52 +01002394 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Scott Moreauff1db4a2012-04-17 19:06:18 -06002395 if (compositor->ping_handler && focus)
2396 compositor->ping_handler(focus, serial);
2397
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002398 weston_compositor_idle_inhibit(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002399 keyboard->grab_key = key;
2400 keyboard->grab_time = time;
Scott Moreauec286eb2012-02-18 05:05:30 -07002401 } else {
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002402 weston_compositor_idle_release(compositor);
Scott Moreauec286eb2012-02-18 05:05:30 -07002403 }
Kristian Høgsberge10a5d92011-04-22 14:01:18 -04002404
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002405 end = keyboard->keys.data + keyboard->keys.size;
2406 for (k = keyboard->keys.data; k < end; k++) {
Daniel Stonec6587ea2012-06-22 13:21:31 +01002407 if (*k == key) {
2408 /* Ignore server-generated repeats. */
2409 if (state == WL_KEYBOARD_KEY_STATE_PRESSED)
2410 return;
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002411 *k = *--end;
Daniel Stonec6587ea2012-06-22 13:21:31 +01002412 }
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002413 }
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002414 keyboard->keys.size = (void *) end - keyboard->keys.data;
Daniel Stonec9785ea2012-05-30 16:31:52 +01002415 if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002416 k = wl_array_add(&keyboard->keys, sizeof *k);
Kristian Høgsberg3c38fa02009-02-23 22:30:29 -05002417 *k = key;
2418 }
Ray Strodee96dcb82008-12-20 02:00:49 -05002419
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002420 if (grab == &keyboard->default_grab) {
2421 weston_compositor_run_key_binding(compositor, seat, time, key,
Daniel Stone325fc2d2012-05-30 16:31:58 +01002422 state);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002423 grab = keyboard->grab;
Scott Moreaubefa6532012-06-11 14:59:31 -06002424 }
Kristian Høgsbergabcef3c2012-03-05 17:47:15 -05002425
Daniel Stone7ace3902012-05-30 16:31:40 +01002426 grab->interface->key(grab, time, key, state);
Daniel Stone05d58682012-06-22 13:21:38 +01002427
2428 if (update_state == STATE_UPDATE_AUTOMATIC) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002429 update_modifier_state(seat,
Daniel Stone05d58682012-06-22 13:21:38 +01002430 wl_display_get_serial(compositor->wl_display),
2431 key,
2432 state);
2433 }
Kristian Høgsberg808fd412010-07-20 17:06:19 -04002434}
2435
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002436WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002437notify_pointer_focus(struct weston_seat *seat, struct weston_output *output,
Daniel Stone37816df2012-05-16 18:45:18 +01002438 wl_fixed_t x, wl_fixed_t y)
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002439{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002440 struct weston_compositor *compositor = seat->compositor;
2441 struct wl_pointer *pointer = seat->seat.pointer;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002442
2443 if (output) {
Daniel Stone37816df2012-05-16 18:45:18 +01002444 weston_seat_update_drag_surface(seat,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002445 x - pointer->x,
2446 y - pointer->y);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02002447
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002448 pointer->x = x;
2449 pointer->y = y;
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002450 compositor->focus = 1;
Kristian Høgsberg9ddb8262012-01-04 21:30:29 -05002451 weston_compositor_repick(compositor);
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002452 } else {
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002453 compositor->focus = 0;
Tiago Vignatticd717b52012-07-12 00:46:10 +03002454 /* FIXME: We should call wl_pointer_set_focus(seat,
2455 * NULL) here, but somehow that breaks re-entry... */
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002456 }
Kristian Høgsberg93331ff2011-01-26 20:35:07 -05002457}
2458
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002459static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002460destroy_device_saved_kbd_focus(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002461{
Daniel Stone37816df2012-05-16 18:45:18 +01002462 struct weston_seat *ws;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002463
Daniel Stone37816df2012-05-16 18:45:18 +01002464 ws = container_of(listener, struct weston_seat,
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002465 saved_kbd_focus_listener);
2466
Daniel Stone37816df2012-05-16 18:45:18 +01002467 ws->saved_kbd_focus = NULL;
Ander Conselvan de Oliveira71b1d0c2012-03-16 17:25:11 +02002468}
2469
Kristian Høgsberg1c562182011-05-02 22:09:20 -04002470WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002471notify_keyboard_focus_in(struct weston_seat *seat, struct wl_array *keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002472 enum weston_key_state_update update_state)
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002473{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002474 struct weston_compositor *compositor = seat->compositor;
2475 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergafee2212012-03-20 17:28:20 -04002476 struct wl_surface *surface;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002477 uint32_t *k, serial;
Kristian Høgsbergf992b2f2011-01-28 15:53:07 -05002478
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002479 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002480 wl_array_copy(&keyboard->keys, keys);
2481 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002482 weston_compositor_idle_inhibit(compositor);
2483 if (update_state == STATE_UPDATE_AUTOMATIC)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002484 update_modifier_state(seat, serial, *k,
Daniel Stoned6da09e2012-06-22 13:21:29 +01002485 WL_KEYBOARD_KEY_STATE_PRESSED);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002486 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002487
Daniel Stoneef172672012-06-22 13:21:32 +01002488 /* Run key bindings after we've updated the state. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002489 wl_array_for_each(k, &keyboard->keys) {
2490 weston_compositor_run_key_binding(compositor, seat, 0, *k,
Daniel Stoneef172672012-06-22 13:21:32 +01002491 WL_KEYBOARD_KEY_STATE_PRESSED);
2492 }
2493
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002494 surface = seat->saved_kbd_focus;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002495
2496 if (surface) {
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002497 wl_list_remove(&seat->saved_kbd_focus_listener.link);
2498 wl_keyboard_set_focus(keyboard, surface);
2499 seat->saved_kbd_focus = NULL;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002500 }
2501}
2502
2503WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002504notify_keyboard_focus_out(struct weston_seat *seat)
Daniel Stoned6da09e2012-06-22 13:21:29 +01002505{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002506 struct weston_compositor *compositor = seat->compositor;
2507 struct wl_keyboard *keyboard = seat->seat.keyboard;
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002508 uint32_t *k, serial;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002509
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002510 serial = wl_display_next_serial(compositor->wl_display);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002511 wl_array_for_each(k, &keyboard->keys) {
Daniel Stoned6da09e2012-06-22 13:21:29 +01002512 weston_compositor_idle_release(compositor);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002513 update_modifier_state(seat, serial, *k,
Kristian Høgsbergc7cd6262012-06-28 13:46:09 -04002514 WL_KEYBOARD_KEY_STATE_RELEASED);
2515 }
Daniel Stoned6da09e2012-06-22 13:21:29 +01002516
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002517 seat->modifier_state = 0;
Daniel Stoned6da09e2012-06-22 13:21:29 +01002518
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002519 if (keyboard->focus) {
2520 seat->saved_kbd_focus = keyboard->focus;
2521 seat->saved_kbd_focus_listener.notify =
Daniel Stoned6da09e2012-06-22 13:21:29 +01002522 destroy_device_saved_kbd_focus;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002523 wl_signal_add(&keyboard->focus->resource.destroy_signal,
2524 &seat->saved_kbd_focus_listener);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002525 }
2526
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002527 wl_keyboard_set_focus(keyboard, NULL);
Daniel Stoned6da09e2012-06-22 13:21:29 +01002528 /* FIXME: We really need keyboard grab cancel here to
2529 * let the grab shut down properly. As it is we leak
2530 * the grab data. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002531 wl_keyboard_end_grab(keyboard);
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002532}
2533
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002534static void
Daniel Stone37816df2012-05-16 18:45:18 +01002535touch_set_focus(struct weston_seat *ws, struct wl_surface *surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002536{
Daniel Stone37816df2012-05-16 18:45:18 +01002537 struct wl_seat *seat = &ws->seat;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002538 struct wl_resource *resource;
2539
Pekka Paalanen56464252012-07-31 13:21:08 +03002540 if (seat->touch->focus == surface)
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002541 return;
2542
Pekka Paalanen56464252012-07-31 13:21:08 +03002543 if (seat->touch->focus_resource)
2544 wl_list_remove(&seat->touch->focus_listener.link);
2545 seat->touch->focus = NULL;
2546 seat->touch->focus_resource = NULL;
2547
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002548 if (surface) {
2549 resource =
Daniel Stone37816df2012-05-16 18:45:18 +01002550 find_resource_for_client(&seat->touch->resource_list,
Casey Dahlin96d8a752012-04-19 22:50:07 -04002551 surface->resource.client);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002552 if (!resource) {
Martin Minarik6d118362012-06-07 18:01:59 +02002553 weston_log("couldn't find resource\n");
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002554 return;
2555 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002556
Daniel Stone37816df2012-05-16 18:45:18 +01002557 seat->touch->focus = surface;
2558 seat->touch->focus_resource = resource;
Pekka Paalanen56464252012-07-31 13:21:08 +03002559 wl_signal_add(&resource->destroy_signal,
2560 &seat->touch->focus_listener);
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002561 }
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002562}
2563
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002564/**
2565 * notify_touch - emulates button touches and notifies surfaces accordingly.
2566 *
2567 * It assumes always the correct cycle sequence until it gets here: touch_down
2568 * → touch_update → ... → touch_update → touch_end. The driver is responsible
2569 * for sending along such order.
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002570 *
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002571 */
2572WL_EXPORT void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002573notify_touch(struct weston_seat *seat, uint32_t time, int touch_id,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002574 wl_fixed_t x, wl_fixed_t y, int touch_type)
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002575{
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002576 struct weston_compositor *ec = seat->compositor;
2577 struct wl_touch *touch = seat->seat.touch;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002578 struct weston_surface *es;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002579 wl_fixed_t sx, sy;
Kristian Høgsbergeae5de72012-04-11 22:42:15 -04002580 uint32_t serial = 0;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002581
2582 switch (touch_type) {
Daniel Stone37816df2012-05-16 18:45:18 +01002583 case WL_TOUCH_DOWN:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002584 weston_compositor_idle_inhibit(ec);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002585
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002586 seat->num_tp++;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002587
2588 /* the first finger down picks the surface, and all further go
2589 * to that surface for the remainder of the touch session i.e.
2590 * until all touch points are up again. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002591 if (seat->num_tp == 1) {
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002592 es = weston_compositor_pick_surface(ec, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002593 touch_set_focus(seat, &es->surface);
2594 } else if (touch->focus) {
2595 es = (struct weston_surface *) touch->focus;
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002596 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002597 }
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002598
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002599 if (touch->focus_resource && touch->focus)
2600 wl_touch_send_down(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002601 serial, time,
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002602 &touch->focus->resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002603 touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002604 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002605 case WL_TOUCH_MOTION:
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002606 es = (struct weston_surface *) touch->focus;
Kristian Høgsberg03cb5cf2011-12-22 14:43:09 -05002607 if (!es)
2608 break;
2609
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04002610 weston_surface_from_global_fixed(es, x, y, &sx, &sy);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002611 if (touch->focus_resource)
2612 wl_touch_send_motion(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002613 time, touch_id, sx, sy);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002614 break;
Daniel Stone37816df2012-05-16 18:45:18 +01002615 case WL_TOUCH_UP:
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05002616 weston_compositor_idle_release(ec);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002617 seat->num_tp--;
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002618
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002619 if (touch->focus_resource)
2620 wl_touch_send_up(touch->focus_resource,
Daniel Stone37816df2012-05-16 18:45:18 +01002621 serial, time, touch_id);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002622 if (seat->num_tp == 0)
2623 touch_set_focus(seat, NULL);
Tiago Vignatti1f221ff2011-12-21 19:34:10 +02002624 break;
2625 }
Tiago Vignatti22c6bce2011-12-21 19:34:09 +02002626}
Kristian Høgsberg3ba48582011-01-27 11:57:19 -05002627
Kristian Høgsberg77fb1672010-08-16 10:38:29 -04002628static void
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002629pointer_handle_sprite_destroy(struct wl_listener *listener, void *data)
2630{
2631 struct weston_seat *seat = container_of(listener, struct weston_seat,
2632 sprite_destroy_listener);
2633
2634 seat->sprite = NULL;
2635}
2636
2637static void
2638pointer_cursor_surface_configure(struct weston_surface *es,
2639 int32_t dx, int32_t dy)
2640{
2641 struct weston_seat *seat = es->private;
2642 int x, y;
2643
2644 assert(es == seat->sprite);
2645
2646 seat->hotspot_x -= dx;
2647 seat->hotspot_y -= dy;
2648
2649 x = wl_fixed_to_int(seat->seat.pointer->x) - seat->hotspot_x;
2650 y = wl_fixed_to_int(seat->seat.pointer->y) - seat->hotspot_y;
2651
2652 weston_surface_configure(seat->sprite, x, y,
2653 es->buffer->width, es->buffer->height);
2654
Ander Conselvan de Oliveiraf1c00c02012-07-04 15:48:29 +03002655 empty_region(&es->input);
2656
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002657 if (!weston_surface_is_mapped(es)) {
2658 wl_list_insert(&es->compositor->cursor_layer.surface_list,
2659 &es->layer_link);
2660 weston_surface_assign_output(es);
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002661 }
2662}
2663
2664static void
2665pointer_unmap_sprite(struct weston_seat *seat)
2666{
2667 if (weston_surface_is_mapped(seat->sprite))
2668 weston_surface_unmap(seat->sprite);
2669
2670 wl_list_remove(&seat->sprite_destroy_listener.link);
2671 seat->sprite->configure = NULL;
2672 seat->sprite->private = NULL;
2673 seat->sprite = NULL;
2674}
2675
2676static void
2677pointer_set_cursor(struct wl_client *client, struct wl_resource *resource,
2678 uint32_t serial, struct wl_resource *surface_resource,
2679 int32_t x, int32_t y)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002680{
Daniel Stone37816df2012-05-16 18:45:18 +01002681 struct weston_seat *seat = resource->data;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002682 struct weston_surface *surface = NULL;
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002683
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002684 if (surface_resource)
2685 surface = container_of(surface_resource->data,
2686 struct weston_surface, surface);
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04002687
Kristian Høgsberg649e1ce2012-08-01 09:46:12 -04002688 if (seat->seat.pointer->focus == NULL)
2689 return;
2690 if (seat->seat.pointer->focus->resource.client != client)
2691 return;
Kristian Høgsberg2e96d3c2012-08-01 09:39:11 -04002692 if (seat->seat.pointer->focus_serial - serial > UINT32_MAX / 2)
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002693 return;
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002694
2695 if (surface && surface != seat->sprite) {
Ander Conselvan de Oliveira1fbda0e2012-06-28 18:08:04 +03002696 if (surface->configure) {
2697 wl_resource_post_error(&surface->surface.resource,
2698 WL_DISPLAY_ERROR_INVALID_OBJECT,
2699 "surface->configure already "
2700 "set");
2701 return;
2702 }
Pekka Paalanenf07cb5d2012-02-10 13:34:36 +02002703 }
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002704
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002705 if (seat->sprite)
2706 pointer_unmap_sprite(seat);
Ander Conselvan de Oliveirae11683a2012-03-27 17:36:40 +03002707
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002708 if (!surface)
2709 return;
2710
2711 wl_signal_add(&surface->surface.resource.destroy_signal,
2712 &seat->sprite_destroy_listener);
2713
2714 surface->configure = pointer_cursor_surface_configure;
2715 surface->private = seat;
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002716 seat->sprite = surface;
Daniel Stone37816df2012-05-16 18:45:18 +01002717 seat->hotspot_x = x;
2718 seat->hotspot_y = y;
Kristian Høgsbergcd9ac1d2011-12-15 09:14:34 -05002719
Kristian Høgsbergd3800e42012-08-13 18:14:15 -04002720 if (surface->buffer)
2721 pointer_cursor_surface_configure(surface, 0, 0);
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002722}
2723
Daniel Stone37816df2012-05-16 18:45:18 +01002724static const struct wl_pointer_interface pointer_interface = {
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03002725 pointer_set_cursor
Kristian Høgsbergab1862d2010-12-09 11:29:40 -05002726};
2727
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002728static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002729handle_drag_surface_destroy(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002730{
Daniel Stone37816df2012-05-16 18:45:18 +01002731 struct weston_seat *seat;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002732
Daniel Stone37816df2012-05-16 18:45:18 +01002733 seat = container_of(listener, struct weston_seat,
2734 drag_surface_destroy_listener);
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002735
Daniel Stone37816df2012-05-16 18:45:18 +01002736 seat->drag_surface = NULL;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02002737}
2738
Casey Dahlin9074db52012-04-19 22:50:09 -04002739static void unbind_resource(struct wl_resource *resource)
Kristian Høgsbergd2baf1f2011-10-11 22:20:37 -04002740{
2741 wl_list_remove(&resource->link);
2742 free(resource);
2743}
2744
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002745static void
Daniel Stone37816df2012-05-16 18:45:18 +01002746seat_get_pointer(struct wl_client *client, struct wl_resource *resource,
2747 uint32_t id)
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002748{
Daniel Stone37816df2012-05-16 18:45:18 +01002749 struct weston_seat *seat = resource->data;
2750 struct wl_resource *cr;
Kristian Høgsberg8e6d7122011-08-29 16:04:39 -04002751
Daniel Stone37816df2012-05-16 18:45:18 +01002752 if (!seat->seat.pointer)
2753 return;
2754
2755 cr = wl_client_add_object(client, &wl_pointer_interface,
2756 &pointer_interface, id, seat);
2757 wl_list_insert(&seat->seat.pointer->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002758 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002759
2760 if (seat->seat.pointer->focus &&
2761 seat->seat.pointer->focus->resource.client == client) {
2762 struct weston_surface *surface;
2763 wl_fixed_t sx, sy;
2764
2765 surface = (struct weston_surface *) seat->seat.pointer->focus;
2766 weston_surface_from_global_fixed(surface,
2767 seat->seat.pointer->x,
2768 seat->seat.pointer->y,
2769 &sx,
2770 &sy);
2771 wl_pointer_set_focus(seat->seat.pointer,
2772 seat->seat.pointer->focus,
2773 sx,
2774 sy);
2775 }
Daniel Stone37816df2012-05-16 18:45:18 +01002776}
2777
2778static void
2779seat_get_keyboard(struct wl_client *client, struct wl_resource *resource,
2780 uint32_t id)
2781{
2782 struct weston_seat *seat = resource->data;
2783 struct wl_resource *cr;
2784
2785 if (!seat->seat.keyboard)
2786 return;
2787
2788 cr = wl_client_add_object(client, &wl_keyboard_interface, NULL, id,
2789 seat);
2790 wl_list_insert(&seat->seat.keyboard->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002791 cr->destroy = unbind_resource;
Daniel Stone17b13bd2012-05-30 16:31:39 +01002792
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002793 wl_keyboard_send_keymap(cr, WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1,
2794 seat->xkb_info.keymap_fd,
2795 seat->xkb_info.keymap_size);
2796
Daniel Stone17b13bd2012-05-30 16:31:39 +01002797 if (seat->seat.keyboard->focus &&
2798 seat->seat.keyboard->focus->resource.client == client) {
2799 wl_keyboard_set_focus(seat->seat.keyboard,
2800 seat->seat.keyboard->focus);
2801 }
Daniel Stone37816df2012-05-16 18:45:18 +01002802}
2803
2804static void
2805seat_get_touch(struct wl_client *client, struct wl_resource *resource,
2806 uint32_t id)
2807{
2808 struct weston_seat *seat = resource->data;
2809 struct wl_resource *cr;
2810
2811 if (!seat->seat.touch)
2812 return;
2813
2814 cr = wl_client_add_object(client, &wl_touch_interface, NULL, id, seat);
2815 wl_list_insert(&seat->seat.touch->resource_list, &cr->link);
Kristian Høgsbergb93b6cf2012-05-16 22:32:40 -04002816 cr->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002817}
2818
2819static const struct wl_seat_interface seat_interface = {
2820 seat_get_pointer,
2821 seat_get_keyboard,
2822 seat_get_touch,
2823};
2824
2825static void
2826bind_seat(struct wl_client *client, void *data, uint32_t version, uint32_t id)
2827{
2828 struct wl_seat *seat = data;
2829 struct wl_resource *resource;
2830 enum wl_seat_capability caps = 0;
2831
2832 resource = wl_client_add_object(client, &wl_seat_interface,
2833 &seat_interface, id, data);
2834 wl_list_insert(&seat->base_resource_list, &resource->link);
Casey Dahlin9074db52012-04-19 22:50:09 -04002835 resource->destroy = unbind_resource;
Daniel Stone37816df2012-05-16 18:45:18 +01002836
2837 if (seat->pointer)
2838 caps |= WL_SEAT_CAPABILITY_POINTER;
2839 if (seat->keyboard)
2840 caps |= WL_SEAT_CAPABILITY_KEYBOARD;
2841 if (seat->touch)
2842 caps |= WL_SEAT_CAPABILITY_TOUCH;
2843
2844 wl_seat_send_capabilities(resource, caps);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04002845}
2846
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002847static void
Kristian Høgsberg27e30522012-04-11 23:18:23 -04002848device_handle_new_drag_icon(struct wl_listener *listener, void *data)
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002849{
Daniel Stone37816df2012-05-16 18:45:18 +01002850 struct weston_seat *seat;
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002851
Daniel Stone37816df2012-05-16 18:45:18 +01002852 seat = container_of(listener, struct weston_seat,
2853 new_drag_icon_listener);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002854
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04002855 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03002856}
2857
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002858static void weston_compositor_xkb_init(struct weston_compositor *ec,
2859 struct xkb_rule_names *names)
Daniel Stone994679a2012-05-30 16:31:43 +01002860{
Daniel Stonee379da92012-05-30 16:32:04 +01002861 if (ec->xkb_context == NULL) {
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002862 ec->xkb_context = xkb_context_new(0);
2863 if (ec->xkb_context == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002864 weston_log("failed to create XKB context\n");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002865 exit(1);
2866 }
Daniel Stone994679a2012-05-30 16:31:43 +01002867 }
2868
2869 if (names)
Daniel Stonee379da92012-05-30 16:32:04 +01002870 ec->xkb_names = *names;
2871 if (!ec->xkb_names.rules)
2872 ec->xkb_names.rules = strdup("evdev");
2873 if (!ec->xkb_names.model)
2874 ec->xkb_names.model = strdup("pc105");
2875 if (!ec->xkb_names.layout)
2876 ec->xkb_names.layout = strdup("us");
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002877}
2878
Daniel Stonee379da92012-05-30 16:32:04 +01002879static void xkb_info_destroy(struct weston_xkb_info *xkb_info)
2880{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002881 if (xkb_info->keymap)
2882 xkb_map_unref(xkb_info->keymap);
2883
2884 if (xkb_info->keymap_area)
2885 munmap(xkb_info->keymap_area, xkb_info->keymap_size);
2886 if (xkb_info->keymap_fd >= 0)
2887 close(xkb_info->keymap_fd);
Daniel Stonee379da92012-05-30 16:32:04 +01002888}
2889
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002890static void weston_compositor_xkb_destroy(struct weston_compositor *ec)
2891{
Daniel Stonee379da92012-05-30 16:32:04 +01002892 free((char *) ec->xkb_names.rules);
2893 free((char *) ec->xkb_names.model);
2894 free((char *) ec->xkb_names.layout);
2895 free((char *) ec->xkb_names.variant);
2896 free((char *) ec->xkb_names.options);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002897
Daniel Stonee379da92012-05-30 16:32:04 +01002898 xkb_info_destroy(&ec->xkb_info);
2899 xkb_context_unref(ec->xkb_context);
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002900}
2901
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002902static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002903weston_xkb_info_new_keymap(struct weston_xkb_info *xkb_info)
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002904{
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002905 char *keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002906
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002907 xkb_info->shift_mod = xkb_map_mod_get_index(xkb_info->keymap,
2908 XKB_MOD_NAME_SHIFT);
2909 xkb_info->caps_mod = xkb_map_mod_get_index(xkb_info->keymap,
2910 XKB_MOD_NAME_CAPS);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002911 xkb_info->ctrl_mod = xkb_map_mod_get_index(xkb_info->keymap,
2912 XKB_MOD_NAME_CTRL);
2913 xkb_info->alt_mod = xkb_map_mod_get_index(xkb_info->keymap,
2914 XKB_MOD_NAME_ALT);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002915 xkb_info->mod2_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod2");
2916 xkb_info->mod3_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod3");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002917 xkb_info->super_mod = xkb_map_mod_get_index(xkb_info->keymap,
2918 XKB_MOD_NAME_LOGO);
Daniel Stoneabb9dcd2012-06-22 13:21:33 +01002919 xkb_info->mod5_mod = xkb_map_mod_get_index(xkb_info->keymap, "Mod5");
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002920
2921 xkb_info->num_led = xkb_map_led_get_index(xkb_info->keymap,
2922 XKB_LED_NAME_NUM);
2923 xkb_info->caps_led = xkb_map_led_get_index(xkb_info->keymap,
2924 XKB_LED_NAME_CAPS);
2925 xkb_info->scroll_led = xkb_map_led_get_index(xkb_info->keymap,
2926 XKB_LED_NAME_SCROLL);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002927
2928 keymap_str = xkb_map_get_as_string(xkb_info->keymap);
2929 if (keymap_str == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002930 weston_log("failed to get string version of keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002931 exit(EXIT_FAILURE);
2932 }
2933 xkb_info->keymap_size = strlen(keymap_str) + 1;
2934
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002935 xkb_info->keymap_fd = os_create_anonymous_file(xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002936 if (xkb_info->keymap_fd < 0) {
Martin Minarik6d118362012-06-07 18:01:59 +02002937 weston_log("creating a keymap file for %lu bytes failed: %m\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002938 (unsigned long) xkb_info->keymap_size);
2939 goto err_keymap_str;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002940 }
2941
2942 xkb_info->keymap_area = mmap(NULL, xkb_info->keymap_size,
2943 PROT_READ | PROT_WRITE,
2944 MAP_SHARED, xkb_info->keymap_fd, 0);
2945 if (xkb_info->keymap_area == MAP_FAILED) {
Martin Minarik6d118362012-06-07 18:01:59 +02002946 weston_log("failed to mmap() %lu bytes\n",
Pekka Paalanen1da1b8f2012-06-06 16:59:43 +03002947 (unsigned long) xkb_info->keymap_size);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002948 goto err_dev_zero;
2949 }
2950 strcpy(xkb_info->keymap_area, keymap_str);
2951 free(keymap_str);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002952
2953 return;
2954
2955err_dev_zero:
2956 close(xkb_info->keymap_fd);
2957 xkb_info->keymap_fd = -1;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002958err_keymap_str:
2959 free(keymap_str);
2960 exit(EXIT_FAILURE);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002961}
2962
2963static void
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002964weston_compositor_build_global_keymap(struct weston_compositor *ec)
2965{
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002966 if (ec->xkb_info.keymap != NULL)
2967 return;
2968
Daniel Stonee379da92012-05-30 16:32:04 +01002969 ec->xkb_info.keymap = xkb_map_new_from_names(ec->xkb_context,
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002970 &ec->xkb_names,
2971 0);
Daniel Stone994679a2012-05-30 16:31:43 +01002972 if (ec->xkb_info.keymap == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02002973 weston_log("failed to compile global XKB keymap\n");
2974 weston_log(" tried rules %s, model %s, layout %s, variant %s, "
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002975 "options %s",
2976 ec->xkb_names.rules, ec->xkb_names.model,
2977 ec->xkb_names.layout, ec->xkb_names.variant,
2978 ec->xkb_names.options);
2979 exit(1);
Daniel Stone994679a2012-05-30 16:31:43 +01002980 }
2981
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002982 weston_xkb_info_new_keymap(&ec->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01002983}
2984
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002985WL_EXPORT void
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002986weston_seat_init_keyboard(struct weston_seat *seat, struct xkb_keymap *keymap)
Daniel Stone994679a2012-05-30 16:31:43 +01002987{
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01002988 if (seat->has_keyboard)
2989 return;
Daniel Stone994679a2012-05-30 16:31:43 +01002990
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002991 if (keymap != NULL) {
2992 seat->xkb_info.keymap = xkb_map_ref(keymap);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01002993 weston_xkb_info_new_keymap(&seat->xkb_info);
Daniel Stonebb1df6a2012-05-30 16:32:06 +01002994 }
2995 else {
2996 weston_compositor_build_global_keymap(seat->compositor);
2997 seat->xkb_info = seat->compositor->xkb_info;
2998 seat->xkb_info.keymap = xkb_map_ref(seat->xkb_info.keymap);
2999 }
Daniel Stoned65b9092012-05-30 16:32:05 +01003000
3001 seat->xkb_state.state = xkb_state_new(seat->xkb_info.keymap);
3002 if (seat->xkb_state.state == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02003003 weston_log("failed to initialise XKB state\n");
Daniel Stoned65b9092012-05-30 16:32:05 +01003004 exit(1);
3005 }
3006
Daniel Stone71c38772012-06-22 13:21:30 +01003007 seat->xkb_state.leds = 0;
Daniel Stoned65b9092012-05-30 16:32:05 +01003008
Daniel Stone9a9ee2c2012-05-30 16:32:03 +01003009 wl_keyboard_init(&seat->keyboard);
3010 wl_seat_set_keyboard(&seat->seat, &seat->keyboard);
3011
3012 seat->has_keyboard = 1;
Daniel Stone994679a2012-05-30 16:31:43 +01003013}
3014
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003015WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003016weston_seat_init_pointer(struct weston_seat *seat)
3017{
3018 if (seat->has_pointer)
3019 return;
3020
3021 wl_pointer_init(&seat->pointer);
3022 wl_seat_set_pointer(&seat->seat, &seat->pointer);
3023
3024 seat->has_pointer = 1;
3025}
3026
3027WL_EXPORT void
Daniel Stone74419a22012-05-30 16:32:02 +01003028weston_seat_init_touch(struct weston_seat *seat)
3029{
3030 if (seat->has_touch)
3031 return;
3032
3033 wl_touch_init(&seat->touch);
3034 wl_seat_set_touch(&seat->seat, &seat->touch);
3035
3036 seat->has_touch = 1;
3037}
3038
3039WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003040weston_seat_init(struct weston_seat *seat, struct weston_compositor *ec)
Kristian Høgsbergcddc0ad2008-11-24 00:31:49 -05003041{
Daniel Stone37816df2012-05-16 18:45:18 +01003042 wl_seat_init(&seat->seat);
Daniel Stone74419a22012-05-30 16:32:02 +01003043 seat->has_pointer = 0;
3044 seat->has_keyboard = 0;
3045 seat->has_touch = 0;
Kristian Høgsberg02ef1c12010-12-06 21:35:19 -05003046
Daniel Stone37816df2012-05-16 18:45:18 +01003047 wl_display_add_global(ec->wl_display, &wl_seat_interface, seat,
3048 bind_seat);
Kristian Høgsberg5fcd0aa2010-08-09 14:43:33 -04003049
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003050 seat->sprite = NULL;
3051 seat->sprite_destroy_listener.notify = pointer_handle_sprite_destroy;
Kristian Høgsberg8244b442011-06-23 15:44:14 -04003052
Daniel Stone37816df2012-05-16 18:45:18 +01003053 seat->compositor = ec;
3054 seat->hotspot_x = 16;
3055 seat->hotspot_y = 16;
3056 seat->modifier_state = 0;
3057 seat->num_tp = 0;
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003058
Daniel Stone37816df2012-05-16 18:45:18 +01003059 seat->drag_surface_destroy_listener.notify =
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003060 handle_drag_surface_destroy;
Ander Conselvan de Oliveirac5fb9a72012-03-01 14:09:44 +02003061
Daniel Stone37816df2012-05-16 18:45:18 +01003062 wl_list_insert(ec->seat_list.prev, &seat->link);
Ander Conselvan de Oliveirad6ea33d2012-03-27 17:36:39 +03003063
Daniel Stone37816df2012-05-16 18:45:18 +01003064 seat->new_drag_icon_listener.notify = device_handle_new_drag_icon;
3065 wl_signal_add(&seat->seat.drag_icon_signal,
3066 &seat->new_drag_icon_listener);
Kristian Høgsberga7ceaff2012-06-03 10:20:13 -04003067
3068 clipboard_create(seat);
Jan Arne Petersene829adc2012-08-10 16:47:22 +02003069 input_method_create(ec, seat);
Kristian Høgsberg5ee1a602008-12-11 23:18:45 -05003070}
3071
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003072WL_EXPORT void
Daniel Stone37816df2012-05-16 18:45:18 +01003073weston_seat_release(struct weston_seat *seat)
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003074{
Daniel Stone37816df2012-05-16 18:45:18 +01003075 wl_list_remove(&seat->link);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003076 /* The global object is destroyed at wl_display_destroy() time. */
3077
Daniel Stone37816df2012-05-16 18:45:18 +01003078 if (seat->sprite)
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003079 pointer_unmap_sprite(seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003080
Daniel Stone74419a22012-05-30 16:32:02 +01003081 if (seat->xkb_state.state != NULL)
3082 xkb_state_unref(seat->xkb_state.state);
Daniel Stoned65b9092012-05-30 16:32:05 +01003083 xkb_info_destroy(&seat->xkb_info);
Daniel Stone994679a2012-05-30 16:31:43 +01003084
Daniel Stone37816df2012-05-16 18:45:18 +01003085 wl_seat_release(&seat->seat);
Pekka Paalanen07753fb2012-01-02 15:31:01 +02003086}
3087
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003088static void
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003089drag_surface_configure(struct weston_surface *es, int32_t sx, int32_t sy)
3090{
3091 weston_surface_configure(es,
3092 es->geometry.x + sx, es->geometry.y + sy,
3093 es->buffer->width, es->buffer->height);
3094}
3095
3096static int
Daniel Stone37816df2012-05-16 18:45:18 +01003097device_setup_new_drag_surface(struct weston_seat *ws,
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003098 struct weston_surface *surface)
3099{
Daniel Stone37816df2012-05-16 18:45:18 +01003100 struct wl_seat *seat = &ws->seat;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003101
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003102 if (surface->configure) {
3103 wl_resource_post_error(&surface->surface.resource,
3104 WL_DISPLAY_ERROR_INVALID_OBJECT,
3105 "surface->configure already set");
3106 return 0;
3107 }
3108
Daniel Stone37816df2012-05-16 18:45:18 +01003109 ws->drag_surface = surface;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003110
Daniel Stone37816df2012-05-16 18:45:18 +01003111 weston_surface_set_position(ws->drag_surface,
3112 wl_fixed_to_double(seat->pointer->x),
3113 wl_fixed_to_double(seat->pointer->y));
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003114
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003115 surface->configure = drag_surface_configure;
3116
Kristian Høgsberg27e30522012-04-11 23:18:23 -04003117 wl_signal_add(&surface->surface.resource.destroy_signal,
Daniel Stone37816df2012-05-16 18:45:18 +01003118 &ws->drag_surface_destroy_listener);
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003119
3120 return 1;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003121}
3122
3123static void
Daniel Stone37816df2012-05-16 18:45:18 +01003124device_release_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003125{
Daniel Stone37816df2012-05-16 18:45:18 +01003126 seat->drag_surface->configure = NULL;
3127 undef_region(&seat->drag_surface->input);
3128 wl_list_remove(&seat->drag_surface_destroy_listener.link);
3129 seat->drag_surface = NULL;
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003130}
3131
3132static void
Daniel Stone37816df2012-05-16 18:45:18 +01003133device_map_drag_surface(struct weston_seat *seat)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003134{
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003135 struct wl_list *list;
3136
Daniel Stone37816df2012-05-16 18:45:18 +01003137 if (weston_surface_is_mapped(seat->drag_surface) ||
3138 !seat->drag_surface->buffer)
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003139 return;
3140
Ander Conselvan de Oliveira37ffc3c2012-06-15 17:27:35 +03003141 if (seat->sprite && weston_surface_is_mapped(seat->sprite))
3142 list = &seat->sprite->layer_link;
3143 else
3144 list = &seat->compositor->cursor_layer.surface_list;
3145
3146 wl_list_insert(list, &seat->drag_surface->layer_link);
Daniel Stone37816df2012-05-16 18:45:18 +01003147 weston_surface_assign_output(seat->drag_surface);
3148 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003149}
3150
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003151static void
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003152weston_seat_update_drag_surface(struct weston_seat *seat,
Daniel Stone37816df2012-05-16 18:45:18 +01003153 int dx, int dy)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003154{
3155 int surface_changed = 0;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003156
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003157 if (!seat->drag_surface && !seat->seat.drag_surface)
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003158 return;
3159
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003160 if (seat->drag_surface && seat->seat.drag_surface &&
3161 (&seat->drag_surface->surface.resource !=
3162 &seat->seat.drag_surface->resource))
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003163 /* between calls to this funcion we got a new drag_surface */
3164 surface_changed = 1;
3165
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003166 if (!seat->seat.drag_surface || surface_changed) {
3167 device_release_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003168 if (!surface_changed)
3169 return;
3170 }
3171
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003172 if (!seat->drag_surface || surface_changed) {
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003173 struct weston_surface *surface = (struct weston_surface *)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003174 seat->seat.drag_surface;
3175 if (!device_setup_new_drag_surface(seat, surface))
Ander Conselvan de Oliveira093bfa32012-03-27 17:36:41 +03003176 return;
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003177 }
3178
Ander Conselvan de Oliveira3e3bb322012-03-01 14:09:44 +02003179 /* the client may not have attached a buffer to the drag surface
3180 * when we setup it up, so check if map is needed on every update */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003181 device_map_drag_surface(seat);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003182
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003183 /* the client may have attached a buffer with a different size to
3184 * the drag surface, causing the input region to be reset */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003185 if (region_is_undefined(&seat->drag_surface->input))
3186 empty_region(&seat->drag_surface->input);
Ander Conselvan de Oliveira90fbbd72012-02-28 17:59:33 +02003187
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003188 if (!dx && !dy)
3189 return;
3190
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003191 weston_surface_set_position(seat->drag_surface,
3192 seat->drag_surface->geometry.x + wl_fixed_to_double(dx),
3193 seat->drag_surface->geometry.y + wl_fixed_to_double(dy));
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003194}
3195
3196WL_EXPORT void
3197weston_compositor_update_drag_surfaces(struct weston_compositor *compositor)
3198{
Daniel Stone4dab5db2012-05-30 16:31:53 +01003199 struct weston_seat *seat;
3200
3201 wl_list_for_each(seat, &compositor->seat_list, link)
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04003202 weston_seat_update_drag_surface(seat, 0, 0);
Ander Conselvan de Oliveira30eebc72012-02-15 17:02:57 +02003203}
3204
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003205static void
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -04003206bind_output(struct wl_client *client,
3207 void *data, uint32_t version, uint32_t id)
Kristian Høgsbergbf9541f2008-11-25 12:10:09 -05003208{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003209 struct weston_output *output = data;
3210 struct weston_mode *mode;
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003211 struct wl_resource *resource;
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003212
Kristian Høgsbergfd07fb72011-08-29 15:03:09 -04003213 resource = wl_client_add_object(client,
3214 &wl_output_interface, NULL, id, data);
3215
Casey Dahlin9074db52012-04-19 22:50:09 -04003216 wl_list_insert(&output->resource_list, &resource->link);
3217 resource->destroy = unbind_resource;
3218
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003219 wl_output_send_geometry(resource,
3220 output->x,
3221 output->y,
3222 output->mm_width,
3223 output->mm_height,
3224 output->subpixel,
Kristian Høgsberg0e696472012-07-22 15:49:57 -04003225 output->make, output->model,
Kristian Høgsberg05890dc2012-08-10 10:09:20 -04003226 output->transform);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003227
3228 wl_list_for_each (mode, &output->mode_list, link) {
Kristian Høgsberg0b5cd0c2012-03-04 21:57:37 -05003229 wl_output_send_mode(resource,
3230 mode->flags,
3231 mode->width,
3232 mode->height,
3233 mode->refresh);
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003234 }
Kristian Høgsberg81ce09a2008-12-31 16:18:42 -05003235}
3236
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003237static const char vertex_shader[] =
3238 "uniform mat4 proj;\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003239 "attribute vec2 position;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003240 "attribute vec2 texcoord;\n"
3241 "varying vec2 v_texcoord;\n"
3242 "void main()\n"
3243 "{\n"
Kristian Høgsbergfa4e2a72011-02-13 13:44:55 -05003244 " gl_Position = proj * vec4(position, 0.0, 1.0);\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003245 " v_texcoord = texcoord;\n"
3246 "}\n";
3247
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003248/* Declare common fragment shader uniforms */
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003249#define FRAGMENT_CONVERT_YUV \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003250 " y *= alpha;\n" \
3251 " u *= alpha;\n" \
3252 " v *= alpha;\n" \
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003253 " gl_FragColor.r = y + 1.59602678 * v;\n" \
3254 " gl_FragColor.g = y - 0.39176229 * u - 0.81296764 * v;\n" \
3255 " gl_FragColor.b = y + 2.01723214 * u;\n" \
Rob Clark0e5a2d02012-08-30 16:47:18 -05003256 " gl_FragColor.a = alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003257
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003258static const char texture_fragment_shader_rgba[] =
Kristian Høgsbergdfce71d2010-12-07 20:19:10 -05003259 "precision mediump float;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003260 "varying vec2 v_texcoord;\n"
3261 "uniform sampler2D tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003262 "uniform float alpha;\n"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003263 "void main()\n"
3264 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003265 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003266 "}\n";
3267
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003268static const char texture_fragment_shader_rgbx[] =
3269 "precision mediump float;\n"
3270 "varying vec2 v_texcoord;\n"
3271 "uniform sampler2D tex;\n"
3272 "uniform float alpha;\n"
3273 "void main()\n"
3274 "{\n"
3275 " gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb\n;"
3276 " gl_FragColor.a = alpha;\n"
3277 "}\n";
3278
Rob Clarke3b95912012-08-31 16:42:18 -05003279static const char texture_fragment_shader_egl_external[] =
3280 "#extension GL_OES_EGL_image_external : require\n"
3281 "precision mediump float;\n"
3282 "varying vec2 v_texcoord;\n"
3283 "uniform samplerExternalOES tex;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003284 "uniform float alpha;\n"
Rob Clarke3b95912012-08-31 16:42:18 -05003285 "void main()\n"
3286 "{\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003287 " gl_FragColor = alpha * texture2D(tex, v_texcoord)\n;"
Rob Clarke3b95912012-08-31 16:42:18 -05003288 "}\n";
3289
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003290static const char texture_fragment_shader_y_uv[] =
3291 "precision mediump float;\n"
3292 "uniform sampler2D tex;\n"
3293 "uniform sampler2D tex1;\n"
3294 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003295 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003296 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003297 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3298 " float u = texture2D(tex1, v_texcoord).r - 0.5;\n"
3299 " float v = texture2D(tex1, v_texcoord).g - 0.5;\n"
3300 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003301 "}\n";
3302
3303static const char texture_fragment_shader_y_u_v[] =
3304 "precision mediump float;\n"
3305 "uniform sampler2D tex;\n"
3306 "uniform sampler2D tex1;\n"
3307 "uniform sampler2D tex2;\n"
3308 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003309 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003310 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003311 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3312 " float u = texture2D(tex1, v_texcoord).x - 0.5;\n"
3313 " float v = texture2D(tex2, v_texcoord).x - 0.5;\n"
3314 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003315 "}\n";
3316
3317static const char texture_fragment_shader_y_xuxv[] =
3318 "precision mediump float;\n"
3319 "uniform sampler2D tex;\n"
3320 "uniform sampler2D tex1;\n"
3321 "varying vec2 v_texcoord;\n"
Rob Clark0e5a2d02012-08-30 16:47:18 -05003322 "uniform float alpha;\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003323 "void main() {\n"
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003324 " float y = 1.16438356 * (texture2D(tex, v_texcoord).x - 0.0625);\n"
3325 " float u = texture2D(tex1, v_texcoord).g - 0.5;\n"
3326 " float v = texture2D(tex1, v_texcoord).a - 0.5;\n"
3327 FRAGMENT_CONVERT_YUV
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003328 "}\n";
3329
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003330static const char solid_fragment_shader[] =
3331 "precision mediump float;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003332 "uniform vec4 color;\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003333 "uniform float alpha;\n"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003334 "void main()\n"
3335 "{\n"
Kristian Høgsbergdbae80e2012-03-29 11:34:39 -04003336 " gl_FragColor = alpha * color\n;"
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003337 "}\n";
3338
Kristian Høgsberga9468212010-06-14 21:03:11 -04003339static int
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003340compile_shader(GLenum type, const char *source)
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003341{
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003342 GLuint s;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003343 char msg[512];
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003344 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003345
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003346 s = glCreateShader(type);
3347 glShaderSource(s, 1, &source, NULL);
3348 glCompileShader(s);
3349 glGetShaderiv(s, GL_COMPILE_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003350 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003351 glGetShaderInfoLog(s, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003352 weston_log("shader info: %s\n", msg);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003353 return GL_NONE;
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003354 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003355
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003356 return s;
3357}
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003358
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003359static int
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003360weston_shader_init(struct weston_shader *shader,
3361 const char *vertex_source, const char *fragment_source)
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003362{
3363 char msg[512];
3364 GLint status;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003365
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003366 shader->vertex_shader =
3367 compile_shader(GL_VERTEX_SHADER, vertex_source);
3368 shader->fragment_shader =
3369 compile_shader(GL_FRAGMENT_SHADER, fragment_source);
3370
3371 shader->program = glCreateProgram();
3372 glAttachShader(shader->program, shader->vertex_shader);
3373 glAttachShader(shader->program, shader->fragment_shader);
3374 glBindAttribLocation(shader->program, 0, "position");
3375 glBindAttribLocation(shader->program, 1, "texcoord");
3376
3377 glLinkProgram(shader->program);
3378 glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003379 if (!status) {
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003380 glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
Martin Minarik6d118362012-06-07 18:01:59 +02003381 weston_log("link info: %s\n", msg);
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003382 return -1;
3383 }
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003384
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003385 shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003386 shader->tex_uniforms[0] = glGetUniformLocation(shader->program, "tex");
3387 shader->tex_uniforms[1] = glGetUniformLocation(shader->program, "tex1");
3388 shader->tex_uniforms[2] = glGetUniformLocation(shader->program, "tex2");
Kristian Høgsberg541e5552011-12-14 09:24:11 -05003389 shader->alpha_uniform = glGetUniformLocation(shader->program, "alpha");
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003390 shader->color_uniform = glGetUniformLocation(shader->program, "color");
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003391
Kristian Høgsberg67a21bd2010-06-25 18:58:24 -04003392 return 0;
Kristian Høgsberg27803c62010-06-06 22:23:21 -04003393}
3394
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003395WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003396weston_output_destroy(struct weston_output *output)
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003397{
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003398 struct weston_compositor *c = output->compositor;
3399
Kristian Høgsberge75cb7f2011-06-21 15:27:41 -04003400 pixman_region32_fini(&output->region);
Kristian Høgsberg8b72f602011-06-23 20:46:34 -04003401 pixman_region32_fini(&output->previous_damage);
Casey Dahlin9074db52012-04-19 22:50:09 -04003402 output->compositor->output_id_pool &= ~(1 << output->id);
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003403
3404 wl_display_remove_global(c->wl_display, output->global);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003405}
3406
Scott Moreau1bad5db2012-08-18 01:04:05 -06003407static void
3408weston_output_compute_transform(struct weston_output *output)
3409{
3410 struct weston_matrix transform;
3411 int flip;
3412
3413 weston_matrix_init(&transform);
3414
3415 switch(output->transform) {
3416 case WL_OUTPUT_TRANSFORM_FLIPPED:
3417 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3418 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3419 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3420 flip = -1;
3421 break;
3422 default:
3423 flip = 1;
3424 break;
3425 }
3426
3427 switch(output->transform) {
3428 case WL_OUTPUT_TRANSFORM_NORMAL:
3429 case WL_OUTPUT_TRANSFORM_FLIPPED:
3430 transform.d[0] = flip;
3431 transform.d[1] = 0;
3432 transform.d[4] = 0;
3433 transform.d[5] = 1;
3434 break;
3435 case WL_OUTPUT_TRANSFORM_90:
3436 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3437 transform.d[0] = 0;
3438 transform.d[1] = -flip;
3439 transform.d[4] = 1;
3440 transform.d[5] = 0;
3441 break;
3442 case WL_OUTPUT_TRANSFORM_180:
3443 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3444 transform.d[0] = -flip;
3445 transform.d[1] = 0;
3446 transform.d[4] = 0;
3447 transform.d[5] = -1;
3448 break;
3449 case WL_OUTPUT_TRANSFORM_270:
3450 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3451 transform.d[0] = 0;
3452 transform.d[1] = flip;
3453 transform.d[4] = -1;
3454 transform.d[5] = 0;
3455 break;
3456 default:
3457 break;
3458 }
3459
3460 weston_matrix_multiply(&output->matrix, &transform);
3461}
3462
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003463WL_EXPORT void
Scott Moreauccbf29d2012-02-22 14:21:41 -07003464weston_output_update_matrix(struct weston_output *output)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003465{
Scott Moreau850ca422012-05-21 15:21:25 -06003466 float magnification;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003467 struct weston_matrix camera;
3468 struct weston_matrix modelview;
Kristian Høgsberg31bd6c72011-02-13 13:00:51 -05003469
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003470 weston_matrix_init(&output->matrix);
3471 weston_matrix_translate(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003472 -(output->x + (output->border.right + output->width - output->border.left) / 2.0),
3473 -(output->y + (output->border.bottom + output->height - output->border.top) / 2.0), 0);
Benjamin Franzke1b765ff2011-02-18 16:51:37 +01003474
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003475 weston_matrix_scale(&output->matrix,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003476 2.0 / (output->width + output->border.left + output->border.right),
3477 -2.0 / (output->height + output->border.top + output->border.bottom), 1);
3478
3479 weston_output_compute_transform(output);
Scott Moreau850ca422012-05-21 15:21:25 -06003480
Scott Moreauccbf29d2012-02-22 14:21:41 -07003481 if (output->zoom.active) {
Scott Moreaue6603982012-06-11 13:07:51 -06003482 magnification = 1 / (1 - output->zoom.spring_z.current);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003483 weston_matrix_init(&camera);
3484 weston_matrix_init(&modelview);
Scott Moreau8dacaab2012-06-17 18:10:58 -06003485 weston_output_update_zoom(output, output->zoom.type);
Scott Moreaue6603982012-06-11 13:07:51 -06003486 weston_matrix_translate(&camera, output->zoom.trans_x,
Kristian Høgsberg99fd1012012-08-10 09:57:56 -04003487 -output->zoom.trans_y, 0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003488 weston_matrix_invert(&modelview, &camera);
Scott Moreau850ca422012-05-21 15:21:25 -06003489 weston_matrix_scale(&modelview, magnification, magnification, 1.0);
Scott Moreauccbf29d2012-02-22 14:21:41 -07003490 weston_matrix_multiply(&output->matrix, &modelview);
3491 }
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003492
Scott Moreauccbf29d2012-02-22 14:21:41 -07003493 output->dirty = 0;
3494}
3495
Scott Moreau1bad5db2012-08-18 01:04:05 -06003496static void
3497weston_output_transform_init(struct weston_output *output, uint32_t transform)
3498{
3499 output->transform = transform;
3500
3501 switch (transform) {
3502 case WL_OUTPUT_TRANSFORM_90:
3503 case WL_OUTPUT_TRANSFORM_270:
3504 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
3505 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
3506 /* Swap width and height */
3507 output->width = output->current->height;
3508 output->height = output->current->width;
3509 break;
3510 case WL_OUTPUT_TRANSFORM_NORMAL:
3511 case WL_OUTPUT_TRANSFORM_180:
3512 case WL_OUTPUT_TRANSFORM_FLIPPED:
3513 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
3514 output->width = output->current->width;
3515 output->height = output->current->height;
3516 break;
3517 default:
3518 break;
3519 }
3520}
3521
Scott Moreauccbf29d2012-02-22 14:21:41 -07003522WL_EXPORT void
3523weston_output_move(struct weston_output *output, int x, int y)
3524{
3525 output->x = x;
3526 output->y = y;
3527
3528 pixman_region32_init(&output->previous_damage);
3529 pixman_region32_init_rect(&output->region, x, y,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003530 output->width,
3531 output->height);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003532}
3533
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003534WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003535weston_output_init(struct weston_output *output, struct weston_compositor *c,
Scott Moreau1bad5db2012-08-18 01:04:05 -06003536 int x, int y, int width, int height, uint32_t transform)
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003537{
3538 output->compositor = c;
3539 output->x = x;
3540 output->y = y;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05003541 output->border.top = 0;
3542 output->border.bottom = 0;
3543 output->border.left = 0;
3544 output->border.right = 0;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04003545 output->mm_width = width;
3546 output->mm_height = height;
Scott Moreauccbf29d2012-02-22 14:21:41 -07003547 output->dirty = 1;
3548
Scott Moreau1bad5db2012-08-18 01:04:05 -06003549 if (transform != WL_OUTPUT_TRANSFORM_NORMAL)
3550 output->disable_planes++;
3551
3552 weston_output_transform_init(output, transform);
Scott Moreau429490d2012-06-17 18:10:59 -06003553 weston_output_init_zoom(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003554
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003555 weston_output_move(output, x, y);
Benjamin Franzke78db8482012-04-10 18:35:33 +02003556 weston_output_damage(output);
Benjamin Franzke9c26ff32011-03-15 15:08:41 +01003557
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -04003558 wl_signal_init(&output->frame_signal);
Scott Moreau9d1b1122012-06-08 19:40:53 -06003559 wl_list_init(&output->animation_list);
Casey Dahlin9074db52012-04-19 22:50:09 -04003560 wl_list_init(&output->resource_list);
Benjamin Franzke06286262011-05-06 19:12:33 +02003561
Casey Dahlin58ba1372012-04-19 22:50:08 -04003562 output->id = ffs(~output->compositor->output_id_pool) - 1;
3563 output->compositor->output_id_pool |= 1 << output->id;
3564
Benjamin Franzkeb6879402012-04-10 18:28:54 +02003565 output->global =
3566 wl_display_add_global(c->wl_display, &wl_output_interface,
3567 output, bind_output);
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003568}
3569
Benjamin Franzke315b3dc2011-03-08 11:32:57 +01003570static void
Kristian Høgsberga8873122011-11-23 10:39:34 -05003571compositor_bind(struct wl_client *client,
3572 void *data, uint32_t version, uint32_t id)
3573{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003574 struct weston_compositor *compositor = data;
Kristian Høgsberga8873122011-11-23 10:39:34 -05003575
3576 wl_client_add_object(client, &wl_compositor_interface,
3577 &compositor_interface, id, compositor);
3578}
3579
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003580static void
Martin Minarikf12c2872012-06-11 00:57:39 +02003581log_uname(void)
3582{
3583 struct utsname usys;
3584
3585 uname(&usys);
3586
3587 weston_log("OS: %s, %s, %s, %s\n", usys.sysname, usys.release,
3588 usys.version, usys.machine);
3589}
3590
3591static void
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003592log_extensions(const char *name, const char *extensions)
3593{
3594 const char *p, *end;
3595 int l;
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003596 int len;
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003597
3598 l = weston_log("%s:", name);
3599 p = extensions;
3600 while (*p) {
3601 end = strchrnul(p, ' ');
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003602 len = end - p;
3603 if (l + len > 78)
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003604 l = weston_log_continue("\n" STAMP_SPACE "%.*s",
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003605 len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003606 else
Pekka Paalanenf1fc10a2012-08-06 14:57:06 +03003607 l += weston_log_continue(" %.*s", len, p);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003608 for (p = end; isspace(*p); p++)
3609 ;
3610 }
3611 weston_log_continue("\n");
3612}
3613
Pekka Paalanen4e4167d2012-06-11 14:06:05 +03003614static void
3615log_egl_gl_info(EGLDisplay egldpy)
3616{
3617 const char *str;
3618
3619 str = eglQueryString(egldpy, EGL_VERSION);
3620 weston_log("EGL version: %s\n", str ? str : "(null)");
3621
3622 str = eglQueryString(egldpy, EGL_VENDOR);
3623 weston_log("EGL vendor: %s\n", str ? str : "(null)");
3624
3625 str = eglQueryString(egldpy, EGL_CLIENT_APIS);
3626 weston_log("EGL client APIs: %s\n", str ? str : "(null)");
3627
3628 str = eglQueryString(egldpy, EGL_EXTENSIONS);
3629 log_extensions("EGL extensions", str ? str : "(null)");
3630
3631 str = (char *)glGetString(GL_VERSION);
3632 weston_log("GL version: %s\n", str ? str : "(null)");
3633
3634 str = (char *)glGetString(GL_SHADING_LANGUAGE_VERSION);
3635 weston_log("GLSL version: %s\n", str ? str : "(null)");
3636
3637 str = (char *)glGetString(GL_VENDOR);
3638 weston_log("GL vendor: %s\n", str ? str : "(null)");
3639
3640 str = (char *)glGetString(GL_RENDERER);
3641 weston_log("GL renderer: %s\n", str ? str : "(null)");
3642
3643 str = (char *)glGetString(GL_EXTENSIONS);
3644 log_extensions("GL extensions", str ? str : "(null)");
3645}
3646
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003647WL_EXPORT int
Daniel Stonebaf43592012-06-01 11:11:10 -04003648weston_compositor_init(struct weston_compositor *ec,
3649 struct wl_display *display,
3650 int argc,
Daniel Stonec1be8e52012-06-01 11:14:02 -04003651 char *argv[],
3652 const char *config_file)
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003653{
Kristian Høgsbergfbdbbdc2008-11-28 17:06:06 -05003654 struct wl_event_loop *loop;
Daniel Stonee2ef43a2012-06-01 12:14:05 +01003655 struct xkb_rule_names xkb_names;
3656 const struct config_key keyboard_config_keys[] = {
3657 { "keymap_rules", CONFIG_KEY_STRING, &xkb_names.rules },
3658 { "keymap_model", CONFIG_KEY_STRING, &xkb_names.model },
3659 { "keymap_layout", CONFIG_KEY_STRING, &xkb_names.layout },
3660 { "keymap_variant", CONFIG_KEY_STRING, &xkb_names.variant },
3661 { "keymap_options", CONFIG_KEY_STRING, &xkb_names.options },
3662 };
3663 const struct config_section cs[] = {
3664 { "keyboard",
3665 keyboard_config_keys, ARRAY_LENGTH(keyboard_config_keys) },
3666 };
3667
3668 memset(&xkb_names, 0, sizeof(xkb_names));
3669 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), ec);
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003670
Kristian Høgsbergf9212892008-10-11 18:40:23 -04003671 ec->wl_display = display;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04003672 wl_signal_init(&ec->destroy_signal);
3673 wl_signal_init(&ec->activate_signal);
3674 wl_signal_init(&ec->lock_signal);
3675 wl_signal_init(&ec->unlock_signal);
Jan Arne Petersen42feced2012-06-21 21:52:17 +02003676 wl_signal_init(&ec->show_input_panel_signal);
3677 wl_signal_init(&ec->hide_input_panel_signal);
Benjamin Franzkebfeda132012-01-30 14:04:04 +01003678 ec->launcher_sock = weston_environment_get_fd("WESTON_LAUNCHER_SOCK");
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04003679
Casey Dahlin58ba1372012-04-19 22:50:08 -04003680 ec->output_id_pool = 0;
3681
Kristian Høgsberga8873122011-11-23 10:39:34 -05003682 if (!wl_display_add_global(display, &wl_compositor_interface,
3683 ec, compositor_bind))
3684 return -1;
Kristian Høgsbergee02ca62008-12-21 23:37:12 -05003685
Daniel Stone725c2c32012-06-22 14:04:36 +01003686 wl_list_init(&ec->surface_list);
3687 wl_list_init(&ec->layer_list);
3688 wl_list_init(&ec->seat_list);
3689 wl_list_init(&ec->output_list);
3690 wl_list_init(&ec->key_binding_list);
3691 wl_list_init(&ec->button_binding_list);
3692 wl_list_init(&ec->axis_binding_list);
3693 wl_list_init(&ec->fade.animation.link);
3694
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003695 weston_plane_init(&ec->primary_plane, 0, 0);
3696
Daniel Stone725c2c32012-06-22 14:04:36 +01003697 weston_compositor_xkb_init(ec, &xkb_names);
3698
3699 ec->ping_handler = NULL;
3700
3701 screenshooter_create(ec);
3702 text_cursor_position_notifier_create(ec);
Daniel Stone725c2c32012-06-22 14:04:36 +01003703
3704 wl_data_device_manager_init(ec->wl_display);
3705
Kristian Høgsberg9629fe32012-03-26 15:56:39 -04003706 wl_display_init_shm(display);
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003707
Daniel Stone725c2c32012-06-22 14:04:36 +01003708 loop = wl_display_get_event_loop(ec->wl_display);
3709 ec->idle_source = wl_event_loop_add_timer(loop, idle_handler, ec);
3710 wl_event_source_timer_update(ec->idle_source, ec->idle_time * 1000);
3711
3712 ec->input_loop = wl_event_loop_create();
3713
3714 return 0;
3715}
3716
3717WL_EXPORT int
3718weston_compositor_init_gl(struct weston_compositor *ec)
3719{
3720 const char *extensions;
Rob Clarke3b95912012-08-31 16:42:18 -05003721 int has_egl_image_external = 0;
Daniel Stone725c2c32012-06-22 14:04:36 +01003722
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003723 log_egl_gl_info(ec->egl_display);
Kristian Høgsbergfc9c5e02012-06-08 16:45:33 -04003724
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003725 ec->image_target_texture_2d =
3726 (void *) eglGetProcAddress("glEGLImageTargetTexture2DOES");
3727 ec->image_target_renderbuffer_storage = (void *)
3728 eglGetProcAddress("glEGLImageTargetRenderbufferStorageOES");
3729 ec->create_image = (void *) eglGetProcAddress("eglCreateImageKHR");
3730 ec->destroy_image = (void *) eglGetProcAddress("eglDestroyImageKHR");
3731 ec->bind_display =
3732 (void *) eglGetProcAddress("eglBindWaylandDisplayWL");
3733 ec->unbind_display =
3734 (void *) eglGetProcAddress("eglUnbindWaylandDisplayWL");
Gwenole Beauchesne28f59b02012-04-20 11:44:06 +02003735 ec->query_buffer =
3736 (void *) eglGetProcAddress("eglQueryWaylandBufferWL");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003737
3738 extensions = (const char *) glGetString(GL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003739 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003740 weston_log("Retrieving GL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003741 return -1;
3742 }
3743
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003744 if (!strstr(extensions, "GL_EXT_texture_format_BGRA8888")) {
Martin Minarik6d118362012-06-07 18:01:59 +02003745 weston_log("GL_EXT_texture_format_BGRA8888 not available\n");
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003746 return -1;
3747 }
3748
Pekka Paalanena1d57db2012-04-17 15:02:08 +03003749 if (strstr(extensions, "GL_EXT_read_format_bgra"))
3750 ec->read_format = GL_BGRA_EXT;
3751 else
3752 ec->read_format = GL_RGBA;
Kristian Høgsbergf9247dd2012-03-27 15:25:46 -04003753
Pekka Paalanen52bfbaa2012-04-11 16:19:37 +03003754 if (strstr(extensions, "GL_EXT_unpack_subimage"))
3755 ec->has_unpack_subimage = 1;
Kristian Høgsbergfb6de222012-03-27 17:10:13 -04003756
Rob Clarke3b95912012-08-31 16:42:18 -05003757 if (strstr(extensions, "GL_OES_EGL_image_external"))
3758 has_egl_image_external = 1;
3759
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003760 extensions =
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003761 (const char *) eglQueryString(ec->egl_display, EGL_EXTENSIONS);
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003762 if (!extensions) {
Martin Minarik6d118362012-06-07 18:01:59 +02003763 weston_log("Retrieving EGL extension string failed.\n");
Pekka Paalanen299e58d2012-04-12 14:45:35 +03003764 return -1;
3765 }
3766
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04003767 if (strstr(extensions, "EGL_WL_bind_wayland_display"))
3768 ec->has_bind_display = 1;
3769 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04003770 ec->bind_display(ec->egl_display, ec->wl_display);
Kristian Høgsberg3d5bae02010-10-06 21:17:40 -04003771
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003772 weston_spring_init(&ec->fade.spring, 30.0, 1.0, 1.0);
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003773 ec->fade.animation.frame = fade_frame;
Kristian Høgsberg3555d092011-04-11 13:58:13 -04003774
Kristian Høgsberg3be2ce92012-02-29 12:42:35 -05003775 weston_layer_init(&ec->fade_layer, &ec->layer_list);
3776 weston_layer_init(&ec->cursor_layer, &ec->fade_layer.link);
3777
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003778 glActiveTexture(GL_TEXTURE0);
Kristian Høgsberg7ffc4482011-04-22 14:23:51 -04003779
Gwenole Beauchesne6d030492012-04-20 11:15:51 +02003780 if (weston_shader_init(&ec->texture_shader_rgba,
3781 vertex_shader, texture_fragment_shader_rgba) < 0)
Kristian Høgsberga9468212010-06-14 21:03:11 -04003782 return -1;
Pekka Paalanen6b5585b2012-08-30 16:47:19 -05003783 if (weston_shader_init(&ec->texture_shader_rgbx,
3784 vertex_shader, texture_fragment_shader_rgbx) < 0)
3785 return -1;
Rob Clarke3b95912012-08-31 16:42:18 -05003786 if (has_egl_image_external &&
3787 weston_shader_init(&ec->texture_shader_egl_external,
3788 vertex_shader, texture_fragment_shader_egl_external) < 0)
3789 return -1;
Gwenole Beauchesnefaf91852012-04-20 11:22:16 +02003790 if (weston_shader_init(&ec->texture_shader_y_uv,
3791 vertex_shader, texture_fragment_shader_y_uv) < 0)
3792 return -1;
3793 if (weston_shader_init(&ec->texture_shader_y_u_v,
3794 vertex_shader, texture_fragment_shader_y_u_v) < 0)
3795 return -1;
3796 if (weston_shader_init(&ec->texture_shader_y_xuxv,
3797 vertex_shader, texture_fragment_shader_y_xuxv) < 0)
3798 return -1;
Pekka Paalanenf55f5442012-02-02 16:49:05 +02003799 if (weston_shader_init(&ec->solid_shader,
3800 vertex_shader, solid_fragment_shader) < 0)
Kristian Høgsbergcce1aec2011-04-22 15:38:14 -04003801 return -1;
Kristian Høgsberg8d7ca6b2008-11-09 00:22:51 -05003802
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003803 weston_compositor_schedule_repaint(ec);
Kristian Høgsbergef7a9ca2008-10-11 21:21:39 -04003804
Kristian Høgsbergce5325d2010-06-14 11:54:00 -04003805 return 0;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003806}
3807
Benjamin Franzkeb8263022011-08-30 11:32:47 +02003808WL_EXPORT void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003809weston_compositor_shutdown(struct weston_compositor *ec)
Matt Roper361d2ad2011-08-29 13:52:23 -07003810{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003811 struct weston_output *output, *next;
Matt Roper361d2ad2011-08-29 13:52:23 -07003812
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003813 wl_event_source_remove(ec->idle_source);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003814 if (ec->input_loop_source)
3815 wl_event_source_remove(ec->input_loop_source);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003816
Matt Roper361d2ad2011-08-29 13:52:23 -07003817 /* Destroy all outputs associated with this compositor */
Tiago Vignattib303a1d2011-12-18 22:27:40 +02003818 wl_list_for_each_safe(output, next, &ec->output_list, link)
Matt Roper361d2ad2011-08-29 13:52:23 -07003819 output->destroy(output);
Pekka Paalanen4738f3b2012-01-02 15:47:07 +02003820
Daniel Stone325fc2d2012-05-30 16:31:58 +01003821 weston_binding_list_destroy_all(&ec->key_binding_list);
3822 weston_binding_list_destroy_all(&ec->button_binding_list);
3823 weston_binding_list_destroy_all(&ec->axis_binding_list);
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003824
Kristian Høgsberg65a11e12012-08-03 11:30:18 -04003825 weston_plane_release(&ec->primary_plane);
3826
Pekka Paalanend1591ae2012-01-02 16:06:56 +02003827 wl_array_release(&ec->vertices);
3828 wl_array_release(&ec->indices);
Rob Clark0e5a2d02012-08-30 16:47:18 -05003829 wl_array_release(&ec->vtxcnt);
Jonas Ådahlc97af922012-03-28 22:36:09 +02003830
3831 wl_event_loop_destroy(ec->input_loop);
Matt Roper361d2ad2011-08-29 13:52:23 -07003832}
3833
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003834static int on_term_signal(int signal_number, void *data)
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003835{
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003836 struct wl_display *display = data;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003837
Martin Minarik6d118362012-06-07 18:01:59 +02003838 weston_log("caught signal %d\n", signal_number);
Kristian Høgsberg6bded3f2011-08-12 14:55:07 -04003839 wl_display_terminate(display);
Kristian Høgsbergb1868472011-04-22 12:27:57 -04003840
3841 return 1;
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003842}
3843
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003844static void
3845on_segv_signal(int s, siginfo_t *siginfo, void *context)
3846{
3847 void *buffer[32];
3848 int i, count;
3849 Dl_info info;
3850
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003851 /* This SIGSEGV handler will do a best-effort backtrace, and
3852 * then call the backend restore function, which will switch
3853 * back to the vt we launched from or ungrab X etc and then
3854 * raise SIGTRAP. If we run weston under gdb from X or a
3855 * different vt, and tell gdb "handle SIGSEGV nostop", this
3856 * will allow weston to switch back to gdb on crash and then
3857 * gdb will catch the crash with SIGTRAP. */
3858
Martin Minarik6d118362012-06-07 18:01:59 +02003859 weston_log("caught segv\n");
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003860
3861 count = backtrace(buffer, ARRAY_LENGTH(buffer));
3862 for (i = 0; i < count; i++) {
3863 dladdr(buffer[i], &info);
Martin Minarik6d118362012-06-07 18:01:59 +02003864 weston_log(" [%016lx] %s (%s)\n",
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003865 (long) buffer[i],
3866 info.dli_sname ? info.dli_sname : "--",
3867 info.dli_fname);
3868 }
3869
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04003870 segv_compositor->restore(segv_compositor);
3871
3872 raise(SIGTRAP);
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003873}
3874
3875
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003876static void *
3877load_module(const char *name, const char *entrypoint, void **handle)
3878{
3879 char path[PATH_MAX];
3880 void *module, *init;
3881
3882 if (name[0] != '/')
Chad Versacebf381902012-05-23 23:42:15 -07003883 snprintf(path, sizeof path, "%s/%s", MODULEDIR, name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003884 else
Benjamin Franzkeb7acce62011-05-06 23:19:22 +02003885 snprintf(path, sizeof path, "%s", name);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003886
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003887 weston_log("Loading module '%s'\n", path);
Kristian Høgsberg1acd9f82012-07-26 11:39:26 -04003888 module = dlopen(path, RTLD_NOW);
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003889 if (!module) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003890 weston_log("Failed to load module: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003891 return NULL;
3892 }
3893
3894 init = dlsym(module, entrypoint);
3895 if (!init) {
Pekka Paalanen1b3c1ea2012-06-11 14:06:04 +03003896 weston_log("Failed to lookup init function: %s\n", dlerror());
Kristian Høgsberg1c562182011-05-02 22:09:20 -04003897 return NULL;
3898 }
3899
3900 return init;
3901}
3902
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003903static const char xdg_error_message[] =
Martin Minarik37032f82012-06-18 20:15:18 +02003904 "fatal: environment variable XDG_RUNTIME_DIR is not set.\n";
3905
3906static const char xdg_wrong_message[] =
3907 "fatal: environment variable XDG_RUNTIME_DIR\n"
3908 "is set to \"%s\", which is not a directory.\n";
3909
3910static const char xdg_wrong_mode_message[] =
3911 "warning: XDG_RUNTIME_DIR \"%s\" is not configured\n"
3912 "correctly. Unix access mode must be 0700 but is %o,\n"
3913 "and XDG_RUNTIME_DIR must be owned by the user, but is\n"
Kristian Høgsberg30334252012-06-20 14:24:21 -04003914 "owned by UID %d.\n";
Martin Minarik37032f82012-06-18 20:15:18 +02003915
3916static const char xdg_detail_message[] =
Pekka Paalanen78a0b572012-06-06 16:59:44 +03003917 "Refer to your distribution on how to get it, or\n"
3918 "http://www.freedesktop.org/wiki/Specifications/basedir-spec\n"
3919 "on how to implement it.\n";
3920
Martin Minarik37032f82012-06-18 20:15:18 +02003921static void
3922verify_xdg_runtime_dir(void)
3923{
3924 char *dir = getenv("XDG_RUNTIME_DIR");
3925 struct stat s;
3926
3927 if (!dir) {
3928 weston_log(xdg_error_message);
3929 weston_log_continue(xdg_detail_message);
3930 exit(EXIT_FAILURE);
3931 }
3932
3933 if (stat(dir, &s) || !S_ISDIR(s.st_mode)) {
3934 weston_log(xdg_wrong_message, dir);
3935 weston_log_continue(xdg_detail_message);
3936 exit(EXIT_FAILURE);
3937 }
3938
3939 if ((s.st_mode & 0777) != 0700 || s.st_uid != getuid()) {
3940 weston_log(xdg_wrong_mode_message,
3941 dir, s.st_mode & 0777, s.st_uid);
3942 weston_log_continue(xdg_detail_message);
3943 }
3944}
3945
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04003946static int
3947usage(int error_code)
3948{
3949 fprintf(stderr,
3950 "Usage: weston [OPTIONS]\n\n"
3951 "This is weston version " VERSION ", the Wayland reference compositor.\n"
3952 "Weston supports multiple backends, and depending on which backend is in use\n"
3953 "different options will be accepted.\n\n"
3954
3955
3956 "Core options:\n\n"
3957 " -B, --backend=MODULE\tBackend module, one of drm-backend.so,\n"
3958 "\t\t\t\tx11-backend.so or wayland-backend.so\n"
3959 " -S, --socket=NAME\tName of socket to listen on\n"
3960 " -i, --idle-time=SECS\tIdle time in seconds\n"
3961 " --xserver\t\tEnable X server integration\n"
3962 " --module\t\tLoad the specified module\n"
3963 " --log==FILE\t\tLog to the given file\n"
3964 " -h, --help\t\tThis help message\n\n");
3965
3966 fprintf(stderr,
3967 "Options for drm-backend.so:\n\n"
3968 " --connector=ID\tBring up only this connector\n"
3969 " --seat=SEAT\t\tThe seat that weston should run on\n"
3970 " --tty=TTY\t\tThe tty to use\n"
3971 " --current-mode\tPrefer current KMS mode over EDID preferred mode\n\n");
3972
3973 fprintf(stderr,
3974 "Options for x11-backend.so:\n\n"
3975 " --width=WIDTH\t\tWidth of X window\n"
3976 " --height=HEIGHT\tHeight of X window\n"
3977 " --fullscreen\t\tRun in fullscreen mode\n"
3978 " --output-count=COUNT\tCreate multiple outputs\n"
3979 " --no-input\t\tDont create input devices\n\n");
3980
3981 fprintf(stderr,
3982 "Options for wayland-backend.so:\n\n"
3983 " --width=WIDTH\t\tWidth of Wayland surface\n"
3984 " --height=HEIGHT\tHeight of Wayland surface\n"
3985 " --display=DISPLAY\tWayland display to connect to\n\n");
3986
3987 exit(error_code);
3988}
3989
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003990int main(int argc, char *argv[])
3991{
Pekka Paalanen3ab72692012-06-08 17:27:28 +03003992 int ret = EXIT_SUCCESS;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05003993 struct wl_display *display;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05003994 struct weston_compositor *ec;
Pekka Paalanen51c769f2012-01-02 16:03:26 +02003995 struct wl_event_source *signals[4];
Kristian Høgsberg50dc6982010-12-01 16:43:56 -05003996 struct wl_event_loop *loop;
Kristian Høgsberg0690da62012-01-16 11:53:54 -05003997 struct sigaction segv_action;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04003998 void *shell_module, *backend_module, *xserver_module;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04003999 int (*module_init)(struct weston_compositor *ec);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05004000 struct weston_compositor
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004001 *(*backend_init)(struct wl_display *display,
Daniel Stonec1be8e52012-06-01 11:14:02 -04004002 int argc, char *argv[], const char *config_file);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004003 int i;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004004 char *backend = NULL;
4005 char *shell = NULL;
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004006 char *module = NULL;
Martin Minarik19e6f262012-06-07 13:08:46 +02004007 char *log = NULL;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004008 int32_t idle_time = 300;
Scott Moreaue17f6102012-04-25 10:03:06 -06004009 int32_t xserver = 0;
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004010 int32_t help = 0;
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004011 char *socket_name = NULL;
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004012 char *config_file;
4013
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004014 const struct config_key shell_config_keys[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004015 { "type", CONFIG_KEY_STRING, &shell },
4016 };
4017
Kristian Høgsberg875ab9e2012-03-30 11:52:39 -04004018 const struct config_section cs[] = {
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004019 { "shell",
4020 shell_config_keys, ARRAY_LENGTH(shell_config_keys) },
4021 };
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004022
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004023 const struct weston_option core_options[] = {
4024 { WESTON_OPTION_STRING, "backend", 'B', &backend },
4025 { WESTON_OPTION_STRING, "socket", 'S', &socket_name },
4026 { WESTON_OPTION_INTEGER, "idle-time", 'i', &idle_time },
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004027 { WESTON_OPTION_BOOLEAN, "xserver", 0, &xserver },
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004028 { WESTON_OPTION_STRING, "module", 0, &module },
Martin Minarik19e6f262012-06-07 13:08:46 +02004029 { WESTON_OPTION_STRING, "log", 0, &log },
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004030 { WESTON_OPTION_BOOLEAN, "help", 'h', &help },
Kristian Høgsbergd34912c2011-05-02 10:36:04 -04004031 };
Kristian Høgsbergd6531262008-12-12 11:06:18 -05004032
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004033 argc = parse_options(core_options,
4034 ARRAY_LENGTH(core_options), argc, argv);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004035
Kristian Høgsberg572c2ff2012-07-30 15:41:14 -04004036 if (help)
4037 usage(EXIT_SUCCESS);
4038
Rafal Mielniczuk6e0a7d82012-06-09 15:10:28 +02004039 weston_log_file_open(log);
4040
Pekka Paalanenbce4c2b2012-06-11 14:04:21 +03004041 weston_log("%s\n"
4042 STAMP_SPACE "%s\n"
4043 STAMP_SPACE "Bug reports to: %s\n"
4044 STAMP_SPACE "Build: %s\n",
4045 PACKAGE_STRING, PACKAGE_URL, PACKAGE_BUGREPORT,
Kristian Høgsberg23cf9eb2012-06-11 12:06:30 -04004046 BUILD_ID);
Pekka Paalanen7f4d1a32012-06-11 14:06:03 +03004047 log_uname();
Kristian Høgsberga411c8b2012-06-08 16:16:52 -04004048
Martin Minarik37032f82012-06-18 20:15:18 +02004049 verify_xdg_runtime_dir();
4050
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004051 display = wl_display_create();
4052
Tiago Vignatti2116b892011-08-08 05:52:59 -07004053 loop = wl_display_get_event_loop(display);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004054 signals[0] = wl_event_loop_add_signal(loop, SIGTERM, on_term_signal,
4055 display);
4056 signals[1] = wl_event_loop_add_signal(loop, SIGINT, on_term_signal,
4057 display);
4058 signals[2] = wl_event_loop_add_signal(loop, SIGQUIT, on_term_signal,
4059 display);
Tiago Vignatti2116b892011-08-08 05:52:59 -07004060
4061 wl_list_init(&child_process_list);
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004062 signals[3] = wl_event_loop_add_signal(loop, SIGCHLD, sigchld_handler,
4063 NULL);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004064
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004065 if (!backend) {
4066 if (getenv("WAYLAND_DISPLAY"))
4067 backend = "wayland-backend.so";
4068 else if (getenv("DISPLAY"))
4069 backend = "x11-backend.so";
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004070 else
4071 backend = "drm-backend.so";
Kristian Høgsberg02ec0a52011-04-23 13:04:11 -04004072 }
4073
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004074 config_file = config_file_path("weston.ini");
4075 parse_config_file(config_file, cs, ARRAY_LENGTH(cs), shell);
Tiago Vignatti9a206c42012-03-21 19:49:18 +02004076
Kristian Høgsberg1c562182011-05-02 22:09:20 -04004077 backend_init = load_module(backend, "backend_init", &backend_module);
4078 if (!backend_init)
4079 exit(EXIT_FAILURE);
Kristian Høgsberga9410222011-01-14 17:22:35 -05004080
Daniel Stonec1be8e52012-06-01 11:14:02 -04004081 ec = backend_init(display, argc, argv, config_file);
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004082 if (ec == NULL) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004083 weston_log("fatal: failed to create compositor\n");
Kristian Høgsberg841883b2008-12-05 11:19:56 -05004084 exit(EXIT_FAILURE);
4085 }
Kristian Høgsberg61a82512010-10-26 11:26:44 -04004086
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004087 segv_action.sa_flags = SA_SIGINFO | SA_RESETHAND;
4088 segv_action.sa_sigaction = on_segv_signal;
4089 sigemptyset(&segv_action.sa_mask);
4090 sigaction(SIGSEGV, &segv_action, NULL);
4091 segv_compositor = ec;
4092
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004093 for (i = 1; argv[i]; i++)
Pekka Paalanen33616392012-07-30 16:56:57 +03004094 weston_log("fatal: unhandled option: %s\n", argv[i]);
4095 if (argv[1]) {
4096 ret = EXIT_FAILURE;
4097 goto out;
4098 }
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004099
Daniel Stonec1be8e52012-06-01 11:14:02 -04004100 free(config_file);
Daniel Stone855028f2012-05-01 20:37:10 +01004101
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004102 ec->option_idle_time = idle_time;
4103 ec->idle_time = idle_time;
Pekka Paalanen7296e792011-12-07 16:22:00 +02004104
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004105 module_init = NULL;
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004106 if (xserver)
Tiago Vignatti629ce232012-05-23 23:04:14 +03004107 module_init = load_module("xwayland.so",
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004108 "weston_xserver_init",
4109 &xserver_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004110 if (module_init && module_init(ec) < 0) {
4111 ret = EXIT_FAILURE;
4112 goto out;
4113 }
Kristian Høgsbergd012e9d2012-04-12 10:37:23 -04004114
Kristian Høgsberg33d75092012-08-13 13:56:03 -04004115 if (socket_name)
4116 setenv("WAYLAND_DISPLAY", socket_name, 1);
4117
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004118 if (!shell)
4119 shell = "desktop-shell.so";
4120 module_init = load_module(shell, "shell_init", &shell_module);
Pekka Paalanen33616392012-07-30 16:56:57 +03004121 if (!module_init || module_init(ec) < 0) {
4122 ret = EXIT_FAILURE;
4123 goto out;
4124 }
Kristian Høgsberg306e3612012-04-12 12:54:14 -04004125
4126
4127 module_init = NULL;
4128 if (module)
4129 module_init = load_module(module, "module_init", NULL);
Pekka Paalanen33616392012-07-30 16:56:57 +03004130 if (module_init && module_init(ec) < 0) {
4131 ret = EXIT_FAILURE;
4132 goto out;
4133 }
Tiago Vignattie4faa2a2012-04-16 17:31:44 +03004134
Kristian Høgsbergbcacef12012-03-11 21:05:57 -04004135 if (wl_display_add_socket(display, socket_name)) {
Pekka Paalanen33616392012-07-30 16:56:57 +03004136 weston_log("fatal: failed to add socket: %m\n");
4137 ret = EXIT_FAILURE;
4138 goto out;
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004139 }
4140
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02004141 weston_compositor_dpms_on(ec);
Pekka Paalanenc0444e32012-01-05 16:28:21 +02004142 weston_compositor_wake(ec);
Kristian Høgsberg122912c2008-12-05 11:13:50 -05004143
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04004144 wl_display_run(display);
4145
4146 out:
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004147 /* prevent further rendering while shutting down */
Kristian Høgsberg3466bc82012-01-03 11:29:15 -05004148 ec->state = WESTON_COMPOSITOR_SLEEPING;
Pekka Paalanen0135abe2012-01-03 10:01:20 +02004149
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -04004150 wl_signal_emit(&ec->destroy_signal, ec);
Pekka Paalanen3c647232011-12-22 13:43:43 +02004151
Kristian Høgsbergb5819dc2011-04-25 15:08:20 -04004152 if (ec->has_bind_display)
Kristian Høgsberg362b6722012-06-18 15:13:51 -04004153 ec->unbind_display(ec->egl_display, display);
Kristian Høgsberg2bb3ebe2010-12-01 15:36:20 -05004154
Pekka Paalanen51c769f2012-01-02 16:03:26 +02004155 for (i = ARRAY_LENGTH(signals); i;)
4156 wl_event_source_remove(signals[--i]);
4157
Daniel Stone855028f2012-05-01 20:37:10 +01004158 weston_compositor_xkb_destroy(ec);
4159
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004160 ec->destroy(ec);
Tiago Vignatti9e2be082011-12-19 00:04:46 +02004161 wl_display_destroy(display);
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05004162
Martin Minarik19e6f262012-06-07 13:08:46 +02004163 weston_log_file_close();
4164
Pekka Paalanen3ab72692012-06-08 17:27:28 +03004165 return ret;
Kristian Høgsberg16eb6752008-10-08 22:51:32 -04004166}